/
MantisError.class.php
400 lines (339 loc) · 12 KB
/
MantisError.class.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
class MantisError
{
/**
* Indicates previous errors
*/
private static $_allErrors = array();
/**
* Indicates if an error/exception has been handled
* Note: this also indicates additional shutdown functions have been setup.
*/
private static $_handled = false;
private static $_errorConstants = array(
'1'=>'E_ERROR',
'2'=>'E_WARNING',
'4'=>'E_PARSE',
'8'=>'E_NOTICE',
'16'=>'E_CORE_ERROR',
'32'=>'E_CORE_WARNING',
'64'=>'E_COMPILE_ERROR',
'128'=>'E_COMPILE_WARNING',
'256'=>'APPLICATION ERROR', // E_USER_ERROR
'512'=>'APPLICATION WARNING', // E_USER_WARNING
'1024'=>'E_USER_NOTICE',
'2048'=>'E_STRICT',
'4096'=>'E_RECOVERABLE_ERROR',
'8192'=>'E_DEPRECATED',
'16384'=>'E_USER_DEPRECATED',
);
private static $_parameters = array();
private static $_proceed_url = null;
public static function init(){
if( self::$_handled === false ) {
// first run
register_shutdown_function(array('MantisError', 'display_errors'));
self::$_handled = true;
}
}
public static function exception_handler( Exception $ex) {
self::init();
$errorInfo = new stdClass();
$errorInfo->time = time();
$errorInfo->type = 'EXCEPTION';
$errorInfo->name = get_class($ex);
$errorInfo->code = $ex->getCode();
$errorInfo->message = $ex->getMessage();
$errorInfo->file = $ex->getFile();
$errorInfo->line = $ex->getLine();
$errorInfo->trace = $ex->getTrace();
if( method_exists( $ex, 'getContext' ) ) {
$errorInfo->context = $ex->getContext();
} else {
$errorInfo->context = null;
}
self::$_allErrors[] = $errorInfo;
}
public static function error_handler( $p_type, $p_error, $p_file, $p_line, $p_context ) {
if (0 == error_reporting())
{
return;
}
if( substr( $p_error, 0, 7) == 'plugin_' || is_numeric( $p_error ) ) {
$t_message = self::error_string( $p_error );
} else {
$t_message = $p_error;
}
throw new ErrorException($t_message, $p_type, /*severity*/0, $p_file, $p_line);
return;
}
public static function shutdown_error_handler() {
$t_error = error_get_last();
if( $t_error === null ) {
return;
}
self::init();
$errorInfo = new stdClass();
$errorInfo->time = time();
$errorInfo->type = 'ERROR_LAST';
$errorInfo->name = isset( self::$_errorConstants[$t_error['type']] ) ? self::$_errorConstants[$t_error['type']] : 'UNKNOWN';
$errorInfo->code = $t_error['type'];
$errorInfo->message = $t_error['message'];
$errorInfo->file = $t_error['file'];
$errorInfo->line = $t_error['line'];
$errorInfo->trace = debug_backtrace();
$errorInfo->context = null;
self::$_allErrors[] = $errorInfo;
}
public static function display_errors( $p_no_header = false ) {
# disable any further event callbacks
if ( function_exists( 'event_clear_callbacks' ) ) {
event_clear_callbacks();
}
$t_oblen = ob_get_length();
if( self::error_handled() && $t_oblen > 0 ) {
$t_old_contents = ob_get_contents();
}
$t_last = error_get_last();
$t_fatal = false;
if( isset($t_last['type']) && ( $t_last['type'] == E_PARSE ) ) {
// FATAL error state - we'll try and get through this as quickly as possible
$t_fatal = true;
}
# We need to ensure compression is off - otherwise the compression headers are output.
compress_disable();
# then clean the buffer, leaving output buffering on.
if( $t_oblen > 0 ) {
ob_clean();
}
echo '<?xml version="1.0" encoding="utf-8"?>';
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en" >';
echo '<head><title>Error Page</title>';
echo '<style>table.width70 { width: 70%; border: solid 1px #000000; }</style></head><body>';
echo '<p align="center"><img src="' . helper_mantis_url('images/mantis_logo.gif') . '" /></p>';
echo '<hr />';
echo '<div align="center">';
if ( !$t_fatal ) {
try {
echo lang_get( 'error_no_proceed' );
} catch (Exception $e) {
}
}
echo '<br />';
foreach ( self::$_allErrors as $key => $errorInfo ) {
self::display_error( $errorInfo );
if ( $key == 0 && sizeof( self::$_allErrors ) > 1 ) {
echo '<p>Previous non-fatal errors occurred:</p>';
}
}
echo '</div>';
if ( !config_get( 'show_friendly_errors' ) ) {
if( isset( $t_old_contents ) ) {
echo '<p>Page contents follow.</p>';
echo '<div style="border: solid 1px black;padding: 4px"><pre>';
echo htmlspecialchars($t_old_contents);
echo '</pre></div>';
}
}
echo '<hr /><br /><br /><br />';
echo '</body></html>', "\n";
exit();
}
public static function display_error( $p_error) {
echo '<br /><div><table class="width70" cellspacing="1">';
echo '<tr><td class="form-title">' . $p_error->name . '</td></tr>';
echo '<tr><td><p class="center" style="color:red">' . nl2br( lang_get_defaulted( $p_error->message ) ) . '</p></td></tr>';
echo '<tr><td>';
self::error_print_details( basename( $p_error->file ), $p_error->line, $p_error->context );
echo '</td></tr>';
if ( !config_get( 'show_friendly_errors' ) ) {
echo '<tr><td>';
self::error_print_stack_trace( $p_error->trace );
echo '</td></tr>';
}
echo '</table></div>';
}
/**
* Print out the error details including context
* @param string $p_file
* @param int $p_line
* @param string $p_context
* @return null
*/
public static function error_print_details( $p_file, $p_line, $p_context ) {
if ( !config_get( 'show_friendly_errors' ) ) {
?>
<table class="width75">
<tr>
<td>Filename: <?php echo htmlentities( $p_file, ENT_COMPAT, 'UTF-8' );?></td>
</tr>
<tr>
<td>Line: <?php echo $p_line?></td>
</tr>
<tr>
<td>
<?php self::error_print_context( $p_context )?>
</td>
</tr>
</table>
<?php
} else {
if( strpos( $p_file, '.' ) !== false ) {
$t_components = explode( '.', $p_file );
$p_file = current( $t_components );
}
?>
<table class="width75">
<tr>
<td>ID: <?php echo htmlentities( $p_file, ENT_COMPAT, 'UTF-8' );?>:<?php echo $p_line?></td>
</tr>
</table>
<?php
}
}
/**
* Print out the variable context given
* @param string $p_context
* @return null
*/
public static function error_print_context( $p_context ) {
if( !is_array( $p_context ) && !is_object( $p_context )) {
return;
}
echo '<table class="width100"><tr><th>Variable</th><th>Value</th><th>Type</th></tr>';
# print normal variables
foreach( $p_context as $t_var => $t_val ) {
if( !is_array( $t_val ) && !is_object( $t_val ) ) {
$t_type = gettype( $t_val );
$t_val = htmlentities( (string) $t_val, ENT_COMPAT, 'UTF-8' );
# Mask Passwords
if( strpos( $t_var, 'pass' ) !== false ) {
$t_val = '**********';
}
echo '<tr><td>', $t_var, '</td><td>', $t_val, '</td><td>', $t_type, '</td></tr>', "\n";
}
}
# print arrays
foreach( $p_context as $t_var => $t_val ) {
if( is_array( $t_val ) && ( $t_var != 'GLOBALS' ) ) {
echo '<tr><td colspan="3"><br /><strong>', $t_var, '</strong></td></tr>';
echo '<tr><td colspan="3">';
self::error_print_context( $t_val );
echo '</td></tr>';
}
}
echo '</table>';
}
public static function error_print_stack_trace( $p_stack ) {
echo '<table class="width75">';
echo '<tr><th>Filename</th><th>Line</th><th></th><th></th><th>Function</th><th>Args</th></tr>';
# remove the call to the error handler from the stack trace
array_shift( $p_stack );
foreach( $p_stack as $t_frame ) {
echo '<tr ', self::error_alternate_class(), '>';
echo '<td>', ( isset( $t_frame['file'] ) ? htmlentities( $t_frame['file'], ENT_COMPAT, 'UTF-8' ) : '-' ), '</td><td>', ( isset( $t_frame['line'] ) ? $t_frame['line'] : '-' ), '</td><td>', ( isset( $t_frame['class'] ) ? $t_frame['class'] : '-' ), '</td><td>', ( isset( $t_frame['type'] ) ? $t_frame['type'] : '-' ), '</td><td>', ( isset( $t_frame['function'] ) ? $t_frame['function'] : '-' ), '</td>';
$t_args = array();
if( isset( $t_frame['args'] ) && !empty( $t_frame['args'] ) ) {
foreach( $t_frame['args'] as $t_value ) {
$t_args[] = self::error_build_parameter_string( $t_value );
}
echo '<td>( ', htmlentities( implode( $t_args, ', ' ), ENT_COMPAT, 'UTF-8' ), ' )</td></tr>';
} else {
echo '<td>-</td></tr>';
}
}
echo '</table>';
}
public static function error_build_parameter_string( $p_param, $p_showtype = true, $p_depth = 0 ) {
if( $p_depth++ > 10 ) {
return '<strong>***Nesting Level Too Deep***</strong>';
}
if( is_array( $p_param ) ) {
$t_results = array();
foreach( $p_param as $t_key => $t_value ) {
# Mask Passwords
if( strpos( $t_key, 'pass' ) !== false ) {
$t_value = '**********';
}
$t_results[] = '[' . self::error_build_parameter_string( $t_key, false, $p_depth ) . ']' . ' => ' . self::error_build_parameter_string( $t_value, false, $p_depth );
}
return '<Array> { ' . implode( $t_results, ', ' ) . ' }';
}
else if( is_object( $p_param ) ) {
$t_results = array();
$t_class_name = get_class( $p_param );
$t_inst_vars = get_object_vars( $p_param );
foreach( $t_inst_vars as $t_name => $t_value ) {
$t_results[] = "[$t_name]" . ' => ' . self::error_build_parameter_string( $t_value, false, $p_depth );
}
return '<Object><' . $t_class_name . '> ( ' . implode( $t_results, ', ' ) . ' )';
} else {
if( $p_showtype ) {
return '<' . gettype( $p_param ) . '>' . var_export( $p_param, true );
} else {
return var_export( $p_param, true );
}
}
}
/**
* Return an error string (in the current language) for the given error.
* @param int $p_error
* @return string
* @access public
*/
public static function error_string( $p_error ) {
# We pad the parameter array to make sure that we don't get errors if
# the caller didn't give enough parameters for the error string
$t_padding = array_pad( array(), 10, '' );
if( is_numeric( $p_error ) ) {
$t_error = lang_get( $p_error, null, false );
} else {
$t_error = lang_get_defaulted( $p_error );
}
if( $t_error == '' ) {
return lang_get( 'missing_error_string' ) . $p_error;
}
# ripped from string_api
$t_string = call_user_func_array( 'sprintf', array_merge( array( $t_error ), self::$_parameters, $t_padding ) );
return preg_replace( "/&(#[0-9]+|[a-z]+);/i", "&$1;", @htmlspecialchars( $t_string, ENT_COMPAT, 'UTF-8' ) );
}
/**
* Simple version of helper_alternate_class for use by error api only.
* @access private
* @return string representing css class
*/
public static function error_alternate_class() {
static $t_errindex = 1;
if( 1 == $t_errindex++ % 2 ) {
return 'class="row-1"';
} else {
return 'class="row-2"';
}
}
/**
* Set additional info parameters to be used when displaying the next error
* This function takes a variable number of parameters
*
* When writing internationalized error strings, note that you can change the
* order of parameters in the string. See the PHP manual page for the
* sprintf() function for more details.
* @access public
* @return null
*/
public static function error_parameters( $p_args ) {
self::$_parameters = $p_args;
}
public static function error_proceed_url( $p_url ) {
self::$_proceed_url = $p_url();
}
/**
* Check if we have handled an error during this page
* Return true if an error has been handled, false otherwise
* @return bool
*/
public static function error_handled() {
return self::$_handled;
}
}
?>