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