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;
218 function Shared_Object_Framework()
221 set_error_handler(array(&$this, '_error_handler'));
224 $this->magicquotes
= get_magic_quotes_gpc();
225 set_magic_quotes_runtime(0);
227 if (defined('ISSO_ESCAPE_STRINGS'))
229 $this->escapestrings
= (bool)constant('ISSO_ESCAPE_STRINGS');
232 // start input sanitize using variable_order GPC
233 if (!$this->escapestrings
)
235 $this->exec_sanitize_data();
238 if (defined('ISSO_CHECK_POST_REFERER'))
240 $this->exec_referer_check();
245 * Prepares a path for being set as the sourcepath
247 * @param string Source path or URL
249 * @return string Prepared source path
251 function fetch_sourcepath($source)
253 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
255 $source .= DIRECTORY_SEPARATOR
;
261 * Loads a framework module
263 * @param string Name of the framework file to load
264 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
265 * @param bool Globalize the internal variable?
267 * @return object Instantiated instance
269 function &load($framework, $asobject, $globalize = false)
271 // set the object interlock
272 if ($GLOBALS['isso:null-framework'] === null)
274 $GLOBALS['isso:null-framework'] =& $this;
277 if ($this->is_loaded($framework))
279 return $this->getobj($framework);
282 if ($this->sourcepath
== '')
284 trigger_error('Invalid sourcepath specified', E_USER_ERROR
);
287 if (file_exists($this->sourcepath
. $framework . '.php'))
289 require_once($this->sourcepath
. $framework . '.php');
293 trigger_error('Could not find the framework ' . $this->sourcepath
. $framework . '.php', E_USER_ERROR
);
296 if ($asobject === null)
301 if (isset($this->$asobject))
303 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR
);
306 $this->$asobject = new $framework($this);
308 $this->modules
["$framework"] =& $this->$asobject;
312 $GLOBALS["$asobject"] =& $this->$asobject;
315 return $this->$asobject;
319 * Prints a list of all currently loaded framework modules
321 * @param bool Return the data as an array?
323 * @return mixed HTML output or an array of loaded modules
325 function show_modules($return = false)
327 foreach ($this->modules
AS $object)
329 $modules[] = get_class($object);
338 $output = "\n\n<ul>\n\t<li>";
339 $output .= implode("</li>\n\t<li>", $modules);
340 $output .= "</li>\n</ul>\n\n";
341 $this->_message('Loaded Modules', $output, 1);
346 * Verifies to see if a framework has been loaded
348 * @param string Framework name
350 * @return bool Whether or not the framework has been loaded
352 function is_loaded($framework)
354 if (isset($this->modules
["$framework"]))
365 * Prints an ISSO message
367 * @param string The title of the message
368 * @param string The content of the message
369 * @param integer Type of message to be printed
370 * @param bool Return the output?
372 * @return mixed Output or null
374 function _message($title, $message, $type, $return = false)
399 $backtrace = debug_backtrace();
400 unset($backtrace[0]);
402 $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
;\">";
403 $output .= "\n
<tr style
=\"color
: $font; text
-align
: left\"
>\n\t<td
><strong
>$prefix: $title</strong
></td
>\n</tr
>";
404 $output .= "\n
<tr style
=\"background
-color
: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
405 $output .= "\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>";
406 $output .= "\n</table>\n<br />\n";
419 * Custom error handler for ISSO
420 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
422 * @param integer Error number
423 * @param string Error message string
424 * @param string File that contains the error
425 * @param string The line number of the error
426 * @param string The active symbol table at which point the error occurred
428 function _error_handler($errno, $errstr, $errfile, $errline)
436 if (!(ini_get('error_reporting') & E_USER_ERROR
))
446 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
457 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
464 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
466 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
468 $this->_message($title, $errstr, $level);
470 if ($errno == E_USER_ERROR
)
477 * Logs a debug message for verbose output
479 * @param string Message
481 function debug($message)
483 $this->debuginfo
[] = $message;
487 * Recursive XSS cleaner
489 * @param mixed Unsanitized REQUEST data
491 * @return mixed Sanitized data
493 function _sanitize_input_recursive($data)
495 foreach ($data AS $key => $value)
497 if (is_array($value))
499 $data["$key"] = $this->_sanitize_input_recursive($value);
503 if ($this->escapestrings)
505 $data["$key"] = $this->escape($this->sanitize($value), false, false);
509 $data["$key"] = $this->sanitize($value);
517 * Simple way to protect against HTML attacks with Unicode support
519 * @param string Unsanitzed text
521 * @return string Properly protected text that only encodes potential threats
523 function sanitize($text)
525 if ($this->magicquotes)
527 return str_replace(array('<', '>', '\"', '"'), array('<
;', '>
;', '"
;', '"
;'), $text);
531 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
536 * Unicode-safe entity encoding system; similar to sanitize()
538 * @param string Unsanitized text
540 * @return string Unicode-safe sanitized text with entities preserved
542 function entity_encode($text)
544 $text = str_replace('&', '&', $text);
545 $text = $this->sanitize($text);
550 * Takes text that has been processed for HTML and unsanitizes it
552 * @param string Text that needs to be turned back into HTML
553 * @param bool Force magicquotes off
555 * @return string Unsanitized text
557 function unsanitize($text, $force = false)
559 if ($this->magicquotes AND !$force)
561 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
565 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
570 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
572 * @param string Some string
573 * @param bool If the data is binary; if so it'll be run through DB
::escape_stringing()
574 * @param
bool Force magic quotes to be off
576 * @return string String that has slashes added
578 function escape($str, $binary = false, $force = true)
580 if ($this->magicquotes
AND !$force)
582 if (isset($this->db
) AND $binary)
584 if (is_resource($this->db
->link_id
))
586 return $this->db
->escape_string(stripslashes($str));
593 if (isset($this->db
) AND $binary)
595 if (is_resource($this->db
->link_id
))
597 return $this->db
->escape_string($str);
600 return addslashes($str);
605 * Runs through all of the input data and sanitizes it.
607 function exec_sanitize_data()
609 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
610 // we're now using magic quotes
611 if ($this->escapestrings
)
613 $this->magicquotes
= 1;
618 * Sanitize function for something other than a string (which everything is sanitized for if you use exec_sanitize_data().
619 * Cleaned data is placed back into $isso->in; this makes it so you don't have to constantly intval() [etc.] data
621 * @param array Array of elements to clean as varname => type
623 function input_clean_array($vars)
625 foreach ($vars AS $varname => $type)
627 $this->input_clean($varname, $type);
632 * Sanitize function that does a single variable as oppoesd to an array (see input_clean_array() for more details)
634 * @param string Variable name in $isso->in[]
635 * @param integer Sanitization type constant
637 function input_clean($varname, $type)
639 $this->in
["$varname"] = $this->clean($this->in["$varname"], $type);
643 * 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[]
646 * @param integer Sanitization type constant
648 * @return mixed Cleaned data
650 function clean($value, $type)
652 if ($type == TYPE_INT
)
654 $value = intval($value);
656 else if ($type == TYPE_UINT
)
658 $value = abs(intval($value));
660 else if ($type == TYPE_FLOAT
)
662 $value = floatval($value);
664 else if ($type == TYPE_BOOL
)
666 $value = (bool)$value;
668 else if ($type == TYPE_STR
)
670 if (!$this->escapestrings
)
672 $value = $this->escape($value);
675 else if ($type == TYPE_STRUN
)
677 $value = $this->unsanitize($value);
679 else if ($type == TYPE_NOCLEAN
)
681 if ($this->escapestrings
)
683 $value = $this->escape($value);
688 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR
);
695 * Checks to see if a POST refer is actually from us
697 function exec_referer_check()
699 if ($_SERVER['REQUEST_METHOD'] == 'POST')
701 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
703 if ($host AND $_SERVER['HTTP_REFERER'])
705 $parts = parse_url($_SERVER['HTTP_REFERER']);
706 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
708 if ($ourhost != $host)
710 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
712 $this->debug('remote post check = ok');
716 $this->debug('remote post check = FAILED');
722 /*=====================================================================*\
723 || ###################################################################
726 || ###################################################################
727 \*=====================================================================*/