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