upstream V3 development https://www.gnusocial.rocks/v3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

130 lines
4.8 KiB

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