]>
src.bluestatic.org Git - isso.git/blob - kernel.php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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.
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
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 \*=====================================================================*/
23 * Blue Static ISSO Framework Kernel
29 if (!function_exists('stripos'))
31 trigger_error('You need PHP version 5.0.0 or newer to run ISSO', E_USER_ERROR
);
35 if ((bool)ini_get('register_globals') === true)
37 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
38 foreach ($superglobals AS $global)
40 if (is_array(${$global}))
42 foreach (${$global} AS $_key => $_val)
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)
60 if (!($oldlevel & $isso))
67 if ($oldlevel & $isso)
73 error_reporting($newlevel);
86 * Blue Static ISSO Framework (ISSO)
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.
93 * ISSO_MT_START - Define the microtime() value at the top of your
94 * script and this will calculate the total execution
96 * SVN - Place SVN keywords (like $Id) to display the information on output
99 * @copyright Copyright ©2002 - [#]year[#], Blue Static
100 * @version $Revision$
110 private $version = '[#]issoversion[#]';
113 * List of all active debug messages
116 private $debuginfo = array();
119 * List of loaded modules
122 private $modules = array();
124 // ###################################################################
128 public function __construct()
130 $GLOBALS['isso:callback'] = null;
133 set_error_handler(array(&$this, '_error_handler'));
136 // ###################################################################
138 * Prepares a path for being set as the sourcepath
140 * @param string Source path or URL
142 * @return string Prepared source path
144 public function fetch_sourcepath($source)
146 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
148 $source .= DIRECTORY_SEPARATOR
;
153 // ###################################################################
155 * Loads a framework module
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?
161 * @return object Instantiated instance
163 public function &load($framework, $asobject, $globalize = false)
165 $this->check_isso_fields(null);
167 // set the object interlock
168 if (!method_exists($GLOBALS['isso:callback'], 'load'))
170 $GLOBALS['isso:callback'] =& $this;
171 $this->modules
['isso'] =& $this;
174 if ($this->is_loaded($framework))
176 return $this->modules
["$framework"];
179 if ($this->sourcepath == '')
181 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
184 if (file_exists($this->sourcepath . $framework . '.php'))
186 require_once($this->sourcepath . $framework . '.php');
190 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
193 if ($asobject === null)
198 if (isset($this->$asobject))
200 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
203 $this->$asobject = new $framework($this);
205 $this->modules["$framework"] =& $this->$asobject;
209 $GLOBALS["$asobject"] =& $this->$asobject;
212 // allow for init_as_package to link
213 if (method_exists($this->modules["$framework"], 'init_as_package'))
215 $this->modules
[ $this->modules
["$framework"]->init_as_package() ] =& $this->modules["$framework"];
218 return $this->$asobject;
221 // ###################################################################
223 * Prints a list of all currently loaded framework modules
225 * @param bool Return the data as an array?
227 * @return mixed HTML output or an array of loaded modules
229 public function show_modules($return = false)
232 foreach ($this->modules
AS $object)
234 $module = get_class($object);
235 if (method_exists($object, 'init_as_package') AND in_array($module, $modules))
237 $module = $object->init_as_package() . " - ($module)";
240 $modules[] = $module;
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);
256 // ###################################################################
258 * Verifies to see if a framework has been loaded
260 * @param string Framework name
262 * @return bool Whether or not the framework has been loaded
264 public function is_loaded($framework)
266 if (isset($this->modules
["$framework"]))
276 // ###################################################################
278 * Prints an ISSO message
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
287 * @return mixed Output or null
289 public function message($title, $message, $type, $return = false, $stack = true, $width = 500)
314 $backtrace = debug_backtrace();
315 array_shift($backtrace);
317 if (isset($backtrace[0]) AND $backtrace[0]['function'] == '_error_handler')
319 array_shift($backtrace);
322 $trace = $this->format_debug_trace($backtrace);
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";
340 // ###################################################################
342 * Prepares a debug_backtrace() array for output to the browser by
343 * compressing the array into visible text
345 * @param array debug_backtrace() array
347 * @return array Formatted trace output
349 public function format_debug_trace($backtrace)
352 foreach ($backtrace AS $i => $step)
355 $file = $step['file'] . ':' . $step['line'];
356 $funct = (isset($step['class']) ? $step['class'] . '::' . $step['function'] : $step['function']);
358 if (isset($step['args']) AND is_array($step['args']))
360 // we need to do this so we don't get "Array to string conversion" notices
361 foreach ($step['args'] AS $id => $arg)
365 $step['args']["$id"] = 'Array';
368 $args = implode(', ', $step['args']);
371 $trace[] = "#$i $funct($args) called at [$file]";
377 // ###################################################################
379 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
380 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
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
388 public function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
390 $level = ini_get('error_reporting');
398 if (!($level & E_USER_ERROR
))
409 if (! (($level & E_USER_WARNING
) AND ($level & E_WARNING
)) )
421 if (! (($level & E_USER_NOTICE
) AND ($level & E_NOTICE
)) )
428 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
430 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
432 $this->message($title, $errstr, $mode);
434 if ($errno == E_USER_ERROR
)
440 // ###################################################################
442 * Creates a table that explains the error reporting levels and their
445 public function explain_error_reporting()
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
,
460 'E_STRICT' => E_STRICT
463 $table = '<table cellspacing="0" cellpadding="2" border="0">';
465 foreach ($levels AS $name => $value)
469 <td>' . $name . '</td>
470 <td>' . (ini_get('error_reporting') & $value) . '</td>
477 $this->message('Error Reporting', $table, 1);
480 // ###################################################################
482 * Logs a debug message for verbose output
484 * @param string Message
486 public function debug($message)
488 $this->debuginfo
[] = $message;
491 // ###################################################################
493 * Constructs a debug information box that contains various debugging
496 * @param bool Show template information?
498 * @return string Debugging block
500 public function construct_debug_block($dotemplates)
513 foreach ($this->modules
['template']->usage
AS $name => $count)
515 if (in_array($name, $this->modules
['template']->uncached
))
517 $optlist[] = $name . '[' . $count . ']';
519 $usage[] = $name . " ($count)";
522 $sizeof = sizeof($this->modules
['template']->uncached
);
525 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' ', $optlist) . " )</li>";
530 $scinfo = 'Not Under Source Control';
533 $scinfo = constant('SVN');
535 if (preg_match('#\$Id:?\s*\$#', $scinfo))
537 $scinfo = 'Not Under Source Control';
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);
547 $scinfo = trim($scinfo);
548 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
551 if (is_object($this->modules
[ISSO_DB_LAYER
]))
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) ? '&query=1' : '?query=1') . "\">?</a>)</li>";
556 // total execution time
557 if (defined('ISSO_MT_START'))
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>";
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)
567 $debug .= "\n\t\t\t<option>--- $msg</option>";
569 $debug .= "\n\t\t</select>\n\t</li>";
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)
576 $debug .= "\n\t\t\t<option>--- $mod</option>";
578 $debug .= "\n\t\t</select>\n\t</li>";
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)
586 $debug .= "\n\t\t\t<option>--- $tpl</option>";
588 $debug .= "\n\t\t</select>\n\t</li>";
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";
600 /*=====================================================================*\
601 || ###################################################################
604 || ###################################################################
605 \*=====================================================================*/