3 $sysdebug_dbquery = forge_get_config('sysdebug_dbquery');
4 $sysdebug_ignored = forge_get_config('sysdebug_ignored');
8 // error handler function
9 function ffErrorHandler($errno, $errstr, $errfile, $errline)
11 global $ffErrors, $sysdebug_ignored, $sysdebug__aborted;
13 if ($sysdebug__aborted) {
14 /* inside the exception handler, ignore everything */
18 if (!$sysdebug_ignored && error_reporting() == 0)
19 /* prepended @ to statement => ignore */
22 $msg = "[$errno] $errstr ($errfile at $errline)";
24 // Display messages only once.
25 foreach ($ffErrors as $m) {
26 if ($m['message'] == $msg)
47 case E_USER_DEPRECATED:
57 if (forge_get_config('sysdebug_backtraces'))
59 '<pre style="font-weight:normal; font-size:90%; color:#000066; line-height:100%;">' .
60 htmlentities(debug_string_backtrace()) . "</pre>";
62 $ffErrors[] = array('type' => $type, 'message' => $msg);
63 /* Don't execute PHP internal error handler */
68 function ffOutputHandler($buffer) {
69 global $ffErrors, $sysdebug_enable, $sysdebug__aborted,
70 $sysdebug_lazymode_on, $sysdebug_doframe, $gfcommon,
71 $sysDTDs, $sysXMLNSs, $HTML;
73 if ($sysdebug__aborted) {
74 /* called from exception handler, discard */
75 $p = strrpos($buffer, "\r\n");
76 return (($p === false) ? "" : substr($buffer, $p + 2));
79 if (!getenv('SERVER_SOFTWARE')) {
83 /* in case we’re aborted */
84 if (!$sysdebug_enable)
87 /* if content-type != text/html* assume abortion */
88 if ($sysdebug_lazymode_on) {
89 $thdr = 'content-type:';
90 $tstr = 'content-type: text/html';
91 foreach (headers_list() as $h) {
92 if (strncasecmp($h, $thdr, strlen($thdr)))
94 if (strncasecmp($h, $tstr, strlen($tstr)))
95 /* application/something, maybe */
100 /* stop calling ffErrorHandler */
101 restore_error_handler();
103 $dtdpath = $gfcommon . 'include/';
104 // this is, sadly, necessary (especially in ff-plugin-mediawiki)
105 $pre_tag = "<pre style=\"margin:0; padding:0; border:0; line-height:125%;\">";
107 $divstring = "\n\n" . '<script language="JavaScript" type="text/javascript">//<![CDATA[
108 function toggle_ffErrors() {
109 var errorsblock = document.getElementById("ffErrorsBlock");
110 var errorsgroup = document.getElementById("ffErrors");
111 if (errorsblock.style.display == "none") {
112 errorsblock.style.display = "block";
113 errorsgroup.style.right = "10px";
115 errorsblock.style.display = "none";
116 errorsgroup.style.right = "300px";
118 }' . "\n//]]></script>\n<div id=\"ffErrors\">\n" .
119 '<a href="javascript:toggle_ffErrors();">Click to toggle</a>' .
120 "\n<div id=\"ffErrorsBlock\">";
122 $doctype = util_ifsetor($HTML->doctype);
124 $doctype = 'transitional';
127 if ($sysdebug_doframe) {
128 $initial = '<?xml version="1.0" encoding="utf-8"?>' .
129 $sysDTDs[$doctype]['doctype'] .
130 '<html xml:lang="en" ' . $sysXMLNSs .
131 "><head><title>AJAX frame</title></head><body>\n";
132 $bufferstrip = strlen($initial);
133 $buffer = $initial . $buffer . '</body></html>';
136 /* cut off </body></html> (hopefully only) at the end */
137 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
138 $bufend = array(false, substr($buffer, -100));
139 if (substr($buffer, -strlen("</html>")) != "</html>") {
141 $ffErrors[] = array('type' => "error",
142 'message' => htmlentities("does not end with </html> tag"));
143 $buffer = str_ireplace("</html>", "", $buffer);
145 $buffer = substr($buffer, 0, -strlen("</html>"));
146 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
147 if (substr($buffer, -strlen("</body>")) != "</body>") {
149 $ffErrors[] = array('type' => "error",
150 'message' => htmlentities("does not end with </body> tag"));
151 $buffer = str_ireplace("</body>", "", $buffer);
153 $buffer = substr($buffer, 0, -strlen("</body>"));
154 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
157 $ffErrors[] = array('type' => "info",
158 'message' => "The output has ended thus: " .
159 htmlentities($bufend[1]));
162 /* append errors, if any */
164 foreach ($ffErrors as $msg) {
166 $buffer .= $divstring;
169 $buffer .= "\n <div class=\"" . $msg['type'] . '">' .
170 $msg['message'] . "</div>";
173 /* generate buffer for checking */
174 $cbuf = str_ireplace('http://www.w3.org/TR/xhtml1/DTD/',
175 'file://' . $dtdpath, $buffer);
177 $cbuf .= "\n</div></div>";
178 $cbuf .= "\n</body></html>\n";
180 /* now check XHTML validity… two means */
184 if (forge_get_config('sysdebug_xmlstarlet')) {
185 /* xmlstarlet (well-formed, DTD and DOCTYPE, encoding */
187 0 => array("pipe", "r"),
188 1 => array("pipe", "w"),
189 2 => array("pipe", "w"),
191 $xmlstarlet = proc_open("xmlstarlet val -d " .
192 escapeshellarg($dtdpath . $sysDTDs[$doctype]['dtdfile']) .
193 " -e -", $dspec, $pipes);
195 if (is_resource($xmlstarlet)) {
196 fwrite($pipes[0], $cbuf);
198 $sout = stream_get_contents($pipes[1]);
199 $serr = stream_get_contents($pipes[2]);
202 $rv = proc_close($xmlstarlet);
203 /* work around Debian #627158 */
204 $serr = join("\n", preg_grep(
205 '/^-:[0-9]*: Entity'." 'nbsp' ".'not defined$/',
206 explode("\n", $serr), PREG_GREP_INVERT));
209 'msg' => "could not run xmlstarlet"
213 'msg' => "xmlstarlet found that this document is not valid (errorlevel $rv)!",
214 'extra' => $pre_tag . htmlspecialchars(trim($serr .
215 "\n\n" . $sout)) . "</pre>",
222 $sysdebug_akelos = forge_get_config('sysdebug_akelos');
223 if ($sysdebug_akelos) {
224 /* Akelos XHTML Validator (most other stuff) */
225 require_once($gfcommon . "include/XhtmlValidator.php");
226 $XhtmlValidator = new XhtmlValidator();
227 $sbuf = explode("<html", $cbuf, 2);
228 $sbuf[1] = "<html" . $sbuf[1];
230 if ($XhtmlValidator->validate($vbuf) === false) {
231 //$vbuf = $XhtmlValidator->highlightErrors($sbuf[1]);
232 $errs = '<ul><li>' . join("</li>\n<li>",
233 $XhtmlValidator->getErrors()) . '</li></ul>';
235 'msg' => "Akelos XHTML Validator found some errors on this document!",
243 /* append XHTML source code, if validation failed */
245 if (!$sysdebug_akelos || $vbuf == $sbuf[1])
246 $vbuf = "<ol><li>" . $pre_tag . join(" </pre></li>\n<li>" . $pre_tag, explode("\n", htmlentities(rtrim($cbuf)))) . " </pre></li></ol>";
248 $vbuf = $pre_tag . htmlentities(rtrim($sbuf[0])) . "</pre>" . $vbuf;
250 'msg' => "Since XHTML validation failed, here’s the checked document for you to look at:",
256 /* append error messages from the validators */
257 foreach ($valck as $msg) {
259 $buffer .= $divstring;
262 if (!isset($msg['type']) || !$msg['type']) {
263 $msg['type'] = 'unknown';
265 $buffer .= "\n <div class=\"" . $msg['type'] . '">' . $msg['msg'];
266 if (isset($msg['extra']))
267 $buffer .= "\n <div style=\"font-weight:normal; font-size:90%; color:#333333;\">" .
268 $msg['extra'] . "</div>\n ";
272 /* return final buffer */
274 $buffer .= "\n</div></div>";
275 if ($sysdebug_doframe) {
276 return substr($buffer, $bufferstrip);
278 return $buffer . "\n</body></html>\n";
282 function ffExceptionHandler($e) {
283 global $sysdebug__aborted;
285 /* drop output buffers and error handler */
286 $sysdebug__aborted = true;
287 while (ob_get_length() > 0 && ob_end_clean()) {
290 restore_error_handler();
292 /* issue exception information */
293 header('HTTP/1.0 500 Exception not handled');
294 header('Content-type: text/plain');
295 echo "\r\nUncaught exception:\n" . str_replace("\r", "",
296 $e->getMessage() . "\n\nBacktrace:\n" . $e->getTraceAsString()) .
302 if (forge_get_config('sysdebug_phphandler')) {
303 // set to the user defined error handler
304 set_error_handler("ffErrorHandler");
307 set_exception_handler("ffExceptionHandler");
309 $sysdebug_lazymode_on = false;
310 $sysdebug_doframe = false;
311 $sysdebug__aborted = false;
312 ob_start("ffOutputHandler", 0, false);
314 function sysdebug_ajaxbody($enable=true) {
315 global $sysdebug_doframe;
317 $sysdebug_doframe = $enable;
320 function sysdebug_off($hdr=false, $replace=true, $resp=false) {
321 global $sysdebug_enable;
323 if ($sysdebug_enable) {
324 $sysdebug_enable = false;
325 $buf = ob_get_flush();
327 if ($buf === false) {
334 if ($hdr !== false) {
335 if ($resp === false) {
336 header($hdr, $replace);
338 header($hdr, $replace, $resp);
345 function sysdebug_lazymode($enable) {
346 global $sysdebug_lazymode_on;
348 $sysdebug_lazymode_on = $enable ? true : false;
351 function ffDebug($type,$intro,$pretext) {
359 $text .= htmlentities($intro);
362 $text .= '<pre style="font-weight:normal; font-size:90%; color:#000066;">' .
363 htmlentities($pretext) . "</pre>";