2021-02-02 15:58:29 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.JEDChecker
|
|
|
|
*
|
2021-03-11 12:56:27 +00:00
|
|
|
* @copyright Copyright (C) 2021 Open Source Matters, Inc. All rights reserved.
|
2021-02-02 15:58:29 +00:00
|
|
|
*
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined('_JEXEC') or die('Restricted access');
|
|
|
|
|
|
|
|
|
|
|
|
// Include the rule base class
|
|
|
|
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class JedcheckerRulesXMLManifest
|
|
|
|
*
|
2021-04-04 10:59:50 +00:00
|
|
|
* This class validates all XML manifests
|
2021-02-02 15:58:29 +00:00
|
|
|
*
|
2021-02-02 16:10:59 +00:00
|
|
|
* @since 2.3
|
2021-02-02 15:58:29 +00:00
|
|
|
*/
|
|
|
|
class JedcheckerRulesXMLManifest extends JEDcheckerRule
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The formal ID of this rule. For example: SE1.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $id = 'MANIFEST';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The title or caption of this rule.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title = 'COM_JEDCHECKER_MANIFEST';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The description of this rule.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'COM_JEDCHECKER_MANIFEST_DESC';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of errors.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $errors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of warnings.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $warnings;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rules for XML nodes
|
|
|
|
* ? - single, optional
|
|
|
|
* = - single, required, warning if missed
|
|
|
|
* ! - single, required, error if missed
|
|
|
|
* * - multiple, optional
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $DTDNodeRules;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rules for attributes
|
|
|
|
* (list of allowed attributes)
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $DTDAttrRules;
|
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
/**
|
|
|
|
* List of extension types
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-02-02 15:58:29 +00:00
|
|
|
protected $types = array(
|
2021-04-04 10:59:50 +00:00
|
|
|
'component', 'file', 'language', 'library',
|
|
|
|
'module', 'package', 'plugin', 'template'
|
2021-02-02 15:58:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates the search and check
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
|
|
|
// Find all XML files of the extension
|
2021-02-13 20:53:36 +00:00
|
|
|
$files = JFolder::files($this->basedir, '\.xml$', true, true);
|
2021-02-02 15:58:29 +00:00
|
|
|
|
|
|
|
// Iterate through all the xml files
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
|
|
|
// Try to check the file
|
|
|
|
$this->find($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a file and validate XML manifest
|
|
|
|
*
|
|
|
|
* @param string $file - The path to the file
|
|
|
|
*
|
|
|
|
* @return boolean True if the manifest file was found, otherwise False.
|
|
|
|
*/
|
|
|
|
protected function find($file)
|
|
|
|
{
|
2021-02-13 20:53:09 +00:00
|
|
|
$xml = simplexml_load_file($file);
|
2021-02-02 15:58:29 +00:00
|
|
|
|
|
|
|
// Failed to parse the xml file.
|
|
|
|
// Assume that this is not a extension manifest
|
|
|
|
if (!$xml)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if this is an extension manifest
|
|
|
|
if ($xml->getName() !== 'extension')
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
// Check extension type
|
2021-02-02 15:58:29 +00:00
|
|
|
$type = (string) $xml['type'];
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
if (!in_array($type, $this->types, true))
|
|
|
|
{
|
|
|
|
$this->report->addError($file, JText::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_TYPE', $type));
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
// Load DTD-like data for this extension type
|
2021-02-23 21:27:50 +00:00
|
|
|
$json_filename = __DIR__ . '/xmlmanifest/dtd_' . $type . '.json';
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
if (!is_file($json_filename))
|
|
|
|
{
|
|
|
|
$this->report->addError($file, JText::sprintf('COM_JEDCHECKER_MANIFEST_TYPE_NOT_ACCEPTED', $type));
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
$data = json_decode(file_get_contents($json_filename), true);
|
|
|
|
$this->DTDNodeRules = $data['nodes'];
|
|
|
|
$this->DTDAttrRules = $data['attributes'];
|
|
|
|
|
|
|
|
$this->errors = array();
|
|
|
|
$this->warnings = array();
|
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
// Validate manifest
|
2021-02-02 15:58:29 +00:00
|
|
|
$this->validateXml($xml, 'extension');
|
|
|
|
|
|
|
|
if (count($this->errors))
|
|
|
|
{
|
|
|
|
$this->report->addError($file, implode('<br />', $this->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->warnings))
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, implode('<br />', $this->warnings));
|
|
|
|
}
|
|
|
|
|
|
|
|
// All checks passed. Return true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-04 10:59:50 +00:00
|
|
|
* @param SimpleXMLElement $node XML node object
|
|
|
|
* @param string $name XML node name
|
|
|
|
*
|
|
|
|
* @return void
|
2021-02-02 15:58:29 +00:00
|
|
|
*/
|
|
|
|
protected function validateXml($node, $name)
|
|
|
|
{
|
|
|
|
// Check attributes
|
|
|
|
$DTDattributes = isset($this->DTDAttrRules[$name]) ? $this->DTDAttrRules[$name] : array();
|
2021-02-23 21:32:28 +00:00
|
|
|
|
|
|
|
if (isset($DTDattributes[0]) && $DTDattributes[0] !== '*')
|
2021-02-02 15:58:29 +00:00
|
|
|
{
|
2021-02-23 21:32:28 +00:00
|
|
|
foreach ($node->attributes() as $attr)
|
2021-02-02 15:58:29 +00:00
|
|
|
{
|
2021-04-04 10:59:50 +00:00
|
|
|
$attrName = (string)$attr->getName();
|
2021-02-23 21:32:28 +00:00
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
if (!in_array($attrName, $DTDattributes, true))
|
2021-02-23 21:32:28 +00:00
|
|
|
{
|
2021-03-10 22:15:13 +00:00
|
|
|
// The node has unknown attribute
|
2021-04-04 10:59:50 +00:00
|
|
|
$this->warnings[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_ATTRIBUTE', $name, $attrName);
|
2021-02-23 21:32:28 +00:00
|
|
|
}
|
2021-02-02 15:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check children nodes
|
|
|
|
if (!isset($this->DTDNodeRules[$name]))
|
|
|
|
{
|
|
|
|
// No children
|
|
|
|
if ($node->count() > 0)
|
|
|
|
{
|
|
|
|
$this->warnings[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_CHILDREN', $name);
|
|
|
|
}
|
2021-02-23 21:33:37 +00:00
|
|
|
}
|
|
|
|
elseif (!isset($this->DTDNodeRules[$name]['*']))
|
|
|
|
{
|
2021-02-02 15:58:29 +00:00
|
|
|
$DTDchildren = $this->DTDNodeRules[$name];
|
|
|
|
|
|
|
|
// 1) check required single elements
|
|
|
|
|
|
|
|
foreach ($DTDchildren as $child => $mode)
|
|
|
|
{
|
|
|
|
$count = $node->$child->count();
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
switch ($mode)
|
|
|
|
{
|
|
|
|
case '!':
|
|
|
|
$errors =& $this->errors;
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
$errors =& $this->warnings;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
continue 2;
|
|
|
|
}
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
if ($count === 0)
|
|
|
|
{
|
|
|
|
$errors[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_REQUIRED', $name, $child);
|
|
|
|
}
|
|
|
|
elseif ($count > 1)
|
|
|
|
{
|
|
|
|
$errors[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_FOUND', $name, $child);
|
|
|
|
}
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
unset($errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2) check unknown/multiple elements
|
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
// Collect unique child node names
|
|
|
|
$childNames = array();
|
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
foreach ($node as $child)
|
|
|
|
{
|
2021-04-04 10:59:50 +00:00
|
|
|
$childNames[$child->getName()] = 1;
|
2021-02-02 15:58:29 +00:00
|
|
|
}
|
2021-04-04 10:59:50 +00:00
|
|
|
$childNames = array_keys($childNames);
|
2021-02-02 15:58:29 +00:00
|
|
|
|
2021-04-04 10:59:50 +00:00
|
|
|
foreach ($childNames as $child)
|
2021-02-02 15:58:29 +00:00
|
|
|
{
|
|
|
|
if (!isset($DTDchildren[$child]))
|
|
|
|
{
|
|
|
|
$this->warnings[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_CHILD', $name, $child);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($DTDchildren[$child] === '?' && $node->$child->count() > 1)
|
|
|
|
{
|
|
|
|
$this->errors[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_FOUND', $name, $child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extra checks (if exist)
|
|
|
|
$method = 'validateXml' . $name;
|
2021-04-04 10:59:50 +00:00
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
if (method_exists($this, $method))
|
|
|
|
{
|
|
|
|
$this->$method($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recursion
|
|
|
|
foreach ($node as $child)
|
|
|
|
{
|
2021-04-04 10:59:50 +00:00
|
|
|
$childName = $child->getName();
|
|
|
|
|
|
|
|
if (isset($this->DTDNodeRules[$childName]))
|
|
|
|
{
|
|
|
|
$this->validateXml($child, $childName);
|
2021-02-02 15:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-04 10:59:50 +00:00
|
|
|
* @param SimpleXMLElement $node XML node
|
|
|
|
*
|
|
|
|
* @return void
|
2021-02-02 15:58:29 +00:00
|
|
|
*/
|
|
|
|
protected function validateXmlMenu($node)
|
|
|
|
{
|
|
|
|
if (isset($node['link']))
|
|
|
|
{
|
2021-03-10 22:15:13 +00:00
|
|
|
// The "link" attribute overrides any other link-related attributes (warn if they present)
|
2021-04-04 10:59:50 +00:00
|
|
|
$skipAttrs = array('act', 'controller', 'layout', 'sub', 'task', 'view');
|
|
|
|
|
2021-02-02 15:58:29 +00:00
|
|
|
foreach ($node->attributes() as $attr)
|
|
|
|
{
|
2021-04-04 10:59:50 +00:00
|
|
|
$attrName = $attr->getName();
|
|
|
|
|
|
|
|
if (in_array($attrName, $skipAttrs, true))
|
2021-02-02 15:58:29 +00:00
|
|
|
{
|
2021-04-04 10:59:50 +00:00
|
|
|
$this->warnings[] = JText::sprintf('COM_JEDCHECKER_MANIFEST_MENU_UNUSED_ATTRIBUTE', $attrName);
|
2021-02-02 15:58:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|