Skip to content

Commit

Permalink
[Deprecate _QQ_ 3] Mark _QQ_ as deprecated and normalize language ini…
Browse files Browse the repository at this point in the history
… parsing (#20321)

* deprecate _QQ_ usage and normalize parse ini method

* some more improvements

* some more

* munor code improvements

* minor changes

* remove forgotten parameters

* remove to do as michael explained it

* $fileName instead of $filename

* slashes

* remove blank line

* c/s
  • Loading branch information
brianteeman authored and Michael Babker committed May 12, 2018
1 parent 51dc6e0 commit be42200
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 111 deletions.
21 changes: 4 additions & 17 deletions administrator/components/com_languages/helpers/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,29 +75,16 @@ public static function getActions()
/**
* Method for parsing ini files.
*
* @param string $filename Path and name of the ini file to parse.
* @param string $fileName Path and name of the ini file to parse.
*
* @return array Array of strings found in the file, the array indices will be the keys. On failure an empty array will be returned.
*
* @since 2.5
* @deprecated __DEPLOY_VERSION__ Use JLanguageHelper::parseIniFile() instead.
*/
public static function parseFile($filename)
public static function parseFile($fileName)
{
if (!is_file($filename))
{
return array();
}

$contents = file_get_contents($filename);
$contents = str_replace('_QQ_', '"\""', $contents);
$strings = @parse_ini_string($contents);

if ($strings === false)
{
return array();
}

return $strings;
return JLanguageHelper::parseIniFile($fileName);
}

/**
Expand Down
21 changes: 9 additions & 12 deletions administrator/components/com_languages/models/override.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,11 @@ protected function loadFormData()
*/
public function getItem($pk = null)
{
JLoader::register('LanguagesHelper', JPATH_ADMINISTRATOR . '/components/com_languages/helpers/languages.php');

$input = JFactory::getApplication()->input;
$pk = (!empty($pk)) ? $pk : $input->get('id');
$filename = constant('JPATH_' . strtoupper($this->getState('filter.client')))
$pk = !empty($pk) ? $pk : $input->get('id');
$fileName = constant('JPATH_' . strtoupper($this->getState('filter.client')))
. '/language/overrides/' . $this->getState('filter.language', 'en-GB') . '.override.ini';
$strings = LanguagesHelper::parseFile($filename);
$strings = JLanguageHelper::parseIniFile($fileName);

$result = new stdClass;
$result->key = '';
Expand All @@ -105,10 +103,10 @@ public function getItem($pk = null)
$result->override = $strings[$pk];
}

$opposite_filename = constant('JPATH_' . strtoupper($this->getState('filter.client') == 'site' ? 'administrator' : 'site'))
$oppositeFileName = constant('JPATH_' . strtoupper($this->getState('filter.client') == 'site' ? 'administrator' : 'site'))
. '/language/overrides/' . $this->getState('filter.language', 'en-GB') . '.override.ini';
$opposite_strings = LanguagesHelper::parseFile($opposite_filename);
$result->both = isset($opposite_strings[$pk]) && ($opposite_strings[$pk] == $strings[$pk]);
$oppositeStrings = JLanguageHelper::parseIniFile($oppositeFileName);
$result->both = isset($oppositeStrings[$pk]) && ($oppositeStrings[$pk] == $strings[$pk]);

return $result;
}
Expand All @@ -125,7 +123,6 @@ public function getItem($pk = null)
*/
public function save($data, $opposite_client = false)
{
JLoader::register('LanguagesHelper', JPATH_ADMINISTRATOR . '/components/com_languages/helpers/languages.php');
jimport('joomla.filesystem.file');

$app = JFactory::getApplication();
Expand All @@ -152,8 +149,8 @@ public function save($data, $opposite_client = false)
$client = $client ? 'administrator' : 'site';

// Parse the override.ini file in oder to get the keys and strings.
$filename = constant('JPATH_' . strtoupper($client)) . '/language/overrides/' . $language . '.override.ini';
$strings = LanguagesHelper::parseFile($filename);
$fileName = constant('JPATH_' . strtoupper($client)) . '/language/overrides/' . $language . '.override.ini';
$strings = JLanguageHelper::parseIniFile($fileName);

if (isset($strings[$data['id']]))
{
Expand All @@ -178,7 +175,7 @@ public function save($data, $opposite_client = false)
}

// Write override.ini file with the strings.
if (JLanguageHelper::saveToIniFile($filename, $strings) === false)
if (JLanguageHelper::saveToIniFile($fileName, $strings) === false)
{
return false;
}
Expand Down
15 changes: 7 additions & 8 deletions administrator/components/com_languages/models/overrides.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ public function getOverrides($all = false)
$client = in_array($this->state->get('filter.client'), array(0, 'site')) ? 'SITE' : 'ADMINISTRATOR';

// Parse the override.ini file in order to get the keys and strings.
$filename = constant('JPATH_' . $client) . '/language/overrides/' . $this->getState('filter.language') . '.override.ini';
$strings = LanguagesHelper::parseFile($filename);
$fileName = constant('JPATH_' . $client) . '/language/overrides/' . $this->getState('filter.language') . '.override.ini';
$strings = JLanguageHelper::parseIniFile($fileName);

// Delete the override.ini file if empty.
if (file_exists($filename) && empty($strings))
if (file_exists($fileName) && $strings === array())
{
JFile::delete($filename);
JFile::delete($fileName);
}

// Filter the loaded strings according to the search box.
Expand Down Expand Up @@ -245,14 +245,13 @@ public function delete($cids)
}

