2021-05-10 17:06:58 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.JEDChecker
|
|
|
|
*
|
|
|
|
* @copyright Copyright (C) 2021 Open Source Matters, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class JedcheckerHelper
|
|
|
|
*
|
|
|
|
* This is a helper class with a set of static methods used by other JEDChecker classes
|
|
|
|
*
|
2021-05-15 13:11:57 +00:00
|
|
|
* @since 2.4
|
2021-05-10 17:06:58 +00:00
|
|
|
*/
|
|
|
|
abstract class JEDCheckerHelper
|
|
|
|
{
|
|
|
|
const CLEAN_HTML = 1;
|
|
|
|
const CLEAN_COMMENTS = 2;
|
|
|
|
const CLEAN_STRINGS = 4;
|
|
|
|
|
2021-05-11 11:40:19 +00:00
|
|
|
/**
|
|
|
|
* Returns XML manifest files in the package (sorted by depth)
|
|
|
|
*
|
|
|
|
* @param string $basedir Extension's directory
|
|
|
|
*
|
|
|
|
* @return string[]
|
2021-05-15 19:15:06 +00:00
|
|
|
* @since 2.4
|
2021-05-11 11:40:19 +00:00
|
|
|
*/
|
|
|
|
public static function findManifests($basedir)
|
|
|
|
{
|
|
|
|
// Find all XML files of the extension
|
|
|
|
$files = JFolder::files($basedir, '\.xml$', true, true);
|
|
|
|
|
|
|
|
$excludeList = array();
|
|
|
|
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
$xml = simplexml_load_file($file);
|
|
|
|
|
|
|
|
if (!$xml || ($xml->getName() !== 'extension' && $xml->getName() !== 'install'))
|
|
|
|
{
|
|
|
|
// Exclude non-install-manifest XML files
|
|
|
|
$excludeList[] = $file;
|
|
|
|
}
|
|
|
|
elseif ((string) $xml['type'] === 'component' && isset($xml->administration->files['folder']))
|
|
|
|
{
|
|
|
|
// Exclude possible duplicates of manifest in components
|
|
|
|
$excludeList[] = dirname($file) . '/' . trim($xml->administration->files['folder'], ' /') . '/' . basename($file);
|
|
|
|
}
|
|
|
|
elseif ((string) $xml['type'] === 'file' && isset($xml->fileset->files))
|
|
|
|
{
|
|
|
|
// Exclude possible duplicates of file-type extension manifest
|
|
|
|
foreach ($xml->fileset->files as $child)
|
|
|
|
{
|
|
|
|
if (isset($child['folder']))
|
|
|
|
{
|
|
|
|
$excludeList[] = dirname($file) . '/' . trim($child['folder'], ' /') . '/' . basename($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$files = array_diff($files, $excludeList);
|
|
|
|
usort($files, array(__CLASS__, 'sortPathsCmp'));
|
|
|
|
|
|
|
|
return $files;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort directories by depth
|
|
|
|
*
|
|
|
|
* @param string $path1 1st path to compare
|
|
|
|
* @param string $path2 2nd path to compare
|
|
|
|
*
|
|
|
|
* @return integer
|
2021-05-15 19:15:06 +00:00
|
|
|
* @since 2.4
|
2021-05-11 11:40:19 +00:00
|
|
|
*/
|
|
|
|
public static function sortPathsCmp($path1, $path2)
|
|
|
|
{
|
|
|
|
$depth1 = substr_count($path1, '/');
|
|
|
|
$depth2 = substr_count($path2, '/');
|
|
|
|
|
|
|
|
return ($depth1 === $depth2) ? strcmp($path1, $path2) : ($depth1 - $depth2);
|
|
|
|
}
|
|
|
|
|
2021-05-10 17:06:58 +00:00
|
|
|
/**
|
|
|
|
* Split text into lines
|
|
|
|
*
|
|
|
|
* @param string $content Text to split
|
|
|
|
*
|
|
|
|
* @return string[]
|
2021-05-15 19:15:06 +00:00
|
|
|
* @since 2.4
|
2021-05-10 17:06:58 +00:00
|
|
|
*/
|
|
|
|
public static function splitLines($content)
|
|
|
|
{
|
|
|
|
// Split on one of EOL characters (except of EOL at the end of text)
|
|
|
|
return preg_split("/(?:\r\n|\n|\r)(?!$)/", $content);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get extension name (element)
|
|
|
|
*
|
|
|
|
* @param SimpleXMLElement $xml XML Manifest
|
|
|
|
*
|
|
|
|
* @return string
|
2021-05-15 19:15:06 +00:00
|
|
|
* @since 2.4
|
2021-05-10 17:06:58 +00:00
|
|
|
*/
|
|
|
|
public static function getElementName($xml)
|
|
|
|
{
|
|
|
|
$type = (string) $xml['type'];
|
|
|
|
|
|
|
|
if (isset($xml->element))
|
|
|
|
{
|
|
|
|
$extension = (string) $xml->element;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$extension = (string) $xml->name;
|
|
|
|
|
|
|
|
if (isset($xml->files))
|
|
|
|
{
|
|
|
|
foreach ($xml->files->children() as $child)
|
|
|
|
{
|
|
|
|
if (isset($child[$type]))
|
|
|
|
{
|
|
|
|
$extension = (string) $child[$type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$extension = strtolower(JFilterInput::getInstance()->clean($extension, 'cmd'));
|
|
|
|
|
|
|
|
if ($type === 'component' && strpos($extension, 'com_') !== 0)
|
|
|
|
{
|
|
|
|
$extension = 'com_' . $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $extension;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes HTML, comments, and/or strings content keeping EOL characters to preserve line numbers
|
|
|
|
*
|
|
|
|
* @param string $content PHP sources
|
|
|
|
* @param int $options Bitwise set of options
|
|
|
|
*
|
|
|
|
* @return string
|
2021-05-15 19:15:06 +00:00
|
|
|
* @since 2.4
|
2021-05-10 17:06:58 +00:00
|
|
|
*/
|
|
|
|
public static function cleanPhpCode($content, $options = self::CLEAN_HTML | self::CLEAN_COMMENTS)
|
|
|
|
{
|
|
|
|
$isCleanHtml = $options & self::CLEAN_HTML;
|
|
|
|
$isCleanComments = $options & self::CLEAN_COMMENTS;
|
|
|
|
$isCleanStrings = $options & self::CLEAN_STRINGS;
|
|
|
|
|
2021-05-13 18:56:05 +00:00
|
|
|
if (!preg_match('/<\?(?:php\s|\s|=)/i', $content, $match, PREG_OFFSET_CAPTURE))
|
2021-05-10 17:06:58 +00:00
|
|
|
{
|
|
|
|
// No PHP code found
|
|
|
|
return $isCleanHtml ? '' : $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pos = $match[0][1];
|
|
|
|
$code = substr($content, 0, $pos);
|
|
|
|
$cleanContent = $isCleanHtml ? self::removeContent($code) : $code;
|
|
|
|
|
2021-05-21 18:26:02 +00:00
|
|
|
while (preg_match('/(?:[\'"]|\/\*|\/\/|#|\?>)/', $content, $match, PREG_OFFSET_CAPTURE, $pos))
|
2021-05-10 17:06:58 +00:00
|
|
|
{
|
|
|
|
$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 . ($isCleanStrings ? $q : substr($content, $pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = $match[0];
|
|
|
|
$cleanContent .= $isCleanStrings ? $q . self::removeContent($code) . $q : $code;
|
|
|
|
$pos += strlen($code);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '/*':
|
|
|
|
$cleanContent .= '/*';
|
|
|
|
$pos += 2;
|
|
|
|
|
|
|
|
$endPos = strpos($content, '*/', $pos);
|
|
|
|
|
|
|
|
if ($endPos === false)
|
|
|
|
{
|
|
|
|
return $isCleanComments ? $cleanContent : $cleanContent . substr($content, $pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = substr($content, $pos, $endPos - $pos);
|
|
|
|
$cleanContent .= $isCleanComments ? self::removeContent($code) : $code;
|
|
|
|
$cleanContent .= '*/';
|
|
|
|
$pos = $endPos + 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '//':
|
2021-05-21 18:26:02 +00:00
|
|
|
case '#':
|
2021-05-10 17:06:58 +00:00
|
|
|
$commentLen = strcspn($content, "\r\n", $pos);
|
2021-05-21 18:26:02 +00:00
|
|
|
$endPhpPos = strpos($content, '?>', $pos);
|
|
|
|
|
|
|
|
if ($endPhpPos !== false && $endPhpPos < $pos + $commentLen)
|
|
|
|
{
|
|
|
|
$commentLen = $endPhpPos - $pos;
|
|
|
|
}
|
2021-05-10 17:06:58 +00:00
|
|
|
|
|
|
|
if (!$isCleanComments)
|
|
|
|
{
|
|
|
|
$cleanContent .= substr($content, $pos, $commentLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
$pos += $commentLen;
|
2021-05-21 18:26:02 +00:00
|
|
|
|
2021-05-10 17:06:58 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '?>':
|
|
|
|
$cleanContent .= '?>';
|
|
|
|
$pos += 2;
|
|
|
|
|
2021-05-13 18:56:05 +00:00
|
|
|
if (!preg_match('/<\?(?:php\s|\s|=)/i', $content, $match, PREG_OFFSET_CAPTURE, $pos))
|
2021-05-10 17:06:58 +00:00
|
|
|
{
|
|
|
|
// No PHP code found (up to the end of the file)
|
|
|
|
return $cleanContent . ($isCleanHtml ? '' : substr($content, $pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
$foundPos = $match[0][1];
|
|
|
|
$code = substr($content, $pos, $foundPos - $pos);
|
|
|
|
$cleanContent .= $isCleanHtml ? self::removeContent($code) : $code;
|
|
|
|
|
|
|
|
$phpPreamble = $match[0][0];
|
|
|
|
$cleanContent .= $phpPreamble;
|
|
|
|
$pos = $foundPos + strlen($phpPreamble);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$cleanContent .= substr($content, $pos);
|
|
|
|
|
|
|
|
return $cleanContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all text content by keeping newline characters only (to preserve line numbers)
|
|
|
|
*
|
|
|
|
* @param string $content Partial content
|
|
|
|
*
|
|
|
|
* @return string
|
2021-05-15 19:15:06 +00:00
|
|
|
* @since 2.4
|
2021-05-10 17:06:58 +00:00
|
|
|
*/
|
|
|
|
protected static function removeContent($content)
|
|
|
|
{
|
|
|
|
return str_repeat("\n", substr_count($content, "\n"));
|
|
|
|
}
|
|
|
|
}
|