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

226 lines
4.9 KiB
PHP
Raw Normal View History

2017-01-06 14:46:54 +00:00
<?php
/**
* @author eaxs <support@projectfork.net>
* @author Daniel Dimitrov <daniel@compojoom.com>
* @date 07/06/2012
* @copyright Copyright (C) 2008 - 2012 compojoom.com . All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
defined('_JEXEC') or die('Restricted access');
// Include the rule base class
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';
/**
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';
/**
* Initiates the search and check
*
* @return void
*/
public function check()
{
// Find all XML files of the extension
$files = JFolder::files($this->basedir, '.xml$', true, true);
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
}
return true;
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)
{
$packageCount = 0;
foreach ($files as $file)
2017-01-06 14:46:54 +00:00
{
2017-01-13 09:39:46 +00:00
$xml = JFactory::getXml($file);
// Check if this is an XML and an extension manifest
if ($xml && ($xml->getName() == 'install' || $xml->getName() == 'extension'))
{
// Check if extension attribute 'type' is for a package
2017-01-13 09:41:10 +00:00
if($xml->attributes()->type == 'package')
{
2017-01-13 09:39:46 +00:00
$packageCount++;
$this->find($file);
}
}
}
// 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
}
}
/**
* Reads a file and searches for paths of xml files
*
* @param string $files - The path to the file
*
* @return void
*/
protected function findXMLPaths($files)
{
$XMLFiles = array();
$componentPaths = array();
foreach ($files as $file)
{
$xml = JFactory::getXml($file);
// Check if this is an XML and an extension manifest
if ($xml && ($xml->getName() == 'install' || $xml->getName() == 'extension'))
{
$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))
);
2017-01-13 13:39:24 +00:00
if ($xml->attributes()->type == 'component')
{
$componentPaths[] = substr($file, 0, strrpos( $file, '/'));
}
}
}
foreach ($XMLFiles as $XMLFile)
{
// Always check component XML files for update servers
if ($XMLFile['type'] == 'component')
{
$this->find($XMLFile['filepath']);
} else {
// If not component, check if XML is nested inside component folder.
$nested = false;
foreach ($componentPaths as $component)
{
if (strpos($XMLFile['directoryPath'], $component) !== false)
{
$nested = true;
}
}
if (!$nested){
$this->find($XMLFile['filepath']);
}
}
}
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)
{
$xml = JFactory::getXml($file);
2017-01-13 09:39:46 +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 this is an extension manifest
// 1.5 uses 'install', 1.6 uses 'extension'
if ($xml->getName() != 'install' && $xml->getName() != 'extension')
{
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;
}
// 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;
}
// 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;
} else {
$this->report->addInfo($file, JText::sprintf('COM_JEDCHECKER_INFO_XML_UPDATE_SERVER_LINK', (string) $server));
}
}
// All checks passed. Return true
return true;
}
}