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