Pass the registry by reference
[isso.git] / kernel.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
6 || #
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.
10 || #
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
14 || # more details.
15 || #
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 \*=====================================================================*/
21
22 /**
23 * Iris Studios Shared Object Framework Kernel
24 * kernel.php
25 *
26 * @package ISSO
27 */
28
29 if (!function_exists('version_compare'))
30 {
31 trigger_error('You need PHP version 4.1.0 or newer to run ISSO', E_USER_ERROR);
32 exit;
33 }
34
35 if (version_compare(PHP_VERSION, '5.0.0', '>='))
36 {
37 if (ini_get('error_reporting') & E_NOTICE)
38 {
39 error_reporting(ini_get('error_reporting') - E_NOTICE);
40 }
41 if (ini_get('error_reporting') & E_USER_NOTICE)
42 {
43 error_reporting(ini_get('error_reporting') - E_USER_NOTICE);
44 }
45 }
46
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)
51 {
52 if ($oldlevel & $php)
53 {
54 if (!($oldlevel & $isso))
55 {
56 //echo "increasing newlevel by $isso; ";
57 $newlevel += $isso;
58 }
59 }
60 else
61 {
62 if ($oldlevel & $isso)
63 {
64 //echo "decreasing newlevel by $isso; ";
65 $newlevel -= $isso;
66 }
67 }
68 }
69 error_reporting($newlevel);
70
71 if ((bool)ini_get('register_globals') === true)
72 {
73 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
74 foreach ($superglobals AS $global)
75 {
76 if (is_array(${$global}))
77 {
78 foreach (${$global} AS $_key => $_val)
79 {
80 if (isset(${$_key}))
81 {
82 unset(${$_key});
83 }
84 }
85 }
86 }
87 }
88
89 /**
90 * Iris Studios Shared Object Framework (ISSO)
91 *
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.
95 *
96 * @author Iris Studios, Inc.
97 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
98 * @version $Revision$
99 * @package ISSO
100 *
101 */
102 class Shared_Object_Framework
103 {
104 /**
105 * ISSO version
106 * @var string
107 */
108 var $version = '[#]version[#]';
109
110 /**
111 * Location of ISSO, used for internal linking
112 * @var string
113 */
114 var $sourcepath = '';
115
116 /**
117 * Path of the current application
118 * @var string
119 */
120 var $apppath = '';
121
122 /**
123 * Name of the current application
124 * @var string
125 */
126 var $application = '';
127
128 /**
129 * Version of the current application
130 * @var string
131 */
132 var $appversion = '';
133
134 /**
135 * Whether debug mode is on or off
136 * @var bool
137 */
138 var $debug = false;
139
140 /**
141 * List of all active debug messages
142 * @var array
143 */
144 var $debuginfo = array();
145
146 /**
147 * List of loaded modules
148 * @var array
149 */
150 var $modules = array();
151
152 /**
153 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
154 * @var array
155 */
156 var $in = array();
157
158 /**
159 * If we are running with magic_quotes_gpc on or off
160 * @var int
161 */
162 var $magicquotes = 0;
163
164 /**
165 * If we should automagically escape strings, mimicking magic_quotes_gpc
166 * @var bool
167 */
168 var $escapestrings = false;
169
170 /**
171 * Constructor
172 */
173 function Shared_Object_Framework()
174 {
175 // error reporting
176 set_error_handler(array(&$this, '_error_handler'));
177
178 // magic quotes
179 $this->magicquotes = get_magic_quotes_gpc();
180 set_magic_quotes_runtime(0);
181
182 if (defined('ISSO_ESCAPE_STRINGS'))
183 {
184 $this->escapestrings = (bool)constant('ISSO_ESCAPE_STRINGS');
185 }
186
187 // start input sanitize using variable_order GPC
188 if (!$this->escapestrings)
189 {
190 $this->exec_sanitize_data();
191 }
192
193 $this->modules['kernel'] = 'Shared Object Framework Core';
194 }
195
196 /**
197 * Prepares a path for being set as the sourcepath
198 *
199 * @param string Source path or URL
200 *
201 * @return string Prepared source path
202 */
203 function fetch_sourcepath($source)
204 {
205 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
206 {
207 $source .= DIRECTORY_SEPARATOR;
208 }
209 return $source;
210 }
211
212 /**
213 * Loads a framework extension
214 *
215 * @param string Name of the framework
216 */
217 function load($framework)
218 {
219 if (!$this->is_loaded($framework))
220 {
221 $newobj = $this->locate($framework);
222 $this->$newobj['OBJ'] = new $newobj['CLASS']($this);
223 $GLOBALS["$newobj[OBJ]"] =& $this->$newobj['OBJ'];
224 $this->modules["$framework"] = $newobj['OBJECT'];
225 }
226 }
227
228 /**
229 * Includes a framework module. Module definitions need three variables:
230 * class, object, and obj. Class is the name of the class, object is
231 * the name human-readable name, and obj is the name that the module
232 * should be initialized as; this is used in class extensions.
233 *
234 * @param string Name of the framework
235 *
236 * @return array List of initialization variables
237 */
238 function locate($framework)
239 {
240 if ($this->sourcepath == '')
241 {
242 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
243 }
244
245 if (file_exists($this->sourcepath . $framework . '.php'))
246 {
247 require_once($this->sourcepath . $framework . '.php');
248 return array('CLASS' => $CLASS, 'OBJECT' => $OBJECT, 'OBJ' => $OBJ);
249 }
250 else
251 {
252 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
253 exit;
254 }
255 }
256
257 /**
258 * Prints a list of all currently loaded framework modules
259 *
260 * @param bool Return the data as an array?
261 *
262 * @return mixed HTML output or an array of loaded modules
263 */
264 function show_modules($return = false)
265 {
266 if ($return)
267 {
268 return $this->modules;
269 }
270 else
271 {
272 $output = "\n\n<ul>\n\t<li>";
273 $output .= implode("</li>\n\t<li>", $this->modules);
274 $output .= "</li>\n</ul>\n\n";
275 $this->_message('Loaded Modules', $output, 1);
276 }
277 }
278
279 /**
280 * Verifies to see if a framework has been loaded
281 *
282 * @param string Framework name
283 *
284 * @return bool Whether or not the framework has been loaded
285 */
286 function is_loaded($framework)
287 {
288 if (isset($this->modules["$framework"]))
289 {
290 return true;
291 }
292 else
293 {
294 return false;
295 }
296 }
297
298 /**
299 * Prints an ISSO message
300 *
301 * @param string The title of the message
302 * @param string The content of the message
303 * @param integer Type of message to be printed
304 * @param bool Return the output?
305 *
306 * @return mixed Output or null
307 */
308 function _message($title, $message, $type, $return = false)
309 {
310 switch ($type)
311 {
312 // Message
313 case 1:
314 $prefix = 'Message';
315 $color = '#669900';
316 $font = '#000000';
317 break;
318
319 // Warning
320 case 2:
321 $prefix = 'Warning';
322 $color = '#003399';
323 $font = '#FFFFFF';
324 break;
325
326 case 3:
327 $prefix = 'Error';
328 $color = '#990000';
329 $font = '#EFEFEF';
330 break;
331 }
332
333 $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;\">";
334 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
335 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
336
337 if ($return)
338 {
339 return $output;
340 }
341 else
342 {
343 print($output);
344 }
345 }
346
347 /**
348 * Custom error handler for ISSO
349 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
350 *
351 * @param integer Error number
352 * @param string Error message string
353 * @param string File that contains the error
354 * @param string The line number of the error
355 * @param string The active symbol table at which point the error occurred
356 */
357 function _error_handler($errno, $errstr, $errfile, $errline)
358 {
359 switch ($errno)
360 {
361 // Fatal
362 case E_USER_ERROR:
363 $title = 'Fatal';
364 if (!(ini_get('error_reporting') & E_USER_ERROR))
365 {
366 return;
367 }
368 break;
369
370 // Error
371 case E_USER_WARNING:
372 $title = 'Warning';
373 if (!(ini_get('error_reporting') & E_USER_WARNING) AND !(ini_get('error_reporting') & E_WARNING))
374 {
375 return;
376 }
377 break;
378
379 // Warning
380 case E_USER_NOTICE:
381 default:
382 $title = 'Notice';
383 if (!(ini_get('error_reporting') & E_USER_NOTICE) AND !(ini_get('error_reporting') & E_NOTICE))
384 {
385 return;
386 }
387 break;
388 }
389
390 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
391
392 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
393
394 $this->_message($title, $errstr, 3);
395
396 if ($errno == E_USER_ERROR)
397 {
398 exit;
399 }
400 }
401
402 /**
403 * Logs a debug message for verbose output
404 *
405 * @param string Message
406 */
407 function debug($message)
408 {
409 $this->debuginfo[] = $message;
410 }
411
412 /**
413 * Recursive XSS cleaner
414 *
415 * @param mixed Unsanitized REQUEST data
416 *
417 * @return mixed Sanitized data
418 */
419 function _sanitize_input_recursive($data)
420 {
421 foreach ($data AS $key => $value)
422 {
423 if (is_array($value))
424 {
425 $data["$key"] = $this->_sanitize_input_recursive($value);
426 }
427 else
428 {
429 if ($this->escapestrings)
430 {
431 $data["$key"] = $this->escape($this->sanitize($value), false, false);
432 }
433 else
434 {
435 $data["$key"] = $this->sanitize($value);
436 }
437 }
438 }
439 return $data;
440 }
441
442 /**
443 * Simple way to protect against HTML attacks with Unicode support
444 *
445 * @param string Unsanitzed text
446 *
447 * @return string Properly protected text that only encodes potential threats
448 */
449 function sanitize($text)
450 {
451 if ($this->magicquotes)
452 {
453 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
454 }
455 else
456 {
457 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
458 }
459 }
460
461 /**
462 * Takes text that has been processed for HTML and unsanitizes it
463 *
464 * @param string Text that needs to be turned back into HTML
465 * @param bool Force magicquotes off
466 *
467 * @return string Unsanitized text
468 */
469 function unsanitize($text, $force = false)
470 {
471 if ($this->magicquotes AND !$force)
472 {
473 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '\"'), $text);
474 }
475 else
476 {
477 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
478 }
479 }
480
481 /**
482 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
483 *
484 * @param string Some string
485 * @param bool If the data is binary; if so it'll be run through DB::escape_stringing()
486 * @param bool Force magic quotes to be off
487 *
488 * @return string String that has slashes added
489 */
490 function escape($str, $binary = false, $force = true)
491 {
492 global $_isso;
493
494 if ($this->magicquotes AND !$force)
495 {
496 if (isset($_isso->db) AND $binary)
497 {
498 if (is_resource($_isso->db->link_id))
499 {
500 return $_isso->db->escape_string(stripslashes($str));
501 }
502 }
503 return $str;
504 }
505 else
506 {
507 if (isset($_isso->db) AND $binary)
508 {
509 if (is_resource($_isso->db->link_id))
510 {
511 return $_isso->db->escape_string($str);
512 }
513 }
514 return addslashes($str);
515 }
516 }
517
518 /**
519 * Runs through all of the input data and sanitizes it.
520 */
521 function exec_sanitize_data()
522 {
523 $this->in = $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
524 // we're now using magic quotes
525 if ($this->escapestrings)
526 {
527 $this->magicquotes = 1;
528 }
529 }
530 }
531
532 /**
533 * Global callback used for module calls back to the kernel
534 */
535 $_isso = new Shared_Object_Framework();
536
537 if (defined('ISSO_CHECK_POST_REFERER'))
538 {
539 if ($_SERVER['REQUEST_METHOD'] == 'POST')
540 {
541 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
542
543 if ($host AND $_SERVER['HTTP_REFERER'])
544 {
545 $parts = parse_url($_SERVER['HTTP_REFERER']);
546 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
547
548 if ($ourhost != $host)
549 {
550 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR);
551 }
552 $_isso->debug('remote post check = ok');
553 }
554 else
555 {
556 $_isso->debug('remote post check = FAILED');
557 }
558 }
559 }
560
561 /*=====================================================================*\
562 || ###################################################################
563 || # $HeadURL$
564 || # $Id$
565 || ###################################################################
566 \*=====================================================================*/
567 ?>