33
2
mirror of https://github.com/joomla-extensions/jedchecker.git synced 2024-11-28 07:46:34 +00:00
jedchecker/administrator/components/com_jedchecker/libraries/rules/xmlinfo.php

233 lines
6.0 KiB
PHP
Raw Normal View History

<?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
*/
defined('_JEXEC') or die('Restricted access');
// Include the rule base class
2013-11-05 20:17:39 +00:00
require_once JPATH_COMPONENT_ADMINISTRATOR . '/models/rule.php';
/**
2013-11-05 20:17:39 +00:00
* class JedcheckerRulesXMLinfo
*
2013-11-05 20:17:39 +00:00
* This class searches all xml manifestes for specific tags
*
* @since 1.0
*/
2013-11-05 20:17:39 +00:00
class JedcheckerRulesXMLinfo extends JEDcheckerRule
{
2013-11-05 20:17:39 +00:00
/**
* The formal ID of this rule. For example: SE1.
*
* @var string
*/
protected $id = 'INFO_XML';
/**
* The title or caption of this rule.
*
* @var string
*/
protected $title = 'COM_JEDCHECKER_INFO_XML';
/**
* The description of this rule.
*
* @var string
*/
protected $description = 'COM_JEDCHECKER_INFO_XML_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);
$manifestFound = false;
2013-11-05 20:17:39 +00:00
// Iterate through all the xml files
foreach ($files as $file)
{
// Try to find the license
if ($this->find($file))
{
$manifestFound = true;
}
}
if (!$manifestFound)
{
$this->report->addError('', JText::_('COM_JEDCHECKER_INFO_XML_NO_MANIFEST'));
2013-11-05 20:17:39 +00:00
}
}
/**
* Reads a file and searches for the license
*
* @param string $file - The path to the file
*
* @return boolean True if the manifest file was found, otherwise False.
2013-11-05 20:17:39 +00:00
*/
protected function find($file)
{
2016-05-25 16:19:19 +00:00
$xml = JFactory::getXml($file);
2013-11-05 20:17:39 +00:00
// Failed to parse the xml file.
// Assume that this is not a extension manifest
if (!$xml)
{
return false;
}
2019-03-09 19:44:14 +00:00
// Check if this is an extension manifest
// 1.5 uses 'install', 1.6+ uses 'extension'
if ($xml->getName() !== 'extension')
{
return false;
}
2019-03-09 19:44:14 +00:00
// Get extension name (element)
$type = (string) $xml['type'];
if (isset($xml->element))
{
$extension = (string) $xml->element;
}
else
{
$extension = (string) $xml->name;
foreach ($xml->files->children as $child)
{
if (isset($child[$type]))
{
$extension = (string) $child[$type];
}
}
}
$extension = strtolower(JFilterInput::getInstance()->clean($extension, 'cmd'));
if ($type === 'component' && strpos($extension, 'com_') !== 0)
{
$extension = 'com_' . $extension;
2019-03-09 19:44:14 +00:00
}
// Load the language of the extension (if any)
$lang = JFactory::getLanguage();
// search for .sys.ini translation file
$lang_dir = dirname($file);
$lang_tag = 'en-GB'; // $lang->getDefault();
$lookup_lang_dirs = array();
if (isset($xml->administration->files['folder']))
{
$lookup_lang_dirs[] = trim($xml->administration->files['folder'], '/') . '/language/' . $lang_tag;
}
if (isset($xml->files['folder']))
2013-11-05 20:17:39 +00:00
{
$lookup_lang_dirs[] = trim($xml->files['folder'], '/') . '/language/' . $lang_tag;
2013-11-05 20:17:39 +00:00
}
$lookup_lang_dirs[] = 'language/' . $lang_tag;
if (isset($xml->administration->languages['folder']))
{
$lookup_lang_dirs[] = trim($xml->administration->languages['folder'], '/');
}
if (isset($xml->languages['folder']))
{
$lookup_lang_dirs[] = trim($xml->languages['folder'], '/');
}
$lookup_lang_dirs[] = '';
foreach ($lookup_lang_dirs as $dir)
{
$lang_sys_file =
$lang_dir . '/' .
($dir === '' ? '' : trim($dir, '/') . '/') .
$lang_tag. '.' . $extension . '.sys.ini';
if (is_file($lang_sys_file))
{
$loadLanguage = new ReflectionMethod($lang, 'loadLanguage');
$loadLanguage->setAccessible(true);
$loadLanguage->invoke($lang, $lang_sys_file, $extension);
break;
}
}
2019-03-09 19:44:14 +00:00
// Get the real extension's name now that the language has been loaded
$extension_name = $lang->_((string) $xml->name);
2019-03-09 19:44:14 +00:00
$info[] = JText::sprintf('COM_JEDCHECKER_INFO_XML_NAME_XML', $extension_name);
2013-11-05 20:17:39 +00:00
$info[] = JText::sprintf('COM_JEDCHECKER_INFO_XML_VERSION_XML', (string) $xml->version);
$info[] = JText::sprintf('COM_JEDCHECKER_INFO_XML_CREATIONDATE_XML', (string) $xml->creationDate);
2013-11-05 20:17:39 +00:00
$this->report->addInfo($file, implode('<br />', $info));
// NM3 - Listing name contains “module” or “plugin”
if (preg_match('/\b(?:module|plugin)\b/i', $extension_name))
{
$this->report->addError($file, JText::_('COM_JEDCHECKER_INFO_XML_NAME_MODULE_PLUGIN'));
}
if (stripos($extension_name, 'template') !== false)
{
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_INFO_XML_NAME_RESERVED_KEYWORDS'));
}
// NM5 - Version in name/title
if (preg_match('/(?:\bversion\b|\d\.\d)/i', $extension_name))
{
$this->report->addError($file, JText::_('COM_JEDCHECKER_INFO_XML_NAME_VERSION'));
}
if (stripos($extension_name, 'joomla') === 0)
{
$this->report->addError($file, JText::_('COM_JEDCHECKER_INFO_XML_NAME_JOOMLA'));
}
elseif (stripos($extension_name, 'joom') !== false)
{
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_INFO_XML_NAME_JOOMLA_DERIVATIVE'));
}
$url = (string)$xml->authorUrl;
if (stripos($url, 'joom') !== false)
{
$domain = (strpos($url, '//') === false) ? $url : parse_url(trim($url), PHP_URL_HOST);
if (stripos($domain, 'joom') !== false) {
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_INFO_XML_URL_JOOMLA_DERIVATIVE'));
}
}
if ($type === 'component' && isset($xml->administration->menu))
{
$menu_name = $lang->_((string) $xml->administration->menu);
if ($extension_name !== $menu_name)
{
$this->report->addWarning($file, JText::sprintf('COM_JEDCHECKER_INFO_XML_NAME_ADMIN_MENU', $menu_name, $extension_name));
}
}
if ($type === 'plugin')
{
$group = (string) $xml['group'];
if (strpos($extension_name, ucfirst($group) . ' - ') !== 0)
{
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_INFO_XML_NAME_PLUGIN_FORMAT'));
}
}
2013-11-05 20:17:39 +00:00
// All checks passed. Return true
return true;
}
}