aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes/core/core.php
blob: 2b2c4bea13a09faaff70ce1b0bf9c1a285d8ae41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
<?php
/**
*
* @package core
* @version $Id$
* @copyright (c) 2008 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

/**
* @ignore
*/
if (!defined('IN_PHPBB'))
{
	exit();
}

/**
* phpBB abstract class
*
* @package core
* @author acydburn
*/
abstract class phpbb
{
	/**
	* The phpBB template object
	*/
	public static $template = NULL;

	/**
	* The phpBB user object
	*/
	public static $user = NULL;

	/**
	* The phpBB database object
	*/
	public static $db = NULL;

	/**
	* The phpBB cache system object
	*/
	public static $acm = NULL;

	/**
	* The phpBB permission object
	*/
	public static $acl = NULL;

	/**
	* The phpBB plugins object
	*/
	public static $plugins = NULL;

	/**
	* The phpBB core url object
	* Responsible for handling URL-related tasks as well as redirects, etc.
	*/
	public static $url = NULL;

	/**
	* The phpBB core security object.
	* Responsible for handling security-related tasks, for example password handling, random number generation...
	*/
	public static $security = NULL;

	/**
	* The phpBB core system object
	* Responsible for handling file/server tasks.
	*/
	public static $system = NULL;

	/**
	* The phpBB API object
	*/
	public static $api = NULL;

	/**
	* @var array The phpBB configuration array
	*/
	public static $config = array();

	/**
	* @var array The base configuration array
	*/
	public static $base_config = array(
		'table_prefix'		=> 'phpbb_',
		'admin_folder'		=> 'adm',
		'acm_type'			=> 'file',

		'config_set'		=> false,
		'extensions_set'	=> false,

		'memory_usage'		=> 0,

		'debug'				=> false,
		'debug_extra'		=> false,
		'installed'			=> false,
	);

	/**
	* @var array Last notice occurred in message handler
	*/
	public static $last_notice = array(
		'file'		=> '',
		'line'		=> 0,
		'message'	=> '',
		'errno'		=> E_NOTICE,
	);

	/**#@+
	* Permission constant
	*/
	const ACL_NEVER = 0;
	const ACL_YES = 1;
	const ACL_NO = -1;
	/**#@-*/

	/**#@+
	* Global constant for {@link phpbb::$system->chmod()}
	*/
	const CHMOD_ALL = 7;
	const CHMOD_READ = 4;
	const CHMOD_WRITE = 2;
	const CHMOD_EXECUTE = 1;
	/**#@-*/

	/**#@+
	* Constant defining plugin mode for objects
	*/
	const METHOD_ADD = 1;
	const METHOD_OVERRIDE = 2;
	const METHOD_INJECT = 4;
	/**#@-*/

	/**#@+
	* Constant defining plugin mode for functions
	*/
	const FUNCTION_OVERRIDE = 1;
	const FUNCTION_INJECT = 2;
	/**#@-*/

	/**#@+
	* Constant to define user level. See {@link phpbb::$user phpbb::$user}
	*/
	const USER_NORMAL = 0;
	const USER_INACTIVE = 1;
	const USER_IGNORE = 2;
	const USER_FOUNDER = 3;
	/**#@-*/

	/**
	* @var array a static array holding custom objects
	*/
	public static $instances = NULL;

	/**
	* We do not want this class instantiable
	*/
	private function ___construct() { }

	/**
	* A failover error handler to handle errors before we assign our own error handler
	*
	* @access public
	*/
	public static function error_handler($errno, $errstr, $errfile, $errline)
	{
		throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
	}

	/**
	* Set base configuration - called from config.php file
	*/
	public static function set_config($config)
	{
		phpbb::$base_config = array_merge(phpbb::$base_config, $config);
		phpbb::$base_config['config_set'] = true;

		if (phpbb::$base_config['debug_extra'] && function_exists('memory_get_usage'))
		{
			phpbb::$base_config['memory_usage'] = memory_get_usage();
		}

		// Load Extensions
		if (!empty(phpbb::$base_config['extensions']) && !phpbb::$base_config['extensions_set'])
		{
			$load_extensions = explode(',', phpbb::$base_config['extensions']);

			foreach ($load_extensions as $extension)
			{
				@dl(trim($extension));
			}

			phpbb::$base_config['extensions_set'] = true;
		}
	}

	/**
	* Get instance of static property
	*
	* @param string	$variable	The name of the instance to retrieve.
	*
	* @return mixed	The property (object/array/...) registered with this name
	* @access public
	*/
	public static function get_instance($variable)
	{
		if (!self::registered($variable))
		{
			return self::register($variable);
		}

		// Please do not try to change it to (expr) ? (true) : (false) - it will not work. ;)
		if (property_exists('phpbb', $variable))
		{
			return self::$$variable;
		}
		else
		{
			return self::$instances[$variable];
		}
	}

	/**
	* Check if the variable is already assigned
	*
	* @param string	$variable	The name of the instance to check
	*
	* @return bool	True if the instance is registered, false if not.
	* @access public
	*/
	public static function registered($variable)
	{
		if (property_exists('phpbb', $variable))
		{
			return (self::$$variable !== NULL) ? true : false;
		}

		return (isset(self::$instances[$variable]) && self::$instances[$variable] !== NULL) ? true : false;
	}

