Deleting old functions.php; moving Functions2.php to Functions.php
[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 * Input cleaning type constant
77 */
78 /**
79 * Integer type
80 */
81 define('TYPE_INT', 1);
82
83 /**
84 * Unsigned integer
85 */
86 define('TYPE_UINT', 2);
87
88 /**
89 * Float type
90 */
91 define('TYPE_FLOAT', 4);
92
93 /**
94 * Boolean type
95 */
96 define('TYPE_BOOL', 8);
97
98 /**
99 * String - cleaned
100 */
101 define('TYPE_STR', 16);
102
103 /**
104 * String - deliberate unclean
105 */
106 define('TYPE_STRUN', 32);
107
108 /**
109 * No cleaning - here for use in API
110 */
111 define('TYPE_NONE', 64);
112
113 /**
114 * Macro for using DB->escape_binary() without cleaning - used in API
115 */
116 define('TYPE_BIN', 128);
117 /**#@-*/
118
119 /**
120 * Yes, required
121 */
122 define('REQ_YES', 1);
123
124 /**
125 * No, not required
126 */
127 define('REQ_NO', 0);
128
129 /**
130 * Blue Static ISSO Framework (ISSO)
131 *
132 * This framework allows a common backend to be used amongst all Blue
133 * Static applications and is built to be abstract and flexible.
134 * The base framework handles all loading and module management.
135 *
136 * Constants:
137 * ISSO_NO_INPUT_SANITIZE - Disables the automatic input sanitizer
138 * ISSO_CHECK_POST_REFERER - Will check to make sure that on POSTed
139 * data, the referer matches the host
140 * ISSO_MT_START - Define the microtime() value at the top of your
141 * script and this will calculate the total execution
142 * time
143 * SVN - Place SVN keywords (like $Id) to display the information on output
144 *
145 * @author Blue Static
146 * @copyright Copyright ©2002 - [#]year[#], Blue Static
147 * @version $Revision$
148 * @package ISSO
149 *
150 */
151 class ISSO
152 {
153 /**
154 * ISSO version
155 * @var string
156 */
157 private $version = '[#]issoversion[#]';
158
159 /**
160 * Location of ISSO, used for internal linking
161 * @var string
162 */
163 private $sourcepath = '';
164
165 /**
166 * Path of the current application
167 * @var string
168 */
169 private $apppath = '';
170
171 /**
172 * Web path used to get the web location of the installation of ISSO; only used for Printer module
173 * @var string
174 */
175 private $webpath = '';
176
177 /**
178 * Name of the current application
179 * @var string
180 */
181 private $application = '';
182
183 /**
184 * Version of the current application
185 * @var string
186 */
187 private $appversion = '';
188
189 /**
190 * Whether debug mode is on or off
191 * @var bool
192 */
193 private $debug = false;
194
195 /**
196 * List of all active debug messages
197 * @var array
198 */
199 private $debuginfo = array();
200
201 /**
202 * List of loaded modules
203 * @var array
204 */
205 private $modules = array();
206
207 /**
208 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
209 * @var array
210 */
211 public $in = array();
212
213 /**
214 * If we are running with magic_quotes_gpc on or off
215 * @var int
216 */
217 private $magicquotes = 0;
218
219 /**
220 * Array of user-specified fields that are required for ISSO initialization
221 * fieldname => array(REQUIRED, CALLBACK PARSER, SET)
222 * @var array
223 */
224 private $fields = array(
225 'sourcepath' => array(REQ_YES, 'fetch_sourcepath', false),
226 'apppath' => array(REQ_YES, 'fetch_sourcepath', false),
227 'webpath' => array(REQ_NO, 'fetch_sourcepath', false),
228 'application' => array(REQ_YES, null, false),
229 'appversion' => array(REQ_NO, null, false),
230 'debug' => array(REQ_NO, null, false)
231 );
232
233 // ###################################################################
234 /**
235 * Constructor
236 */
237 public function __construct()
238 {
239 $GLOBALS['isso:callback'] = null;
240
241 // error reporting
242 set_error_handler(array(&$this, '_error_handler'));
243
244 // magic quotes
245 $this->magicquotes = get_magic_quotes_gpc();
246 set_magic_quotes_runtime(0);
247
248 // some debug info that's always useful
249 $this->debug('magic_quotes_gpc = ' . $this->magicquotes);
250 $this->debug('register_globals = ' . ini_get('register_globals'));
251
252 // attempt to set the sourcepath
253 $path = call_user_func('debug_backtrace');
254 $this->set('sourcepath', str_replace('kernel.php', '', $path[0]['file']));
255
256 // start input sanitize using variable_order GPC
257 if (!defined('ISSO_NO_INPUT_SANITIZE'))
258 {
259 $this->exec_sanitize_data();
260 }
261
262 if (defined('ISSO_CHECK_POST_REFERER'))
263 {
264 $this->exec_referer_check();
265 }
266 }
267
268 // ###################################################################
269 /**
270 * Prepares a path for being set as the sourcepath
271 *
272 * @param string Source path or URL
273 *
274 * @return string Prepared source path
275 */
276 public function fetch_sourcepath($source)
277 {
278 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
279 {
280 $source .= DIRECTORY_SEPARATOR;
281 }
282 return $source;
283 }
284
285 // ###################################################################
286 /**
287 * Loads a framework module
288 *
289 * @param string Name of the framework file to load
290 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
291 * @param bool Globalize the internal variable?
292 *
293 * @return object Instantiated instance
294 */
295 public function &load($framework, $asobject, $globalize = false)
296 {
297 $this->check_isso_fields(null);
298
299 // set the object interlock
300 if (!method_exists($GLOBALS['isso:callback'], 'load'))
301 {
302 $GLOBALS['isso:callback'] =& $this;
303 $this->modules['isso'] =& $this;
304 }
305
306 if ($this->is_loaded($framework))
307 {
308 return $this->modules["$framework"];
309 }
310
311 if ($this->sourcepath == '')
312 {
313 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
314 }
315
316 if (file_exists($this->sourcepath . $framework . '.php'))
317 {
318 require_once($this->sourcepath . $framework . '.php');
319 }
320 else
321 {
322 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
323 }
324
325 if ($asobject === null)
326 {
327 return;
328 }
329
330 if (isset($this->$asobject))
331 {
332 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
333 }
334
335 $this->$asobject = new $framework($this);
336
337 $this->modules["$framework"] =& $this->$asobject;
338
339 if ($globalize)
340 {
341 $GLOBALS["$asobject"] =& $this->$asobject;
342 }
343
344 // allow for init_as_package to link
345 if (method_exists($this->modules["$framework"], 'init_as_package'))
346 {
347 $this->modules[ $this->modules["$framework"]->init_as_package() ] =& $this->modules["$framework"];
348 }
349
350 return $this->$asobject;
351 }
352
353 // ###################################################################
354 /**
355 * Prints a list of all currently loaded framework modules
356 *
357 * @param bool Return the data as an array?
358 *
359 * @return mixed HTML output or an array of loaded modules
360 */
361 public function show_modules($return = false)
362 {
363 $modules = array();
364 foreach ($this->modules AS $object)
365 {
366 $module = get_class($object);
367 if (method_exists($object, 'init_as_package') AND in_array($module, $modules))
368 {
369 $module = $object->init_as_package() . " - ($module)";
370 }
371
372 $modules[] = $module;
373 }
374
375 if ($return)
376 {
377 return $modules;
378 }
379 else
380 {
381 $output = "\n\n<ul>\n\t<li>";
382 $output .= implode("</li>\n\t<li>", $modules);
383 $output .= "</li>\n</ul>\n\n";
384 $this->message('Loaded Modules', $output, 1);
385 }
386 }
387
388 // ###################################################################
389 /**
390 * Verifies to see if a framework has been loaded
391 *
392 * @param string Framework name
393 *
394 * @return bool Whether or not the framework has been loaded
395 */
396 public function is_loaded($framework)
397 {
398 if (isset($this->modules["$framework"]))
399 {
400 return true;
401 }
402 else
403 {
404 return false;
405 }
406 }
407
408 // ###################################################################
409 /**
410 * Prints an ISSO message
411 *
412 * @param string The title of the message
413 * @param string The content of the message
414 * @param integer Type of message to be printed
415 * @param bool Return the output?
416 * @param bool Show the debug stack?
417 * @param integer Message width
418 *
419 * @return mixed Output or null
420 */
421 public function message($title, $message, $type, $return = false, $stack = true, $width = 500)
422 {
423 switch ($type)
424 {
425 // Message
426 case 1:
427 $prefix = 'Message';
428 $color = '#669900';
429 $font = '#000000';
430 break;
431
432 // Warning
433 case 2:
434 $prefix = 'Warning';
435 $color = '#003399';
436 $font = '#FFFFFF';
437 break;
438
439 case 3:
440 $prefix = 'Error';
441 $color = '#990000';
442 $font = '#EFEFEF';
443 break;
444 }
445
446 $backtrace = debug_backtrace();
447 array_shift($backtrace);
448
449 if (isset($backtrace[0]) AND $backtrace[0]['function'] == '_error_handler')
450 {
451 array_shift($backtrace);
452 }
453
454 $trace = $this->format_debug_trace($backtrace);
455
456 $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;\">";
457 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
458 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
459 $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>" : '');
460 $output .= "\n</table>\n<br />\n";
461
462 if ($return)
463 {
464 return $output;
465 }
466 else
467 {
468 print($output);
469 }
470 }
471
472 // ###################################################################
473 /**
474 * Prepares a debug_backtrace() array for output to the browser by
475 * compressing the array into visible text
476 *
477 * @param array debug_backtrace() array
478 *
479 * @return array Formatted trace output
480 */
481 public function format_debug_trace($backtrace)
482 {
483 $trace = array();
484 foreach ($backtrace AS $i => $step)
485 {
486 $args = '';
487 $file = $step['file'] . ':' . $step['line'];
488 $funct = (isset($step['class']) ? $step['class'] . '::' . $step['function'] : $step['function']);
489
490 if (isset($step['args']) AND is_array($step['args']))
491 {
492 // we need to do this so we don't get "Array to string conversion" notices
493 foreach ($step['args'] AS $id => $arg)
494 {
495 if (is_array($arg))
496 {
497 $step['args']["$id"] = 'Array';
498 }
499 }
500 $args = implode(', ', $step['args']);
501 }
502
503 $trace[] = "#$i $funct($args) called at [$file]";
504 }
505
506 return $trace;
507 }
508
509 // ###################################################################
510 /**
511 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
512 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
513 *
514 * @param integer Error number
515 * @param string Error message string
516 * @param string File that contains the error
517 * @param string The line number of the error
518 * @param string The active symbol table at which point the error occurred
519 */
520 public function _error_handler($errno, $errstr, $errfile, $errline, $errcontext)
521 {
522 $level = ini_get('error_reporting');
523
524 switch ($errno)
525 {
526 // Fatal
527 case E_USER_ERROR:
528 $title = 'Fatal';
529 $mode = 3;
530 if (!($level & E_USER_ERROR))
531 {
532 return;
533 }
534 break;
535
536 // Error
537 case E_USER_WARNING:
538 case E_WARNING:
539 $title = 'Warning';
540 $mode = 2;
541 if (! (($level & E_USER_WARNING) AND ($level & E_WARNING)) )
542 {
543 return;
544 }
545 break;
546
547 // Warning
548 case E_USER_NOTICE:
549 case E_NOTICE:
550 default:
551 $title = 'Notice';
552 $mode = 1;
553 if (! (($level & E_USER_NOTICE) AND ($level & E_NOTICE)) )
554 {
555 return;
556 }
557 break;
558 }
559
560 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
561
562 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
563
564 $this->message($title, $errstr, $mode);
565
566 if ($errno == E_USER_ERROR)
567 {
568 exit;
569 }
570 }
571
572 // ###################################################################
573 /**
574 * Creates a table that explains the error reporting levels and their
575 * state
576 */
577 public function explain_error_reporting()
578 {
579 $levels = array(
580 'E_ERROR' => E_ERROR,
581 'E_WARNING' => E_WARNING,
582 'E_PARSE' => E_PARSE,
583 'E_NOTICE' => E_NOTICE,
584 'E_CORE_ERROR' => E_CORE_ERROR,
585 'E_CORE_WARNING' => E_CORE_WARNING,
586 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
587 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
588 'E_USER_ERROR' => E_USER_ERROR,
589 'E_USER_WARNING' => E_USER_WARNING,
590 'E_USER_NOTICE' => E_USER_NOTICE,
591 'E_ALL' => E_ALL,
592 'E_STRICT' => E_STRICT
593 );
594
595 $table = '<table cellspacing="0" cellpadding="2" border="0">';
596
597 foreach ($levels AS $name => $value)
598 {
599 $table .= '
600 <tr>
601 <td>' . $name . '</td>
602 <td>' . (ini_get('error_reporting') & $value) . '</td>
603 </tr>';
604 }
605
606 $table .= '
607 </table>';
608
609 $this->message('Error Reporting', $table, 1);
610 }
611
612 // ###################################################################
613 /**
614 * Logs a debug message for verbose output
615 *
616 * @param string Message
617 */
618 public function debug($message)
619 {
620 $this->debuginfo[] = $message;
621 }
622
623 // ###################################################################
624 /**
625 * Recursive XSS cleaner
626 *
627 * @param mixed Unsanitized REQUEST data
628 *
629 * @return mixed Sanitized data
630 */
631 public function _sanitize_input_recursive($data)
632 {
633 foreach ($data AS $key => $value)
634 {
635 if (is_array($value))
636 {
637 $data["$key"] = $this->_sanitize_input_recursive($value);
638 }
639 else
640 {
641 if ($this->magicquotes)
642 {
643 $value = str_replace("\'", "'", $value);
644 }
645 $data["$key"] = $this->sanitize($value);
646 }
647 }
648 return $data;
649 }
650
651 // ###################################################################
652 /**
653 * Simple way to protect against HTML attacks with Unicode support
654 *
655 * @param string Unsanitzed text
656 *
657 * @return string Properly protected text that only encodes potential threats
658 */
659 public function sanitize($text)
660 {
661 if ($this->magicquotes)
662 {
663 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
664 }
665 else
666 {
667 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
668 }
669 }
670
671 // ###################################################################
672 /**
673 * Unicode-safe entity encoding system; similar to sanitize()
674 *
675 * @param string Unsanitized text
676 *
677 * @return string Unicode-safe sanitized text with entities preserved
678 */
679 public function entity_encode($text)
680 {
681 $text = str_replace('&', '&amp;', $text);
682 $text = $this->sanitize($text);
683 return $text;
684 }
685
686 // ###################################################################
687 /**
688 * Takes text that has been processed for HTML and unsanitizes it
689 *
690 * @param string Text that needs to be turned back into HTML
691 *
692 * @return string Unsanitized text
693 */
694 public function unsanitize($text)
695 {
696 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
697 }
698
699 // ###################################################################
700 /**
701 * Smart addslashes() that only applies itself it the Magic Quotes GPC
702 * is off. This should only be run on database query values that come
703 * from ISSO->in[] input; data that needs sanitization should be run
704 * through ISSO->DB->escape_string()
705 *
706 * @param string Some string
707 * @param bool Force magic quotes to be off
708 *
709 * @return string String that has slashes added
710 */
711 public function escape($str, $force = true)
712 {
713 if ($this->magicquotes AND !$force)
714 {
715 if (isset($this->modules[ISSO_DB_LAYER]))
716 {
717 return $this->modules[ISSO_DB_LAYER]->escape_string(str_replace(array("\'", '\"'), array("'", '"'), $str));
718 }
719 return $str;
720 }
721 else
722 {
723 if (isset($this->modules[ISSO_DB_LAYER]))
724 {
725 return $this->modules[ISSO_DB_LAYER]->escape_string($str);
726 }
727 return addslashes($str);
728 }
729 }
730
731 // ###################################################################
732 /**
733 * Runs through all of the input data and sanitizes it.
734 */
735 private function exec_sanitize_data()
736 {
737 $this->in = $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
738 }
739
740 // ###################################################################
741 /**
742 * Sanitize function for something other than a string (which
743 * everything is sanitized for if you use exec_sanitize_data(). Cleaned
744 * data is placed back into $isso->in; this makes it so you don't have
745 * to constantly intval() [etc.] data.
746 *
747 * @param array Array of elements to clean as varname => type
748 */
749 public function input_clean_array($vars)
750 {
751 foreach ($vars AS $varname => $type)
752 {
753 $this->input_clean($varname, $type);
754 }
755 }
756
757 // ###################################################################
758 /**
759 * Sanitize function that does a single variable as oppoesd to an array
760 * (see input_clean_array() for more details)
761 *
762 * @param string Variable name in $isso->in[]
763 * @param integer Sanitization type constant
764 */
765 public function input_clean($varname, $type)
766 {
767 if (isset($this->in["$varname"]))
768 {
769 $this->in["$varname"] = $this->clean($this->in["$varname"], $type);
770 }
771 else
772 {
773 $this->in["$varname"] = $this->clean(null, $type);
774 }
775
776 return $this->in["$varname"];
777 }
778
779 // ###################################################################
780 /**
781 * Runs ISSO->escape() on a variable on ISSO->in[]. This is just a
782 * short-hand wrapper so that queries can be shortened. When this is used,
783 * the actual value in ISSO->in[] is not changed, only the return value
784 * is escaped.
785 *
786 * @param string Input variable
787 *
788 * @return string Escaped input
789 */
790 public function input_escape($varname)
791 {
792 if (isset($this->in["$varname"]))
793 {
794 return $this->escape($this->in["$varname"]);
795 }
796 else
797 {
798 return $this->escape(null);
799 }
800 }
801
802 // ###################################################################
803 /**
804 * Cleaning function that does the work for input_clean(); this is
805 * moved here so it can be used to clean things that aren't in
806 * $isso->in[]
807 *
808 * @param mixed Data
809 * @param integer Sanitization type constant
810 *
811 * @return mixed Cleaned data
812 */
813 public function clean($value, $type)
814 {
815 if (is_array($value))
816 {
817 return $this->clean_array($value, $type);
818 }
819
820 if ($type == TYPE_INT)
821 {
822 $value = intval($value);
823 }
824 else if ($type == TYPE_UINT)
825 {
826 $value = (($val = intval($value)) < 0 ? 0 : $val);
827 }
828 else if ($type == TYPE_FLOAT)
829 {
830 $value = floatval($value);
831 }
832 else if ($type == TYPE_BOOL)
833 {
834 $value = (bool)$value;
835 }
836 else if ($type == TYPE_STR)
837 {
838 $value = $value;
839 }
840 else if ($type == TYPE_STRUN)
841 {
842 $value = $this->unsanitize($value);
843 }
844 else if ($type == TYPE_NOCLEAN)
845 {
846 if ($this->magicquotes)
847 {
848 $value = str_replace(array('\"', "\'"), array('"', "'"), $value);
849 }
850 else
851 {
852 $value = $value;
853 }
854 }
855 else if ($type == TYPE_BIN)
856 {
857 $value = $value;
858 }
859 else
860 {
861 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR);
862 }
863
864 return $value;
865 }
866
867 // ###################################################################
868 /**
869 * Recursion function for ISSO->clean()
870 *
871 * @param array Uncleaned array
872 * @param integer Sanitization type constant
873 *
874 * @return array Cleaned array of data
875 */
876 private function clean_array($array, $type)
877 {
878 foreach ($array AS $key => $value)
879 {
880 $array["$key"] = $this->clean($value, $type);
881 }
882
883 return $array;
884 }
885
886 // ###################################################################
887 /**
888 * Checks to see if a POST refer is actually from us
889 */
890 public function exec_referer_check()
891 {
892 if ($_SERVER['REQUEST_METHOD'] == 'POST')
893 {
894 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
895
896 if ($host AND $_SERVER['HTTP_REFERER'])
897 {
898 $parts = parse_url($_SERVER['HTTP_REFERER']);
899 $ourhost = $parts['host'] . (isset($parts['port']) ? ":$parts[port]" : '');
900
901 if ($ourhost != $host)
902 {
903 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR);
904 }
905 $this->debug('remote post check = ok');
906 }
907 else
908 {
909 $this->debug('remote post check = FAILED');
910 }
911 }
912 }
913
914 // ###################################################################
915 /**
916 * Constructs a debug information box that contains various debugging
917 * information points
918 *
919 * @param bool Show template information?
920 *
921 * @return string Debugging block
922 */
923 public function construct_debug_block($dotemplates)
924 {
925 $debug = '';
926
927 if ($this->debug)
928 {
929 $debug = "\n<ul>";
930
931 // templates
932 if ($dotemplates)
933 {
934 $optlist = array();
935 $usage = array();
936 foreach ($this->modules['template']->usage AS $name => $count)
937 {
938 if (in_array($name, $this->modules['template']->uncached))
939 {
940 $optlist[] = $name . '[' . $count . ']';
941 }
942 $usage[] = $name . " ($count)";
943 }
944
945 $sizeof = sizeof($this->modules['template']->uncached);
946 if ($sizeof > 0)
947 {
948 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' &nbsp; ', $optlist) . " )</li>";
949 }
950 }
951
952 // source control
953 $scinfo = 'Not Under Source Control';
954 if (defined('SVN'))
955 {
956 $scinfo = constant('SVN');
957
958 if (preg_match('#\$Id:?\s*\$#', $scinfo))
959 {
960 $scinfo = 'Not Under Source Control';
961 }
962 else
963 {
964 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
965 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
966 $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);
967 }
968 }
969
970 $scinfo = trim($scinfo);
971 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
972
973 // query information
974 if (is_object($this->modules[ISSO_DB_LAYER]))
975 {
976 $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>";
977 }
978
979 // total execution time
980 if (defined('ISSO_MT_START'))
981 {
982 $this->load('functions', 'functions');
983 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->modules['functions']->fetch_microtime_diff(ISSO_MT_START), 10) . "</li>";
984 }
985
986 // debug notices
987 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->debuginfo) . ")</option>";
988 foreach ((array)$this->debuginfo AS $msg)
989 {
990 $debug .= "\n\t\t\t<option>--- $msg</option>";
991 }
992 $debug .= "\n\t\t</select>\n\t</li>";
993
994 // loaded modules
995 $modules = $this->show_modules(true);
996 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
997 foreach ($modules AS $mod)
998 {
999 $debug .= "\n\t\t\t<option>--- $mod</option>";
1000 }
1001 $debug .= "\n\t\t</select>\n\t</li>";
1002
1003 // template usage
1004 if ($dotemplates)
1005 {
1006 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($this->modules['template']->usage) . ")</option>";
1007 foreach ($usage AS $tpl)
1008 {
1009 $debug .= "\n\t\t\t<option>--- $tpl</option>";
1010 }
1011 $debug .= "\n\t\t</select>\n\t</li>";
1012 }
1013
1014 $debug .= "\n</ul>";
1015
1016 $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";
1017 }
1018
1019 return $debug;
1020 }
1021 }
1022
1023 /*=====================================================================*\
1024 || ###################################################################
1025 || # $HeadURL$
1026 || # $Id$
1027 || ###################################################################
1028 \*=====================================================================*/
1029 ?>