Making the entire BSRegister class static
[isso.git] / Register.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework [#]issoversion[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
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 * ISSO Registry (Register.php)
24 *
25 * @package ISSO
26 */
27
28 // we need PHP5 to run
29 if (!function_exists('stripos'))
30 {
31 trigger_error('You need PHP version 5.0.0 or newer to run ISSO', E_USER_ERROR);
32 exit;
33 }
34
35 // get rid of register_globals
36 if ((bool)ini_get('register_globals') === true)
37 {
38 $superglobals = array('_GET', '_COOKIE', '_FILES', '_POST', '_SERVER', '_ENV');
39 foreach ($superglobals AS $global)
40 {
41 if (is_array(${$global}))
42 {
43 foreach (${$global} AS $_key => $_val)
44 {
45 if (isset(${$_key}))
46 {
47 unset(${$_key});
48 }
49 }
50 }
51 }
52 }
53
54 require_once('ISSO/Functions.php');
55
56 /**
57 * Register Class
58 *
59 * This is an ISSO registry class. It holds all of the ISSO system variables as well
60 * as serving as an object registry that is avaliable in the global scope to prevent
61 * globalization of variables. There can only be one instance of this existing
62 * at any given time.
63 *
64 * @author Blue Static
65 * @copyright Copyright ©2002 - [#]year[#], Blue Static
66 * @version $Revision$
67 * @package ISSO
68 *
69 */
70 class BSRegister
71 {
72 /**
73 * Instance of this class
74 * @var object
75 */
76 private static $instance;
77
78 /**
79 * Application name
80 * @var string
81 */
82 private $application = 'Unknown ISSO Project';
83
84 /**
85 * Application path
86 * @var string
87 */
88 private $appPath;
89
90 /**
91 * Application version
92 * @var string
93 */
94 private $appVersion;
95
96 /**
97 * Web path
98 * @var string
99 */
100 private $webPath;
101
102 /**
103 * Debug mode?
104 * @var bool
105 */
106 private $debug = false;
107
108 /**
109 * The master registry list
110 * @var array
111 */
112 private $registry = array();
113
114 /**
115 * An array of debug messages
116 * @var array
117 */
118 private $debugInfo = array();
119
120 // ###################################################################
121 /**
122 * Constructor
123 */
124 private function __construct() {}
125
126 // ###################################################################
127 /**
128 * Returns the single instance of the register
129 *
130 * @param string A string param
131 *
132 * @return object BSRegister instance
133 */
134 private static function Instance()
135 {
136 if (self::$instance == null)
137 {
138 self::$instance = new BSRegister();
139 set_error_handler(array(self::$instance, '_errorHandler'));
140 }
141 return self::$instance;
142 }
143
144 // ###################################################################
145 /**
146 * Sets the application name
147 *
148 * @param string Application name
149 */
150 public static function SetApplication($name)
151 {
152 self::Instance()->application = $name;
153 }
154
155 // ###################################################################
156 /**
157 * Gets the application name
158 *
159 * @return string Application name
160 */
161 public static function GetApplication()
162 {
163 return self::Instance()->application;
164 }
165
166 // ###################################################################
167 /**
168 * Sets the application's working path
169 *
170 * @param string Path
171 */
172 public static function SetAppPath($path)
173 {
174 self::Instance()->appPath = BSFunctions::FetchSourcePath($path);
175 }
176
177 // ###################################################################
178 /**
179 * Returns the path to the application
180 *
181 * @return string Application path
182 */
183 public static function GetAppPath()
184 {
185 return self::Instance()->appPath;
186 }
187
188 // ###################################################################
189 /**
190 * Sets the application version
191 *
192 * @param string Application version
193 */
194 public static function SetAppVersion($vers)
195 {
196 self::Instance()->appVersion = $vers;
197 }
198
199 // ###################################################################
200 /**
201 * Gets the application version
202 *
203 * @return string Application version
204 */
205 public static function GetAppVersion()
206 {
207 return self::Instance()->appVersion;
208 }
209
210 // ###################################################################
211 /**
212 * Sets the application's web path, which is the full path from the
213 * server's web root
214 *
215 * @param string Path
216 */
217 public static function SetWebPath($path)
218 {
219 self::Instance()->webPath = BSFunctions::FetchSourcePath($path);
220 }
221
222 // ###################################################################
223 /**
224 * Returns the web path to the application
225 *
226 * @return string Application's web path
227 */
228 public static function GetWebPath()
229 {
230 return self::Instance()->webPath;
231 }
232
233 // ###################################################################
234 /**
235 * Sets the debug state
236 *
237 * @param bool Debug mode on?
238 */
239 public static function SetDebug($debug)
240 {
241 self::Instance()->debug = $debug;
242 }
243
244 // ###################################################################
245 /**
246 * Gets the debug mode state
247 *
248 * @return bool Debug mode on?
249 */
250 public static function GetDebug()
251 {
252 return self::Instance()->debug;
253 }
254
255 // ###################################################################
256 /**
257 * Registers a value in the master registry. You cannot overwrite
258 * values. You must first unregister() them if you wish to do so.
259 *
260 * @param string Registry key
261 * @param mixed Value to register
262 */
263 public static function Register($key, $value)
264 {
265 if (isset(self::Instance()->registry["$key"]))
266 {
267 trigger_error('Cannot overwrite a key in the registry');
268 return;
269 }
270
271 self::Instance()->registry["$key"] = $value;
272 }
273
274 // ###################################################################
275 /**
276 * Unregisters a value from the registry. This removes all traces of
277 * it from this object.
278 *
279 * @param string Registry key
280 */
281 public static function Unregister($key)
282 {
283 if (!isset(self::Instance()->registry["$key"]))
284 {
285 trigger_error('You cannot unregister a key that does not exist');
286 return;
287 }
288
289 unset(self::Instance()->registry["$key"]);
290 }
291
292 // ###################################################################
293 /**
294 * This gets a value from the registry with a specific key
295 *
296 * @param string The key
297 *
298 * @return mixed Value in the registry for key
299 */
300 public static function Get($key)
301 {
302 if (!isset(self::Instance()->registry["$key"]))
303 {
304 trigger_error('Cannot access the registry with a non-existent key');
305 return;
306 }
307
308 return self::Instance()->registry["$key"];
309 }
310
311 // ###################################################################
312 /**
313 * Returns the entire registry stack
314 *
315 * @return array Complete registry
316 */
317 public static function GetAll()
318 {
319 return self::Instance()->registry;
320 }
321
322 // ###################################################################
323 /**
324 * Adds a debug message to the array. This only works when debug mode
325 * is enabled.
326 *
327 * @param string Debug message
328 */
329 public static function Debug($msg)
330 {
331 if (self::Instance()->debug)
332 {
333 self::Instance()->debugInfo[] = $msg;
334 }
335 }
336
337 // ###################################################################
338 /**
339 * Loads the specified module.
340 *
341 * @param string Module name
342 *
343 * @return object Instantiated module
344 */
345 public static function LoadModule($name)
346 {
347 @include_once("ISSO/$name.php");
348
349 $class = "BS$name";
350
351 if (!class_exists($class))
352 {
353 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
354 return;
355 }
356
357 return new $class;
358 }
359
360 // ###################################################################
361 /**
362 * Prints an ISSO message
363 *
364 * @param string The title of the message
365 * @param string The content of the message
366 * @param integer Type of message to be printed
367 * @param bool Return the output?
368 * @param bool Show the debug stack?
369 * @param integer Message width
370 *
371 * @return mixed Output or null
372 */
373 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
374 {
375 switch ($type)
376 {
377 // Message
378 case 1:
379 $prefix = 'Message';
380 $color = '#669900';
381 $font = '#000000';
382 break;
383
384 // Warning
385 case 2:
386 $prefix = 'Warning';
387 $color = '#003399';
388 $font = '#FFFFFF';
389 break;
390
391 case 3:
392 $prefix = 'Error';
393 $color = '#990000';
394 $font = '#EFEFEF';
395 break;
396 }
397
398 echo "\n<br />\n<table cellpadding=\"4\" cellspacing=\"1\" border=\"0\" width=\"$width\" style=\"background-color: $color; color: black; font-family: Verdana, sans-serif; font-size: 12px;\">";
399 echo "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
400 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
401 if ($stack AND self::Instance()->getDebug())
402 {
403 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>";
404 debug_print_backtrace();
405 echo "</pre></td>\n</tr>";
406 }
407 echo "\n</table>\n<br />\n";
408 }
409
410 // ###################################################################
411 /**
412 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
413 * E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE
414 *
415 * @param integer Error number
416 * @param string Error message string
417 * @param string File that contains the error
418 * @param string The line number of the error
419 * @param string The active symbol table at which point the error occurred
420 */
421 private function _errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
422 {
423 $level = ini_get('error_reporting');
424
425 switch ($errno)
426 {
427 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
428 // are fatal and automatically kill the script
429 case E_USER_NOTICE:
430 $title = 'Fatal';
431 $mode = 3;
432 break;
433
434 // A non-fatal but important warning
435 case E_WARNING:
436 $title = 'Warning';
437 $mode = 2;
438 if (!($level & E_WARNING))
439 {
440 return;
441 }
442 break;
443
444 // A non-fatal notice that should all but be ignored unless in dev environments
445 case E_NOTICE:
446 case E_STRICT:
447 $title = 'Notice';
448 $mode = 1;
449 if (!($level & E_NOTICE))
450 {
451 return;
452 }
453 break;
454
455 case E_USER_WARNING:
456 case E_USER_NOTICE:
457 default:
458 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE');
459 break;
460 }
461
462 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
463
464 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
465
466 self::Message($title, $errstr, $mode);
467
468 if ($errno == E_USER_NOTICE)
469 {
470 exit;
471 }
472 }
473
474 // ###################################################################
475 /**
476 * Creates a table that explains the error reporting levels and their
477 * state
478 */
479 public static function ExplainErrorReporting()
480 {
481 $levels = array(
482 'E_ERROR' => E_ERROR,
483 'E_WARNING' => E_WARNING,
484 'E_PARSE' => E_PARSE,
485 'E_NOTICE' => E_NOTICE,
486 'E_CORE_ERROR' => E_CORE_ERROR,
487 'E_CORE_WARNING' => E_CORE_WARNING,
488 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
489 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
490 'E_USER_ERROR' => E_USER_ERROR,
491 'E_USER_WARNING' => E_USER_WARNING,
492 'E_USER_NOTICE' => E_USER_NOTICE,
493 'E_ALL' => E_ALL,
494 'E_STRICT' => E_STRICT
495 );
496
497 $table = '<table cellspacing="0" cellpadding="2" border="0">';
498
499 foreach ($levels AS $name => $value)
500 {
501 $table .= '
502 <tr>
503 <td>' . $name . '</td>
504 <td>' . (ini_get('error_reporting') & $value) . '</td>
505 </tr>';
506 }
507
508 $table .= '
509 </table>';
510
511 self::Message('Error Reporting', $table, 1);
512 }
513 }
514
515 /*=====================================================================*\
516 || ###################################################################
517 || # $HeadURL$
518 || # $Id$
519 || ###################################################################
520 \*=====================================================================*/
521 ?>