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