Skip to content

Commit d1a632c

Browse files
committed
Language file import script
1 parent ad98c90 commit d1a632c

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

helper_scripts/import_langfile.php

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
<?php
2+
3+
/*
4+
Copyright (c) 2007-2016, Till Brehm, projektfarm Gmbh
5+
All rights reserved.
6+
7+
Redistribution and use in source and binary forms, with or without modification,
8+
are permitted provided that the following conditions are met:
9+
10+
* Redistributions of source code must retain the above copyright notice,
11+
this list of conditions and the following disclaimer.
12+
* Redistributions in binary form must reproduce the above copyright notice,
13+
this list of conditions and the following disclaimer in the documentation
14+
and/or other materials provided with the distribution.
15+
* Neither the name of ISPConfig nor the names of its contributors
16+
may be used to endorse or promote products derived from this software without
17+
specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22+
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23+
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24+
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26+
OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28+
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
require "/usr/local/ispconfig/interface/lib/config.inc.php";
32+
require "/usr/local/ispconfig/interface/lib/app.inc.php";
33+
34+
set_time_limit(0);
35+
ini_set('error_reporting', E_ALL & ~E_NOTICE);
36+
37+
//** Get commandline options
38+
$cmd_opt = getopt('', array('lng:','isppath::'));
39+
40+
if(isset($cmd_opt['lng']) && is_file($cmd_opt['lng'])) {
41+
// Language file that shall be imported
42+
$lang_file = $cmd_opt['lng'];
43+
} else {
44+
die('Usage example: php import_langfile.php --lng=de.lng --isppath=/usr/local/ispconfig'."\n");
45+
}
46+
47+
if(isset($cmd_opt['isppath']) && is_dir($cmd_opt['isppath'])) {
48+
$ispconfig_path = $cmd_opt['isppath'];
49+
} else {
50+
$ispconfig_path = '/usr/local/ispconfig';
51+
}
52+
53+
function normalize_string($string, $quote, $allow_special = false) {
54+
$escaped = false;
55+
$in_string = true;
56+
$new_string = '';
57+
58+
for($c = 0; $c < mb_strlen($string); $c++) {
59+
$char = mb_substr($string, $c, 1);
60+
61+
if($in_string === true && $escaped === false && $char === $quote) {
62+
// this marks a string end (e.g. for concatenation)
63+
$in_string = false;
64+
continue;
65+
} elseif($in_string === false) {
66+
if($escaped === false && $char === $quote) {
67+
$in_string = true;
68+
continue;
69+
} else {
70+
continue; // we strip everything from outside the string!
71+
}
72+
}
73+
74+
if($char === '"' && $escaped === true && $quote === '"') {
75+
// unescape this
76+
$new_string .= $char;
77+
$escaped = false;
78+
continue;
79+
} elseif($char === "'" && $escaped === false && $quote === '"') {
80+
// escape this
81+
$new_string .= '\\' . $char;
82+
continue;
83+
}
84+
85+
if($escaped === true) {
86+
// the next character is the escaped one.
87+
if($allow_special === true && ($char === 'n' || $char === 'r' || $char === 't')) {
88+
$new_string .= '\' . "\\' . $char . '" . \'';
89+
} else {
90+
$new_string .= '\\' . $char;
91+
}
92+
$escaped = false;
93+
} else {
94+
if($char === '\\') {
95+
$escaped = true;
96+
} else {
97+
$new_string .= $char;
98+
}
99+
}
100+
}
101+
return $new_string;
102+
}
103+
104+
function validate_line($line) {
105+
$line = trim($line);
106+
if($line === '' || $line === '<?php' || $line === '?>') return $line; // don't treat empty lines as malicious
107+
108+
$ok = preg_match('/^\s*\$wb\[(["\'])(.*?)\\1\]\s*=\s*(["\'])(.*?)\\3\s*;\s*$/', $line, $matches);
109+
if(!$ok) return false; // this line has invalid form and could lead to malfunction
110+
111+
$keyquote = $matches[1]; // ' or "
112+
$key = $matches[2];
113+
if(strpos($key, '"') !== false || strpos($key, "'") !== false) return false;
114+
115+
$textquote = $matches[3]; // ' or "
116+
$text = $matches[4];
117+
118+
$new_line = '$wb[\'';
119+
120+
// validate the language key
121+
$key = normalize_string($key, $keyquote);
122+
123+
$new_line .= $key . '\'] = \'';
124+
125+
// validate this text to avoid code injection
126+
$text = normalize_string($text, $textquote, true);
127+
128+
$new_line .= $text . '\';';
129+
130+
return $new_line;
131+
}
132+
133+
$lines = file($lang_file);
134+
135+
define('ISPC_ROOT_PATH', $ispconfig_path.'/interface');
136+
define('ISPC_LIB_PATH', ISPC_ROOT_PATH.'/lib');
137+
define('ISPC_WEB_PATH', ISPC_ROOT_PATH.'/web');
138+
139+
// initial check
140+
$parts = explode('|', $lines[0]);
141+
if($parts[0] == '---' && $parts[1] == 'ISPConfig Language File') {
142+
unset($lines[0]);
143+
144+
$buffer = '';
145+
$langfile_path = '';
146+
// all other lines
147+
$ln = 1;
148+
foreach($lines as $line) {
149+
$ln++;
150+
$parts = explode('|', $line);
151+
if(is_array($parts) && count($parts) > 0 && $parts[0] == '--') {
152+
// Write language file, if its not the first file
153+
if($buffer != '' && $langfile_path != '') {
154+
$buffer = trim($buffer)."\n";
155+
$msg .= "File written: $langfile_path\n";
156+
file_put_contents($langfile_path, $buffer);
157+
}
158+
// empty buffer and set variables
159+
$buffer = '';
160+
$module_name = trim($parts[1]);
161+
$selected_language = trim($parts[2]);
162+
$file_name = trim($parts[3]);
163+
if(!preg_match("/^[a-z]{2}$/i", $selected_language)) die("unallowed characters in selected language name: $selected_language");
164+
if(!preg_match("/^[a-z_]+$/i", $module_name)) die('unallowed characters in module name.');
165+
if(!preg_match("/^[a-z\._\-]+$/i", $file_name) || stristr($file_name, '..')) die("unallowed characters in language file name: '$file_name'");
166+
if($module_name == 'global') {
167+
$langfile_path = trim(ISPC_LIB_PATH."/lang/".$selected_language.".lng");
168+
} else {
169+
$langfile_path = trim(ISPC_WEB_PATH.'/'.$module_name.'/lib/lang/'.$file_name);
170+
}
171+
} elseif(is_array($parts) && count($parts) > 1 && $parts[0] == '---' && $parts[1] == 'EOF') {
172+
// EOF line, ignore it.
173+
} else {
174+
$line = validate_line($line);
175+
if($line === false) $error .= "Language file contains invalid language entry on line $ln.\n";
176+
else $buffer .= $line."\n";
177+
}
178+
}
179+
}
180+
181+
echo $error;
182+
echo $msg;
183+
die("finished import.\n");
184+
185+
?>

0 commit comments

Comments
 (0)