forked from esotalk/esoTalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
executable file
·323 lines (237 loc) · 13.3 KB
/
index.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
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
<?php
// OMG FIRST COMMENT!!!11!
// Copyright 2011 Toby Zerner, Simon Zerner
// This file is part of esoTalk. Please see the included license file for usage information.
/**
* Index page
*
* Main entry point. Does the following:
* 1. Sets up the environment.
* 2. Includes the configuration.
* 3. Requires and registers essential classes.
* 4. Sets up plugins.
* 5. Initializes the session, the database, and the cache.
* 6. Parses the page request.
* 7. Sets up skins.
* 8. Sets up the language.
* 9. Sets up the appropriate controller.
* 10. Dispatches to the controller, which will in turn render the page.
*
* @package esoTalk
*/
define("IN_ESOTALK", 1);
//***** 1. SET UP ENVIRONMENT
// Start a page load timer. We don't make use of it by default, but a plugin can if it needs to.
define("PAGE_START_TIME", microtime(true));
// By default, only display important errors (no warnings or notices.)
ini_set("display_errors", "On");
error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR);
// Make sure a default timezone is set... silly PHP 5.
if (ini_get("date.timezone") == "") date_default_timezone_set("GMT");
// Define directory constants.
if (!defined("PATH_ROOT")) define("PATH_ROOT", dirname(__FILE__));
if (!defined("PATH_CACHE")) define("PATH_CACHE", PATH_ROOT."/cache");
if (!defined("PATH_CONFIG")) define("PATH_CONFIG", PATH_ROOT."/config");
if (!defined("PATH_CONTROLLERS")) define("PATH_CONTROLLERS", PATH_ROOT."/controllers");
if (!defined("PATH_LANGUAGES")) define("PATH_LANGUAGES", PATH_ROOT."/languages");
if (!defined("PATH_LIBRARY")) define("PATH_LIBRARY", PATH_ROOT."/lib");
if (!defined("PATH_MODELS")) define("PATH_MODELS", PATH_ROOT."/models");
if (!defined("PATH_PLUGINS")) define("PATH_PLUGINS", PATH_ROOT."/plugins");
if (!defined("PATH_SKINS")) define("PATH_SKINS", PATH_ROOT."/skins");
if (!defined("PATH_UPLOADS")) define("PATH_UPLOADS", PATH_ROOT."/uploads");
if (!defined("PATH_VIEWS")) define("PATH_VIEWS", PATH_ROOT."/views");
// Include some essential files.
require PATH_LIBRARY."/functions.general.php";
require PATH_LIBRARY."/ET.class.php"; // phone home!
// Set up error and exception handling.
function errorHandler($code, $message, $file, $line)
{
// Make sure this error code is included in error_reporting.
if ((error_reporting() & $code) != $code) return false;
ET::fatalError(new ErrorException($message, $code, 1, $file, $line));
}
set_error_handler("errorHandler", E_USER_ERROR);
set_exception_handler(array("ET", "fatalError"));
// Determine the relative path to this forum. For example, if the forum is at http://forum.com/test/forum/,
// the web path should be /test/forum.
$parts = explode("/", $_SERVER["PHP_SELF"]);
$key = array_search("index.php", $parts);
if ($key !== false) ET::$webPath = implode("/", array_slice($parts, 0, $key));
// Undo register_globals.
undoRegisterGlobals();
// If magic quotes is on, strip the slashes that it added.
if (get_magic_quotes_gpc()) {
$_REQUEST = array_map("undoMagicQuotes", $_REQUEST);
$_GET = array_map("undoMagicQuotes", $_GET);
$_POST = array_map("undoMagicQuotes", $_POST);
$_COOKIE = array_map("undoMagicQuotes", $_COOKIE);
}
//***** 2. INCLUDE CONFIGURATION
// Include our config files.
ET::loadConfig(PATH_ROOT."/config.defaults.php");
// If the config path is different from the default, but there's still a config file at the default location, include it.
if (PATH_CONFIG != PATH_ROOT."/config" and file_exists($file = PATH_ROOT."/config/config.php")) ET::loadConfig($file);
// Include the real config file.
if (file_exists($file = PATH_CONFIG."/config.php")) ET::loadConfig($file);
// In debug mode, show all errors.
if (C("esoTalk.debug")) error_reporting(E_ALL);
//***** 3. REQUIRE AND REGISTER ESSENTIAL CLASSES
// Require base classes that may be extended.
require PATH_LIBRARY."/ETFactory.class.php";
require PATH_LIBRARY."/ETPluggable.class.php";
require PATH_LIBRARY."/ETController.class.php";
require PATH_LIBRARY."/ETAdminController.class.php";
require PATH_LIBRARY."/ETModel.class.php";
require PATH_LIBRARY."/ETPlugin.class.php";
require PATH_LIBRARY."/ETSkin.class.php";
// Register main classes.
ETFactory::register("database", "ETDatabase", PATH_LIBRARY."/ETDatabase.class.php");
ETFactory::register("databaseStructure", "ETDatabaseStructure", PATH_LIBRARY."/ETDatabaseStructure.class.php");
ETFactory::register("sqlQuery", "ETSQLQuery", PATH_LIBRARY."/ETSQLQuery.class.php");
ETFactory::register("sqlResult", "ETSQLResult", PATH_LIBRARY."/ETSQLResult.class.php");
ETFactory::register("session", "ETSession", PATH_LIBRARY."/ETSession.class.php");
ETFactory::register("cache", "ETCache", PATH_LIBRARY."/ETCache.class.php");
ETFactory::register("form", "ETForm", PATH_LIBRARY."/ETForm.class.php");
ETFactory::register("format", "ETFormat", PATH_LIBRARY."/ETFormat.class.php");
ETFactory::register("upload", "ETUpload", PATH_LIBRARY."/ETUpload.class.php");
ETFactory::register("menu", "ETMenu", PATH_LIBRARY."/ETMenu.class.php");
// Register models.
ETFactory::register("searchModel", "ETSearchModel", PATH_MODELS."/ETSearchModel.class.php");
ETFactory::register("conversationModel", "ETConversationModel", PATH_MODELS."/ETConversationModel.class.php");
ETFactory::register("memberModel", "ETMemberModel", PATH_MODELS."/ETMemberModel.class.php");
ETFactory::register("postModel", "ETPostModel", PATH_MODELS."/ETPostModel.class.php");
ETFactory::register("channelModel", "ETChannelModel", PATH_MODELS."/ETChannelModel.class.php");
ETFactory::register("groupModel", "ETGroupModel", PATH_MODELS."/ETGroupModel.class.php");
ETFactory::register("activityModel", "ETActivityModel", PATH_MODELS."/ETActivityModel.class.php");
ETFactory::register("upgradeModel", "ETUpgradeModel", PATH_MODELS."/ETUpgradeModel.class.php");
// If esoTalk hasn't been installed, register the install controller and set it as the default route.
if (!C("esoTalk.installed")) {
ETFactory::registerController("install", "ETInstallController", PATH_CONTROLLERS."/ETInstallController.class.php");
ET::$config["esoTalk.defaultRoute"] = "install";
}
elseif (C("esoTalk.version") != ESOTALK_VERSION) {
ETFactory::registerController("upgrade", "ETUpgradeController", PATH_CONTROLLERS."/ETUpgradeController.class.php");
}
// Otherwise, register all the default controllers and admin controllers.
else {
ETFactory::registerController("conversations", "ETConversationsController", PATH_CONTROLLERS."/ETConversationsController.class.php");
ETFactory::registerController("conversation", "ETConversationController", PATH_CONTROLLERS."/ETConversationController.class.php");
ETFactory::registerController("post", "ETPostController", PATH_CONTROLLERS."/ETPostController.class.php");
ETFactory::registerController("user", "ETUserController", PATH_CONTROLLERS."/ETUserController.class.php");
ETFactory::registerController("settings", "ETSettingsController", PATH_CONTROLLERS."/ETSettingsController.class.php");
ETFactory::registerController("channels", "ETChannelsController", PATH_CONTROLLERS."/ETChannelsController.class.php");
ETFactory::registerController("member", "ETMemberController", PATH_CONTROLLERS."/ETMemberController.class.php");
ETFactory::registerController("members", "ETMembersController", PATH_CONTROLLERS."/ETMembersController.class.php");
ETFactory::registerController("feed", "ETFeedController", PATH_CONTROLLERS."/ETFeedController.class.php");
ETFactory::registerController("admin", "ETAdminController", PATH_CONTROLLERS."/ETAdminController.class.php");
ETFactory::registerAdminController("dashboard", "ETDashboardAdminController", PATH_CONTROLLERS."/admin/ETDashboardAdminController.class.php");
ETFactory::registerAdminController("settings", "ETSettingsAdminController", PATH_CONTROLLERS."/admin/ETSettingsAdminController.class.php");
ETFactory::registerAdminController("appearance", "ETAppearanceAdminController", PATH_CONTROLLERS."/admin/ETAppearanceAdminController.class.php");
ETFactory::registerAdminController("channels", "ETChannelsAdminController", PATH_CONTROLLERS."/admin/ETChannelsAdminController.class.php");
ETFactory::registerAdminController("plugins", "ETPluginsAdminController", PATH_CONTROLLERS."/admin/ETPluginsAdminController.class.php");
ETFactory::registerAdminController("groups", "ETGroupsAdminController", PATH_CONTROLLERS."/admin/ETGroupsAdminController.class.php");
ETFactory::registerAdminController("languages", "ETLanguagesAdminController", PATH_CONTROLLERS."/admin/ETLanguagesAdminController.class.php");
}
//***** 5. SET UP PLUGINS
if (C("esoTalk.installed")) {
foreach (C("esoTalk.enabledPlugins") as $v) {
if (file_exists($file = PATH_PLUGINS."/".sanitizeFileName($v)."/plugin.php")) include_once $file;
$className = "ETPlugin_$v";
if (class_exists($className)) ET::$plugins[$v] = new $className("plugins/".$v);
}
}
//***** 6. INITIALIZE SESSION AND DATABASE, AND CACHE
// Initialize the cache.
$cacheClass = C("esoTalk.cache");
ET::$cache = ETFactory::make($cacheClass ? $cacheClass : "cache");
// Connect to the database.
ET::$database = ETFactory::make("database");
ET::$database->init(C("esoTalk.database.host"), C("esoTalk.database.user"), C("esoTalk.database.password"), C("esoTalk.database.dbName"), C("esoTalk.database.prefix"), C("esoTalk.database.connectionOptions"));
// Initialize the session.
ET::$session = ETFactory::make("session");
// Check if any plugins need upgrading by comparing the versions in ET::$pluginInfo with the versions in
// ET::$config.
foreach (ET::$plugins as $k => $v) {
if (C("$k.version") != ET::$pluginInfo[$k]["version"]) {
if ($v->setup(C("$k.version"))) ET::writeConfig(array("$k.version" => ET::$pluginInfo[$k]["version"]));
}
}
//***** 7. PARSE REQUEST
// If $_GET["p"] was explicitly specified, use that.
if (!empty($_GET["p"])) {
$request = $_GET["p"];
unset($_GET["p"]);
}
// If friendly URLs are turned on, process the REQUEST_URI into what the value of $_GET["p"] would normally be.
elseif (C("esoTalk.urls.friendly") and isset($_SERVER["REQUEST_URI"])) {
// Remove the base path from the request URI.
$request = preg_replace("|^".preg_quote(ET::$webPath)."|", "", $_SERVER["REQUEST_URI"]);
// If there is a querystring, remove it.
$selfURL = $request;
if (($pos = strpos($request, "?")) !== false) $request = substr_replace($request, "", $pos);
// Explode the request string. Make sure index.php is not the first item.
$parts = explode("/", trim(urldecode($request), "/"));
if ($parts[0] == "index.php") array_shift($parts);
$request = implode("/", $parts);
}
// If we have no request information, use the default route.
if (empty($request)) $request = C("esoTalk.defaultRoute");
// We need to work out what the URL to this exact page is and set it to the controller later.
// If we didn't work it out above, set it to $request and append any GET variables.
if (empty($selfURL)) {
$selfURL = $request;
$get = array();
foreach ($_GET as $k => $v) $get[] = "$k=".urlencode($v);
$get = implode("&", $get);
if ($get) $selfURL .= "?$get";
}
$requestParts = explode("/", $request);
//***** 8. SET UP SKIN
// If the user is an administrator and we're in the admin section, use the admin skin.
if (ET::$session->isAdmin() and $requestParts[0] == "admin") $skinName = C("esoTalk.adminSkin");
// If it's a mobile browser, use the mobile skin.
elseif (isMobileBrowser()) $skinName = C("esoTalk.mobileSkin");
// Otherwise, use the default skin.
else $skinName = C("esoTalk.skin");
// Include the skin file and instantiate its class.
ET::$skinName = $skinName;
if (file_exists($file = PATH_SKINS."/$skinName/skin.php")) include_once $file;
$skinClass = "ETSkin_".$skinName;
if (class_exists($skinClass)) ET::$skin = new $skinClass("skins/".$skinName);
// If we haven't got a working skin, just use the base class. It'll be ugly, but it'll do.
if (empty(ET::$skin)) ET::$skin = new ETSkin("");
// Add the class as a plugin as well so that its event handlers are called through the normal process.
array_unshift(ET::$plugins, ET::$skin);
//***** 9. SET UP LANGUAGE
ET::loadLanguage(ET::$session->preference("language"));
//***** 10. SET UP CONTROLLER
require PATH_LIBRARY."/functions.render.php";
// If the first part of the request is "admin", presume we're in the admin section.
if ($requestParts[0] == "admin") {
$controllers = ETFactory::$adminControllers;
array_shift($requestParts);
if (empty($requestParts[0])) $requestParts[0] = "dashboard";
}
// Otherwise, use normal public controllers.
else {
$controllers = ETFactory::$controllers;
// If the first character of the URL parameter is numeric, assume the conversation controller.
if ($requestParts[0] and is_numeric($requestParts[0][0])) array_unshift($requestParts, "conversation");
}
// Parse the request store all of the request information.
list(ET::$controllerName, ET::$controller, $method, $arguments, $responseType) = parseRequest($requestParts, $controllers);
ET::$controller->selfURL = $selfURL;
ET::$controller->controllerMethod = $method;
ET::$controller->responseType = $responseType;
//***** 11. SHOW THE PAGE
// Initialize plugins.
foreach (ET::$plugins as $plugin) $plugin->init();
// Initialize the controller.
ET::$controller->init();
if (!C("esoTalk.gzipOutput") or C("esoTalk.debug") or !ob_start("ob_gzhandler")) ob_start();
// Dispatch request information to the controller. The controller will then call the appropriate function which
// will in turn render the page.
ET::$controller->dispatch(ET::$controller->controllerMethod, $arguments);
ob_end_flush();
//***** 12. CLEANUP
ET::$database->close();