Use different ISSO message styles for various error levels
[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) != DIRECTORY_SEPARATOR)
203 {
204 $source .= DIRECTORY_SEPARATOR;
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; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
332 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\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 $level = 3;
362 if (!(ini_get('error_reporting') & E_USER_ERROR))
363 {
364 return;
365 }
366 break;
367
368 // Error
369 case E_USER_WARNING:
370 $title = 'Warning';
371 $level = 2;
372 if (!(ini_get('error_reporting') & E_USER_WARNING) AND !(ini_get('error_reporting') & E_WARNING))
373 {
374 return;
375 }
376 break;
377
378 // Warning
379 case E_USER_NOTICE:
380 default:
381 $title = 'Notice';
382 $level = 1;
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, $level);
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 str 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 str Unsanitzed text
446 *
447 * @return str 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 str Text that needs to be turned back into HTML
465 * @param bool Force magicquotes off
466 *
467 * @return str 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 str Some string
485 * @param bool If the data is binary; if so it'll be run through DB::escape_string()
486 * @param bool Force magic quotes to be off
487 *
488 * @return str 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 /**
538 * Wrapper for ternary operator that has to be in the global scope.
539 * This function is going to be removed from our applications due
540 * to it's latency in processing. This will remain here purely
541 * for legacy reasons. It will be removed when necessary.
542 *
543 * @deprecated The normal ternary operators should be used instead; function calls are too expensive
544 *
545 * @param expr Expression
546 * @param mixed If the expression is true
547 * @param mixed If the expression is false
548 *
549 * @return mixed True or false data
550 */
551 function iff($condition, $iftrue, $iffalse = null)
552 {
553 return ($condition) ? ($iftrue) : ($iffalse);
554 }
555
556 if (defined('ISSO_CHECK_POST_REFERER'))
557 {
558 if ($_SERVER['REQUEST_METHOD'] == 'POST')
559 {
560 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
561
562 if ($host AND $_SERVER['HTTP_REFERER'])
563 {
564 $parts = parse_url($_SERVER['HTTP_REFERER']);
565 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
566
567 if ($ourhost != $host)
568 {
569 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR);
570 }
571 $_isso->debug('remote post check = ok');
572 }
573 else
574 {
575 $_isso->debug('remote post check = FAILED');
576 }
577 }
578 }
579
580 /*=====================================================================*\
581 || ###################################################################
582 || # $HeadURL$
583 || # $Id$
584 || ###################################################################
585 \*=====================================================================*/
586 ?>