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