]> src.bluestatic.org Git - isso.git/blob - kernel.php
Moving input stuff out of the kernel and into Input
[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 * Prepares a path for being set as the sourcepath
139 *
140 * @param string Source path or URL
141 *
142 * @return string Prepared source path
143 */
144 public function fetch_sourcepath($source)
145 {
146 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
147 {
148 $source .= DIRECTORY_SEPARATOR;
149 }
150 return $source;
151 }
152
153 // ###################################################################
154 /**
155 * Loads a framework module
156 *
157 * @param string Name of the framework file to load
158 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
159 * @param bool Globalize the internal variable?
160 *
161 * @return object Instantiated instance
162 */
163 public function &load($framework, $asobject, $globalize = false)
164 {
165 $this->check_isso_fields(null);
166
167 // set the object interlock
168 if (!method_exists($GLOBALS['isso:callback'], 'load'))
169 {
170 $GLOBALS['isso:callback'] =& $this;
171 $this->modules['isso'] =& $this;
172 }
173
174 if ($this->is_loaded($framework))
175 {
176 return $this->modules["$framework"];
177 }
178
179 if ($this->sourcepath == '')
180 {
181 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
182 }
183
184 if (file_exists($this->sourcepath . $framework . '.php'))
185 {
186 require_once($this->sourcepath . $framework . '.php');
187 }
188 else
189 {
190 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
191 }
192
193 if ($asobject === null)
194 {
195 return;
196 }
197
198 if (isset($this->$asobject))
199 {
200 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
201 }
202
203 $this->$asobject = new $framework($this);
204
205 $this->modules["$framework"] =& $this->$asobject;
206
207 if ($globalize)
208 {
209 $GLOBALS["$asobject"] =& $this->$asobject;
210 }
211
212 // allow for init_as_package to link
213 if (method_exists($this->modules["$framework"], 'init_as_package'))
214 {
215 $this->modules[ $this->modules["$framework"]->init_as_package() ] =& $this->modules["$framework"];
216 }
217
218 return $this->$asobject;
219 }
220
221 // ###################################################################
222 /**
223 * Prints a list of all currently loaded framework modules
224 *
225 * @param bool Return the data as an array?
226 *
227 * @return mixed HTML output or an array of loaded modules
228 */
229 public function show_modules($return = false)
230 {
231 $modules = array();
232 foreach ($this->modules AS $object)
233 {
234 $module = get_class($object);
235 if (method_exists($object, 'init_as_package') AND in_array($module, $modules))
236 {
237 $module = $object->init_as_package() . " - ($module)";
238 }
239
240 $modules[] = $module;
241 }
242
243 if ($return)
244 {
245 return $modules;
246 }
247 else
248 {
249 $output = "\n\n<ul>\n\t<li>";
250 $output .= implode("</li>\n\t<li>", $modules);
251 $output .= "</li>\n</ul>\n\n";
252 $this->message('Loaded Modules', $output, 1);
253 }
254 }
255
256 // ###################################################################
257 /**
258 * Verifies to see if a framework has been loaded
259 *
260 * @param string Framework name
261 *
262 * @return bool Whether or not the framework has been loaded
263 */
264 public function is_loaded($framework)
265 {
266 if (isset($this->modules["$framework"]))
267 {
268 return true;
269 }
270 else
271 {
272 return false;
273 }
274 }
275
276 // ###################################################################
277 /**
278 * Prints an ISSO message
279 *
280 * @param string The title of the message
281 * @param string The content of the message
282 * @param integer Type of message to be printed
283 * @param bool Return the output?
284 * @param bool Show the debug stack?
285 * @param integer Message width
286 *
287 * @return mixed Output or null
288 */
289 public function message($title, $message, $type, $return = false, $stack = true, $width = 500)
290 {
291 switch ($type)
292 {
293 // Message
294 case 1:
295 $prefix = 'Message';
296 $color = '#669900';
297 $font = '#000000';
298 break;
299
300 // Warning
301 case 2:
302 $prefix = 'Warning';
303 $color = '#003399';
304 $font = '#FFFFFF';
305 break;
306
307 case 3:
308 $prefix = 'Error';
309 $color = '#990000';
310 $font = '#EFEFEF';
311 break;
312 }
313
314 $backtrace = debug_backtrace();
315 array_shift($backtrace);
316
317 if (isset($backtrace[0]) AND $backtrace[0]['function'] == '_error_handler')
318 {
319 array_shift($backtrace);
320 }
321
322 $trace = $this->format_debug_trace($backtrace);
323
324 $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;\">";
325 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
326 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
327 $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>" : '');
328 $output .= "\n</table>\n<br />\n";
329
330 if ($return)
331 {
332 return $output;
333 }
334 else
335 {
336 print($output);
337 }
338 }
339
340 // ###################################################################
341 /**
342 * Prepares a debug_backtrace() array for output to the browser by
343 * compressing the array into visible text
344 *
345 * @param array debug_backtrace() array
346 *
347 * @return array Formatted trace output
348 */
349 public function format_debug_trace($backtrace)
350 {
351 $trace = array();
352 foreach ($backtrace AS $i => $step)
353 {
354 $args = '';
355 $file = $step['file'] . ':' . $step['line'];
356 $funct = (isset($step['class']) ? $step['class'] . '::' . $step['function'] : $step['function']);
357
358 if (isset($step['args']) AND is_array($step['args']))
359 {
360 // we need to do this so we don't get "Array to string conversion" notices
361 foreach ($step['args'] AS $id => $arg)
362 {
363 if (is_array($arg))
364 {
365 $step['args']["$id"] = 'Array';
366 }
367 }
368 $args = implode(', ', $step['args']);
369 }
370
371 $trace[] = "#$i $funct($args) called at [$file]";
372 }
373
374 return $trace;
375 }
376
377 // ###################################################################
378 /**
379 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
380 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
381 *
382 * @param integer Error number
383 * @param string Error message string
384 * @param string File that contains the error
385 * @param string The line number of the error
386 * @param string The active symbol table at which point the error occurred
387 */
388 public function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
389 {
390 $level = ini_get('error_reporting');
391
392 switch ($errno)
393 {
394 // Fatal
395 case E_USER_ERROR:
396 $title = 'Fatal';
397 $mode = 3;
398 if (!($level & E_USER_ERROR))
399 {
400 return;
401 }
402 break;
403
404 // Error
405 case E_USER_WARNING:
406 case E_WARNING:
407 $title = 'Warning';
408 $mode = 2;
409 if (! (($level & E_USER_WARNING) AND ($level & E_WARNING)) )
410 {
411 return;
412 }
413 break;
414
415 // Warning
416 case E_USER_NOTICE:
417 case E_NOTICE:
418 default:
419 $title = 'Notice';
420 $mode = 1;
421 if (! (($level & E_USER_NOTICE) AND ($level & E_NOTICE)) )
422 {
423 return;
424 }
425 break;
426 }
427
428 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
429
430 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
431
432 $this->message($title, $errstr, $mode);
433
434 if ($errno == E_USER_ERROR)
435 {
436 exit;
437 }
438 }
439
440 // ###################################################################
441 /**
442 * Creates a table that explains the error reporting levels and their
443 * state
444 */
445 public function explain_error_reporting()
446 {
447 $levels = array(
448 'E_ERROR' => E_ERROR,
449 'E_WARNING' => E_WARNING,
450 'E_PARSE' => E_PARSE,
451 'E_NOTICE' => E_NOTICE,
452 'E_CORE_ERROR' => E_CORE_ERROR,
453 'E_CORE_WARNING' => E_CORE_WARNING,
454 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
455 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
456 'E_USER_ERROR' => E_USER_ERROR,
457 'E_USER_WARNING' => E_USER_WARNING,
458 'E_USER_NOTICE' => E_USER_NOTICE,
459 'E_ALL' => E_ALL,
460 'E_STRICT' => E_STRICT
461 );
462
463 $table = '<table cellspacing="0" cellpadding="2" border="0">';
464
465 foreach ($levels AS $name => $value)
466 {
467 $table .= '
468 <tr>
469 <td>' . $name . '</td>
470 <td>' . (ini_get('error_reporting') & $value) . '</td>
471 </tr>';
472 }
473
474 $table .= '
475 </table>';
476
477 $this->message('Error Reporting', $table, 1);
478 }
479
480 // ###################################################################
481 /**
482 * Logs a debug message for verbose output
483 *
484 * @param string Message
485 */
486 public function debug($message)
487 {
488 $this->debuginfo[] = $message;
489 }
490
491 // ###################################################################
492 /**
493 * Constructs a debug information box that contains various debugging
494 * information points
495 *
496 * @param bool Show template information?
497 *
498 * @return string Debugging block
499 */
500 public function construct_debug_block($dotemplates)
501 {
502 $debug = '';
503
504 if ($this->debug)
505 {
506 $debug = "\n<ul>";
507
508 // templates
509 if ($dotemplates)
510 {
511 $optlist = array();
512 $usage = array();
513 foreach ($this->modules['template']->usage AS $name => $count)
514 {
515 if (in_array($name, $this->modules['template']->uncached))
516 {
517 $optlist[] = $name . '[' . $count . ']';
518 }
519 $usage[] = $name . " ($count)";
520 }
521
522 $sizeof = sizeof($this->modules['template']->uncached);
523 if ($sizeof > 0)
524 {
525 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' &nbsp; ', $optlist) . " )</li>";
526 }
527 }
528
529 // source control
530 $scinfo = 'Not Under Source Control';
531 if (defined('SVN'))
532 {
533 $scinfo = constant('SVN');
534
535 if (preg_match('#\$Id:?\s*\$#', $scinfo))
536 {
537 $scinfo = 'Not Under Source Control';
538 }
539 else
540 {
541 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
542 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
543 $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);
544 }
545 }
546
547 $scinfo = trim($scinfo);
548 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
549
550 // query information
551 if (is_object($this->modules[ISSO_DB_LAYER]))
552 {
553 $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>";
554 }
555
556 // total execution time
557 if (defined('ISSO_MT_START'))
558 {
559 $this->load('functions', 'functions');
560 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->modules['functions']->fetch_microtime_diff(ISSO_MT_START), 10) . "</li>";
561 }
562
563 // debug notices
564 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->debuginfo) . ")</option>";
565 foreach ((array)$this->debuginfo AS $msg)
566 {
567 $debug .= "\n\t\t\t<option>--- $msg</option>";
568 }
569 $debug .= "\n\t\t</select>\n\t</li>";
570
571 // loaded modules
572 $modules = $this->show_modules(true);
573 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
574 foreach ($modules AS $mod)
575 {
576 $debug .= "\n\t\t\t<option>--- $mod</option>";
577 }
578 $debug .= "\n\t\t</select>\n\t</li>";
579
580 // template usage
581 if ($dotemplates)
582 {
583 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->modules['template']->usage) . ")</option>";
584 foreach ($usage AS $tpl)
585 {
586 $debug .= "\n\t\t\t<option>--- $tpl</option>";
587 }
588 $debug .= "\n\t\t</select>\n\t</li>";
589 }
590
591 $debug .= "\n</ul>";
592
593 $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";
594 }
595
596 return $debug;
597 }
598 }
599
600 /*=====================================================================*\
601 || ###################################################################
602 || # $HeadURL$
603 || # $Id$
604 || ###################################################################
605 \*=====================================================================*/
606 ?>