2020-03-12 05:29:08 +09:00
|
|
|
<?php
|
2020-03-16 06:21:11 +09:00
|
|
|
|
2020-05-21 01:53:53 +09:00
|
|
|
// {{{ License
|
2020-07-24 07:51:07 +09:00
|
|
|
|
2020-05-21 01:53:53 +09: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-24 07:51:07 +09:00
|
|
|
|
2020-05-21 01:53:53 +09:00
|
|
|
// }}}
|
|
|
|
|
2020-03-12 05:29:08 +09:00
|
|
|
/**
|
|
|
|
* Main GNU social entry point
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Framework
|
2020-03-16 06:21:11 +09:00
|
|
|
*
|
2020-03-22 05:18:05 +09:00
|
|
|
* StatusNet and GNU social 1
|
|
|
|
*
|
|
|
|
* @author Refer to CREDITS.md
|
|
|
|
* @copyright 2010 Free Software Foundation, Inc http://www.fsf.org
|
|
|
|
*
|
|
|
|
* GNU social 2
|
|
|
|
* @author Bruno Casteleiro <brunoccast@fc.up.pt>
|
2020-03-12 05:29:08 +09:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2020-03-22 05:18:05 +09:00
|
|
|
*
|
|
|
|
* GNU social 3
|
2021-02-20 08:29:43 +09:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2018-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-03-12 05:29:08 +09:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2020-05-12 03:15:08 +09:00
|
|
|
namespace App\Core;
|
2020-03-12 05:29:08 +09:00
|
|
|
|
2020-06-05 06:00:05 +09:00
|
|
|
use App\Core\DB\DB;
|
2020-07-22 08:12:49 +09:00
|
|
|
use App\Core\I18n\I18n;
|
2020-07-10 00:00:58 +09:00
|
|
|
use App\Core\Queue\Queue;
|
2020-06-05 06:00:05 +09:00
|
|
|
use App\Core\Router\Router;
|
2020-09-23 07:13:36 +09:00
|
|
|
use App\Util\Common;
|
2021-07-20 19:37:01 +09:00
|
|
|
use App\Util\Exception\ConfigurationException;
|
2021-04-15 09:59:30 +09:00
|
|
|
use App\Util\Formatting;
|
2020-05-13 21:10:24 +09:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-08-20 09:37:00 +09:00
|
|
|
use HtmlSanitizer\SanitizerInterface;
|
2020-03-16 06:21:11 +09:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-03-21 07:48:27 +09:00
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
2020-09-23 07:13:36 +09:00
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
2020-03-21 07:48:27 +09:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2020-03-12 05:29:08 +09:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2020-06-24 05:27:02 +09:00
|
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
2021-05-13 00:45:00 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
2020-07-22 20:45:03 +09:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2020-03-16 06:21:11 +09:00
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
2020-03-12 05:29:08 +09:00
|
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
2020-07-10 00:00:58 +09:00
|
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
2020-08-09 01:10:25 +09:00
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
2020-06-05 06:00:05 +09:00
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
2020-07-24 08:33:00 +09:00
|
|
|
use Symfony\Component\Security\Core\Security as SSecurity;
|
2020-07-22 20:45:03 +09:00
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
2020-08-15 22:48:42 +09:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
2020-03-16 06:21:11 +09:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2021-07-20 19:37:01 +09:00
|
|
|
use Twig\Environment;
|
2020-03-12 05:29:08 +09:00
|
|
|
|
2021-07-22 01:43:05 +09:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2020-03-12 05:29:08 +09:00
|
|
|
class GNUsocial implements EventSubscriberInterface
|
|
|
|
{
|
2020-07-22 20:45:03 +09:00
|
|
|
use TargetPathTrait;
|
|
|
|
|
2021-07-20 19:37:01 +09:00
|
|
|
protected bool $initialized = false;
|
|
|
|
protected LoggerInterface $logger;
|
|
|
|
protected TranslatorInterface $translator;
|
|
|
|
protected EntityManagerInterface $entity_manager;
|
|
|
|
protected RouterInterface $router;
|
|
|
|
protected UrlGeneratorInterface $url_generator;
|
|
|
|
protected FormFactoryInterface $form_factory;
|
|
|
|
protected MessageBusInterface $message_bus;
|
2020-07-22 10:54:01 +09:00
|
|
|
protected EventDispatcherInterface $event_dispatcher;
|
2021-07-20 19:37:01 +09:00
|
|
|
protected SessionInterface $session;
|
|
|
|
protected SSecurity $security;
|
|
|
|
protected ModuleManager $module_manager;
|
|
|
|
protected HttpClientInterface $client;
|
|
|
|
protected SanitizerInterface $sanitizer;
|
|
|
|
protected ContainerBagInterface $config;
|
|
|
|
protected Environment $twig;
|
|
|
|
protected ?Request $request;
|
2020-08-20 09:37:00 +09:00
|
|
|
|
2020-03-22 05:18:05 +09:00
|
|
|
/**
|
|
|
|
* Symfony dependency injection gives us access to these services
|
|
|
|
*/
|
2020-06-08 21:04:20 +09:00
|
|
|
public function __construct(LoggerInterface $logger,
|
2020-07-22 10:54:01 +09:00
|
|
|
TranslatorInterface $trans,
|
2020-06-05 06:00:05 +09:00
|
|
|
EntityManagerInterface $em,
|
2020-06-24 05:27:02 +09:00
|
|
|
RouterInterface $router,
|
2020-08-09 01:10:25 +09:00
|
|
|
UrlGeneratorInterface $url_gen,
|
2020-07-10 00:00:58 +09:00
|
|
|
FormFactoryInterface $ff,
|
2020-07-22 10:54:01 +09:00
|
|
|
MessageBusInterface $mb,
|
2020-07-22 20:45:03 +09:00
|
|
|
EventDispatcherInterface $ed,
|
2020-07-24 08:33:00 +09:00
|
|
|
SessionInterface $sess,
|
2020-07-31 07:40:37 +09:00
|
|
|
SSecurity $sec,
|
2020-08-15 22:48:42 +09:00
|
|
|
ModuleManager $mm,
|
2020-08-20 09:37:00 +09:00
|
|
|
HttpClientInterface $cl,
|
2020-09-23 07:13:36 +09:00
|
|
|
SanitizerInterface $san,
|
2021-04-15 09:59:30 +09:00
|
|
|
ContainerBagInterface $conf,
|
2021-07-20 19:37:01 +09:00
|
|
|
Environment $twig,
|
2021-05-13 00:45:00 +09:00
|
|
|
RequestStack $request_stack)
|
2020-03-12 05:29:08 +09:00
|
|
|
{
|
2020-07-22 10:54:01 +09:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->translator = $trans;
|
|
|
|
$this->entity_manager = $em;
|
|
|
|
$this->router = $router;
|
2020-08-09 01:10:25 +09:00
|
|
|
$this->url_generator = $url_gen;
|
2020-07-22 10:54:01 +09:00
|
|
|
$this->form_factory = $ff;
|
|
|
|
$this->message_bus = $mb;
|
|
|
|
$this->event_dispatcher = $ed;
|
2020-07-24 08:33:00 +09:00
|
|
|
$this->session = $sess;
|
|
|
|
$this->security = $sec;
|
2020-08-04 05:56:00 +09:00
|
|
|
$this->module_manager = $mm;
|
2020-08-15 22:48:42 +09:00
|
|
|
$this->client = $cl;
|
2021-07-20 19:37:01 +09:00
|
|
|
$this->sanitizer = $san;
|
2020-09-23 07:13:36 +09:00
|
|
|
$this->config = $conf;
|
2021-04-15 09:59:30 +09:00
|
|
|
$this->twig = $twig;
|
2021-05-13 00:45:00 +09:00
|
|
|
$this->request = $request_stack->getCurrentRequest();
|
2020-07-22 10:54:01 +09:00
|
|
|
|
2020-07-26 08:58:26 +09:00
|
|
|
$this->initialize();
|
2020-03-16 06:21:11 +09:00
|
|
|
}
|
2020-03-14 07:31:56 +09:00
|
|
|
|
2020-03-22 05:18:05 +09:00
|
|
|
/**
|
2020-03-28 23:39:48 +09:00
|
|
|
* Store these services to be accessed statically and load modules
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2021-07-20 19:37:01 +09:00
|
|
|
* @throws ConfigurationException
|
2020-03-22 05:18:05 +09:00
|
|
|
*/
|
2020-07-26 08:58:26 +09:00
|
|
|
public function initialize(): void
|
2020-03-16 06:21:11 +09:00
|
|
|
{
|
2020-08-04 05:56:00 +09:00
|
|
|
if (!$this->initialized) {
|
2020-10-01 07:57:25 +09:00
|
|
|
Common::setupConfig($this->config);
|
2021-05-24 04:56:45 +09:00
|
|
|
if (!is_null($this->request)) {
|
|
|
|
Common::setRequest($this->request);
|
|
|
|
}
|
2020-08-04 05:56:00 +09:00
|
|
|
Log::setLogger($this->logger);
|
|
|
|
Event::setDispatcher($this->event_dispatcher);
|
|
|
|
I18n::setTranslator($this->translator);
|
|
|
|
DB::setManager($this->entity_manager);
|
|
|
|
Form::setFactory($this->form_factory);
|
|
|
|
Queue::setMessageBus($this->message_bus);
|
2021-07-20 19:37:01 +09:00
|
|
|
Security::setHelper($this->security, $this->sanitizer);
|
2020-08-09 01:10:25 +09:00
|
|
|
Router::setRouter($this->router, $this->url_generator);
|
2020-08-15 22:48:42 +09:00
|
|
|
HTTPClient::setClient($this->client);
|
2021-04-15 09:59:30 +09:00
|
|
|
Formatting::setTwig($this->twig);
|
2020-08-04 05:56:00 +09:00
|
|
|
Cache::setupCache();
|
2020-07-31 07:40:37 +09:00
|
|
|
|
2021-04-16 02:01:52 +09:00
|
|
|
DB::initTableMap();
|
|
|
|
|
2020-08-04 05:56:00 +09:00
|
|
|
// Events are proloaded on compilation, but set at runtime
|
|
|
|
$this->module_manager->loadModules();
|
|
|
|
|
|
|
|
$this->initialized = true;
|
|
|
|
}
|
2020-03-21 07:48:27 +09:00
|
|
|
}
|
|
|
|
|
2020-03-22 05:18:05 +09:00
|
|
|
/**
|
2021-07-20 19:37:01 +09:00
|
|
|
* Event very early on in the Symfony HTTP lifecycle, but after everything is registered
|
2020-03-22 05:18:05 +09:00
|
|
|
* where we get access to the event dispatcher
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2021-07-20 19:37:01 +09:00
|
|
|
* @param RequestEvent $event
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2020-05-11 05:43:15 +09:00
|
|
|
* @return RequestEvent
|
2020-03-22 05:18:05 +09:00
|
|
|
*/
|
2021-07-20 19:37:01 +09:00
|
|
|
public function onKernelRequest(RequestEvent $event): RequestEvent
|
2020-03-21 07:48:27 +09:00
|
|
|
{
|
2021-05-13 00:45:00 +09:00
|
|
|
$this->request = $event->getRequest();
|
2020-07-24 07:51:07 +09:00
|
|
|
|
|
|
|
// Save the target path, so we can redirect back after logging in
|
2021-05-13 00:45:00 +09:00
|
|
|
if (!(!$event->isMasterRequest() || $this->request->isXmlHttpRequest() || Common::isRoute(['login', 'register', 'logout']))) {
|
|
|
|
$this->saveTargetPath($this->session, 'main', $this->request->getBaseUrl());
|
2020-07-22 20:45:03 +09:00
|
|
|
}
|
|
|
|
|
2020-07-26 08:58:26 +09:00
|
|
|
$this->initialize();
|
2020-03-21 07:48:27 +09:00
|
|
|
return $event;
|
|
|
|
}
|
2020-03-21 07:10:01 +09:00
|
|
|
|
2020-03-22 05:18:05 +09:00
|
|
|
/**
|
|
|
|
* Event after everything is initialized when using the `bin/console` command
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2021-07-20 19:37:01 +09:00
|
|
|
* @param ConsoleCommandEvent $event
|
|
|
|
*
|
|
|
|
* @throws ConfigurationException
|
2020-05-12 02:39:12 +09:00
|
|
|
*
|
2020-05-11 05:43:15 +09:00
|
|
|
* @return ConsoleCommandEvent
|
2020-03-22 05:18:05 +09:00
|
|
|
*/
|
2021-07-20 19:37:01 +09:00
|
|
|
public function onCommand(ConsoleCommandEvent $event): ConsoleCommandEvent
|
2020-03-21 07:48:27 +09:00
|
|
|
{
|
2020-07-26 08:58:26 +09:00
|
|
|
$this->initialize();
|
2020-03-12 05:29:08 +09:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2020-03-22 05:18:05 +09:00
|
|
|
/**
|
2021-07-20 19:37:01 +09:00
|
|
|
* Tell Symfony which events we want to listen to, which Symfony detects and auto-wires
|
2020-03-22 05:18:05 +09:00
|
|
|
* due to this implementing the `EventSubscriberInterface`
|
2021-05-13 00:45:00 +09:00
|
|
|
*
|
2020-03-22 05:18:05 +09:00
|
|
|
*/
|
2021-07-20 19:37:01 +09:00
|
|
|
public static function getSubscribedEvents(): array
|
2020-03-12 05:29:08 +09:00
|
|
|
{
|
2020-03-21 07:48:27 +09:00
|
|
|
return [
|
|
|
|
KernelEvents::REQUEST => 'onKernelRequest',
|
|
|
|
'console.command' => 'onCommand',
|
|
|
|
];
|
2020-03-12 05:29:08 +09:00
|
|
|
}
|
|
|
|
}
|