Removing the SVN constant option and the ISSO_MT_START (thanks to $_SERVER['REQUEST_T...
[isso.git] / App.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # Blue Static ISSO Framework
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 Application Root (App.php)
24 *
25 * @package ISSO
26 */
27
28 // we need PHP 5.2.0 to run
29 if (!function_exists('array_fill_keys'))
30 {
31 print('You need PHP version 5.2.0 or newer to run ISSO');
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 * Application Class
58 *
59 * This is an ISSO application 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 BSApp
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 = null;
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 $this->registry = new BSVariableRegistry();
127 }
128
129 // ###################################################################
130 /**
131 * Returns the single instance of the register
132 *
133 * @param string A string param
134 *
135 * @return object BSApp instance
136 */
137 private static function _Instance()
138 {
139 if (!self::$instance)
140 {
141 self::$instance = new BSApp();
142 }
143 return self::$instance;
144 }
145
146 // ###################################################################
147 /**
148 * Sets the application name
149 *
150 * @param string Application name
151 */
152 public static function SetApplication($name)
153 {
154 self::_Instance()->application = $name;
155 }
156
157 // ###################################################################
158 /**
159 * Gets the application name
160 *
161 * @return string Application name
162 */
163 public static function GetApplication()
164 {
165 return self::_Instance()->application;
166 }
167
168 // ###################################################################
169 /**
170 * Sets the application's working path
171 *
172 * @param string Path
173 */
174 public static function SetAppPath($path)
175 {
176 self::_Instance()->appPath = BSFunctions::FetchSourcePath($path);
177 }
178
179 // ###################################################################
180 /**
181 * Returns the path to the application
182 *
183 * @return string Application path
184 */
185 public static function GetAppPath()
186 {
187 return self::_Instance()->appPath;
188 }
189
190 // ###################################################################
191 /**
192 * Sets the application version
193 *
194 * @param string Application version
195 */
196 public static function SetAppVersion($vers)
197 {
198 self::_Instance()->appVersion = $vers;
199 }
200
201 // ###################################################################
202 /**
203 * Gets the application version
204 *
205 * @return string Application version
206 */
207 public static function GetAppVersion()
208 {
209 return self::_Instance()->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 static function SetWebPath($path)
220 {
221 self::_Instance()->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 static function GetWebPath()
231 {
232 return self::_Instance()->webPath;
233 }
234
235 // ###################################################################
236 /**
237 * Sets the debug state
238 *
239 * @param bool Debug mode on?
240 */
241 public static function SetDebug($debug)
242 {
243 self::_Instance()->debug = $debug;
244 }
245
246 // ###################################################################
247 /**
248 * Gets the debug mode state
249 *
250 * @return bool Debug mode on?
251 */
252 public static function GetDebug()
253 {
254 return self::_Instance()->debug;
255 }
256
257 // ###################################################################
258 /**
259 * Returns the registry object without it ever being able to be cleared
260 * or unset
261 *
262 * @return StdClass
263 */
264 public static function Registry()
265 {
266 return self::_Instance()->registry;
267 }
268
269 // ###################################################################
270 /**
271 * Adds a debug message to the array. This only works when debug mode
272 * is enabled.
273 *
274 * @param string Debug message
275 */
276 public static function Debug($msg)
277 {
278 if (self::_Instance()->debug)
279 {
280 self::_Instance()->debugInfo[] = $msg;
281 }
282 }
283
284 // ###################################################################
285 /**
286 * Returns a <select> menu of all the debug notices
287 *
288 * @return string Debug list
289 */
290 public static function GetDebugList()
291 {
292 $output = '<select><option>Debug Notices (' . sizeof(self::_Instance()->debugInfo) . ')</option>';
293 foreach (self::_Instance()->debugInfo AS $notice)
294 {
295 $output .= "<option>--- $notice</option>";
296 }
297 return "$output</select>";
298 }
299
300 // ###################################################################
301 /**
302 * Loads the specified module.
303 *
304 * @param string Module name
305 *
306 * @return object Instantiated module
307 */
308 public static function LoadModule($name)
309 {
310 if (self::GetDebug())
311 {
312 include_once("ISSO/$name.php");
313 }
314 else
315 {
316 @include_once("ISSO/$name.php");
317 }
318
319 $class = "BS$name";
320
321 if (!class_exists($class))
322 {
323 throw new Exception('Specifed module does not conform to the ISSO specification, or the class is missing');
324 return;
325 }
326
327 return new $class;
328 }
329
330 // ###################################################################
331 /**
332 * This function is used by other framework modules to check and see if
333 * the passed array of module names have been loaded. If not, this will
334 * throw an error informing the developer that the given modules are
335 * required in order for the framework to work.
336 *
337 * @param array Array of module names to check for loadedness
338 */
339 public static function RequiredModules($modules)
340 {
341 foreach ($modules AS $module)
342 {
343 if (self::Registry()->getType($module) == null AND !class_exists("BS$module"))
344 {
345 throw new Exception('The ' . $module . ' module is required in order to use this framework module');
346 }
347 }
348 }
349 }
350
351 /**
352 * Variable Registry
353 *
354 * This class can be used to store unlimited number of properties. It is a (make believe)
355 * inner class to BSApp and cannot be instantiated outside of it.
356 *
357 * @author Blue Static
358 * @copyright Copyright (c)2002 - [#]year[#], Blue Static
359 * @version $Id$
360 * @package ISSO
361 *
362 */
363 class BSVariableRegistry
364 {
365 // ###################################################################
366 /**
367 * Constructor: only able to be called by BSApp
368 */
369 public function __construct()
370 {
371 $bt = debug_backtrace();
372 if (!isset($bt[1]['class']) OR $bt[1]['class'] != 'BSApp')
373 {
374 exit('You cannot instantiate ' . __CLASS__ . ' directly. It is an internal class.');
375 }
376 }
377
378 // ###################################################################
379 /**
380 * If you try and get a non-existent property, throw a new exception
381 */
382 public function __get($name)
383 {
384 throw new Exception('Failed to get nonexistent property "' . $name . '"');
385 }
386
387 // ###################################################################
388 /**
389 * Returns the first value of the given type in the variable registry
390 *
391 * @return mixed
392 */
393 public function getType($class)
394 {
395 $class = 'BS' . $class;
396 foreach ($this AS $object)
397 {
398 if ($object instanceof $class)
399 {
400 return $object;
401 }
402 }
403 }
404
405 // ###################################################################
406 /**
407 * Returns all the objects in the registry by iterating over it
408 *
409 * @return array
410 */
411 public function allObjects()
412 {
413 $registry = array();
414 foreach ($this AS $key => $value)
415 {
416 $registry[$key] = $value;
417 }
418 return $registry;
419 }
420 }
421
422 /*=====================================================================*\
423 || ###################################################################
424 || # $HeadURL$
425 || # $Id$
426 || ###################################################################
427 \*=====================================================================*/
428 ?>