Normalize use of form security tokens in Admin modules
[friendica.git/.git] / src / Module / Admin / Addons / Index.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Admin\Addons;
23
24 use Friendica\Core\Addon;
25 use Friendica\Core\Renderer;
26 use Friendica\DI;
27 use Friendica\Module\BaseAdmin;
28
29 class Index extends BaseAdmin
30 {
31         public static function content(array $parameters = [])
32         {
33                 parent::content($parameters);
34
35                 // reload active themes
36                 if (!empty($_GET['action'])) {
37                         self::checkFormSecurityTokenRedirectOnError('/admin/addons', 'admin_addons', 't');
38
39                         switch ($_GET['action']) {
40                                 case 'reload':
41                                         Addon::reload();
42                                         info('Addons reloaded');
43                                         break;
44
45                                 case 'toggle' :
46                                         $addon = $_GET['addon'] ?? '';
47                                         if (Addon::isEnabled($addon)) {
48                                                 Addon::uninstall($addon);
49                                                 info(DI::l10n()->t('Addon %s disabled.', $addon));
50                                         } elseif (Addon::install($addon)) {
51                                                 info(DI::l10n()->t('Addon %s enabled.', $addon));
52                                         } else {
53                                                 info(DI::l10n()->t('Addon %s failed to install.', $addon));
54                                         }
55
56                                         break;
57
58                         }
59
60                         DI::baseUrl()->redirect('admin/addons');
61                 }
62
63                 $addons = Addon::getAvailableList();
64
65                 $t = Renderer::getMarkupTemplate('admin/addons/index.tpl');
66                 return Renderer::replaceMacros($t, [
67                         '$title' => DI::l10n()->t('Administration'),
68                         '$page' => DI::l10n()->t('Addons'),
69                         '$submit' => DI::l10n()->t('Save Settings'),
70                         '$reload' => DI::l10n()->t('Reload active addons'),
71                         '$baseurl' => DI::baseUrl()->get(true),
72                         '$function' => 'addons',
73                         '$addons' => $addons,
74                         '$pcount' => count($addons),
75                         '$noplugshint' => DI::l10n()->t('There are currently no addons available on your node. You can find the official addon repository at %1$s and might find other interesting addons in the open addon registry at %2$s', 'https://github.com/friendica/friendica-addons', 'http://addons.friendi.ca'),
76                         '$form_security_token' => self::getFormSecurityToken('admin_addons'),
77                 ]);
78         }
79 }