1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
|
<?php
# Lifter010: TODO
/**
* plugin.php - plugin administration controller
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Elmar Ludwig
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @package admin
*/
class Admin_PluginController extends AuthenticatedController
{
private $plugin_admin;
/**
* Common tasks for all actions.
*/
public function before_filter(&$action, &$args)
{
global $perm;
parent::before_filter($action, $args);
// user must have root permission
$perm->check('root');
// set page title and navigation
PageLayout::setTitle(_('Verwaltung von Plugins'));
Navigation::activateItem('/admin/config/plugins');
$this->plugin_admin = new PluginAdministration();
if (Request::int('reset_filter')) {
$GLOBALS['user']->cfg->delete('PLUGINADMIN_DISPLAY_SETTINGS');
}
// Extract display settings
$settings = $current = $GLOBALS['user']->cfg->PLUGINADMIN_DISPLAY_SETTINGS;
foreach ((array)$settings as $key => $value) {
$settings[$key] = Request::get($key, $settings[$key]) ?: null;
}
if ($settings !== $current) {
$GLOBALS['user']->cfg->store('PLUGINADMIN_DISPLAY_SETTINGS', $settings);
}
$this->plugin_filter = $settings['plugin_filter'];
$this->core_filter = $settings['core_filter'];
$views = Sidebar::get()->addWidget(new ViewsWidget());
$views->addLink(
_('Pluginverwaltung'),
$this->indexURL()
)->setActive($action === 'index');
$views->addLink(
_('Weitere Plugins installieren'),
$this->searchURL()
)->setActive($action === 'search');
$views->addLink(
_('Vorhandene Plugins registrieren'),
$this->unregisteredURL()
)->setActive($action === 'unregistered');
}
/**
* Validate ticket (passed via request environment).
* This method always checks Request::quoted('ticket').
*
* @throws InvalidArgumentException if ticket is not valid
*/
private function check_ticket()
{
if (!check_ticket(Request::option('studip_ticket'))) {
throw new InvalidArgumentException(_('Das Ticket für diese Aktion ist ungültig.'));
}
}
/**
* Try to get update information for a list of plugins. If no
* update information is available, an error message is set in
* this controller and an empty array is returned.
*
* @param array $plugins array of plugin meta data
*/
private function get_update_info($plugins)
{
try {
return $this->plugin_admin->getUpdateInfo($plugins);
} catch (Exception $ex) {
PageLayout::postError(
_('Informationen über Plugin-Updates sind nicht verfügbar.'),
[$ex->getMessage()]
);
// Read current information from local files
$update_info = [];
$plugin_manager = PluginManager::getInstance();
foreach ($plugins as $plugin) {
$plugin_path = Config::get()->PLUGINS_PATH . '/' . $plugin['path'];
$manifest = $plugin_manager->getPluginManifest($plugin_path);
$update_info[$plugin['id']] = ['version' => $manifest['version']];
}
return $update_info;
}
}
/**
* Display the list of installed plugins and show all available
* updates (if any).
*/
public function index_action()
{
// Check if an activation error has been flashed from the last request
if (isset($this->flash['activation-error'])) {
PageLayout::postError(
$this->get_template_factory()->render(
'admin/plugin/activation-error-form.php',
$this->flash['activation-error'] + ['controller' => $this]
)
);
}
$plugin_manager = PluginManager::getInstance();
$plugins = $plugin_manager->getPluginInfos($this->plugin_filter);
if ($this->core_filter && $this->core_filter !== 'yes') {
$plugins = array_filter($plugins, function ($plugin) {
return ($this->core_filter === 'no' && !$plugin['core'])
|| ($this->core_filter === 'only' && $plugin['core']);
});
}
$this->plugins = $plugins;
$this->plugin_types = $this->plugin_admin->getPluginTypes();
$this->update_info = $this->get_update_info($this->plugins);
$this->migrations = $this->plugin_admin->getMigrationInfo();
$this->num_updates = 0;
foreach ($this->update_info as $id => $info) {
if (isset($info['update']) && !$this->plugins[$id]['depends']) {
$this->num_updates += 1;
}
}
}
/**
* Save the modified plugin configuration (status and position).
*/
public function save_action()
{
$plugin_manager = PluginManager::getInstance();
$plugin_filter = Request::option('plugin_filter', '');
$type = $plugin_filter != '' ? $plugin_filter : NULL;
$plugins = $plugin_manager->getPluginInfos($type);
$force = (bool) Request::int('force');
$this->check_ticket();
// update enabled/disabled status and position if set
$messages = [];
$errors = [];
$memory = [];
foreach ($plugins as $plugin){
// Skip plugins that are currently not visible due to filter settings
if (!Request::submittedSome('position_' . $plugin['id'], 'enabled_' . $plugin['id'])) {
continue;
}
$enabled = Request::int('enabled_' . $plugin['id'], 0);
$navpos = Request::int('position_' . $plugin['id']);
$result = $plugin_manager->setPluginEnabled($plugin['id'], $enabled, $force);
if ($result === false) {
$error = $enabled
? _('Plugin "%s" hat die Aktivierung verhindert')
: _('Plugin "%s" hat die Deaktivierung verhindert');
$errors[$plugin['id']] = sprintf($error, $plugin['name']);
$memory[$plugin['id']] = $enabled;
} elseif ($result === true) {
$message = $enabled
? _('Plugin "%s" wurde aktiviert')
: _('Plugin "%s" wurde deaktiviert');
$messages[] = sprintf($message, $plugin['name']);
}
if (isset($navpos)) {
$result = $plugin_manager->setPluginPosition($plugin['id'], max($navpos, 1));
if ($result) {
$messages[] = sprintf(
_('Die Position von Plugin "%s" wurde verändert.'),
$plugin['name']
);
}
}
}
if (count($errors) > 0) {
// Unfortunately, we need to flash this since it needs a fresh
// ticket (the current one is invalid due to the redirection)
$this->flash['activation-error'] = compact('memory', 'errors');
}
if (count($messages) > 0) {
PageLayout::postSuccess(
_('Die folgenden Änderungen wurden durchgeführt:'),
array_map('htmlReady', $messages)
);
}
$this->redirect('admin/plugin?plugin_filter=' . $plugin_filter);
}
/**
* Compare two plugins by their score (used for sorting).
*/
private function compare_score($plugin1, $plugin2)
{
return $plugin2['score'] - $plugin1['score'];
}
/**
* Search the list of available plugins or display the most
* recommended plugins if the user did not trigger a search.
*/
public function search_action()
{
Helpbar::Get()->addPlainText(_('Empfohlene Plugins'), _('In der Liste "Empfohlene Plugins" finden Sie von anderen Betreibern empfohlene Plugins.'), Icon::create('info'));
Helpbar::Get()->addPlainText(_('Upload'), _('Alternativ können Plugins und Plugin-Updates auch als ZIP-Datei hochgeladen werden.'), Icon::create('info'));
$search = Request::int('reset-search')
? null
: Request::get('search');
// search for plugins in all repositories
try {
$repository = new PluginRepository();
$search_results = $repository->getPlugins($search);
} catch (Exception $ex) {
$search_results = [];
}
$plugins = PluginManager::getInstance()->getPluginInfos();
// filter out already installed plugins
foreach ($plugins as $plugin) {
if (isset($search_results[$plugin['name']])) {
unset($search_results[$plugin['name']]);
}
}
if ($search === null) {
// sort plugins by score
uasort($search_results, [$this, 'compare_score']);
$search_results = array_slice($search_results, 0, 6);
} else {
// sort plugins by name
uksort($search_results, 'strnatcasecmp');
}
$this->search = $search;
$this->search_results = $search_results;
$this->plugins = $plugins;
$search_widget = Sidebar::get()->addWidget(new SearchWidget());
$search_widget->setTitle(_('Plugins suchen'));
$search_widget->addNeedle(_('Pluginname'), 'search', true, null, null, $search);
$links = Sidebar::get()->addWidget(new LinksWidget());
$links->setTitle(_('Verweise'));
$links->addLink(
_('Alle Plugins im Plugin-Marktplatz'),
'https://plugins.studip.de/',
Icon::create('export'),
['target' => '_blank', 'rel' => 'noopener noreferrer']
);
}
/**
* Install a given plugin, either by URL (from the repository)
* or using a file uploaded by the administrator.
*/
public function install_action()
{
$this->check_ticket();
$plugin_manager = PluginManager::getInstance();
$this->flash['plugins_disabled'] = $plugin_manager->isPluginsDisabled();
$this->flash['plugin_url'] = Request::get('plugin_url');
if (isset($_FILES['upload_file'])) {
$upload_file = tempnam(Config::get()->TMP_PATH, 'plugin');
if (move_uploaded_file($_FILES['upload_file']['tmp_name'], $upload_file)) {
$this->flash['upload_file'] = $upload_file;
}
}
$plugin_manager->setPluginsDisabled(true);
$this->redirect('admin/plugin/internal_install');
}
/**
* Install a given plugin, either by URL (from the repository)
* or using a file uploaded by the administrator.
* Note: This action is only called internally via redirect.
*/
public function internal_install_action()
{
$plugin_manager = PluginManager::getInstance();
$plugin_manager->setPluginsDisabled($this->flash['plugins_disabled']);
$plugin_url = $this->flash['plugin_url'];
try {
if (isset($plugin_url)) {
$this->plugin_admin->installPluginFromURL($plugin_url);
} else if (Config::get()->PLUGINS_UPLOAD_ENABLE) {
// process the upload and register plugin in the database
$upload_file = $this->flash['upload_file'];
$this->plugin_admin->installPlugin($upload_file);
}
PageLayout::postSuccess(_('Das Plugin wurde erfolgreich installiert.'));
} catch (PluginInstallationException $ex) {
PageLayout::postError($ex->getMessage());
}
if (isset($upload_file)) {
unlink($upload_file);
}
$this->redirect('admin/plugin');
}
/**
* Ask for confirmation from the user before deleting a plugin.
*
* @param int $plugin_id id of plugin to delete
*/
public function ask_delete_action($plugin_id)
{
$plugin = PluginManager::getInstance()->getPluginInfoById($plugin_id);
if (!$plugin['core']) {
PageLayout::postQuestion(
sprintf(
_('Wollen Sie wirklich "%s" deinstallieren?'),
htmlReady($plugin['name'])
),
$this->url_for("admin/plugin/delete/{$plugin_id}")
)->includeTicket();
}
$this->redirect('admin/plugin');
}
/**
* Completely delete a plugin from the system.
*
* @param int $plugin_id id of plugin to delete
*/
public function delete_action($plugin_id)
{
$plugin_manager = PluginManager::getInstance();
$plugin_filter = Request::option('plugin_filter', '');
$plugin = $plugin_manager->getPluginInfoById($plugin_id);
$this->check_ticket();
if (isset($plugin) && !$plugin['core']) {
$this->plugin_admin->uninstallPlugin($plugin);
PageLayout::postSuccess(sprintf(
_('Das Plugin "%s" wurde deinstalliert.'),
$plugin['name']
));
}
$this->redirect('admin/plugin?plugin_filter=' . $plugin_filter);
}
/**
* Download a ZIP file containing the given plugin.
*
* @param int $plugin_id id of plugin to download
*/
public function download_action($plugin_id)
{
$plugin_manager = PluginManager::getInstance();
$plugin = $plugin_manager->getPluginInfoById($plugin_id);
// prepare file name for download
$pluginpath = Config::get()->PLUGINS_PATH . '/' . $plugin['path'];
$manifest = $plugin_manager->getPluginManifest($pluginpath);
$filename = $plugin['class'] . '-' . $manifest['version'] . '.zip';
$filepath = Config::get()->TMP_PATH . '/' . $filename;
FileArchiveManager::createArchiveFromPhysicalFolder(
$pluginpath,
$filepath
);
$this->render_temporary_file($filepath, $filename, 'application/zip');
}
/**
* Install updates for all selected plugins.
*/
public function install_updates_action()
{
$this->check_ticket();
$plugin_manager = PluginManager::getInstance();
$this->flash['plugins_disabled'] = $plugin_manager->isPluginsDisabled();
$this->flash['plugin_filter'] = Request::option('plugin_filter', '');
$this->flash['update'] = Request::intArray('update');
$plugin_manager->setPluginsDisabled(true);
$this->redirect('admin/plugin/internal_install_updates');
}
/**
* Install updates for all selected plugins.
* Note: This action is only called internally via redirect.
*/
public function internal_install_updates_action()
{
$plugin_manager = PluginManager::getInstance();
$plugin_manager->setPluginsDisabled($this->flash['plugins_disabled']);
$plugins = $plugin_manager->getPluginInfos();
$plugin_filter = $this->flash['plugin_filter'];
$update_info = $this->plugin_admin->getUpdateInfo($plugins);
$update = $this->flash['update'];
if (!empty($update)) { // update each plugin in turn
foreach ($update as $id) {
if (isset($update_info[$id]['update'])) {
try {
$update_url = $update_info[$id]['update']['url'];
$this->plugin_admin->installPluginFromURL($update_url);
} catch (PluginInstallationException $ex) {
$update_errors[] = sprintf('%s: %s', $plugins[$id]['name'], $ex->getMessage());
}
}
}
}
// collect and report errors
if (isset($update_errors)) {
$error = ngettext(
'Beim Update ist ein Fehler aufgetreten:',
'Beim Update sind Fehler aufgetreten:',
count($update_errors)
);
PageLayout::postError($error, $update_errors);
} else {
PageLayout::postSuccess(_('Update erfolgreich installiert.'));
}
$this->redirect('admin/plugin?plugin_filter=' . $plugin_filter);
}
/**
* Show a page describing this plugin's meta data and description,
* if available.
*
* @param int $plugin_id if of plugin to show manifest
*/
public function manifest_action($plugin_id)
{
$plugin_manager = PluginManager::getInstance();
$plugin = $plugin_manager->getPluginInfoById($plugin_id);
PageLayout::setTitle(sprintf(_('Details von %s'), $plugin['name']));
// retrieve manifest
$pluginpath = Config::get()->PLUGINS_PATH . '/' . $plugin['path'];
$manifest = $plugin_manager->getPluginManifest($pluginpath);
$this->plugin = $plugin;
$this->manifest = $manifest;
}
/**
* migrate a plugin to top version
*
* @param int $plugin_id id of plugin to migrate
*/
public function migrate_action($plugin_id)
{
$plugin_manager = PluginManager::getInstance();
$plugin_filter = Request::option('plugin_filter', '');
$plugin = $plugin_manager->getPluginInfoById($plugin_id);
$log = $this->plugin_admin->migratePlugin($plugin_id);
if ($log) {
PageLayout::postMessage(MessageBox::success(_('Die Migration wurde durchgeführt.'), array_map('htmlReady', explode("\n", trim($log)))));
} else {
PageLayout::postMessage(MessageBox::error(_('Die Migration konnte nicht durchgeführt werden.')));
}
$this->redirect('admin/plugin?plugin_filter=' . $plugin_filter);
}
public function unregistered_action()
{
$this->unknown_plugins = $this->plugin_admin->scanPluginDirectory();
}
/**
* register a plugin in database when it
* already exists in file system
*
* @param int $number number of found plugin
*/
public function register_action($number)
{
CSRFProtection::verifyUnsafeRequest();
$unknown_plugins = $this->plugin_admin->scanPluginDirectory();
$plugin = $unknown_plugins[$number];
try {
$this->plugin_admin->registerPlugin($plugin['path']);
PageLayout::postSuccess(_('Das Plugin wurde erfolgreich installiert.'));
} catch (PluginInstallationException $ex) {
PageLayout::postError($ex->getMessage());
}
$this->redirect('admin/plugin');
}
public function edit_automaticupdate_action($plugin_id = null)
{
$this->plugin = $plugin_id
? PluginManager::getInstance()->getPluginInfoById($plugin_id)
: [];
if (Request::isPost()) {
CSRFProtection::verifyUnsafeRequest();
$this->check_ticket();
if (!$plugin_id) {
$plugin_id = $this->plugin_admin->installPluginFromURL(Request::get('automatic_update_url'));
$this->plugin = PluginManager::getInstance()->getPluginInfoById($plugin_id);
}
$token = $this->plugin['automatic_update_secret'] ?: md5(uniqid());
$statement = DBManager::get()->prepare("
UPDATE plugins
SET automatic_update_url = :url,
automatic_update_secret = :secret
WHERE pluginid = :id
");
$statement->execute([
'id' => $plugin_id,
'url' => Request::get('automatic_update_url'),
'secret' => Request::get('use_security_token') ? $token : null
]);
PageLayout::postMessage(MessageBox::success(_('Daten gespeichert.')));
if (Request::get('automatic_update_url') && Request::get('use_security_token')) {
PageLayout::postInfo(_('Unten können Sie den Security Token jetzt heraus kopieren.'));
}
$this->redirect("admin/plugin/edit_automaticupdate/{$plugin_id}");
}
if ($plugin_id) {
PageLayout::setTitle(sprintf(_('Automatisches Update für %s'), $this->plugin['name']));
} else {
PageLayout::setTitle(_('Plugin von URL installieren'));
}
}
public function edit_description_action(Plugin $plugin)
{
$this->plugin = PluginManager::getInstance()->getPluginById($plugin->getId());
$this->metadata = $this->plugin->getMetadata();
$this->form = \Studip\Forms\Form::fromSORM($plugin, [
'legend' => _('Pluginbeschreibung'),
'fields' => [
'description' => [
'label' => _('Beschreibung'),
'type' => 'i18n_formatted'
],
'manifest_info_de' => [
'label' => _('Standardbeschreibung des Plugins'),
'type' => 'info',
'value' => $this->metadata['descriptionlong'] ?? $this->metadata['description'],
'if' => "i18n.description === 'de_DE'"
],
'manifest_info_en' => [
'label' => sprintf(_('Standardbeschreibung des Plugins (%s)'), _('Englisch')),
'type' => 'info',
'value' => $this->metadata['descriptionlong_en'] ?? $this->metadata['description_en'] ?? null,
'if' => "i18n.description === 'en_GB'"
],
'description_mode' => [
'label' => _('Modus der neuen Beschreibung'),
'type' => 'select',
'options' => [
'add' => _('Hinzufügen zur Standardbeschreibung'),
'override_description' => _('Standardbeschreibung überschreiben'),
'replace_all' => _('Beschreibungsfenster komplett ersetzen durch Beschreibung')
]
],
'highlight_until' => [
'label' => _('In Veranstaltungen bewerben bis (oder leer lassen)'),
'type' => 'datetimepicker'
],
'highlight_text' => [
'label' => _('Bewerbungs-Infotext')
]
]
])->autoStore()
//->setDebugMode(true)
->setURL(URLHelper::getURL('dispatch.php/admin/plugin/index'));
}
}
|