Normalize use of form security tokens in Admin modules
[friendica.git/.git] / src / Module / Admin / Addons / Details.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\Content\Text\Markdown;
25 use Friendica\Core\Addon;
26 use Friendica\Core\Renderer;
27 use Friendica\DI;
28 use Friendica\Module\BaseAdmin;
29 use Friendica\Util\Strings;
30
31 class Details extends BaseAdmin
32 {
33         public static function post(array $parameters = [])
34         {
35                 parent::post($parameters);
36
37                 $addon = Strings::sanitizeFilePathItem($parameters['addon']);
38
39                 $redirect = 'admin/addons/' . $addon;
40
41                 if (is_file('addon/' . $addon . '/' . $addon . '.php')) {
42                         include_once 'addon/' . $addon . '/' . $addon . '.php';
43
44                         if (function_exists($addon . '_addon_admin_post')) {
45                                 self::checkFormSecurityTokenRedirectOnError($redirect, 'admin_addons_details');
46
47                                 $func = $addon . '_addon_admin_post';
48                                 $func(DI::app());
49                         }
50                 }
51
52                 DI::baseUrl()->redirect($redirect);
53         }
54
55         public static function content(array $parameters = [])
56         {
57                 parent::content($parameters);
58
59                 $a = DI::app();
60
61                 $addons_admin = Addon::getAdminList();
62
63                 $addon = Strings::sanitizeFilePathItem($parameters['addon']);
64                 if (!is_file("addon/$addon/$addon.php")) {
65                         notice(DI::l10n()->t('Addon not found.'));
66                         Addon::uninstall($addon);
67                         DI::baseUrl()->redirect('admin/addons');
68                 }
69
70                 if (($_GET['action'] ?? '') == 'toggle') {
71                         self::checkFormSecurityTokenRedirectOnError('/admin/addons', 'admin_addons_details', 't');
72
73                         // Toggle addon status
74                         if (Addon::isEnabled($addon)) {
75                                 Addon::uninstall($addon);
76                                 info(DI::l10n()->t('Addon %s disabled.', $addon));
77                         } else {
78                                 Addon::install($addon);
79                                 info(DI::l10n()->t('Addon %s enabled.', $addon));
80                         }
81
82                         DI::baseUrl()->redirect('admin/addons/' . $addon);
83                 }
84
85                 // display addon details
86                 if (Addon::isEnabled($addon)) {
87                         $status = 'on';
88                         $action = DI::l10n()->t('Disable');
89                 } else {
90                         $status = 'off';
91                         $action = DI::l10n()->t('Enable');
92                 }
93
94                 $readme = null;
95                 if (is_file("addon/$addon/README.md")) {
96                         $readme = Markdown::convert(file_get_contents("addon/$addon/README.md"), false);
97                 } elseif (is_file("addon/$addon/README")) {
98                         $readme = '<pre>' . file_get_contents("addon/$addon/README") . '</pre>';
99                 }
100
101                 $admin_form = '';
102                 if (array_key_exists($addon, $addons_admin)) {
103                         require_once "addon/$addon/$addon.php";
104                         $func = $addon . '_addon_admin';
105                         $func($a, $admin_form);
106                 }
107
108                 $t = Renderer::getMarkupTemplate('admin/addons/details.tpl');
109
110                 return Renderer::replaceMacros($t, [
111                         '$title' => DI::l10n()->t('Administration'),
112                         '$page' => DI::l10n()->t('Addons'),
113                         '$toggle' => DI::l10n()->t('Toggle'),
114                         '$settings' => DI::l10n()->t('Settings'),
115                         '$baseurl' => DI::baseUrl()->get(true),
116
117                         '$addon' => $addon,
118                         '$status' => $status,
119                         '$action' => $action,
120                         '$info' => Addon::getInfo($addon),
121                         '$str_author' => DI::l10n()->t('Author: '),
122                         '$str_maintainer' => DI::l10n()->t('Maintainer: '),
123
124                         '$admin_form' => $admin_form,
125                         '$function' => 'addons',
126                         '$screenshot' => '',
127                         '$readme' => $readme,
128
129                         '$form_security_token' => self::getFormSecurityToken('admin_addons_details'),
130                 ]);
131         }
132 }