vendor/elements/azure-auth-bundle/src/EventSubscriber/CodeInjectionSubscriber.php line 37

Open in your IDE?
  1. <?php
  2. namespace Elements\Bundle\AzureAuthBundle\EventSubscriber;
  3. use Elements\Bundle\AzureAuthBundle\Service\RestrictionService;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  6. use Symfony\Component\HttpKernel\Event\RequestEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class CodeInjectionSubscriber implements EventSubscriberInterface
  9. {
  10.     protected $enabled false;
  11.     public function __construct(
  12.         private RestrictionService $restrictionService
  13.     )
  14.     {
  15.     }
  16.     /**
  17.      * @return array
  18.      */
  19.     public static function getSubscribedEvents()// : array
  20.     {
  21.         return [
  22.             KernelEvents::RESPONSE => ['inject'0],
  23.             KernelEvents::REQUEST => ['check'0],
  24.         ];
  25.     }
  26.     /**
  27.      * @param RequestEvent $event
  28.      */
  29.     public function check(RequestEvent $event) {
  30.         // only enable on login page
  31.         if ($event->isMasterRequest()) {
  32.             if ($event->getRequest()->getPathInfo() == "/admin/login"&&
  33.                 $this->restrictionService->checkIpAddresses($event->getRequest()->getClientIp())
  34.             ) {
  35.                 $this->enabled true;
  36.             }
  37.         }
  38.     }
  39.     /**
  40.      * @param ResponseEvent $event
  41.      */
  42.     public function inject(ResponseEvent $event) {
  43.         $response $event->getResponse();
  44.         if ($this->enabled && $event->isMasterRequest()) {
  45.             $content $response->getContent();
  46.             // search for the end <head> tag, and insert the google analytics code before
  47.             // this method is much faster than using simple_html_dom and uses less memory
  48.             $bodyEndPosition strripos($content'</body>');
  49.             if ($bodyEndPosition !== false) {
  50.                 $code '<script src="/bundles/elementsazureauth/js/main.js"></script>';
  51.                 $content substr_replace($content$code "\n\n" '</body>'$bodyEndPosition7);
  52.             }
  53.             $headEndPosition strripos($content'</head>');
  54.             if ($headEndPosition !== false) {
  55.                 $code '<link rel="stylesheet" href="/bundles/elementsazureauth/css/style.css" type="text/css">';
  56.                 $content substr_replace($content$code "\n\n" '</head>'$headEndPosition7);
  57.             }
  58.             $response->setContent($content);
  59.         }
  60.     }
  61. }