2021-02-02 22:14:16 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package Joomla.JEDChecker
|
|
|
|
*
|
2021-03-11 13:00:58 +00:00
|
|
|
* @copyright Copyright (C) 2021 Open Source Matters, Inc. All rights reserved.
|
2021-02-02 22:14:16 +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 JedcheckerRulesLanguage
|
|
|
|
*
|
|
|
|
* This class validates language ini file
|
|
|
|
*
|
2021-02-24 11:52:01 +00:00
|
|
|
* @since 3.0
|
2021-02-02 22:14:16 +00:00
|
|
|
*/
|
|
|
|
class JedcheckerRulesLanguage extends JEDcheckerRule
|
|
|
|
{
|
2021-02-22 21:00:06 +00:00
|
|
|
/**
|
|
|
|
* The formal ID of this rule. For example: SE1.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $id = 'LANG';
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
/**
|
|
|
|
* The title or caption of this rule.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $title = 'COM_JEDCHECKER_LANG';
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
/**
|
|
|
|
* The description of this rule.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $description = 'COM_JEDCHECKER_LANG_DESC';
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
/**
|
|
|
|
* Initiates the search and check
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function check()
|
|
|
|
{
|
2021-03-09 20:42:18 +00:00
|
|
|
// Find all INI files of the extension
|
|
|
|
$files = JFolder::files($this->basedir, '\.ini$', true, true);
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
// Iterate through all the ini files
|
|
|
|
foreach ($files as $file)
|
|
|
|
{
|
2021-03-09 20:42:18 +00:00
|
|
|
/* Language file format is either tag.extension.ini or tag.extension.sys.ini
|
|
|
|
(where "tag" is a language code, e.g. en-GB, and "extension" is the extension element name, e.g. com_content)
|
|
|
|
Joomla!4 allows to skip tag prefix inside of the tag directory
|
|
|
|
(i.e. to name files as extension.ini and extension.sys.ini) */
|
|
|
|
if (preg_match('#(?:^|/)([a-z]{2,3}-[A-Z]{2})[./]\w+(?:\.sys)?\.ini$#', $file, $match))
|
|
|
|
{
|
|
|
|
// Try to validate the file
|
|
|
|
$this->find($file, $match[1]);
|
|
|
|
}
|
2021-02-22 21:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
/**
|
|
|
|
* Reads and validates an ini file
|
|
|
|
*
|
|
|
|
* @param string $file - The path to the file
|
2021-03-09 20:42:18 +00:00
|
|
|
* @param string $tag - Language tag code
|
2021-02-22 21:00:06 +00:00
|
|
|
*
|
2021-04-04 10:40:43 +00:00
|
|
|
* @return boolean True on success, otherwise False.
|
2021-02-22 21:00:06 +00:00
|
|
|
*/
|
2021-03-09 20:42:18 +00:00
|
|
|
protected function find($file, $tag)
|
2021-02-22 21:00:06 +00:00
|
|
|
{
|
2021-03-09 20:43:21 +00:00
|
|
|
$content = file_get_contents($file);
|
|
|
|
|
|
|
|
if ($content === false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check EOL format is \n (not \r or \n\r)
|
|
|
|
if (strpos($content, "\r") !== false)
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_INCORRECT_EOL'));
|
|
|
|
}
|
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
$lines = file($file);
|
2021-02-23 20:41:30 +00:00
|
|
|
$nLines = count($lines);
|
2021-02-23 20:44:53 +00:00
|
|
|
$keys = array();
|
2021-02-23 20:41:30 +00:00
|
|
|
|
|
|
|
for ($lineno = 0; $lineno < $nLines; $lineno++)
|
2021-02-22 21:00:06 +00:00
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$startLineno = $lineno + 1;
|
|
|
|
$line = trim($lines[$lineno]);
|
|
|
|
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for BOM sequence
|
2021-02-23 19:26:09 +00:00
|
|
|
if ($lineno === 0 && strncmp($line, "\xEF\xBB\xBF", 3) === 0)
|
|
|
|
{
|
2021-02-24 11:52:01 +00:00
|
|
|
if (isset($line[3]) && strpos(";\n\r", $line[3]) === false)
|
|
|
|
{
|
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_BOM_FOUND'), $startLineno);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_BOM_FOUND'), $startLineno);
|
|
|
|
}
|
|
|
|
|
2021-03-10 22:35:15 +00:00
|
|
|
// Romeve BOM for further checks
|
2021-02-23 19:26:09 +00:00
|
|
|
$line = substr($line, 3);
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Skip empty lines, comments, and section names
|
2021-02-22 21:00:06 +00:00
|
|
|
if ($line === '' || $line[0] === ';' || $line[0] === '[')
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Report incorrect comment character
|
2021-02-22 21:00:06 +00:00
|
|
|
if ($line[0] === '#')
|
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_INCORRECT_COMMENT'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for "=" character in the line
|
2021-02-22 21:00:06 +00:00
|
|
|
if (strpos($line, '=') === false)
|
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_WRONG_LINE'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Extract key and value
|
2021-02-22 21:00:06 +00:00
|
|
|
list ($key, $value) = explode('=', $line, 2);
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-23 20:41:30 +00:00
|
|
|
// Validate key
|
2021-02-22 21:00:06 +00:00
|
|
|
$key = rtrim($key);
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for empty key
|
2021-02-22 21:00:06 +00:00
|
|
|
if ($key === '')
|
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_KEY_EMPTY'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for spaces in the key name
|
2021-02-22 21:00:06 +00:00
|
|
|
if (strpos($key, ' ') !== false)
|
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_KEY_WHITESPACE'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for invalid characters (see https://www.php.net/manual/en/function.parse-ini-file.php)
|
2021-02-22 21:00:06 +00:00
|
|
|
if (strpbrk($key, '{}|&~![()^"') !== false)
|
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_KEY_INVALID_CHARACTER'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for invalid key names (see https://www.php.net/manual/en/function.parse-ini-file.php)
|
2021-02-22 21:00:06 +00:00
|
|
|
if (in_array($key, array('null', 'yes', 'no', 'true', 'false', 'on', 'off', 'none'), true))
|
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_KEY_RESERVED'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
2021-02-23 20:49:12 +00:00
|
|
|
if (preg_match('/[\x00-\x1F\x80-\xFF]/', $key))
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_KEY_NOT_ASCII'), $startLineno, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($key !== strtoupper($key))
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_KEY_NOT_UPPERCASE'), $startLineno, $line);
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:44:53 +00:00
|
|
|
if (isset($keys[$key]))
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::sprintf('COM_JEDCHECKER_LANG_KEY_DUPLICATED', $keys[$key]), $startLineno, $line);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$keys[$key] = $startLineno;
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:41:30 +00:00
|
|
|
// Validate value
|
2021-02-22 21:00:06 +00:00
|
|
|
$value = ltrim($value);
|
2021-03-10 22:35:15 +00:00
|
|
|
|
2021-02-23 20:41:30 +00:00
|
|
|
// Parse multiline values
|
|
|
|
while (!preg_match('/^((?>\'(?>[^\'\\\\]+|\\\\.)*\'|"(?>[^"\\\\]+|\\\\.)*"|[^\'";]+)*)(;.*)?$/', $value, $matches))
|
|
|
|
{
|
|
|
|
if ($lineno + 1 >= $nLines)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$lineno++;
|
|
|
|
$chunk = "\n" . trim($lines[$lineno]);
|
|
|
|
$line .= $chunk;
|
|
|
|
$value .= $chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isset($matches[0]))
|
2021-02-22 21:00:06 +00:00
|
|
|
{
|
2021-02-23 20:41:30 +00:00
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_TRANSLATION_ERROR'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-03-10 22:35:15 +00:00
|
|
|
|
2021-02-23 20:41:30 +00:00
|
|
|
$value = trim($matches[1]);
|
2021-02-23 20:41:30 +00:00
|
|
|
|
2021-03-10 22:35:15 +00:00
|
|
|
// Check for empty value
|
2021-02-22 21:00:06 +00:00
|
|
|
if ($value === '""')
|
|
|
|
{
|
2021-02-23 20:47:14 +00:00
|
|
|
$this->report->addInfo($file, JText::_('COM_JEDCHECKER_LANG_TRANSLATION_EMPTY'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
continue;
|
|
|
|
}
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-23 20:41:30 +00:00
|
|
|
if (strlen($value) < 2 || $value[0] !== '"' || substr($value, -1) !== '"')
|
2021-02-23 20:41:30 +00:00
|
|
|
{
|
|
|
|
$this->report->addError($file, JText::_('COM_JEDCHECKER_LANG_TRANSLATION_QUOTES'), $startLineno, $line);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// // Remove quotes around
|
2021-02-22 21:00:06 +00:00
|
|
|
$value = substr($value, 1, -1);
|
2021-03-10 22:35:15 +00:00
|
|
|
|
|
|
|
// Check for legacy "_QQ_" code (deprecated since Joomla! 3.9 if favor of escaped double quote \"; removed in Joomla! 4)
|
2021-02-22 21:00:06 +00:00
|
|
|
if (strpos($value, '"_QQ_"') !== false)
|
|
|
|
{
|
2021-02-23 20:47:14 +00:00
|
|
|
$this->report->addCompat($file, JText::_('COM_JEDCHECKER_LANG_QQ_DEPRECATED'), $startLineno, $line);
|
2021-02-22 21:00:06 +00:00
|
|
|
}
|
2021-02-23 20:41:30 +00:00
|
|
|
|
2021-02-23 20:53:52 +00:00
|
|
|
$value = str_replace('"_QQ_"', '\"', $value);
|
|
|
|
|
|
|
|
if (preg_match('/[^\\\\]"/', $value))
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_UNESCAPED_QUOTE'), $startLineno, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strpos($value, '${') !== false)
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_VARIABLE_REF'), $startLineno, $line);
|
|
|
|
}
|
|
|
|
|
2021-03-01 22:22:39 +00:00
|
|
|
// The code below detects incorrect format of numbered placeholders (e.g. "%1s" instead of "%1$s")
|
2021-02-23 20:41:30 +00:00
|
|
|
|
2021-03-01 22:22:39 +00:00
|
|
|
// Count numbered placeholders in the string (e.g. "%1s")
|
|
|
|
$count = preg_match_all('/(?<=^|[^%])%(\d+)\w/', $value, $matches);
|
2021-02-23 20:41:30 +00:00
|
|
|
|
2021-03-01 22:22:39 +00:00
|
|
|
if ($count)
|
2021-02-24 11:52:01 +00:00
|
|
|
{
|
2021-03-01 22:22:39 +00:00
|
|
|
// To avoid false-positives (e.g. %10s for a ten-characters-wide output string in a CLI),
|
|
|
|
// we check that placeholder numbers form a sequence from 1 to N.
|
|
|
|
|
|
|
|
$maxNumber = 0;
|
|
|
|
|
|
|
|
foreach ($matches as $match)
|
|
|
|
{
|
|
|
|
$maxNumber = max($maxNumber, (int) $match[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If placeholder numbers form a sequence, the maximal value is equal to the number of elements
|
|
|
|
if ($maxNumber === $count)
|
|
|
|
{
|
|
|
|
$this->report->addWarning($file, JText::_('COM_JEDCHECKER_LANG_INCORRECT_ARGNUM'), $startLineno, $line);
|
|
|
|
}
|
2021-02-23 20:41:30 +00:00
|
|
|
}
|
2021-02-22 21:00:06 +00:00
|
|
|
}
|
2021-02-02 22:14:16 +00:00
|
|
|
|
2021-02-22 21:00:06 +00:00
|
|
|
// All checks passed. Return true
|
|
|
|
return true;
|
|
|
|
}
|
2021-02-02 22:14:16 +00:00
|
|
|
}
|