Skip to content

Commit

Permalink
Merge pull request #14 from SilverStripers/fix/added-url-excemption-r…
Browse files Browse the repository at this point in the history
…ules

added URL exception rules
  • Loading branch information
Priyashantha authored Mar 16, 2020
2 parents 42aeaf9 + 53757af commit c420e96
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 c420e96

Please sign in to comment.