2021-02-02 16:09:20 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.JEDChecker
|
|
|
|
*
|
2021-03-11 12:59:54 +00:00
|
|
|
* @copyright Copyright (C) 2021 Open Source Matters, Inc. All rights reserved.
|
2021-02-02 16:09:20 +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 JedcheckerRulesXMLFiles
|
|
|
|
*
|
2021-04-04 10:44:33 +00:00
|
|
|
* This class searches all xml manifests for valid files declarations
|
2021-02-02 16:09:20 +00:00
|
|
|
*
|
|
|
|
* @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';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of errors.
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
|
|
|
protected $errors;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initiates the search and check
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
|
|
|
// Find all XML files of the extension
|
2021-02-13 20:56:59 +00:00
|
|
|
$files = JFolder::files($this->basedir, '\.xml$', true, true);
|
2021-02-02 16:09:20 +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:56:51 +00:00
|
|
|
$xml = simplexml_load_file($file);
|
2021-02-02 16:09:20 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->errors = array();
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check declared files and folders do exist
|
2021-02-02 16:09:20 +00:00
|
|
|
|
|
|
|
$basedir = dirname($file) . '/';
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check: files[folder] (filename|folder)*
|
|
|
|
// ( for package: files[folder] (file|folder)* )
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->files))
|
|
|
|
{
|
|
|
|
$node = $xml->files;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
|
|
|
$this->checkFiles($node->filename, $dir);
|
2021-04-04 10:44:33 +00:00
|
|
|
$this->checkFiles($node->file, $dir);
|
2021-02-02 16:09:20 +00:00
|
|
|
$this->checkFolders($node->folder, $dir);
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check: media[folder] (filename|folder)*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->media))
|
|
|
|
{
|
|
|
|
$node = $xml->media;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
2021-02-24 11:52:58 +00:00
|
|
|
$this->checkFiles($node->filename, $dir);
|
|
|
|
$this->checkFolders($node->folder, $dir);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check: fonts[folder] (filename|folder)*
|
|
|
|
if (isset($xml->fonts))
|
|
|
|
{
|
|
|
|
$node = $xml->fonts;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
2021-02-02 16:09:20 +00:00
|
|
|
$this->checkFiles($node->filename, $dir);
|
|
|
|
$this->checkFolders($node->folder, $dir);
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check files: languages[folder] language*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->languages))
|
|
|
|
{
|
|
|
|
$node = $xml->languages;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
|
|
|
$this->checkFiles($node->language, $dir);
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:57:03 +00:00
|
|
|
$admindir = $basedir;
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check: administration files[folder] (filename|folder)*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->administration->files))
|
|
|
|
{
|
|
|
|
$node = $xml->administration->files;
|
2021-02-23 20:57:03 +00:00
|
|
|
$admindir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
|
|
|
$this->checkFiles($node->filename, $admindir);
|
|
|
|
$this->checkFolders($node->folder, $admindir);
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check: administration media[folder] (filename|folder)*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->administration->media))
|
|
|
|
{
|
|
|
|
$node = $xml->administration->media;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
|
|
|
$this->checkFiles($node->filename, $dir);
|
|
|
|
$this->checkFolders($node->folder, $dir);
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check files: administration languages[folder] language*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->administration->languages))
|
|
|
|
{
|
|
|
|
$node = $xml->administration->languages;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
|
|
|
$this->checkFiles($node->language, $dir);
|
|
|
|
}
|
|
|
|
|
2021-03-09 20:54:58 +00:00
|
|
|
// For type="file" extensions:
|
|
|
|
// Check files: fileset files[folder] (filename|file|folder)*
|
|
|
|
if (isset($xml->fileset->files))
|
|
|
|
{
|
|
|
|
$node = $xml->fileset->files;
|
|
|
|
$dir = $basedir . (isset($node['folder']) ? $node['folder'] . '/' : '');
|
|
|
|
$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
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->scriptfile))
|
|
|
|
{
|
|
|
|
$this->checkFiles($xml->scriptfile, $basedir);
|
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check files: install sql file*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->install->sql->file))
|
|
|
|
{
|
2021-02-23 20:57:03 +00:00
|
|
|
$this->checkFiles($xml->install->sql->file, $admindir);
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check files: uninstall sql file*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->uninstall->sql->file))
|
|
|
|
{
|
2021-02-23 20:57:03 +00:00
|
|
|
$this->checkFiles($xml->uninstall->sql->file, $admindir);
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
2021-04-04 10:44:33 +00:00
|
|
|
// Check folders: update schemas schemapath*
|
2021-02-02 16:09:20 +00:00
|
|
|
if (isset($xml->update->schemas->schemapath))
|
|
|
|
{
|
2021-02-23 20:57:03 +00:00
|
|
|
$this->checkFolders($xml->update->schemas->schemapath, $admindir);
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (count($this->errors))
|
|
|
|
{
|
|
|
|
$this->report->addError($file, implode('<br />', $this->errors));
|
|
|
|
}
|
|
|
|
|
|
|
|
// All checks passed. Return true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check files exist
|
|
|
|
*
|
2021-04-04 10:44:33 +00:00
|
|
|
* @param SimpleXMLElement $files Files to check
|
|
|
|
* @param string $dir Base directory
|
2021-02-02 16:09:20 +00:00
|
|
|
*
|
2021-04-04 10:44:33 +00:00
|
|
|
* @return void
|
2021-02-02 16:09:20 +00:00
|
|
|
*/
|
|
|
|
protected function checkFiles($files, $dir)
|
|
|
|
{
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
2021-02-18 12:20:00 +00:00
|
|
|
$filename = $dir . $file;
|
2021-04-04 10:44:33 +00:00
|
|
|
|
2021-02-18 12:20:00 +00:00
|
|
|
if (is_file($filename))
|
2021-02-02 16:09:20 +00:00
|
|
|
{
|
2021-02-18 12:20:00 +00:00
|
|
|
continue;
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
2021-04-04 10:44:33 +00:00
|
|
|
|
|
|
|
// Extra check for unzipped files
|
2021-03-09 21:39:18 +00:00
|
|
|
if (preg_match('/^(.*)\.(zip|tgz|tar\.gz)$/', $filename, $matches) && is_dir($matches[1]))
|
2021-02-18 12:20:00 +00:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-04-04 10:44:33 +00:00
|
|
|
|
|
|
|
$this->errors[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FILE_NOT_FOUND', (string) $file);
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check folders exist
|
|
|
|
*
|
2021-04-04 10:44:33 +00:00
|
|
|
* @param SimpleXMLElement $folders Directories to check
|
|
|
|
* @param string $dir Base directory
|
2021-02-02 16:09:20 +00:00
|
|
|
*
|
2021-04-04 10:44:33 +00:00
|
|
|
* @return void
|
2021-02-02 16:09:20 +00:00
|
|
|
*/
|
|
|
|
protected function checkFolders($folders, $dir)
|
|
|
|
{
|
|
|
|
foreach ($folders as $folder)
|
|
|
|
{
|
|
|
|
if (!is_dir($dir . $folder))
|
|
|
|
{
|
2021-04-04 10:44:33 +00:00
|
|
|
$this->errors[] = JText::sprintf('COM_JEDCHECKER_XML_FILES_FOLDER_NOT_FOUND', (string) $folder);
|
2021-02-02 16:09:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|