3 * FusionForge extradebug feature
5 * Copyright 2011, Fusionforge Team
6 * http://fusionforge.org
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 // initialise globals used by the debugging code
25 $sysdebug_dberrors = forge_get_config('sysdebug_dberrors');
26 $sysdebug_dbquery = forge_get_config('sysdebug_dbquery');
27 $sysdebug_ignored = forge_get_config('sysdebug_ignored');
28 if (!isset($ffErrors)) {
32 // error handler function
33 function ffErrorHandler($errno, $errstr, $errfile, $errline) {
34 global $ffErrors, $sysdebug_ignored, $sysdebug__aborted;
36 if ($sysdebug__aborted) {
37 /* inside the exception handler, ignore everything */
41 if (!$sysdebug_ignored && error_reporting() == 0) {
42 /* prepended @ to statement => ignore */
46 $msg = "[$errno] $errstr ($errfile at $errline)";
48 // display messages only once
49 foreach ($ffErrors as $m) {
50 if ($m['message'] == $msg) {
72 case E_USER_DEPRECATED:
82 if (forge_get_config('sysdebug_backtraces')) {
84 '<pre style="font-weight:normal; font-size:90%; color:#000066; line-height:100%;">' .
85 htmlentities(debug_string_backtrace()) . "</pre>";
92 /* Don't execute PHP internal error handler */
96 // output buffer finaliser function
97 function ffOutputHandler($buffer) {
98 global $ffErrors, $sysdebug_enable, $sysdebug__aborted,
99 $sysdebug_lazymode_on, $sysdebug_doframe, $gfcommon,
100 $sysDTDs, $sysXMLNSs, $HTML;
102 if ($sysdebug__aborted) {
103 /* called from exception handler, discard */
104 $p = strrpos($buffer, "\r\n");
105 return (($p === false) ? "" : substr($buffer, $p + 2));
108 if (!getenv('SERVER_SOFTWARE')) {
112 /* in case we’re aborted */
113 if (!$sysdebug_enable) {
117 /* if content-type != text/html* assume abortion */
118 if ($sysdebug_lazymode_on) {
119 $thdr = 'content-type:';
120 $tstr = 'content-type: text/html';
121 foreach (headers_list() as $h) {
122 if (strncasecmp($h, $thdr, strlen($thdr))) {
125 if (strncasecmp($h, $tstr, strlen($tstr))) {
126 /* application/something, maybe */
132 /* stop calling ffErrorHandler */
133 restore_error_handler();
135 $dtdpath = $gfcommon . 'include/';
136 // this is, sadly, necessary (especially in ff-plugin-mediawiki)
137 $pre_tag = "<pre style=\"margin:0; padding:0; border:0; line-height:125%;\">";
139 $divstring = "\n\n" . '<script language="JavaScript" type="text/javascript">//<![CDATA[
140 function toggle_ffErrors() {
141 var errorsblock = document.getElementById("ffErrorsBlock");
142 var errorsgroup = document.getElementById("ffErrors");
143 if (errorsblock.style.display == "none") {
144 errorsblock.style.display = "block";
145 errorsgroup.style.right = "10px";
147 errorsblock.style.display = "none";
148 errorsgroup.style.right = "300px";
150 }' . "\n//]]></script>\n<div id=\"ffErrors\">\n" .
151 '<a href="javascript:toggle_ffErrors();">Click to toggle</a>' .
152 "\n<div id=\"ffErrorsBlock\">";
154 if (!($doctype = util_ifsetor($HTML->doctype))) {
155 $doctype = 'transitional';
158 if ($sysdebug_doframe) {
159 $initial = '<?xml version="1.0" encoding="utf-8"?>' .
160 $sysDTDs[$doctype]['doctype'] .
161 '<html xml:lang="en" ' . $sysXMLNSs .
162 "><head><title>AJAX frame</title></head><body>\n";
163 $bufferstrip = strlen($initial);
164 $buffer = $initial . $buffer . '</body></html>';
167 /* cut off </body></html> (hopefully only) at the end */
168 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
169 $bufend = array(false, substr($buffer, -100));
170 if (substr($buffer, -strlen("</html>")) != "</html>") {
174 'message' => htmlentities("does not end with </html> tag"),
176 $buffer = str_ireplace("</html>", "", $buffer);
178 $buffer = substr($buffer, 0, -strlen("</html>"));
180 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
181 if (substr($buffer, -strlen("</body>")) != "</body>") {
185 'message' => htmlentities("does not end with </body> tag"),
187 $buffer = str_ireplace("</body>", "", $buffer);
189 $buffer = substr($buffer, 0, -strlen("</body>"));
191 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
196 'message' => "The output has ended thus: " .
197 htmlentities($bufend[1]),
201 /* append errors, if any */
203 foreach ($ffErrors as $msg) {
205 $buffer .= $divstring;
208 $buffer .= "\n <div class=\"" . $msg['type'] . '">' .
209 $msg['message'] . "</div>";
212 /* generate buffer for checking */
213 $cbuf = str_ireplace('http://www.w3.org/TR/xhtml1/DTD/',
214 'file://' . $dtdpath, str_ireplace('http://evolvis.org/DTD/',
215 'file://' . $dtdpath, $buffer));
217 $cbuf .= "\n</div></div>";
219 $cbuf .= "\n</body></html>\n";
221 /* now check XHTML validity… two means */
225 if (forge_get_config('sysdebug_xmlstarlet')) {
226 /* xmlstarlet (well-formed, DTD and DOCTYPE, encoding */
228 0 => array("pipe", "r"),
229 1 => array("pipe", "w"),
230 2 => array("pipe", "w"),
232 $xmlstarlet = proc_open("xmlstarlet val -d " .
233 escapeshellarg($dtdpath . $sysDTDs[$doctype]['dtdfile']) .
234 " -e -", $dspec, $pipes);
236 if (is_resource($xmlstarlet)) {
237 fwrite($pipes[0], $cbuf);
239 $sout = stream_get_contents($pipes[1]);
240 $serr = stream_get_contents($pipes[2]);
243 $rv = proc_close($xmlstarlet);
244 /* work around Debian #627158 */
245 $serr = join("\n", preg_grep(
246 '/^-:[0-9]*: Entity'." 'nbsp' ".'not defined$/',
247 explode("\n", $serr), PREG_GREP_INVERT));
250 'msg' => "could not run xmlstarlet",
255 'msg' => "xmlstarlet found that this document is not valid (errorlevel $rv)!",
256 'extra' => $pre_tag .
257 htmlspecialchars(trim($serr .
258 "\n\n" . $sout)) . "</pre>",
265 /* append XHTML source code, if validation failed */
267 $vbuf = "<ol><li>" . $pre_tag .
268 join(" </pre></li>\n<li>" . $pre_tag,
269 explode("\n", htmlentities(rtrim($cbuf)))) .
272 'msg' => "Since XHTML validation failed, here’s the checked document for you to look at:",
278 /* append error messages from the validators */
279 foreach ($valck as $msg) {
281 $buffer .= $divstring;
284 if (!isset($msg['type']) || !$msg['type']) {
285 $msg['type'] = 'unknown';
287 $buffer .= "\n <div class=\"" . $msg['type'] . '">' .
289 if (isset($msg['extra'])) {
290 $buffer .= "\n <div style=\"font-weight:normal; font-size:90%; color:#333333;\">" .
291 $msg['extra'] . "</div>\n ";
296 /* return final buffer */
298 $buffer .= "\n</div></div>";
300 if ($sysdebug_doframe) {
301 return substr($buffer, $bufferstrip);
303 return $buffer . "\n</body></html>\n";
307 // exception handler function
308 function ffExceptionHandler($e) {
309 global $sysdebug__aborted;
311 /* drop output buffers and error handler */
312 $sysdebug__aborted = true;
313 while (ob_get_length() > 0 && ob_end_clean()) {
316 restore_error_handler();
318 /* issue exception information */
319 header('HTTP/1.0 500 Exception not handled');
320 header('Content-type: text/plain');
321 echo "\r\nUncaught exception:\n" . str_replace("\r", "",
322 $e->getMessage() . "\n\nBacktrace:\n" . $e->getTraceAsString()) .
328 if (forge_get_config('sysdebug_phphandler')) {
329 // set to the user defined error handler
330 set_error_handler("ffErrorHandler");
333 set_exception_handler("ffExceptionHandler");
335 $sysdebug_lazymode_on = false;
336 $sysdebug_doframe = false;
337 $sysdebug__aborted = false;
338 ob_start("ffOutputHandler", 0, false);
340 function sysdebug_ajaxbody($enable=true) {
341 global $sysdebug_doframe;
343 $sysdebug_doframe = $enable;
346 function sysdebug_off($hdr=false, $replace=true, $resp=false) {
347 global $ffErrors, $sysdebug_enable;
349 if ($sysdebug_enable) {
350 $sysdebug_enable = false;
351 $buf = @ob_get_flush();
353 if ($buf === false) {
357 /* if we had any old errors, log them */
359 foreach ($ffErrors as $msg) {
360 $olderrors .= "\n(" . $msg['type'] . ") " .
364 if (!forge_get_config('sysdebug_backtraces')) {
365 $olderrors .= "\n" . debug_string_backtrace();
367 $olderrors = rtrim($olderrors);
369 foreach (explode("\n",
370 "sysdebug_off: previous errors found:" . $olderrors)
372 error_log($pfx . $olderrorline);
373 /* followup lines get indented */
381 if ($hdr !== false) {
382 if ($resp === false) {
383 header($hdr, $replace);
385 header($hdr, $replace, $resp);
392 function sysdebug_lazymode($enable) {
393 global $sysdebug_lazymode_on;
395 $sysdebug_lazymode_on = $enable ? true : false;
398 function ffDebug($type, $intro, $pretext=false) {
406 $text .= htmlentities($intro);
409 $text .= '<pre style="font-weight:normal; font-size:90%; color:#000066;">' .
410 htmlentities($pretext) . "</pre>";