Skip to content

Commit c22bac5

Browse files
authored
Merge pull request #47 from experius/feature/IN-23-453
[FEATURE][IN23-453] Add configurable exclude list to be able to exclude url's from the logging
2 parents b0507ba + 9fa1865 commit c22bac5

4 files changed

Lines changed: 55 additions & 18 deletions

File tree

Helper/Settings.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class Settings extends AbstractHelper
1616
const IS_CRON_ENABLED = 'pagenotfound/cron_config/is_cron_enabled';
1717
const CONFIG_DAYS_TO_CLEAN = 'pagenotfound/cron_config/days_to_clean';
1818
const DELETE_NOT_EMPTY_REDIRECT = 'pagenotfound/cron_config/delete_not_empty_redirect';
19-
19+
const ENABLED = 'pagenotfound/general/enabled';
20+
const INCLUDED_PARAMS_REDIRECT = 'pagenotfound/general/included_params_redirect';
21+
const INCLUDED_PARAMS_FROM_URL = 'pagenotfound/general/included_params_from_url';
22+
const EXCLUDE_LIST = 'pagenotfound/general/exclude_list';
2023
/**
2124
* @param Context $context
2225
*/
@@ -50,4 +53,23 @@ public function getDeleteNotEmpyRedirect()
5053
return $this->scopeConfig->getValue(self::DELETE_NOT_EMPTY_REDIRECT);
5154
}
5255

56+
public function isEnabled()
57+
{
58+
return $this->scopeConfig->getValue(self::ENABLED,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
59+
}
60+
61+
public function includedParamsInRedirect()
62+
{
63+
return $this->scopeConfig->getValue(self::INCLUDED_PARAMS_REDIRECT,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
64+
}
65+
66+
public function includedParamsInFromUrl()
67+
{
68+
return $this->scopeConfig->getValue(self::INCLUDED_PARAMS_FROM_URL,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
69+
}
70+
71+
public function getExcludeList()
72+
{
73+
return $this->scopeConfig->getValue(self::EXCLUDE_LIST,\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
74+
}
5375
}

Observer/Controller/ActionPredispatch.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ class ActionPredispatch implements \Magento\Framework\Event\ObserverInterface
2727
protected $urlParts = [];
2828

2929
protected $storeManager;
30+
31+
protected $settings;
32+
3033
private $resultFactory;
3134

3235
public function __construct(
@@ -38,6 +41,7 @@ public function __construct(
3841
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
3942
\Magento\Framework\Controller\ResultFactory $resultFactory,
4043
\Magento\Store\Model\StoreManagerInterface $storeManager,
44+
\Experius\PageNotFound\Helper\Settings $settings
4145

4246
) {
4347
$this->url = $url;
@@ -48,32 +52,34 @@ public function __construct(
4852
$this->scopeConfig = $scopeConfig;
4953
$this->resultFactory = $resultFactory;
5054
$this->storeManager = $storeManager;
51-
55+
$this->settings = $settings;
5256
}
5357

54-
private function isEnabled()
55-
{
56-
$configValue = $this->scopeConfig->getValue('pagenotfound/general/enabled',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
57-
return $configValue ? explode(',',$configValue) : [];
58-
}
5958

60-
private function includedParamsInRedirect()
59+
protected function shouldExcludeUrl($url)
6160
{
62-
$configValue = $this->scopeConfig->getValue('pagenotfound/general/included_params_redirect',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
63-
return $configValue ? explode(',',$configValue) : [];
64-
}
61+
$excludeList = $this->settings->getExcludeList();
62+
if (empty($excludeList)) {
63+
return false;
64+
}
6565

66-
private function includedParamsInFromUrl()
67-
{
68-
$configValue = $this->scopeConfig->getValue('pagenotfound/general/included_params_from_url',\Magento\Store\Model\ScopeInterface::SCOPE_STORE);
69-
return $configValue ? explode(',',$configValue) : [];
66+
$urlPath = parse_url($url, PHP_URL_PATH);
67+
$urlPath = ltrim($urlPath, '/');
68+
69+
foreach ($excludeList as $excludeItem) {
70+
if (strpos($urlPath, $excludeItem) !== false) {
71+
return true;
72+
}
73+
}
74+
75+
return false;
7076
}
7177

7278
public function execute(
7379
\Magento\Framework\Event\Observer $observer
7480
) {
7581

76-
if(!$this->isEnabled()){
82+
if(!$this->settings->isEnabled()){
7783
return;
7884
}
7985

@@ -86,7 +92,11 @@ public function execute(
8692

8793
$this->urlParts = parse_url($this->url->getCurrentUrl());
8894

89-
$this->savePageNotFound($this->getCurrentUrl());
95+
$currentUrl = $this->getCurrentUrl();
96+
97+
if (!$this->shouldExcludeUrl($currentUrl)) {
98+
$this->savePageNotFound($currentUrl);
99+
}
90100

91101
}
92102

@@ -173,7 +183,7 @@ protected function getParams($redirect=true){
173183

174184
$queryArray = $this->getRequest()->getParams();
175185

176-
$unsetParams = ($redirect) ? $this->includedParamsInRedirect() : $this->includedParamsInFromUrl();
186+
$unsetParams = ($redirect) ? $this->settings->includedParamsInRedirect() : $this->settings->includedParamsInFromUrl();
177187

178188
foreach($queryArray as $key=>$value){
179189

etc/adminhtml/system.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
<label>Included Params in To Url</label>
2121
<comment>comma separated. For example language,p,id</comment>
2222
</field>
23+
<field id="exclude_list" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="60" translate="label" type="textarea">
24+
<label>Exclude List</label>
25+
<comment>Comma separated values that should not be logged to database. For example: wp-content,wp-admin,.htaccess,robots.txt</comment>
26+
</field>
2327
</group>
2428
<group id="cron_config" showInDefault="1" showInStore="1" showInWebsite="1" sortOrder="10" translate="label">
2529
<label>config</label>

etc/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<enabled>1</enabled>
77
<included_params_redirect/>
88
<included_params_from_url/>
9+
<exclude_list>wp-content,wp-admin,.htaccess,robots.txt,favicon.ico,apple-touch-icon,sitemap.xml,wp-json,wp-includes,wp-config.php,license.txt,admin,phpmyadmin,backup,backups,old,test,tmp,temp,index.html,index.php</exclude_list>
910
</general>
1011
<cron_config>
1112
<is_cron_enabled>0</is_cron_enabled>

0 commit comments

Comments
 (0)