33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-12-24 11:25:26 +00:00
jedchecker/administrator/components/com_jedchecker/libraries/rules/xmlupdateserver.php

227 lines
4.7 KiB
PHP
Raw Normal View History

2017-01-06 14:46:54 +00:00
<?php
/**
2019-03-09 19:44:14 +00:00
* @package Joomla.JEDChecker
*
2019-03-10 16:09:42 +00:00
* @copyright Copyright (C) 2017 - 2019 Open Source Matters, Inc. All rights reserved.
* Copyright (C) 2008 - 2016 compojoom.com . All rights reserved.
2019-03-10 08:49:52 +00:00
* @author Daniel Dimitrov <daniel@compojoom.com>
* eaxs <support@projectfork.net>
*
2019-03-09 19:44:14 +00:00
* @license GNU General Public License version 2 or later; see LICENSE.txt
2017-01-06 14:46:54 +00:00
*/
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';
2017-01-06 14:46:54 +00:00
/**
2017-01-06 14:48:48 +00:00
* class JedcheckerRulesXMLUpdateServer
2017-01-06 14:46:54 +00:00
*
2017-01-06 14:48:48 +00:00
* This class searches all xml files for valid Update Servers
2017-01-06 14:46:54 +00:00
*
* @since 1.0
*/
class JedcheckerRulesXMLUpdateServer extends JEDcheckerRule
{
/**
* The formal ID of this rule. For example: SE1.
*
* @var string
*/
protected $id = 'US1';
/**
* The title or caption of this rule.
*
* @var string
*/
protected $title = 'COM_JEDCHECKER_RULE_US1';
/**
* The description of this rule.
*
* @var string
*/
protected $description = 'COM_JEDCHECKER_RULE_US1_DESC';
/**
* The ordering value to sort rules in the menu.
*
* @var integer
*/
public static $ordering = 400;
2017-01-06 14:46:54 +00:00
/**
* Initiates the search and check
*
* @return void
*/
public function check()
{
// Find all XML files of the extension
$files = JEDCheckerHelper::findManifests($this->basedir);
2019-03-09 19:44:14 +00:00
2017-01-13 09:39:46 +00:00
// Find XML package file
$packageFile = $this->checkPackageXML($files);
2017-01-06 14:46:54 +00:00
2017-01-13 13:39:24 +00:00
if (!$packageFile)
{
$XMLFiles = $this->findXMLPaths($files);
2017-01-13 09:39:46 +00:00
}
2019-03-09 19:44:14 +00:00
return true;
2017-01-13 09:39:46 +00:00
}
2019-03-09 19:44:14 +00:00
2017-01-13 09:39:46 +00:00
/**
* Reads a file and searches for package xml file
*
* @param string $files - The path to the file
2017-01-13 09:39:46 +00:00
*
* @return boolean True if the package xml file was found, otherwise False.
*/
protected function checkPackageXML($files)
2019-03-09 19:44:14 +00:00
{
2017-01-13 09:39:46 +00:00
$packageCount = 0;
2019-03-09 19:44:14 +00:00
foreach ($files as $file)
2017-01-06 14:46:54 +00:00
{
2021-02-13 20:01:07 +00:00
$xml = simplexml_load_file($file);
2017-01-13 09:39:46 +00:00
// Check if extension attribute 'type' is for a package
if ($xml && (string) $xml['type'] === 'package')
2019-03-09 19:44:14 +00:00
{
$packageCount++;
$this->find($file);
2019-03-09 19:44:14 +00:00
}
2017-01-13 09:39:46 +00:00
}
2019-03-09 19:44:14 +00:00
2017-01-13 09:39:46 +00:00
// No XML file found for package
2017-01-13 09:41:10 +00:00
if ($packageCount == 0)
{
2017-01-13 09:39:46 +00:00
return false;
} else {
return true;
2017-01-06 14:46:54 +00:00
}
}
2019-03-09 19:44:14 +00:00
/**
* Reads a file and searches for paths of xml files
*
* @param string $files - The path to the file
*
* @return void
*/
protected function findXMLPaths($files)
2019-03-09 19:44:14 +00:00
{
$XMLFiles = array();
$componentPaths = array();
2019-03-09 19:44:14 +00:00
foreach ($files as $file)
{
2021-02-13 20:01:07 +00:00
$xml = simplexml_load_file($file);
if ($xml)
{
$directories = explode('/', substr($file, 0, strrpos( $file, '/')));
$XMLFiles[] = array(
'type' => (string) $xml->attributes()->type,
'filepath' => $file,
'directoryPath' => substr($file, 0, strrpos( $file, '/')),
'directory' => trim(end($directories))
);
2019-03-09 19:44:14 +00:00
2017-01-13 13:39:24 +00:00
if ($xml->attributes()->type == 'component')
{
$componentPaths[] = substr($file, 0, strrpos( $file, '/'));
}
2019-03-09 19:44:14 +00:00
}
}
2019-03-09 19:44:14 +00:00
foreach ($XMLFiles as $XMLFile)
{
// Always check component XML files for update servers
if ($XMLFile['type'] == 'component')
{
$this->find($XMLFile['filepath']);
2019-03-09 19:44:14 +00:00
} else {
// If not component, check if XML is nested inside component folder.
$nested = false;
2019-03-09 19:44:14 +00:00
foreach ($componentPaths as $component)
{
if (strpos($XMLFile['directoryPath'], $component) !== false)
{
$nested = true;
}
}
2019-03-09 19:44:14 +00:00
if (!$nested){
$this->find($XMLFile['filepath']);
}
}
}
2019-03-09 19:44:14 +00:00
return true;
}
2017-01-06 14:46:54 +00:00
/**
2017-01-13 09:39:46 +00:00
* Reads a file and searches for the update server
2017-01-06 14:46:54 +00:00
*
* @param string $file - The path to the file
*
2017-01-13 09:39:46 +00:00
* @return boolean True if the update server was found, otherwise False.
2017-01-06 14:46:54 +00:00
*/
protected function find($file)
{
2021-02-13 20:01:07 +00:00
$xml = simplexml_load_file($file);
2019-03-09 19:44:14 +00:00
2017-01-06 14:46:54 +00:00
// Failed to parse the xml file.
// Assume that this is not a extension manifest
if (!$xml)
{
return true;
}
// Check if there is an updateservers tag
if (!isset($xml->updateservers))
{
$this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_XML_UPDATE_SERVER_NOT_FOUND'));
return false;
}
2019-03-09 19:44:14 +00:00
2017-01-06 14:46:54 +00:00
// Check if server tag(s) exist
if (!isset($xml->updateservers->server))
{
$this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_XML_UPDATE_SERVER_NOT_FOUND'));
return false;
2019-03-09 19:44:14 +00:00
}
2017-01-06 14:46:54 +00:00
// Check if server tag(s) contain valid links
foreach ($xml->updateservers->server as $server)
{
if (stripos($server, 'http') === false)
{
$this->report->addError($file, JText::_('COM_JEDCHECKER_ERROR_XML_UPDATE_SERVER_LINK_NOT_FOUND'));
return false;
2019-03-09 19:44:14 +00:00
2017-01-06 14:46:54 +00:00
} else {
$this->report->addInfo($file, JText::sprintf('COM_JEDCHECKER_INFO_XML_UPDATE_SERVER_LINK', (string) $server));
}
}
// All checks passed. Return true
return true;
}
}