31
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-09-29 05:29:08 +00:00

change foreach-based to regex-based search in encoding.php

This commit is contained in:
Denis Ryabov 2021-05-10 20:20:20 +03:00
parent eeaa31a338
commit b59e2aab24

View File

@ -15,6 +15,9 @@ defined('_JEXEC') or die('Restricted access');
// Include the rule base class // Include the rule base class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php'; require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';
// Include the helper class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/libraries/helper.php';
/** /**
* class JedcheckerRulesEncoding * class JedcheckerRulesEncoding
* *
@ -45,6 +48,13 @@ class JedcheckerRulesEncoding extends JEDcheckerRule
*/ */
protected $description = 'COM_JEDCHECKER_RULE_ENCODING_DESC'; protected $description = 'COM_JEDCHECKER_RULE_ENCODING_DESC';
/**
* Regular expression to look for encoding functions.
*
* @var string
*/
protected $encodingsRegex;
/** /**
* Initiates the file search and check * Initiates the file search and check
* *
@ -52,6 +62,17 @@ class JedcheckerRulesEncoding extends JEDcheckerRule
*/ */
public function check() 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 // Find all php files of the extension
$files = JFolder::files($this->basedir, '\.php$', true, true); $files = JFolder::files($this->basedir, '\.php$', true, true);
@ -76,27 +97,23 @@ class JedcheckerRulesEncoding extends JEDcheckerRule
*/ */
protected function find($file) protected function find($file)
{ {
$content = (array) file($file); $content = file_get_contents($file);
// Get the functions to look for // Exclude comments
$encodings = explode(',', $this->params->get('encodings')); $content = JEDCheckerHelper::cleanPhpCode(
$encodings = array_map('trim', $encodings); $content,
JEDCheckerHelper::CLEAN_HTML | JEDCheckerHelper::CLEAN_COMMENTS
);
$content = JEDCheckerHelper::splitLines($content);
$found = false; $found = false;
foreach ($content as $i => $line) foreach ($content as $i => $line)
{ {
foreach ($encodings as $encoding) if (preg_match($this->encodingsRegex, $line))
{ {
// Search for "base64" $found = true;
$pos_1 = stripos($line, $encoding); $this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ENCODING'), $i + 1, $line);
if ($pos_1 !== false)
{
$found = true;
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ENCODING'), $i + 1, $line);
break;
}
} }
} }