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.
 
 
 
 
 
 

278 lines
10 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. * Main GNU social entry point
  21. *
  22. * @package GNUsocial
  23. * @category Framework
  24. *
  25. * StatusNet and GNU social 1
  26. *
  27. * @author Refer to CREDITS.md
  28. * @copyright 2010 Free Software Foundation, Inc http://www.fsf.org
  29. *
  30. * GNU social 2
  31. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  32. * @author Diogo Cordeiro <diogo@fc.up.pt>
  33. *
  34. * GNU social 3
  35. * @author Hugo Sales <hugo@hsal.es>
  36. * @copyright 2018-2021 Free Software Foundation, Inc http://www.fsf.org
  37. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  38. */
  39. namespace App\Core;
  40. use App\Core\DB\DB;
  41. use App\Core\I18n\I18n;
  42. use App\Core\Queue\Queue;
  43. use App\Core\Router\Router;
  44. use App\Kernel;
  45. use App\Util\Common;
  46. use App\Util\Exception\ConfigurationException;
  47. use App\Util\Formatting;
  48. use Doctrine\ORM\EntityManagerInterface;
  49. use HtmlSanitizer\SanitizerInterface;
  50. use Psr\Log\LoggerInterface;
  51. use Symfony\Component\Config\Loader\LoaderInterface;
  52. use Symfony\Component\Console\Event\ConsoleCommandEvent;
  53. use Symfony\Component\DependencyInjection\ContainerBuilder;
  54. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  55. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  56. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  57. use Symfony\Component\Form\FormFactoryInterface;
  58. use Symfony\Component\HttpFoundation\Request;
  59. use Symfony\Component\HttpFoundation\RequestStack;
  60. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  61. use Symfony\Component\HttpKernel\Event\RequestEvent;
  62. use Symfony\Component\HttpKernel\KernelEvents;
  63. use Symfony\Component\Mailer\MailerInterface;
  64. use Symfony\Component\Messenger\MessageBusInterface;
  65. use Symfony\Component\Routing\RouterInterface;
  66. use Symfony\Component\Security\Core\Security as SSecurity;
  67. use Symfony\Component\Security\Http\Util\TargetPathTrait;
  68. use Symfony\Contracts\HttpClient\HttpClientInterface;
  69. use Symfony\Contracts\Translation\TranslatorInterface;
  70. use SymfonyCasts\Bundle\ResetPassword\ResetPasswordHelperInterface;
  71. use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface;
  72. use Twig\Environment;
  73. /**
  74. * @codeCoverageIgnore
  75. */
  76. class GNUsocial implements EventSubscriberInterface
  77. {
  78. use TargetPathTrait;
  79. protected bool $initialized = false;
  80. protected LoggerInterface $logger;
  81. protected TranslatorInterface $translator;
  82. protected EntityManagerInterface $entity_manager;
  83. protected RouterInterface $router;
  84. protected FormFactoryInterface $form_factory;
  85. protected MessageBusInterface $message_bus;
  86. protected EventDispatcherInterface $event_dispatcher;
  87. protected SessionInterface $session;
  88. protected SSecurity $security;
  89. protected ModuleManager $module_manager;
  90. protected HttpClientInterface $client;
  91. protected SanitizerInterface $sanitizer;
  92. protected ContainerBagInterface $config;
  93. protected Environment $twig;
  94. protected ?Request $request;
  95. protected MailerInterface $mailer_helper;
  96. protected VerifyEmailHelperInterface $email_verify_helper;
  97. protected ResetPasswordHelperInterface $reset_password_helper;
  98. /**
  99. * Symfony dependency injection gives us access to these services
  100. */
  101. public function __construct(
  102. LoggerInterface $logger,
  103. TranslatorInterface $trans,
  104. EntityManagerInterface $em,
  105. RouterInterface $router,
  106. FormFactoryInterface $ff,
  107. MessageBusInterface $mb,
  108. EventDispatcherInterface $ed,
  109. SessionInterface $sess,
  110. SSecurity $sec,
  111. ModuleManager $mm,
  112. HttpClientInterface $cl,
  113. SanitizerInterface $san,
  114. ContainerBagInterface $conf,
  115. Environment $twig,
  116. RequestStack $request_stack,
  117. MailerInterface $mailer,
  118. VerifyEmailHelperInterface $email_verify_helper,
  119. ResetPasswordHelperInterface $reset_helper,
  120. ) {
  121. $this->logger = $logger;
  122. $this->translator = $trans;
  123. $this->entity_manager = $em;
  124. $this->router = $router;
  125. $this->form_factory = $ff;
  126. $this->message_bus = $mb;
  127. $this->event_dispatcher = $ed;
  128. $this->session = $sess;
  129. $this->security = $sec;
  130. $this->module_manager = $mm;
  131. $this->client = $cl;
  132. $this->sanitizer = $san;
  133. $this->config = $conf;
  134. $this->twig = $twig;
  135. $this->request = $request_stack->getCurrentRequest();
  136. $this->mailer_helper = $mailer;
  137. $this->email_verify_helper = $email_verify_helper;
  138. $this->reset_password_helper = $reset_helper;
  139. $this->initialize();
  140. }
  141. /**
  142. * Store these services to be accessed statically and load modules
  143. *
  144. * @throws ConfigurationException
  145. */
  146. public function initialize(): void
  147. {
  148. if (!$this->initialized) {
  149. Common::setupConfig($this->config);
  150. if (!\is_null($this->request)) {
  151. Common::setRequest($this->request);
  152. }
  153. Log::setLogger($this->logger);
  154. Event::setDispatcher($this->event_dispatcher);
  155. I18n::setTranslator($this->translator);
  156. DB::setManager($this->entity_manager);
  157. Form::setFactory($this->form_factory);
  158. Queue::setMessageBus($this->message_bus);
  159. Security::setHelper($this->security, $this->sanitizer);
  160. Router::setRouter($this->router);
  161. HTTPClient::setClient($this->client);
  162. Formatting::setTwig($this->twig);
  163. Cache::setupCache();
  164. DB::initTableMap();
  165. // Events are preloaded on compilation, but set at runtime, along with configuration
  166. $this->module_manager->loadModules();
  167. $this->initialized = true;
  168. }
  169. }
  170. /**
  171. * Event very early on in the Symfony HTTP lifecycle, but after everything is registered
  172. * where we get access to the event dispatcher
  173. */
  174. public function onKernelRequest(RequestEvent $event): RequestEvent
  175. {
  176. $this->request = $event->getRequest();
  177. // Save the target path, so we can redirect back after logging in
  178. if (!(!$event->isMainRequest() || $this->request->isXmlHttpRequest() || Common::isRoute(['login', 'register', 'logout']))) {
  179. $this->saveTargetPath($this->session, 'main', $this->request->getBaseUrl());
  180. }
  181. $this->initialize();
  182. Event::handle('InitializeModule');
  183. return $event;
  184. }
  185. /**
  186. * Event after everything is initialized when using the `bin/console` command
  187. *
  188. * @throws ConfigurationException
  189. */
  190. public function onCommand(ConsoleCommandEvent $event): ConsoleCommandEvent
  191. {
  192. $this->initialize();
  193. return $event;
  194. }
  195. /**
  196. * Load configuration files
  197. *
  198. * Happens at "compile time"
  199. *
  200. * @codeCoverageIgnore
  201. */
  202. public static function configureContainer(ContainerBuilder $container, LoaderInterface $loader): void
  203. {
  204. // Overriding doesn't work as we want, overrides the top-most key, do it manually
  205. $local_file = INSTALLDIR . '/social.local.yaml';
  206. if (!file_exists($local_file)) {
  207. file_put_contents($local_file, "parameters:\n locals:\n gnusocial:\n");
  208. }
  209. // Load .local
  210. $loader->load($local_file);
  211. $locals = $container->getParameter('locals');
  212. $container->getParameterBag()->remove('locals');
  213. // Load normal config
  214. $loader->load(INSTALLDIR . '/social' . Kernel::CONFIG_EXTS, 'glob');
  215. $defaults = $container->getParameter('gnusocial');
  216. // Load module config
  217. $module_configs = ModuleManager::configureContainer($container, $loader);
  218. // Merge parameter $from with values already set in $to
  219. $merge_local_config = function ($from, $to = null) use ($container, $locals) {
  220. $to ??= $from;
  221. $wrapper = $container->hasParameter($to) ? $container->getParameter($to) : [];
  222. $content = [$from => $container->getParameter($from)];
  223. $container->getParameterBag()->remove($from);
  224. $locals = $locals[$from] ?? [];
  225. $configs = array_replace_recursive($wrapper, $content, $locals);
  226. $container->setParameter($to, $configs);
  227. };
  228. // Override and merge any of the previous settings from the locals
  229. if (\is_array($locals)) {
  230. $merge_local_config('gnusocial');
  231. foreach ($module_configs as $mod => $type) {
  232. $loader->load(INSTALLDIR . \PATH_SEPARATOR . $type . \PATH_SEPARATOR . ucfirst($mod) . 'config' . Kernel::CONFIG_EXTS, 'glob');
  233. $defaults[$mod] = $container->getParameter($mod);
  234. $merge_local_config($mod, $type); // TODO likely broken
  235. }
  236. }
  237. $container->setParameter('gnusocial_defaults', $defaults);
  238. $container->setParameter('gnusocial', $defaults);
  239. }
  240. /**
  241. * Tell Symfony which events we want to listen to, which Symfony detects and auto-wires
  242. * due to this implementing the `EventSubscriberInterface`
  243. */
  244. public static function getSubscribedEvents(): array
  245. {
  246. return [
  247. KernelEvents::REQUEST => 'onKernelRequest',
  248. 'console.command' => 'onCommand',
  249. ];
  250. }
  251. }