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