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