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 * Web path used to get the web location of the installation of ISSO; only used for Printer module
168 * Name of the current application
171 var $application = '';
174 * Version of the current application
177 var $appversion = '';
180 * Whether debug mode is on or off
186 * List of all active debug messages
189 var $debuginfo = array();
192 * List of loaded modules
195 var $modules = array();
198 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
204 * If we are running with magic_quotes_gpc on or off
207 var $magicquotes = 0;
210 * If we should automagically escape strings, mimicking magic_quotes_gpc
213 var $escapestrings = false
;
215 // ###################################################################
219 function __construct()
222 set_error_handler(array(&$this, '_error_handler'));
225 $this->magicquotes
= get_magic_quotes_gpc();
226 set_magic_quotes_runtime(0);
228 if (defined('ISSO_ESCAPE_STRINGS'))
230 $this->escapestrings
= (bool
)constant('ISSO_ESCAPE_STRINGS');
233 // start input sanitize using variable_order GPC
234 if (!$this->escapestrings
)
236 $this->exec_sanitize_data();
239 if (defined('ISSO_CHECK_POST_REFERER'))
241 $this->exec_referer_check();
244 $GLOBALS['isso:null-framework'] = null
;
247 // ###################################################################
249 * (PHP 4) Constructor
251 function Shared_Object_Framework()
253 $this->__construct();
256 // ###################################################################
258 * Prepares a path for being set as the sourcepath
262 * @param string Source path or URL
264 * @return string Prepared source path
266 function fetch_sourcepath($source)
268 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
270 $source .= DIRECTORY_SEPARATOR
;
275 // ###################################################################
277 * Loads a framework module
281 * @param string Name of the framework file to load
282 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
283 * @param bool Globalize the internal variable?
285 * @return object Instantiated instance
287 function &load($framework, $asobject, $globalize = false
)
289 // set the object interlock
290 if (!method_exists($GLOBALS['isso:null-framework'], 'load'))
292 $GLOBALS['isso:null-framework'] =& $this;
295 if ($this->is_loaded($framework))
297 return $this->modules
["$framework"];
300 if ($this->sourcepath
== '')
302 trigger_error('Invalid sourcepath specified', E_USER_ERROR
);
305 if (file_exists($this->sourcepath
. $framework . '.php'))
307 require_once($this->sourcepath
. $framework . '.php');
311 trigger_error('Could not find the framework ' . $this->sourcepath
. $framework . '.php', E_USER_ERROR
);
314 if ($asobject === null
)
319 if (isset($this->$asobject))
321 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR
);
324 $this->$asobject = new $framework($this);
326 $this->modules
["$framework"] =& $this->$asobject;
330 $GLOBALS["$asobject"] =& $this->$asobject;
333 return $this->$asobject;
336 // ###################################################################
338 * Prints a list of all currently loaded framework modules
342 * @param bool Return the data as an array?
344 * @return mixed HTML output or an array of loaded modules
346 function show_modules($return = false
)
348 foreach ($this->modules
AS $object)
350 $modules[] = get_class($object);
359 $output = "\n\n<ul>\n\t<li>";
360 $output .= implode("</li>\n\t<li>", $modules);
361 $output .= "</li>\n</ul>\n\n";
362 $this->message('Loaded Modules', $output, 1);
366 // ###################################################################
368 * Verifies to see if a framework has been loaded
372 * @param string Framework name
374 * @return bool Whether or not the framework has been loaded
376 function is_loaded($framework)
378 if (isset($this->modules
["$framework"]))
388 // ###################################################################
390 * Prints an ISSO message
394 * @param string The title of the message
395 * @param string The content of the message
396 * @param integer Type of message to be printed
397 * @param bool Return the output?
398 * @param bool Show the debug stack?
400 * @return mixed Output or null
402 function message($title, $message, $type, $return = false
, $stack = true
)
427 $backtrace = debug_backtrace();
428 unset($backtrace[0]);
430 $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;\">";
431 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
432 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
433 $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>" : '');
434 $output .= "\n</table>\n<br />\n";
446 // ###################################################################
448 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
449 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
453 * @param integer Error number
454 * @param string Error message string
455 * @param string File that contains the error
456 * @param string The line number of the error
457 * @param string The active symbol table at which point the error occurred
459 function _error_handler($errno, $errstr, $errfile, $errline)
467 if (!(ini_get('error_reporting') & E_USER_ERROR
))
477 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
488 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
495 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
497 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
499 $this->message($title, $errstr, $level);
501 if ($errno == E_USER_ERROR
)
507 // ###################################################################
509 * Creates a table that explains the error reporting levels and their
514 function explain_error_reporting()
517 'E_ERROR' => E_ERROR
,
518 'E_WARNING' => E_WARNING
,
519 'E_PARSE' => E_PARSE
,
520 'E_NOTICE' => E_NOTICE
,
521 'E_CORE_ERROR' => E_CORE_ERROR
,
522 'E_CORE_WARNING' => E_CORE_WARNING
,
523 'E_COMPILE_ERROR' => 64,
524 'E_COMPILE_WARNING' => 128,
525 'E_USER_ERROR' => E_USER_ERROR
,
526 'E_USER_WARNING' => E_USER_WARNING
,
527 'E_USER_NOTICE' => E_USER_NOTICE
,
532 $table = '<table cellspacing="0" cellpadding="2" border="0">';
534 foreach ($levels AS $name => $value)
538 <td>' . $name . '</td>
539 <td>' . (ini_get('error_reporting') & $value) . '</td>
546 $this->message('Error Reporting', $table, 1);
549 // ###################################################################
551 * Logs a debug message for verbose output
555 * @param string Message
557 function debug($message)
559 $this->debuginfo
[] = $message;
562 // ###################################################################
564 * Recursive XSS cleaner
568 * @param mixed Unsanitized REQUEST data
570 * @return mixed Sanitized data
572 function _sanitize_input_recursive($data)
574 foreach ($data AS $key => $value)
576 if (is_array($value))
578 $data["$key"] = $this->_sanitize_input_recursive($value);
582 if ($this->escapestrings
)
584 $data["$key"] = $this->escape($this->sanitize($value), false
, false
);
588 $data["$key"] = $this->sanitize($value);
595 // ###################################################################
597 * Simple way to protect against HTML attacks with Unicode support
601 * @param string Unsanitzed text
603 * @return string Properly protected text that only encodes potential threats
605 function sanitize($text)
607 if ($this->magicquotes
)
609 return str_replace(array('<', '>', '\"', '"'), array('<', '>', '"', '"'), $text);
613 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
617 // ###################################################################
619 * Unicode-safe entity encoding system; similar to sanitize()
623 * @param string Unsanitized text
625 * @return string Unicode-safe sanitized text with entities preserved
627 function entity_encode($text)
629 $text = str_replace('&', '&', $text);
630 $text = $this->sanitize($text);
634 // ###################################################################
636 * Takes text that has been processed for HTML and unsanitizes it
640 * @param string Text that needs to be turned back into HTML
641 * @param bool Force magicquotes off
643 * @return string Unsanitized text
645 function unsanitize($text, $force = false
)
647 if ($this->magicquotes
AND !$force)
649 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
653 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
657 // ###################################################################
659 * Smart addslashes() that only applies itself it the Magic Quotes GPC
664 * @param string Some string
665 * @param bool If the data is binary; if so it'll be run through DB::escape_stringing()
666 * @param bool Force magic quotes to be off
668 * @return string String that has slashes added
670 function escape($str, $binary = false
, $force = true
)
672 if ($this->magicquotes
AND !$force)
674 if (isset($this->db
) AND $binary)
676 if (is_resource($this->db
->link_id
))
678 return $this->db
->escape_string(stripslashes($str));
685 if (isset($this->db
) AND $binary)
687 if (is_resource($this->db
->link_id
))
689 return $this->db
->escape_string($str);
692 return addslashes($str);
696 // ###################################################################
698 * Runs through all of the input data and sanitizes it.
702 function exec_sanitize_data()
704 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
705 // we're now using magic quotes
706 if ($this->escapestrings
)
708 $this->magicquotes
= 1;
712 // ###################################################################
714 * Sanitize function for something other than a string (which
715 * everything is sanitized for if you use exec_sanitize_data(). Cleaned
716 * data is placed back into $isso->in; this makes it so you don't have
717 * to constantly intval() [etc.] data.
721 * @param array Array of elements to clean as varname => type
723 function input_clean_array($vars)
725 foreach ($vars AS $varname => $type)
727 $this->input_clean($varname, $type);
731 // ###################################################################
733 * Sanitize function that does a single variable as oppoesd to an array
734 * (see input_clean_array() for more details)
738 * @param string Variable name in $isso->in[]
739 * @param integer Sanitization type constant
741 function input_clean($varname, $type)
743 if (isset($this->in
["$varname"]))
745 $this->in
["$varname"] = $this->clean($this->in
["$varname"], $type);
749 $this->in
["$varname"] = null
;
753 // ###################################################################
755 * Cleaning function that does the work for input_clean(); this is
756 * moved here so it can be used to clean things that aren't in
762 * @param integer Sanitization type constant
764 * @return mixed Cleaned data
766 function clean($value, $type)
768 if ($type == TYPE_INT
)
770 $value = intval($value);
772 else if ($type == TYPE_UINT
)
774 $value = abs(intval($value));
776 else if ($type == TYPE_FLOAT
)
778 $value = floatval($value);
780 else if ($type == TYPE_BOOL
)
782 $value = (bool
)$value;
784 else if ($type == TYPE_STR
)
786 if (!$this->escapestrings
)
788 $value = $this->escape($value);
791 else if ($type == TYPE_STRUN
)
793 $value = $this->unsanitize($value);
795 else if ($type == TYPE_NOCLEAN
)
797 if ($this->escapestrings
)
799 $value = $this->escape($value);
804 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR
);
810 // ###################################################################
812 * Checks to see if a POST refer is actually from us
816 function exec_referer_check()
818 if ($_SERVER['REQUEST_METHOD'] == 'POST')
820 $host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
822 if ($host AND $_SERVER['HTTP_REFERER'])
824 $parts = parse_url($_SERVER['HTTP_REFERER']);
825 $ourhost = $parts['host'] . (($parts['port']) ?
":$parts[port]" : '');
827 if ($ourhost != $host)
829 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
831 $this->debug('remote post check = ok');
835 $this->debug('remote post check = FAILED');
841 /*=====================================================================*\
842 || ###################################################################
845 || ###################################################################
846 \*=====================================================================*/