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