Moving the debug function to Register
[isso.git] / kernel.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 /**
23 * Blue Static ISSO Framework Kernel
24 * kernel.php
25 *
26 * @package ISSO
27 */
28
29 if (!function_exists('stripos'))
30 {
31 trigger_error('You need PHP version 5.0.0 or newer to run ISSO', E_USER_ERROR);
32 exit;
33 }
34
35 if ((bool)ini_get('register_globals') === true)
36 {
37 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
38 foreach ($superglobals AS $global)
39 {
40 if (is_array(${$global}))
41 {
42 foreach (${$global} AS $_key => $_val)
43 {
44 if (isset(${$_key}))
45 {
46 unset(${$_key});
47 }
48 }
49 }
50 }
51 }
52
53 $oldlevel = ini_get('error_reporting');
54 $newlevel = $oldlevel;
55 $levels = array(E_ERROR => E_USER_ERROR, E_WARNING => E_USER_WARNING, E_NOTICE => E_USER_NOTICE);
56 foreach ($levels AS $php => $isso)
57 {
58 if ($oldlevel & $php)
59 {
60 if (!($oldlevel & $isso))
61 {
62 $newlevel += $isso;
63 }
64 }
65 else
66 {
67 if ($oldlevel & $isso)
68 {
69 $newlevel -= $isso;
70 }
71 }
72 }
73 error_reporting($newlevel);
74
75 /**
76 * Yes, required
77 */
78 define('REQ_YES', 1);
79
80 /**
81 * No, not required
82 */
83 define('REQ_NO', 0);
84
85 /**
86 * Blue Static ISSO Framework (ISSO)
87 *
88 * This framework allows a common backend to be used amongst all Blue
89 * Static applications and is built to be abstract and flexible.
90 * The base framework handles all loading and module management.
91 *
92 * Constants:
93 * ISSO_MT_START - Define the microtime() value at the top of your
94 * script and this will calculate the total execution
95 * time
96 * SVN - Place SVN keywords (like $Id) to display the information on output
97 *
98 * @author Blue Static
99 * @copyright Copyright ©2002 - [#]year[#], Blue Static
100 * @version $Revision$
101 * @package ISSO
102 *
103 */
104 class ISSO
105 {
106 /**
107 * ISSO version
108 * @var string
109 */
110 private $version = '[#]issoversion[#]';
111
112 /**
113 * List of loaded modules
114 * @var array
115 */
116 private $modules = array();
117
118 // ###################################################################
119 /**
120 * Constructor
121 */
122 public function __construct()
123 {
124 $GLOBALS['isso:callback'] = null;
125
126 // error reporting
127 set_error_handler(array(&$this, '_error_handler'));
128 }
129
130 // ###################################################################
131 /**
132 * Prints a list of all currently loaded framework modules
133 *
134 * @param bool Return the data as an array?
135 *
136 * @return mixed HTML output or an array of loaded modules
137 */
138 public function show_modules($return = false)
139 {
140 $modules = array();
141 foreach ($this->modules AS $object)
142 {
143 $module = get_class($object);
144 if (method_exists($object, 'init_as_package') AND in_array($module, $modules))
145 {
146 $module = $object->init_as_package() . " - ($module)";
147 }
148
149 $modules[] = $module;
150 }
151
152 if ($return)
153 {
154 return $modules;
155 }
156 else
157 {
158 $output = "\n\n<ul>\n\t<li>";
159 $output .= implode("</li>\n\t<li>", $modules);
160 $output .= "</li>\n</ul>\n\n";
161 $this->message('Loaded Modules', $output, 1);
162 }
163 }
164
165 // ###################################################################
166 /**
167 * Prints an ISSO message
168 *
169 * @param string The title of the message
170 * @param string The content of the message
171 * @param integer Type of message to be printed
172 * @param bool Return the output?
173 * @param bool Show the debug stack?
174 * @param integer Message width
175 *
176 * @return mixed Output or null
177 */
178 public function message($title, $message, $type, $return = false, $stack = true, $width = 500)
179 {
180 switch ($type)
181 {
182 // Message
183 case 1:
184 $prefix = 'Message';
185 $color = '#669900';
186 $font = '#000000';
187 break;
188
189 // Warning
190 case 2:
191 $prefix = 'Warning';
192 $color = '#003399';
193 $font = '#FFFFFF';
194 break;
195
196 case 3:
197 $prefix = 'Error';
198 $color = '#990000';
199 $font = '#EFEFEF';
200 break;
201 }
202
203 $backtrace = debug_backtrace();
204 array_shift($backtrace);
205
206 if (isset($backtrace[0]) AND $backtrace[0]['function'] == '_error_handler')
207 {
208 array_shift($backtrace);
209 }
210
211 $trace = $this->format_debug_trace($backtrace);
212
213 $output = "\n<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"$width\" style=\"background-color: $color; color: black; font-family: Verdana, sans-serif; font-size: 12px;\">";
214 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
215 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
216 $output .= (($stack AND $GLOBALS['isso:callback']->debug) ? "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>" . implode("\n", $trace) . "</pre></td>\n</tr>" : '');
217 $output .= "\n</table>\n<br />\n";
218
219 if ($return)
220 {
221 return $output;
222 }
223 else
224 {
225 print($output);
226 }
227 }
228
229 // ###################################################################
230 /**
231 * Prepares a debug_backtrace() array for output to the browser by
232 * compressing the array into visible text
233 *
234 * @param array debug_backtrace() array
235 *
236 * @return array Formatted trace output
237 */
238 public function format_debug_trace($backtrace)
239 {
240 $trace = array();
241 foreach ($backtrace AS $i => $step)
242 {
243 $args = '';
244 $file = $step['file'] . ':' . $step['line'];
245 $funct = (isset($step['class']) ? $step['class'] . '::' . $step['function'] : $step['function']);
246
247 if (isset($step['args']) AND is_array($step['args']))
248 {
249 // we need to do this so we don't get "Array to string conversion" notices
250 foreach ($step['args'] AS $id => $arg)
251 {
252 if (is_array($arg))
253 {
254 $step['args']["$id"] = 'Array';
255 }
256 }
257 $args = implode(', ', $step['args']);
258 }
259
260 $trace[] = "#$i $funct($args) called at [$file]";
261 }
262
263 return $trace;
264 }
265
266 // ###################################################################
267 /**
268 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
269 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
270 *
271 * @param integer Error number
272 * @param string Error message string
273 * @param string File that contains the error
274 * @param string The line number of the error
275 * @param string The active symbol table at which point the error occurred
276 */
277 public function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
278 {
279 $level = ini_get('error_reporting');
280
281 switch ($errno)
282 {
283 // Fatal
284 case E_USER_ERROR:
285 $title = 'Fatal';
286 $mode = 3;
287 if (!($level & E_USER_ERROR))
288 {
289 return;
290 }
291 break;
292
293 // Error
294 case E_USER_WARNING:
295 case E_WARNING:
296 $title = 'Warning';
297 $mode = 2;
298 if (! (($level & E_USER_WARNING) AND ($level & E_WARNING)) )
299 {
300 return;
301 }
302 break;
303
304 // Warning
305 case E_USER_NOTICE:
306 case E_NOTICE:
307 default:
308 $title = 'Notice';
309 $mode = 1;
310 if (! (($level & E_USER_NOTICE) AND ($level & E_NOTICE)) )
311 {
312 return;
313 }
314 break;
315 }
316
317 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
318
319 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
320
321 $this->message($title, $errstr, $mode);
322
323 if ($errno == E_USER_ERROR)
324 {
325 exit;
326 }
327 }
328
329 // ###################################################################
330 /**
331 * Creates a table that explains the error reporting levels and their
332 * state
333 */
334 public function explain_error_reporting()
335 {
336 $levels = array(
337 'E_ERROR' => E_ERROR,
338 'E_WARNING' => E_WARNING,
339 'E_PARSE' => E_PARSE,
340 'E_NOTICE' => E_NOTICE,
341 'E_CORE_ERROR' => E_CORE_ERROR,
342 'E_CORE_WARNING' => E_CORE_WARNING,
343 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
344 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
345 'E_USER_ERROR' => E_USER_ERROR,
346 'E_USER_WARNING' => E_USER_WARNING,
347 'E_USER_NOTICE' => E_USER_NOTICE,
348 'E_ALL' => E_ALL,
349 'E_STRICT' => E_STRICT
350 );
351
352 $table = '<table cellspacing="0" cellpadding="2" border="0">';
353
354 foreach ($levels AS $name => $value)
355 {
356 $table .= '
357 <tr>
358 <td>' . $name . '</td>
359 <td>' . (ini_get('error_reporting') & $value) . '</td>
360 </tr>';
361 }
362
363 $table .= '
364 </table>';
365
366 $this->message('Error Reporting', $table, 1);
367 }
368
369 // ###################################################################
370 /**
371 * Constructs a debug information box that contains various debugging
372 * information points
373 *
374 * @param bool Show template information?
375 *
376 * @return string Debugging block
377 */
378 public function construct_debug_block($dotemplates)
379 {
380 $debug = '';
381
382 if ($this->debug)
383 {
384 $debug = "\n<ul>";
385
386 // templates
387 if ($dotemplates)
388 {
389 $optlist = array();
390 $usage = array();
391 foreach ($this->modules['template']->usage AS $name => $count)
392 {
393 if (in_array($name, $this->modules['template']->uncached))
394 {
395 $optlist[] = $name . '[' . $count . ']';
396 }
397 $usage[] = $name . " ($count)";
398 }
399
400 $sizeof = sizeof($this->modules['template']->uncached);
401 if ($sizeof > 0)
402 {
403 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' &nbsp; ', $optlist) . " )</li>";
404 }
405 }
406
407 // source control
408 $scinfo = 'Not Under Source Control';
409 if (defined('SVN'))
410 {
411 $scinfo = constant('SVN');
412
413 if (preg_match('#\$Id:?\s*\$#', $scinfo))
414 {
415 $scinfo = 'Not Under Source Control';
416 }
417 else
418 {
419 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
420 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
421 $scinfo = preg_replace('#\$' . 'Id: (.+?) ([0-9].+?) [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}(.+?) (.+?) \$#', '\\1 - SVN \\2', $scinfo);
422 }
423 }
424
425 $scinfo = trim($scinfo);
426 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
427
428 // query information
429 if (is_object($this->modules[ISSO_DB_LAYER]))
430 {
431 $debug .= "\n\t<li><strong>Total Queries:</strong> " . sizeof($this->modules[ISSO_DB_LAYER]->history) . " (<a href=\"" . $this->sanitize($_SERVER['REQUEST_URI']) . ((strpos($_SERVER['REQUEST_URI'], '?') !== false) ? '&amp;query=1' : '?query=1') . "\">?</a>)</li>";
432 }
433
434 // total execution time
435 if (defined('ISSO_MT_START'))
436 {
437 $this->load('functions', 'functions');
438 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->modules['functions']->fetch_microtime_diff(ISSO_MT_START), 10) . "</li>";
439 }
440
441 // debug notices
442 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->debuginfo) . ")</option>";
443 foreach ((array)$this->debuginfo AS $msg)
444 {
445 $debug .= "\n\t\t\t<option>--- $msg</option>";
446 }
447 $debug .= "\n\t\t</select>\n\t</li>";
448
449 // loaded modules
450 $modules = $this->show_modules(true);
451 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
452 foreach ($modules AS $mod)
453 {
454 $debug .= "\n\t\t\t<option>--- $mod</option>";
455 }
456 $debug .= "\n\t\t</select>\n\t</li>";
457
458 // template usage
459 if ($dotemplates)
460 {
461 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->modules['template']->usage) . ")</option>";
462 foreach ($usage AS $tpl)
463 {
464 $debug .= "\n\t\t\t<option>--- $tpl</option>";
465 }
466 $debug .= "\n\t\t</select>\n\t</li>";
467 }
468
469 $debug .= "\n</ul>";
470
471 $debug = "\n\n<!-- dev debug -->\n<div align=\"center\">\n\n<hr />\n" . $this->message('Debug Information', $debug, 1, true, false) . "\n</div>\n<!-- / dev debug -->\n\n";
472 }
473
474 return $debug;
475 }
476 }
477
478 /*=====================================================================*\
479 || ###################################################################
480 || # $HeadURL$
481 || # $Id$
482 || ###################################################################
483 \*=====================================================================*/
484 ?>