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)
90 * Input cleaning type constant
95 define('TYPE_INT', 1);
100 define('TYPE_UINT', 2);
105 define('TYPE_FLOAT', 4);
110 define('TYPE_BOOL', 8);
115 define('TYPE_STR', 16);
118 * String - deliberate unclean
120 define('TYPE_STRUN', 32);
123 * No cleaning - here for use in API
125 define('TYPE_NOCLEAN', 64);
129 * Iris Studios Shared Object Framework (ISSO)
131 * This framework allows a common backend to be used amongst all Iris
132 * Studios applications and is built to be abstract and flexible.
133 * The base framework handles all loading and module management.
135 * @author Iris Studios, Inc.
136 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
137 * @version $Revision$
141 class Shared_Object_Framework
147 var $version = '[#]version[#]';
150 * Location of ISSO, used for internal linking
153 var $sourcepath = '';
156 * Path of the current application
162 * Name of the current application
165 var $application = '';
168 * Version of the current application
171 var $appversion = '';
174 * Whether debug mode is on or off
180 * List of all active debug messages
183 var $debuginfo = array();
186 * List of loaded modules
189 var $modules = array();
192 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
198 * If we are running with magic_quotes_gpc on or off
201 var $magicquotes = 0;
204 * If we should automagically escape strings, mimicking magic_quotes_gpc
207 var $escapestrings = false;
212 function Shared_Object_Framework()
215 set_error_handler(array(&$this, '_error_handler'));
218 $this->magicquotes
= get_magic_quotes_gpc();
219 set_magic_quotes_runtime(0);
221 if (defined('ISSO_ESCAPE_STRINGS'))
223 $this->escapestrings
= (bool)constant('ISSO_ESCAPE_STRINGS');
226 // start input sanitize using variable_order GPC
227 if (!$this->escapestrings
)
229 $this->exec_sanitize_data();
232 if (defined('ISSO_CHECK_POST_REFERER'))
234 $this->exec_referer_check();
239 * Prepares a path for being set as the sourcepath
241 * @param string Source path or URL
243 * @return string Prepared source path
245 function fetch_sourcepath($source)
247 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
249 $source .= DIRECTORY_SEPARATOR
;
255 * Loads a framework module
257 * @param string Name of the framework file to load
258 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
259 * @param bool Globalize the internal variable?
261 * @return object Instantiated instance
263 function &load($framework, $asobject, $globalize = false)
265 if ($this->is_loaded($framework))
267 return $this->getobj($framework);
270 if ($this->sourcepath
== '')
272 trigger_error('Invalid sourcepath specified', E_USER_ERROR
);
275 if (file_exists($this->sourcepath
. $framework . '.php'))
277 require_once($this->sourcepath
. $framework . '.php');
281 trigger_error('Could not find the framework ' . $this->sourcepath
. $framework . '.php', E_USER_ERROR
);
284 if ($asobject === null)
289 if (isset($this->$asobject))
291 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR
);
294 $this->$asobject = new $framework($this);
296 $this->modules
["$framework"] =& $this->$asobject;
300 $GLOBALS["$asobject"] =& $this->$asobject;
303 return $this->$asobject;
307 * Prints a list of all currently loaded framework modules
309 * @param bool Return the data as an array?
311 * @return mixed HTML output or an array of loaded modules
313 function show_modules($return = false)
315 foreach ($this->modules
AS $object)
317 $modules[] = get_class($object);
326 $output = "\n\n<ul>\n\t<li>";
327 $output .= implode("</li>\n\t<li>", $modules);
328 $output .= "</li>\n</ul>\n\n";
329 $this->_message('Loaded Modules', $output, 1);
334 * Verifies to see if a framework has been loaded
336 * @param string Framework name
338 * @return bool Whether or not the framework has been loaded
340 function is_loaded($framework)
342 if (isset($this->modules
["$framework"]))
353 * Prints an ISSO message
355 * @param string The title of the message
356 * @param string The content of the message
357 * @param integer Type of message to be printed
358 * @param bool Return the output?
360 * @return mixed Output or null
362 function _message($title, $message, $type, $return = false)
387 $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
;\">";
388 $output .= "\n
<tr style
=\"color
: $font; text
-align
: left\"
>\n\t<td
><strong
>$prefix: $title</strong
></td
>\n</tr
>";
389 $output .= "\n
<tr style
=\"background
-color
: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
402 * Custom error handler for ISSO
403 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
405 * @param integer Error number
406 * @param string Error message string
407 * @param string File that contains the error
408 * @param string The line number of the error
409 * @param string The active symbol table at which point the error occurred
411 function _error_handler($errno, $errstr, $errfile, $errline)
419 if (!(ini_get('error_reporting') & E_USER_ERROR
))
429 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
440 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
447 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
449 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
451 $this->_message($title, $errstr, $level);
453 if ($errno == E_USER_ERROR
)
460 * Logs a debug message for verbose output
462 * @param string Message
464 function debug($message)
466 $this->debuginfo
[] = $message;
470 * Recursive XSS cleaner
472 * @param mixed Unsanitized REQUEST data
474 * @return mixed Sanitized data
476 function _sanitize_input_recursive($data)
478 foreach ($data AS $key => $value)
480 if (is_array($value))
482 $data["$key"] = $this->_sanitize_input_recursive($value);
486 if ($this->escapestrings)
488 $data["$key"] = $this->escape($this->sanitize($value), false, false);
492 $data["$key"] = $this->sanitize($value);
500 * Simple way to protect against HTML attacks with Unicode support
502 * @param string Unsanitzed text
504 * @return string Properly protected text that only encodes potential threats
506 function sanitize($text)
508 if ($this->magicquotes)
510 return str_replace(array('<', '>', '\"', '"'), array('<
;', '>
;', '"
;', '"
;'), $text);
514 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
519 * Takes text that has been processed for HTML and unsanitizes it
521 * @param string Text that needs to be turned back into HTML
522 * @param bool Force magicquotes off
524 * @return string Unsanitized text
526 function unsanitize($text, $force = false)
528 if ($this->magicquotes AND !$force)
530 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
534 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
539 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
541 * @param string Some string
542 * @param bool If the data is binary; if so it'll be run through DB
::escape_stringing()
543 * @param
bool Force magic quotes to be off
545 * @return string String that has slashes added
547 function escape($str, $binary = false, $force = true)
549 if ($this->magicquotes
AND !$force)
551 if (isset($this->db
) AND $binary)
553 if (is_resource($this->db
->link_id
))
555 return $this->db
->escape_string(stripslashes($str));
562 if (isset($this->db
) AND $binary)
564 if (is_resource($this->db
->link_id
))
566 return $this->db
->escape_string($str);
569 return addslashes($str);
574 * Runs through all of the input data and sanitizes it.
576 function exec_sanitize_data()
578 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
579 // we're now using magic quotes
580 if ($this->escapestrings
)
582 $this->magicquotes
= 1;
587 * Sanitize function for something other than a string (which everything is sanitized for if you use exec_sanitize_data().
588 * Cleaned data is placed back into $isso->in; this makes it so you don't have to constantly intval() [etc.] data
590 * @param array Array of elements to clean as varname => type
592 function input_clean_array($vars)
594 foreach ($vars AS $varname => $type)
596 $this->input_clean($varname, $type);
601 * Sanitize function that does a single variable as oppoesd to an array (see input_clean_array() for more details)
603 * @param string Variable name in $isso->in[]
604 * @param integer Sanitization type constant
606 function input_clean($varname, $type)
608 $this->in
["$varname"] = $this->clean($this->in["$varname"], $type);
612 * 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[]
615 * @param integer Sanitization type constant
617 * @return mixed Cleaned data
619 function clean($value, $type)
621 if ($type == TYPE_INT
)
623 $value = intval($value);
625 else if ($type == TYPE_UINT
)
627 $value = abs(intval($value));
629 else if ($type == TYPE_FLOAT
)
631 $value = floatval($value);
633 else if ($type == TYPE_BOOL
)
635 $value = (bool)$value;
637 else if ($type == TYPE_STR
)
639 if (!$this->escapestrings
)
641 $value = $this->escape($value);
644 else if ($type == TYPE_STRUN
)
646 $value = $this->unsanitize($value);
648 else if ($type == TYPE_NOCLEAN
)
653 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR
);
660 * Checks to see if a POST refer is actually from us
662 function exec_referer_check()
664 if ($_SERVER['REQUEST_METHOD'] == 'POST')
666 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
668 if ($host AND $_SERVER['HTTP_REFERER'])
670 $parts = parse_url($_SERVER['HTTP_REFERER']);
671 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
673 if ($ourhost != $host)
675 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
677 $this->debug('remote post check = ok');
681 $this->debug('remote post check = FAILED');
687 /*=====================================================================*\
688 || ###################################################################
691 || ###################################################################
692 \*=====================================================================*/