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