/
vb.php
295 lines (242 loc) · 8.2 KB
/
vb.php
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
<?php if (!defined('VB_ENTRY')) die('Access denied.');
/*======================================================================*\
|| #################################################################### ||
|| # vBulletin 4.2.2 Alpha 1 - Licence Number VBFSA2W3VC
|| # ---------------------------------------------------------------- # ||
|| # Copyright 2000-2013 vBulletin Solutions Inc. All Rights Reserved. ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------- VBULLETIN IS NOT FREE SOFTWARE ---------------- # ||
|| # http://www.vbulletin.com | http://www.vbulletin.com/license.html # ||
|| #################################################################### ||
\*======================================================================*/
/**
* The vB core class.
* Everything required at the core level should be accessible through this.
*
* The core class performs initialisation for error handling, exception handling,
* application instatiation and optionally debug handling.
*
* @TODO: Much of what goes on in global.php and init.php will be handled, or at
* least called here during the initialisation process. This will be moved over as
* global.php is refactored.
*
* @package vBulletin
* @version $Revision: 28823 $
* @since $Date: 2008-12-16 17:43:04 +0000 (Tue, 16 Dec 2008) $
* @copyright vBulletin Solutions Inc.
*/
abstract class vB
{
/*Properties====================================================================*/
/**
* Whether the framework has been initialized.
*
* @var bool
*/
private static $initialized;
/**
* The legacy registry object
*
* @var vB_Registry
*/
public static $vbulletin;
/**
* The database class.
* This is the main database used by vBulletin.
*
* @var vB_Database
*/
public static $db;
/**
* Array of classname => filename for autoloading.
* This is purely for performance reasons and is optional. Each entry should
* specify the class name and the absolute path to the file to include.
*
* It is also useful for any classes residing in files that do not meet the
* class name => file location pattern that is resolved by the autoloader.
*
* @var array classname => filename
*/
private static $load_map = array();
/**
* Whether content headers have been sent.
* This only tracks the Content-Type and Content-Length headers. When a view or
* other system sends content headers, it should let vB know with
* vB::contentHeadersSent(true).
*
* @var bool
*/
private static $content_headers_sent = false;
/*Initialisation================================================================*/
/**
* Initializes the vB framework.
* All framework level objects and services are created so that they are available
* throughout the application. This is only done once per actual request.
*
* Note: If the framework is used this way then there are several limitations.
* - If no vB_Bootstrap was created (ie, the admincp), then you cannot render any
* views created by the framework.
* - VB_ENTRY must be defined to a valid request script that runs the framework
* with vB::Main()
* - If you are rendering views, try to create all of the views that will be
* used before rendering any of them. This optimises template and phrase
* fetching.
*/
public static function init($relative_path = false)
{
global $vbulletin, $vbphrase;
if (self::$initialized)
{
return;
}
// Reference legacy registry
self::$vbulletin = $vbulletin;
// Legacy DB
self::$db = $vbulletin->db;
// Set a top level exception handler
set_exception_handler(array('vB', 'handleException'));
// Set unserializer to use spl registered autoloader
ini_set('unserialize_callback_func', 'spl_autoload_call');
// Set class autoloader
spl_autoload_register(array('vB', 'autoload'));
// Legacy language
vB_Phrase::setLanguage((!empty(self::$vbulletin->session->vars['languageid']) ? self::$vbulletin->session->vars['languageid'] : intval(self::$vbulletin->options['languageid'])));
// Ensure we have friendly url class
require_once (DIR . '/includes/class_friendly_url.php');
if ($relative_path)
{
vB_Router::setRelativePath($relative_path);
}
// Done
self::$initialized = true;
}
/*Response======================================================================*/
/**
* Gets or sets whether content headers have been sent.
*
* @param bool $sent - If true, the headers have been sent
*/
public static function contentHeadersSent($sent = false)
{
if ($sent)
{
self::$content_headers_sent = true;
}
return self::$content_headers_sent;
}
/*Autoload======================================================================*/
/**
* Autloads a class file when required.
* Classnames are broken down into segments which are used to resolve the class
* directory and filename.
*
* If the first segment matches 'vB' then it is in /vB else it is in
* /packages/segment/
*
* An optional load map of classname => filename can be passed to skip the path
* resolution.
*
* @TODO: Investigate performance of file_exists and where $check_file could be
* used, defaulting to false.
*
* @param string $classname - The name of the class to load
* @param array $load_map - Assoc array of classname => filename for quick load
* @param bool $check_file - Whether to check if the file exists and return false
* @return bool - Success
*/
public static function autoload($classname, $load_map = false, $check_file = true)
{
if (!$classname)
{
return false;
}
$filename = false;
$fclassname = strtolower($classname);
if (preg_match('#\W#', $fclassname))
{
return false;
}
if (isset($load_map[$classname]))
{
$filename = $load_map[$classname];
}
else if (isset(self::$load_map[$classname]))
{
$filename = self::$load_map[$classname];
}
else
{
$segments = explode('_', $fclassname);
$filename = ('vb' == $segments[0] ? VB_PATH : VB_PKG_PATH);
if (sizeof($segments) > ('vb' == $segments[0] ? 2 : 1))
{
$filename .= implode('/', array_slice($segments, ('vb' == $segments[0] ? 1 : 0), -1)) . '/';
}
$filename .= array_pop($segments) . '.php';
}
// Include the required class file
if ($filename)
{
if ($check_file AND !file_exists($filename))
{
return false;
}
require($filename);
if (class_exists($classname, false) OR interface_exists($classname, false))
{
return true;
}
}
return false;
}
/**
* Allows autoloaders to be registered before the vb autoloader.
*
* @param callback $callback
*/
public static function autoloadPreregister($add_callback)
{
$registered = spl_autoload_functions();
foreach ($registered AS $callback)
{
spl_autoload_unregister($callback);
}
array_unshift($registered, $add_callback);
foreach ($registered AS $callback)
{
spl_autoload_register($callback);
}
}
/**
* Allows entries to be added to the load map.
* This is especially useful for calling during application initialization.
*
* @param array class => file $load_map
*/
public static function autoloadMap(array $load_map)
{
self::$load_map = array_merge($load_map, self::$load_map);
}
/*ErrorHandling=================================================================*/
/**
* Catches any uncaught exceptions.
*
* @param Exception $exception
*/
public static function handleException($exception)
{
if (!$code = $exception->getCode())
{
$code = E_USER_ERROR;
}
echo($exception->getMessage() . ' on line ' . $exception->getLine() . ' in ' . $exception->getFile() . "<br />\n" . nl2br($exception->getTraceAsString()) . '<br /><br />');
// TODO: Our own error output, with the exception's message and stack trace
trigger_error($exception->getMessage() . ' on line ' . $exception->getLine() . ' in ' . $exception->getFile() . "\n", $code);
}
}
/*======================================================================*\
|| ####################################################################
|| # Downloaded: 03:13, Sat Sep 7th 2013
|| # SVN: $Revision: 28823 $
|| ####################################################################
\*======================================================================*/