	/**
	* Simpler method to access assigned instances.
	* (Overloading is not possible here due to the object being static and our use of PHP 5.2.x+.)
	*
	* @param string	$variable	The instance name to retrieve
	*
	* @return mixed	The instance
	* @access public
	*/
	public static function get($variable)
	{
		// No error checking done here... returned right away
		return self::$instances[$variable];
	}

	/**
	* Register new class/object.
	* Any additional parameter will be forwarded to the class instantiation.
	*
	* @param string			$variable		The resulting instance name.
	* 										If a property with the given name exists, it will be assigned.
	* 										Else it will be put in the {@link $instances intances} array
	* @param string			$class			Define a custom class name.
	* 										This is useful if the class used does not abide to the rules (phpbb_{$class}).
	* @param string|array	$includes		Define additional files/includes required for this class to be correctly set up. Files are expected to be in /includes/.
	* @param mixed			$arguments,...	Any number of additional arguments passed to the constructor of the object to create
	*
	* @return mixed	The instance of the created object
	* @access public
	*/
	public static function register($variable, $class = false, $includes = false)
	{
		if (self::registered($variable))
		{
			return self::get_instance($variable);
		}

		$arguments = (func_num_args() > 3) ? array_slice(func_get_args(), 3) : array();
		$class = ($class === false) ? 'phpbb_' . $variable : $class;

		if ($includes !== false)
		{
			if (!is_array($includes))
			{
				$includes = array($includes);
			}

			foreach ($includes as $file)
			{
				require_once PHPBB_ROOT_PATH . 'includes/' . $file . '.' . PHP_EXT;
			}
		}

		$reflection = new ReflectionClass($class);

		if (!$reflection->isInstantiable())
		{
			throw new Exception('Assigned classes need to be instantiated.');
		}

		if (!property_exists('phpbb', $variable))
		{
			self::$instances[$variable] = (sizeof($arguments)) ? call_user_func_array(array($reflection, 'newInstance'), $arguments) : $reflection->newInstance();
		}
		else
		{
			self::$$variable = (sizeof($arguments)) ? call_user_func_array(array($reflection, 'newInstance'), $arguments) : $reflection->newInstance();
		}

		return self::get_instance($variable);
	}

	/**
	* Instead of registering we also can assign a variable. This is helpful if we have an application builder or use a factory.
	*
	* @param string	$variable	The resulting instance name.
	* 							If a property with the given name exists, it will be assigned.
	* 							Else it will be put in the {@link $instances intances} array
	* @param mixed	$object		The variable to assign to the instance
	*
	* @return mixed	The instance
	* @access public
	*/
	public static function assign($variable, $object)
	{
		if (self::registered($variable))
		{
			return self::get_instance($variable);
		}

		if (!property_exists('phpbb', $variable))
		{
			self::$instances[$variable] = $object;
		}
		else
		{
			self::$$variable = $object;
		}

		return self::get_instance($variable);
	}

	/**
	* Unset/unregister a specific object.
	*
	* @param string	$variable	The name of the instance to unset
	* @access public
	*/
	public static function unregister($variable)
	{
		if (!self::registered($variable))
		{
			return;
		}

		if (!property_exists('phpbb', $variable))
		{
			unset(self::$instances[$variable]);
		}
		else
		{
			self::$$variable = NULL;
		}
	}

	/**
	* Function to return to a clean state, unregistering everything. This is helpful for unit tests if you want to return to a "clean state"
	*
	* @access public
	*/
	public static function reset()
	{
		$class_vars = array_keys(get_class_vars('phpbb'));
		$class_vars = array_merge(array_keys(self::$instances), $class_vars);

		foreach ($class_vars as $variable)
		{
			self::unregister($variable);
		}
	}
}

/**
* phpBB SPL Autoload Function. A phpbb_ prefix will be stripped from the class name.
*
* The files this function tries to include are:
*	includes/{$class_name}/bootstrap.php
*	includes/{$class_name}/index.php
* Additionally, every _ within $class_name is replaced by / for the following directories:
*	includes/{$class_name}.php
*	includes/classes/{$class_name}.php
*
* @param string	$class_name	The class name. An existing phpbb_ prefix will be removed.
*/
function __phpbb_autoload($class_name)
{
	if (strpos($class_name, 'phpbb_') === 0)
	{
		$class_name = substr($class_name, 6);
	}

	$class_name = basename($class_name);

	$filenames = array(
		'includes/' . $class_name . '/bootstrap',
		'includes/' . $class_name . '/index',
		'includes/' . $class_name,
		'includes/classes/' . $class_name,
	);

	if (strpos($class_name, '_') !== false)
	{
		$class_name = str_replace('_', '/', $class_name);

		$filenames = array_merge($filenames, array(
			'includes/' . $class_name,
			'includes/classes/' . $class_name,
		));
	}

	foreach ($filenames as $filename)
	{
		if (file_exists(PHPBB_ROOT_PATH . $filename . '.' . PHP_EXT))
		{
			include PHPBB_ROOT_PATH . $filename . '.' . PHP_EXT;
			return;
		}
	}
}

/*
class phpbb_exception extends Exception
{
}
*/
?>