jimport('joomla.filesystem.file');
JLoader::register('LanguagesHelper', JPATH_ADMINISTRATOR . '/components/com_languages/helpers/languages.php');

$filterclient = JFactory::getApplication()->getUserState('com_languages.overrides.filter.client');
$client = $filterclient == 0 ? 'SITE' : 'ADMINISTRATOR';

// Parse the override.ini file in oder to get the keys and strings.
$filename = constant('JPATH_' . $client) . '/language/overrides/' . $this->getState('filter.language') . '.override.ini';
$strings = LanguagesHelper::parseFile($filename);
$fileName = constant('JPATH_' . $client) . '/language/overrides/' . $this->getState('filter.language') . '.override.ini';
$strings = JLanguageHelper::parseIniFile($fileName);

// Unset strings that shall be deleted
foreach ($cids as $key)
Expand All @@ -264,7 +263,7 @@ public function delete($cids)
}

// Write override.ini file with the strings.
if (JLanguageHelper::saveToIniFile($filename, $strings) === false)
if (JLanguageHelper::saveToIniFile($fileName, $strings) === false)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
$listOrder = $this->escape($this->state->get('list.ordering'));
$listDirn = $this->escape($this->state->get('list.direction'));

$opposite_client = $this->state->get('filter.client') == '1' ? JText::_('JSITE') : JText::_('JADMINISTRATOR');
$opposite_filename = constant('JPATH_' . strtoupper(1 - $this->state->get('filter.client')? 'administrator' : 'site'))
$oppositeClient = $this->state->get('filter.client') == '1' ? JText::_('JSITE') : JText::_('JADMINISTRATOR');
$oppositeFileName = constant('JPATH_' . strtoupper(1 - $this->state->get('filter.client')? 'administrator' : 'site'))
. '/language/overrides/' . $this->state->get('filter.language', 'en-GB') . '.override.ini';
$opposite_strings = LanguagesHelper::parseFile($opposite_filename);
$oppositeStrings = JLanguageHelper::parseIniFile($oppositeFileName);
?>

<form action="<?php echo JRoute::_('index.php?option=com_languages&view=overrides'); ?>" method="post" name="adminForm" id="adminForm">
Expand Down Expand Up @@ -101,9 +101,9 @@
</td>
<td class="hidden-phone">
<?php echo $client; ?><?php
if (isset($opposite_strings[$key]) && ($opposite_strings[$key] == $text))
if (isset($oppositeStrings[$key]) && $oppositeStrings[$key] === $text)
{
echo '/' . $opposite_client;
echo '/' . $oppositeClient;
}
?>
</td>
Expand Down
79 changes: 13 additions & 66 deletions libraries/src/Language/Language.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@

use Joomla\String\StringHelper;

/**
* Allows for quoting in language .ini files.
*/
define('_QQ_', '"');

