Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Remove the calls to eval() when using the `System::convertXlfToPhp(…
Browse files Browse the repository at this point in the history
…)` method
  • Loading branch information
leofeyer committed Aug 27, 2014
1 parent 3474d99 commit e761e1e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
2 changes: 1 addition & 1 deletion system/modules/core/dca/tl_settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ public function disableModules(DataContainer $dc)
// Load the modules language file of disabled extensions
if (file_exists(TL_ROOT . '/' . $strFile . '.xlf'))
{
eval(static::convertXlfToPhp($strFile . '.xlf', $lng));
static::convertXlfToPhp($strFile . '.xlf', $lng, true);
}
elseif (file_exists(TL_ROOT . '/' . $strFile . '.php'))
{
Expand Down
53 changes: 36 additions & 17 deletions system/modules/core/library/Contao/System.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public static function loadLanguageFile($strName, $strLanguage=null, $blnNoCache

if (file_exists(TL_ROOT . '/' . $strFile . '.xlf'))
{
eval(static::convertXlfToPhp($strFile . '.xlf', $strCreateLang));
static::convertXlfToPhp($strFile . '.xlf', $strCreateLang, true);
}
elseif (file_exists(TL_ROOT . '/' . $strFile . '.php'))
{
Expand Down Expand Up @@ -628,12 +628,13 @@ protected static function readPhpFileWithoutTags($strName)
/**
* Convert an .xlf file into a PHP language file
*
* @param string $strName The name of the .xlf file
* @param string $strLanguage The language code
* @param string $strName The name of the .xlf file
* @param string $strLanguage The language code
* @param boolean $blnLoad Add the labels to the global language array
*
* @return string The PHP code
*/
protected static function convertXlfToPhp($strName, $strLanguage)
protected static function convertXlfToPhp($strName, $strLanguage, $blnLoad=false)
{
// Read the .xlf file
$xml = new \DOMDocument();
Expand All @@ -660,6 +661,19 @@ protected static function convertXlfToPhp($strName, $strLanguage)
}
};

// Set up the quotevalue function
$quotevalue = function($value)
{
if (strpos($value, '\n') !== false)
{
return '"' . str_replace('"', '\\"', $value) . '"';
}
else
{
return "'" . str_replace("'", "\\'", $value) . "'";
}
};

// Add the labels
foreach ($units as $unit)
{
Expand All @@ -678,16 +692,6 @@ protected static function convertXlfToPhp($strName, $strLanguage)
$value = str_replace('</ em>', '</em>', $value);
}

// Quote the value
if (strpos($value, '\n') !== false)
{
$value = '"' . str_replace('"', '\\"', $value) . '"';
}
else
{
$value = "'" . str_replace("'", "\\'", $value) . "'";
}

$chunks = explode('.', $unit->getAttribute('id'));

// Handle keys with dots
Expand All @@ -700,15 +704,30 @@ protected static function convertXlfToPhp($strName, $strLanguage)
switch (count($chunks))
{
case 2:
$return .= "\$GLOBALS['TL_LANG']['" . $chunks[0] . "'][" . $quotekey($chunks[1]) . "] = " . $value . ";\n";
$return .= "\$GLOBALS['TL_LANG']['" . $chunks[0] . "'][" . $quotekey($chunks[1]) . "] = " . $quotevalue($value) . ";\n";

if ($blnLoad)
{
$GLOBALS['TL_LANG'][$chunks[0]][$chunks[1]] = $value;
}
break;

case 3:
$return .= "\$GLOBALS['TL_LANG']['" . $chunks[0] . "'][" . $quotekey($chunks[1]) . "][" . $quotekey($chunks[2]) . "] = " . $value . ";\n";
$return .= "\$GLOBALS['TL_LANG']['" . $chunks[0] . "'][" . $quotekey($chunks[1]) . "][" . $quotekey($chunks[2]) . "] = " . $quotevalue($value) . ";\n";

if ($blnLoad)
{
$GLOBALS['TL_LANG'][$chunks[0]][$chunks[1]][$chunks[2]] = $value;
}
break;

case 4:
$return .= "\$GLOBALS['TL_LANG']['" . $chunks[0] . "'][" . $quotekey($chunks[1]) . "][" . $quotekey($chunks[2]) . "][" . $quotekey($chunks[3]) . "] = " . $value . ";\n";
$return .= "\$GLOBALS['TL_LANG']['" . $chunks[0] . "'][" . $quotekey($chunks[1]) . "][" . $quotekey($chunks[2]) . "][" . $quotekey($chunks[3]) . "] = " . $quotevalue($value) . ";\n";

if ($blnLoad)
{
$GLOBALS['TL_LANG'][$chunks[0]][$chunks[1]][$chunks[2]][$chunks[3]] = $value;
}
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions system/modules/devtools/modules/ModuleLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,12 @@ protected function compile()

// Include English file
$GLOBALS['TL_LANG'] = array();
eval(\System::convertXlfToPhp($strPath . '/' . $strFile, 'en'));
\System::convertXlfToPhp($strPath . '/' . $strFile, 'en', true);
$arrOld = $GLOBALS['TL_LANG'];

// Include foreign file
$GLOBALS['TL_LANG'] = array();
eval(\System::convertXlfToPhp($strLang . '/' . $strFile, $lng));
\System::convertXlfToPhp($strLang . '/' . $strFile, $lng, true);
$arrNew = $GLOBALS['TL_LANG'];

// Restore the former labels
Expand Down

0 comments on commit e761e1e

Please sign in to comment.