2020-07-06 21:51:08 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// {{{ 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>
|
|
|
|
* @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-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-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;
|
2020-07-06 21:51:08 +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-08-03 18:48:17 +01: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;
|
2020-07-06 21:51:08 +01:00
|
|
|
|
2020-07-23 18:55:06 +01:00
|
|
|
class Controller extends AbstractController implements EventSubscriberInterface
|
2020-07-06 21:51:08 +01:00
|
|
|
{
|
2021-04-15 23:33:37 +01:00
|
|
|
private array $vars = [];
|
|
|
|
private ?Request $request = null;
|
|
|
|
|
|
|
|
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;
|
|
|
|
$class = get_called_class();
|
|
|
|
$method = 'on' . ucfirst(strtolower($request->getMethod()));
|
2020-07-06 21:51:08 +01:00
|
|
|
if (method_exists($class, $method)) {
|
2020-07-23 18:55:06 +01:00
|
|
|
return $class::$method($request, $this->vars);
|
2020-07-06 21:51:08 +01:00
|
|
|
} else {
|
2020-07-23 18:55:06 +01:00
|
|
|
return $class::handle($request, $this->vars);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
$this->vars = ['controler' => $controller, 'request' => $request, 'have_user' => Common::user() !== null];
|
2020-10-19 19:22:59 +01:00
|
|
|
Event::handle('StartTwigPopulateVars', [&$this->vars]);
|
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
|
|
|
|
*/
|
2020-07-23 18:55:06 +01:00
|
|
|
public function onKernelView(ViewEvent $event)
|
|
|
|
{
|
|
|
|
$request = $event->getRequest();
|
|
|
|
$response = $event->getControllerResult();
|
|
|
|
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);
|
2020-10-19 19:22:59 +01:00
|
|
|
Event::handle('EndTwigPopulateVars', [&$this->vars]);
|
2020-07-23 18:55:06 +01:00
|
|
|
|
|
|
|
$template = $this->vars['_template'];
|
2021-08-08 01:35:27 +01:00
|
|
|
unset($this->vars['_template'], $this->vars['request'], $response['_template']);
|
2020-07-06 21:51:08 +01:00
|
|
|
|
2021-07-28 22:29:27 +01:00
|
|
|
// Respond in the the most preffered acceptable content type
|
|
|
|
$accept = $request->getAcceptableContentTypes() ?: ['text/html'];
|
|
|
|
$format = $request->getFormat($accept[0]);
|
2020-07-06 21:51:08 +01:00
|
|
|
switch ($format) {
|
|
|
|
case 'html':
|
2020-07-23 18:55:06 +01:00
|
|
|
$event->setResponse($this->render($template, $this->vars));
|
|
|
|
break;
|
2020-07-06 21:51:08 +01:00
|
|
|
case 'json':
|
2021-08-08 01:35:27 +01:00
|
|
|
$event->setResponse(new JsonResponse($response));
|
2021-04-30 21:16:42 +01:00
|
|
|
break;
|
2020-07-06 21:51:08 +01:00
|
|
|
default:
|
2021-08-08 01:35:27 +01:00
|
|
|
throw new ClientException(_m('Unsupported format'), 406); // 406 Not Acceptable
|
2020-07-06 21:51:08 +01:00
|
|
|
}
|
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);
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Get and convert GET parameters. Can be called with `int`, `bool`, etc
|
|
|
|
*
|
|
|
|
* @param string $name
|
|
|
|
*
|
|
|
|
* @throws ValidatorException
|
|
|
|
*/
|
|
|
|
public function __call(string $method, array $args)
|
|
|
|
{
|
|
|
|
$name = $args[0];
|
|
|
|
$value = $this->request->query->get($name);
|
|
|
|
switch ($method) {
|
|
|
|
case 'int':
|
|
|
|
return (int) $value;
|
|
|
|
case 'bool':
|
|
|
|
return (bool) $value;
|
|
|
|
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
|
|
|
}
|