/
test_langs.php
284 lines (254 loc) · 7.6 KB
/
test_langs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
<?php
# MantisBT - a php based bugtracking system
# MantisBT is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# MantisBT is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MantisBT. If not, see <http://www.gnu.org/licenses/>.
/**
* @package MantisBT
* @copyright Copyright (C) 2000 - 2002 Kenzaburo Ito - kenito@300baud.org
* @copyright Copyright (C) 2002 - 2009 MantisBT Team - mantisbt-dev@lists.sourceforge.net
* @link http://www.mantisbt.org
*/
/**
* MantisBT Core API's
*/
require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core.php' );
access_ensure_global_level( ADMINISTRATOR );
set_time_limit( 0 );
if( function_exists( 'xdebug_disable' ) ) {
xdebug_disable();
}
require_once( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'core' . DIRECTORY_SEPARATOR . 'constant_inc.php' );
if( !defined( 'T_ML_COMMENT' ) ) {
define( 'T_ML_COMMENT', T_COMMENT );
}
else {
define( 'T_DOC_COMMENT', T_ML_COMMENT );
}
// check core language files
if( function_exists( 'opendir' ) && function_exists( 'readdir' ) ) {
$t_lang_files = Array();
if( $handle = opendir( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' ) ) {
while( false !== ( $file = readdir( $handle ) ) ) {
if( $file[0] != '.' && $file != 'langreadme.txt' && !is_dir( $file ) ) {
$t_lang_files[] = $file;
}
}
closedir( $handle );
}
}
else {
$t_lang_files = Array();
foreach( $g_language_choices_arr as $t_lang ) {
if( $t_lang == 'auto' ) {
continue;
}
$t_lang_files[] = $t_lang;
}
}
if( sizeof( $t_lang_files ) > 0 ) {
echo 'Retrieved ', sizeof( $t_lang_files ), ' languages<br />';
foreach( $t_lang_files as $t_file ) {
echo "Testing language file '$t_file' (phase 1)...<br />";
flush();
checkfile( dirname( dirname( __FILE__ ) ) . DIRECTORY_SEPARATOR . 'lang' . DIRECTORY_SEPARATOR, $t_file );
}
}
// attempt to find plugin language files
echo "Trying to find+check plugin language files...<br />";
if( function_exists( 'opendir' ) && function_exists( 'readdir' ) ) {
checklangdir ( config_get( 'plugin_path' ) );
} else {
echo 'php opendir/readdir are disabled - skipping<br />';
}
function checklangdir( $p_path, $p_subpath = '' ) {
$p_path = $p_path . DIRECTORY_SEPARATOR . $p_subpath . DIRECTORY_SEPARATOR;
if( $handle = opendir( $p_path ) ) {
while( false !== ( $file = readdir( $handle ) ) ) {
if ( $file[0] == '.' )
continue;
if ( $p_subpath == '' ) {
echo "Checking language files for plugin $file:<br />";
}
if( !is_dir( $p_path . DIRECTORY_SEPARATOR . $file ) && $p_subpath == 'lang' ) {
checkfile( $p_path, $file );
} else {
if ( is_dir( $p_path . DIRECTORY_SEPARATOR . $file ) )
checklangdir( $p_path, $file);
}
}
closedir( $handle );
}
}
function checkfile( $p_path, $p_file ) {
echo "Testing language file '$p_file' (phase 1)...<br />";
flush();
$file = $p_path . $p_file;
$result = checktoken( $file );
if( !$result ) {
print_error( "FAILED: Language file '$p_file' failed at phase 1." );
}
echo "Testing language file '$p_file' (phase 2)...<br />";
flush();
set_error_handler( 'lang_error_handler' );
ob_start();
$result = eval( "require_once( '$file' );" );
$data = ob_get_contents();;
ob_end_clean();
restore_error_handler();
if( $result === false ) {
print_error( "FAILED: Language file '$p_file' failed at eval" );
}
if( strlen( $data ) > 0 ) {
print_error( "FAILED: Language file '$p_file' failed at require_once (data output of length " . strlen( $data ) . ")" );
}
}
function checktoken( $file ) {
$in_php_code = false;
$variables = Array();
$current_var = null;
$last_token = 0;
$set_variable = false;
$variablearr = false;
$twopartstring = false;
$need_end_variable = false;
$source = file_get_contents( $file );
$tokens = token_get_all( $source );
$expectendarr = false;
$settingvariable = false;
$pass = true;
foreach( $tokens as $token ) {
$last_token2 = 0;
if( is_string( $token ) ) {
switch( $token ) {
case '=':
if( $last_token != T_VARIABLE ) {
print_error( "ERROR: = sign without variable" );
$pass = false;
}
$set_variable = true;
break;
case '[':
if( $last_token != T_VARIABLE ) {
$pass = false;
}
$variablearr = true;
break;
case ']':
if( !$expectendarr ) {
$pass = false;
}
$expectendarr = false;
break;
case ';':
if( !$need_end_variable ) {
print_error( "ERROR: function seperator found at unexpected location (line $line)" );
$pass = false;
}
$need_end_variable = false;
break;
case '.':
if( $last_token == T_CONSTANT_ENCAPSED_STRING ) {
$twopartstring = true;
} else {
print_error( "ERROR: string concat found at unexpected location (line $line)" );
$pass = false;
}
break;
default:
print_error( "UNKNOWN TOKEN" . $token );
$pass = false;
break;
}
} else {
// token array
list( $id, $text, $line ) = $token;
if( $id == T_WHITESPACE || $id == T_COMMENT || $id == T_DOC_COMMENT || $id == T_ML_COMMENT ) {
continue;
}
if( $need_end_variable ) {
if( $twopartstring && $id == T_CONSTANT_ENCAPSED_STRING ) {
$twopartstring = false;
continue;
}
if( $settingvariable && $id == T_STRING ) {
$last_token = T_VARIABLE;
$expectendarr = true;
continue;
}
print_error( "ERROR" . $id . token_name( $id ) . $text . $line );
$pass = false;
}
switch( $id ) {
case T_OPEN_TAG:
$in_php_code = true;
break;
case T_CLOSE_TAG:
$in_php_code = false;
break;
case T_INLINE_HTML:
print_error( "ERROR: Whitespace in language file outside of PHP code block (line $line)" );
$pass = false;
break;
case T_VARIABLE:
if( $set_variable && $current_var != null ) {
$need_end_variable = true;
$settingvariable = true;
$current_var = null;
break;
}
$current_var = $text;
break;
case T_STRING:
if( $variablearr ) {
$current_var .= $text;
if( !defined( $text ) ) {
print_error( "undefined constant: $current_var" );
}
} else {
print_error( "ERROR: T_STRING found at unexpected location (line $line)" );
$pass = false;
}
$last_token2 = T_VARIABLE;
$expectendarr = true;
break;
case T_CONSTANT_ENCAPSED_STRING:
if( $last_token == T_VARIABLE && $set_variable && $current_var != null ) {
if( isset( $variables[$current_var] ) ) {
print_error( "ERROR: duplicate language string ($current_var ) (line $line)" );
} else {
$variables[$current_var] = $text;
}
}
$current_var = null;
$need_end_variable = true;
break;
default:
// if (!$in_php_code)
print_error( "PARSER: " . $id . token_name( $id ) . $text . $line );
$pass = false;
break;
}
$last_token = $id;
if( $last_token2 > 0 ) {
$last_token = $last_token2;
}
}
}
return $pass;
}
function lang_error_handler( $p_type, $p_error, $p_file, $p_line, $p_context ) {
print_error( "error handler thrown: ", $p_type, $p_error, $p_file, $p_line, $p_context );
}
function print_error( $p_string ) {
echo "<font color='red'>ERROR: ", $p_string, '</font><br>';
}