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
113 * @see fetch_sourcepath(), load(), locate()
115 var $sourcepath = '';
118 * Path of the current application
124 * Name of the current application
127 var $application = '';
130 * Version of the current application
133 var $appversion = '';
136 * Whether debug mode is on or off
142 * List of all active debug messages
146 var $debuginfo = array();
149 * List of loaded modules
151 * @see load(), show_modules()
153 var $modules = array();
156 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
158 * @see sanitize(), exec_sanitize_data()
163 * If we are running with magic_quotes_gpc on or off
165 * @see escape(), exec_sanitize_data()
167 var $magicquotes = 0;
170 * If we should automagically escape strings, mimicking magic_quotes_gpc
172 * @see exec_sanitize_data()
174 var $escapestrings = false
;
179 function Shared_Object_Framework()
182 set_error_handler(array(&$this, '_error_handler'));
185 $this->magicquotes
= get_magic_quotes_gpc();
186 set_magic_quotes_runtime(0);
188 if (defined('ISSO_ESCAPE_STRINGS'))
190 $this->escapestrings
= (bool
)constant('ISSO_ESCAPE_STRINGS');
193 // start input sanitize using variable_order GPC
194 if (!$this->escapestrings
)
196 $this->exec_sanitize_data();
199 $this->modules
['kernel'] = 'Shared Object Framework Core';
203 * Prepares a path for being set as the sourcepath
205 * @param str Source path or URL
207 * @return str Prepared source path
209 function fetch_sourcepath($source)
211 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR
)
213 $source .= DIRECTORY_SEPARATOR
;
219 * Loads a framework extension
221 * @param str Name of the framework
223 function load($framework)
225 if (!$this->is_loaded($framework))
227 $newobj = $this->locate($framework);
228 $this->$newobj['OBJ'] = new $newobj['CLASS']();
229 $GLOBALS["$newobj[OBJ]"] =& $this->$newobj['OBJ'];
230 $this->modules
["$framework"] = $newobj['OBJECT'];
235 * Includes a framework module. Module definitions need three variables:
236 * class, object, and obj. Class is the name of the class, object is
237 * the name human-readable name, and obj is the name that the module
238 * should be initialized as; this is used in class extensions.
240 * @param str Name of the framework
242 * @return array List of initialization variables
244 function locate($framework)
246 if ($this->sourcepath
== '')
248 trigger_error('Invalid sourcepath specified', E_USER_ERROR
);
251 if (file_exists($this->sourcepath
. $framework . '.php'))
253 require_once($this->sourcepath
. $framework . '.php');
254 return array('CLASS' => $CLASS, 'OBJECT' => $OBJECT, 'OBJ' => $OBJ);
258 trigger_error('Could not find the framework ' . $this->sourcepath
. $framework . '.php', E_USER_ERROR
);
264 * Prints a list of all currently loaded framework modules
266 * @param bool Return the data as an array?
268 * @return mixed HTML output or an array of loaded modules
270 function show_modules($return = false
)
274 return $this->modules
;
278 $output = "\n\n<ul>\n\t<li>";
279 $output .= implode("</li>\n\t<li>", $this->modules
);
280 $output .= "</li>\n</ul>\n\n";
281 $this->_message('Loaded Modules', $output, 1);
286 * Verifies to see if a framework has been loaded
288 * @param str Framework name
290 * @return bool Whether or not the framework has been loaded
292 function is_loaded($framework)
294 if (isset($this->modules
["$framework"]))
305 * Prints an ISSO message
307 * @param str The title of the message
308 * @param str The content of the message
309 * @param int Type of message to be printed
310 * @param bool Return the output?
312 * @return mixed Output or null
314 function _message($title, $message, $type, $return = false
)
339 $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;\">";
340 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
341 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
354 * Custom error handler for ISSO
355 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
357 * @param int Error number
358 * @param str Error message string
359 * @param str File that contains the error
360 * @param str The line number of the error
361 * @param str The active symbol table at which point the error occurred
363 function _error_handler($errno, $errstr, $errfile, $errline)
370 if (!(ini_get('error_reporting') & E_USER_ERROR
))
379 if (!(ini_get('error_reporting') & E_USER_WARNING
) AND !(ini_get('error_reporting') & E_WARNING
))
389 if (!(ini_get('error_reporting') & E_USER_NOTICE
) AND !(ini_get('error_reporting') & E_NOTICE
))
396 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
398 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
400 $this->_message($title, $errstr, 3);
402 if ($errno == E_USER_ERROR
)
409 * Logs a debug message for verbose output
413 function debug($message)
415 $this->debuginfo
[] = $message;
419 * Recursive XSS cleaner
421 * @param mixed Unsanitized REQUEST data
423 * @return mixed Sanitized data
425 function _sanitize_input_recursive($data)
427 foreach ($data AS $key => $value)
429 if (is_array($value))
431 $data["$key"] = $this->_sanitize_input_recursive($value);
435 if ($this->escapestrings
)
437 $data["$key"] = $this->escape($this->sanitize($value), false
, false
);
441 $data["$key"] = $this->sanitize($value);
449 * Simple way to protect against HTML attacks with Unicode support
451 * @param str Unsanitzed text
453 * @return str Properly protected text that only encodes potential threats
455 function sanitize($text)
457 if ($this->magicquotes
)
459 return str_replace(array('<', '>', '\"', '"'), array('<', '>', '"', '"'), $text);
463 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
468 * Takes text that has been processed for HTML and unsanitizes it
470 * @param str Text that needs to be turned back into HTML
471 * @param bool Force magicquotes off
473 * @return str Unsanitized text
475 function unsanitize($text, $force = false
)
477 if ($this->magicquotes
AND !$force)
479 return str_replace(array('<', '>', '"'), array('<', '>', '\"'), $text);
483 return str_replace(array('<', '>', '"'), array('<', '>', '"'), $text);
488 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
490 * @param str Some string
491 * @param bool If the data is binary; if so it'll be run through DB::escape_string()
492 * @param bool Force magic quotes to be off
494 * @return str String that has slashes added
496 function escape($str, $binary = false
, $force = true
)
500 if ($this->magicquotes
AND !$force)
502 if (isset($_isso->db
) AND $binary)
504 if (is_resource($_isso->db
->link_id
))
506 return $_isso->db
->escape_string(stripslashes($str));
513 if (isset($_isso->db
) AND $binary)
515 if (is_resource($_isso->db
->link_id
))
517 return $_isso->db
->escape_string($str);
520 return addslashes($str);
525 * Runs through all of the input data and sanitizes it.
527 function exec_sanitize_data()
529 $this->in
= $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
530 // we're now using magic quotes
531 if ($this->escapestrings
)
533 $this->magicquotes
= 1;
539 * Global callback used for module calls back to the kernel
541 $_isso = new Shared_Object_Framework();
544 * Wrapper for ternary operator that has to be in the global scope.
545 * This function is going to be removed from our applications due
546 * to it's latency in processing. This will remain here purely
547 * for legacy reasons. It will be removed when necessary.
549 * @deprecated The normal ternary operators should be used instead; function calls are too expensive
551 * @param expr Expression
552 * @param mixed If the expression is true
553 * @param mixed If the expression is false
555 * @return mixed True or false data
557 function iff($condition, $iftrue, $iffalse = null
)
559 return ($condition) ?
($iftrue) : ($iffalse);
562 if (defined('ISSO_CHECK_POST_REFERER'))
564 if ($_SERVER['REQUEST_METHOD'] == 'POST')
566 $host = ($_SERVER['HTTP_HOST']) ?
$_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
568 if ($host AND $_SERVER['HTTP_REFERER'])
570 $parts = parse_url($_SERVER['HTTP_REFERER']);
571 $ourhost = $parts['host'] . (($parts['port']) ?
":$parts[port]" : '');
573 if ($ourhost != $host)
575 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR
);
577 $_isso->debug('remote post check = ok');
581 $_isso->debug('remote post check = FAILED');
586 /*=====================================================================*\
587 || ###################################################################
590 || ###################################################################
591 \*=====================================================================*/