33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-10-31 18:52:31 +00:00

Merge PR #105 into develop

This commit is contained in:
Llewellyn van der Merwe 2021-08-31 11:47:52 +02:00
commit b4c277ac9b
Signed by: Llewellyn
GPG Key ID: EFC0C720A240551C

View File

@ -14,6 +14,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';
/** /**
* JedcheckerRulesErrorreporting * JedcheckerRulesErrorreporting
* *
@ -46,6 +49,12 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule
*/ */
protected $description = 'COM_JEDCHECKER_RULE_ERRORREPORTING_DESC'; protected $description = 'COM_JEDCHECKER_RULE_ERRORREPORTING_DESC';
/**
* Regular expression to look for error_reporting calls.
*
* @var string
*/
protected $errorreportingRegex;
/** /**
* The ordering value to sort rules in the menu. * The ordering value to sort rules in the menu.
@ -61,6 +70,17 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule
*/ */
public function check() 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 // Find all php files of the extension
$files = JFolder::files($this->basedir, '\.php$', true, true); $files = JFolder::files($this->basedir, '\.php$', true, true);
@ -70,14 +90,13 @@ class JedcheckerRulesErrorreporting extends JEDcheckerRule
// Try to find the base64 use in the file // Try to find the base64 use in the file
if ($this->find($file)) if ($this->find($file))
{ {
// Add as error to the report if it was not found // The error has been added by the find() method
$this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'));
} }
} }
} }
/** /**
* 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 * Not a very clever way of doing this, but it should be fine for now
* *
* @param string $file - The path to the file * @param string $file - The path to the file
@ -86,27 +105,26 @@ class JedcheckerRulesErrorreporting 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 non-code content
$encodings = explode(',', $this->params->get('errorreportings')); $content = JEDCheckerHelper::cleanPhpCode(
$content,
JEDCheckerHelper::CLEAN_HTML | JEDCheckerHelper::CLEAN_COMMENTS | JEDCheckerHelper::CLEAN_STRINGS
);
$content = JEDCheckerHelper::splitLines($content);
foreach ($encodings as $encoding) $found = false;
foreach ($content as $i => $line)
{ {
$encoding = trim($encoding); if (preg_match($this->errorreportingRegex, $line))
foreach ($content AS $line)
{ {
// Search for "base64" $found = true;
$pos_1 = stripos($line, $encoding); $this->report->addWarning($file, JText::_('COM_JEDCHECKER_ERROR_ERRORREPORTING'), $i + 1, $line);
if ($pos_1 !== false)
{
return true;
}
} }
} }
return false; return $found;
} }
} }