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