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))
56 //echo "increasing newlevel by $isso; ";
62 if ($oldlevel & $isso)
64 //echo "decreasing newlevel by $isso; ";
69 error_reporting($newlevel);
71 if ((bool
)ini_get('register_globals') === true
)
73 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
74 foreach ($superglobals AS $global)
76 if (is_array($
{$global}))
78 foreach ($
{$global} AS $_key => $_val)
92 define('TYPE_INT', 1);
97 define('TYPE_UINT', 2);
102 define('TYPE_FLOAT', 4);
107 define('TYPE_BOOL', 8);
112 define('TYPE_STR', 16);
115 * String - deliberate unclean
117 define('TYPE_STRUN', 32);
120 * No cleaning - here for use in API
122 define('TYPE_NOCLEAN', 64);
125 * Iris Studios Shared Object Framework (ISSO)
127 * This framework allows a common backend to be used amongst all Iris
128 * Studios applications and is built to be abstract and flexible.
129 * The base framework handles all loading and module management.
131 * @author Iris Studios, Inc.
132 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
133 * @version $Revision$
137 class Shared_Object_Framework
143 var $version = '[#]version[#]';
146 * Location of ISSO, used for internal linking
149 var $sourcepath = '';
152 * Path of the current application
158 * Name of the current application
161 var $application = '';
164 * Version of the current application
167 var $appversion = '';
170 * Whether debug mode is on or off
176 * List of all active debug messages
179 var $debuginfo = array();
182 * List of loaded modules
185 var $modules = array();
188 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
194 * If we are running with magic_quotes_gpc on or off
197 var $magicquotes = 0;
200 * If we should automagically escape strings, mimicking magic_quotes_gpc
203 var $escapestrings = false
;
208 function Shared_Object_Framework()
211 set_error_handler(array(&$this, '_error_handler'));
214 $this->magicquotes
= get_magic_quotes_gpc();
215 set_magic_quotes_runtime(0);
217 if (defined('ISSO_ESCAPE_STRINGS'))
219 $this->escapestrings
= (bool
)constant('ISSO_ESCAPE_STRINGS');
222 // start input sanitize using variable_order GPC
223 if (!$this->escapestrings
)
225 $this->exec_sanitize_data();
228 if (defined('ISSO_CHECK_POST_REFERER'))
230 $this->exec_referer_check();
235 * Prepares a path for being set as the sourcepath
237 * @param string Source path or URL
239 * @return string Prepared source path
241 function fetch_sourcepath($source)
243 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
245 $source .= DIRECTORY_SEPARATOR
;
251 * Loads a framework module
253 * @param string Name of the framework file to load
254 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
255 * @param bool Globalize the internal variable?
257 * @return object Instantiated instance
259 function &load($framework, $asobject, $globalize = false
)
261 if ($this->is_loaded($framework))
263 return $this->getobj($framework);
266 if ($this->sourcepath
== '')
268 trigger_error('Invalid sourcepath specified', E_USER_ERROR
);
271 if (file_exists($this->sourcepath
. $framework . '.php'))
273 require_once($this->sourcepath
. $framework . '.php');
277 trigger_error('Could not find the framework ' . $this->sourcepath
. $framework . '.php', E_USER_ERROR
);
280 if ($asobject === null
)
285 if (isset($this->$asobject))
287 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR
);
290 $this->$asobject = new $framework($this);
292 $this->modules
["$framework"] =& $this->$asobject;
296 $GLOBALS["$asobject"] =& $this->$asobject;
299 return $this->$asobject;
303 * Prints a list of all currently loaded framework modules
305 * @param bool Return the data as an array?
307 * @return mixed HTML output or an array of loaded modules
309 function show_modules($return = false
)
311 foreach ($this->modules
AS $object)
313 $modules[] = get_class($object);
322 $output = "\n\n<ul>\n\t<li>";
323 $output .= implode("</li>\n\t<li>", $modules);
324 $output .= "</li>\n</ul>\n\n";
325 $this->_message('Loaded Modules', $output, 1);
330 * Verifies to see if a framework has been loaded
332 * @param string Framework name
334 * @return bool Whether or not the framework has been loaded
336 function is_loaded($framework)
338 if (isset($this->modules
["$framework"]))
349 * Prints an ISSO message
351 * @param string The title of the message
352 * @param string The content of the message
353 * @param integer Type of message to be printed
354 * @param bool Return the output?
356 * @return mixed Output or null
358 function _message($title, $message, $type, $return = false
)
383 $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;\">";
384 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
385 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
398 * Custom error handler for ISSO
399 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
401 * @param integer Error number
402 * @param string Error message string
403 * @param string File that contains the error
404 * @param string The line number of the error
405 * @param string The active symbol table at which point the error occurred
407 function _error_handler($errno, $errstr, $errfile, $errline)
415 if (!(ini_get('error_reporting') & E_USER_ERROR
))
425 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
436 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
443 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
445 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
447 $this->_message($title, $errstr, $level);
449 if ($errno == E_USER_ERROR
)
456 * Logs a debug message for verbose output
458 * @param string Message
460 function debug($message)
462 $this->debuginfo
[] = $message;
466 * Recursive XSS cleaner
468 * @param mixed Unsanitized REQUEST data
470 * @return mixed Sanitized data
472 function _sanitize_input_recursive($data)
474 foreach ($data AS $key => $value)
476 if (is_array($value))
478 $data["$key"] = $this->_sanitize_input_recursive($value);
482 if ($this->escapestrings
)
484 $data["$key"] = $this->escape($this->sanitize($value), false
, false
);
488 $data["$key"] = $this->sanitize($value);
496 * Simple way to protect against HTML attacks with Unicode support
498 * @param string Unsanitzed text
500 * @return string Properly protected text that only encodes potential threats
502 function sanitize($text)
504 if ($this->magicquotes
)
506 return str_replace(array('<', '>', '\"', '"'), array('<', '>', '"', '"'), $text);
510 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
515 * Takes text that has been processed for HTML and unsanitizes it
517 * @param string Text that needs to be turned back into HTML
518 * @param bool Force magicquotes off
520 * @return string Unsanitized text
522 function unsanitize($text, $force = false
)
524 if ($this->magicquotes
AND !$force)
526 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
530 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
535 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
537 * @param string Some string
538 * @param bool If the data is binary; if so it'll be run through DB::escape_stringing()
539 * @param bool Force magic quotes to be off
541 * @return string String that has slashes added
543 function escape($str, $binary = false
, $force = true
)
545 if ($this->magicquotes
AND !$force)
547 if (isset($this->db
) AND $binary)
549 if (is_resource($this->db
->link_id
))
551 return $this->db
->escape_string(stripslashes($str));
558 if (isset($this->db
) AND $binary)
560 if (is_resource($this->db
->link_id
))
562 return $this->db
->escape_string($str);
565 return addslashes($str);
570 * Runs through all of the input data and sanitizes it.
572 function exec_sanitize_data()
574 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
575 // we're now using magic quotes
576 if ($this->escapestrings
)
578 $this->magicquotes
= 1;
583 * Sanitize function for something other than a string (which everything is sanitized for if you use exec_sanitize_data().
584 * Cleaned data is placed back into $isso->in; this makes it so you don't have to constantly intval() [etc.] data
586 * @param array Array of elements to clean as varname => type
588 function input_clean_array($vars)
590 foreach ($vars AS $varname => $type)
592 $this->input_clean($varname, $type);
597 * Sanitize function that does a single variable as oppoesd to an array (see input_clean_array() for more details)
599 * @param string Variable name in $isso->in[]
600 * @param integer Sanitization type constant
602 function input_clean($varname, $type)
604 $this->in
["$varname"] = $this->clean($this->in
["$varname"], $type);
608 * Cleaning function that does the work for input_clean(); this is moved here so it can be used to clean things that aren't in $isso->in[]
611 * @param integer Sanitization type constant
613 * @return mixed Cleaned data
615 function clean($value, $type)
617 if ($type == TYPE_INT
)
619 $value = intval($value);
621 else if ($type == TYPE_UINT
)
623 $value = abs(intval($value));
625 else if ($type == TYPE_FLOAT
)
627 $value = floatval($value);
629 else if ($type == TYPE_BOOL
)
631 $value = (bool
)$value;
633 else if ($type == TYPE_STR
)
635 if (!$this->escapestrings
)
637 $value = $this->escape($value);
640 else if ($type == TYPE_STRUN
)
642 $value = $this->unsanitize($value);
644 else if ($type == TYPE_NOCLEAN
)
649 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR
);
656 * Checks to see if a POST refer is actually from us
658 function exec_referer_check()
660 if ($_SERVER['REQUEST_METHOD'] == 'POST')
662 $host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
664 if ($host AND $_SERVER['HTTP_REFERER'])
666 $parts = parse_url($_SERVER['HTTP_REFERER']);
667 $ourhost = $parts['host'] . (($parts['port']) ?
":$parts[port]" : '');
669 if ($ourhost != $host)
671 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
673 $this->debug('remote post check = ok');
677 $this->debug('remote post check = FAILED');
683 /*=====================================================================*\
684 || ###################################################################
687 || ###################################################################
688 \*=====================================================================*/