]> src.bluestatic.org Git - isso.git/blob - kernel.php
show_modules() now gets class names
[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 if (defined('ISSO_CHECK_POST_REFERER'))
194 {
195 $this->exec_referer_check();
196 }
197 }
198
199 /**
200 * Prepares a path for being set as the sourcepath
201 *
202 * @param string Source path or URL
203 *
204 * @return string Prepared source path
205 */
206 function fetch_sourcepath($source)
207 {
208 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
209 {
210 $source .= DIRECTORY_SEPARATOR;
211 }
212 return $source;
213 }
214
215 /**
216 * Loads a framework module
217 *
218 * @param string Name of the framework file to load
219 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
220 * @param bool Globalize the internal variable?
221 *
222 * @return object Instantiated instance
223 */
224 function &load($framework, $asobject, $globalize = false)
225 {
226 if ($this->is_loaded($framework))
227 {
228 return $this->getobj($framework);
229 }
230
231 if ($this->sourcepath == '')
232 {
233 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
234 }
235
236 if (file_exists($this->sourcepath . $framework . '.php'))
237 {
238 require_once($this->sourcepath . $framework . '.php');
239 }
240 else
241 {
242 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
243 }
244
245 if ($asobject === null)
246 {
247 return;
248 }
249
250 if (isset($this->$asobject))
251 {
252 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
253 }
254
255 $this->$asobject = new $framework($this);
256
257 $this->modules["$framework"] =& $this->$asobject;
258
259 if ($globalize)
260 {
261 $GLOBALS["$asobject"] =& $this->$asobject;
262 }
263
264 return $this->$asobject;
265 }
266
267 /**
268 * Prints a list of all currently loaded framework modules
269 *
270 * @param bool Return the data as an array?
271 *
272 * @return mixed HTML output or an array of loaded modules
273 */
274 function show_modules($return = false)
275 {
276 foreach ($this->modules AS $object)
277 {
278 $modules[] = get_class($object);
279 }
280
281 if ($return)
282 {
283 return $modules;
284 }
285 else
286 {
287 $output = "\n\n<ul>\n\t<li>";
288 $output .= implode("</li>\n\t<li>", $modules);
289 $output .= "</li>\n</ul>\n\n";
290 $this->_message('Loaded Modules', $output, 1);
291 }
292 }
293
294 /**
295 * Verifies to see if a framework has been loaded
296 *
297 * @param string Framework name
298 *
299 * @return bool Whether or not the framework has been loaded
300 */
301 function is_loaded($framework)
302 {
303 if (isset($this->modules["$framework"]))
304 {
305 return true;
306 }
307 else
308 {
309 return false;
310 }
311 }
312
313 /**
314 * Prints an ISSO message
315 *
316 * @param string The title of the message
317 * @param string The content of the message
318 * @param integer Type of message to be printed
319 * @param bool Return the output?
320 *
321 * @return mixed Output or null
322 */
323 function _message($title, $message, $type, $return = false)
324 {
325 switch ($type)
326 {
327 // Message
328 case 1:
329 $prefix = 'Message';
330 $color = '#669900';
331 $font = '#000000';
332 break;
333
334 // Warning
335 case 2:
336 $prefix = 'Warning';
337 $color = '#003399';
338 $font = '#FFFFFF';
339 break;
340
341 case 3:
342 $prefix = 'Error';
343 $color = '#990000';
344 $font = '#EFEFEF';
345 break;
346 }
347
348 $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;\">";
349 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
350 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
351
352 if ($return)
353 {
354 return $output;
355 }
356 else
357 {
358 print($output);
359 }
360 }
361
362 /**
363 * Custom error handler for ISSO
364 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
365 *
366 * @param integer Error number
367 * @param string Error message string
368 * @param string File that contains the error
369 * @param string The line number of the error
370 * @param string The active symbol table at which point the error occurred
371 */
372 function _error_handler($errno, $errstr, $errfile, $errline)
373 {
374 switch ($errno)
375 {
376 // Fatal
377 case E_USER_ERROR:
378 $title = 'Fatal';
379 $level = 3;
380 if (!(ini_get('error_reporting') & E_USER_ERROR))
381 {
382 return;
383 }
384 break;
385
386 // Error
387 case E_USER_WARNING:
388 $title = 'Warning';
389 $level = 2;
390 if (!(ini_get('error_reporting') & E_USER_WARNING) AND !(ini_get('error_reporting') & E_WARNING))
391 {
392 return;
393 }
394 break;
395
396 // Warning
397 case E_USER_NOTICE:
398 default:
399 $title = 'Notice';
400 $level = 1;
401 if (!(ini_get('error_reporting') & E_USER_NOTICE) AND !(ini_get('error_reporting') & E_NOTICE))
402 {
403 return;
404 }
405 break;
406 }
407
408 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
409
410 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
411
412 $this->_message($title, $errstr, $level);
413
414 if ($errno == E_USER_ERROR)
415 {
416 exit;
417 }
418 }
419
420 /**
421 * Logs a debug message for verbose output
422 *
423 * @param string Message
424 */
425 function debug($message)
426 {
427 $this->debuginfo[] = $message;
428 }
429
430 /**
431 * Recursive XSS cleaner
432 *
433 * @param mixed Unsanitized REQUEST data
434 *
435 * @return mixed Sanitized data
436 */
437 function _sanitize_input_recursive($data)
438 {
439 foreach ($data AS $key => $value)
440 {
441 if (is_array($value))
442 {
443 $data["$key"] = $this->_sanitize_input_recursive($value);
444 }
445 else
446 {
447 if ($this->escapestrings)
448 {
449 $data["$key"] = $this->escape($this->sanitize($value), false, false);
450 }
451 else
452 {
453 $data["$key"] = $this->sanitize($value);
454 }
455 }
456 }
457 return $data;
458 }
459
460 /**
461 * Simple way to protect against HTML attacks with Unicode support
462 *
463 * @param string Unsanitzed text
464 *
465 * @return string Properly protected text that only encodes potential threats
466 */
467 function sanitize($text)
468 {
469 if ($this->magicquotes)
470 {
471 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
472 }
473 else
474 {
475 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
476 }
477 }
478
479 /**
480 * Takes text that has been processed for HTML and unsanitizes it
481 *
482 * @param string Text that needs to be turned back into HTML
483 * @param bool Force magicquotes off
484 *
485 * @return string Unsanitized text
486 */
487 function unsanitize($text, $force = false)
488 {
489 if ($this->magicquotes AND !$force)
490 {
491 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '\"'), $text);
492 }
493 else
494 {
495 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
496 }
497 }
498
499 /**
500 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
501 *
502 * @param string Some string
503 * @param bool If the data is binary; if so it'll be run through DB::escape_stringing()
504 * @param bool Force magic quotes to be off
505 *
506 * @return string String that has slashes added
507 */
508 function escape($str, $binary = false, $force = true)
509 {
510 if ($this->magicquotes AND !$force)
511 {
512 if (isset($this->db) AND $binary)
513 {
514 if (is_resource($this->db->link_id))
515 {
516 return $this->db->escape_string(stripslashes($str));
517 }
518 }
519 return $str;
520 }
521 else
522 {
523 if (isset($this->db) AND $binary)
524 {
525 if (is_resource($this->db->link_id))
526 {
527 return $this->db->escape_string($str);
528 }
529 }
530 return addslashes($str);
531 }
532 }
533
534 /**
535 * Runs through all of the input data and sanitizes it.
536 */
537 function exec_sanitize_data()
538 {
539 $this->in = $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
540 // we're now using magic quotes
541 if ($this->escapestrings)
542 {
543 $this->magicquotes = 1;
544 }
545 }
546
547 /**
548 * Checks to see if a POST refer is actually from us
549 */
550 function exec_referer_check()
551 {
552 if ($_SERVER['REQUEST_METHOD'] == 'POST')
553 {
554 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
555
556 if ($host AND $_SERVER['HTTP_REFERER'])
557 {
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 $this->debug('remote post check = ok');
566 }
567 else
568 {
569 $this->debug('remote post check = FAILED');
570 }
571 }
572 }
573 }
574
575 /*=====================================================================*\
576 || ###################################################################
577 || # $HeadURL$
578 || # $Id$
579 || ###################################################################
580 \*=====================================================================*/
581 ?>