33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-11-15 09:44:20 +00:00
jedchecker/administrator/components/com_jedchecker/libraries/rules/xmlmanifest.php

516 lines
12 KiB
PHP
Raw Normal View History

2021-02-02 15:58:29 +00:00
<?php
/**
* @package Joomla.JEDChecker
*
* @copyright Copyright (C) 2021-2022 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');
use Joomla\CMS\Language\Text;
2021-02-02 15:58:29 +00:00
// 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';
2021-02-02 15:58:29 +00:00
/**
* 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';
2021-05-17 20:25:11 +00:00
/**
* The ordering value to sort rules in the menu.
*
* @var integer
*/
public static $ordering = 200;
2021-02-02 15:58:29 +00:00
/**
* List of errors.
*
* @var string[]
*/
protected $errors;
/**
* List of warnings.
*
* @var string[]
*/
protected $warnings;
/**
2021-09-09 07:32:52 +00:00
* List of notices.
*
* @var string[]
*/
2021-09-09 07:32:52 +00:00
protected $notices;
2021-02-02 15:58:29 +00:00
/**
* 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[]
*/
protected $joomlaTypes = 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
$files = JEDCheckerHelper::findManifests($this->basedir);
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.
2023-07-27 16:33:34 +00:00
// Assume that this is not an extension manifest
2021-02-02 15:58:29 +00:00
if (!$xml)
{
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
if (!in_array($type, $this->joomlaTypes, true))
2021-02-02 15:58:29 +00:00
{
$this->report->addError($file, Text::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-04-04 12:12:02 +00:00
$jsonFilename = __DIR__ . '/xmlmanifest/dtd_' . $type . '.json';
2021-04-04 10:59:50 +00:00
2021-04-04 12:12:02 +00:00
if (!is_file($jsonFilename))
2021-02-02 15:58:29 +00:00
{
return true;
}
2021-04-04 10:59:50 +00:00
2021-04-04 12:12:02 +00:00
// Warn if method="upgrade" attribute is not found
2021-04-04 11:24:10 +00:00
if ((string) $xml['method'] !== 'upgrade')
{
$this->report->addWarning($file, Text::_('COM_JEDCHECKER_MANIFEST_MISSED_METHOD_UPGRADE'));
2021-04-04 11:24:10 +00:00
}
switch ($type)
{
case 'language':
case 'module':
case 'template':
// Check 'client' attribute is "site" or "administrator" (for language/module/template only)
$client = (string) $xml['client'];
if (!isset($xml['client']))
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ATTRIBUTE', $xml->getName(), 'client'));
}
elseif ($client !== 'site' && $client !== 'administrator')
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_ATTRIBUTE_VALUE', $xml->getName(), 'client', $client));
}
2023-07-27 16:33:34 +00:00
if ($type === 'module')
{
// Either <element> or "module" attribute (once only) should be present
$elements = $this->collectElements($xml->files, $type);
if (count($elements) >= 2)
{
$this->report->addWarning($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_ATTRIBUTES', 'module'));
2023-07-27 16:33:34 +00:00
}
if (isset($xml->element))
{
$element = (string) $xml->element;
if (count($elements) && $elements[0] !== $element)
{
$this->report->addWarning($file, Text::_('COM_JEDCHECKER_MANIFEST_MODULE_ELEMENT_MISMATCH'));
2023-07-27 16:33:34 +00:00
}
}
else
{
if (count($elements) === 0)
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ELEMENT_ATTRIBUTE', 'module'));
2023-07-27 16:33:34 +00:00
}
}
}
break;
case 'plugin':
// "plugin" attribute (once only) should be present
$elements = $this->collectElements($xml->files, $type);
if (count($elements) >= 2)
{
$this->report->addWarning($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_ATTRIBUTES', 'plugin'));
2023-07-27 16:33:34 +00:00
}
if (count($elements) === 0)
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ELEMENT_ATTRIBUTE', 'plugin'));
2023-07-27 16:33:34 +00:00
}
break;
case 'package':
// Check type-specific attributes
2023-07-27 16:33:34 +00:00
foreach ($xml->files->children() as $item)
{
2023-07-27 16:33:34 +00:00
if (!isset($item['type']))
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ATTRIBUTE', $item->getName(), 'type'));
2023-07-27 16:33:34 +00:00
}
if (!isset($item['id']))
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ATTRIBUTE', $item->getName(), 'id'));
2023-07-27 16:33:34 +00:00
}
switch ((string) $item['type'])
{
case 'plugin':
if (!isset($item['group']))
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ATTRIBUTE', $item->getName(), 'group'));
}
break;
case 'language':
case 'module':
case 'template':
$client = (string) $item['client'];
if (!isset($item['client']))
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_ATTRIBUTE', $item->getName(), 'client'));
}
elseif ($client !== 'site' && $client !== 'administrator')
{
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_ATTRIBUTE_VALUE', $item->getName(), 'client', $client));
}
break;
case 'component':
case 'file':
case 'library':
break;
default:
$this->report->addError($file, Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_TYPE', $item['type']));
}
}
}
2021-04-04 12:12:02 +00:00
$data = json_decode(file_get_contents($jsonFilename), true);
2021-02-02 15:58:29 +00:00
$this->DTDNodeRules = $data['nodes'];
$this->DTDAttrRules = $data['attributes'];
$this->errors = array();
$this->warnings = array();
2021-09-09 07:32:52 +00:00
$this->notices = array();
2021-02-02 15:58:29 +00:00
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));
}
2021-09-09 07:32:52 +00:00
if (count($this->notices))
{
2021-09-09 07:32:52 +00:00
$this->report->addNotice($file, implode('<br />', $this->notices));
}
2021-02-02 15:58:29 +00:00
// All checks passed. Return true
return true;
}
/**
* @param SimpleXMLElement $node XML node object
2021-05-11 18:04:56 +00:00
* @param string $ruleset ruleset name in the DTD array
2021-04-04 10:59:50 +00:00
*
* @return void
2021-02-02 15:58:29 +00:00
*/
protected function validateXml($node, $ruleset)
2021-02-02 15:58:29 +00:00
{
// Get node name
$name = $node->getName();
2021-02-02 15:58:29 +00:00
// Check attributes
$DTDattributes = isset($this->DTDAttrRules[$ruleset]) ? $this->DTDAttrRules[$ruleset] : array();
if (count($DTDattributes) === 0)
{
// No known attributes for this node
foreach ($node->attributes() as $attr)
{
$this->notices[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_ATTRIBUTE', $name, (string) $attr->getName());
}
}
elseif ($DTDattributes[0] !== '*') // Skip node with arbitrary attributes (e.g. "field")
2021-02-02 15:58:29 +00:00
{
foreach ($node->attributes() as $attr)
2021-02-02 15:58:29 +00:00
{
$attrName = (string) $attr->getName();
2021-04-04 10:59:50 +00:00
if (!in_array($attrName, $DTDattributes, true))
{
2021-03-10 22:15:13 +00:00
// The node has unknown attribute
$this->notices[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_ATTRIBUTE', $name, $attrName);
}
2021-02-02 15:58:29 +00:00
}
}
// Check children nodes
$DTDchildRules = isset($this->DTDNodeRules[$ruleset]) ? $this->DTDNodeRules[$ruleset] : array();
// Child node name to ruleset name mapping
$DTDchildToRule = array();
if (count($DTDchildRules) === 0)
2021-02-02 15:58:29 +00:00
{
// No known children for this node
2021-02-02 15:58:29 +00:00
if ($node->count() > 0)
{
$this->notices[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_CHILDREN', $name);
2021-02-02 15:58:29 +00:00
}
}
elseif (!isset($DTDchildRules['*'])) // Skip node with arbitrary children
{
2021-02-02 15:58:29 +00:00
// 1) check required single elements
foreach ($DTDchildRules as $childRuleset => $mode)
2021-02-02 15:58:29 +00:00
{
$child = $childRuleset;
if (strpos($child, ':') !== false)
{
// Split ruleset name into a prefix and the child node name
list ($prefix, $child) = explode(':', $child, 2);
}
// Populate node-to-ruleset mapping
$DTDchildToRule[$child] = $childRuleset;
2021-02-02 15:58:29 +00:00
$count = $node->$child->count();
2021-04-04 10:59:50 +00:00
2021-02-02 15:58:29 +00:00
switch ($mode)
{
case '!':
if ($count === 0)
{
// The node doesn't contain required child element
$this->errors[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_REQUIRED', $name, $child);
}
elseif ($count > 1)
{
// The node contains multiple child elements when single only is expected
$this->errors[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_FOUND', $name, $child);
}
2021-02-02 15:58:29 +00:00
break;
2021-02-02 15:58:29 +00:00
case '=':
if ($count === 0)
{
// The node doesn't contain optional child element
$this->notices[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_MISSED_OPTIONAL', $name, $child);
}
elseif ($count > 1)
{
// The node contains multiple child elements when single only is expected
$this->warnings[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_FOUND', $name, $child);
}
2021-04-04 10:59:50 +00:00
break;
2021-02-02 15:58:29 +00:00
}
}
// 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($DTDchildToRule[$child]))
2021-02-02 15:58:29 +00:00
{
// The node contains unknown child element
$this->notices[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_UNKNOWN_CHILD', $name, $child);
2021-02-02 15:58:29 +00:00
}
else
{
if ($DTDchildRules[$DTDchildToRule[$child]] === '?' && $node->$child->count() > 1)
2021-02-02 15:58:29 +00:00
{
// The node contains multiple child elements when single only is expected
$this->errors[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_MULTIPLE_FOUND', $name, $child);
2021-02-02 15:58:29 +00:00
}
}
}
2021-02-24 11:57:11 +00:00
// 3) check empty elements
foreach ($node as $child)
{
if ($child->count() === 0 && $child->attributes()->count() === 0 && (string) $child === '')
2021-02-24 11:57:11 +00:00
{
$this->notices[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_EMPTY_CHILD', $child->getName());
2021-02-24 11:57:11 +00:00
}
}
2021-02-02 15:58:29 +00:00
}
// 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($DTDchildToRule[$childName]))
2021-04-04 10:59:50 +00:00
{
$this->validateXml($child, $DTDchildToRule[$childName]);
2021-02-02 15:58:29 +00:00
}
}
}
/**
* Extra check for menu nodes
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
{
$this->warnings[] = Text::sprintf('COM_JEDCHECKER_MANIFEST_MENU_UNUSED_ATTRIBUTE', $attrName);
2021-02-02 15:58:29 +00:00
}
}
}
}
2023-07-27 16:33:34 +00:00
/**
* Collect values of $type attribute from all children
* @param SimpleXMLElement $node XML node <files>
* @param string $type Extension's type (plugin or module)
*
* @return array List of found attributes
*/
protected function collectElements($node, $type)
{
$elements = array();
if (isset($node))
{
foreach ($node->children() as $child)
{
if (isset($child[$type]))
{
$elements[] = (string) $child[$type];
}
}
}
return $elements;
}
2021-02-02 15:58:29 +00:00
}