Making everything properly documented so that we don't produce warnings when running...
[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 (PHP_VERSION < '4.1.0')
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 can 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 $input = array();
152 var $i = array();
153 var $in = array();
154
155 /**
156 * If we are running with magic_quotes_gpc on or off
157 * @var int
158 * @see escape(), exec_sanitize_data()
159 */
160 var $magicquotes = 0;
161
162 /**
163 * If we should automagically escape strings, mimicking magic_quotes_gpc
164 * @var bool
165 * @see exec_sanitize_data()
166 */
167 var $escapestrings = false;
168
169 /**
170 * Constructor
171 */
172 function Shared_Object_Framework()
173 {
174 // error reporting
175 set_error_handler(array(&$this, '_error_handler'));
176
177 // magic quotes
178 $this->magicquotes = get_magic_quotes_gpc();
179 set_magic_quotes_runtime(0);
180
181 if (defined('ISSO_ESCAPE_STRINGS'))
182 {
183 $this->escapestrings = (bool)constant('ISSO_ESCAPE_STRINGS');
184 }
185
186 // start input sanitize using variable_order GP
187 if (!$this->escapestrings)
188 {
189 $this->exec_sanitize_data();
190 }
191
192 $this->modules['kernel'] = 'Shared Object Framework Core';
193 }
194
195 /**
196 * Prepares a path for being set as the sourcepath
197 *
198 * @param str Source path or URL
199 *
200 * @return str Prepared source path
201 */
202 function fetch_sourcepath($source)
203 {
204 if (substr($source, strlen($source) - 1) != '/')
205 {
206 $source .= '/';
207 }
208 return $source;
209 }
210
211 /**
212 * Loads a framework extension
213 *
214 * @param str Name of the framework
215 */
216 function load($framework)
217 {
218 if (!$this->is_loaded($framework))
219 {
220 $newobj = $this->locate($framework);
221 $this->$newobj['OBJ'] = new $newobj['CLASS']();
222 $GLOBALS["$newobj[OBJ]"] =& $this->$newobj['OBJ'];
223 $this->modules["$framework"] = $newobj['OBJECT'];
224 }
225 }
226
227 /**
228 * Includes a framework module. Module definitions need three variables:
229 * class, object, and obj. Class is the name of the class, object is
230 * the name human-readable name, and obj is the name that the module
231 * should be initialized as; this is used in class extensions.
232 *
233 * @param str Name of the framework
234 *
235 * @return array List of initialization variables
236 */
237 function locate($framework)
238 {
239 if ($this->sourcepath == '')
240 {
241 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
242 }
243
244 if (file_exists($this->sourcepath . $framework . '.php'))
245 {
246 require_once($this->sourcepath . $framework . '.php');
247 return array('CLASS' => $CLASS, 'OBJECT' => $OBJECT, 'OBJ' => $OBJ);
248 }
249 else
250 {
251 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
252 exit;
253 }
254 }
255
256 /**
257 * Prints a list of all currently loaded framework modules
258 *
259 * @param bool Return the data as an array?
260 *
261 * @return mixed HTML output or an array of loaded modules
262 */
263 function show_modules($return = false)
264 {
265 if ($return)
266 {
267 return $this->modules;
268 }
269 else
270 {
271 $output = "\n\n<ul>\n\t<li>";
272 $output .= implode("</li>\n\t<li>", $this->modules);
273 $output .= "</li>\n</ul>\n\n";
274 $this->_message('Loaded Modules', $output, 1);
275 }
276 }
277
278 /**
279 * Verifies to see if a framework has been loaded
280 *
281 * @param str Framework name
282 *
283 * @return bool Whether or not the framework has been loaded
284 */
285 function is_loaded($framework)
286 {
287 if (isset($this->modules["$framework"]))
288 {
289 return true;
290 }
291 else
292 {
293 return false;
294 }
295 }
296
297 /**
298 * Prints an ISSO message
299 *
300 * @param str The title of the message
301 * @param str The content of the message
302 * @param int Type of message to be printed
303 * @param bool Return the output?
304 *
305 * @return mixed Output or null
306 */
307 function _message($title, $message, $type, $return = false)
308 {
309 switch ($type)
310 {
311 // Message
312 case 1:
313 $prefix = 'Message';
314 $color = '#669900';
315 $font = '#000000';
316 break;
317
318 // Warning
319 case 2:
320 $prefix = 'Warning';
321 $color = '#003399';
322 $font = '#FFFFFF';
323 break;
324
325 case 3:
326 $prefix = 'Error';
327 $color = '#990000';
328 $font = '#EFEFEF';
329 break;
330 }
331
332 $output = "\n<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"500\" style=\"background-color: $color; font-family: Verdana, sans-serif; font-size: 12px;\">";
333 $output .= "\n<tr style=\"color: $font\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
334 $output .= "\n<tr style=\"background-color: #FFFFFF\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
335
336 if ($return)
337 {
338 return $output;
339 }
340 else
341 {
342 print($output);
343 }
344 }
345
346 /**
347 * Custom error handler for ISSO
348 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
349 *
350 * @param int Error number
351 * @param str Error message string
352 * @param str File that contains the error
353 * @param str The line number of the error
354 * @param str The active symbol table at which point the error occurred
355 */
356 function _error_handler($errno, $errstr, $errfile, $errline)
357 {
358 switch ($errno)
359 {
360 // Fatal
361 case E_USER_ERROR:
362 $title = 'Fatal';
363 if (!(ini_get('error_reporting') & E_USER_ERROR))
364 {
365 return;
366 }
367 break;
368
369 // Error
370 case E_USER_WARNING:
371 $title = 'Warning';
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 if (!(ini_get('error_reporting') & E_USER_NOTICE) AND !(ini_get('error_reporting') & E_NOTICE))
383 {
384 return;
385 }
386 break;
387 }
388
389 $errfile = str_replace(getcwd(), '', $errfile);
390
391 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
392
393 $this->_message($title, $errstr, 3);
394
395 if ($errno == E_USER_ERROR)
396 {
397 exit;
398 }
399 }
400
401 /**
402 * Logs a debug message for verbose output
403 *
404 * @param str Message
405 */
406 function debug($message)
407 {
408 $this->debuginfo[] = $message;
409 }
410
411 /**
412 * Recursive XSS cleaner
413 *
414 * @param mixed Unsanitized REQUEST data
415 *
416 * @return mixed Sanitized data
417 */
418 function _sanitize_input_recursive($data)
419 {
420 foreach ($data AS $key => $value)
421 {
422 if (is_array($value))
423 {
424 $data["$key"] = $this->_sanitize_input_recursive($value);
425 }
426 else
427 {
428 if ($this->escapestrings)
429 {
430 $data["$key"] = $this->escape($this->sanitize($value));
431 }
432 else
433 {
434 $data["$key"] = $this->sanitize($value);
435 }
436 }
437 }
438 return $data;
439 }
440
441 /**
442 * Simple way to protect against HTML attacks with Unicode support
443 *
444 * @param str Unsanitzed text
445 *
446 * @return str Properly protected text that only encodes potential threats
447 */
448 function sanitize($text)
449 {
450 if ($this->magicquotes)
451 {
452 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
453 }
454 else
455 {
456 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
457 }
458 }
459
460 /**
461 * Takes text that has been processed for HTML and unsanitizes it
462 *
463 * @param str Text that needs to be turned back into HTML
464 * @param bool Force magicquotes off
465 *
466 * @return str Unsanitized text
467 */
468 function unsanitize($text, $force = false)
469 {
470 if ($this->magicquotes AND !$force)
471 {
472 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '\"'), $text);
473 }
474 else
475 {
476 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
477 }
478 }
479
480 /**
481 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
482 *
483 * @param str Some string
484 * @param bool If the data is binary; if so it'll be run through DB::escape_string()
485 * @param bool Force magic quotes to be off
486 *
487 * @return str String that has slashes added
488 */
489 function escape($str, $binary = false, $force = false)
490 {
491 global $_isso;
492
493 if ($this->magicquotes AND !$force)
494 {
495 if (isset($_isso->db) AND $binary)
496 {
497 if (is_resource($_isso->db->link_id))
498 {
499 return $_isso->db->escape_string(stripslashes($str));
500 }
501 }
502 return $str;
503 }
504 else
505 {
506 if (isset($_isso->db) AND $binary)
507 {
508 if (is_resource($_isso->db->link_id))
509 {
510 return $_isso->db->escape_string($str);
511 }
512 }
513 return addslashes($str);
514 }
515 }
516
517 /**
518 * Runs through all of the input data and sanitizes it.
519 */
520 function exec_sanitize_data()
521 {
522 $this->input = $this->_sanitize_input_recursive(array_merge($_GET, $_POST));
523 $this->i =& $this->input;
524 $this->in =& $this->input;
525 // we're now using magic quotes
526 if ($this->escapestrings)
527 {
528 $this->magicquotes = 1;
529 }
530 }
531 }
532
533 /**
534 * Global callback used for module calls back to the kernel
535 */
536 $_isso = new Shared_Object_Framework();
537
538 /**
539 * Wrapper for ternary operator that has to be in the global scope.
540 * This function is going to be removed from our applications due
541 * to it's latency in processing. This will remain here purely
542 * for legacy reasons. It will be removed when necessary.
543 *
544 * @param expr Expression
545 * @param mixed If the expression is true
546 * @param mixed If the expression is false
547 *
548 * @return mixed True or false data
549 */
550 function iff($condition, $iftrue, $iffalse = null)
551 {
552 return ($condition) ? ($iftrue) : ($iffalse);
553 }
554
555 if (defined('ISSO_CHECK_POST_REFERER'))
556 {
557 if ($_SERVER['REQUEST_METHOD'] == 'POST')
558 {
559 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
560 $parts = parse_url($_SERVER['HTTP_REFERER']);
561 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
562
563 if ($ourhost != $host)
564 {
565 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR);
566 }
567 $_isso->debug('remote post check = ok');
568 }
569 }
570
571 /*=====================================================================*\
572 || ###################################################################
573 || # $HeadURL$
574 || # $Id$
575 || ###################################################################
576 \*=====================================================================*/
577 ?>