Browse the code
| Revision log Information on the revision | |
|---|---|
| Revision: | 243 (differences) |
| Author: | xbright |
| Log message: |
* Updated the way to show sourcecode |
| Change revision: | |
0
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
<?php # This file is a part of CodingTeam. See <http://www.codingteam.net>. # Copyright (C) 2007-2009 CodingTeam (See AUTHORS and THANKS for details) # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, version 3 only. # # This program 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 Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. /** * @file * This file contains the ErrorHandler class * * Handle both PHP and CodingTeam errors, show them in a human-way to * the user, log them and finally exit. */ /** * ErrorHandler class */ class ErrorHandler { public $lang, $show_debug, $log_errors, $log_level, $show_notice; /* * Constructor * * Construct an instance of ErrorHandler. * @param $lang * The spoken language of the user. * @param $show_debug * A boolean to turn on or off the debug. This will show source code and * error context. It's only launched if the error status is 3. * @param $log_errors * A boolean to turn on or off errors logging. * @param $log_level * An int of the first level of error status to be logged. * If you want to log level 2 and 3 (critical and internal), set it to 2. * @param $show_notice * A boolean to turn on or off notice catch and show. */ function __construct($lang, $show_debug, $log_errors, $log_level, $show_notice) { $this->lang = $lang; $this->show_debug = $show_debug; $this->log_errors = $log_errors; $this->log_level = $log_level; $this->show_notice = $show_notice; } /** * Load the view * * Generate a pretty textarea with optional Javascript content. * @param $error * The error message. * @param $status * The error status (int). Different levels are: * - -1: Unlogged user, in login.tpl * (only if the forge is not public, force login) * - 0: Banal, in warning.tpl * (eg. 404, 403, 401, non-existing resource…) * - 1: Important,in error.tpl * (eg. database request error…) * - 2: Critical, in error.tpl * (eg. database connection error…) * - 3: Internal error, in error.tpl * (only PHP errors, not trigged manually by displayError) * @param $http (optional) * HTTP Headers to send. */ function displayError($error, $status, $http='') { // If the configuration tell to ignore E_NOTICE, ignore them if (!$this->show_notice && $status == 3 && $error[0] == 8) return FALSE; /* In this file, if an external server (for OpenForge external search) * is not online, we get an ugly warning, just ignore it (that's not * so bad, that's just a work-around: WARNING shouldn't be blocking, * but our class make them so). */ if (($error[0] == 2 || $error[0] == 8) && $error[2] == CT_BASEDIR. '/inc/modules/search/views/default.php') return FALSE; // Same work to prevent ugly errors when documenters are bad boys // FIXME: this is bug in textview.php, that sort of "fix" are ugly! if ($error[0] == 2 && $error[2] == CT_BASEDIR. '/inc/classes/textview.php') return FALSE; // Don't show E_STRICT and E_NOTICE for libs used by CodingTeam $libs = CT_BASEDIR.'/inc/libs'; $nlibs = mb_strlen($libs); if (($error[0] == 2048 || $error[0] == 8) && mb_substr($error[2], 0, $nlibs) == $libs) return FALSE; /* Error logging. * * If you enable error logging, don't forget to create a error.log file * in the CodingTeam root directory and allow Apache HTTPD to write in * this file. */ if ($status == 3) { switch ($error[0]) { case 1: $errmsg = 'E_ERROR'; break; case 2: $errmsg = 'E_WARNING'; break; case 4: $errmsg = 'E_PARSE'; break; case 8: $errmsg = 'E_NOTICE'; break; case 2048: $errmsg = 'E_STRICT'; break; default: $errmsg = 'E_UNKNOWN'; } $errinfos = ' (file: '.$error[2].'@'.$error[3].')'; $errdesc = $error[1]; } elseif ($status == -1) { $errmsg = 'E_CODINGTEAM'; $errinfos = ''; $errdesc = $error[0]; } else { $errmsg = 'E_CODINGTEAM'; $errinfos = ''; $errdesc = $error; } // Log the error into a file (use log_errors/log_level). if ($status >= $this->log_level && $this->log_errors) { $file = CT_BASEDIR.'/error.log'; $data = '['.date('Y-m-d H:i:s').'] ['.$errmsg.'] [client '. $_SERVER['REMOTE_ADDR'].'] '.$errdesc. ' '. "'".$_SERVER['REQUEST_URI']."'".$errinfos."\n"; file_put_contents($file, $data, FILE_APPEND | LOCK_EX); } // If a HTTP header is specified, send it if (!empty($http)) Header('HTTP/1.0 '.$http); // Error status if ($status == -1) $errstat = i18n('Unlogged user'); elseif ($status == 0) $erstat = i18n('Banal'); elseif ($status == 1) $erstat = i18n('Important'); elseif ($status == 2) $erstat = i18n('Critical'); elseif ($status == 3) $erstat = i18n('Internal error'); else exit('Error.'); // Globals $temp['tpl:lang'] = $this->lang; $temp['tpl:baseurl'] = CT_BASEURL; $temp['tpl:cssdir'] = 'inc/templates/'; $temp['tpl:logosimages'] = 'public/images/logos/'; $temp['erstat'] = $status; // Error if ($status < 3) $temp['error'] = $error; else { $temp['error'] = i18n('An error occured.').'<br />'.$error[1]. ' ('.$error[0].' - '.$errmsg.')<br />'. i18n('In: %(file)s at line %(line)d', array('file' => $error[2], 'line' => $error[3])); if ($this->show_debug) { ob_start(); print_r($error[4]); $datas = htmlspecialchars(ob_get_clean()); if (empty($datas)) $datas = i18n('No context available for this error.'); if (is_file($error[2]) && is_readable($error[2])) { $output = ''; $lines = array(); $content = explode("\n", file_get_contents($error[2])); $line = $error[3]; for ($i=($line-5); $i<($line+4); $i++) if (array_key_exists($i, $content)) { $output .= $content[$i]."\n"; array_push($lines, ($i+1)); } $hlline = array($lines, $error[3]); $code = HTMLSourceView($output, 'php', $hlline); } $dbimg = 'public/images/icons/emblems/emblem-important.png'; $debug_output = '<table style="width: 100%;"> <tr> <td style="vertical-align:top;width: 70%;"> <h4>'.i18n('Code').'</h4> <strong>'.$error[2].'</strong> <div class="errdebug">'.$code.'</div> </td> <td style="vertical-align:top;width: 30%;"> <h4>'.i18n('Memory dump').'</h4> <textarea cols="48" rows="12" readonly="readonly">'.$datas.'</textarea> </td> </tr> </table> <p style="clear: both;"> </p> <div style="background-image: url(\''.$dbimg.'\');background-repeat: '. 'no-repeat;padding-left: 20px;height: 18px;"> '.i18n('You can see a memory dump and the source code because '. '<strong>show_debug</strong> is activated in the configuration.').' </div>'; } } /* Load the template * * Load login.tpl, warning.tpl or error.tpl and fill it. */ if ($status == 0) { $html = file_get_contents(CT_BASEDIR.'/inc/templates/warning.tpl'); $temp['title'] = i18n('Something goes wrong'); $temp['header'] = i18n('Warning! Something goes wrong…'); $temp['error'] = $error; $temp['link-index'] = i18n('Go to the index'); $temp['link-precedent'] = i18n('Go back'); $temp['url'] = $_SERVER['REQUEST_URI']; $in_array = array('projects' => i18n('Projects'), 'projects-bugs' => i18n('Bugs'), 'projects-doc' => i18n('Documentation'), 'projects-forum' => i18n('Forum'), 'projects-news' => i18n('News'), 'notepad' => i18n('Notepad'), 'users' => i18n('Users')); $str = ''; foreach ($in_array as $key => $value) { $explode = explode('-', $key); if (isset($explode[1])) $value = ' '.$value; $str .= '<option value="'.$key.'">'.$value.'</option>'; } $temp['header-search'] = i18n('Search'); $temp['select-search'] = $str; $temp['button-search'] = i18n('Search'); $temp['text-search'] = i18n('If you were trying to find something'. ' on the forge, you should try to search it.'); } elseif ($status == -1) { $html = file_get_contents(CT_BASEDIR.'/inc/templates/login.tpl'); require(CT_BASEDIR.'/inc/modules/head_member/head_member.php'); $hmember = new head_member($error[1], $error[2], array('index')); $hmember->treatForms(); $temp['header-login'] = $error[0]; ob_start(); $hmember->getPageContent(); $temp['form'] = ob_get_clean(); $temp['error'] = $errstat; $temp['title'] = i18n('Access denied'); $temp['header'] = i18n('This forge is not public!'); } else { $html = file_get_contents(CT_BASEDIR.'/inc/templates/error.tpl'); $temp['title'] = i18n('An error has occured'); $temp['header'] = i18n('An error has occured.'); $temp['header-report'] = i18n('Report'); $version = file_get_contents(CT_BASEDIR.'/VERSION'); $server = str_replace('address', 'strong', $_SERVER['SERVER_SIGNATURE']); $temp['table-bugstatus-key'] = i18n('Bug status:'); $temp['table-bugstatus-value'] = $erstat; $temp['table-affects-key'] = i18n('Affects:'); $temp['table-affects-value'] = $_SERVER['SERVER_NAME']; $temp['table-version-key'] = i18n('CodingTeam version:'); $temp['table-version-value'] = $version; $temp['table-server-key'] = i18n('Server identification:'); $temp['table-server-value'] = $server; $temp['table-date-key'] = i18n('Date/time:'); $temp['table-date-value'] = date('Y-m-d H:i:s'); $temp['header-reproduce'] = i18n('What to do if you can reproduce'. ' this bug'); $temp['contact-srvadmin'] = i18n('Contact your server admin'); $temp['contact-srvadmin-more'] = i18n('It is the first thing to '. 'do when you see a bug on '. 'your forge.'); $temp['report-upstream'] = i18n('Report this bug to upstream'); $temp['report-upstream-more'] = i18n('You can send all datas on '. 'this page to the CodingTeam bug tracker at this page: %(url)s.', array('url' => '<a href="http://codingteam.net/project/codingteam/'. 'bugs">http://codingteam.net/project/codingteam/bugs</a>')). '<br />'. i18n('Be careful not to transmit private data! Also, be sure that'. ' this bug concerns only the CodingTeam development team.'); if ($status < 3) $temp['debug'] = ''; else $temp['debug'] = $debug_output; } // Parse the template foreach ($temp as $key => $value) { $search[] = '{'.$key.'}'; $replace[] = $value; } $output = str_replace($search, $replace, $html); echo $output; exit(); } } ?>

CodingTeam