]> src.bluestatic.org Git - isso.git/blob - kernel.php
Creating isso:null-framework global; this can't be accessed through $isso:null-framew...
[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 * Input cleaning type constant
91 */
92 /**
93 * Integer type
94 */
95 define('TYPE_INT', 1);
96
97 /**
98 * Unsigned integer
99 */
100 define('TYPE_UINT', 2);
101
102 /**
103 * Float type
104 */
105 define('TYPE_FLOAT', 4);
106
107 /**
108 * Boolean type
109 */
110 define('TYPE_BOOL', 8);
111
112 /**
113 * String - cleaned
114 */
115 define('TYPE_STR', 16);
116
117 /**
118 * String - deliberate unclean
119 */
120 define('TYPE_STRUN', 32);
121
122 /**
123 * No cleaning - here for use in API
124 */
125 define('TYPE_NOCLEAN', 64);
126 /**#@-*/
127
128 /**
129 * Iris Studios Shared Object Framework (ISSO)
130 *
131 * This framework allows a common backend to be used amongst all Iris
132 * Studios applications and is built to be abstract and flexible.
133 * The base framework handles all loading and module management.
134 *
135 * @author Iris Studios, Inc.
136 * @copyright Copyright ©2002 - [#]year[#], Iris Studios, Inc.
137 * @version $Revision$
138 * @package ISSO
139 *
140 */
141 class Shared_Object_Framework
142 {
143 /**
144 * ISSO version
145 * @var string
146 */
147 var $version = '[#]version[#]';
148
149 /**
150 * Location of ISSO, used for internal linking
151 * @var string
152 */
153 var $sourcepath = '';
154
155 /**
156 * Path of the current application
157 * @var string
158 */
159 var $apppath = '';
160
161 /**
162 * Web path used to get the web location of the installation of ISSO; only used for Printer module
163 * @var string
164 */
165 var $webpath = '';
166
167 /**
168 * Name of the current application
169 * @var string
170 */
171 var $application = '';
172
173 /**
174 * Version of the current application
175 * @var string
176 */
177 var $appversion = '';
178
179 /**
180 * Whether debug mode is on or off
181 * @var bool
182 */
183 var $debug = false;
184
185 /**
186 * List of all active debug messages
187 * @var array
188 */
189 var $debuginfo = array();
190
191 /**
192 * List of loaded modules
193 * @var array
194 */
195 var $modules = array();
196
197 /**
198 * An array of sanitized variables that have been cleaned for HTML tag openers and double quotes
199 * @var array
200 */
201 var $in = array();
202
203 /**
204 * If we are running with magic_quotes_gpc on or off
205 * @var int
206 */
207 var $magicquotes = 0;
208
209 /**
210 * If we should automagically escape strings, mimicking magic_quotes_gpc
211 * @var bool
212 */
213 var $escapestrings = false;
214
215 /**
216 * Constructor
217 */
218 function Shared_Object_Framework()
219 {
220 // error reporting
221 set_error_handler(array(&$this, '_error_handler'));
222
223 // magic quotes
224 $this->magicquotes = get_magic_quotes_gpc();
225 set_magic_quotes_runtime(0);
226
227 if (defined('ISSO_ESCAPE_STRINGS'))
228 {
229 $this->escapestrings = (bool)constant('ISSO_ESCAPE_STRINGS');
230 }
231
232 // start input sanitize using variable_order GPC
233 if (!$this->escapestrings)
234 {
235 $this->exec_sanitize_data();
236 }
237
238 if (defined('ISSO_CHECK_POST_REFERER'))
239 {
240 $this->exec_referer_check();
241 }
242 }
243
244 /**
245 * Prepares a path for being set as the sourcepath
246 *
247 * @param string Source path or URL
248 *
249 * @return string Prepared source path
250 */
251 function fetch_sourcepath($source)
252 {
253 if (substr($source, strlen($source) - 1) != DIRECTORY_SEPARATOR)
254 {
255 $source .= DIRECTORY_SEPARATOR;
256 }
257 return $source;
258 }
259
260 /**
261 * Loads a framework module
262 *
263 * @param string Name of the framework file to load
264 * @param string Internal variable to initialize as; to not instantiate (just require) leave it as NULL
265 * @param bool Globalize the internal variable?
266 *
267 * @return object Instantiated instance
268 */
269 function &load($framework, $asobject, $globalize = false)
270 {
271 // set the object interlock
272 if ($GLOBALS['isso:null-framework'] === null)
273 {
274 $GLOBALS['isso:null-framework'] =& $this;
275 }
276
277 if ($this->is_loaded($framework))
278 {
279 return $this->getobj($framework);
280 }
281
282 if ($this->sourcepath == '')
283 {
284 trigger_error('Invalid sourcepath specified', E_USER_ERROR);
285 }
286
287 if (file_exists($this->sourcepath . $framework . '.php'))
288 {
289 require_once($this->sourcepath . $framework . '.php');
290 }
291 else
292 {
293 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', E_USER_ERROR);
294 }
295
296 if ($asobject === null)
297 {
298 return;
299 }
300
301 if (isset($this->$asobject))
302 {
303 trigger_error('Cannot instantiate framework `' . $framework . '` into `' . $asobject . '`', E_USER_ERROR);
304 }
305
306 $this->$asobject = new $framework($this);
307
308 $this->modules["$framework"] =& $this->$asobject;
309
310 if ($globalize)
311 {
312 $GLOBALS["$asobject"] =& $this->$asobject;
313 }
314
315 return $this->$asobject;
316 }
317
318 /**
319 * Prints a list of all currently loaded framework modules
320 *
321 * @param bool Return the data as an array?
322 *
323 * @return mixed HTML output or an array of loaded modules
324 */
325 function show_modules($return = false)
326 {
327 foreach ($this->modules AS $object)
328 {
329 $modules[] = get_class($object);
330 }
331
332 if ($return)
333 {
334 return $modules;
335 }
336 else
337 {
338 $output = "\n\n<ul>\n\t<li>";
339 $output .= implode("</li>\n\t<li>", $modules);
340 $output .= "</li>\n</ul>\n\n";
341 $this->_message('Loaded Modules', $output, 1);
342 }
343 }
344
345 /**
346 * Verifies to see if a framework has been loaded
347 *
348 * @param string Framework name
349 *
350 * @return bool Whether or not the framework has been loaded
351 */
352 function is_loaded($framework)
353 {
354 if (isset($this->modules["$framework"]))
355 {
356 return true;
357 }
358 else
359 {
360 return false;
361 }
362 }
363
364 /**
365 * Prints an ISSO message
366 *
367 * @param string The title of the message
368 * @param string The content of the message
369 * @param integer Type of message to be printed
370 * @param bool Return the output?
371 *
372 * @return mixed Output or null
373 */
374 function _message($title, $message, $type, $return = false)
375 {
376 switch ($type)
377 {
378 // Message
379 case 1:
380 $prefix = 'Message';
381 $color = '#669900';
382 $font = '#000000';
383 break;
384
385 // Warning
386 case 2:
387 $prefix = 'Warning';
388 $color = '#003399';
389 $font = '#FFFFFF';
390 break;
391
392 case 3:
393 $prefix = 'Error';
394 $color = '#990000';
395 $font = '#EFEFEF';
396 break;
397 }
398
399 $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;\">";
400 $output .= "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
401 $output .= "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
402
403 if ($return)
404 {
405 return $output;
406 }
407 else
408 {
409 print($output);
410 }
411 }
412
413 /**
414 * Custom error handler for ISSO
415 * We only handle E_WARNING, E_NOTICE, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
416 *
417 * @param integer Error number
418 * @param string Error message string
419 * @param string File that contains the error
420 * @param string The line number of the error
421 * @param string The active symbol table at which point the error occurred
422 */
423 function _error_handler($errno, $errstr, $errfile, $errline)
424 {
425 switch ($errno)
426 {
427 // Fatal
428 case E_USER_ERROR:
429 $title = 'Fatal';
430 $level = 3;
431 if (!(ini_get('error_reporting') & E_USER_ERROR))
432 {
433 return;
434 }
435 break;
436
437 // Error
438 case E_USER_WARNING:
439 $title = 'Warning';
440 $level = 2;
441 if (!(ini_get('error_reporting') & E_USER_WARNING) AND !(ini_get('error_reporting') & E_WARNING))
442 {
443 return;
444 }
445 break;
446
447 // Warning
448 case E_USER_NOTICE:
449 default:
450 $title = 'Notice';
451 $level = 1;
452 if (!(ini_get('error_reporting') & E_USER_NOTICE) AND !(ini_get('error_reporting') & E_NOTICE))
453 {
454 return;
455 }
456 break;
457 }
458
459 $errfile = str_replace(array(getcwd(), dirname(getcwd())), '', $errfile);
460
461 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
462
463 $this->_message($title, $errstr, $level);
464
465 if ($errno == E_USER_ERROR)
466 {
467 exit;
468 }
469 }
470
471 /**
472 * Logs a debug message for verbose output
473 *
474 * @param string Message
475 */
476 function debug($message)
477 {
478 $this->debuginfo[] = $message;
479 }
480
481 /**
482 * Recursive XSS cleaner
483 *
484 * @param mixed Unsanitized REQUEST data
485 *
486 * @return mixed Sanitized data
487 */
488 function _sanitize_input_recursive($data)
489 {
490 foreach ($data AS $key => $value)
491 {
492 if (is_array($value))
493 {
494 $data["$key"] = $this->_sanitize_input_recursive($value);
495 }
496 else
497 {
498 if ($this->escapestrings)
499 {
500 $data["$key"] = $this->escape($this->sanitize($value), false, false);
501 }
502 else
503 {
504 $data["$key"] = $this->sanitize($value);
505 }
506 }
507 }
508 return $data;
509 }
510
511 /**
512 * Simple way to protect against HTML attacks with Unicode support
513 *
514 * @param string Unsanitzed text
515 *
516 * @return string Properly protected text that only encodes potential threats
517 */
518 function sanitize($text)
519 {
520 if ($this->magicquotes)
521 {
522 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
523 }
524 else
525 {
526 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
527 }
528 }
529
530 /**
531 * Unicode-safe entity encoding system; similar to sanitize()
532 *
533 * @param string Unsanitized text
534 *
535 * @return string Unicode-safe sanitized text with entities preserved
536 */
537 function entity_encode($text)
538 {
539 $text = str_replace('&', '&amp;', $text);
540 $text = $this->sanitize($text);
541 return $text;
542 }
543
544 /**
545 * Takes text that has been processed for HTML and unsanitizes it
546 *
547 * @param string Text that needs to be turned back into HTML
548 * @param bool Force magicquotes off
549 *
550 * @return string Unsanitized text
551 */
552 function unsanitize($text, $force = false)
553 {
554 if ($this->magicquotes AND !$force)
555 {
556 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '\"'), $text);
557 }
558 else
559 {
560 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
561 }
562 }
563
564 /**
565 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
566 *
567 * @param string Some string
568 * @param bool If the data is binary; if so it'll be run through DB::escape_stringing()
569 * @param bool Force magic quotes to be off
570 *
571 * @return string String that has slashes added
572 */
573 function escape($str, $binary = false, $force = true)
574 {
575 if ($this->magicquotes AND !$force)
576 {
577 if (isset($this->db) AND $binary)
578 {
579 if (is_resource($this->db->link_id))
580 {
581 return $this->db->escape_string(stripslashes($str));
582 }
583 }
584 return $str;
585 }
586 else
587 {
588 if (isset($this->db) AND $binary)
589 {
590 if (is_resource($this->db->link_id))
591 {
592 return $this->db->escape_string($str);
593 }
594 }
595 return addslashes($str);
596 }
597 }
598
599 /**
600 * Runs through all of the input data and sanitizes it.
601 */
602 function exec_sanitize_data()
603 {
604 $this->in = $this->_sanitize_input_recursive(array_merge($_GET, $_POST, $_COOKIE));
605 // we're now using magic quotes
606 if ($this->escapestrings)
607 {
608 $this->magicquotes = 1;
609 }
610 }
611
612 /**
613 * Sanitize function for something other than a string (which everything is sanitized for if you use exec_sanitize_data().
614 * Cleaned data is placed back into $isso->in; this makes it so you don't have to constantly intval() [etc.] data
615 *
616 * @param array Array of elements to clean as varname => type
617 */
618 function input_clean_array($vars)
619 {
620 foreach ($vars AS $varname => $type)
621 {
622 $this->input_clean($varname, $type);
623 }
624 }
625
626 /**
627 * Sanitize function that does a single variable as oppoesd to an array (see input_clean_array() for more details)
628 *
629 * @param string Variable name in $isso->in[]
630 * @param integer Sanitization type constant
631 */
632 function input_clean($varname, $type)
633 {
634 $this->in["$varname"] = $this->clean($this->in["$varname"], $type);
635 }
636
637 /**
638 * Cleaning function that does the work for input_clean(); this is moved here so it can be used to clean things that aren't in $isso->in[]
639 *
640 * @param mixed Data
641 * @param integer Sanitization type constant
642 *
643 * @return mixed Cleaned data
644 */
645 function clean($value, $type)
646 {
647 if ($type == TYPE_INT)
648 {
649 $value = intval($value);
650 }
651 else if ($type == TYPE_UINT)
652 {
653 $value = abs(intval($value));
654 }
655 else if ($type == TYPE_FLOAT)
656 {
657 $value = floatval($value);
658 }
659 else if ($type == TYPE_BOOL)
660 {
661 $value = (bool)$value;
662 }
663 else if ($type == TYPE_STR)
664 {
665 if (!$this->escapestrings)
666 {
667 $value = $this->escape($value);
668 }
669 }
670 else if ($type == TYPE_STRUN)
671 {
672 $value = $this->unsanitize($value);
673 }
674 else if ($type == TYPE_NOCLEAN)
675 {
676 if ($this->escapestrings)
677 {
678 $value = $this->escape($value);
679 }
680 }
681 else
682 {
683 trigger_error('Invalid clean type `' . $type . '` specified', E_USER_ERROR);
684 }
685
686 return $value;
687 }
688
689 /**
690 * Checks to see if a POST refer is actually from us
691 */
692 function exec_referer_check()
693 {
694 if ($_SERVER['REQUEST_METHOD'] == 'POST')
695 {
696 $host = ($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_ENV['HTTP_HOST'];
697
698 if ($host AND $_SERVER['HTTP_REFERER'])
699 {
700 $parts = parse_url($_SERVER['HTTP_REFERER']);
701 $ourhost = $parts['host'] . (($parts['port']) ? ":$parts[port]" : '');
702
703 if ($ourhost != $host)
704 {
705 trigger_error('No external hosts are allowed to POST to this application', E_USER_ERROR);
706 }
707 $this->debug('remote post check = ok');
708 }
709 else
710 {
711 $this->debug('remote post check = FAILED');
712 }
713 }
714 }
715 }
716
717 /*=====================================================================*\
718 || ###################################################################
719 || # $HeadURL$
720 || # $Id$
721 || ###################################################################
722 \*=====================================================================*/
723 ?>