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