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