Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
38 / 38 |
AdminPanel | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
13 | |
100.00% |
38 / 38 |
site | |
100.00% |
1 / 1 |
13 | |
100.00% |
38 / 38 |
1 | <?php |
2 | |
3 | // {{{ License |
4 | |
5 | // This file is part of GNU social - https://www.gnu.org/software/social |
6 | // |
7 | // GNU social is free software: you can redistribute it and/or modify |
8 | // it under the terms of the GNU Affero General Public License as published by |
9 | // the Free Software Foundation, either version 3 of the License, or |
10 | // (at your option) any later version. |
11 | // |
12 | // GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>. |
19 | |
20 | // }}} |
21 | |
22 | /** |
23 | * Handle network public feed |
24 | * |
25 | * @package GNUsocial |
26 | * @category Controller |
27 | * |
28 | * @author Hugo Sales <hugo@hsal.es> |
29 | * @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org |
30 | * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later |
31 | */ |
32 | |
33 | namespace App\Controller; |
34 | |
35 | use App\Core\Controller; |
36 | use App\Core\Form; |
37 | use function App\Core\I18n\_m; |
38 | use App\Util\Common; |
39 | use App\Util\Exception\InvalidFormException; |
40 | use App\Util\Formatting; |
41 | use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
42 | use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
43 | use Symfony\Component\Form\Extension\Core\Type\TextType; |
44 | use Symfony\Component\HttpFoundation\Request; |
45 | |
46 | class AdminPanel extends Controller |
47 | { |
48 | /** |
49 | * Handler for the site admin panel section. Allows the |
50 | * administrator to change various configuration options |
51 | */ |
52 | public function site(Request $request) |
53 | { |
54 | // TODO CHECK PERMISSION |
55 | $defaults = Common::getConfigDefaults(); |
56 | $options = []; |
57 | foreach ($defaults as $key => $inner) { |
58 | $options[$key] = []; |
59 | foreach (array_keys($inner) as $inner_key) { |
60 | $options[_m($key)][_m($inner_key)] = "{$key}:{$inner_key}"; |
61 | } |
62 | } |
63 | |
64 | $form = Form::create([ |
65 | ['setting', ChoiceType::class, ['label' => _m('Setting'), 'choices' => $options]], |
66 | ['value', TextType::class, ['label' => _m('Value')]], |
67 | ['save_admin', SubmitType::class, ['label' => _m('Set site setting')]], |
68 | ]); |
69 | |
70 | $form->handleRequest($request); |
71 | if ($form->isSubmitted()) { |
72 | $data = $form->getData(); |
73 | if ($form->isValid() && array_key_exists('setting', $data)) { |
74 | [$section, $setting] = explode(':', $data['setting']); |
75 | if (!isset($defaults[$section]) && !isset($defaults[$section][$setting])) { |
76 | // @codeCoverageIgnoreStart |
77 | throw new ClientException(_m('The supplied field doesn\'t exist')); |
78 | // @codeCoverageIgnoreEnd |
79 | } |
80 | |
81 | foreach ([ |
82 | 'int' => FILTER_VALIDATE_INT, |
83 | 'bool' => FILTER_VALIDATE_BOOL, |
84 | 'string' => [fn ($v) => strstr($v, ',') === false, fn ($v) => $v], |
85 | 'array' => [fn ($v) => strstr($v, ',') !== false, function ($v) { Formatting::toArray($v, $v); return $v; }], |
86 | ] as $type => $validator) { |
87 | if (!is_array($validator)) { |
88 | $value = filter_var($data['value'], $validator, FILTER_NULL_ON_FAILURE); |
89 | if ($value !== null) { |
90 | break; |
91 | } |
92 | } else { |
93 | [$check, $convert] = $validator; |
94 | if ($check($data['value'])) { |
95 | $value = $convert($data['value']); |
96 | } |
97 | } |
98 | } |
99 | |
100 | $default = $defaults[$section][$setting]; |
101 | |
102 | // Sanity check |
103 | if (gettype($default) === gettype($value)) { |
104 | $old_value = Common::config($section, $setting); |
105 | Common::setConfig($section, $setting, $value); |
106 | return [ |
107 | '_template' => 'config/admin.html.twig', |
108 | 'form' => $form->createView(), |
109 | 'old_value' => Formatting::toString($old_value), |
110 | 'default' => Formatting::toString($default), |
111 | ]; |
112 | } |
113 | } else { |
114 | // @codeCoverageIgnoreStart |
115 | throw new InvalidFormException(); |
116 | // @codeCoverageIgnoreEnd |
117 | } |
118 | } |
119 | |
120 | return [ |
121 | '_template' => 'config/admin.html.twig', |
122 | 'form' => $form->createView(), |
123 | ]; |
124 | } |
125 | } |