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