]> src.bluestatic.org Git - isso.git/blob - Register.php
Removing BSLoader and we'll just go with a singleton instance of BSRegister
[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: set the error handler
123 */
124 private function __construct()
125 {
126 set_error_handler(array(self::$instance, '_errorHandler'));
127 }
128
129 // ###################################################################
130 /**
131 * Returns the single instance of the register
132 *
133 * @param string A string param
134 *
135 * @return object BSRegister instance
136 */
137 public static function Instance()
138 {
139 if (self::$instance == null)
140 {
141 self::$instance = new BSRegister();
142 }
143 return self::$instance;
144 }
145
146 // ###################################################################
147 /**
148 * Sets the application name
149 *
150 * @param string Application name
151 */
152 public function setApplication($name)
153 {
154 $this->application = $name;
155 }
156
157 // ###################################################################
158 /**
159 * Gets the application name
160 *
161 * @return string Application name
162 */
163 public function getApplication()
164 {
165 return $this->application;
166 }
167
168 // ###################################################################
169 /**
170 * Sets the application's working path
171 *
172 * @param string Path
173 */
174 public function setAppPath($path)
175 {
176 $this->appPath = BSFunctions::FetchSourcePath($path);
177 }
178
179 // ###################################################################
180 /**
181 * Returns the path to the application
182 *
183 * @return string Application path
184 */
185 public function getAppPath()
186 {
187 return $this->appPath;
188 }
189
190 // ###################################################################
191 /**
192 * Sets the application version
193 *
194 * @param string Application version
195 */
196 public function setAppVersion($vers)
197 {
198 $this->appVersion = $vers;
199 }
200
201 // ###################################################################
202 /**
203 * Gets the application version
204 *
205 * @return string Application version
206 */
207 public function getAppVersion()
208 {
209 return $this->appVersion;
210 }
211
212 // ###################################################################
213 /**
214 * Sets the application's web path, which is the full path from the
215 * server's web root
216 *
217 * @param string Path
218 */
219 public function setWebPath($path)
220 {
221 $this->webPath = BSFunctions::FetchSourcePath($path);
222 }
223
224 // ###################################################################
225 /**
226 * Returns the web path to the application
227 *
228 * @return string Application's web path
229 */
230 public function getWebPath()
231 {
232 return $this->webPath;
233 }
234
235 // ###################################################################
236 /**
237 * Sets the debug state
238 *
239 * @param bool Debug mode on?
240 */
241 public function setDebug($debug)
242 {
243 $this->debug = $debug;
244 }
245
246 // ###################################################################
247 /**
248 * Gets the debug mode state
249 *
250 * @return bool Debug mode on?
251 */
252 public function getDebug()
253 {
254 return $this->debug;
255 }
256
257 // ###################################################################
258 /**
259 * Registers a value in the master registry. You cannot overwrite
260 * values. You must first unregister() them if you wish to do so.
261 *
262 * @param string Registry key
263 * @param mixed Value to register
264 */
265 public function register($key, $value)
266 {
267 if (isset($this->registry["$key"]))
268 {
269 trigger_error('Cannot overwrite a key in the registry');
270 return;
271 }
272
273 $this->registry["$key"] = $value;
274 }
275
276 // ###################################################################
277 /**
278 * Unregisters a value from the registry. This removes all traces of
279 * it from this object.
280 *
281 * @param string Registry key
282 */
283 public function unregister($key)
284 {
285 if (!isset($this->registry["$key"]))
286 {
287 trigger_error('You cannot unregister a key that does not exist');
288 return;
289 }
290
291 unset($this->registry["$key"]);
292 }
293
294 // ###################################################################
295 /**
296 * This gets a value from the registry with a specific key
297 *
298 * @param string The key
299 *
300 * @return mixed Value in the registry for key
301 */
302 public function get($key)
303 {
304 if (!isset($this->registry["$key"]))
305 {
306 trigger_error('Cannot access the registry with a non-existent key');
307 return;
308 }
309
310 return $this->registry["$key"];
311 }
312
313 // ###################################################################
314 /**
315 * Returns the entire registry stack
316 *
317 * @return array Complete registry
318 */
319 public function getAll()
320 {
321 return $this->registry;
322 }
323
324 // ###################################################################
325 /**
326 * Adds a debug message to the array. This only works when debug mode
327 * is enabled.
328 *
329 * @param string Debug message
330 */
331 public function debug($msg)
332 {
333 if ($this->debug)
334 {
335 $this->debugInfo[] = $msg;
336 }
337 }
338
339 // ###################################################################
340 /**
341 * Loads the specified module.
342 *
343 * @param string Module name
344 *
345 * @return object Instantiated module
346 */
347 public static function LoadModule($name)
348 {
349 @include_once("ISSO/$name.php");
350
351 $class = "BS$name";
352
353 if (!class_exists($class))
354 {
355 trigger_error('Specifed module does not conform to the ISSO specification, or the class is missing');
356 return;
357 }
358
359 return new $class;
360 }
361
362 // ###################################################################
363 /**
364 * Prints an ISSO message
365 *
366 * @param string The title of the message
367 * @param string The content of the message
368 * @param integer Type of message to be printed
369 * @param bool Return the output?
370 * @param bool Show the debug stack?
371 * @param integer Message width
372 *
373 * @return mixed Output or null
374 */
375 public static function Message($title, $message, $type, $return = false, $stack = true, $width = 500)
376 {
377 switch ($type)
378 {
379 // Message
380 case 1:
381 $prefix = 'Message';
382 $color = '#669900';
383 $font = '#000000';
384 break;
385
386 // Warning
387 case 2:
388 $prefix = 'Warning';
389 $color = '#003399';
390 $font = '#FFFFFF';
391 break;
392
393 case 3:
394 $prefix = 'Error';
395 $color = '#990000';
396 $font = '#EFEFEF';
397 break;
398 }
399
400 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;\">";
401 echo "\n<tr style=\"color: $font; text-align: left\">\n\t<td><strong>$prefix: $title</strong></td>\n</tr>";
402 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td>$message</td>\n</tr>";
403 if ($stack AND self::Instance()->getDebug())
404 {
405 echo "\n<tr style=\"background-color: #FFFFFF; text-align: left\">\n\t<td><strong>Debug Stack:</strong> <pre>";
406 debug_print_backtrace();
407 echo "</pre></td>\n</tr>";
408 }
409 echo "\n</table>\n<br />\n";
410 }
411
412 // ###################################################################
413 /**
414 * Custom error handler for ISSO; only handle E_WARNING, E_NOTICE,
415 * 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 private function _errorHandler($errno, $errstr, $errfile, $errline, $errcontext)
424 {
425 $level = ini_get('error_reporting');
426
427 switch ($errno)
428 {
429 // So we don't need to specify the error type in trigger_error(), all E_USER_NOTICEs
430 // are fatal and automatically kill the script
431 case E_USER_NOTICE:
432 $title = 'Fatal';
433 $mode = 3;
434 break;
435
436 // A non-fatal but important warning
437 case E_WARNING:
438 $title = 'Warning';
439 $mode = 2;
440 if (!($level & E_WARNING))
441 {
442 return;
443 }
444 break;
445
446 // A non-fatal notice that should all but be ignored unless in dev environments
447 case E_NOTICE:
448 case E_STRICT:
449 $title = 'Notice';
450 $mode = 1;
451 if (!($level & E_NOTICE))
452 {
453 return;
454 }
455 break;
456
457 case E_USER_WARNING:
458 case E_USER_NOTICE:
459 default:
460 trigger_error('The only error types supported are E_USER_NOTICE (fatal), E_WARNING, and E_NOTICE');
461 break;
462 }
463
464 $errstr .= " in <strong>$errfile</strong> on line <strong>$errline</strong>";
465
466 $errstr = str_replace(array(getcwd(), dirname(getcwd())), '', $errstr);
467
468 self::Message($title, $errstr, $mode);
469
470 if ($errno == E_USER_NOTICE)
471 {
472 exit;
473 }
474 }
475
476 // ###################################################################
477 /**
478 * Creates a table that explains the error reporting levels and their
479 * state
480 */
481 public static function ExplainErrorReporting()
482 {
483 $levels = array(
484 'E_ERROR' => E_ERROR,
485 'E_WARNING' => E_WARNING,
486 'E_PARSE' => E_PARSE,
487 'E_NOTICE' => E_NOTICE,
488 'E_CORE_ERROR' => E_CORE_ERROR,
489 'E_CORE_WARNING' => E_CORE_WARNING,
490 'E_COMPILE_ERROR' => E_COMPILE_ERROR,
491 'E_COMPILE_WARNING' => E_COMPILE_WARNING,
492 'E_USER_ERROR' => E_USER_ERROR,
493 'E_USER_WARNING' => E_USER_WARNING,
494 'E_USER_NOTICE' => E_USER_NOTICE,
495 'E_ALL' => E_ALL,
496 'E_STRICT' => E_STRICT
497 );
498
499 $table = '<table cellspacing="0" cellpadding="2" border="0">';
500
501 foreach ($levels AS $name => $value)
502 {
503 $table .= '
504 <tr>
505 <td>' . $name . '</td>
506 <td>' . (ini_get('error_reporting') & $value) . '</td>
507 </tr>';
508 }
509
510 $table .= '
511 </table>';
512
513 self::Message('Error Reporting', $table, 1);
514 }
515 }
516
517 /*=====================================================================*\
518 || ###################################################################
519 || # $HeadURL$
520 || # $Id$
521 || ###################################################################
522 \*=====================================================================*/
523 ?>