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