From 14138dd097ccb6bd4b96c0239075df811be67b80 Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Tue, 23 Feb 2021 23:09:31 +0300 Subject: [PATCH 1/5] show line number and code in the errorreporting rule --- .../libraries/rules/errorreporting.php | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php index f74f669..d4e3f21 100644 --- a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php +++ b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php @@ -63,8 +63,7 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule // Try to find the base64 use in the file if ($this->find($file)) { - // Add as error to the report if it was not found - $this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING')); + // The error has been added by the find() method } } } @@ -84,22 +83,28 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule // Get the functions to look for $encodings = explode(',', $this->params->get('errorreportings')); - foreach ($encodings as $encoding) + foreach ($encodings as $i => $encoding) { - $encoding = trim($encoding); + $encodings[$i] = trim($encoding); + } - foreach ($content AS $line) + $found = false; + + foreach ($content as $i => $line) + { + foreach ($encodings as $encoding) { - // Search for "base64" $pos_1 = stripos($line, $encoding); if ($pos_1 !== false) { - return true; + $found = true; + $this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line); + break; } } } - return false; + return $found; } } From 606fb5225c3ca5526a37fb7eed72897279db1569 Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Sun, 4 Apr 2021 11:28:30 +0300 Subject: [PATCH 2/5] fix comment --- .../com_jedchecker/libraries/rules/errorreporting.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php index d4e3f21..df8dedb 100644 --- a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php +++ b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php @@ -69,7 +69,7 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule } /** - * Reads a file and searches for any encoding function defined in the params + * Reads a file and searches for any function defined in the params * Not a very clever way of doing this, but it should be fine for now * * @param string $file - The path to the file From 8f1bb04d8d351e4060f132c4445eaa1e0ad7c108 Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Tue, 2 Mar 2021 01:23:55 +0300 Subject: [PATCH 3/5] correct variable name --- .../libraries/rules/errorreporting.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php index df8dedb..93ffa1f 100644 --- a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php +++ b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php @@ -81,20 +81,16 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule $content = (array) file($file); // Get the functions to look for - $encodings = explode(',', $this->params->get('errorreportings')); - - foreach ($encodings as $i => $encoding) - { - $encodings[$i] = trim($encoding); - } + $errorreportings = explode(',', $this->params->get('errorreportings')); + $errorreportings = array_map('trim', $errorreportings); $found = false; foreach ($content as $i => $line) { - foreach ($encodings as $encoding) + foreach ($errorreportings as $errorreporting) { - $pos_1 = stripos($line, $encoding); + $pos_1 = stripos($line, $errorreporting); if ($pos_1 !== false) { From a8ea5b75c2f6e57df9c1d0f57f4c8fc5953e581b Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Tue, 11 May 2021 18:38:26 +0300 Subject: [PATCH 4/5] Mitigate ErrorReporting rule to warning-level --- .../com_jedchecker/libraries/rules/errorreporting.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php index 93ffa1f..a6e3062 100644 --- a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php +++ b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php @@ -95,7 +95,7 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule if ($pos_1 !== false) { $found = true; - $this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line); + $this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line); break; } } From 893beace318d9e9f29096b5be9de0e772b17de9b Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Mon, 10 May 2021 20:20:42 +0300 Subject: [PATCH 5/5] change foreach-based to regex-based search in errorreporting.php --- .../libraries/rules/errorreporting.php | 43 +++++++++++++------ 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php index a6e3062..6f6d100 100644 --- a/administrator/components/com_jedchecker/libraries/rules/errorreporting.php +++ b/administrator/components/com_jedchecker/libraries/rules/errorreporting.php @@ -14,6 +14,9 @@ defined('_JEXEC') or die('Restricted access'); // Include the rule base class require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php'; +// Include the helper class +require_once JPATH_COMPONENT_ADMINISTRATOR . '/libraries/helper.php'; + /** * JedcheckerRulesErrorreporting * @@ -46,6 +49,12 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule */ protected $description = 'COM_JEDCHECKER_RULE_ERRORREPORTING_DESC'; + /** + * Regular expression to look for error_reporting calls. + * + * @var string + */ + protected $errorreportingRegex; /** * Initiates the file search and check @@ -54,6 +63,17 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule */ public function check() { + // Get the functions to look for + $codes = explode(',', $this->params->get('errorreportings')); + + // Prepare regex + foreach ($codes as $i => $encoding) + { + $codes[$i] = preg_quote(trim($encoding), '/'); + } + + $this->errorreportingRegex = '/' . implode('|', $codes) . '/i'; + // Find all php files of the extension $files = JFolder::files($this->basedir, '\.php$', true, true); @@ -78,26 +98,23 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule */ protected function find($file) { - $content = (array) file($file); + $content = file_get_contents($file); - // Get the functions to look for - $errorreportings = explode(',', $this->params->get('errorreportings')); - $errorreportings = array_map('trim', $errorreportings); + // Exclude non-code content + $content = JEDCheckerHelper::cleanPhpCode( + $content, + JEDCheckerHelper::CLEAN_HTML | JEDCheckerHelper::CLEAN_COMMENTS | JEDCheckerHelper::CLEAN_STRINGS + ); + $content = JEDCheckerHelper::splitLines($content); $found = false; foreach ($content as $i => $line) { - foreach ($errorreportings as $errorreporting) + if (preg_match($this->errorreportingRegex, $line)) { - $pos_1 = stripos($line, $errorreporting); - - if ($pos_1 !== false) - { - $found = true; - $this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line); - break; - } + $found = true; + $this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line); } }