2020-03-11 20:29:08 +00:00
|
|
|
<?php
|
2020-03-15 21:21:11 +00:00
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// {{{ License
|
2020-07-23 23:51:07 +01:00
|
|
|
|
2020-05-20 17:53:53 +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-23 23:51:07 +01:00
|
|
|
|
2020-05-20 17:53:53 +01:00
|
|
|
// }}}
|
|
|
|
|
2020-03-11 20:29:08 +00:00
|
|
|
/**
|
|
|
|
* Main GNU social entry point
|
|
|
|
*
|
|
|
|
* @package GNUsocial
|
|
|
|
* @category Framework
|
2020-03-15 21:21:11 +00:00
|
|
|
*
|
2020-03-21 20:18:05 +00: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-11 20:29:08 +00:00
|
|
|
* @author Diogo Cordeiro <diogo@fc.up.pt>
|
2020-03-21 20:18:05 +00:00
|
|
|
*
|
|
|
|
* GNU social 3
|
2021-02-19 23:29:43 +00:00
|
|
|
* @author Hugo Sales <hugo@hsal.es>
|
|
|
|
* @copyright 2018-2021 Free Software Foundation, Inc http://www.fsf.org
|
2020-03-11 20:29:08 +00:00
|
|
|
* @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
|
|
|
|
*/
|
|
|
|
|
2020-05-11 19:15:08 +01:00
|
|
|
namespace App\Core;
|
2020-03-11 20:29:08 +00:00
|
|
|
|
2020-06-04 22:00:05 +01:00
|
|
|
use App\Core\DB\DB;
|
2020-07-22 00:12:49 +01:00
|
|
|
use App\Core\I18n\I18n;
|
2020-07-09 16:00:58 +01:00
|
|
|
use App\Core\Queue\Queue;
|
2020-06-04 22:00:05 +01:00
|
|
|
use App\Core\Router\Router;
|
2021-08-21 22:27:34 +01:00
|
|
|
use App\Kernel;
|
2021-11-25 23:08:30 +00:00
|
|
|
use App\Security\EmailVerifier;
|
2020-09-22 23:13:36 +01:00
|
|
|
use App\Util\Common;
|
2021-07-20 11:37:01 +01:00
|
|
|
use App\Util\Exception\ConfigurationException;
|
2021-04-15 01:59:30 +01:00
|
|
|
use App\Util\Formatting;
|
2022-01-12 17:12:26 +00:00
|
|
|
use App\Util\HTML;
|
2020-05-13 13:10:24 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-08-20 01:37:00 +01:00
|
|
|
use HtmlSanitizer\SanitizerInterface;
|
2020-03-15 21:21:11 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2021-08-21 22:27:34 +01:00
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface;
|
2020-03-20 22:48:27 +00:00
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent;
|
2021-08-21 22:27:34 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
2020-09-22 23:13:36 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
2020-03-20 22:48:27 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2020-03-11 20:29:08 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
2020-06-23 21:27:02 +01:00
|
|
|
use Symfony\Component\Form\FormFactoryInterface;
|
2021-05-12 16:45:00 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
2020-07-22 12:45:03 +01:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2020-03-15 21:21:11 +00:00
|
|
|
use Symfony\Component\HttpKernel\Event\RequestEvent;
|
2020-03-11 20:29:08 +00:00
|
|
|
use Symfony\Component\HttpKernel\KernelEvents;
|
2021-07-29 18:26:14 +01:00
|
|
|
use Symfony\Component\Mailer\MailerInterface;
|
2020-07-09 16:00:58 +01:00
|
|
|
use Symfony\Component\Messenger\MessageBusInterface;
|
2020-06-04 22:00:05 +01:00
|
|
|
use Symfony\Component\Routing\RouterInterface;
|
2020-07-24 00:33:00 +01:00
|
|
|
use Symfony\Component\Security\Core\Security as SSecurity;
|
2020-07-22 12:45:03 +01:00
|
|
|
use Symfony\Component\Security\Http\Util\TargetPathTrait;
|
2020-08-15 14:48:42 +01:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
2020-03-15 21:21:11 +00:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2021-07-29 18:26:14 +01:00
|
|
|
use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
|
2021-07-29 16:03:52 +01:00
|
|
|
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
|
2021-07-20 11:37:01 +01:00
|
|
|
use Twig\Environment;
|
2020-03-11 20:29:08 +00:00
|
|
|
|
2021-07-21 17:43:05 +01:00
|
|
|
/**
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2020-03-11 20:29:08 +00:00
|
|
|
class GNUsocial implements EventSubscriberInterface
|
|
|
|
{
|
2020-07-22 12:45:03 +01:00
|
|
|
use TargetPathTrait;
|
|
|
|
|
2021-07-20 11:37:01 +01:00
|
|
|
protected bool $initialized = false;
|
|
|
|
protected LoggerInterface $logger;
|
|
|
|
protected TranslatorInterface $translator;
|
|
|
|
protected EntityManagerInterface $entity_manager;
|
|
|
|
protected RouterInterface $router;
|
|
|
|
protected FormFactoryInterface $form_factory;
|
|
|
|
protected MessageBusInterface $message_bus;
|
2020-07-22 02:54:01 +01:00
|
|
|
protected EventDispatcherInterface $event_dispatcher;
|
2021-07-20 11:37:01 +01: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;
|
2021-07-29 18:26:14 +01:00
|
|
|
protected MailerInterface $mailer_helper;
|
2021-07-29 16:03:52 +01:00
|
|
|
protected VerifyEmailHelperInterface $email_verify_helper;
|
2021-07-29 18:26:14 +01:00
|
|
|
protected ResetPasswordHelperInterface $reset_password_helper;
|
2020-08-20 01:37:00 +01:00
|
|
|
|
2020-03-21 20:18:05 +00:00
|
|
|
/**
|
|
|
|
* Symfony dependency injection gives us access to these services
|
|
|
|
*/
|
2021-10-10 09:26:18 +01:00
|
|
|
public function __construct(
|
|
|
|
LoggerInterface $logger,
|
|
|
|
TranslatorInterface $trans,
|
|
|
|
EntityManagerInterface $em,
|
|
|
|
RouterInterface $router,
|
|
|
|
FormFactoryInterface $ff,
|
|
|
|
MessageBusInterface $mb,
|
|
|
|
EventDispatcherInterface $ed,
|
|
|
|
SessionInterface $sess,
|
|
|
|
SSecurity $sec,
|
|
|
|
ModuleManager $mm,
|
|
|
|
HttpClientInterface $cl,
|
|
|
|
SanitizerInterface $san,
|
|
|
|
ContainerBagInterface $conf,
|
|
|
|
Environment $twig,
|
|
|
|
RequestStack $request_stack,
|
|
|
|
MailerInterface $mailer,
|
|
|
|
VerifyEmailHelperInterface $email_verify_helper,
|
|
|
|
ResetPasswordHelperInterface $reset_helper,
|
|
|
|
) {
|
2021-07-29 18:26:14 +01:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->translator = $trans;
|
|
|
|
$this->entity_manager = $em;
|
|
|
|
$this->router = $router;
|
|
|
|
$this->form_factory = $ff;
|
|
|
|
$this->message_bus = $mb;
|
|
|
|
$this->event_dispatcher = $ed;
|
|
|
|
$this->session = $sess;
|
|
|
|
$this->security = $sec;
|
|
|
|
$this->module_manager = $mm;
|
|
|
|
$this->client = $cl;
|
|
|
|
$this->sanitizer = $san;
|
|
|
|
$this->config = $conf;
|
|
|
|
$this->twig = $twig;
|
|
|
|
$this->request = $request_stack->getCurrentRequest();
|
|
|
|
$this->mailer_helper = $mailer;
|
|
|
|
$this->email_verify_helper = $email_verify_helper;
|
|
|
|
$this->reset_password_helper = $reset_helper;
|
2020-07-22 02:54:01 +01:00
|
|
|
|
2020-07-26 00:58:26 +01:00
|
|
|
$this->initialize();
|
2020-03-15 21:21:11 +00:00
|
|
|
}
|
2020-03-13 22:31:56 +00:00
|
|
|
|
2020-03-21 20:18:05 +00:00
|
|
|
/**
|
2020-03-28 14:39:48 +00:00
|
|
|
* Store these services to be accessed statically and load modules
|
2020-05-11 18:39:12 +01:00
|
|
|
*
|
2021-07-20 11:37:01 +01:00
|
|
|
* @throws ConfigurationException
|
2020-03-21 20:18:05 +00:00
|
|
|
*/
|
2020-07-26 00:58:26 +01:00
|
|
|
public function initialize(): void
|
2020-03-15 21:21:11 +00:00
|
|
|
{
|
2020-08-03 21:56:00 +01:00
|
|
|
if (!$this->initialized) {
|
2020-09-30 23:57:25 +01:00
|
|
|
Common::setupConfig($this->config);
|
2021-10-10 09:26:18 +01:00
|
|
|
if (!\is_null($this->request)) {
|
2021-05-23 20:56:45 +01:00
|
|
|
Common::setRequest($this->request);
|
|
|
|
}
|
2020-08-03 21:56:00 +01: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);
|
2022-01-12 17:12:26 +00:00
|
|
|
Security::setHelper($this->security);
|
|
|
|
HTML::setSanitizer($this->sanitizer);
|
2021-11-10 13:59:02 +00:00
|
|
|
Router::setRouter($this->router);
|
2020-08-15 14:48:42 +01:00
|
|
|
HTTPClient::setClient($this->client);
|
2021-04-15 01:59:30 +01:00
|
|
|
Formatting::setTwig($this->twig);
|
2021-11-25 23:08:30 +00:00
|
|
|
EmailVerifier::setHelpers($this->email_verify_helper, $this->mailer_helper);
|
2020-08-03 21:56:00 +01:00
|
|
|
Cache::setupCache();
|
2020-07-30 23:40:37 +01:00
|
|
|
|
2021-04-15 18:01:52 +01:00
|
|
|
DB::initTableMap();
|
|
|
|
|
2021-08-21 22:30:24 +01:00
|
|
|
// Events are preloaded on compilation, but set at runtime, along with configuration
|
2020-08-03 21:56:00 +01:00
|
|
|
$this->module_manager->loadModules();
|
|
|
|
|
|
|
|
$this->initialized = true;
|
|
|
|
}
|
2020-03-20 22:48:27 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:18:05 +00:00
|
|
|
/**
|
2021-07-20 11:37:01 +01:00
|
|
|
* Event very early on in the Symfony HTTP lifecycle, but after everything is registered
|
2020-03-21 20:18:05 +00:00
|
|
|
* where we get access to the event dispatcher
|
|
|
|
*/
|
2021-07-20 11:37:01 +01:00
|
|
|
public function onKernelRequest(RequestEvent $event): RequestEvent
|
2020-03-20 22:48:27 +00:00
|
|
|
{
|
2021-05-12 16:45:00 +01:00
|
|
|
$this->request = $event->getRequest();
|
2020-07-23 23:51:07 +01:00
|
|
|
|
|
|
|
// Save the target path, so we can redirect back after logging in
|
2021-11-16 14:10:19 +00:00
|
|
|
if (!(!$event->isMainRequest() || $this->request->isXmlHttpRequest() || Common::isRoute(['login', 'register', 'logout']))) {
|
2021-05-12 16:45:00 +01:00
|
|
|
$this->saveTargetPath($this->session, 'main', $this->request->getBaseUrl());
|
2020-07-22 12:45:03 +01:00
|
|
|
}
|
|
|
|
|
2020-07-26 00:58:26 +01:00
|
|
|
$this->initialize();
|
2021-08-22 13:11:46 +01:00
|
|
|
Event::handle('InitializeModule');
|
2020-03-20 22:48:27 +00:00
|
|
|
return $event;
|
|
|
|
}
|
2020-03-20 22:10:01 +00:00
|
|
|
|
2020-03-21 20:18:05 +00:00
|
|
|
/**
|
|
|
|
* Event after everything is initialized when using the `bin/console` command
|
2020-05-11 18:39:12 +01:00
|
|
|
*
|
2021-07-20 11:37:01 +01:00
|
|
|
* @throws ConfigurationException
|
2020-03-21 20:18:05 +00:00
|
|
|
*/
|
2021-07-20 11:37:01 +01:00
|
|
|
public function onCommand(ConsoleCommandEvent $event): ConsoleCommandEvent
|
2020-03-20 22:48:27 +00:00
|
|
|
{
|
2020-07-26 00:58:26 +01:00
|
|
|
$this->initialize();
|
2020-03-11 20:29:08 +00:00
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2021-08-21 22:27:34 +01:00
|
|
|
/**
|
|
|
|
* Load configuration files
|
2021-08-21 22:30:24 +01:00
|
|
|
*
|
|
|
|
* Happens at "compile time"
|
|
|
|
*
|
|
|
|
* @codeCoverageIgnore
|
2021-08-21 22:27:34 +01:00
|
|
|
*/
|
|
|
|
public static function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
|
|
|
|
{
|
|
|
|
// Overriding doesn't work as we want, overrides the top-most key, do it manually
|
|
|
|
$local_file = INSTALLDIR . '/social.local.yaml';
|
|
|
|
if (!file_exists($local_file)) {
|
2021-09-20 15:20:25 +01:00
|
|
|
file_put_contents($local_file, "parameters:\n locals:\n gnusocial:\n");
|
2021-08-21 22:27:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Load .local
|
|
|
|
$loader->load($local_file);
|
|
|
|
$locals = $container->getParameter('locals');
|
2021-11-08 15:05:55 +00:00
|
|
|
$container->getParameterBag()->remove('locals');
|
2021-08-21 22:27:34 +01:00
|
|
|
|
|
|
|
// Load normal config
|
|
|
|
$loader->load(INSTALLDIR . '/social' . Kernel::CONFIG_EXTS, 'glob');
|
2021-11-08 15:05:55 +00:00
|
|
|
$defaults = $container->getParameter('gnusocial');
|
2021-08-21 22:27:34 +01:00
|
|
|
|
2021-08-21 22:30:24 +01:00
|
|
|
// Load module config
|
2021-11-08 15:05:55 +00:00
|
|
|
$module_configs = ModuleManager::configureContainer($container, $loader);
|
2021-08-21 22:30:24 +01:00
|
|
|
|
|
|
|
// Merge parameter $from with values already set in $to
|
2021-11-08 15:05:55 +00:00
|
|
|
$merge_local_config = function ($from, $to = null) use ($container, $locals) {
|
2021-10-10 09:26:18 +01:00
|
|
|
$to ??= $from;
|
2021-08-21 22:30:24 +01:00
|
|
|
$wrapper = $container->hasParameter($to) ? $container->getParameter($to) : [];
|
|
|
|
$content = [$from => $container->getParameter($from)];
|
|
|
|
$container->getParameterBag()->remove($from);
|
2021-09-20 15:20:25 +01:00
|
|
|
$locals = $locals[$from] ?? [];
|
2021-08-21 22:30:24 +01:00
|
|
|
$configs = array_replace_recursive($wrapper, $content, $locals);
|
|
|
|
$container->setParameter($to, $configs);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Override and merge any of the previous settings from the locals
|
2021-10-10 09:26:18 +01:00
|
|
|
if (\is_array($locals)) {
|
2021-11-08 15:05:55 +00:00
|
|
|
$merge_local_config('gnusocial');
|
|
|
|
foreach ($module_configs as $mod => $type) {
|
|
|
|
$loader->load(INSTALLDIR . \PATH_SEPARATOR . $type . \PATH_SEPARATOR . ucfirst($mod) . 'config' . Kernel::CONFIG_EXTS, 'glob');
|
|
|
|
$defaults[$mod] = $container->getParameter($mod);
|
|
|
|
$merge_local_config($mod, $type); // TODO likely broken
|
2021-08-21 22:30:24 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-08 15:05:55 +00:00
|
|
|
$container->setParameter('gnusocial_defaults', $defaults);
|
2021-08-21 22:27:34 +01:00
|
|
|
}
|
|
|
|
|
2020-03-21 20:18:05 +00:00
|
|
|
/**
|
2021-07-20 11:37:01 +01:00
|
|
|
* Tell Symfony which events we want to listen to, which Symfony detects and auto-wires
|
2020-03-21 20:18:05 +00:00
|
|
|
* due to this implementing the `EventSubscriberInterface`
|
|
|
|
*/
|
2021-07-20 11:37:01 +01:00
|
|
|
public static function getSubscribedEvents(): array
|
2020-03-11 20:29:08 +00:00
|
|
|
{
|
2020-03-20 22:48:27 +00:00
|
|
|
return [
|
|
|
|
KernelEvents::REQUEST => 'onKernelRequest',
|
|
|
|
'console.command' => 'onCommand',
|
|
|
|
];
|
2020-03-11 20:29:08 +00:00
|
|
|
}
|
|
|
|
}
|