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 * Iris Studios Shared Object Framework (ISSO)
92 * This framework allows a common backend to be used amongst all Iris
93 * Studios applications and is built to be abstract and flexible.
94 * The base framework handles all loading and module management.
96 * @author Iris Studios, Inc.
97 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
102 class Shared_Object_Framework
108 var $version = '[#]version[#]';
111 * Location of ISSO, used for internal linking
114 var $sourcepath = '';
117 * Path of the current application
123 * Name of the current application
126 var $application = '';
129 * Version of the current application
132 var $appversion = '';
135 * Whether debug mode is on or off
141 * List of all active debug messages
144 var $debuginfo = array();
147 * List of loaded modules
150 var $modules = array();
153 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
159 * If we are running with magic_quotes_gpc on or off
162 var $magicquotes = 0;
165 * If we should automagically escape strings, mimicking magic_quotes_gpc
168 var $escapestrings = false;
173 function Shared_Object_Framework()
176 set_error_handler(array(&$this, '_error_handler'));
179 $this->magicquotes
= get_magic_quotes_gpc();
180 set_magic_quotes_runtime(0);
182 if (defined('ISSO_ESCAPE_STRINGS'))
184 $this->escapestrings
= (bool)constant('ISSO_ESCAPE_STRINGS');
187 // start input sanitize using variable_order GPC
188 if (!$this->escapestrings
)
190 $this->exec_sanitize_data();
193 if (defined('ISSO_CHECK_POST_REFERER'))
195 $this->exec_referer_check();
198 $this->modules
['kernel'] = 'Shared Object Framework Core';
202 * Prepares a path for being set as the sourcepath
204 * @param string Source path or URL
206 * @return string Prepared source path
208 function fetch_sourcepath($source)
210 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
212 $source .= DIRECTORY_SEPARATOR
;
218 * Loads a framework module
220 * @param string Name of the framework file to load
221 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
222 * @param bool Globalize the internal variable?
224 * @return object Instantiated instance
226 function &load($framework, $asobject, $globalize = false)
228 if ($this->is_loaded($framework))
230 return $this->getobj($framework);
233 if ($this->sourcepath
== '')
235 trigger_error('Invalid sourcepath specified', E_USER_ERROR
);
238 if (file_exists($this->sourcepath
. $framework . '.php'))
240 require_once($this->sourcepath
. $framework . '.php');
244 trigger_error('Could not find the framework ' . $this->sourcepath
. $framework . '.php', E_USER_ERROR
);
247 if ($asobject === null)
252 $this->modules
["$framework"] = $asobject;
254 if (isset($this->$asobject))
256 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
259 $this->$asobject = new $framework($this);
263 $GLOBALS["$asobject"] =& $this->$asobject;
266 return $this->$asobject;
270 * Returns the instance of a loaded module
272 * @param string Framework name
274 * @return object Instantiated instance
276 function &getobject($framework)
278 if (!$this->is_loaded($framework))
280 trigger_error('Framework module `' . $framework . '` is not loaded', E_USER_WARNING
);
283 return $this->{$this
->modules
["$framework"]};
287 * Prints a list of all currently loaded framework modules
289 * @param bool Return the data as an array?
291 * @return mixed HTML output or an array of loaded modules
293 function show_modules($return = false)
297 return $this->modules
;
301 $output = "\n\n<ul>\n\t<li>";
302 $output .= implode("</li>\n\t<li>", $this->modules
);
303 $output .= "</li>\n</ul>\n\n";
304 $this->_message('Loaded Modules', $output, 1);
309 * Verifies to see if a framework has been loaded
311 * @param string Framework name
313 * @return bool Whether or not the framework has been loaded
315 function is_loaded($framework)
317 if (isset($this->modules
["$framework"]))
328 * Prints an ISSO message
330 * @param string The title of the message
331 * @param string The content of the message
332 * @param integer Type of message to be printed
333 * @param bool Return the output?
335 * @return mixed Output or null
337 function _message($title, $message, $type, $return = false)
362 $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
;\">";
363 $output .= "\n
<tr style
=\"color
: $font; text
-align
: left\"
>\n\t<td
><strong
>$prefix: $title</strong
></td
>\n</tr
>";
364 $output .= "\n
<tr style
=\"background
-color
: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
377 * Custom error handler for ISSO
378 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
380 * @param integer Error number
381 * @param string Error message string
382 * @param string File that contains the error
383 * @param string The line number of the error
384 * @param string The active symbol table at which point the error occurred
386 function _error_handler($errno, $errstr, $errfile, $errline)
394 if (!(ini_get('error_reporting') & E_USER_ERROR
))
404 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
415 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
422 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
424 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
426 $this->_message($title, $errstr, $level);
428 if ($errno == E_USER_ERROR
)
435 * Logs a debug message for verbose output
437 * @param string Message
439 function debug($message)
441 $this->debuginfo
[] = $message;
445 * Recursive XSS cleaner
447 * @param mixed Unsanitized REQUEST data
449 * @return mixed Sanitized data
451 function _sanitize_input_recursive($data)
453 foreach ($data AS $key => $value)
455 if (is_array($value))
457 $data["$key"] = $this->_sanitize_input_recursive($value);
461 if ($this->escapestrings)
463 $data["$key"] = $this->escape($this->sanitize($value), false, false);
467 $data["$key"] = $this->sanitize($value);
475 * Simple way to protect against HTML attacks with Unicode support
477 * @param string Unsanitzed text
479 * @return string Properly protected text that only encodes potential threats
481 function sanitize($text)
483 if ($this->magicquotes)
485 return str_replace(array('<', '>', '\"', '"'), array('<
;', '>
;', '"
;', '"
;'), $text);
489 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
494 * Takes text that has been processed for HTML and unsanitizes it
496 * @param string Text that needs to be turned back into HTML
497 * @param bool Force magicquotes off
499 * @return string Unsanitized text
501 function unsanitize($text, $force = false)
503 if ($this->magicquotes AND !$force)
505 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
509 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
514 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
516 * @param string Some string
517 * @param bool If the data is binary; if so it'll be run through DB
::escape_stringing()
518 * @param
bool Force magic quotes to be off
520 * @return string String that has slashes added
522 function escape($str, $binary = false, $force = true)
524 if ($this->magicquotes
AND !$force)
526 if (isset($this->db
) AND $binary)
528 if (is_resource($this->db
->link_id
))
530 return $this->db
->escape_string(stripslashes($str));
537 if (isset($this->db
) AND $binary)
539 if (is_resource($this->db
->link_id
))
541 return $this->db
->escape_string($str);
544 return addslashes($str);
549 * Runs through all of the input data and sanitizes it.
551 function exec_sanitize_data()
553 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
554 // we're now using magic quotes
555 if ($this->escapestrings
)
557 $this->magicquotes
= 1;
562 * Checks to see if a POST refer is actually from us
564 function exec_referer_check()
566 if ($_SERVER['REQUEST_METHOD'] == 'POST')
568 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
570 if ($host AND $_SERVER['HTTP_REFERER'])
572 $parts = parse_url($_SERVER['HTTP_REFERER']);
573 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
575 if ($ourhost != $host)
577 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
579 $this->debug('remote post check = ok');
583 $this->debug('remote post check = FAILED');
589 /*=====================================================================*\
590 || ###################################################################
593 || ###################################################################
594 \*=====================================================================*/