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