33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-12-25 03:36:00 +00:00
jedchecker/administrator/components/com_jedchecker/libraries/rules/xmlfiles.php

398 lines
8.8 KiB
PHP
Raw Normal View History

<?php
/**
* @package Joomla.JEDChecker
*
2021-03-11 12:59:54 +00:00
* @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');
// 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';
/**
* class JedcheckerRulesXMLFiles
*
2021-04-04 10:44:33 +00:00
* This class searches all xml manifests for valid files declarations
*
* @since 2.3
*/
class JedcheckerRulesXMLFiles extends JEDcheckerRule
{
/**
* The formal ID of this rule. For example: SE1.
*
* @var string
*/
protected $id = 'XMLFILES';
/**
* The title or caption of this rule.
*
* @var string
*/
protected $title = 'COM_JEDCHECKER_XML_FILES';
/**
* The description of this rule.
*
* @var string
*/
protected $description = 'COM_JEDCHECKER_XML_FILES_DESC';
2021-05-17 20:25:11 +00:00
/**
* The ordering value to sort rules in the menu.
*
* @var integer
*/
public static $ordering = 300;
/**
* List of errors.
*
* @var string[]
*/
protected $errors;
2021-06-14 19:16:04 +00:00
/**
* List of warnings.
*
* @var string[]
*/
protected $warnings;
/**
* Manifest's directory
*
* @var string
*/
protected $basedir;
/**
* Initiates the search and check
*
* @return void
*/
public function check()
{
// Find all XML files of the extension
$files = JEDCheckerHelper::findManifests($this->basedir);
// 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:56:51 +00:00
$xml = simplexml_load_file($file);
// Failed to parse the xml file.
// Assume that this is not a extension manifest
if (!$xml)
{
return false;
}
$this->errors = array();
2021-06-14 19:16:04 +00:00
$this->warnings = array();
2021-04-04 10:44:33 +00:00
// Check declared files and folders do exist
$this->basedir = dirname($file) . '/';
$sitedir = '';
2021-03-09 21:41:52 +00:00
2021-04-04 10:44:33 +00:00
// Check: files[folder] (filename|folder)*
// ( for package: files[folder] (file|folder)* )
if (isset($xml->files))
{
$node = $xml->files;
2021-03-09 21:41:52 +00:00
// Get path to site files from "folder" attribute
2021-06-14 19:16:04 +00:00
$sitedir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->filename, $sitedir);
$this->checkFiles($node->file, $sitedir);
$this->checkFolders($node->folder, $sitedir);
}
2021-03-09 21:41:52 +00:00
// Check: media[folder] (filename|file|folder)*
if (isset($xml->media))
{
$node = $xml->media;
2021-06-14 19:16:04 +00:00
$dir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
2021-02-24 11:52:58 +00:00
$this->checkFiles($node->filename, $dir);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->file, $dir);
2021-02-24 11:52:58 +00:00
$this->checkFolders($node->folder, $dir);
}
2021-03-09 21:41:52 +00:00
// Check: fonts[folder] (filename|file|folder)*
2021-02-24 11:52:58 +00:00
if (isset($xml->fonts))
{
$node = $xml->fonts;
2021-06-14 19:16:04 +00:00
$dir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->filename, $dir);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->file, $dir);
$this->checkFolders($node->folder, $dir);
}
2021-04-04 10:44:33 +00:00
// Check files: languages[folder] language*
if (isset($xml->languages))
{
$node = $xml->languages;
2021-06-14 19:16:04 +00:00
$dir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->language, $dir);
}
$admindir = '';
2021-02-23 20:57:03 +00:00
2021-03-09 21:41:52 +00:00
// Check: administration files[folder] (filename|file|folder)*
if (isset($xml->administration->files))
{
$node = $xml->administration->files;
2021-03-09 21:41:52 +00:00
// Get path to admin files from "folder" attribute
2021-06-14 19:16:04 +00:00
$admindir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
2021-02-23 20:57:03 +00:00
$this->checkFiles($node->filename, $admindir);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->file, $admindir);
2021-02-23 20:57:03 +00:00
$this->checkFolders($node->folder, $admindir);
}
2021-03-09 21:41:52 +00:00
// Check: administration media[folder] (filename|file|folder)*
if (isset($xml->administration->media))
{
$node = $xml->administration->media;
2021-06-14 19:16:04 +00:00
$dir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->filename, $dir);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->file, $dir);
$this->checkFolders($node->folder, $dir);
}
2021-04-04 10:44:33 +00:00
// Check files: administration languages[folder] language*
if (isset($xml->administration->languages))
{
$node = $xml->administration->languages;
2021-06-14 19:16:04 +00:00
$dir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->language, $dir);
}
// For type="file" extensions:
// Check files: fileset files[folder] (filename|file|folder)*
if (isset($xml->fileset->files))
{
$node = $xml->fileset->files;
2021-06-14 19:16:04 +00:00
$dir = $this->getSourceFolder($node);
$this->checkFiles($node->filename, $dir);
$this->checkFiles($node->file, $dir);
$this->checkFolders($node->folder, $dir);
}
// Check: api files[folder] (filename|file|folder)*
if (isset($xml->api->files))
{
$node = $xml->api->files;
$dir = $this->getSourceFolder($node);
2021-03-09 21:41:52 +00:00
$this->checkFiles($node->filename, $dir);
$this->checkFiles($node->file, $dir);
$this->checkFolders($node->folder, $dir);
}
2021-04-04 10:44:33 +00:00
// Check file: scriptfile
if (isset($xml->scriptfile))
{
$this->checkFiles($xml->scriptfile);
}
2021-04-04 10:44:33 +00:00
// Check files: install sql file*
if (isset($xml->install->sql->file))
{
2021-02-23 20:57:03 +00:00
$this->checkFiles($xml->install->sql->file, $admindir);
}
2021-04-04 10:44:33 +00:00
// Check files: uninstall sql file*
if (isset($xml->uninstall->sql->file))
{
2021-02-23 20:57:03 +00:00
$this->checkFiles($xml->uninstall->sql->file, $admindir);
}
2021-04-04 10:44:33 +00:00
// Check folders: update schemas schemapath*
if (isset($xml->update->schemas->schemapath))
{
2021-02-23 20:57:03 +00:00
$this->checkFolders($xml->update->schemas->schemapath, $admindir);
}
// Check: config [add...path] directories exist
if (isset($xml->config))
{
$attributes = array('addfieldpath', 'addformpath', 'addrulepath');
$element = JEDCheckerHelper::getElementName($xml);
$extensionPath = false;
$type = (string) $xml['type'];
switch ($type)
{
case 'module':
$extensionPath = 'modules/' . $element . '/';
break;
case 'plugin':
$group = (string) $xml['group'];
$extensionPath = 'plugins/' . $group . '/' . $element . '/';
break;
case 'template':
$extensionPath = 'templates/' . $element . '/';
}
if ($extensionPath !== false)
{
foreach ($attributes as $attribute)
{
foreach ($xml->config->xpath('//*[@' . $attribute . ']') as $node)
{
$attrPath = (string) $node[$attribute];
$folder = ltrim($attrPath, '/');
// Convert absolute path to relative (if matches extension path)
if (strpos($folder, $extensionPath) === 0)
{
$folder = $this->basedir . $sitedir . substr($folder, strlen($extensionPath));
if (!is_dir($folder))
{
$this->errors[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FOLDER_NOT_FOUND', $attrPath);
}
}
}
}
}
}
2021-03-09 21:40:53 +00:00
// Check /namespace[path] directory exists (Joomla!4)
if (isset($xml->namespace['path']))
{
$folder = (string) $xml->namespace['path'];
if (!is_dir($this->basedir . $admindir . $folder) && !is_dir($this->basedir . $sitedir . $folder))
2021-03-09 21:40:53 +00:00
{
$this->errors[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FOLDER_NOT_FOUND', $folder);
}
}
if (count($this->errors))
{
$this->report->addError($file, implode('<br />', $this->errors));
}
2021-06-14 19:16:04 +00:00
if (count($this->warnings))
{
$this->report->addWarning($file, implode('<br />', $this->warnings));
}
// All checks passed. Return true
return true;
}
2021-06-14 19:16:04 +00:00
/**
* Get source folder for a node
*
* @param SimpleXMLElement $node The node to check for "folder" attribute
*
* @return string
*/
protected function getSourceFolder($node)
{
if (!isset($node['folder']))
{
return '';
}
$folder = (string) $node['folder'];
if (is_dir($this->basedir . $folder))
{
return $folder . '/';
}
$this->warnings[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FOLDER_NOT_FOUND', $folder);
return '';
}
/**
* Check files exist
*
2021-04-04 10:44:33 +00:00
* @param SimpleXMLElement $files Files to check
* @param string $dir Base directory
*
2021-04-04 10:44:33 +00:00
* @return void
*/
protected function checkFiles($files, $dir = '')
{
foreach ($files as $file)
{
$filename = $this->basedir . $dir . $file;
2021-04-04 10:44:33 +00:00
if (is_file($filename))
{
continue;
}
2021-04-04 10:44:33 +00:00
// Extra check for unzipped files
if (preg_match('/^(.*)\.(zip|tgz|tar\.gz)$/', $filename, $matches) && is_dir($matches[1]))
{
continue;
}
2021-04-04 10:44:33 +00:00
$this->errors[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FILE_NOT_FOUND', $dir . $file);
}
}
/**
* Check folders exist
*
2021-04-04 10:44:33 +00:00
* @param SimpleXMLElement $folders Directories to check
* @param string $dir Base directory
*
2021-04-04 10:44:33 +00:00
* @return void
*/
protected function checkFolders($folders, $dir = '')
{
foreach ($folders as $folder)
{
if (!is_dir($this->basedir . $dir . $folder))
{
$this->errors[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FOLDER_NOT_FOUND', $dir . $folder);
}
}
}
}