2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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.
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
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 \*=====================================================================*/
23 * Iris Studios Shared Object Framework Kernel
29 if (!function_exists('version_compare'))
31 trigger_error('You need PHP version 4.1.0 or newer to run ISSO', E_USER_ERROR
);
35 if (version_compare(PHP_VERSION
, '5.0.0', '>='))
37 if (ini_get('error_reporting') & E_NOTICE
)
39 error_reporting(ini_get('error_reporting') - E_NOTICE
);
41 if (ini_get('error_reporting') & E_USER_NOTICE
)
43 error_reporting(ini_get('error_reporting') - E_USER_NOTICE
);
47 $oldlevel = ini_get('error_reporting');
48 $newlevel = $oldlevel;
49 $levels = array(E_ERROR
=> E_USER_ERROR
, E_WARNING
=> E_USER_WARNING
, E_NOTICE
=> E_USER_NOTICE
);
50 foreach ($levels AS $php => $isso)
54 if (!($oldlevel & $isso))
61 if ($oldlevel & $isso)
67 error_reporting($newlevel);
69 if ((bool)ini_get('register_globals') === true)
71 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
72 foreach ($superglobals AS $global)
74 if (is_array(${$global}))
76 foreach (${$global} AS $_key => $_val)
88 * Input cleaning type constant
93 define('TYPE_INT', 1);
98 define('TYPE_UINT', 2);
103 define('TYPE_FLOAT', 4);
108 define('TYPE_BOOL', 8);
113 define('TYPE_STR', 16);
116 * String - deliberate unclean
118 define('TYPE_STRUN', 32);
121 * No cleaning - here for use in API
123 define('TYPE_NOCLEAN', 64);
127 * Iris Studios Shared Object Framework (ISSO)
129 * This framework allows a common backend to be used amongst all Iris
130 * Studios applications and is built to be abstract and flexible.
131 * The base framework handles all loading and module management.
133 * @author Iris Studios, Inc.
134 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
135 * @version $Revision$
139 class Shared_Object_Framework
145 var $version = '[#]version[#]';
148 * Location of ISSO, used for internal linking
151 var $sourcepath = '';
154 * Path of the current application
160 * Web path used to get the web location of the installation of ISSO; only used for Printer module
166 * Name of the current application
169 var $application = '';
172 * Version of the current application
175 var $appversion = '';
178 * Whether debug mode is on or off
184 * List of all active debug messages
187 var $debuginfo = array();
190 * List of loaded modules
193 var $modules = array();
196 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
202 * If we are running with magic_quotes_gpc on or off
205 var $magicquotes = 0;
208 * If we should automagically escape strings, mimicking magic_quotes_gpc
211 var $escapestrings = false;
213 // ###################################################################
217 function __construct()
220 set_error_handler(array(&$this, '_error_handler'));
223 $this->magicquotes
= get_magic_quotes_gpc();
224 set_magic_quotes_runtime(0);
226 if (defined('ISSO_ESCAPE_STRINGS'))
228 $this->escapestrings
= (bool)constant('ISSO_ESCAPE_STRINGS');
231 // start input sanitize using variable_order GPC
232 if (!$this->escapestrings
)
234 $this->exec_sanitize_data();
237 if (defined('ISSO_CHECK_POST_REFERER'))
239 $this->exec_referer_check();
242 $GLOBALS['isso:null-framework'] = null;
245 // ###################################################################
247 * (PHP 4) Constructor
249 function Shared_Object_Framework()
251 $this->__construct();
254 // ###################################################################
256 * Prepares a path for being set as the sourcepath
260 * @param string Source path or URL
262 * @return string Prepared source path
264 function fetch_sourcepath($source)
266 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
268 $source .= DIRECTORY_SEPARATOR
;
273 // ###################################################################
275 * Loads a framework module
279 * @param string Name of the framework file to load
280 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
281 * @param bool Globalize the internal variable?
283 * @return object Instantiated instance
285 function &load($framework, $asobject, $globalize = false)
287 // set the object interlock
288 if (!method_exists($GLOBALS['isso:null-framework'], 'load'))
290 $GLOBALS['isso:null-framework'] =& $this;
293 if ($this->is_loaded($framework))
295 return $this->modules
["$framework"];
298 if ($this->sourcepath == '')
300 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
303 if (file_exists($this->sourcepath . $framework . '.php'))
305 require_once($this->sourcepath . $framework . '.php');
309 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
312 if ($asobject === null)
317 if (isset($this->$asobject))
319 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
322 $this->$asobject = new $framework($this);
324 $this->modules["$framework"] =& $this->$asobject;
328 $GLOBALS["$asobject"] =& $this->$asobject;
331 return $this->$asobject;
334 // ###################################################################
336 * Prints a list of all currently loaded framework modules
340 * @param bool Return the data as an array?
342 * @return mixed HTML output or an array of loaded modules
344 function show_modules($return = false)
346 foreach ($this->modules AS $object)
348 $modules[] = get_class($object);
357 $output = "\n\n
<ul
>\n\t<li
>";
358 $output .= implode("</li
>\n\t<li
>", $modules);
359 $output .= "</li
>\n</ul
>\n\n";
360 $this->message('Loaded Modules', $output, 1);
364 // ###################################################################
366 * Verifies to see if a framework has been loaded
370 * @param string Framework name
372 * @return bool Whether or not the framework has been loaded
374 function is_loaded($framework)
376 if (isset($this->modules["$framework"]))
386 // ###################################################################
388 * Prints an ISSO message
392 * @param string The title of the message
393 * @param string The content of the message
394 * @param integer Type of message to be printed
395 * @param bool Return the output?
396 * @param bool Show the debug stack?
398 * @return mixed Output or null
400 function message($title, $message, $type, $return = false, $stack = true)
425 $backtrace = debug_backtrace();
426 unset($backtrace[0]);
428 $output = "\n<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"500\" style=\"background-color: $color; color: black; font-family: Verdana, sans-serif; font-size: 12px;\">";
429 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
430 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
431 $output .= (($stack AND $GLOBALS['isso:null-framework']->debug
) ? "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>" . print_r($backtrace, true) . "</pre></td>\n</tr>" : '');
432 $output .= "\n</table>\n<br />\n";
444 // ###################################################################
446 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
447 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
451 * @param integer Error number
452 * @param string Error message string
453 * @param string File that contains the error
454 * @param string The line number of the error
455 * @param string The active symbol table at which point the error occurred
457 function _error_handler($errno, $errstr, $errfile, $errline)
465 if (!(ini_get('error_reporting') & E_USER_ERROR
))
475 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
486 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
493 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
495 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
497 $this->message($title, $errstr, $level);
499 if ($errno == E_USER_ERROR
)
505 // ###################################################################
507 * Creates a table that explains the error reporting levels and their
512 function explain_error_reporting()
515 'E_ERROR' => E_ERROR
,
516 'E_WARNING' => E_WARNING
,
517 'E_PARSE' => E_PARSE
,
518 'E_NOTICE' => E_NOTICE
,
519 'E_CORE_ERROR' => E_CORE_ERROR
,
520 'E_CORE_WARNING' => E_CORE_WARNING
,
521 'E_COMPILE_ERROR' => 64,
522 'E_COMPILE_WARNING' => 128,
523 'E_USER_ERROR' => E_USER_ERROR
,
524 'E_USER_WARNING' => E_USER_WARNING
,
525 'E_USER_NOTICE' => E_USER_NOTICE
,
530 $table = '<table cellspacing="0" cellpadding="2" border="0">';
532 foreach ($levels AS $name => $value)
536 <td>' . $name . '</td>
537 <td>' . (ini_get('error_reporting') & $value) . '</td>
544 $this->message('Error Reporting', $table, 1);
547 // ###################################################################
549 * Logs a debug message for verbose output
553 * @param string Message
555 function debug($message)
557 $this->debuginfo
[] = $message;
560 // ###################################################################
562 * Recursive XSS cleaner
566 * @param mixed Unsanitized REQUEST data
568 * @return mixed Sanitized data
570 function _sanitize_input_recursive($data)
572 foreach ($data AS $key => $value)
574 if (is_array($value))
576 $data["$key"] = $this->_sanitize_input_recursive($value);
580 if ($this->escapestrings)
582 $data["$key"] = $this->escape($this->sanitize($value), false, false);
586 $data["$key"] = $this->sanitize($value);
593 // ###################################################################
595 * Simple way to protect against HTML attacks with Unicode support
599 * @param string Unsanitzed text
600 * @param bool Force magic quotes off?
602 * @return string Properly protected text that only encodes potential threats
604 function sanitize($text, $force = false)
606 if ($this->magicquotes AND !$force)
608 return str_replace(array('<', '>', '\"', '"'), array('<
;', '>
;', '"
;', '"
;'), $text);
612 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
616 // ###################################################################
618 * Unicode-safe entity encoding system; similar to sanitize()
622 * @param string Unsanitized text
624 * @return string Unicode-safe sanitized text with entities preserved
626 function entity_encode($text)
628 $text = str_replace('&', '&', $text);
629 $text = $this->sanitize($text);
633 // ###################################################################
635 * Takes text that has been processed for HTML and unsanitizes it
639 * @param string Text that needs to be turned back into HTML
640 * @param bool Force magicquotes off
642 * @return string Unsanitized text
644 function unsanitize($text, $force = false)
646 if ($this->magicquotes AND !$force)
648 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
652 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
656 // ###################################################################
658 * Smart addslashes() that only applies itself it the Magic Quotes GPC
663 * @param string Some string
664 * @param bool If the data is binary; if so it'll be run through DB
::escape_stringing()
665 * @param
bool Force magic quotes to be off
667 * @return string String that has slashes added
669 function escape($str, $binary = false, $force = true)
671 if ($this->magicquotes
AND !$force)
673 if (isset($this->db
) AND $binary)
675 if (is_resource($this->db
->link_id
))
677 return $this->db
->escape_string(stripslashes($str));
684 if (isset($this->db
) AND $binary)
686 if (is_resource($this->db
->link_id
))
688 return $this->db
->escape_string($str);
691 return addslashes($str);
695 // ###################################################################
697 * Runs through all of the input data and sanitizes it.
701 function exec_sanitize_data()
703 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
704 // we're now using magic quotes
705 if ($this->escapestrings
)
707 $this->magicquotes
= 1;
711 // ###################################################################
713 * Sanitize function for something other than a string (which
714 * everything is sanitized for if you use exec_sanitize_data(). Cleaned
715 * data is placed back into $isso->in; this makes it so you don't have
716 * to constantly intval() [etc.] data.
720 * @param array Array of elements to clean as varname => type
722 function input_clean_array($vars)
724 foreach ($vars AS $varname => $type)
726 $this->input_clean($varname, $type);
730 // ###################################################################
732 * Sanitize function that does a single variable as oppoesd to an array
733 * (see input_clean_array() for more details)
737 * @param string Variable name in $isso->in[]
738 * @param integer Sanitization type constant
740 function input_clean($varname, $type)
742 if (isset($this->in
["$varname"]))
744 $this->in["$varname"] = $this->clean($this->in
["$varname"], $type);
748 $this->in["$varname"] = null;
752 // ###################################################################
754 * Cleaning function that does the work for input_clean(); this is
755 * moved here so it can be used to clean things that aren't in
761 * @param integer Sanitization type constant
763 * @return mixed Cleaned data
765 function clean($value, $type)
767 if ($type == TYPE_INT
)
769 $value = intval($value);
771 else if ($type == TYPE_UINT
)
773 $value = abs(intval($value));
775 else if ($type == TYPE_FLOAT
)
777 $value = floatval($value);
779 else if ($type == TYPE_BOOL
)
781 $value = (bool)$value;
783 else if ($type == TYPE_STR
)
785 if (!$this->escapestrings
)
787 $value = $this->escape($value);
790 else if ($type == TYPE_STRUN
)
792 $value = $this->unsanitize($value);
794 else if ($type == TYPE_NOCLEAN
)
796 if ($this->escapestrings
)
798 $value = $this->escape($value);
803 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR
);
809 // ###################################################################
811 * Checks to see if a POST refer is actually from us
815 function exec_referer_check()
817 if ($_SERVER['REQUEST_METHOD'] == 'POST')
819 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
821 if ($host AND $_SERVER['HTTP_REFERER'])
823 $parts = parse_url($_SERVER['HTTP_REFERER']);
824 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
826 if ($ourhost != $host)
828 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
830 $this->debug('remote post check = ok');
834 $this->debug('remote post check = FAILED');
840 /*=====================================================================*\
841 || ###################################################################
844 || ###################################################################
845 \*=====================================================================*/