diff --git a/administrator/components/com_jedchecker/libraries/rules/encoding.ini b/administrator/components/com_jedchecker/libraries/rules/encoding.ini index 95e727e..ab46109 100644 --- a/administrator/components/com_jedchecker/libraries/rules/encoding.ini +++ b/administrator/components/com_jedchecker/libraries/rules/encoding.ini @@ -7,4 +7,4 @@ ; @license GNU General Public License version 2 or later; see LICENSE.txt ; The valid constants to search for -encodings ="base64" +encodings ="base64_decode,base64_encode,zlib_decode,zlib_encode" diff --git a/administrator/components/com_jedchecker/libraries/rules/encoding.php b/administrator/components/com_jedchecker/libraries/rules/encoding.php index 28ce70b..b2ce752 100644 --- a/administrator/components/com_jedchecker/libraries/rules/encoding.php +++ b/administrator/components/com_jedchecker/libraries/rules/encoding.php @@ -15,6 +15,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'; + /** * class JedcheckerRulesEncoding * @@ -45,6 +48,13 @@ class JedcheckerRulesEncoding extends JEDcheckerRule */ protected $description = 'COM_JEDCHECKER_RULE_ENCODING_DESC'; + /** + * Regular expression to look for encoding functions. + * + * @var string + */ + protected $encodingsRegex; + /** * Initiates the file search and check * @@ -52,6 +62,17 @@ class JedcheckerRulesEncoding extends JEDcheckerRule */ public function check() { + // Get the functions to look for + $encodings = explode(',', $this->params->get('encodings')); + + // Prepare regex + foreach ($encodings as $i => $encoding) + { + $encodings[$i] = preg_quote(trim($encoding), '/'); + } + + $this->encodingsRegex = '/' . implode('|', $encodings) . '/i'; + // Find all php files of the extension $files = JFolder::files($this->basedir, '\.php$', true, true); @@ -61,8 +82,7 @@ class JedcheckerRulesEncoding 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_ENCODING')); + // The error has been added by the find() method } } } @@ -77,27 +97,26 @@ class JedcheckerRulesEncoding extends JEDcheckerRule */ protected function find($file) { - $content = (array) file($file); + $content = file_get_contents($file); - // Get the functions to look for - $encodings = explode(',', $this->params->get('encodings')); + // Exclude comments + $content = JEDCheckerHelper::cleanPhpCode( + $content, + JEDCheckerHelper::CLEAN_HTML | JEDCheckerHelper::CLEAN_COMMENTS + ); + $content = JEDCheckerHelper::splitLines($content); - foreach ($encodings as $encoding) + $found = false; + + foreach ($content as $i => $line) { - $encoding = trim($encoding); - - foreach ($content AS $line) + if (preg_match($this->encodingsRegex, $line)) { - // Search for "base64" - $pos_1 = stripos($line, $encoding); - - if ($pos_1 !== false) - { - return true; - } + $found = true; + $this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ENCODING'), $i + 1, $line); } } - return false; + return $found; } }