Updating minified JavaScript files
[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 */
271 public static final function handleException(\Exception $e) {
272 try {
273 if ($e instanceof IPrintableException) {
274 $e->show();
275 exit;
276 }
277
278 // repack Exception
279 self::handleException(new SystemException($e->getMessage(), $e->getCode(), '', $e));
280 }
281 catch (\Exception $exception) {
282 die("<pre>WCF::handleException() Unhandled exception: ".$exception->getMessage()."\n\n".$exception->getTraceAsString());
283 }
284 }
285
286 /**
287 * Catches php errors and throws instead a system exception.
288 *
289 * @param integer $errorNo
290 * @param string $message
291 * @param string $filename
292 * @param integer $lineNo
293 */
294 public static final function handleError($errorNo, $message, $filename, $lineNo) {
295 if (error_reporting() != 0) {
296 $type = 'error';
297 switch ($errorNo) {
298 case 2: $type = 'warning';
299 break;
300 case 8: $type = 'notice';
301 break;
302 }
303
304 throw new SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
305 }
306 }
307
308 /**
309 * Loads the database configuration and creates a new connection to the database.
310 */
311 protected function initDB() {
312 // get configuration
313 $dbHost = $dbUser = $dbPassword = $dbName = '';
314 $dbPort = 0;
315 $dbClass = 'wcf\system\database\MySQLDatabase';
316 require(WCF_DIR.'config.inc.php');
317
318 // create database connection
319 self::$dbObj = new $dbClass($dbHost, $dbUser, $dbPassword, $dbName, $dbPort);
320 }
321
322 /**
323 * Loads the options file, automatically created if not exists.
324 */
325 protected function loadOptions() {
326 $filename = WCF_DIR.'options.inc.php';
327
328 // create options file if doesn't exist
329 if (!file_exists($filename) || filemtime($filename) <= 1) {
330 OptionEditor::rebuild();
331 }
332 require_once($filename);
333 }
334
335 /**
336 * Starts the session system.
337 */
338 protected function initSession() {
339 $factory = new SessionFactory();
340 $factory->load();
341
342 self::$sessionObj = SessionHandler::getInstance();
343 }
344
345 /**
346 * Initialises the language engine.
347 */
348 protected function initLanguage() {
349 if (isset($_GET['l']) && !self::getUser()->userID) {
350 self::getSession()->setLanguageID(intval($_GET['l']));
351 }
352
353 // set mb settings
354 mb_internal_encoding('UTF-8');
355 if (function_exists('mb_regex_encoding')) mb_regex_encoding('UTF-8');
356 mb_language('uni');
357
358 // get language
359 self::$languageObj = LanguageFactory::getInstance()->getUserLanguage(self::getSession()->getLanguageID());
360 }
361
362 /**
363 * Initialises the template engine.
364 */
365 protected function initTPL() {
366 self::$tplObj = TemplateEngine::getInstance();
367 self::getTPL()->setLanguageID(self::getLanguage()->languageID);
368 $this->assignDefaultTemplateVariables();
369
370 $this->initStyle();
371 }
372
373 /**
374 * Initializes the user's style.
375 */
376 protected function initStyle() {
377 if (isset($_REQUEST['styleID'])) {
378 self::getSession()->setStyleID(intval($_REQUEST['styleID']));
379 }
380
381 StyleHandler::getInstance()->changeStyle(self::getSession()->getStyleID());
382 }
383
384 /**
385 * Executes the blacklist.
386 */
387 protected function initBlacklist() {
fa75ccfb
AE
388 $isAjax = isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');
389
d335fa16 390 if (defined('BLACKLIST_IP_ADDRESSES') && BLACKLIST_IP_ADDRESSES != '') {
dc0015ab 391 if (!StringUtil::executeWordFilter(UserUtil::convertIPv6To4(self::getSession()->ipAddress), BLACKLIST_IP_ADDRESSES)) {
fa75ccfb 392 if ($isAjax) {
4a1aeea1 393 throw new AJAXException(self::getLanguage()->get('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS);
fa75ccfb
AE
394 }
395 else {
396 throw new PermissionDeniedException();
397 }
dc0015ab
AE
398 }
399 else if (!StringUtil::executeWordFilter(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 }
d335fa16
AE
406 }
407 }
408 if (defined('BLACKLIST_USER_AGENTS') && BLACKLIST_USER_AGENTS != '') {
409 if (!StringUtil::executeWordFilter(self::getSession()->userAgent, BLACKLIST_USER_AGENTS)) {
fa75ccfb 410 if ($isAjax) {
4a1aeea1 411 throw new AJAXException(self::getLanguage()->get('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS);
fa75ccfb
AE
412 }
413 else {
414 throw new PermissionDeniedException();
415 }
d335fa16
AE
416 }
417 }
418 if (defined('BLACKLIST_HOSTNAMES') && BLACKLIST_HOSTNAMES != '') {
419 if (!StringUtil::executeWordFilter(@gethostbyaddr(self::getSession()->ipAddress), BLACKLIST_HOSTNAMES)) {
fa75ccfb 420 if ($isAjax) {
4a1aeea1 421 throw new AJAXException(self::getLanguage()->get('wcf.ajax.error.permissionDenied'), AJAXException::INSUFFICIENT_PERMISSIONS);
fa75ccfb
AE
422 }
423 else {
424 throw new PermissionDeniedException();
425 }
d335fa16
AE
426 }
427 }
428
429 // handle banned users
430 if (self::getUser()->userID && self::getUser()->banned) {
fa75ccfb 431 if ($isAjax) {
d335fa16
AE
432 throw new AJAXException(self::getLanguage()->getDynamicVariable('wcf.user.error.isBanned'), AJAXException::INSUFFICIENT_PERMISSIONS);
433 }
434 else {
435 throw new NamedUserException(self::getLanguage()->getDynamicVariable('wcf.user.error.isBanned'));
436 }
437 }
438 }
439
440 /**
441 * Initializes applications.
442 */
443 protected function initApplications() {
444 // step 1) load all applications
445 $loadedApplications = array();
446
447 // register WCF as application
448 self::$applications['wcf'] = ApplicationHandler::getInstance()->getWCF();
449
450 if (PACKAGE_ID == 1) {
451 return;
452 }
453
454 // start main application
455 $application = ApplicationHandler::getInstance()->getActiveApplication();
456 $loadedApplications[] = $this->loadApplication($application);
457
458 // register primary application
459 $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);
460 self::$applications[$abbreviation] = $application;
461
462 // start dependent applications
463 $applications = ApplicationHandler::getInstance()->getDependentApplications();
464 foreach ($applications as $application) {
465 $loadedApplications[] = $this->loadApplication($application, true);
466 }
467
468 // step 2) run each application
469 if (!class_exists('wcf\system\WCFACP', false)) {
470 foreach ($loadedApplications as $application) {
471 $application->__run();
472 }
473
474 // refresh the session 1 minute before it expires
475 self::getTPL()->assign('__sessionKeepAlive', (SESSION_TIMEOUT - 60));
476 }
477 }
478
479 /**
480 * Loads an application.
481 *
482 * @param \wcf\data\application\Application $application
483 * @param boolean $isDependentApplication
484 * @return \wcf\system\application\IApplication
485 */
486 protected function loadApplication(Application $application, $isDependentApplication = false) {
487 $applicationObject = null;
488 $package = PackageCache::getInstance()->getPackage($application->packageID);
489 // package cache might be outdated
490 if ($package === null) {
491 $package = new Package($application->packageID);
492
493 // package cache is outdated, discard cache
494 if ($package->packageID) {
495 PackageEditor::resetCache();
496 }
497 else {
498 // package id is invalid
499 throw new SystemException("application identified by package id '".$application->packageID."' is unknown");
500 }
501 }
502
503 $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);
504 $packageDir = FileUtil::getRealPath(WCF_DIR.$package->packageDir);
505 self::$autoloadDirectories[$abbreviation] = $packageDir . 'lib/';
506
507 $className = $abbreviation.'\system\\'.strtoupper($abbreviation).'Core';
508 if (class_exists($className) && ClassUtil::isInstanceOf($className, 'wcf\system\application\IApplication')) {
509 // include config file
510 $configPath = $packageDir . PackageInstallationDispatcher::CONFIG_FILE;
511 if (file_exists($configPath)) {
512 require_once($configPath);
513 }
514 else {
515 throw new SystemException('Unable to load configuration for '.$package->package);
516 }
517
518 // register template path if not within ACP
519 if (!class_exists('wcf\system\WCFACP', false)) {
520 // add template path and abbreviation
521 $this->getTPL()->addApplication($abbreviation, $packageDir . 'templates/');
522 }
523
524 // init application and assign it as template variable
525 self::$applicationObjects[$application->packageID] = call_user_func(array($className, 'getInstance'));
526 $this->getTPL()->assign('__'.$abbreviation, self::$applicationObjects[$application->packageID]);
527 }
528 else {
529 unset(self::$autoloadDirectories[$abbreviation]);
530 throw new SystemException("Unable to run '".$package->package."', '".$className."' is missing or does not implement 'wcf\system\application\IApplication'.");
531 }
532
533 // register template path in ACP
534 if (class_exists('wcf\system\WCFACP', false)) {
535 $this->getTPL()->addApplication($abbreviation, $packageDir . 'acp/templates/');
536 }
537 else if (!$isDependentApplication) {
538 // assign base tag
539 $this->getTPL()->assign('baseHref', $application->getPageURL());
540 }
541
542 // register application
543 self::$applications[$abbreviation] = $application;
544
545 return self::$applicationObjects[$application->packageID];
546 }
547
548 /**
549 * Returns the corresponding application object. Does not support the 'wcf' pseudo application.
550 *
551 * @param wcf\data\application\Application $application
552 * @return \wcf\system\application\IApplication
553 */
554 public static function getApplicationObject(Application $application) {
555 if (isset(self::$applicationObjects[$application->packageID])) {
556 return self::$applicationObjects[$application->packageID];
557 }
558
559 return null;
560 }
561
562 /**
563 * Loads an application on runtime, do not use this outside the package installation.
564 *
565 * @param integer $packageID
566 */
567 public static function loadRuntimeApplication($packageID) {
568 $package = new Package($packageID);
569 $application = new Application($packageID);
570
571 $abbreviation = Package::getAbbreviation($package->package);
572 $packageDir = FileUtil::getRealPath(WCF_DIR.$package->packageDir);
573 self::$autoloadDirectories[$abbreviation] = $packageDir . 'lib/';
574 self::$applications[$abbreviation] = $application;
575 self::getTPL()->addApplication($abbreviation, $packageDir . 'acp/templates/');
576 }
577
578 /**
579 * Initializes core object cache.
580 */
581 protected function initCoreObjects() {
582 // ignore core objects if installing WCF
583 if (PACKAGE_ID == 0) {
584 return;
585 }
586
587 self::$coreObjectCache = CoreObjectCacheBuilder::getInstance()->getData();
588 }
589
590 /**
591 * Assigns some default variables to the template engine.
592 */
593 protected function assignDefaultTemplateVariables() {
594 self::getTPL()->registerPrefilter(array('event', 'hascontent', 'lang'));
595 self::getTPL()->assign(array(
596 '__wcf' => $this,
597 '__wcfVersion' => LAST_UPDATE_TIME // @deprecated since 2.1, use LAST_UPDATE_TIME directly
598 ));
599 }
600
601 /**
602 * Wrapper for the getter methods of this class.
603 *
604 * @param string $name
605 * @return mixed value
606 */
607 public function __get($name) {
608 $method = 'get'.ucfirst($name);
609 if (method_exists($this, $method)) {
610 return $this->$method();
611 }
612
613 throw new SystemException("method '".$method."' does not exist in class WCF");
614 }
615
616 /**
617 * Changes the active language.
618 *
619 * @param integer $languageID
620 */
621 public static final function setLanguage($languageID) {
622 self::$languageObj = LanguageFactory::getInstance()->getLanguage($languageID);
623 self::getTPL()->setLanguageID(self::getLanguage()->languageID);
624 }
625
626 /**
627 * Includes the required util or exception classes automatically.
628 *
629 * @param string $className
630 * @see spl_autoload_register()
631 */
632 public static final function autoload($className) {
633 $namespaces = explode('\\', $className);
634 if (count($namespaces) > 1) {
635 $applicationPrefix = array_shift($namespaces);
636 if ($applicationPrefix === '') {
637 $applicationPrefix = array_shift($namespaces);
638 }
639 if (isset(self::$autoloadDirectories[$applicationPrefix])) {
640 $classPath = self::$autoloadDirectories[$applicationPrefix] . implode('/', $namespaces) . '.class.php';
641 if (file_exists($classPath)) {
642 require_once($classPath);
643 }
644 }
645 }
646 }
647
648 /**
649 * @see \wcf\system\WCF::__callStatic()
650 */
651 public final function __call($name, array $arguments) {
652 // bug fix to avoid php crash, see http://bugs.php.net/bug.php?id=55020
653 if (!method_exists($this, $name)) {
654 return self::__callStatic($name, $arguments);
655 }
656
657 return $this->$name($arguments);
658 }
659
660 /**
661 * Returns dynamically loaded core objects.
662 *
663 * @param string $name
664 * @param array $arguments
665 */
666 public static final function __callStatic($name, array $arguments) {
667 $className = preg_replace('~^get~', '', $name);
668
669 if (isset(self::$coreObject[$className])) {
670 return self::$coreObject[$className];
671 }
672
673 $objectName = self::getCoreObject($className);
674 if ($objectName === null) {
675 throw new SystemException("Core object '".$className."' is unknown.");
676 }
677
678 if (class_exists($objectName)) {
679 if (!(ClassUtil::isInstanceOf($objectName, 'wcf\system\SingletonFactory'))) {
680 throw new SystemException("class '".$objectName."' does not implement the interface 'SingletonFactory'");
681 }
682
683 self::$coreObject[$className] = call_user_func(array($objectName, 'getInstance'));
684 return self::$coreObject[$className];
685 }
686 }
687
688 /**
689 * Searches for cached core object definition.
690 *
691 * @param string $className
692 * @return string
693 */
694 protected static final function getCoreObject($className) {
695 if (isset(self::$coreObjectCache[$className])) {
696 return self::$coreObjectCache[$className];
697 }
698
699 return null;
700 }
701
702 /**
703 * Returns true if the debug mode is enabled, otherwise false.
704 *
705 * @param boolean $ignoreACP
706 * @return boolean
707 */
708 public static function debugModeIsEnabled($ignoreACP = false) {
709 // ACP override
710 if (!$ignoreACP && self::$overrideDebugMode) {
711 return true;
712 }
713 else if (defined('ENABLE_DEBUG_MODE') && ENABLE_DEBUG_MODE) {
714 return true;
715 }
716
717 return false;
718 }
719
720 /**
721 * Returns true if benchmarking is enabled, otherwise false.
722 *
723 * @return boolean
724 */
725 public static function benchmarkIsEnabled() {
726 // benchmarking is enabled by default
727 if (!defined('ENABLE_BENCHMARK') || ENABLE_BENCHMARK) return true;
728 return false;
729 }
730
731 /**
732 * Returns domain path for given application.
733 *
734 * @param string $abbreviation
735 * @return string
736 */
737 public static function getPath($abbreviation = 'wcf') {
738 // workaround during WCFSetup
739 if (!PACKAGE_ID) {
740 return '../';
741 }
742
743 if (!isset(self::$applications[$abbreviation])) {
744 $abbreviation = 'wcf';
745 }
746
747 return self::$applications[$abbreviation]->getPageURL();
748 }
749
750 /**
751 * Returns a fully qualified anchor for current page.
752 *
753 * @param string $fragment
754 * @return string
755 */
756 public function getAnchor($fragment) {
757 return self::getRequestURI() . '#' . $fragment;
758 }
759
760 /**
761 * Returns the URI of the current page.
762 *
763 * @return string
764 */
765 public static function getRequestURI() {
766 if (URL_LEGACY_MODE) {
767 // resolve path and query components
768 $scriptName = $_SERVER['SCRIPT_NAME'];
769 $pathInfo = RouteHandler::getPathInfo();
770 if (empty($pathInfo)) {
771 // bug fix if URL omits script name and path
772 $scriptName = substr($scriptName, 0, strrpos($scriptName, '/'));
773 }
774
775 $path = str_replace('/index.php', '', str_replace($scriptName, '', $_SERVER['REQUEST_URI']));
776 if (!StringUtil::isUTF8($path)) {
777 $path = StringUtil::convertEncoding('ISO-8859-1', 'UTF-8', $path);
778 }
779 $path = FileUtil::removeLeadingSlash($path);
780 $baseHref = self::getTPL()->get('baseHref');
781
782 if (!empty($path) && mb_strpos($path, '?') !== 0) {
783 $baseHref .= 'index.php/';
784 }
785
786 return $baseHref . $path;
787 }
788 else {
4f86656e
AE
789 $url = preg_replace('~^(https?://[^/]+)(?:/.*)?$~', '$1', self::getTPL()->get('baseHref'));
790 $url .= $_SERVER['REQUEST_URI'];
d335fa16 791
4f86656e 792 return $url;
d335fa16
AE
793 }
794 }
795
796 /**
797 * Resets Zend Opcache cache if installed and enabled.
798 *
799 * @param string $script
800 */
801 public static function resetZendOpcache($script = '') {
802 if (self::$zendOpcacheEnabled === null) {
803 self::$zendOpcacheEnabled = false;
804
805 if (extension_loaded('Zend Opcache') && @ini_get('opcache.enable')) {
806 self::$zendOpcacheEnabled = true;
807 }
808
809 }
810
811 if (self::$zendOpcacheEnabled) {
812 if (empty($script)) {
813 opcache_reset();
814 }
815 else {
816 opcache_invalidate($script, true);
817 }
818 }
819 }
820
821 /**
822 * Returns style handler.
823 *
824 * @return \wcf\system\style\StyleHandler
825 */
826 public function getStyleHandler() {
827 return StyleHandler::getInstance();
828 }
829
830 /**
831 * Returns number of available updates.
832 *
833 * @return integer
834 */
835 public function getAvailableUpdates() {
836 $data = PackageUpdateCacheBuilder::getInstance()->getData();
837 return $data['updates'];
838 }
839
840 /**
841 * Returns a 8 character prefix for editor autosave.
842 *
843 * @return string
844 */
845 public function getAutosavePrefix() {
846 return substr(sha1(preg_replace('~^https~', 'http', self::getPath())), 0, 8);
847 }
848
849 /**
850 * Returns the favicon URL or a base64 encoded image.
851 *
852 * @return string
853 */
854 public function getFavicon() {
855 $activeApplication = ApplicationHandler::getInstance()->getActiveApplication();
856 $primaryApplication = ApplicationHandler::getInstance()->getPrimaryApplication();
857
858 if ($activeApplication->domainName != $primaryApplication->domainName) {
859 if (file_exists(WCF_DIR.'images/favicon.ico')) {
860 $favicon = file_get_contents(WCF_DIR.'images/favicon.ico');
861
862 return 'data:image/x-icon;base64,' . base64_encode($favicon);
863 }
864 }
865
866 return self::getPath() . 'images/favicon.ico';
867 }
868
869 /**
870 * Initialises the cronjobs.
871 */
872 protected function initCronjobs() {
873 if (PACKAGE_ID) {
874 self::getTPL()->assign('executeCronjobs', (CronjobScheduler::getInstance()->getNextExec() < TIME_NOW && defined('OFFLINE') && !OFFLINE));
875 }
876 }
877}