You are here

public function SecurepagesSubscriber::checkResponseRedirection in Secure Pages 8

Event handler for response processing. Alters redirects if needed.

Parameters

\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event:

File

src/EventSubscriber/SecurepagesSubscriber.php, line 61
Contains \Drupal\securepages\EventSubscriber\SecurepagesSubscriber.

Class

SecurepagesSubscriber

Namespace

Drupal\securepages\EventSubscriber

Code

public function checkResponseRedirection(FilterResponseEvent $event) {
  $response = $event
    ->getResponse();
  if ($response instanceof RedirectResponse) {
    $config = $this->configFactory
      ->get('securepages.settings');
    if ($config
      ->get('enable')) {
      $role_match = Securepages::matchCurrentUser();
      $page_match = Securepages::matchCurrentPath();
      $is_https = $event
        ->getRequest()
        ->isSecure();
      $target = $response
        ->getTargetUrl();
      if ($role_match || $page_match) {

        // If we are not already on HTTPS and the redirect target is HTTP,
        // replace the non-secure base with a secure base.
        if (!$is_https && strpos($target, 'http://') === 0) {
          $target = str_replace(Securepages::getUrl('<front>', [], [], FALSE)
            ->toString(), Securepages::getUrl('<front>')
            ->toString(), $target);
          $response
            ->setTargetUrl($target);
        }
      }
      elseif ($page_match === FALSE && $is_https && $config
        ->get('switch') && strpos($target, 'https://') === 0) {

        // If we are not already on HTTP, should switch to HTTP and the
        // redirect target is HTTPS, replace the secure base with a non-secure
        // base.
        $target = str_replace(Securepages::getUrl('<front>')
          ->toString(), Securepages::getUrl('<front>', [], [], FALSE)
          ->toString(), $target);
        $response
          ->setTargetUrl($target);
      }
    }
  }
}