2020-06-04 21:37:17 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-06-04 21:37:17 +01:00
|
|
|
// {{{ License
|
2020-07-18 03:16:18 +01:00
|
|
|
|
2020-06-04 21:37:17 +01:00
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// GNU social is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
|
2020-07-18 03:16:18 +01:00
|
|
|
|
2020-06-04 21:37:17 +01:00
|
|
|
// }}}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle network public feed
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Controller
|
|
|
|
*
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-06-04 21:37:17 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Controller;
|
|
|
|
|
2020-07-06 21:51:08 +01:00
|
|
|
use App\Core\Controller;
|
2020-06-23 22:24:14 +01:00
|
|
|
use App\Core\Form;
|
2020-06-04 22:53:34 +01:00
|
|
|
use function App\Core\I18n\_m;
|
2020-07-25 15:43:43 +01:00
|
|
|
use App\Util\Common;
|
2021-09-06 17:32:44 +01:00
|
|
|
use App\Util\Exception\ClientException;
|
2021-08-03 18:56:25 +01:00
|
|
|
use App\Util\Exception\InvalidFormException;
|
2020-07-25 15:43:43 +01:00
|
|
|
use App\Util\Formatting;
|
2020-06-04 21:37:17 +01:00
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
|
2020-07-23 15:08:31 +01:00
|
|
|
class AdminPanel extends Controller
|
2020-06-04 21:37:17 +01:00
|
|
|
{
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Handler for the site admin panel section. Allows the
|
|
|
|
* administrator to change various configuration options
|
|
|
|
*/
|
2020-07-23 18:55:06 +01:00
|
|
|
public function site(Request $request)
|
2020-06-04 21:37:17 +01:00
|
|
|
{
|
2022-01-22 15:02:21 +00:00
|
|
|
$this->denyAccessUnlessGranted('ROLE_OPERATOR');
|
2020-09-30 23:57:25 +01:00
|
|
|
$defaults = Common::getConfigDefaults();
|
2020-06-23 22:24:14 +01:00
|
|
|
$options = [];
|
|
|
|
foreach ($defaults as $key => $inner) {
|
2020-06-04 21:37:17 +01:00
|
|
|
$options[$key] = [];
|
|
|
|
foreach (array_keys($inner) as $inner_key) {
|
2020-06-04 22:53:34 +01:00
|
|
|
$options[_m($key)][_m($inner_key)] = "{$key}:{$inner_key}";
|
2020-06-04 21:37:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-23 15:08:31 +01:00
|
|
|
$form = Form::create([
|
2020-07-25 15:43:43 +01:00
|
|
|
['setting', ChoiceType::class, ['label' => _m('Setting'), 'choices' => $options]],
|
|
|
|
['value', TextType::class, ['label' => _m('Value')]],
|
2021-08-18 18:38:54 +01:00
|
|
|
['save_admin', SubmitType::class, ['label' => _m('Set site setting')]],
|
2020-07-23 15:08:31 +01:00
|
|
|
]);
|
2020-06-04 21:37:17 +01:00
|
|
|
|
|
|
|
$form->handleRequest($request);
|
2020-06-23 22:24:14 +01:00
|
|
|
if ($form->isSubmitted()) {
|
2020-06-04 21:37:17 +01:00
|
|
|
$data = $form->getData();
|
2021-10-10 09:26:18 +01:00
|
|
|
if ($form->isValid() && \array_key_exists('setting', $data)) {
|
2021-08-03 18:56:25 +01:00
|
|
|
[$section, $setting] = explode(':', $data['setting']);
|
2021-08-07 19:22:25 +01:00
|
|
|
if (!isset($defaults[$section]) && !isset($defaults[$section][$setting])) {
|
|
|
|
// @codeCoverageIgnoreStart
|
|
|
|
throw new ClientException(_m('The supplied field doesn\'t exist'));
|
|
|
|
// @codeCoverageIgnoreEnd
|
|
|
|
}
|
|
|
|
|
2021-09-06 20:59:36 +01:00
|
|
|
$value = null;
|
2021-08-03 18:56:25 +01:00
|
|
|
foreach ([
|
2021-10-10 09:26:18 +01:00
|
|
|
'int' => \FILTER_VALIDATE_INT,
|
|
|
|
'bool' => \FILTER_VALIDATE_BOOL,
|
|
|
|
'string' => [fn ($v) => mb_strstr($v, ',') === false, fn ($v) => $v],
|
|
|
|
'array' => [fn ($v) => mb_strstr($v, ',') !== false, function ($v) { Formatting::toArray($v, $v); return $v; }],
|
2021-08-03 18:56:25 +01:00
|
|
|
] as $type => $validator) {
|
2021-10-10 09:26:18 +01:00
|
|
|
if (!\is_array($validator)) {
|
|
|
|
$value = filter_var($data['value'], $validator, \FILTER_NULL_ON_FAILURE);
|
2021-08-03 18:56:25 +01:00
|
|
|
if ($value !== null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[$check, $convert] = $validator;
|
|
|
|
if ($check($data['value'])) {
|
|
|
|
$value = $convert($data['value']);
|
|
|
|
}
|
|
|
|
}
|
2020-07-25 15:43:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$default = $defaults[$section][$setting];
|
2021-08-03 18:56:25 +01:00
|
|
|
|
2020-07-23 15:08:31 +01:00
|
|
|
// Sanity check
|
2021-10-10 09:26:18 +01:00
|
|
|
if (\gettype($default) === \gettype($value)) {
|
2020-07-25 15:43:43 +01:00
|
|
|
$old_value = Common::config($section, $setting);
|
|
|
|
Common::setConfig($section, $setting, $value);
|
2020-07-23 15:08:31 +01:00
|
|
|
return [
|
|
|
|
'_template' => 'config/admin.html.twig',
|
|
|
|
'form' => $form->createView(),
|
2020-07-25 15:43:43 +01:00
|
|
|
'old_value' => Formatting::toString($old_value),
|
|
|
|
'default' => Formatting::toString($default),
|
2020-07-23 15:08:31 +01:00
|
|
|
];
|
2020-06-23 22:24:14 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-04 21:03:21 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2020-09-08 01:12:33 +01:00
|
|
|
throw new InvalidFormException();
|
2021-08-04 21:03:21 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-06-23 22:24:14 +01:00
|
|
|
}
|
2020-06-04 21:37:17 +01:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:51:08 +01:00
|
|
|
return [
|
|
|
|
'_template' => 'config/admin.html.twig',
|
|
|
|
'form' => $form->createView(),
|
|
|
|
];
|
2020-06-04 21:37:17 +01:00
|
|
|
}
|
|
|
|
}
|