2020-07-06 21:51:08 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-18 13:17:08 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-07-06 21:51:08 +01:00
|
|
|
// {{{ License
|
|
|
|
// 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/>.
|
|
|
|
// }}}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for controllers
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Controller
|
|
|
|
*
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
2021-10-18 13:17:08 +01:00
|
|
|
* @author Diogo Peralta Cordeiro <@diogo.site>
|
2021-02-19 23:29:43 +00:00
|
|
|
* @copyright 2020-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-07-06 21:51:08 +01:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Core;
|
|
|
|
|
2021-08-08 01:35:27 +01:00
|
|
|
use function App\Core\I18n\_m;
|
2020-09-10 21:38:40 +01:00
|
|
|
use App\Util\Common;
|
2021-12-26 22:17:26 +00:00
|
|
|
use App\Util\Exception\BugFoundException;
|
2021-08-08 01:35:27 +01:00
|
|
|
use App\Util\Exception\ClientException;
|
2020-09-06 22:38:37 +01:00
|
|
|
use App\Util\Exception\RedirectException;
|
2021-11-17 00:49:23 +00:00
|
|
|
use App\Util\Exception\ServerException;
|
2022-01-02 20:04:52 +00:00
|
|
|
use Component\Collection\Util\Controller\FeedController;
|
2021-08-01 12:44:45 +01:00
|
|
|
use Exception;
|
2020-07-06 21:51:08 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
2020-07-23 18:55:06 +01:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2021-09-06 14:29:07 +01:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2020-09-06 22:38:37 +01:00
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse;
|
2020-07-06 21:51:08 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-04-15 23:33:37 +01:00
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
2021-11-17 00:49:23 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2020-07-23 18:55:06 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\ControllerEvent;
|
2020-09-06 22:38:37 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
|
2020-07-23 18:55:06 +01:00
|
|
|
use Symfony\Component\HttpKernel\Event\ViewEvent;
|
|
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
2021-09-06 23:47:28 +01:00
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException;
|
2022-01-03 17:39:28 +00:00
|
|
|
use Throwable;
|
2020-07-06 21:51:08 +01:00
|
|
|
|
2021-09-06 20:59:36 +01:00
|
|
|
/**
|
2022-01-03 20:35:26 +00:00
|
|
|
* @method ?int int(string $param, ?\Throwable $throw = null)
|
|
|
|
* @method ?bool bool(string $param, ?\Throwable $throw = null)
|
|
|
|
* @method ?string string(string $param, ?\Throwable $throw = null)
|
2021-12-26 22:17:26 +00:00
|
|
|
* @method ?string params(string $param)
|
|
|
|
* @method mixed handle(Request $request, mixed ...$extra)
|
2021-09-06 20:59:36 +01:00
|
|
|
*/
|
2021-09-27 10:36:48 +01:00
|
|
|
abstract class Controller extends AbstractController implements EventSubscriberInterface
|
2020-07-06 21:51:08 +01:00
|
|
|
{
|
2021-09-01 23:50:45 +01:00
|
|
|
private array $vars = [];
|
2021-08-24 05:30:54 +01:00
|
|
|
protected ?Request $request = null;
|
2021-04-15 23:33:37 +01:00
|
|
|
|
|
|
|
public function __construct(RequestStack $requestStack)
|
|
|
|
{
|
|
|
|
$this->request = $requestStack->getCurrentRequest();
|
|
|
|
}
|
2020-07-23 18:55:06 +01:00
|
|
|
|
2021-08-08 01:35:27 +01:00
|
|
|
/**
|
|
|
|
* TODO: Not currently used, so not tested, but should be
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2020-07-06 21:51:08 +01:00
|
|
|
public function __invoke(Request $request)
|
|
|
|
{
|
2021-04-15 23:33:37 +01:00
|
|
|
$this->request = $request;
|
2021-09-27 10:36:48 +01:00
|
|
|
$class = static::class;
|
2021-10-18 13:17:08 +01:00
|
|
|
$method = 'on' . ucfirst(mb_strtolower($request->getMethod()));
|
2021-09-27 10:36:48 +01:00
|
|
|
$attributes = array_diff_key($request->attributes->get('_route_params'), array_flip(['_format', '_fragment', '_locale', 'template', 'accept', 'is_system_path']));
|
2020-07-06 21:51:08 +01:00
|
|
|
if (method_exists($class, $method)) {
|
2021-09-27 10:36:48 +01:00
|
|
|
return $this->{$method}($request, ...$attributes);
|
2020-07-06 21:51:08 +01:00
|
|
|
} else {
|
2021-09-27 10:36:48 +01:00
|
|
|
return $this->handle($request, ...$attributes);
|
2020-07-23 18:55:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Symfony event when it's searching for which controller to use
|
|
|
|
*/
|
2020-07-23 18:55:06 +01:00
|
|
|
public function onKernelController(ControllerEvent $event)
|
|
|
|
{
|
|
|
|
$controller = $event->getController();
|
|
|
|
$request = $event->getRequest();
|
|
|
|
|
2021-04-15 23:33:37 +01:00
|
|
|
$this->request = $request;
|
2021-09-18 07:27:17 +01:00
|
|
|
|
|
|
|
$this->vars = ['controller' => $controller, 'request' => $request];
|
|
|
|
$user = Common::user();
|
|
|
|
if ($user !== null) {
|
|
|
|
$this->vars['current_actor'] = $user->getActor();
|
|
|
|
}
|
2020-07-23 18:55:06 +01:00
|
|
|
|
2020-08-15 14:49:38 +01:00
|
|
|
$event->stopPropagation();
|
2020-07-23 18:55:06 +01:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Symfony event when the controller result is not a Response object
|
2021-11-17 00:49:23 +00:00
|
|
|
*
|
|
|
|
* @throws ClientException
|
|
|
|
* @throws ServerException
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2020-07-23 18:55:06 +01:00
|
|
|
public function onKernelView(ViewEvent $event)
|
|
|
|
{
|
|
|
|
$request = $event->getRequest();
|
|
|
|
$response = $event->getControllerResult();
|
2021-10-18 13:17:08 +01:00
|
|
|
if (!\is_array($response)) {
|
2021-08-08 01:35:27 +01:00
|
|
|
// This means it's not one of our custom format responses, nothing to do
|
|
|
|
// @codeCoverageIgnoreStart
|
2020-07-23 18:55:06 +01:00
|
|
|
return $event;
|
2021-08-08 01:35:27 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|
2020-07-23 18:55:06 +01:00
|
|
|
|
|
|
|
$this->vars = array_merge_recursive($this->vars, $response);
|
|
|
|
|
2021-11-17 01:12:36 +00:00
|
|
|
$template = $this->vars['_template'] ?? null;
|
2021-10-28 14:29:34 +01:00
|
|
|
Event::handle('OverrideTemplate', [$this->vars, &$template]); // Allow plugins to replace the template used for anything
|
2021-09-18 07:27:17 +01:00
|
|
|
unset($this->vars['_template'], $response['_template']);
|
2020-07-06 21:51:08 +01:00
|
|
|
|
2022-01-02 03:15:06 +00:00
|
|
|
$redirect = $this->vars['_redirect'] ?? false;
|
|
|
|
|
2021-12-30 18:26:32 +00:00
|
|
|
$controller = $this->vars['controller'];
|
2021-12-08 10:20:37 +00:00
|
|
|
if (\is_array($controller)) {
|
|
|
|
$controller = $controller[0];
|
|
|
|
}
|
|
|
|
if (is_subclass_of($controller, FeedController::class)) {
|
2021-12-30 18:26:32 +00:00
|
|
|
$this->vars = $controller->postProcess($this->vars);
|
2021-12-08 10:20:37 +00:00
|
|
|
}
|
|
|
|
|
2021-08-24 05:30:54 +01:00
|
|
|
// Respond in the most preferred acceptable content type
|
2021-11-17 00:49:23 +00:00
|
|
|
$route = $request->get('_route');
|
2021-12-19 21:05:39 +00:00
|
|
|
$accept = $request->getAcceptableContentTypes() ?: ['text/html']; // Assume html if not specified, */* is considered specified
|
2021-11-17 00:49:23 +00:00
|
|
|
$format = $request->getFormat($accept[0]);
|
2021-09-06 14:29:07 +01:00
|
|
|
$potential_response = null;
|
|
|
|
if (Event::handle('ControllerResponseInFormat', [
|
2021-10-28 18:05:28 +01:00
|
|
|
'route' => $route,
|
2021-09-06 14:29:07 +01:00
|
|
|
'accept_header' => $accept,
|
|
|
|
'vars' => $this->vars,
|
|
|
|
'response' => &$potential_response,
|
2021-09-14 17:15:37 +01:00
|
|
|
]) !== Event::stop) {
|
2021-09-06 14:29:07 +01:00
|
|
|
switch ($format) {
|
|
|
|
case 'json':
|
|
|
|
$event->setResponse(new JsonResponse($response));
|
|
|
|
break;
|
2021-12-19 21:05:39 +00:00
|
|
|
default: // html (assume if not specified)
|
2022-01-02 03:15:06 +00:00
|
|
|
if ($redirect !== false) {
|
|
|
|
$event->setResponse(new RedirectResponse($redirect));
|
|
|
|
} elseif (!\is_null($template)) {
|
2021-10-18 13:17:08 +01:00
|
|
|
$event->setResponse($this->render($template, $this->vars));
|
|
|
|
break;
|
|
|
|
} else {
|
2021-12-19 21:05:39 +00:00
|
|
|
throw new ClientException(_m('Unsupported format: {format}', ['format' => $format]), 406); // 406 Not Acceptable
|
2021-10-18 13:17:08 +01:00
|
|
|
}
|
2021-08-24 05:30:54 +01:00
|
|
|
}
|
2021-09-06 14:29:07 +01:00
|
|
|
} else {
|
2021-10-28 18:05:28 +01:00
|
|
|
if (\is_null($potential_response)) {
|
2021-12-26 22:17:26 +00:00
|
|
|
throw new BugFoundException("ControllerResponseInFormat for route '{$route}' returned Event::stop but didn't provide a response");
|
2021-10-28 18:05:28 +01:00
|
|
|
}
|
2021-12-26 22:17:26 +00:00
|
|
|
$event->setResponse($potential_response); // @phpstan-ignore-line
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|
2020-07-23 18:55:06 +01:00
|
|
|
|
2022-01-02 03:14:27 +00:00
|
|
|
// Set some inoffensive headers to every controller
|
|
|
|
// TODO: If response already has this set, do not reset!
|
|
|
|
$event->getResponse()->headers->set('permissions-policy', 'interest-cohort=()');
|
|
|
|
$event->getResponse()->headers->set('strict-transport-security', 'max-age=15768000; preload;');
|
|
|
|
$event->getResponse()->headers->set('vary', 'Accept-Encoding,Cookie');
|
|
|
|
$event->getResponse()->headers->set('x-frame-options', 'SAMEORIGIN');
|
|
|
|
$event->getResponse()->headers->set('x-xss-protection', '1; mode=block');
|
|
|
|
$policy = "default-src 'self' 'unsafe-inline'; frame-ancestors 'self'; form-action 'self'; style-src 'self' 'unsafe-inline'; img-src * blob: data:;";
|
|
|
|
$event->getResponse()->headers->set('Content-Security-Policy', $policy);
|
|
|
|
$event->getResponse()->headers->set('X-Content-Security-Policy', $policy);
|
|
|
|
$event->getResponse()->headers->set('X-WebKit-CSP', $policy);
|
|
|
|
|
2021-08-22 13:11:46 +01:00
|
|
|
Event::handle('CleanupModule');
|
|
|
|
|
2020-07-23 18:55:06 +01:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2020-11-06 19:47:15 +00:00
|
|
|
/**
|
|
|
|
* Symfony event when the controller throws an exception
|
2021-08-08 01:35:27 +01:00
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2020-11-06 19:47:15 +00:00
|
|
|
*/
|
2020-09-06 22:38:37 +01:00
|
|
|
public function onKernelException(ExceptionEvent $event)
|
|
|
|
{
|
2020-09-10 21:38:40 +01:00
|
|
|
$except = $event->getThrowable();
|
2021-08-03 18:48:17 +01:00
|
|
|
if ($_ENV['APP_ENV'] !== 'dev') {
|
|
|
|
// TODO: This is where our custom exception pages could go
|
|
|
|
// $event->setResponse((new Response())->setStatusCode(455));
|
|
|
|
}
|
2020-09-10 21:38:40 +01:00
|
|
|
do {
|
|
|
|
if ($except instanceof RedirectException) {
|
|
|
|
if (($redir = $except->redirect_response) != null) {
|
|
|
|
$event->setResponse($redir);
|
|
|
|
} else {
|
|
|
|
$event->setResponse(new RedirectResponse($event->getRequest()->getPathInfo()));
|
|
|
|
}
|
2020-09-06 22:38:37 +01:00
|
|
|
}
|
2020-09-10 21:38:40 +01:00
|
|
|
} while ($except != null && ($except = $except->getPrevious()) != null);
|
2021-08-22 13:11:46 +01:00
|
|
|
|
|
|
|
Event::handle('CleanupModule');
|
|
|
|
|
2020-09-06 22:38:37 +01:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2021-08-08 01:35:27 +01:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2020-07-23 18:55:06 +01:00
|
|
|
public static function getSubscribedEvents()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
KernelEvents::CONTROLLER => 'onKernelController',
|
2020-09-06 22:38:37 +01:00
|
|
|
KernelEvents::EXCEPTION => 'onKernelException',
|
2020-07-23 18:55:06 +01:00
|
|
|
KernelEvents::VIEW => 'onKernelView',
|
|
|
|
];
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|
2021-04-15 23:33:37 +01:00
|
|
|
|
|
|
|
/**
|
2021-09-05 19:41:06 +01:00
|
|
|
* Get and convert GET parameters. Can be called with `int`, `bool`, `string`, etc
|
2021-04-15 23:33:37 +01:00
|
|
|
*
|
2021-09-05 19:41:06 +01:00
|
|
|
* @throws Exception
|
2021-10-18 13:17:08 +01:00
|
|
|
* @throws ValidatorException
|
2021-09-05 19:41:06 +01:00
|
|
|
*
|
2021-10-18 13:17:08 +01:00
|
|
|
* @return null|array|bool|int|string the value or null if no parameter exists
|
2021-04-15 23:33:37 +01:00
|
|
|
*/
|
2021-10-18 13:17:08 +01:00
|
|
|
public function __call(string $method, array $args): array|bool|int|string|null
|
2021-04-15 23:33:37 +01:00
|
|
|
{
|
|
|
|
switch ($method) {
|
|
|
|
case 'int':
|
|
|
|
case 'bool':
|
2021-09-05 19:41:06 +01:00
|
|
|
case 'string':
|
2022-01-03 17:39:28 +00:00
|
|
|
if ($this->request->query->has($args[0])) {
|
|
|
|
return match ($method) {
|
|
|
|
'int' => $this->request->query->getInt($args[0]),
|
|
|
|
'bool' => $this->request->query->getBoolean($args[0]),
|
|
|
|
'string' => $this->request->query->get($args[0]),
|
|
|
|
default => throw new BugFoundException('Inconsistent switch/match spotted'),
|
|
|
|
};
|
|
|
|
} elseif (\array_key_exists(1, $args) && $args[1] instanceof Throwable) {
|
|
|
|
throw $args[1];
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2021-10-18 13:17:08 +01:00
|
|
|
case 'params':
|
|
|
|
return $this->request->query->all();
|
2021-04-15 23:33:37 +01:00
|
|
|
default:
|
2021-08-08 01:35:27 +01:00
|
|
|
// @codeCoverageIgnoreStart
|
2021-04-15 23:33:37 +01:00
|
|
|
Log::critical($m = "Method '{$method}' on class App\\Core\\Controller not found (__call)");
|
2021-08-01 12:44:45 +01:00
|
|
|
throw new Exception($m);
|
2021-08-08 01:35:27 +01:00
|
|
|
// @codeCoverageIgnoreEnd
|
2021-04-15 23:33:37 +01:00
|
|
|
}
|
|
|
|
}
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|