Added reference instance variables to isso::input (named i and in). Allows for more...
[isso.git] / kernel.php
1 <?php
2 /*=====================================================================*\
3 || ################################################################### ||
4 || # Iris Studios Shared Object Framework [#]version[#]
5 || # --------------------------------------------------------------- # ||
6 || # All parts of this file are ©2003-[#]year[#] Iris Studios, Inc. No # ||
7 || # part of this file may be reproduced in any way: part or whole. # ||
8 || # --------------------------------------------------------------- # ||
9 || # ©2003 - [#]year[#] Iris Studios, Inc. | http://www.iris-studios.com # ||
10 || ################################################################### ||
11 \*=====================================================================*/
12
13 define('ERR_FATAL', E_USER_ERROR);
14 define('ERR_ALERT', E_USER_WARNING);
15 define('ERR_WARNING', E_USER_NOTICE);
16
17 if (PHP_VERSION < '4.1.0')
18 {
19 trigger_error('You need PHP version 4.1.0 or newer to run ISSO', ERR_FATAL);
20 exit;
21 }
22
23 if (PHP_VERSION > '5.0.0')
24 {
25 if (ini_get('error_reporting') & E_USER_NOTICE)
26 {
27 error_reporting(ini_get('error_reporting') - E_USER_NOTICE);
28 }
29 }
30
31 if ((bool)ini_get('register_globals') === true)
32 {
33 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
34 foreach ($superglobals AS $global)
35 {
36 if (is_array(${$global}))
37 {
38 foreach (${$global} AS $_key => $_val)
39 {
40 if (isset(${$_key}))
41 {
42 unset(${$_key});
43 }
44 }
45 }
46 }
47 }
48
49 /**
50 * Iris Studios Shared Object Framework (ISSO)
51 *
52 * This framework allows a common backend to be used amongst all Iris
53 * Studios applications and can is built to be abstract and flexible.
54 * The base framework handles all loading and module management.
55 *
56 * @author Iris Studios, Inc.
57 * @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc.
58 * @version $Revision$
59 *
60 */
61 class Shared_Object_Framework
62 {
63 /**
64 * Global environment variables
65 *
66 * This is where we keep the global variables that are used within the shared framework
67 *
68 * @var version ISSO version
69 * @var sourcepath The location of the framework sources
70 * @var appath The path to the application's location
71 * @var application The name of the application that is using the framework
72 * @var debug Variable for debug mode
73 * @var debuginfo Listing of all debug notices
74 * @var modules An array of loaded framework modules
75 * @var input All input data for the system
76 * @var i Short-hand reference to $isso::input
77 * @var in Short-hand reference to $isso::input
78 * @var magicquotes Status of Magic Quotes GPC
79 */
80 var $version = '[#]version[#]';
81 var $sourcepath = '';
82 var $apppath = '';
83 var $application = '';
84 var $debug = false;
85 var $debuginfo = array();
86 var $modules = array();
87 var $input = array();
88 var $i = array();
89 var $in = array();
90 var $magicquotes = 0;
91
92 /**
93 * Constructor
94 */
95 function Shared_Object_Framework()
96 {
97 // error reporting
98 set_error_handler(array(&$this, '_error_handler'));
99
100 // magic quotes
101 $this->magicquotes = get_magic_quotes_gpc();
102 set_magic_quotes_runtime(0);
103
104 // start input sanitize using variable_order GP
105 $this->input = $this->_sanitize_input_recursive(array_merge($_GET, $_POST));
106 $this->i =& $this->input;
107 $this->in =& $this->input;
108
109 $this->modules['kernel'] = 'Shared Object Framework Core';
110 }
111
112 /**
113 * Prepares a path for being set as the sourcepath
114 *
115 * @param str Source path or URL
116 *
117 * @return str Prepared source path
118 */
119 function fetch_sourcepath($source)
120 {
121 if (substr($source, strlen($source) - 1) != '/')
122 {
123 $source .= '/';
124 }
125 return $source;
126 }
127
128 /**
129 * Loads a framework extension
130 *
131 * @param str Name of the framework
132 */
133 function load($framework)
134 {
135 if (!$this->is_loaded($framework))
136 {
137 $newobj = $this->locate($framework);
138 $this->$newobj['OBJ'] = new $newobj['CLASS']();
139 $GLOBALS["$newobj[OBJ]"] =& $this->$newobj['OBJ'];
140 $this->modules["$framework"] = $newobj['OBJECT'];
141 }
142 }
143
144 /**
145 * Includes a framework module. Module definitions need three variables:
146 * class, object, and obj. Class is the name of the class, object is
147 * the name human-readable name, and obj is the name that the module
148 * should be initialized as; this is used in class extensions.
149 *
150 * @param str Name of the framework
151 *
152 * @return array List of initialization variables
153 */
154 function locate($framework)
155 {
156 if ($this->sourcepath == '')
157 {
158 trigger_error('Invalid sourcepath specified', ERR_FATAL);
159 }
160
161 if (file_exists($this->sourcepath . $framework . '.php'))
162 {
163 require_once($this->sourcepath . $framework . '.php');
164 return array('CLASS' => $CLASS, 'OBJECT' => $OBJECT, 'OBJ' => $OBJ);
165 }
166 else
167 {
168 trigger_error('Could not find the framework ' . $this->sourcepath . $framework . '.php', ERR_FATAL);
169 exit;
170 }
171 }
172
173 /**
174 * Prints a list of all currently loaded framework modules
175 *
176 * @param bool Return the data as an array?
177 *
178 * @return mixed HTML output or an array of loaded modules
179 */
180 function show_modules($return = false)
181 {
182 if ($return)
183 {
184 return $this->modules;
185 }
186 else
187 {
188 $output = "\n\n<ul>\n\t<li>";
189 $output .= implode("</li>\n\t<li>", $this->modules);
190 $output .= "</li>\n</ul>\n\n";
191 $this->_message('Loaded Modules', $output, 1);
192 }
193 }
194
195 /**
196 * Verifies to see if a framework has been loaded
197 *
198 * @param str Framework name
199 *
200 * @return bool Whether or not the framework has been loaded
201 */
202 function is_loaded($framework)
203 {
204 if (isset($this->modules["$framework"]))
205 {
206 return true;
207 }
208 else
209 {
210 return false;
211 }
212 }
213
214 /**
215 * Prints an ISSO message
216 *
217 * @param str The title of the message
218 * @param str The content of the message
219 * @param int Type of message to be printed
220 * @param bool Return the output?
221 *
222 * @return mixed Output or null
223 */
224 function _message($title, $message, $type, $return = false)
225 {
226 switch ($type)
227 {
228 // Message
229 case 1:
230 $prefix = 'Message';
231 $color = '#669900';
232 $font = '#000000';
233 break;
234
235 // Warning
236 case 2:
237 $prefix = 'Warning';
238 $color = '#003399';
239 $font = '#FFFFFF';
240 break;
241
242 case 3:
243 $prefix = 'Error';
244 $color = '#990000';
245 $font = '#EFEFEF';
246 break;
247 }
248
249 $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;\">";
250 $output .= "\n<tr style=\"color: $font\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
251 $output .= "\n<tr style=\"background-color: #FFFFFF\">\n\t<td>$message</td>\n</tr>\n</table>\n<br />\n";
252
253 if ($return)
254 {
255 return $output;
256 }
257 else
258 {
259 print($output);
260 }
261 }
262
263 /**
264 * Custom error handler for ISSO
265 *
266 * @param int Error number
267 * @param str Error message string
268 * @param str File that contains the error
269 * @param str The line number of the error
270 * @param str The active symbol table at which point the error occurred
271 */
272 function _error_handler($errno, $errstr, $errfile, $errline)
273 {
274 switch ($errno)
275 {
276 // Fatal
277 case ERR_FATAL:
278 $title = 'Fatal';
279 if (!(ini_get('error_reporting') & ERR_FATAL))
280 {
281 return;
282 }
283 break;
284
285 // Error
286 case ERR_ALERT:
287 $title = 'Alert';
288 if (!(ini_get('error_reporting') & ERR_ALERT))
289 {
290 return;
291 }
292 break;
293
294 // Warning
295 case ERR_WARNING:
296 default:
297 $title = 'Warning';
298 if (!(ini_get('error_reporting') & ERR_WARNING))
299 {
300 return;
301 }
302 break;
303 }
304
305 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
306
307 $this->_message($title, $errstr, 3);
308
309 if ($errno == ERR_FATAL)
310 {
311 exit;
312 }
313 }
314
315 /**
316 * Logs a debug message for verbose output
317 *
318 * @param str Message
319 */
320 function debug($message)
321 {
322 if ($this->debug)
323 {
324 $this->debuginfo[] = $message;
325 }
326 }
327
328 /**
329 * Recursive XSS cleaner
330 *
331 * @param mixed Unsanitized REQUEST data
332 *
333 * @return mixed Sanitized data
334 */
335 function _sanitize_input_recursive($data)
336 {
337 foreach($data AS $key => $value)
338 {
339 if (is_array($value))
340 {
341 $data["$key"] = $this->_sanitize_input_recursive($value);
342 }
343 else
344 {
345 $data["$key"] = $this->sanitize($value);
346 }
347 }
348 return $data;
349 }
350
351 /**
352 * Simple way to protect against HTML attacks with Unicode support
353 *
354 * @param str Unsanitzed text
355 *
356 * @return str Properly protected text that only encodes potential threats
357 */
358 function sanitize($text)
359 {
360 if ($this->magicquotes)
361 {
362 return str_replace(array('<', '>', '\"', '"'), array('&lt;', '&gt;', '&quot;', '&quot;'), $text);
363 }
364 else
365 {
366 return str_replace(array('<', '>', '"'), array('&lt;', '&gt;', '&quot;'), $text);
367 }
368 }
369
370 /**
371 * Takes text that has been processed for HTML and unsanitizes it
372 *
373 * @param str Text that needs to be turned back into HTML
374 *
375 * @return str Unsanitized text
376 */
377 function unsanitize($text)
378 {
379 if ($this->magicquotes)
380 {
381 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '\"'), $text);
382 }
383 else
384 {
385 return str_replace(array('&lt;', '&gt;', '&quot;'), array('<', '>', '"'), $text);
386 }
387 }
388
389 /**
390 * Smart addslashes() that only applies itself it the Magic Quotes GPC is off
391 *
392 * @param str Some string
393 *
394 * @return str String that has slashes added
395 */
396 function escape($str)
397 {
398 global $_isso;
399
400 if ($this->magicquotes)
401 {
402 return $str;
403 }
404 else
405 {
406 if (isset($_isso->db))
407 {
408 if (is_resource($_isso->db->link_id))
409 {
410 return $_isso->db->escape_string($str);
411 }
412 else
413 {
414 return addslashes($str);
415 }
416 }
417 else
418 {
419 return addslashes($str);
420 }
421 }
422 }
423 }
424
425 /**
426 * Global callback used for module calls back to the kernel
427 */
428 $_isso = new Shared_Object_Framework();
429
430 /**
431 * Wrapper for ternary operator that has to be in the global scope
432 *
433 * @param expr Expression
434 * @param mixed If the expression is true
435 * @param mixed If the expression is false
436 *
437 * @return mixed True or false data
438 */
439 function iff($condition, $iftrue, $iffalse = null)
440 {
441 return ($condition) ? ($iftrue) : ($iffalse);
442 }
443
444 /*=====================================================================*\
445 || ###################################################################
446 || # $HeadURL$
447 || # $Id$
448 || ###################################################################
449 \*=====================================================================*/
450 ?>