Skip to content

Commit

Permalink
added URL excemption rules
Browse files Browse the repository at this point in the history
  • Loading branch information
fonsekaean committed Mar 16, 2020
1 parent 42aeaf9 commit 53757af
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Control/SEORequestProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
}

}

0 comments on commit 53757af

Please sign in to comment.