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