class SecurepagesSubscriber in Secure Pages 8
Hierarchy
- class \Drupal\securepages\EventSubscriber\SecurepagesSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of SecurepagesSubscriber
1 string reference to 'SecurepagesSubscriber'
1 service uses SecurepagesSubscriber
File
- src/
EventSubscriber/ SecurepagesSubscriber.php, line 19 - Contains \Drupal\securepages\EventSubscriber\SecurepagesSubscriber.
Namespace
Drupal\securepages\EventSubscriberView source
class SecurepagesSubscriber implements EventSubscriberInterface {
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new SecurepagesSubscriber.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$this->configFactory = $config_factory;
}
/**
* Event handler for request processing. Redirects as needed.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
*/
public function checkRequestRedirection(GetResponseEvent $event) {
$config = $this->configFactory
->get('securepages.settings');
if ($config
->get('enable') && php_sapi_name() != 'cli') {
$redirect = Securepages::checkRedirect();
if ($redirect !== NULL) {
$request = $event
->getRequest();
$route_match = RouteMatch::createFromRequest($request);
$route_name = $route_match
->getRouteName();
$route_parameters = $route_match
->getRawParameters()
->all();
$qs = $request
->getQueryString();
$url = Securepages::getUrl($route_name, $route_parameters, [], $redirect)
->toString() . ($qs ? '?' . $qs : '');
$event
->setResponse(new TrustedRedirectResponse($url));
}
}
}
/**
* Event handler for response processing. Alters redirects if needed.
*
* @param \Symfony\Component\HttpKernel\Event\FilterResponseEvent $event
*/
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);
}
}
}
}
/**
* {@inheritdoc}
*/
static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = array(
'checkRequestRedirection',
);
$events[KernelEvents::RESPONSE][] = array(
'checkResponseRedirection',
);
return $events;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
SecurepagesSubscriber:: |
protected | property | The config factory. | |
SecurepagesSubscriber:: |
public | function | Event handler for request processing. Redirects as needed. | |
SecurepagesSubscriber:: |
public | function | Event handler for response processing. Alters redirects if needed. | |
SecurepagesSubscriber:: |
static | function | Returns an array of event names this subscriber wants to listen to. | |
SecurepagesSubscriber:: |
public | function | Constructs a new SecurepagesSubscriber. |