Normalize use of form security tokens in Admin modules
[friendica.git/.git] / src / Module / Admin / Features.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;
23
24 use Friendica\Content\Feature;
25 use Friendica\Core\Renderer;
26 use Friendica\DI;
27 use Friendica\Module\BaseAdmin;
28
29 class Features extends BaseAdmin
30 {
31         public static function post(array $parameters = [])
32         {
33                 parent::post($parameters);
34
35                 self::checkFormSecurityTokenRedirectOnError('/admin/features', 'admin_manage_features');
36
37                 $features = Feature::get(false);
38
39                 foreach ($features as $fname => $fdata) {
40                         foreach (array_slice($fdata, 1) as $f) {
41                                 $feature = $f[0];
42                                 $feature_state = 'feature_' . $feature;
43                                 $featurelock = 'featurelock_' . $feature;
44
45                                 if (!empty($_POST[$feature_state])) {
46                                         $val = intval($_POST[$feature_state]);
47                                 } else {
48                                         $val = 0;
49                                 }
50                                 DI::config()->set('feature', $feature, $val);
51
52                                 if (!empty($_POST[$featurelock])) {
53                                         DI::config()->set('feature_lock', $feature, $val);
54                                 } else {
55                                         DI::config()->delete('feature_lock', $feature);
56                                 }
57                         }
58                 }
59
60                 DI::baseUrl()->redirect('admin/features');
61         }
62
63         public static function content(array $parameters = [])
64         {
65                 parent::content($parameters);
66
67                 $features = [];
68
69                 foreach (Feature::get(false) as $fname => $fdata) {
70                         $features[$fname] = [];
71                         $features[$fname][0] = $fdata[0];
72                         foreach (array_slice($fdata, 1) as $f) {
73                                 $set = DI::config()->get('feature', $f[0], $f[3]);
74                                 $features[$fname][1][] = [
75                                         ['feature_' . $f[0], $f[1], $set, $f[2]],
76                                         ['featurelock_' . $f[0], DI::l10n()->t('Lock feature %s', $f[1]), $f[4], '']
77                                 ];
78                         }
79                 }
80
81                 $tpl = Renderer::getMarkupTemplate('admin/features.tpl');
82                 $o = Renderer::replaceMacros($tpl, [
83                         '$form_security_token' => self::getFormSecurityToken("admin_manage_features"),
84                         '$baseurl'             => DI::baseUrl()->get(true),
85                         '$title'               => DI::l10n()->t('Manage Additional Features'),
86                         '$features'            => $features,
87                         '$submit'              => DI::l10n()->t('Save Settings'),
88                 ]);
89
90                 return $o;
91         }
92 }