AnonymousRedirectSubscriber.php in Anonymous Redirect 8.2
File
src/EventSubscriber/AnonymousRedirectSubscriber.php
View source
<?php
namespace Drupal\anonymous_redirect\EventSubscriber;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Url;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpKernel\KernelEvents;
use Drupal\Core\Path\PathMatcher;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\Core\Language\LanguageManagerInterface;
class AnonymousRedirectSubscriber extends ControllerBase implements EventSubscriberInterface {
protected $account;
protected $pathMatcher;
protected $languageManager;
public function __construct(AccountInterface $account, PathMatcher $pathMatcher, LanguageManagerInterface $languageManager) {
$this->account = $account;
$this->pathMatcher = $pathMatcher;
$this->languageManager = $languageManager;
}
public static function getSubscribedEvents() {
$events[KernelEvents::REQUEST][] = [
'redirectAnonymous',
100,
];
return $events;
}
public function redirectAnonymous(GetResponseEvent $event) {
$config = $this
->config('anonymous_redirect.settings');
$redirectEnabled = $config
->get('enable_redirect');
if (!$redirectEnabled || $this->account
->isAuthenticated() || $this
->state()
->get('system.maintenance_mode')) {
return;
}
$redirectUrl = $config
->get('redirect_url');
$redirectUrlOverridesText = $config
->get('redirect_url_overrides');
$redirectUrlOverrides = $redirectUrlOverridesText ? explode("\r\n", $redirectUrlOverridesText) : [];
$currentPath = $event
->getRequest()
->getPathInfo();
$currentLanguagePrefix = $this
->languageManager()
->getCurrentLanguage()
->getId();
if (substr($currentPath, 1, strlen($currentLanguagePrefix)) == $currentLanguagePrefix) {
$currentPath = substr($currentPath, strlen($currentLanguagePrefix) + 1);
}
if (in_array($currentPath, $redirectUrlOverrides) || $this->pathMatcher
->matchPath($currentPath, $redirectUrlOverridesText)) {
return;
}
if (UrlHelper::isExternal($redirectUrl)) {
$event
->setResponse(new TrustedRedirectResponse($redirectUrl));
return;
}
if ($this
->isFrontPage($redirectUrl) && $currentPath !== Url::fromRoute("<front>")
->toString()) {
$event
->setResponse(new RedirectResponse(Url::fromRoute("<front>")
->toString()));
}
if ($this
->isFrontPage($redirectUrl) == FALSE && strpos($currentPath, $redirectUrl) === FALSE) {
if ($redirectUrl === Url::fromRoute('user.login')
->toString() && $currentPath !== '/') {
$options = [
'query' => [
'destination' => $currentPath,
],
];
$redirectUrl = Url::fromUserInput($redirectUrl, $options)
->toString();
}
$event
->setResponse(new RedirectResponse(Url::fromUri('internal:' . $redirectUrl)
->toString()));
}
}
public function isFrontPage($urlString) {
if ($urlString == "<front>") {
return TRUE;
}
return FALSE;
}
}