/**
* Languages/translation handler class
*
Expand Down Expand Up @@ -197,19 +192,7 @@ public function __construct($lang = null, $debug = false)
$this->metadata = LanguageHelper::getMetadata($this->lang);
$this->setDebug($debug);

$filename = JPATH_BASE . "/language/overrides/$lang.override.ini";

if (file_exists($filename) && $contents = $this->parse($filename))
{
if (is_array($contents))
{
// Sort the underlying heap by key values to optimize merging
ksort($contents, SORT_STRING);
$this->override = $contents;
}

unset($contents);
}
$this->override = $this->parse(JPATH_BASE . '/language/overrides/' . $lang . '.override.ini');

// Look for a language specific localise class
$class = str_replace('-', '_', $lang . 'Localise');
Expand Down Expand Up @@ -771,27 +754,22 @@ public function load($extension = 'joomla', $basePath = JPATH_BASE, $lang = null
*
* This method will not note the successful loading of a file - use load() instead.
*
* @param string $filename The name of the file.
* @param string $fileName The name of the file.
* @param string $extension The name of the extension.
*
* @return boolean True if new strings have been added to the language
*
* @see Language::load()
* @since 11.1
*/
protected function loadLanguage($filename, $extension = 'unknown')
protected function loadLanguage($fileName, $extension = 'unknown')
{
$this->counter++;

$result = false;
$strings = false;

if (file_exists($filename))
{
$strings = $this->parse($filename);
}
$result = false;
$strings = $this->parse($fileName);

if (is_array($strings) && count($strings))
if ($strings !== array())
{
$this->strings = array_replace($this->strings, $strings, $this->override);
$result = true;
Expand All @@ -803,59 +781,28 @@ protected function loadLanguage($filename, $extension = 'unknown')
$this->paths[$extension] = array();
}

$this->paths[$extension][$filename] = $result;
$this->paths[$extension][$fileName] = $result;

return $result;
}

/**
* Parses a language file.
*
* @param string $filename The name of the file.
* @param string $fileName The name of the file.
*
* @return array The array of parsed strings.
*
* @since 11.1
*/
protected function parse($filename)
protected function parse($fileName)
{
// Capture hidden PHP errors from the parsing.
if ($this->debug)
{
// See https://secure.php.net/manual/en/reserved.variables.phperrormsg.php
$php_errormsg = null;
$strings = \JLanguageHelper::parseIniFile($fileName, $this->debug);

$trackErrors = ini_get('track_errors');
ini_set('track_errors', true);
}

// This was required for https://github.com/joomla/joomla-cms/issues/17198 but not sure what server setup
// issue it is solving
$disabledFunctions = explode(',', ini_get('disable_functions'));
$isParseIniFileDisabled = in_array('parse_ini_file', array_map('trim', $disabledFunctions));

if (!function_exists('parse_ini_file') || $isParseIniFileDisabled)
// Debug the ini file if needed.
if ($this->debug === true && file_exists($fileName))
{
$contents = file_get_contents($filename);
$contents = str_replace('_QQ_', '"\""', $contents);
$strings = @parse_ini_string($contents);
}
else
{
$strings = @parse_ini_file($filename);
}

if (!is_array($strings))
{
$strings = array();
}

// Restore error tracking to what it was before.
if ($this->debug)
{
ini_set('track_errors', $trackErrors);

$this->debugFile($filename);
$this->debugFile($fileName);
}

return $strings;
Expand Down
51 changes: 48 additions & 3 deletions libraries/src/Language/LanguageHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,17 +407,62 @@ public static function getContentLanguages($publishedStates = array(1), $checkIn
return $languages;
}

/**
* Parse strings from a language file.
*
* @param string $fileName The language ini file path.
* @param boolean $debug If set to true debug language ini file.
*
* @return boolean True if saved, false otherwise.
*
* @since __DEPLOY_VERSION__
*/
public static function parseIniFile($fileName, $debug = false)
{
// Check if file exists.
if (!file_exists($fileName))
{
return array();
}

// @deprecated __DEPLOY_VERSION__ Usage of "_QQ_" is deprecated. Use escaped double quotes (\") instead.
if (!defined('_QQ_'))
{
define('_QQ_', '"');
}

// Capture hidden PHP errors from the parsing.
if ($debug === true)
{
// See https://secure.php.net/manual/en/reserved.variables.phperrormsg.php
$php_errormsg = null;

$trackErrors = ini_get('track_errors');
ini_set('track_errors', true);
}

$strings = @parse_ini_file($fileName);

// Restore error tracking to what it was before.
if ($debug === true)
{
ini_set('track_errors', $trackErrors);
}

return is_array($strings) ? $strings : array();
}

/**
* Save strings to a language file.
*
* @param string $filename The language ini file path.
* @param string $fileName The language ini file path.
* @param array $strings The array of strings.
*
* @return boolean True if saved, false otherwise.
*
* @since 3.7.0
*/
public static function saveToIniFile($filename, array $strings)
public static function saveToIniFile($fileName, array $strings)
{
\JLoader::register('\JFile', JPATH_LIBRARIES . '/joomla/filesystem/file.php');

Expand All @@ -430,7 +475,7 @@ public static function saveToIniFile($filename, array $strings)
// Write override.ini file with the strings.
$registry = new Registry($strings);

return \JFile::write($filename, $registry->toString('INI'));
return \JFile::write($fileName, $registry->toString('INI'));
}

/**
Expand Down

0 comments on commit be42200

Please sign in to comment.