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