Skip to content

Commit 24cfa2f

Browse files
caseysoftwareclaude
andcommitted
Read translation files with a parser instead of eval()
locales/core.php (and the translation editor in modules/system/translate.php) concatenated the .inc translation files and passed them to eval() wrapped in "array(...)", so anything written into a .inc file ran as PHP code. w2p_Utilities_Translations::parse() reads the same format with PHP's tokenizer and accepts only quoted string literals, "=>" and commas (plus whitespace and comments). An entry containing anything else is skipped rather than executed. For all 259 .inc files in the repository, and for each locale's files concatenated the way core.php does, it returns exactly the array eval() produced (minus the trailing '0' sentinel eval needed). French and English UI strings render as before. Fixes #313 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016CP89WVmGZ1VVCCZMS6ntN
1 parent 708285f commit 24cfa2f

3 files changed

Lines changed: 131 additions & 3 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Reads the translation (.inc) files without eval().
4+
*
5+
* A translation file is a list of PHP array entries, one per line:
6+
*
7+
* 'English text',
8+
* 'key'=>'Translated text',
9+
*
10+
* Earlier versions wrapped the concatenated files in "array(...)" and passed
11+
* the result to eval(), so anything written into a .inc file ran as PHP code.
12+
* This parser uses PHP's own tokenizer and accepts only quoted string
13+
* literals, "=>" and commas (plus whitespace and comments). Any other token
14+
* makes that one entry invalid; it is skipped instead of being executed.
15+
*
16+
* @package web2project\utilities
17+
* @see https://github.com/web2project/web2project/issues/313
18+
*/
19+
class w2p_Utilities_Translations
20+
{
21+
/**
22+
* Parses the content of one or more concatenated .inc files.
23+
*
24+
* @param string $buffer
25+
*
26+
* @return array<int|string, string> the same array eval() produced, without
27+
* the trailing '0' sentinel it needed
28+
*/
29+
public static function parse($buffer)
30+
{
31+
$result = array();
32+
$key = null;
33+
$value = null;
34+
$arrow = false;
35+
$valid = true;
36+
37+
foreach (token_get_all("<?php\n" . $buffer) as $token) {
38+
$id = is_array($token) ? $token[0] : $token;
39+
40+
if (T_OPEN_TAG === $id || T_WHITESPACE === $id || T_COMMENT === $id || T_DOC_COMMENT === $id) {
41+
continue;
42+
}
43+
44+
if (',' === $id) {
45+
if ($valid && null !== $key && (!$arrow || null !== $value)) {
46+
if ($arrow) {
47+
$result[$key] = $value;
48+
} else {
49+
$result[] = $key;
50+
}
51+
}
52+
$key = null;
53+
$value = null;
54+
$arrow = false;
55+
$valid = true;
56+
continue;
57+
}
58+
59+
if (T_CONSTANT_ENCAPSED_STRING === $id) {
60+
if (!$arrow && null === $key) {
61+
$key = self::unquote($token[1]);
62+
} elseif ($arrow && null === $value) {
63+
$value = self::unquote($token[1]);
64+
} else {
65+
$valid = false;
66+
}
67+
} elseif (T_DOUBLE_ARROW === $id && !$arrow && null !== $key) {
68+
$arrow = true;
69+
} else {
70+
// Anything else (a function call, a variable, a concatenation...)
71+
// is not a translation entry.
72+
$valid = false;
73+
}
74+
}
75+
76+
// A final entry without a trailing comma.
77+
if ($valid && null !== $key && (!$arrow || null !== $value)) {
78+
if ($arrow) {
79+
$result[$key] = $value;
80+
} else {
81+
$result[] = $key;
82+
}
83+
}
84+
85+
return $result;
86+
}
87+
88+
/**
89+
* Reads one or more .inc files and parses their combined content.
90+
*
91+
* @param array<int, string> $files
92+
*
93+
* @return array<int|string, string>
94+
*/
95+
public static function parseFiles(array $files)
96+
{
97+
$buffer = '';
98+
foreach ($files as $file) {
99+
if (is_file($file)) {
100+
$buffer .= file_get_contents($file) . "\n";
101+
}
102+
}
103+
104+
return self::parse($buffer);
105+
}
106+
107+
/**
108+
* Returns the value of a PHP string literal token, as PHP would.
109+
*
110+
* @param string $literal
111+
*
112+
* @return string
113+
*/
114+
protected static function unquote($literal)
115+
{
116+
$quote = $literal[0];
117+
$body = substr($literal, 1, -1);
118+
119+
if ("'" === $quote) {
120+
return strtr($body, array('\\\\' => '\\', "\\'" => "'"));
121+
}
122+
123+
// A double-quoted literal with no variables in it (otherwise the
124+
// tokenizer would not have returned T_CONSTANT_ENCAPSED_STRING).
125+
return stripcslashes($body);
126+
}
127+
}

‎locales/core.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@
5959
}
6060
break;
6161
}
62-
eval('$GLOBALS[\'translate\']=array(' . ob_get_contents() . "\n'0');");
62+
// Parsed, not eval()'d: see w2p_Utilities_Translations.
63+
$GLOBALS['translate'] = w2p_Utilities_Translations::parse(ob_get_contents());
6364
ob_end_clean();

‎modules/system/translate.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
readfile($localeFile);
3333
}
3434
}
35-
eval("\$english=array(" . ob_get_contents() . "\n'0');");
35+
$english = w2p_Utilities_Translations::parse(ob_get_contents());
3636
ob_end_clean();
3737

3838
$trans = array();
@@ -54,7 +54,7 @@
5454
readfile($localeFile);
5555
}
5656
}
57-
eval("\$locale=array(" . ob_get_contents() . "\n'0');");
57+
$locale = w2p_Utilities_Translations::parse(ob_get_contents());
5858
ob_end_clean();
5959

6060
foreach ($locale as $k => $v) {

0 commit comments

Comments
 (0)