Removing unused stuff from the kernel so we can see what still needs to be moved
[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 all active debug messages
114 * @var array
115 */
116 private $debuginfo = array();
117
118 /**
119 * List of loaded modules
120 * @var array
121 */
122 private $modules = array();
123
124 // ###################################################################
125 /**
126 * Constructor
127 */
128 public function __construct()
129 {
130 $GLOBALS['isso:callback'] = null;
131
132 // error reporting
133 set_error_handler(array(&$this, '_error_handler'));
134 }
135
136 // ###################################################################
137 /**
138 * Prints a list of all currently loaded framework modules
139 *
140 * @param bool Return the data as an array?
141 *
142 * @return mixed HTML output or an array of loaded modules
143 */
144 public function show_modules($return = false)
145 {
146 $modules = array();
147 foreach ($this->modules AS $object)
148 {
149 $module = get_class($object);
150 if (method_exists($object, 'init_as_package') AND in_array($module, $modules))
151 {
152 $module = $object->init_as_package() . " - ($module)";
153 }
154
155 $modules[] = $module;
156 }
157
158 if ($return)
159 {
160 return $modules;
161 }
162 else
163 {
164 $output = "\n\n<ul>\n\t<li>";
165 $output .= implode("</li>\n\t<li>", $modules);
166 $output .= "</li>\n</ul>\n\n";
167 $this->message('Loaded Modules', $output, 1);
168 }
169 }
170
171 // ###################################################################
172 /**
173 * Prints an ISSO message
174 *
175 * @param string The title of the message
176 * @param string The content of the message
177 * @param integer Type of message to be printed
178 * @param bool Return the output?
179 * @param bool Show the debug stack?
180 * @param integer Message width
181 *
182 * @return mixed Output or null
183 */
184 public function message($title, $message, $type, $return = false, $stack = true, $width = 500)
185 {
186 switch ($type)
187 {
188 // Message
189 case 1:
190 $prefix = 'Message';
191 $color = '#669900';
192 $font = '#000000';
193 break;
194
195 // Warning
196 case 2:
197 $prefix = 'Warning';
198 $color = '#003399';
199 $font = '#FFFFFF';
200 break;
201
202 case 3:
203 $prefix = 'Error';
204 $color = '#990000';
205 $font = '#EFEFEF';
206 break;
207 }
208
209 $backtrace = debug_backtrace();
210 array_shift($backtrace);
211
212 if (isset($backtrace[0]) AND $backtrace[0]['function'] == '_error_handler')
213 {
214 array_shift($backtrace);
215 }
216
217 $trace = $this->format_debug_trace($backtrace);
218
219 $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;\">";
220 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
221 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
222 $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>" : '');
223 $output .= "\n</table>\n<br />\n";
224
225 if ($return)
226 {
227 return $output;
228 }
229 else
230 {
231 print($output);
232 }
233 }
234
235 // ###################################################################
236 /**
237 * Prepares a debug_backtrace() array for output to the browser by
238 * compressing the array into visible text
239 *
240 * @param array debug_backtrace() array
241 *
242 * @return array Formatted trace output
243 */
244 public function format_debug_trace($backtrace)
245 {
246 $trace = array();
247 foreach ($backtrace AS $i => $step)
248 {
249 $args = '';
250 $file = $step['file'] . ':' . $step['line'];
251 $funct = (isset($step['class']) ? $step['class'] . '::' . $step['function'] : $step['function']);
252
253 if (isset($step['args']) AND is_array($step['args']))
254 {
255 // we need to do this so we don't get "Array to string conversion" notices
256 foreach ($step['args'] AS $id => $arg)
257 {
258 if (is_array($arg))
259 {
260 $step['args']["$id"] = 'Array';
261 }
262 }
263 $args = implode(', ', $step['args']);
264 }
265
266 $trace[] = "#$i $funct($args) called at [$file]";
267 }
268
269 return $trace;
270 }
271
272 // ###################################################################
273 /**
274 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
275 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
276 *
277 * @param integer Error number
278 * @param string Error message string
279 * @param string File that contains the error
280 * @param string The line number of the error
281 * @param string The active symbol table at which point the error occurred
282 */
283 public function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
284 {
285 $level = ini_get('error_reporting');
286
287 switch ($errno)
288 {
289 // Fatal
290 case E_USER_ERROR:
291 $title = 'Fatal';
292 $mode = 3;
293 if (!($level & E_USER_ERROR))
294 {
295 return;
296 }
297 break;
298
299 // Error
300 case E_USER_WARNING:
301 case E_WARNING:
302 $title = 'Warning';
303 $mode = 2;
304 if (! (($level & E_USER_WARNING) AND ($level & E_WARNING)) )
305 {
306 return;
307 }
308 break;
309
310 // Warning
311 case E_USER_NOTICE:
312 case E_NOTICE:
313 default:
314 $title = 'Notice';
315 $mode = 1;
316 if (! (($level & E_USER_NOTICE) AND ($level & E_NOTICE)) )
317 {
318 return;
319 }
320 break;
321 }
322
323 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
324
325 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
326
327 $this->message($title, $errstr, $mode);
328
329 if ($errno == E_USER_ERROR)
330 {
331 exit;
332 }
333 }
334
335 // ###################################################################
336 /**
337 * Creates a table that explains the error reporting levels and their
338 * state
339 */
340 public function explain_error_reporting()
341 {
342 $levels = array(
343 'E_ERROR' => E_ERROR,
344 'E_WARNING' => E_WARNING,
345 'E_PARSE' => E_PARSE,
346 'E_NOTICE' => E_NOTICE,
347 'E_CORE_ERROR' => E_CORE_ERROR,
348 'E_CORE_WARNING' => E_CORE_WARNING,
349 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
350 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
351 'E_USER_ERROR' => E_USER_ERROR,
352 'E_USER_WARNING' => E_USER_WARNING,
353 'E_USER_NOTICE' => E_USER_NOTICE,
354 'E_ALL' => E_ALL,
355 'E_STRICT' => E_STRICT
356 );
357
358 $table = '<table cellspacing="0" cellpadding="2" border="0">';
359
360 foreach ($levels AS $name => $value)
361 {
362 $table .= '
363 <tr>
364 <td>' . $name . '</td>
365 <td>' . (ini_get('error_reporting') & $value) . '</td>
366 </tr>';
367 }
368
369 $table .= '
370 </table>';
371
372 $this->message('Error Reporting', $table, 1);
373 }
374
375 // ###################################################################
376 /**
377 * Logs a debug message for verbose output
378 *
379 * @param string Message
380 */
381 public function debug($message)
382 {
383 $this->debuginfo[] = $message;
384 }
385
386 // ###################################################################
387 /**
388 * Constructs a debug information box that contains various debugging
389 * information points
390 *
391 * @param bool Show template information?
392 *
393 * @return string Debugging block
394 */
395 public function construct_debug_block($dotemplates)
396 {
397 $debug = '';
398
399 if ($this->debug)
400 {
401 $debug = "\n<ul>";
402
403 // templates
404 if ($dotemplates)
405 {
406 $optlist = array();
407 $usage = array();
408 foreach ($this->modules['template']->usage AS $name => $count)
409 {
410 if (in_array($name, $this->modules['template']->uncached))
411 {
412 $optlist[] = $name . '[' . $count . ']';
413 }
414 $usage[] = $name . " ($count)";
415 }
416
417 $sizeof = sizeof($this->modules['template']->uncached);
418 if ($sizeof > 0)
419 {
420 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' &nbsp; ', $optlist) . " )</li>";
421 }
422 }
423
424 // source control
425 $scinfo = 'Not Under Source Control';
426 if (defined('SVN'))
427 {
428 $scinfo = constant('SVN');
429
430 if (preg_match('#\$Id:?\s*\$#', $scinfo))
431 {
432 $scinfo = 'Not Under Source Control';
433 }
434 else
435 {
436 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
437 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
438 $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);
439 }
440 }
441
442 $scinfo = trim($scinfo);
443 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
444
445 // query information
446 if (is_object($this->modules[ISSO_DB_LAYER]))
447 {
448 $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>";
449 }
450
451 // total execution time
452 if (defined('ISSO_MT_START'))
453 {
454 $this->load('functions', 'functions');
455 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->modules['functions']->fetch_microtime_diff(ISSO_MT_START), 10) . "</li>";
456 }
457
458 // debug notices
459 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->debuginfo) . ")</option>";
460 foreach ((array)$this->debuginfo AS $msg)
461 {
462 $debug .= "\n\t\t\t<option>--- $msg</option>";
463 }
464 $debug .= "\n\t\t</select>\n\t</li>";
465
466 // loaded modules
467 $modules = $this->show_modules(true);
468 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
469 foreach ($modules AS $mod)
470 {
471 $debug .= "\n\t\t\t<option>--- $mod</option>";
472 }
473 $debug .= "\n\t\t</select>\n\t</li>";
474
475 // template usage
476 if ($dotemplates)
477 {
478 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->modules['template']->usage) . ")</option>";
479 foreach ($usage AS $tpl)
480 {
481 $debug .= "\n\t\t\t<option>--- $tpl</option>";
482 }
483 $debug .= "\n\t\t</select>\n\t</li>";
484 }
485
486 $debug .= "\n</ul>";
487
488 $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";
489 }
490
491 return $debug;
492 }
493 }
494
495 /*=====================================================================*\
496 || ###################################################################
497 || # $HeadURL$
498 || # $Id$
499 || ###################################################################
500 \*=====================================================================*/
501 ?>