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