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