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;
13 if (!$sysdebug_ignored && error_reporting() == 0)
14 /* prepended @ to statement => ignore */
17 $msg = "[$errno] $errstr ($errfile at $errline)";
19 // Display messages only once.
20 foreach ($ffErrors as $m) {
21 if ($m['message'] == $msg)
42 case E_USER_DEPRECATED:
52 if (forge_get_config('sysdebug_backtraces'))
54 '<pre style="font-weight:normal; font-size:90%; color:#000066; line-height:100%;">' .
55 htmlentities(debug_string_backtrace()) . "</pre>";
57 $ffErrors[] = array('type' => $type, 'message' => $msg);
58 /* Don't execute PHP internal error handler */
63 function ffOutputHandler($buffer) {
64 global $ffErrors, $sysdebug_enable, $sysdebug_lazymode_on,
65 $sysdebug_doframe, $gfcommon, $sysDTDs, $sysXMLNSs, $HTML;
67 if (! getenv ('SERVER_SOFTWARE')) {
71 /* in case we’re aborted */
72 if (!$sysdebug_enable)
75 /* if content-type != text/html* assume abortion */
76 if ($sysdebug_lazymode_on) {
77 $thdr = 'content-type:';
78 $tstr = 'content-type: text/html';
79 foreach (headers_list() as $h) {
80 if (strncasecmp($h, $thdr, strlen($thdr)))
82 if (strncasecmp($h, $tstr, strlen($tstr)))
83 /* application/something, maybe */
88 /* stop calling ffErrorHandler */
89 restore_error_handler();
91 $dtdpath = $gfcommon . 'include/';
92 // this is, sadly, necessary (especially in ff-plugin-mediawiki)
93 $pre_tag = "<pre style=\"margin:0; padding:0; border:0; line-height:125%;\">";
95 $divstring = "\n\n" . '<script language="JavaScript" type="text/javascript">/* <![CDATA[ */
96 function toggle_ffErrors() {
97 var errorsblock = document.getElementById("ffErrorsBlock");
98 var errorsgroup = document.getElementById("ffErrors");
99 if (errorsblock.style.display == "none") {
100 errorsblock.style.display = "block";
101 errorsgroup.style.right = "10px";
103 errorsblock.style.display = "none";
104 errorsgroup.style.right = "300px";
106 }' . "\n/* ]]> */</script>\n<div id=\"ffErrors\">\n" .
107 '<a href="javascript:toggle_ffErrors();">Click to toggle</a>' .
108 "\n<div id=\"ffErrorsBlock\">";
110 $doctype = util_ifsetor($HTML->doctype);
112 $doctype = 'transitional';
115 if ($sysdebug_doframe) {
116 $initial = '<?xml version="1.0" encoding="utf-8"?>' .
117 $sysDTDs[$doctype]['doctype'] .
118 '<html xml:lang="en" ' . $sysXMLNSs .
119 "><head><title>AJAX frame</title></head><body>\n";
120 $bufferstrip = strlen($initial);
121 $buffer = $initial . $buffer . '</body></html>';
124 /* cut off </body></html> (hopefully only) at the end */
125 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
126 $bufend = array(false, substr($buffer, -100));
127 if (substr($buffer, -strlen("</html>")) != "</html>") {
129 $ffErrors[] = array('type' => "error",
130 'message' => htmlentities("does not end with </html> tag"));
131 $buffer = str_ireplace("</html>", "", $buffer);
133 $buffer = substr($buffer, 0, -strlen("</html>"));
134 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
135 if (substr($buffer, -strlen("</body>")) != "</body>") {
137 $ffErrors[] = array('type' => "error",
138 'message' => htmlentities("does not end with </body> tag"));
139 $buffer = str_ireplace("</body>", "", $buffer);
141 $buffer = substr($buffer, 0, -strlen("</body>"));
142 $buffer = rtrim($buffer); /* spaces, newlines, etc. */
145 $ffErrors[] = array('type' => "info",
146 'message' => "The output has ended thus: " .
147 htmlentities($bufend[1]));
150 /* append errors, if any */
152 foreach ($ffErrors as $msg) {
154 $buffer .= $divstring;
157 $buffer .= "\n <div class=\"" . $msg['type'] . '">' .
158 $msg['message'] . "</div>";
161 /* generate buffer for checking */
162 $cbuf = str_ireplace('http://www.w3.org/TR/xhtml1/DTD/',
163 'file://' . $dtdpath, $buffer);
165 $cbuf .= "\n</div></div>";
166 $cbuf .= "\n</body></html>\n";
168 /* now check XHTML validity… two means */
172 if (forge_get_config('sysdebug_xmlstarlet')) {
173 /* xmlstarlet (well-formed, DTD and DOCTYPE, encoding */
175 0 => array("pipe", "r"),
176 1 => array("pipe", "w"),
177 2 => array("pipe", "w"),
179 $xmlstarlet = proc_open("xmlstarlet val -d " .
180 escapeshellarg($dtdpath . $sysDTDs[$doctype]['dtdfile']) .
181 " -e -", $dspec, $pipes);
183 if (is_resource($xmlstarlet)) {
184 fwrite($pipes[0], $cbuf);
186 $sout = stream_get_contents($pipes[1]);
187 $serr = stream_get_contents($pipes[2]);
190 $rv = proc_close($xmlstarlet);
191 /* work around Debian #627158 */
192 $serr = join("\n", preg_grep(
193 '/^-:[0-9]*: Entity'." 'nbsp' ".'not defined$/',
194 explode("\n", $serr), PREG_GREP_INVERT));
197 'msg' => "could not run xmlstarlet"
201 'msg' => "xmlstarlet found that this document is not valid (errorlevel $rv)!",
202 'extra' => $pre_tag . htmlspecialchars(trim($serr .
203 "\n\n" . $sout)) . "</pre>",
210 $sysdebug_akelos = forge_get_config('sysdebug_akelos');
211 if ($sysdebug_akelos) {
212 /* Akelos XHTML Validator (most other stuff) */
213 require_once($gfcommon . "include/XhtmlValidator.php");
214 $XhtmlValidator = new XhtmlValidator();
215 $sbuf = explode("<html", $cbuf, 2);
216 $sbuf[1] = "<html" . $sbuf[1];
218 if ($XhtmlValidator->validate($vbuf) === false) {
219 //$vbuf = $XhtmlValidator->highlightErrors($sbuf[1]);
220 $errs = '<ul><li>' . join("</li>\n<li>",
221 $XhtmlValidator->getErrors()) . '</li></ul>';
223 'msg' => "Akelos XHTML Validator found some errors on this document!",
231 /* append XHTML source code, if validation failed */
233 if (!$sysdebug_akelos || $vbuf == $sbuf[1])
234 $vbuf = "<ol><li>" . $pre_tag . join(" </pre></li>\n<li>" . $pre_tag, explode("\n", htmlentities(rtrim($cbuf)))) . " </pre></li></ol>";
236 $vbuf = $pre_tag . htmlentities(rtrim($sbuf[0])) . "</pre>" . $vbuf;
238 'msg' => "Since XHTML validation failed, here’s the checked document for you to look at:",
244 /* append error messages from the validators */
245 foreach ($valck as $msg) {
247 $buffer .= $divstring;
250 $buffer .= "\n <div class=\"" . $msg['type'] . '">' . $msg['msg'];
251 if (isset($msg['extra']))
252 $buffer .= "\n <div style=\"font-weight:normal; font-size:90%; color:#333333;\">" .
253 $msg['extra'] . "</div>\n ";
257 /* return final buffer */
259 $buffer .= "\n</div></div>";
260 if ($sysdebug_doframe) {
261 return substr($buffer, $bufferstrip);
263 return $buffer . "\n</body></html>\n";
267 if (forge_get_config('sysdebug_phphandler')) {
268 // set to the user defined error handler
269 set_error_handler("ffErrorHandler");
272 $sysdebug_lazymode_on = false;
273 $sysdebug_doframe = false;
274 ob_start("ffOutputHandler", 0, false);
276 function sysdebug_ajaxbody($enable=true) {
277 global $sysdebug_doframe;
279 $sysdebug_doframe = $enable;
282 function sysdebug_off($hdr=false, $replace=true, $resp=false) {
283 global $sysdebug_enable;
285 if ($sysdebug_enable) {
286 $sysdebug_enable = false;
287 $buf = ob_get_flush();
289 if ($buf === false) {
296 if ($hdr !== false) {
297 if ($resp === false) {
298 header($hdr, $replace);
300 header($hdr, $replace, $resp);
307 function sysdebug_lazymode($enable) {
308 global $sysdebug_lazymode_on;
310 $sysdebug_lazymode_on = $enable ? true : false;
313 function ffDebug($type,$intro,$pretext) {
321 $text .= htmlentities($intro);
324 $text .= '<pre style="font-weight:normal; font-size:90%; color:#000066;">' .
325 htmlentities($pretext) . "</pre>";