Update code_sniffer build.xml file to be executable on our system
[phpbb.git] / phpBB / includes / core / core.php
blob2b2c4bea13a09faaff70ce1b0bf9c1a285d8ae41
1 <?php
2 /**
4 * @package core
5 * @version $Id$
6 * @copyright (c) 2008 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit();
19 /**
20 * phpBB abstract class
22 * @package core
23 * @author acydburn
25 abstract class phpbb
27 /**
28 * The phpBB template object
30 public static $template = NULL;
32 /**
33 * The phpBB user object
35 public static $user = NULL;
37 /**
38 * The phpBB database object
40 public static $db = NULL;
42 /**
43 * The phpBB cache system object
45 public static $acm = NULL;
47 /**
48 * The phpBB permission object
50 public static $acl = NULL;
52 /**
53 * The phpBB plugins object
55 public static $plugins = NULL;
57 /**
58 * The phpBB core url object
59 * Responsible for handling URL-related tasks as well as redirects, etc.
61 public static $url = NULL;
63 /**
64 * The phpBB core security object.
65 * Responsible for handling security-related tasks, for example password handling, random number generation...
67 public static $security = NULL;
69 /**
70 * The phpBB core system object
71 * Responsible for handling file/server tasks.
73 public static $system = NULL;
75 /**
76 * The phpBB API object
78 public static $api = NULL;
80 /**
81 * @var array The phpBB configuration array
83 public static $config = array();
85 /**
86 * @var array The base configuration array
88 public static $base_config = array(
89 'table_prefix' => 'phpbb_',
90 'admin_folder' => 'adm',
91 'acm_type' => 'file',
93 'config_set' => false,
94 'extensions_set' => false,
96 'memory_usage' => 0,
98 'debug' => false,
99 'debug_extra' => false,
100 'installed' => false,
104 * @var array Last notice occurred in message handler
106 public static $last_notice = array(
107 'file' => '',
108 'line' => 0,
109 'message' => '',
110 'errno' => E_NOTICE,
113 /**#@+
114 * Permission constant
116 const ACL_NEVER = 0;
117 const ACL_YES = 1;
118 const ACL_NO = -1;
119 /**#@-*/
121 /**#@+
122 * Global constant for {@link phpbb::$system->chmod()}
124 const CHMOD_ALL = 7;
125 const CHMOD_READ = 4;
126 const CHMOD_WRITE = 2;
127 const CHMOD_EXECUTE = 1;
128 /**#@-*/
130 /**#@+
131 * Constant defining plugin mode for objects
133 const METHOD_ADD = 1;
134 const METHOD_OVERRIDE = 2;
135 const METHOD_INJECT = 4;
136 /**#@-*/
138 /**#@+
139 * Constant defining plugin mode for functions
141 const FUNCTION_OVERRIDE = 1;
142 const FUNCTION_INJECT = 2;
143 /**#@-*/
145 /**#@+
146 * Constant to define user level. See {@link phpbb::$user phpbb::$user}
148 const USER_NORMAL = 0;
149 const USER_INACTIVE = 1;
150 const USER_IGNORE = 2;
151 const USER_FOUNDER = 3;
152 /**#@-*/
155 * @var array a static array holding custom objects
157 public static $instances = NULL;
160 * We do not want this class instantiable
162 private function ___construct() { }
165 * A failover error handler to handle errors before we assign our own error handler
167 * @access public
169 public static function error_handler($errno, $errstr, $errfile, $errline)
171 throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
175 * Set base configuration - called from config.php file
177 public static function set_config($config)
179 phpbb::$base_config = array_merge(phpbb::$base_config, $config);
180 phpbb::$base_config['config_set'] = true;
182 if (phpbb::$base_config['debug_extra'] && function_exists('memory_get_usage'))
184 phpbb::$base_config['memory_usage'] = memory_get_usage();
187 // Load Extensions
188 if (!empty(phpbb::$base_config['extensions']) && !phpbb::$base_config['extensions_set'])
190 $load_extensions = explode(',', phpbb::$base_config['extensions']);
192 foreach ($load_extensions as $extension)
194 @dl(trim($extension));
197 phpbb::$base_config['extensions_set'] = true;
202 * Get instance of static property
204 * @param string $variable The name of the instance to retrieve.
206 * @return mixed The property (object/array/...) registered with this name
207 * @access public
209 public static function get_instance($variable)
211 if (!self::registered($variable))
213 return self::register($variable);
216 // Please do not try to change it to (expr) ? (true) : (false) - it will not work. ;)
217 if (property_exists('phpbb', $variable))
219 return self::$$variable;
221 else
223 return self::$instances[$variable];
228 * Check if the variable is already assigned
230 * @param string $variable The name of the instance to check
232 * @return bool True if the instance is registered, false if not.
233 * @access public
235 public static function registered($variable)
237 if (property_exists('phpbb', $variable))
239 return (self::$$variable !== NULL) ? true : false;
242 return (isset(self::$instances[$variable]) && self::$instances[$variable] !== NULL) ? true : false;
246 * Simpler method to access assigned instances.
247 * (Overloading is not possible here due to the object being static and our use of PHP 5.2.x+.)
249 * @param string $variable The instance name to retrieve
251 * @return mixed The instance
252 * @access public
254 public static function get($variable)
256 // No error checking done here... returned right away
257 return self::$instances[$variable];
261 * Register new class/object.
262 * Any additional parameter will be forwarded to the class instantiation.
264 * @param string $variable The resulting instance name.
265 * If a property with the given name exists, it will be assigned.
266 * Else it will be put in the {@link $instances intances} array
267 * @param string $class Define a custom class name.
268 * This is useful if the class used does not abide to the rules (phpbb_{$class}).
269 * @param string|array $includes Define additional files/includes required for this class to be correctly set up. Files are expected to be in /includes/.
270 * @param mixed $arguments,... Any number of additional arguments passed to the constructor of the object to create
272 * @return mixed The instance of the created object
273 * @access public
275 public static function register($variable, $class = false, $includes = false)
277 if (self::registered($variable))
279 return self::get_instance($variable);
282 $arguments = (func_num_args() > 3) ? array_slice(func_get_args(), 3) : array();
283 $class = ($class === false) ? 'phpbb_' . $variable : $class;
285 if ($includes !== false)
287 if (!is_array($includes))
289 $includes = array($includes);
292 foreach ($includes as $file)
294 require_once PHPBB_ROOT_PATH . 'includes/' . $file . '.' . PHP_EXT;
298 $reflection = new ReflectionClass($class);
300 if (!$reflection->isInstantiable())
302 throw new Exception('Assigned classes need to be instantiated.');
305 if (!property_exists('phpbb', $variable))
307 self::$instances[$variable] = (sizeof($arguments)) ? call_user_func_array(array($reflection, 'newInstance'), $arguments) : $reflection->newInstance();
309 else
311 self::$$variable = (sizeof($arguments)) ? call_user_func_array(array($reflection, 'newInstance'), $arguments) : $reflection->newInstance();
314 return self::get_instance($variable);
318 * Instead of registering we also can assign a variable. This is helpful if we have an application builder or use a factory.
320 * @param string $variable The resulting instance name.
321 * If a property with the given name exists, it will be assigned.
322 * Else it will be put in the {@link $instances intances} array
323 * @param mixed $object The variable to assign to the instance
325 * @return mixed The instance
326 * @access public
328 public static function assign($variable, $object)
330 if (self::registered($variable))
332 return self::get_instance($variable);
335 if (!property_exists('phpbb', $variable))
337 self::$instances[$variable] = $object;
339 else
341 self::$$variable = $object;
344 return self::get_instance($variable);
348 * Unset/unregister a specific object.
350 * @param string $variable The name of the instance to unset
351 * @access public
353 public static function unregister($variable)
355 if (!self::registered($variable))
357 return;
360 if (!property_exists('phpbb', $variable))
362 unset(self::$instances[$variable]);
364 else
366 self::$$variable = NULL;
371 * Function to return to a clean state, unregistering everything. This is helpful for unit tests if you want to return to a "clean state"
373 * @access public
375 public static function reset()
377 $class_vars = array_keys(get_class_vars('phpbb'));
378 $class_vars = array_merge(array_keys(self::$instances), $class_vars);
380 foreach ($class_vars as $variable)
382 self::unregister($variable);
388 * phpBB SPL Autoload Function. A phpbb_ prefix will be stripped from the class name.
390 * The files this function tries to include are:
391 * includes/{$class_name}/bootstrap.php
392 * includes/{$class_name}/index.php
393 * Additionally, every _ within $class_name is replaced by / for the following directories:
394 * includes/{$class_name}.php
395 * includes/classes/{$class_name}.php
397 * @param string $class_name The class name. An existing phpbb_ prefix will be removed.
399 function __phpbb_autoload($class_name)
401 if (strpos($class_name, 'phpbb_') === 0)
403 $class_name = substr($class_name, 6);
406 $class_name = basename($class_name);
408 $filenames = array(
409 'includes/' . $class_name . '/bootstrap',
410 'includes/' . $class_name . '/index',
411 'includes/' . $class_name,
412 'includes/classes/' . $class_name,
415 if (strpos($class_name, '_') !== false)
417 $class_name = str_replace('_', '/', $class_name);
419 $filenames = array_merge($filenames, array(
420 'includes/' . $class_name,
421 'includes/classes/' . $class_name,
425 foreach ($filenames as $filename)
427 if (file_exists(PHPBB_ROOT_PATH . $filename . '.' . PHP_EXT))
429 include PHPBB_ROOT_PATH . $filename . '.' . PHP_EXT;
430 return;
436 class phpbb_exception extends Exception