From 53757af8edf167bb8b6eafbfafbbcd2d67e54b40 Mon Sep 17 00:00:00 2001 From: Nivanka Fonseka Date: Mon, 16 Mar 2020 08:43:03 +0530 Subject: [PATCH] added URL excemption rules --- src/Control/SEORequestProcessor.php | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/Control/SEORequestProcessor.php b/src/Control/SEORequestProcessor.php index 4d7945f..2d09339 100644 --- a/src/Control/SEORequestProcessor.php +++ b/src/Control/SEORequestProcessor.php @@ -13,10 +13,18 @@ use SilverStripe\Control\HTTPRequest; use SilverStripe\Control\HTTPResponse; use SilverStripe\Control\Middleware\HTTPMiddleware; +use SilverStripe\Core\Config\Configurable; use SilverStripe\SiteConfig\SiteConfig; class SEORequestProcessor implements HTTPMiddleware { + use Configurable; + + private static $exclude_rules = [ + 'Security/*', + 'admin/*', + 'dev/*' + ]; public function processInputs($body) { @@ -77,10 +85,24 @@ private function canAddSEOScripts(HTTPRequest $request, HTTPResponse $response) $url = ltrim($request->getURL(), '/'); $headers = $response->getHeaders(); + $rules = self::config()->get('exclude_rules'); + if (count($rules)) { + foreach ($rules as $rule) { + if (substr($rule, -1) == '*') { + if (strpos($url, substr($rule, 0, -1)) === 0) { + return false; + } + } elseif (substr($rule, 0, 1) == '*') { + if (substr($url, -1 * strlen(substr($rule, 0, 1))) == substr($rule, 0, 1)) { + return false; + } + } elseif (ltrim($url) == ltrim($rule)) { + return false; + } + } + } + return isset($headers['content-type']) - && strpos($headers['content-type'], 'text/html;') !== false - && strpos($url, 'admin') === false - && strpos($url, 'dev') === false; + && strpos($headers['content-type'], 'text/html;') !== false; } - }