Commit | Line | Data |
---|---|---|
d335fa16 AE |
1 | <?php |
2 | namespace wcf\system; | |
3 | use wcf\data\application\Application; | |
4 | use wcf\data\option\OptionEditor; | |
5 | use wcf\data\package\Package; | |
6 | use wcf\data\package\PackageCache; | |
7 | use wcf\data\package\PackageEditor; | |
12a2ff01 AE |
8 | use wcf\data\page\Page; |
9 | use wcf\data\page\PageCache; | |
5346b183 | 10 | use wcf\page\CmsPage; |
d335fa16 | 11 | use wcf\system\application\ApplicationHandler; |
f341086b | 12 | use wcf\system\application\IApplication; |
55b402a0 | 13 | use wcf\system\box\BoxHandler; |
d335fa16 AE |
14 | use wcf\system\cache\builder\CoreObjectCacheBuilder; |
15 | use wcf\system\cache\builder\PackageUpdateCacheBuilder; | |
16 | use wcf\system\cronjob\CronjobScheduler; | |
157054c9 | 17 | use wcf\system\database\MySQLDatabase; |
d335fa16 AE |
18 | use wcf\system\event\EventHandler; |
19 | use wcf\system\exception\AJAXException; | |
b25ad8f3 | 20 | use wcf\system\exception\ErrorException; |
d335fa16 AE |
21 | use wcf\system\exception\IPrintableException; |
22 | use wcf\system\exception\NamedUserException; | |
7b9ff46b | 23 | use wcf\system\exception\ParentClassException; |
d335fa16 AE |
24 | use wcf\system\exception\PermissionDeniedException; |
25 | use wcf\system\exception\SystemException; | |
26 | use wcf\system\language\LanguageFactory; | |
27 | use wcf\system\package\PackageInstallationDispatcher; | |
12a2ff01 | 28 | use wcf\system\request\Request; |
a80873d5 | 29 | use wcf\system\request\RequestHandler; |
d335fa16 AE |
30 | use wcf\system\session\SessionFactory; |
31 | use wcf\system\session\SessionHandler; | |
32 | use wcf\system\style\StyleHandler; | |
15621401 | 33 | use wcf\system\template\EmailTemplateEngine; |
d335fa16 AE |
34 | use wcf\system\template\TemplateEngine; |
35 | use wcf\system\user\storage\UserStorageHandler; | |
d335fa16 | 36 | use wcf\util\FileUtil; |
8418bed7 | 37 | use wcf\util\HeaderUtil; |
d335fa16 | 38 | use wcf\util\StringUtil; |
dc0015ab | 39 | use wcf\util\UserUtil; |
d335fa16 AE |
40 | |
41 | // try to set a time-limit to infinite | |
42 | @set_time_limit(0); | |
43 | ||
44 | // fix timezone warning issue | |
45 | if (!@ini_get('date.timezone')) { | |
46 | @date_default_timezone_set('Europe/London'); | |
47 | } | |
48 | ||
359841c3 | 49 | // define current woltlab suite version |
51eb4d08 | 50 | define('WCF_VERSION', '3.0.7'); |
d335fa16 AE |
51 | |
52 | // define current unix timestamp | |
53 | define('TIME_NOW', time()); | |
54 | ||
55 | // wcf imports | |
56 | if (!defined('NO_IMPORTS')) { | |
57 | require_once(WCF_DIR.'lib/core.functions.php'); | |
d11a8c9e | 58 | require_once(WCF_DIR.'lib/system/api/autoload.php'); |
d335fa16 AE |
59 | } |
60 | ||
61 | /** | |
e71525e4 | 62 | * WCF is the central class for the WoltLab Suite Core. |
d335fa16 AE |
63 | * It holds the database connection, access to template and language engine. |
64 | * | |
65 | * @author Marcel Werk | |
46d29bca | 66 | * @copyright 2001-2017 WoltLab GmbH |
d335fa16 | 67 | * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
e71525e4 | 68 | * @package WoltLabSuite\Core\System |
d335fa16 AE |
69 | */ |
70 | class WCF { | |
71 | /** | |
72 | * list of currently loaded applications | |
893aace3 | 73 | * @var Application[] |
d335fa16 | 74 | */ |
058cbd6a | 75 | protected static $applications = []; |
d335fa16 AE |
76 | |
77 | /** | |
78 | * list of currently loaded application objects | |
7a23a706 | 79 | * @var IApplication[] |
d335fa16 | 80 | */ |
058cbd6a | 81 | protected static $applicationObjects = []; |
d335fa16 AE |
82 | |
83 | /** | |
84 | * list of autoload directories | |
85 | * @var array | |
86 | */ | |
058cbd6a | 87 | protected static $autoloadDirectories = []; |
d335fa16 AE |
88 | |
89 | /** | |
90 | * list of unique instances of each core object | |
7a23a706 | 91 | * @var SingletonFactory[] |
d335fa16 | 92 | */ |
058cbd6a | 93 | protected static $coreObject = []; |
d335fa16 AE |
94 | |
95 | /** | |
96 | * list of cached core objects | |
7a23a706 | 97 | * @var string[] |
d335fa16 | 98 | */ |
058cbd6a | 99 | protected static $coreObjectCache = []; |
d335fa16 AE |
100 | |
101 | /** | |
102 | * database object | |
4e25add7 | 103 | * @var MySQLDatabase |
d335fa16 AE |
104 | */ |
105 | protected static $dbObj = null; | |
106 | ||
107 | /** | |
108 | * language object | |
a59497a6 | 109 | * @var \wcf\data\language\Language |
d335fa16 AE |
110 | */ |
111 | protected static $languageObj = null; | |
112 | ||
113 | /** | |
114 | * overrides disabled debug mode | |
115 | * @var boolean | |
116 | */ | |
117 | protected static $overrideDebugMode = false; | |
118 | ||
119 | /** | |
120 | * session object | |
4e25add7 | 121 | * @var SessionHandler |
d335fa16 AE |
122 | */ |
123 | protected static $sessionObj = null; | |
124 | ||
125 | /** | |
126 | * template object | |
4e25add7 | 127 | * @var TemplateEngine |
d335fa16 AE |
128 | */ |
129 | protected static $tplObj = null; | |
130 | ||
131 | /** | |
132 | * true if Zend Opcache is loaded and enabled | |
133 | * @var boolean | |
134 | */ | |
135 | protected static $zendOpcacheEnabled = null; | |
136 | ||
ab84d9ca AE |
137 | /** |
138 | * force logout during destructor call | |
139 | * @var boolean | |
140 | */ | |
141 | protected static $forceLogout = false; | |
142 | ||
d335fa16 AE |
143 | /** |
144 | * Calls all init functions of the WCF class. | |
145 | */ | |
146 | public function __construct() { | |
147 | // add autoload directory | |
148 | self::$autoloadDirectories['wcf'] = WCF_DIR . 'lib/'; | |
149 | ||
150 | // define tmp directory | |
151 | if (!defined('TMP_DIR')) define('TMP_DIR', FileUtil::getTempFolder()); | |
152 | ||
153 | // start initialization | |
d335fa16 AE |
154 | $this->initDB(); |
155 | $this->loadOptions(); | |
156 | $this->initSession(); | |
157 | $this->initLanguage(); | |
158 | $this->initTPL(); | |
159 | $this->initCronjobs(); | |
160 | $this->initCoreObjects(); | |
161 | $this->initApplications(); | |
162 | $this->initBlacklist(); | |
163 | ||
164 | EventHandler::getInstance()->fireAction($this, 'initialized'); | |
165 | } | |
166 | ||
167 | /** | |
3ba197fa TD |
168 | * Flushes the output, closes the session, performs background tasks and more. |
169 | * | |
170 | * You *must* not create output in here under normal circumstances, as it might get eaten | |
171 | * when gzip is enabled. | |
d335fa16 AE |
172 | */ |
173 | public static function destruct() { | |
174 | try { | |
175 | // database has to be initialized | |
176 | if (!is_object(self::$dbObj)) return; | |
177 | ||
3ba197fa TD |
178 | $debug = self::debugModeIsEnabled(true); |
179 | if (!$debug) { | |
180 | // flush output | |
181 | if (ob_get_level()) ob_end_flush(); | |
d335fa16 | 182 | flush(); |
3ba197fa TD |
183 | |
184 | // close connection if using FPM | |
185 | if (function_exists('fastcgi_finish_request')) fastcgi_finish_request(); | |
d335fa16 AE |
186 | } |
187 | ||
188 | // update session | |
189 | if (is_object(self::getSession())) { | |
ab84d9ca AE |
190 | if (self::$forceLogout) { |
191 | // do logout | |
1eb97b3a | 192 | self::getSession()->delete(); |
ab84d9ca AE |
193 | } |
194 | else { | |
195 | self::getSession()->update(); | |
196 | } | |
d335fa16 AE |
197 | } |
198 | ||
199 | // execute shutdown actions of user storage handler | |
200 | UserStorageHandler::getInstance()->shutdown(); | |
201 | } | |
202 | catch (\Exception $exception) { | |
203 | die("<pre>WCF::destruct() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString()); | |
204 | } | |
205 | } | |
206 | ||
d335fa16 AE |
207 | /** |
208 | * Returns the database object. | |
209 | * | |
210 | * @return \wcf\system\database\Database | |
211 | */ | |
212 | public static final function getDB() { | |
213 | return self::$dbObj; | |
214 | } | |
215 | ||
216 | /** | |
217 | * Returns the session object. | |
218 | * | |
4e25add7 | 219 | * @return SessionHandler |
d335fa16 AE |
220 | */ |
221 | public static final function getSession() { | |
222 | return self::$sessionObj; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Returns the user object. | |
227 | * | |
228 | * @return \wcf\data\user\User | |
229 | */ | |
230 | public static final function getUser() { | |
231 | return self::getSession()->getUser(); | |
232 | } | |
233 | ||
234 | /** | |
235 | * Returns the language object. | |
236 | * | |
237 | * @return \wcf\data\language\Language | |
238 | */ | |
239 | public static final function getLanguage() { | |
240 | return self::$languageObj; | |
241 | } | |
242 | ||
243 | /** | |
244 | * Returns the template object. | |
245 | * | |
4e25add7 | 246 | * @return TemplateEngine |
d335fa16 AE |
247 | */ |
248 | public static final function getTPL() { | |
249 | return self::$tplObj; | |
250 | } | |
251 | ||
252 | /** | |
253 | * Calls the show method on the given exception. | |
254 | * | |
255 | * @param \Exception $e | |
256 | */ | |
e09c7044 | 257 | public static final function handleException($e) { |
f8434d25 AE |
258 | if (ob_get_level()) { |
259 | // discard any output generated before the exception occured, prevents exception | |
260 | // being hidden inside HTML elements and therefore not visible in browser output | |
5f2c3ab4 AE |
261 | ob_end_clean(); |
262 | ||
263 | // `identity` is the default "encoding" and basically means that the client | |
264 | // must treat the content as if the header did not appear in first place, this | |
265 | // also overrules the gzip header if present | |
266 | @header('Content-Encoding: identity'); | |
8418bed7 | 267 | HeaderUtil::exceptionDisableGzip(); |
f8434d25 AE |
268 | } |
269 | ||
5bc5a7e6 TD |
270 | // backwards compatibility |
271 | if ($e instanceof IPrintableException) { | |
272 | $e->show(); | |
273 | exit; | |
274 | } | |
275 | ||
276 | @header('HTTP/1.1 503 Service Unavailable'); | |
d335fa16 | 277 | try { |
5bc5a7e6 | 278 | \wcf\functions\exception\printThrowable($e); |
d335fa16 | 279 | } |
5bc5a7e6 TD |
280 | catch (\Throwable $e2) { |
281 | echo "<pre>An Exception was thrown while handling an Exception:\n\n"; | |
6b7d6653 | 282 | echo preg_replace('/Database->__construct\(.*\)/', 'Database->__construct(...)', $e2); |
5bc5a7e6 | 283 | echo "\n\nwas thrown while:\n\n"; |
6b7d6653 | 284 | echo preg_replace('/Database->__construct\(.*\)/', 'Database->__construct(...)', $e); |
5bc5a7e6 TD |
285 | echo "\n\nwas handled.</pre>"; |
286 | exit; | |
e09c7044 | 287 | } |
5bc5a7e6 TD |
288 | catch (\Exception $e2) { |
289 | echo "<pre>An Exception was thrown while handling an Exception:\n\n"; | |
6b7d6653 | 290 | echo preg_replace('/Database->__construct\(.*\)/', 'Database->__construct(...)', $e2); |
5bc5a7e6 | 291 | echo "\n\nwas thrown while:\n\n"; |
6b7d6653 | 292 | echo preg_replace('/Database->__construct\(.*\)/', 'Database->__construct(...)', $e); |
5bc5a7e6 TD |
293 | echo "\n\nwas handled.</pre>"; |
294 | exit; | |
d335fa16 AE |
295 | } |
296 | } | |
297 | ||
298 | /** | |
b25ad8f3 | 299 | * Turns PHP errors into an ErrorException. |
d335fa16 | 300 | * |
ac52543a | 301 | * @param integer $severity |
d335fa16 | 302 | * @param string $message |
ac52543a MS |
303 | * @param string $file |
304 | * @param integer $line | |
2b770bdd | 305 | * @throws ErrorException |
d335fa16 | 306 | */ |
b25ad8f3 | 307 | public static final function handleError($severity, $message, $file, $line) { |
b2aa772d | 308 | // this is necessary for the shut-up operator |
b25ad8f3 TD |
309 | if (error_reporting() == 0) return; |
310 | ||
311 | throw new ErrorException($message, 0, $severity, $file, $line); | |
d335fa16 AE |
312 | } |
313 | ||
314 | /** | |
315 | * Loads the database configuration and creates a new connection to the database. | |
316 | */ | |
317 | protected function initDB() { | |
318 | // get configuration | |
319 | $dbHost = $dbUser = $dbPassword = $dbName = ''; | |
320 | $dbPort = 0; | |
d335fa16 AE |
321 | require(WCF_DIR.'config.inc.php'); |
322 | ||
323 | // create database connection | |
a2d822ad | 324 | self::$dbObj = new MySQLDatabase($dbHost, $dbUser, $dbPassword, $dbName, $dbPort); |
d335fa16 AE |
325 | } |
326 | ||
327 | /** | |
328 | * Loads the options file, automatically created if not exists. | |
329 | */ | |
330 | protected function loadOptions() { | |
331 | $filename = WCF_DIR.'options.inc.php'; | |
332 | ||
333 | // create options file if doesn't exist | |
334 | if (!file_exists($filename) || filemtime($filename) <= 1) { | |
335 | OptionEditor::rebuild(); | |
336 | } | |
337 | require_once($filename); | |
b842faa1 AE |
338 | |
339 | // check if option file is complete and writable | |
340 | if (PACKAGE_ID) { | |
341 | if (!is_writable($filename)) { | |
342 | FileUtil::makeWritable($filename); | |
343 | ||
344 | if (!is_writable($filename)) { | |
345 | throw new SystemException("The option file '" . $filename . "' is not writable."); | |
346 | } | |
347 | } | |
348 | ||
349 | // check if a previous write operation was incomplete and force rebuilding | |
350 | if (!defined('WCF_OPTION_INC_PHP_SUCCESS')) { | |
351 | OptionEditor::rebuild(); | |
352 | ||
353 | require_once($filename); | |
354 | } | |
355 | } | |
d335fa16 AE |
356 | } |
357 | ||
358 | /** | |
359 | * Starts the session system. | |
360 | */ | |
361 | protected function initSession() { | |
f341086b | 362 | $factory = new SessionFactory(); |
d335fa16 AE |
363 | $factory->load(); |
364 | ||
f341086b | 365 | self::$sessionObj = SessionHandler::getInstance(); |
91082aee | 366 | self::$sessionObj->setHasValidCookie($factory->hasValidCookie()); |
d335fa16 AE |
367 | } |
368 | ||
369 | /** | |
370 | * Initialises the language engine. | |
371 | */ | |
372 | protected function initLanguage() { | |
373 | if (isset($_GET['l']) && !self::getUser()->userID) { | |
374 | self::getSession()->setLanguageID(intval($_GET['l'])); | |
375 | } | |
376 | ||
377 | // set mb settings | |
378 | mb_internal_encoding('UTF-8'); | |
379 | if (function_exists('mb_regex_encoding')) mb_regex_encoding('UTF-8'); | |
380 | mb_language('uni'); | |
381 | ||
382 | // get language | |
f341086b | 383 | self::$languageObj = LanguageFactory::getInstance()->getUserLanguage(self::getSession()->getLanguageID()); |
d335fa16 AE |
384 | } |
385 | ||
386 | /** | |
387 | * Initialises the template engine. | |
388 | */ | |
389 | protected function initTPL() { | |
f341086b | 390 | self::$tplObj = TemplateEngine::getInstance(); |
d335fa16 AE |
391 | self::getTPL()->setLanguageID(self::getLanguage()->languageID); |
392 | $this->assignDefaultTemplateVariables(); | |
393 | ||
394 | $this->initStyle(); | |
395 | } | |
396 | ||
397 | /** | |
398 | * Initializes the user's style. | |
399 | */ | |
400 | protected function initStyle() { | |
401 | if (isset($_REQUEST['styleID'])) { | |
402 | self::getSession()->setStyleID(intval($_REQUEST['styleID'])); | |
403 | } | |
404 | ||
f341086b | 405 | $styleHandler = StyleHandler::getInstance(); |
d11a8c9e | 406 | $styleHandler->changeStyle(self::getSession()->getStyleID()); |
d335fa16 AE |
407 | } |
408 | ||
409 | /** | |
410 | * Executes the blacklist. | |
411 | */ | |
412 | protected function initBlacklist() { | |
fa75ccfb AE |
413 | $isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); |
414 | ||
d335fa16 | 415 | if (defined('BLACKLIST_IP_ADDRESSES') && BLACKLIST_IP_ADDRESSES != '') { |
dc0015ab | 416 | if (!StringUtil::executeWordFilter(UserUtil::convertIPv6To4(self::getSession()->ipAddress), BLACKLIST_IP_ADDRESSES)) { |
fa75ccfb | 417 | if ($isAjax) { |
a1721f86 | 418 | throw new AJAXException(self::getLanguage()->getDynamicVariable('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS); |
fa75ccfb AE |
419 | } |
420 | else { | |
421 | throw new PermissionDeniedException(); | |
422 | } | |
dc0015ab AE |
423 | } |
424 | else if (!StringUtil::executeWordFilter(self::getSession()->ipAddress, BLACKLIST_IP_ADDRESSES)) { | |
fa75ccfb | 425 | if ($isAjax) { |
a1721f86 | 426 | throw new AJAXException(self::getLanguage()->getDynamicVariable('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS); |
fa75ccfb AE |
427 | } |
428 | else { | |
429 | throw new PermissionDeniedException(); | |
430 | } | |
d335fa16 AE |
431 | } |
432 | } | |
433 | if (defined('BLACKLIST_USER_AGENTS') && BLACKLIST_USER_AGENTS != '') { | |
434 | if (!StringUtil::executeWordFilter(self::getSession()->userAgent, BLACKLIST_USER_AGENTS)) { | |
fa75ccfb | 435 | if ($isAjax) { |
a1721f86 | 436 | throw new AJAXException(self::getLanguage()->getDynamicVariable('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS); |
fa75ccfb AE |
437 | } |
438 | else { | |
439 | throw new PermissionDeniedException(); | |
440 | } | |
d335fa16 AE |
441 | } |
442 | } | |
443 | if (defined('BLACKLIST_HOSTNAMES') && BLACKLIST_HOSTNAMES != '') { | |
444 | if (!StringUtil::executeWordFilter(@gethostbyaddr(self::getSession()->ipAddress), BLACKLIST_HOSTNAMES)) { | |
fa75ccfb | 445 | if ($isAjax) { |
a1721f86 | 446 | throw new AJAXException(self::getLanguage()->getDynamicVariable('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS); |
fa75ccfb AE |
447 | } |
448 | else { | |
449 | throw new PermissionDeniedException(); | |
450 | } | |
d335fa16 AE |
451 | } |
452 | } | |
453 | ||
454 | // handle banned users | |
455 | if (self::getUser()->userID && self::getUser()->banned) { | |
fa75ccfb | 456 | if ($isAjax) { |
d335fa16 AE |
457 | throw new AJAXException(self::getLanguage()->getDynamicVariable('wcf.user.error.isBanned'), AJAXException::INSUFFICIENT_PERMISSIONS); |
458 | } | |
459 | else { | |
ab84d9ca AE |
460 | self::$forceLogout = true; |
461 | ||
462 | // remove cookies | |
463 | if (isset($_COOKIE[COOKIE_PREFIX.'userID'])) { | |
464 | HeaderUtil::setCookie('userID', 0); | |
465 | } | |
466 | if (isset($_COOKIE[COOKIE_PREFIX.'password'])) { | |
467 | HeaderUtil::setCookie('password', ''); | |
468 | } | |
469 | ||
d335fa16 AE |
470 | throw new NamedUserException(self::getLanguage()->getDynamicVariable('wcf.user.error.isBanned')); |
471 | } | |
472 | } | |
473 | } | |
474 | ||
475 | /** | |
476 | * Initializes applications. | |
477 | */ | |
478 | protected function initApplications() { | |
479 | // step 1) load all applications | |
058cbd6a | 480 | $loadedApplications = []; |
d335fa16 AE |
481 | |
482 | // register WCF as application | |
39abe192 | 483 | self::$applications['wcf'] = ApplicationHandler::getInstance()->getApplicationByID(1); |
d335fa16 | 484 | |
39abe192 | 485 | if (!class_exists(WCFACP::class, false)) { |
b2b2c26b | 486 | static::getTPL()->assign('baseHref', self::$applications['wcf']->getPageURL()); |
39abe192 AE |
487 | } |
488 | ||
d335fa16 AE |
489 | // start main application |
490 | $application = ApplicationHandler::getInstance()->getActiveApplication(); | |
39abe192 AE |
491 | if ($application->packageID != 1) { |
492 | $loadedApplications[] = $this->loadApplication($application); | |
493 | ||
494 | // register primary application | |
495 | $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID); | |
496 | self::$applications[$abbreviation] = $application; | |
497 | } | |
d335fa16 AE |
498 | |
499 | // start dependent applications | |
500 | $applications = ApplicationHandler::getInstance()->getDependentApplications(); | |
501 | foreach ($applications as $application) { | |
39abe192 AE |
502 | if ($application->packageID == 1) { |
503 | // ignore WCF | |
504 | continue; | |
505 | } | |
f4eafe37 AE |
506 | else if ($application->isTainted) { |
507 | // ignore apps flagged for uninstallation | |
508 | continue; | |
509 | } | |
39abe192 | 510 | |
d335fa16 AE |
511 | $loadedApplications[] = $this->loadApplication($application, true); |
512 | } | |
513 | ||
514 | // step 2) run each application | |
515 | if (!class_exists('wcf\system\WCFACP', false)) { | |
f341086b | 516 | /** @var IApplication $application */ |
d335fa16 AE |
517 | foreach ($loadedApplications as $application) { |
518 | $application->__run(); | |
519 | } | |
520 | ||
521 | // refresh the session 1 minute before it expires | |
63b9817b | 522 | self::getTPL()->assign('__sessionKeepAlive', SESSION_TIMEOUT - 60); |
d335fa16 AE |
523 | } |
524 | } | |
525 | ||
526 | /** | |
527 | * Loads an application. | |
528 | * | |
2b770bdd MS |
529 | * @param Application $application |
530 | * @param boolean $isDependentApplication | |
531 | * @return IApplication | |
532 | * @throws SystemException | |
d335fa16 AE |
533 | */ |
534 | protected function loadApplication(Application $application, $isDependentApplication = false) { | |
d335fa16 AE |
535 | $package = PackageCache::getInstance()->getPackage($application->packageID); |
536 | // package cache might be outdated | |
537 | if ($package === null) { | |
538 | $package = new Package($application->packageID); | |
539 | ||
540 | // package cache is outdated, discard cache | |
541 | if ($package->packageID) { | |
542 | PackageEditor::resetCache(); | |
543 | } | |
544 | else { | |
545 | // package id is invalid | |
546 | throw new SystemException("application identified by package id '".$application->packageID."' is unknown"); | |
547 | } | |
548 | } | |
e54191ea | 549 | |
d335fa16 AE |
550 | $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID); |
551 | $packageDir = FileUtil::getRealPath(WCF_DIR.$package->packageDir); | |
552 | self::$autoloadDirectories[$abbreviation] = $packageDir . 'lib/'; | |
553 | ||
554 | $className = $abbreviation.'\system\\'.strtoupper($abbreviation).'Core'; | |
157054c9 | 555 | if (class_exists($className) && is_subclass_of($className, IApplication::class)) { |
d335fa16 AE |
556 | // include config file |
557 | $configPath = $packageDir . PackageInstallationDispatcher::CONFIG_FILE; | |
b842faa1 AE |
558 | if (!file_exists($configPath)) { |
559 | Package::writeConfigFile($package->packageID); | |
560 | } | |
561 | ||
d335fa16 AE |
562 | if (file_exists($configPath)) { |
563 | require_once($configPath); | |
564 | } | |
565 | else { | |
566 | throw new SystemException('Unable to load configuration for '.$package->package); | |
567 | } | |
568 | ||
569 | // register template path if not within ACP | |
570 | if (!class_exists('wcf\system\WCFACP', false)) { | |
571 | // add template path and abbreviation | |
b2b2c26b | 572 | static::getTPL()->addApplication($abbreviation, $packageDir . 'templates/'); |
d335fa16 | 573 | } |
15621401 | 574 | EmailTemplateEngine::getInstance()->addApplication($abbreviation, $packageDir . 'templates/'); |
d335fa16 AE |
575 | |
576 | // init application and assign it as template variable | |
058cbd6a | 577 | self::$applicationObjects[$application->packageID] = call_user_func([$className, 'getInstance']); |
b2b2c26b | 578 | static::getTPL()->assign('__'.$abbreviation, self::$applicationObjects[$application->packageID]); |
15621401 | 579 | EmailTemplateEngine::getInstance()->assign('__'.$abbreviation, self::$applicationObjects[$application->packageID]); |
d335fa16 AE |
580 | } |
581 | else { | |
582 | unset(self::$autoloadDirectories[$abbreviation]); | |
157054c9 | 583 | throw new SystemException("Unable to run '".$package->package."', '".$className."' is missing or does not implement '".IApplication::class."'."); |
d335fa16 AE |
584 | } |
585 | ||
586 | // register template path in ACP | |
587 | if (class_exists('wcf\system\WCFACP', false)) { | |
b2b2c26b | 588 | static::getTPL()->addApplication($abbreviation, $packageDir . 'acp/templates/'); |
d335fa16 AE |
589 | } |
590 | else if (!$isDependentApplication) { | |
591 | // assign base tag | |
b2b2c26b | 592 | static::getTPL()->assign('baseHref', $application->getPageURL()); |
d335fa16 AE |
593 | } |
594 | ||
595 | // register application | |
596 | self::$applications[$abbreviation] = $application; | |
597 | ||
598 | return self::$applicationObjects[$application->packageID]; | |
599 | } | |
600 | ||
601 | /** | |
602 | * Returns the corresponding application object. Does not support the 'wcf' pseudo application. | |
603 | * | |
4e25add7 MS |
604 | * @param Application $application |
605 | * @return IApplication | |
d335fa16 AE |
606 | */ |
607 | public static function getApplicationObject(Application $application) { | |
608 | if (isset(self::$applicationObjects[$application->packageID])) { | |
609 | return self::$applicationObjects[$application->packageID]; | |
610 | } | |
611 | ||
612 | return null; | |
613 | } | |
614 | ||
615 | /** | |
616 | * Loads an application on runtime, do not use this outside the package installation. | |
617 | * | |
618 | * @param integer $packageID | |
619 | */ | |
620 | public static function loadRuntimeApplication($packageID) { | |
621 | $package = new Package($packageID); | |
622 | $application = new Application($packageID); | |
623 | ||
624 | $abbreviation = Package::getAbbreviation($package->package); | |
625 | $packageDir = FileUtil::getRealPath(WCF_DIR.$package->packageDir); | |
626 | self::$autoloadDirectories[$abbreviation] = $packageDir . 'lib/'; | |
627 | self::$applications[$abbreviation] = $application; | |
628 | self::getTPL()->addApplication($abbreviation, $packageDir . 'acp/templates/'); | |
629 | } | |
630 | ||
631 | /** | |
632 | * Initializes core object cache. | |
633 | */ | |
634 | protected function initCoreObjects() { | |
635 | // ignore core objects if installing WCF | |
636 | if (PACKAGE_ID == 0) { | |
637 | return; | |
638 | } | |
639 | ||
640 | self::$coreObjectCache = CoreObjectCacheBuilder::getInstance()->getData(); | |
641 | } | |
642 | ||
643 | /** | |
644 | * Assigns some default variables to the template engine. | |
645 | */ | |
646 | protected function assignDefaultTemplateVariables() { | |
058cbd6a MS |
647 | self::getTPL()->registerPrefilter(['event', 'hascontent', 'lang']); |
648 | self::getTPL()->assign([ | |
d335fa16 | 649 | '__wcf' => $this, |
e71525e4 | 650 | '__wcfVersion' => LAST_UPDATE_TIME // @deprecated 2.1, use LAST_UPDATE_TIME directly |
058cbd6a | 651 | ]); |
15621401 TD |
652 | EmailTemplateEngine::getInstance()->registerPrefilter(['event', 'hascontent', 'lang']); |
653 | EmailTemplateEngine::getInstance()->assign([ | |
654 | '__wcf' => $this | |
655 | ]); | |
d335fa16 AE |
656 | } |
657 | ||
658 | /** | |
659 | * Wrapper for the getter methods of this class. | |
660 | * | |
661 | * @param string $name | |
662 | * @return mixed value | |
2b770bdd | 663 | * @throws SystemException |
d335fa16 AE |
664 | */ |
665 | public function __get($name) { | |
666 | $method = 'get'.ucfirst($name); | |
667 | if (method_exists($this, $method)) { | |
668 | return $this->$method(); | |
669 | } | |
670 | ||
671 | throw new SystemException("method '".$method."' does not exist in class WCF"); | |
672 | } | |
673 | ||
487db634 MW |
674 | /** |
675 | * Returns true if current application (WCF) is treated as active and was invoked directly. | |
676 | * | |
677 | * @return boolean | |
678 | */ | |
679 | public function isActiveApplication() { | |
680 | return (ApplicationHandler::getInstance()->getActiveApplication()->packageID == 1); | |
681 | } | |
682 | ||
d335fa16 AE |
683 | /** |
684 | * Changes the active language. | |
685 | * | |
686 | * @param integer $languageID | |
687 | */ | |
688 | public static final function setLanguage($languageID) { | |
5b8eae2c MW |
689 | if (!$languageID || LanguageFactory::getInstance()->getLanguage($languageID) === null) { |
690 | $languageID = LanguageFactory::getInstance()->getDefaultLanguageID(); | |
691 | } | |
2d90d60f | 692 | |
d335fa16 AE |
693 | self::$languageObj = LanguageFactory::getInstance()->getLanguage($languageID); |
694 | self::getTPL()->setLanguageID(self::getLanguage()->languageID); | |
10ae3eb8 | 695 | EmailTemplateEngine::getInstance()->setLanguageID(self::getLanguage()->languageID); |
d335fa16 AE |
696 | } |
697 | ||
698 | /** | |
699 | * Includes the required util or exception classes automatically. | |
700 | * | |
701 | * @param string $className | |
702 | * @see spl_autoload_register() | |
703 | */ | |
704 | public static final function autoload($className) { | |
705 | $namespaces = explode('\\', $className); | |
706 | if (count($namespaces) > 1) { | |
707 | $applicationPrefix = array_shift($namespaces); | |
708 | if ($applicationPrefix === '') { | |
709 | $applicationPrefix = array_shift($namespaces); | |
710 | } | |
711 | if (isset(self::$autoloadDirectories[$applicationPrefix])) { | |
712 | $classPath = self::$autoloadDirectories[$applicationPrefix] . implode('/', $namespaces) . '.class.php'; | |
713 | if (file_exists($classPath)) { | |
714 | require_once($classPath); | |
715 | } | |
716 | } | |
717 | } | |
718 | } | |
719 | ||
720 | /** | |
0fcfe5f6 | 721 | * @inheritDoc |
d335fa16 AE |
722 | */ |
723 | public final function __call($name, array $arguments) { | |
724 | // bug fix to avoid php crash, see http://bugs.php.net/bug.php?id=55020 | |
725 | if (!method_exists($this, $name)) { | |
726 | return self::__callStatic($name, $arguments); | |
727 | } | |
728 | ||
729 | return $this->$name($arguments); | |
730 | } | |
731 | ||
732 | /** | |
733 | * Returns dynamically loaded core objects. | |
734 | * | |
735 | * @param string $name | |
736 | * @param array $arguments | |
71952a87 | 737 | * @return object |
2b770bdd | 738 | * @throws SystemException |
d335fa16 AE |
739 | */ |
740 | public static final function __callStatic($name, array $arguments) { | |
741 | $className = preg_replace('~^get~', '', $name); | |
742 | ||
743 | if (isset(self::$coreObject[$className])) { | |
744 | return self::$coreObject[$className]; | |
745 | } | |
746 | ||
747 | $objectName = self::getCoreObject($className); | |
748 | if ($objectName === null) { | |
749 | throw new SystemException("Core object '".$className."' is unknown."); | |
750 | } | |
751 | ||
752 | if (class_exists($objectName)) { | |
63b9817b | 753 | if (!is_subclass_of($objectName, SingletonFactory::class)) { |
7b9ff46b | 754 | throw new ParentClassException($objectName, SingletonFactory::class); |
d335fa16 AE |
755 | } |
756 | ||
058cbd6a | 757 | self::$coreObject[$className] = call_user_func([$objectName, 'getInstance']); |
d335fa16 AE |
758 | return self::$coreObject[$className]; |
759 | } | |
760 | } | |
761 | ||
762 | /** | |
763 | * Searches for cached core object definition. | |
764 | * | |
765 | * @param string $className | |
766 | * @return string | |
767 | */ | |
768 | protected static final function getCoreObject($className) { | |
769 | if (isset(self::$coreObjectCache[$className])) { | |
770 | return self::$coreObjectCache[$className]; | |
771 | } | |
772 | ||
773 | return null; | |
774 | } | |
775 | ||
776 | /** | |
777 | * Returns true if the debug mode is enabled, otherwise false. | |
778 | * | |
779 | * @param boolean $ignoreACP | |
780 | * @return boolean | |
781 | */ | |
782 | public static function debugModeIsEnabled($ignoreACP = false) { | |
783 | // ACP override | |
784 | if (!$ignoreACP && self::$overrideDebugMode) { | |
785 | return true; | |
786 | } | |
787 | else if (defined('ENABLE_DEBUG_MODE') && ENABLE_DEBUG_MODE) { | |
788 | return true; | |
789 | } | |
790 | ||
791 | return false; | |
792 | } | |
793 | ||
794 | /** | |
795 | * Returns true if benchmarking is enabled, otherwise false. | |
796 | * | |
797 | * @return boolean | |
798 | */ | |
799 | public static function benchmarkIsEnabled() { | |
800 | // benchmarking is enabled by default | |
801 | if (!defined('ENABLE_BENCHMARK') || ENABLE_BENCHMARK) return true; | |
802 | return false; | |
803 | } | |
804 | ||
805 | /** | |
806 | * Returns domain path for given application. | |
807 | * | |
808 | * @param string $abbreviation | |
809 | * @return string | |
810 | */ | |
811 | public static function getPath($abbreviation = 'wcf') { | |
812 | // workaround during WCFSetup | |
813 | if (!PACKAGE_ID) { | |
814 | return '../'; | |
815 | } | |
816 | ||
817 | if (!isset(self::$applications[$abbreviation])) { | |
818 | $abbreviation = 'wcf'; | |
819 | } | |
820 | ||
821 | return self::$applications[$abbreviation]->getPageURL(); | |
822 | } | |
823 | ||
5d1c96bc AE |
824 | /** |
825 | * Returns the domain path for the currently active application, | |
826 | * used to avoid CORS requests. | |
827 | * | |
828 | * @return string | |
829 | */ | |
830 | public static function getActivePath() { | |
831 | if (!PACKAGE_ID) { | |
832 | return self::getPath(); | |
833 | } | |
834 | ||
835 | return self::getPath(ApplicationHandler::getInstance()->getAbbreviation(ApplicationHandler::getInstance()->getActiveApplication()->packageID)); | |
836 | } | |
837 | ||
d335fa16 AE |
838 | /** |
839 | * Returns a fully qualified anchor for current page. | |
840 | * | |
841 | * @param string $fragment | |
842 | * @return string | |
843 | */ | |
844 | public function getAnchor($fragment) { | |
694f6545 | 845 | return StringUtil::encodeHTML(self::getRequestURI() . '#' . $fragment); |
d335fa16 AE |
846 | } |
847 | ||
12a2ff01 AE |
848 | /** |
849 | * Returns the currently active page or null if unknown. | |
850 | * | |
b212ba66 | 851 | * @return Page|null |
12a2ff01 AE |
852 | */ |
853 | public static function getActivePage() { | |
a039ebd2 AE |
854 | if (self::getActiveRequest() === null) { |
855 | return null; | |
856 | } | |
857 | ||
5346b183 AE |
858 | if (self::getActiveRequest()->getClassName() === CmsPage::class) { |
859 | $metaData = self::getActiveRequest()->getMetaData(); | |
b212ba66 MS |
860 | if (isset($metaData['cms'])) { |
861 | return PageCache::getInstance()->getPage($metaData['cms']['pageID']); | |
862 | } | |
863 | ||
864 | return null; | |
5346b183 AE |
865 | } |
866 | ||
12a2ff01 AE |
867 | return PageCache::getInstance()->getPageByController(self::getActiveRequest()->getClassName()); |
868 | } | |
869 | ||
870 | /** | |
871 | * Returns the currently active request. | |
872 | * | |
873 | * @return Request | |
874 | */ | |
875 | public static function getActiveRequest() { | |
876 | return RequestHandler::getInstance()->getActiveRequest(); | |
877 | } | |
878 | ||
d335fa16 AE |
879 | /** |
880 | * Returns the URI of the current page. | |
881 | * | |
882 | * @return string | |
883 | */ | |
884 | public static function getRequestURI() { | |
c68f6161 | 885 | return preg_replace('~^(https?://[^/]+)(?:/.*)?$~', '$1', self::getTPL()->get('baseHref')) . $_SERVER['REQUEST_URI']; |
d335fa16 AE |
886 | } |
887 | ||
888 | /** | |
889 | * Resets Zend Opcache cache if installed and enabled. | |
890 | * | |
891 | * @param string $script | |
892 | */ | |
893 | public static function resetZendOpcache($script = '') { | |
894 | if (self::$zendOpcacheEnabled === null) { | |
895 | self::$zendOpcacheEnabled = false; | |
896 | ||
897 | if (extension_loaded('Zend Opcache') && @ini_get('opcache.enable')) { | |
898 | self::$zendOpcacheEnabled = true; | |
899 | } | |
900 | ||
901 | } | |
902 | ||
903 | if (self::$zendOpcacheEnabled) { | |
904 | if (empty($script)) { | |
905 | opcache_reset(); | |
906 | } | |
907 | else { | |
908 | opcache_invalidate($script, true); | |
909 | } | |
910 | } | |
911 | } | |
912 | ||
913 | /** | |
914 | * Returns style handler. | |
915 | * | |
4e25add7 | 916 | * @return StyleHandler |
d335fa16 AE |
917 | */ |
918 | public function getStyleHandler() { | |
919 | return StyleHandler::getInstance(); | |
920 | } | |
921 | ||
55b402a0 MW |
922 | /** |
923 | * Returns box handler. | |
924 | * | |
925 | * @return BoxHandler | |
e71525e4 | 926 | * @since 3.0 |
55b402a0 MW |
927 | */ |
928 | public function getBoxHandler() { | |
929 | return BoxHandler::getInstance(); | |
930 | } | |
931 | ||
d335fa16 AE |
932 | /** |
933 | * Returns number of available updates. | |
934 | * | |
935 | * @return integer | |
936 | */ | |
937 | public function getAvailableUpdates() { | |
938 | $data = PackageUpdateCacheBuilder::getInstance()->getData(); | |
939 | return $data['updates']; | |
940 | } | |
941 | ||
942 | /** | |
943 | * Returns a 8 character prefix for editor autosave. | |
944 | * | |
945 | * @return string | |
946 | */ | |
947 | public function getAutosavePrefix() { | |
948 | return substr(sha1(preg_replace('~^https~', 'http', self::getPath())), 0, 8); | |
949 | } | |
950 | ||
951 | /** | |
952 | * Returns the favicon URL or a base64 encoded image. | |
953 | * | |
954 | * @return string | |
955 | */ | |
956 | public function getFavicon() { | |
957 | $activeApplication = ApplicationHandler::getInstance()->getActiveApplication(); | |
39abe192 | 958 | $wcf = ApplicationHandler::getInstance()->getWCF(); |
d335fa16 | 959 | |
39abe192 | 960 | if ($activeApplication->domainName !== $wcf->domainName) { |
d335fa16 AE |
961 | if (file_exists(WCF_DIR.'images/favicon.ico')) { |
962 | $favicon = file_get_contents(WCF_DIR.'images/favicon.ico'); | |
963 | ||
964 | return 'data:image/x-icon;base64,' . base64_encode($favicon); | |
965 | } | |
966 | } | |
967 | ||
968 | return self::getPath() . 'images/favicon.ico'; | |
969 | } | |
970 | ||
a80873d5 AE |
971 | /** |
972 | * Returns true if currently active request represents the landing page. | |
973 | * | |
8ff2cd79 | 974 | * @return boolean |
a80873d5 | 975 | */ |
0fdd7f96 | 976 | public static function isLandingPage() { |
a039ebd2 AE |
977 | if (self::getActiveRequest() === null) { |
978 | return false; | |
979 | } | |
980 | ||
981 | return self::getActiveRequest()->isLandingPage(); | |
a80873d5 AE |
982 | } |
983 | ||
d335fa16 AE |
984 | /** |
985 | * Initialises the cronjobs. | |
986 | */ | |
987 | protected function initCronjobs() { | |
988 | if (PACKAGE_ID) { | |
63b9817b | 989 | self::getTPL()->assign('executeCronjobs', CronjobScheduler::getInstance()->getNextExec() < TIME_NOW && defined('OFFLINE') && !OFFLINE); |
d335fa16 AE |
990 | } |
991 | } | |
992 | } |