From 14ba00024a38fa68a5c6ba21451516c00794f71a Mon Sep 17 00:00:00 2001 From: Denis Ryabov Date: Mon, 10 May 2021 20:18:14 +0300 Subject: [PATCH] switch to use of JEDCheckerHelper in framework --- .../libraries/rules/framework.php | 102 ++---------------- 1 file changed, 9 insertions(+), 93 deletions(-) diff --git a/administrator/components/com_jedchecker/libraries/rules/framework.php b/administrator/components/com_jedchecker/libraries/rules/framework.php index 001f4cc..a67ecf3 100644 --- a/administrator/components/com_jedchecker/libraries/rules/framework.php +++ b/administrator/components/com_jedchecker/libraries/rules/framework.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'; + /** * JedcheckerRulesFramework * @@ -132,7 +135,12 @@ class JedcheckerRulesFramework extends JEDcheckerRule return false; } - $cleanContent = preg_split("/(?:\r\n|\n|\r)(?!$)/", $this->cleanNonCode($file)); + $content = file_get_contents($file); + $content = JEDCheckerHelper::cleanPhpCode( + $content, + JEDCheckerHelper::CLEAN_HTML | JEDCheckerHelper::CLEAN_COMMENTS | JEDCheckerHelper::CLEAN_STRINGS + ); + $cleanContent = JEDCheckerHelper::splitLines($content); $result = false; @@ -147,98 +155,6 @@ class JedcheckerRulesFramework extends JEDcheckerRule return $result; } - /** - * @param string $file - * - * @return string - */ - protected function cleanNonCode($file) - { - $content = file_get_contents($file); - - if (!preg_match('/<\?php\s/i', $content, $match, PREG_OFFSET_CAPTURE)) - { - // No PHP code found - return ''; - } - - $pos = $match[0][1]; - $cleanContent = $this->removeContent(substr($content, 0, $pos)); - - while (preg_match('/(?:[\'"]|\/\*|\/\/|\?>)/', $content, $match, PREG_OFFSET_CAPTURE, $pos)) - { - $foundPos = $match[0][1]; - $cleanContent .= substr($content, $pos, $foundPos - $pos); - $pos = $foundPos; - - switch ($match[0][0]) - { - case '"': - case "'": - $q = $match[0][0]; - - if (!preg_match("/$q(?>[^$q\\\\]+|\\\\.)*$q/As", $content, $match, 0, $pos)) - { - return $cleanContent . $q; - } - - $cleanContent .= $q . $this->removeContent($match[0]) . $q; - $pos += strlen($match[0]); - break; - - case '/*': - $cleanContent .= '/*'; - $pos += 2; - - $endPos = strpos($content, '*/', $pos); - - if ($endPos === false) - { - return $cleanContent; - } - - $cleanContent .= $this->removeContent(substr($content, $pos, $endPos - $pos)) . '*/'; - $pos = $endPos + 2; - - break; - - case '//': - $pos += strcspn($content, "\r\n", $pos); - break; - - case '?>': - $cleanContent .= '?>'; - $pos += 2; - - if (!preg_match('/<\?php\s/i', $content, $match, PREG_OFFSET_CAPTURE, $pos)) - { - // No PHP code found (up to the end of the file) - return $cleanContent; - } - - $foundPos = $match[0][1]; - $cleanContent .= $this->removeContent(substr($content, $pos, $foundPos - $pos)) . $match[0][0]; - $pos = $foundPos + strlen($match[0][0]); - - break; - } - } - - return $cleanContent; - } - - /** - * Remove all text content by keeping newline characters only (to preserve line numbers) - * - * @param string $content - * - * @return string - */ - protected function removeContent($content) - { - return str_repeat("\n", substr_count($content, "\n")); - } - /** * runs tests and reports to the appropriate function if strings match. *