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