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