Allow check_isso_fields() to be called for fun
[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 exit;
382 }
383 }
384
385 // ###################################################################
386 /**
387 * Prepares a path for being set as the sourcepath
388 *
389 * @access public
390 *
391 * @param string Source path or URL
392 *
393 * @return string Prepared source path
394 */
395 function fetch_sourcepath($source)
396 {
397 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
398 {
399 $source .= DIRECTORY_SEPARATOR;
400 }
401 return $source;
402 }
403
404 // ###################################################################
405 /**
406 * Loads a framework module
407 *
408 * @access public
409 *
410 * @param string Name of the framework file to load
411 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
412 * @param bool Globalize the internal variable?
413 *
414 * @return object Instantiated instance
415 */
416 function &load($framework, $asobject, $globalize = false)
417 {
418 $this->check_isso_fields();
419
420 // set the object interlock
421 if (!method_exists($GLOBALS['isso:null-framework'], 'load'))
422 {
423 $GLOBALS['isso:null-framework'] =& $this;
424 }
425
426 if ($this->is_loaded($framework))
427 {
428 return $this->modules["$framework"];
429 }
430
431 if ($this->sourcepath == '')
432 {
433 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
434 }
435
436 if (file_exists($this->sourcepath . $framework . '.php'))
437 {
438 require_once($this->sourcepath . $framework . '.php');
439 }
440 else
441 {
442 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
443 }
444
445 if ($asobject === null)
446 {
447 return;
448 }
449
450 if (isset($this->$asobject))
451 {
452 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
453 }
454
455 $this->$asobject = new $framework($this);
456
457 $this->modules["$framework"] =& $this->$asobject;
458
459 if ($globalize)
460 {
461 $GLOBALS["$asobject"] =& $this->$asobject;
462 }
463
464 return $this->$asobject;
465 }
466
467 // ###################################################################
468 /**
469 * Prints a list of all currently loaded framework modules
470 *
471 * @access public
472 *
473 * @param bool Return the data as an array?
474 *
475 * @return mixed HTML output or an array of loaded modules
476 */
477 function show_modules($return = false)
478 {
479 foreach ($this->modules AS $object)
480 {
481 $modules[] = get_class($object);
482 }
483
484 if ($return)
485 {
486 return $modules;
487 }
488 else
489 {
490 $output = "\n\n<ul>\n\t<li>";
491 $output .= implode("</li>\n\t<li>", $modules);
492 $output .= "</li>\n</ul>\n\n";
493 $this->message('Loaded Modules', $output, 1);
494 }
495 }
496
497 // ###################################################################
498 /**
499 * Verifies to see if a framework has been loaded
500 *
501 * @access public
502 *
503 * @param string Framework name
504 *
505 * @return bool Whether or not the framework has been loaded
506 */
507 function is_loaded($framework)
508 {
509 if (isset($this->modules["$framework"]))
510 {
511 return true;
512 }
513 else
514 {
515 return false;
516 }
517 }
518
519 // ###################################################################
520 /**
521 * Prints an ISSO message
522 *
523 * @access public
524 *
525 * @param string The title of the message
526 * @param string The content of the message
527 * @param integer Type of message to be printed
528 * @param bool Return the output?
529 * @param bool Show the debug stack?
530 *
531 * @return mixed Output or null
532 */
533 function message($title, $message, $type, $return = false, $stack = true)
534 {
535 switch ($type)
536 {
537 // Message
538 case 1:
539 $prefix = 'Message';
540 $color = '#669900';
541 $font = '#000000';
542 break;
543
544 // Warning
545 case 2:
546 $prefix = 'Warning';
547 $color = '#003399';
548 $font = '#FFFFFF';
549 break;
550
551 case 3:
552 $prefix = 'Error';
553 $color = '#990000';
554 $font = '#EFEFEF';
555 break;
556 }
557
558 $backtrace = debug_backtrace();
559 unset($backtrace[0]);
560
561 $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;\">";
562 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
563 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
564 $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>" : '');
565 $output .= "\n</table>\n<br />\n";
566
567 if ($return)
568 {
569 return $output;
570 }
571 else
572 {
573 print($output);
574 }
575 }
576
577 // ###################################################################
578 /**
579 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
580 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
581 *
582 * @access private
583 *
584 * @param integer Error number
585 * @param string Error message string
586 * @param string File that contains the error
587 * @param string The line number of the error
588 * @param string The active symbol table at which point the error occurred
589 */
590 function _error_handler($errno, $errstr, $errfile, $errline)
591 {
592 switch ($errno)
593 {
594 // Fatal
595 case E_USER_ERROR:
596 $title = 'Fatal';
597 $level = 3;
598 if (!(ini_get('error_reporting') & E_USER_ERROR))
599 {
600 return;
601 }
602 break;
603
604 // Error
605 case E_USER_WARNING:
606 $title = 'Warning';
607 $level = 2;
608 if (!(ini_get('error_reporting') & E_USER_WARNING) AND !(ini_get('error_reporting') & E_WARNING))
609 {
610 return;
611 }
612 break;
613
614 // Warning
615 case E_USER_NOTICE:
616 default:
617 $title = 'Notice';
618 $level = 1;
619 if (!(ini_get('error_reporting') & E_USER_NOTICE) AND !(ini_get('error_reporting') & E_NOTICE))
620 {
621 return;
622 }
623 break;
624 }
625
626 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
627
628 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
629
630 $this->message($title, $errstr, $level);
631
632 if ($errno == E_USER_ERROR)
633 {
634 exit;
635 }
636 }
637
638 // ###################################################################
639 /**
640 * Creates a table that explains the error reporting levels and their
641 * state
642 *
643 * @access public
644 */
645 function explain_error_reporting()
646 {
647 $levels = array(
648 'E_ERROR' => E_ERROR,
649 'E_WARNING' => E_WARNING,
650 'E_PARSE' => E_PARSE,
651 'E_NOTICE' => E_NOTICE,
652 'E_CORE_ERROR' => E_CORE_ERROR,
653 'E_CORE_WARNING' => E_CORE_WARNING,
654 'E_COMPILE_ERROR' => 64,
655 'E_COMPILE_WARNING' => 128,
656 'E_USER_ERROR' => E_USER_ERROR,
657 'E_USER_WARNING' => E_USER_WARNING,
658 'E_USER_NOTICE' => E_USER_NOTICE,
659 'E_ALL' => E_ALL,
660 'E_STRICT' => 2048
661 );
662
663 $table = '<table cellspacing="0" cellpadding="2" border="0">';
664
665 foreach ($levels AS $name => $value)
666 {
667 $table .= '
668 <tr>
669 <td>' . $name . '</td>
670 <td>' . (ini_get('error_reporting') & $value) . '</td>
671 </tr>';
672 }
673
674 $table .= '
675 </table>';
676
677 $this->message('Error Reporting', $table, 1);
678 }
679
680 // ###################################################################
681 /**
682 * Logs a debug message for verbose output
683 *
684 * @access public
685 *
686 * @param string Message
687 */
688 function debug($message)
689 {
690 $this->debuginfo[] = $message;
691 }
692
693 // ###################################################################
694 /**
695 * Recursive XSS cleaner
696 *
697 * @access private
698 *
699 * @param mixed Unsanitized REQUEST data
700 *
701 * @return mixed Sanitized data
702 */
703 function _sanitize_input_recursive($data)
704 {
705 foreach ($data AS $key => $value)
706 {
707 if (is_array($value))
708 {
709 $data["$key"] = $this->_sanitize_input_recursive($value);
710 }
711 else
712 {
713 $data["$key"] = $this->sanitize($value);
714 }
715 }
716 return $data;
717 }
718
719 // ###################################################################
720 /**
721 * Simple way to protect against HTML attacks with Unicode support
722 *
723 * @access public
724 *
725 * @param string Unsanitzed text
726 *
727 * @return string Properly protected text that only encodes potential threats
728 */
729 function sanitize($text)
730 {
731 if ($this->magicquotes)
732 {
733 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
734 }
735 else
736 {
737 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
738 }
739 }
740
741 // ###################################################################
742 /**
743 * Unicode-safe entity encoding system; similar to sanitize()
744 *
745 * @access public
746 *
747 * @param string Unsanitized text
748 *
749 * @return string Unicode-safe sanitized text with entities preserved
750 */
751 function entity_encode($text)
752 {
753 $text = str_replace('&', '&amp;', $text);
754 $text = $this->sanitize($text);
755 return $text;
756 }
757
758 // ###################################################################
759 /**
760 * Takes text that has been processed for HTML and unsanitizes it
761 *
762 * @access public
763 *
764 * @param string Text that needs to be turned back into HTML
765 *
766 * @return string Unsanitized text
767 */
768 function unsanitize($text)
769 {
770 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
771 }
772
773 // ###################################################################
774 /**
775 * Smart addslashes() that only applies itself it the Magic Quotes GPC
776 * is off. This should only be run on database query values.
777 *
778 * @access public
779 *
780 * @param string Some string
781 * @param bool If the data is binary; if so it'll be run through DB::escape_stringing()
782 * @param bool Force magic quotes to be off
783 *
784 * @return string String that has slashes added
785 */
786 function escape($str, $binary = false, $force = true)
787 {
788 if ($this->magicquotes AND !$force)
789 {
790 if (isset($this->modules[ISSO_DB_LAYER]) AND $binary)
791 {
792 return $this->modules[ISSO_DB_LAYER]->escape_string(str_replace(array("\'", '\"'), array("'", '"'), $str));
793 }
794 return $str;
795 }
796 else
797 {
798 if (isset($this->modules[ISSO_DB_LAYER]) AND $binary)
799 {
800 return $this->modules[ISSO_DB_LAYER]->escape_string($str);
801 }
802 return addslashes($str);
803 }
804 }
805
806 // ###################################################################
807 /**
808 * Runs through all of the input data and sanitizes it.
809 *
810 * @access public
811 */
812 function exec_sanitize_data()
813 {
814 $this->in = $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
815 }
816
817 // ###################################################################
818 /**
819 * Sanitize function for something other than a string (which
820 * everything is sanitized for if you use exec_sanitize_data(). Cleaned
821 * data is placed back into $isso->in; this makes it so you don't have
822 * to constantly intval() [etc.] data.
823 *
824 * @access public
825 *
826 * @param array Array of elements to clean as varname => type
827 */
828 function input_clean_array($vars)
829 {
830 foreach ($vars AS $varname => $type)
831 {
832 $this->input_clean($varname, $type);
833 }
834 }
835
836 // ###################################################################
837 /**
838 * Sanitize function that does a single variable as oppoesd to an array
839 * (see input_clean_array() for more details)
840 *
841 * @access public
842 *
843 * @param string Variable name in $isso->in[]
844 * @param integer Sanitization type constant
845 */
846 function input_clean($varname, $type)
847 {
848 if (isset($this->in["$varname"]))
849 {
850 $this->in["$varname"] = $this->clean($this->in["$varname"], $type);
851 }
852 else
853 {
854 $this->in["$varname"] = null;
855 }
856
857 return $this->in["$varname"];
858 }
859
860 // ###################################################################
861 /**
862 * Cleaning function that does the work for input_clean(); this is
863 * moved here so it can be used to clean things that aren't in
864 * $isso->in[]
865 *
866 * @access public
867 *
868 * @param mixed Data
869 * @param integer Sanitization type constant
870 *
871 * @return mixed Cleaned data
872 */
873 function clean($value, $type)
874 {
875 if ($type == TYPE_INT)
876 {
877 $value = intval($value);
878 }
879 else if ($type == TYPE_UINT)
880 {
881 $value = abs(intval($value));
882 }
883 else if ($type == TYPE_FLOAT)
884 {
885 $value = floatval($value);
886 }
887 else if ($type == TYPE_BOOL)
888 {
889 $value = (bool)$value;
890 }
891 else if ($type == TYPE_STR)
892 {
893 $value = $value;
894 }
895 else if ($type == TYPE_STRUN)
896 {
897 $value = $this->unsanitize($value);
898 }
899 else if ($type == TYPE_NOCLEAN)
900 {
901 $value = $value;
902 }
903 else
904 {
905 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR);
906 }
907
908 return $value;
909 }
910
911 // ###################################################################
912 /**
913 * Checks to see if a POST refer is actually from us
914 *
915 * @access public
916 */
917 function exec_referer_check()
918 {
919 if ($_SERVER['REQUEST_METHOD'] == 'POST')
920 {
921 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
922
923 if ($host AND $_SERVER['HTTP_REFERER'])
924 {
925 $parts = parse_url($_SERVER['HTTP_REFERER']);
926 $ourhost = $parts['host'] . (isset($parts['port']) ? ":$parts[port]" : '');
927
928 if ($ourhost != $host)
929 {
930 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR);
931 }
932 $this->debug('remote post check = ok');
933 }
934 else
935 {
936 $this->debug('remote post check = FAILED');
937 }
938 }
939 }
940
941 // ###################################################################
942 /**
943 * Constructs a debug information box that contains various debugging
944 * information points
945 *
946 * @access public
947 *
948 * @param bool Show template information?
949 *
950 * @return string Debugging block
951 */
952 function construct_debug_block($dotemplates)
953 {
954 $debug = '';
955
956 if ($this->debug)
957 {
958 $debug = "\n<ul>";
959
960 // templates
961 if ($dotemplates)
962 {
963 // both template and template_fs are viable, so we need to determine the right one
964 if ($this->is_loaded('template'))
965 {
966 $tpl_obj =& $this->modules['template'];
967 }
968 else if ($this->is_loaded('template_fs'))
969 {
970 $tpl_obj =& $this->modules['template_fs'];
971 }
972 else
973 {
974 $tpl_obj = null;
975 }
976
977 $optlist = array();
978 $usage = array();
979 foreach ($tpl_obj->usage AS $name => $count)
980 {
981 if (in_array($name, $tpl_obj->uncached))
982 {
983 $optlist[] = $name . '[' . $count . ']';
984 }
985 $usage[] = $name . " ($count)";
986 }
987
988 $sizeof = sizeof($tpl_obj->uncached);
989 if ($sizeof > 0)
990 {
991 $debug .= "\n\t<li><strong style=\"color: red\">Uncached Template(s):</strong> $sizeof ( " . implode(' &nbsp; ', $optlist) . " )</li>";
992 }
993 }
994
995 // source control
996 $scinfo = 'Not Under Source Control';
997 if (defined('SVN'))
998 {
999 $scinfo = constant('SVN');
1000
1001 if (preg_match('#\$Id:?\s*\$#', $scinfo))
1002 {
1003 $scinfo = 'Not Under Source Control';
1004 }
1005 else
1006 {
1007 $scinfo = preg_replace('#\$' . '(Head)?URL: (.+?) \$#e', "end(explode('/', '\\2'))", $scinfo);
1008 $scinfo = preg_replace('#\$' . '(LastModified)?Revision: (.+?) \$#', 'SVN \\2', $scinfo);
1009 $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);
1010 }
1011 }
1012
1013 $scinfo = trim($scinfo);
1014 $debug .= "\n\t<li><strong>Source Control:</strong> $scinfo</li>";
1015
1016 // query information
1017 if (is_object($this->modules[ISSO_DB_LAYER]))
1018 {
1019 $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>";
1020 }
1021
1022 // total execution time
1023 if (defined('ISSO_MT_START'))
1024 {
1025 $this->load('functions', 'functions');
1026 $debug .= "\n\t<li><strong>Total Execution Time:</strong> " . round($this->modules['functions']->fetch_microtime_diff(ISSO_MT_START), 10) . "</li>";
1027 }
1028
1029 // debug notices
1030 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Debug Notices (" . sizeof($this->debuginfo) . ")</option>";
1031 foreach ((array)$this->debuginfo AS $msg)
1032 {
1033 $debug .= "\n\t\t\t<option>--- $msg</option>";
1034 }
1035 $debug .= "\n\t\t</select>\n\t</li>";
1036
1037 // loaded modules
1038 $modules = $this->show_modules(true);
1039 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Loaded Modules (" . sizeof($modules) . ")</option>";
1040 foreach ($modules AS $mod)
1041 {
1042 $debug .= "\n\t\t\t<option>--- $mod</option>";
1043 }
1044 $debug .= "\n\t\t</select>\n\t</li>";
1045
1046 // template usage
1047 if ($dotemplates)
1048 {
1049 $debug .= "\n\t<li>\n\t\t<select>\n\t\t\t<option>Template Usage (" . array_sum($tpl_obj->usage) . ")</option>";
1050 foreach ($usage AS $tpl)
1051 {
1052 $debug .= "\n\t\t\t<option>--- $tpl</option>";
1053 }
1054 $debug .= "\n\t\t</select>\n\t</li>";
1055 }
1056
1057 $debug .= "\n</ul>";
1058
1059 $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";
1060 }
1061
1062 return $debug;
1063 }
1064 }
1065
1066 /*=====================================================================*\
1067 || ###################################################################
1068 || # $HeadURL$
1069 || # $Id$
1070 || ###################################################################
1071 \*=====================================================================*/
1072 ?>