Commited dos linebreaks by accident
authorTim Düsterhus <timwolla@arcor.de>
Sat, 7 Jul 2012 15:35:48 +0000 (17:35 +0200)
committerTim Düsterhus <timwolla@arcor.de>
Sat, 7 Jul 2012 15:35:48 +0000 (17:35 +0200)
wcfsetup/install/files/lib/system/WCF.class.php

index 7842618cfd755c87d79310170efbc3f594c0154a..86049bc132b613aed67201c9e38a2d4ff376116f 100644 (file)
-<?php\r
-namespace wcf\system;\r
-use wcf\data\application\Application;\r
-use wcf\data\package\Package;\r
-use wcf\data\package\PackageCache;\r
-use wcf\system\application\ApplicationHandler;\r
-use wcf\system\cache\CacheHandler;\r
-use wcf\system\cronjob\CronjobScheduler;\r
-use wcf\system\database\statement\PreparedStatement;\r
-use wcf\system\language\LanguageFactory;\r
-use wcf\system\package\PackageInstallationDispatcher;\r
-use wcf\system\session\SessionFactory;\r
-use wcf\system\session\SessionHandler;\r
-use wcf\system\template\TemplateEngine;\r
-use wcf\system\user\storage\UserStorageHandler;\r
-use wcf\system\exception;\r
-use wcf\util\StringStack;\r
-use wcf\util;\r
-\r
-// try to disable execution time limit\r
-@set_time_limit(0);\r
-\r
-// define current wcf version\r
-define('WCF_VERSION', '2.0.0 Alpha 1 (Maelstrom)');\r
-\r
-// define current unix timestamp\r
-define('TIME_NOW', time());\r
-\r
-// wcf imports\r
-if (!defined('NO_IMPORTS')) {\r
-       require_once(WCF_DIR.'lib/core.functions.php');\r
-}\r
-\r
-/**\r
- * WCF is the central class for the community framework.\r
- * It holds the database connection, access to template and language engine.\r
- *\r
- * @author     Marcel Werk\r
- * @copyright  2001-2011 WoltLab GmbH\r
- * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>\r
- * @package    com.woltlab.wcf\r
- * @subpackage system\r
- * @category   Community Framework\r
- */\r
-class WCF {\r
-       /**\r
-        * list of currently loaded applications\r
-        * @var array<wcf\system\application\IApplication>\r
-        */\r
-       protected static $applications = array();\r
-       \r
-       /**\r
-        * list of autoload directories\r
-        * @var array\r
-        */\r
-       protected static $autoloadDirectories = array();\r
-       \r
-       /**\r
-        * list of unique instances of each core object\r
-        * @var array<wcf\system\SingletonFactory>\r
-        */\r
-       protected static $coreObject = array();\r
-       \r
-       /**\r
-        * list of cached core objects\r
-        * @var array<array>\r
-        */\r
-       protected static $coreObjectCache = array();\r
-       \r
-       /**\r
-        * list of ids of dependent packages\r
-        * @var array<integer>\r
-        */     \r
-       protected static $packageDependencies = array();\r
-       \r
-       /**\r
-        * database object\r
-        * @var wcf\system\database\Database\r
-        */\r
-       protected static $dbObj = null;\r
-       \r
-       /**\r
-        * language object\r
-        * @var wcf\system\language\Language\r
-        */\r
-       protected static $languageObj = null;\r
-       \r
-       /**\r
-        * session object\r
-        * @var wcf\system\session\SessionHandler\r
-        */\r
-       protected static $sessionObj = null;\r
-       \r
-       /**\r
-        * template object\r
-        * @var wcf\system\template\TemplateEngine\r
-        */\r
-       protected static $tplObj = null;\r
-       \r
-       /**\r
-        * Calls all init functions of the WCF class.\r
-        */\r
-       public function __construct() {\r
-               // add autoload directory\r
-               self::$autoloadDirectories['wcf'] = WCF_DIR . 'lib/';\r
-               \r
-               // define tmp directory\r
-               if (!defined('TMP_DIR')) define('TMP_DIR', util\FileUtil::getTempFolder());\r
-               \r
-               // start initialization\r
-               $this->initMagicQuotes();\r
-               $this->initDB();\r
-               $this->loadOptions();\r
-               $this->initCache();\r
-               $this->initSession();\r
-               $this->initLanguage();\r
-               $this->initTPL();\r
-               $this->initCronjobs();\r
-               $this->initBlacklist();\r
-               $this->initCoreObjects();\r
-               $this->initApplications();\r
-       }\r
-       \r
-       /**\r
-        * Replacement of the "__destruct()" method.\r
-        * Seems that under specific conditions (windows) the destructor is not called automatically.\r
-        * So we use the php register_shutdown_function to register an own destructor method.\r
-        * Flushs the output, updates the session and executes the shutdown queries.\r
-        */\r
-       public static function destruct() {\r
-               // flush ouput\r
-               if (ob_get_level() && ini_get('output_handler')) {\r
-                       ob_flush();\r
-               }\r
-               else {\r
-                       flush();\r
-               }\r
-               \r
-               // update session\r
-               if (is_object(self::getSession())) {\r
-                       self::getSession()->update();\r
-               }\r
-               \r
-               // close cache source\r
-               if (is_object(CacheHandler::getInstance()) && is_object(CacheHandler::getInstance()->getCacheSource())) {\r
-                       CacheHandler::getInstance()->getCacheSource()->close();\r
-               }\r
-               \r
-               // execute shutdown actions of user storage handler\r
-               UserStorageHandler::getInstance()->shutdown();\r
-       }\r
-       \r
-       /**\r
-        * Removes slashes in superglobal gpc data arrays if 'magic quotes gpc' is enabled.\r
-        */\r
-       protected function initMagicQuotes() {\r
-               if (function_exists('get_magic_quotes_gpc')) {\r
-                       if (@get_magic_quotes_gpc()) {\r
-                               if (count($_REQUEST)) {\r
-                                       $_REQUEST = util\ArrayUtil::stripslashes($_REQUEST);\r
-                               }\r
-                               if (count($_POST)) {\r
-                                       $_POST = util\ArrayUtil::stripslashes($_POST);\r
-                               }\r
-                               if (count($_GET)) {\r
-                                       $_GET = util\ArrayUtil::stripslashes($_GET);\r
-                               }\r
-                               if (count($_COOKIE)) {\r
-                                       $_COOKIE = util\ArrayUtil::stripslashes($_COOKIE);\r
-                               }\r
-                               if (count($_FILES)) {\r
-                                       foreach ($_FILES as $name => $attributes) {\r
-                                               foreach ($attributes as $key => $value) {\r
-                                                       if ($key != 'tmp_name') {\r
-                                                               $_FILES[$name][$key] = util\ArrayUtil::stripslashes($value);\r
-                                                       }\r
-                                               }\r
-                                       }\r
-                               }\r
-                       }\r
-               }\r
-       \r
-               if (function_exists('set_magic_quotes_runtime')) {\r
-                       @set_magic_quotes_runtime(0);\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Returns the database object.\r
-        *\r
-        * @return      wcf\system\database\Database\r
-        */\r
-       public static final function getDB() {\r
-               return self::$dbObj;\r
-       }\r
-       \r
-       /**\r
-        * Returns the session object.\r
-        *\r
-        * @return      wcf\system\session\SessionHandler\r
-        */\r
-       public static final function getSession() {\r
-               return self::$sessionObj;\r
-       }\r
-       \r
-       /**\r
-        * Returns the user object.\r
-        *\r
-        * @return      wcf\data\user\User\r
-        */\r
-       public static final function getUser() {\r
-               return self::getSession()->getUser();\r
-       }\r
-       \r
-       /**\r
-        * Returns the language object.\r
-        *\r
-        * @return      wcf\data\language\Language\r
-        */\r
-       public static final function getLanguage() {\r
-               return self::$languageObj;\r
-       }\r
-       \r
-       /**\r
-        * Returns the template object.\r
-        *\r
-        * @return      wcf\system\template\TemplateEngine\r
-        */\r
-       public static final function getTPL() {\r
-               return self::$tplObj;\r
-       }\r
-       \r
-       /**\r
-        * Calls the show method on the given exception.\r
-        *\r
-        * @param       \Exception      $e\r
-        */\r
-       public static final function handleException(\Exception $e) {\r
-               if ($e instanceof exception\IPrintableException) {\r
-                       $e->show();\r
-                       exit;\r
-               }\r
-               \r
-               // repack Exception\r
-               self::handleException(new exception\SystemException($e->getMessage(), $e->getCode(), '', $e));\r
-       }\r
-       \r
-       /**\r
-        * Catches php errors and throws instead a system exception.\r
-        *\r
-        * @param       integer         $errorNo\r
-        * @param       string          $message\r
-        * @param       string          $filename\r
-        * @param       integer         $lineNo\r
-        */\r
-       public static final function handleError($errorNo, $message, $filename, $lineNo) {\r
-               if (error_reporting() != 0) {\r
-                       $type = 'error';\r
-                       switch ($errorNo) {\r
-                               case 2: $type = 'warning';\r
-                                       break;\r
-                               case 8: $type = 'notice';\r
-                                       break;\r
-                       }\r
-                       \r
-                       throw new exception\SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Loads the database configuration and creates a new connection to the database.\r
-        */\r
-       protected function initDB() {\r
-               // get configuration\r
-               $dbHost = $dbUser = $dbPassword = $dbName = '';\r
-               $dbPort = 0;\r
-               $dbClass = 'wcf\system\database\MySQLDatabase';\r
-               require(WCF_DIR.'config.inc.php');\r
-               \r
-               // create database connection\r
-               self::$dbObj = new $dbClass($dbHost, $dbUser, $dbPassword, $dbName, $dbPort);\r
-       }\r
-       \r
-       /**\r
-        * Initialises the cache handler and loads the default cache resources.\r
-        */\r
-       protected function initCache() {\r
-               $this->loadDefaultCacheResources();\r
-       }\r
-       \r
-       /**\r
-        * Loads the default cache resources.\r
-        */\r
-       protected function loadDefaultCacheResources() {\r
-               CacheHandler::getInstance()->addResource(\r
-                       'languages',\r
-                       WCF_DIR.'cache/cache.languages.php',\r
-                       'wcf\system\cache\builder\LanguageCacheBuilder'\r
-               );\r
-               CacheHandler::getInstance()->addResource(\r
-                       'spiders',\r
-                       WCF_DIR.'cache/cache.spiders.php',\r
-                       'wcf\system\cache\builder\SpiderCacheBuilder'\r
-               );\r
-               \r
-               if (defined('PACKAGE_ID')) {\r
-                       CacheHandler::getInstance()->addResource(\r
-                               'coreObjects-'.PACKAGE_ID,\r
-                               WCF_DIR.'cache/cache.coreObjects-'.PACKAGE_ID.'.php',\r
-                               'wcf\system\cache\builder\CoreObjectCacheBuilder'\r
-                       );\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Includes the options file.\r
-        * If the option file doesn't exist, the rebuild of it is started.\r
-        * \r
-        * @param       string          $filename\r
-        */\r
-       protected function loadOptions($filename = null, $packageID = 1) {\r
-               if ($filename === null) $filename = WCF_DIR.'options.inc.php';\r
-               \r
-               // create options file if doesn't exist\r
-               if (!file_exists($filename) || filemtime($filename) <= 1) {\r
-                       \wcf\data\option\OptionEditor::rebuildFile($filename, $packageID);\r
-               }\r
-               require_once($filename);\r
-       }\r
-       \r
-       /**\r
-        * Starts the session system.\r
-        */\r
-       protected function initSession() {\r
-               $factory = new SessionFactory();\r
-               $factory->load();\r
-               \r
-               self::$sessionObj = SessionHandler::getInstance();\r
-       }\r
-       \r
-       /**\r
-        * Initialises the language engine.\r
-        */\r
-       protected function initLanguage() {\r
-               if (isset($_GET['l']) && !self::getUser()->userID) {\r
-                       self::getSession()->setLanguageID(intval($_GET['l']));\r
-               }\r
-               \r
-               // set mb settings\r
-               mb_internal_encoding('UTF-8');\r
-               if (function_exists('mb_regex_encoding')) mb_regex_encoding('UTF-8');\r
-               mb_language('uni');\r
-               \r
-               // get language\r
-               self::$languageObj = LanguageFactory::getInstance()->getUserLanguage(self::getSession()->getLanguageID());\r
-               self::$languageObj->setLocale();\r
-       }\r
-       \r
-       /**\r
-        * Initialises the template engine.\r
-        */\r
-       protected function initTPL() {\r
-               self::$tplObj = TemplateEngine::getInstance();\r
-               self::getTPL()->setLanguageID(self::getLanguage()->languageID);\r
-               $this->assignDefaultTemplateVariables();\r
-       }\r
-       \r
-       /**\r
-        * Executes the blacklist.\r
-        */\r
-       protected function initBlacklist() {\r
-               if (defined('BLACKLIST_IP_ADDRESSES') && BLACKLIST_IP_ADDRESSES != '') {\r
-                       if (!util\StringUtil::executeWordFilter(WCF::getSession()->ipAddress, BLACKLIST_IP_ADDRESSES)) {\r
-                               throw new exception\PermissionDeniedException();\r
-                       }\r
-               }\r
-               if (defined('BLACKLIST_USER_AGENTS') && BLACKLIST_USER_AGENTS != '') {\r
-                       if (!util\StringUtil::executeWordFilter(WCF::getSession()->userAgent, BLACKLIST_USER_AGENTS)) {\r
-                               throw new exception\PermissionDeniedException();\r
-                       }\r
-               }\r
-               if (defined('BLACKLIST_HOSTNAMES') && BLACKLIST_HOSTNAMES != '') {\r
-                       if (!util\StringUtil::executeWordFilter(@gethostbyaddr(WCF::getSession()->ipAddress), BLACKLIST_HOSTNAMES)) {\r
-                               throw new exception\PermissionDeniedException();\r
-                       }\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Initializes applications.\r
-        */\r
-       protected function initApplications() {\r
-               // register WCF as application\r
-               self::$applications['wcf'] = new Application(1);\r
-               \r
-               // do not init applications if within wcf\r
-               if (PACKAGE_ID == 1) return;\r
-               \r
-               // start main application\r
-               $application = ApplicationHandler::getInstance()->getActiveApplication();\r
-               $this->loadApplication($application);\r
-               \r
-               // register primary application\r
-               $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);\r
-               self::$applications[$abbreviation] = $application;\r
-               \r
-               // start dependent applications\r
-               $applications = ApplicationHandler::getInstance()->getDependentApplications();\r
-               foreach ($applications as $application) {\r
-                       $this->loadApplication($application, true);\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Loads an application.\r
-        *\r
-        * @param       wcf\data\application\Application                $application\r
-        * @param       boolean                                         $isDependentApplication\r
-        */     \r
-       protected function loadApplication(Application $application, $isDependentApplication = false) {\r
-               $package = PackageCache::getInstance()->getPackage($application->packageID);\r
-               \r
-               $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);\r
-               $packageDir = util\FileUtil::getRealPath(WCF_DIR.$package->packageDir);\r
-               self::$autoloadDirectories[$abbreviation] = $packageDir . 'lib/';\r
-               \r
-               $className = $abbreviation.'\system\\'.strtoupper($abbreviation).'Core';\r
-               if (class_exists($className) && util\ClassUtil::isInstanceOf($className, 'wcf\system\application\IApplication')) {\r
-                       // include config file\r
-                       $configPath = $packageDir . PackageInstallationDispatcher::CONFIG_FILE;\r
-                       if (file_exists($configPath)) {\r
-                               require_once($configPath);\r
-                       }\r
-                       else {\r
-                               throw new exception\SystemException('Unable to load configuration for '.$package->package);\r
-                       }\r
-                       \r
-                       // start application if not within ACP\r
-                       if (!class_exists('wcf\system\WCFACP', false)) {\r
-                               new $className();\r
-                       }\r
-               }\r
-               else {\r
-                       unset(self::$autoloadDirectories[$abbreviation]);\r
-                       throw new exception\SystemException("Unable to run '".$package->package."', '".$className."' is missing or does not implement 'wcf\system\application\IApplication'.");\r
-               }\r
-               \r
-               // load options\r
-               $this->loadOptions($packageDir.'options.inc.php', $application->packageID);\r
-               \r
-               // register template path in ACP\r
-               if (class_exists('wcf\system\WCFACP', false)) {\r
-                       $this->getTPL()->addTemplatePath($application->packageID, $packageDir . 'acp/templates/');\r
-               }\r
-               else if (!$isDependentApplication) {\r
-                       // assign base tag\r
-                       $this->getTPL()->assign('baseHref', $application->domainName . $application->domainPath);\r
-               }\r
-               \r
-               // register application\r
-               self::$applications[$abbreviation] = $application;\r
-       }\r
-       \r
-       /**\r
-        * Initializes core object cache.\r
-        */\r
-       protected function initCoreObjects() {\r
-               // ignore core objects if installing WCF\r
-               if (PACKAGE_ID == 0) {\r
-                       return;\r
-               }\r
-               \r
-               self::$coreObjectCache = CacheHandler::getInstance()->get('coreObjects-'.PACKAGE_ID);\r
-               self::$packageDependencies = \wcf\system\package\PackageDependencyHandler::getInstance()->getDependencies();\r
-       }\r
-       \r
-       /**\r
-        * Assigns some default variables to the template engine.\r
-        */\r
-       protected function assignDefaultTemplateVariables() {\r
-               self::getTPL()->registerPrefilter(array('event', 'hascontent', 'lang', 'icon'));\r
-               self::getTPL()->assign(array('__wcf' => $this));\r
-       }\r
-       \r
-       /**\r
-        * Wrapper for the getter methods of this class.\r
-        *\r
-        * @param       string          $name\r
-        * @return      mixed           value\r
-        */\r
-       public function __get($name) {\r
-               $method = 'get'.ucfirst($name);\r
-               if (method_exists($this, $method)) {\r
-                       return $this->$method();\r
-               }\r
-               \r
-               throw new exception\SystemException("method '".$method."' does not exist in class WCF");\r
-       }\r
-       \r
-       /**\r
-        * Changes the active language.\r
-        *\r
-        * @param       integer         $languageID\r
-        */\r
-       public static final function setLanguage($languageID) {\r
-               self::$languageObj = LanguageFactory::getInstance()->getLanguage($languageID);\r
-               self::getTPL()->setLanguageID(self::getLanguage()->languageID);\r
-       }\r
-       \r
-       /**\r
-        * Includes the required util or exception classes automatically.\r
-        *\r
-        * @param       string          $className\r
-        * @see         spl_autoload_register()\r
-        */\r
-       public static final function autoload($className) {\r
-               $namespaces = explode('\\', $className);\r
-               if (count($namespaces) > 1) {\r
-                       $applicationPrefix = array_shift($namespaces);\r
-                       if (isset(self::$autoloadDirectories[$applicationPrefix])) {\r
-                               $classPath = self::$autoloadDirectories[$applicationPrefix] . implode('/', $namespaces) . '.class.php';\r
-                               if (file_exists($classPath)) {\r
-                                       require_once($classPath);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * @see wcf\system\WCF::__callStatic()\r
-        */\r
-       public final function __call($name, array $arguments) {\r
-               // bug fix to avoid php crash, see http://bugs.php.net/bug.php?id=55020\r
-               if (!method_exists($this, $name)) {\r
-                       return self::__callStatic($name, $arguments);\r
-               }\r
-               \r
-               return $this->$name($arguments);\r
-       }\r
-       \r
-       /**\r
-        * Returns dynamically loaded core objects.\r
-        *\r
-        * @param       string          $name\r
-        * @param       array           $arguments\r
-        */\r
-       public static final function __callStatic($name, array $arguments) {\r
-               $className = preg_replace('~^get~', '', $name);\r
-               \r
-               if (isset(self::$coreObject[$className])) {\r
-                       return self::$coreObject[$className];\r
-               }\r
-               \r
-               $objectName = self::getCoreObject($className);\r
-               if ($objectName === null) {\r
-                       throw new exception\SystemException("Core object '".$className."' is unknown.");\r
-               }\r
-               \r
-               if (class_exists($objectName)) {\r
-                       if (!(util\ClassUtil::isInstanceOf($objectName, 'wcf\system\SingletonFactory'))) {\r
-                               throw new exception\SystemException("class '".$objectName."' does not implement the interface 'SingletonFactory'");\r
-                       }\r
-                       \r
-                       self::$coreObject[$className] = call_user_func(array($objectName, 'getInstance'));\r
-                       return self::$coreObject[$className];\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Searches for cached core object definition.\r
-        *\r
-        * @param       string          $className\r
-        * @return      string\r
-        */\r
-       protected static final function getCoreObject($className) {\r
-               foreach (self::$packageDependencies as $packageID) {\r
-                       if (isset(self::$coreObjectCache[$packageID][$className])) {\r
-                               return self::$coreObjectCache[$packageID][$className];\r
-                       }\r
-               }\r
-               \r
-               return null;\r
-       }\r
-       \r
-       /**\r
-        * Returns true if the debug mode is enabled, otherwise false.\r
-        * \r
-        * @return boolean\r
-        */\r
-       public static function debugModeIsEnabled() {\r
-               if (defined('ENABLE_DEBUG_MODE') && ENABLE_DEBUG_MODE) return true;\r
-               return false;\r
-       }\r
-       \r
-       /**\r
-        * Returns true if benchmarking is enabled, otherwise false.\r
-        * \r
-        * @return boolean\r
-        */\r
-       public static function benchmarkIsEnabled() {\r
-               // benchmarking is enabled by default\r
-               if (!defined('ENABLE_BENCHMARK') || ENABLE_BENCHMARK) return true;\r
-               return false;\r
-       }\r
-       \r
-       /**\r
-        * Returns domain path for given application.\r
-        * \r
-        * @param       string          $abbreviation\r
-        * @return      string\r
-        */\r
-       public static function getPath($abbreviation = 'wcf') {\r
-               // workaround during WCFSetup\r
-               if (!PACKAGE_ID) {\r
-                       return '../';\r
-               }\r
-               \r
-               if (!isset(self::$applications[$abbreviation])) {\r
-                       $abbreviation = 'wcf';\r
-               }\r
-               \r
-               return self::$applications[$abbreviation]->domainName . self::$applications[$abbreviation]->domainPath;\r
-       }\r
-       \r
-       /**\r
-        * Returns a fully qualified anchor for current page.\r
-        * \r
-        * @param       string          $fragment\r
-        * @return      string\r
-        */\r
-       public function getAnchor($fragment) {\r
-               // resolve path and query components\r
-               $scriptName = $_SERVER['SCRIPT_NAME'];\r
-               if (empty($_SERVER['PATH_INFO'])) {\r
-                       // bug fix if URL omits script name and path\r
-                       $scriptName = substr($scriptName, 0, strrpos($scriptName, '/'));\r
-               }\r
-               \r
-               $path = str_replace('/index.php', '', str_replace($scriptName, '', $_SERVER['REQUEST_URI']));\r
-               $baseHref = self::getTPL()->get('baseHref');\r
-               \r
-               return $baseHref . 'index.php' . $path . '#' . $fragment;\r
-       }\r
-       \r
-       /**\r
-        * Initialises the cronjobs.\r
-        */\r
-       protected function initCronjobs() {\r
-               if (PACKAGE_ID) {\r
-                       self::getTPL()->assign('executeCronjobs', CronjobScheduler::getInstance()->getNextExec() < TIME_NOW);\r
-               }\r
-       }\r
-}\r
+<?php
+namespace wcf\system;
+use wcf\data\application\Application;
+use wcf\data\package\Package;
+use wcf\data\package\PackageCache;
+use wcf\system\application\ApplicationHandler;
+use wcf\system\cache\CacheHandler;
+use wcf\system\cronjob\CronjobScheduler;
+use wcf\system\database\statement\PreparedStatement;
+use wcf\system\language\LanguageFactory;
+use wcf\system\package\PackageInstallationDispatcher;
+use wcf\system\session\SessionFactory;
+use wcf\system\session\SessionHandler;
+use wcf\system\template\TemplateEngine;
+use wcf\system\user\storage\UserStorageHandler;
+use wcf\system\exception;
+use wcf\util\StringStack;
+use wcf\util;
+
+// try to disable execution time limit
+@set_time_limit(0);
+
+// define current wcf version
+define('WCF_VERSION', '2.0.0 Alpha 1 (Maelstrom)');
+
+// define current unix timestamp
+define('TIME_NOW', time());
+
+// wcf imports
+if (!defined('NO_IMPORTS')) {
+       require_once(WCF_DIR.'lib/core.functions.php');
+}
+
+/**
+ * WCF is the central class for the community framework.
+ * It holds the database connection, access to template and language engine.
+ *
+ * @author     Marcel Werk
+ * @copyright  2001-2011 WoltLab GmbH
+ * @license    GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @package    com.woltlab.wcf
+ * @subpackage system
+ * @category   Community Framework
+ */
+class WCF {
+       /**
+        * list of currently loaded applications
+        * @var array<wcf\system\application\IApplication>
+        */
+       protected static $applications = array();
+       
+       /**
+        * list of autoload directories
+        * @var array
+        */
+       protected static $autoloadDirectories = array();
+       
+       /**
+        * list of unique instances of each core object
+        * @var array<wcf\system\SingletonFactory>
+        */
+       protected static $coreObject = array();
+       
+       /**
+        * list of cached core objects
+        * @var array<array>
+        */
+       protected static $coreObjectCache = array();
+       
+       /**
+        * list of ids of dependent packages
+        * @var array<integer>
+        */     
+       protected static $packageDependencies = array();
+       
+       /**
+        * database object
+        * @var wcf\system\database\Database
+        */
+       protected static $dbObj = null;
+       
+       /**
+        * language object
+        * @var wcf\system\language\Language
+        */
+       protected static $languageObj = null;
+       
+       /**
+        * session object
+        * @var wcf\system\session\SessionHandler
+        */
+       protected static $sessionObj = null;
+       
+       /**
+        * template object
+        * @var wcf\system\template\TemplateEngine
+        */
+       protected static $tplObj = null;
+       
+       /**
+        * Calls all init functions of the WCF class.
+        */
+       public function __construct() {
+               // add autoload directory
+               self::$autoloadDirectories['wcf'] = WCF_DIR . 'lib/';
+               
+               // define tmp directory
+               if (!defined('TMP_DIR')) define('TMP_DIR', util\FileUtil::getTempFolder());
+               
+               // start initialization
+               $this->initMagicQuotes();
+               $this->initDB();
+               $this->loadOptions();
+               $this->initCache();
+               $this->initSession();
+               $this->initLanguage();
+               $this->initTPL();
+               $this->initCronjobs();
+               $this->initBlacklist();
+               $this->initCoreObjects();
+               $this->initApplications();
+       }
+       
+       /**
+        * Replacement of the "__destruct()" method.
+        * Seems that under specific conditions (windows) the destructor is not called automatically.
+        * So we use the php register_shutdown_function to register an own destructor method.
+        * Flushs the output, updates the session and executes the shutdown queries.
+        */
+       public static function destruct() {
+               // flush ouput
+               if (ob_get_level() && ini_get('output_handler')) {
+                       ob_flush();
+               }
+               else {
+                       flush();
+               }
+               
+               // update session
+               if (is_object(self::getSession())) {
+                       self::getSession()->update();
+               }
+               
+               // close cache source
+               if (is_object(CacheHandler::getInstance()) && is_object(CacheHandler::getInstance()->getCacheSource())) {
+                       CacheHandler::getInstance()->getCacheSource()->close();
+               }
+               
+               // execute shutdown actions of user storage handler
+               UserStorageHandler::getInstance()->shutdown();
+       }
+       
+       /**
+        * Removes slashes in superglobal gpc data arrays if 'magic quotes gpc' is enabled.
+        */
+       protected function initMagicQuotes() {
+               if (function_exists('get_magic_quotes_gpc')) {
+                       if (@get_magic_quotes_gpc()) {
+                               if (count($_REQUEST)) {
+                                       $_REQUEST = util\ArrayUtil::stripslashes($_REQUEST);
+                               }
+                               if (count($_POST)) {
+                                       $_POST = util\ArrayUtil::stripslashes($_POST);
+                               }
+                               if (count($_GET)) {
+                                       $_GET = util\ArrayUtil::stripslashes($_GET);
+                               }
+                               if (count($_COOKIE)) {
+                                       $_COOKIE = util\ArrayUtil::stripslashes($_COOKIE);
+                               }
+                               if (count($_FILES)) {
+                                       foreach ($_FILES as $name => $attributes) {
+                                               foreach ($attributes as $key => $value) {
+                                                       if ($key != 'tmp_name') {
+                                                               $_FILES[$name][$key] = util\ArrayUtil::stripslashes($value);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+       
+               if (function_exists('set_magic_quotes_runtime')) {
+                       @set_magic_quotes_runtime(0);
+               }
+       }
+       
+       /**
+        * Returns the database object.
+        *
+        * @return      wcf\system\database\Database
+        */
+       public static final function getDB() {
+               return self::$dbObj;
+       }
+       
+       /**
+        * Returns the session object.
+        *
+        * @return      wcf\system\session\SessionHandler
+        */
+       public static final function getSession() {
+               return self::$sessionObj;
+       }
+       
+       /**
+        * Returns the user object.
+        *
+        * @return      wcf\data\user\User
+        */
+       public static final function getUser() {
+               return self::getSession()->getUser();
+       }
+       
+       /**
+        * Returns the language object.
+        *
+        * @return      wcf\data\language\Language
+        */
+       public static final function getLanguage() {
+               return self::$languageObj;
+       }
+       
+       /**
+        * Returns the template object.
+        *
+        * @return      wcf\system\template\TemplateEngine
+        */
+       public static final function getTPL() {
+               return self::$tplObj;
+       }
+       
+       /**
+        * Calls the show method on the given exception.
+        *
+        * @param       \Exception      $e
+        */
+       public static final function handleException(\Exception $e) {
+               if ($e instanceof exception\IPrintableException) {
+                       $e->show();
+                       exit;
+               }
+               
+               // repack Exception
+               self::handleException(new exception\SystemException($e->getMessage(), $e->getCode(), '', $e));
+       }
+       
+       /**
+        * Catches php errors and throws instead a system exception.
+        *
+        * @param       integer         $errorNo
+        * @param       string          $message
+        * @param       string          $filename
+        * @param       integer         $lineNo
+        */
+       public static final function handleError($errorNo, $message, $filename, $lineNo) {
+               if (error_reporting() != 0) {
+                       $type = 'error';
+                       switch ($errorNo) {
+                               case 2: $type = 'warning';
+                                       break;
+                               case 8: $type = 'notice';
+                                       break;
+                       }
+                       
+                       throw new exception\SystemException('PHP '.$type.' in file '.$filename.' ('.$lineNo.'): '.$message, 0);
+               }
+       }
+       
+       /**
+        * Loads the database configuration and creates a new connection to the database.
+        */
+       protected function initDB() {
+               // get configuration
+               $dbHost = $dbUser = $dbPassword = $dbName = '';
+               $dbPort = 0;
+               $dbClass = 'wcf\system\database\MySQLDatabase';
+               require(WCF_DIR.'config.inc.php');
+               
+               // create database connection
+               self::$dbObj = new $dbClass($dbHost, $dbUser, $dbPassword, $dbName, $dbPort);
+       }
+       
+       /**
+        * Initialises the cache handler and loads the default cache resources.
+        */
+       protected function initCache() {
+               $this->loadDefaultCacheResources();
+       }
+       
+       /**
+        * Loads the default cache resources.
+        */
+       protected function loadDefaultCacheResources() {
+               CacheHandler::getInstance()->addResource(
+                       'languages',
+                       WCF_DIR.'cache/cache.languages.php',
+                       'wcf\system\cache\builder\LanguageCacheBuilder'
+               );
+               CacheHandler::getInstance()->addResource(
+                       'spiders',
+                       WCF_DIR.'cache/cache.spiders.php',
+                       'wcf\system\cache\builder\SpiderCacheBuilder'
+               );
+               
+               if (defined('PACKAGE_ID')) {
+                       CacheHandler::getInstance()->addResource(
+                               'coreObjects-'.PACKAGE_ID,
+                               WCF_DIR.'cache/cache.coreObjects-'.PACKAGE_ID.'.php',
+                               'wcf\system\cache\builder\CoreObjectCacheBuilder'
+                       );
+               }
+       }
+       
+       /**
+        * Includes the options file.
+        * If the option file doesn't exist, the rebuild of it is started.
+        * 
+        * @param       string          $filename
+        */
+       protected function loadOptions($filename = null, $packageID = 1) {
+               if ($filename === null) $filename = WCF_DIR.'options.inc.php';
+               
+               // create options file if doesn't exist
+               if (!file_exists($filename) || filemtime($filename) <= 1) {
+                       \wcf\data\option\OptionEditor::rebuildFile($filename, $packageID);
+               }
+               require_once($filename);
+       }
+       
+       /**
+        * Starts the session system.
+        */
+       protected function initSession() {
+               $factory = new SessionFactory();
+               $factory->load();
+               
+               self::$sessionObj = SessionHandler::getInstance();
+       }
+       
+       /**
+        * Initialises the language engine.
+        */
+       protected function initLanguage() {
+               if (isset($_GET['l']) && !self::getUser()->userID) {
+                       self::getSession()->setLanguageID(intval($_GET['l']));
+               }
+               
+               // set mb settings
+               mb_internal_encoding('UTF-8');
+               if (function_exists('mb_regex_encoding')) mb_regex_encoding('UTF-8');
+               mb_language('uni');
+               
+               // get language
+               self::$languageObj = LanguageFactory::getInstance()->getUserLanguage(self::getSession()->getLanguageID());
+               self::$languageObj->setLocale();
+       }
+       
+       /**
+        * Initialises the template engine.
+        */
+       protected function initTPL() {
+               self::$tplObj = TemplateEngine::getInstance();
+               self::getTPL()->setLanguageID(self::getLanguage()->languageID);
+               $this->assignDefaultTemplateVariables();
+       }
+       
+       /**
+        * Executes the blacklist.
+        */
+       protected function initBlacklist() {
+               if (defined('BLACKLIST_IP_ADDRESSES') && BLACKLIST_IP_ADDRESSES != '') {
+                       if (!util\StringUtil::executeWordFilter(WCF::getSession()->ipAddress, BLACKLIST_IP_ADDRESSES)) {
+                               throw new exception\PermissionDeniedException();
+                       }
+               }
+               if (defined('BLACKLIST_USER_AGENTS') && BLACKLIST_USER_AGENTS != '') {
+                       if (!util\StringUtil::executeWordFilter(WCF::getSession()->userAgent, BLACKLIST_USER_AGENTS)) {
+                               throw new exception\PermissionDeniedException();
+                       }
+               }
+               if (defined('BLACKLIST_HOSTNAMES') && BLACKLIST_HOSTNAMES != '') {
+                       if (!util\StringUtil::executeWordFilter(@gethostbyaddr(WCF::getSession()->ipAddress), BLACKLIST_HOSTNAMES)) {
+                               throw new exception\PermissionDeniedException();
+                       }
+               }
+       }
+       
+       /**
+        * Initializes applications.
+        */
+       protected function initApplications() {
+               // register WCF as application
+               self::$applications['wcf'] = new Application(1);
+               
+               // do not init applications if within wcf
+               if (PACKAGE_ID == 1) return;
+               
+               // start main application
+               $application = ApplicationHandler::getInstance()->getActiveApplication();
+               $this->loadApplication($application);
+               
+               // register primary application
+               $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);
+               self::$applications[$abbreviation] = $application;
+               
+               // start dependent applications
+               $applications = ApplicationHandler::getInstance()->getDependentApplications();
+               foreach ($applications as $application) {
+                       $this->loadApplication($application, true);
+               }
+       }
+       
+       /**
+        * Loads an application.
+        *
+        * @param       wcf\data\application\Application                $application
+        * @param       boolean                                         $isDependentApplication
+        */     
+       protected function loadApplication(Application $application, $isDependentApplication = false) {
+               $package = PackageCache::getInstance()->getPackage($application->packageID);
+               
+               $abbreviation = ApplicationHandler::getInstance()->getAbbreviation($application->packageID);
+               $packageDir = util\FileUtil::getRealPath(WCF_DIR.$package->packageDir);
+               self::$autoloadDirectories[$abbreviation] = $packageDir . 'lib/';
+               
+               $className = $abbreviation.'\system\\'.strtoupper($abbreviation).'Core';
+               if (class_exists($className) && util\ClassUtil::isInstanceOf($className, 'wcf\system\application\IApplication')) {
+                       // include config file
+                       $configPath = $packageDir . PackageInstallationDispatcher::CONFIG_FILE;
+                       if (file_exists($configPath)) {
+                               require_once($configPath);
+                       }
+                       else {
+                               throw new exception\SystemException('Unable to load configuration for '.$package->package);
+                       }
+                       
+                       // start application if not within ACP
+                       if (!class_exists('wcf\system\WCFACP', false)) {
+                               new $className();
+                       }
+               }
+               else {
+                       unset(self::$autoloadDirectories[$abbreviation]);
+                       throw new exception\SystemException("Unable to run '".$package->package."', '".$className."' is missing or does not implement 'wcf\system\application\IApplication'.");
+               }
+               
+               // load options
+               $this->loadOptions($packageDir.'options.inc.php', $application->packageID);
+               
+               // register template path in ACP
+               if (class_exists('wcf\system\WCFACP', false)) {
+                       $this->getTPL()->addTemplatePath($application->packageID, $packageDir . 'acp/templates/');
+               }
+               else if (!$isDependentApplication) {
+                       // assign base tag
+                       $this->getTPL()->assign('baseHref', $application->domainName . $application->domainPath);
+               }
+               
+               // register application
+               self::$applications[$abbreviation] = $application;
+       }
+       
+       /**
+        * Initializes core object cache.
+        */
+       protected function initCoreObjects() {
+               // ignore core objects if installing WCF
+               if (PACKAGE_ID == 0) {
+                       return;
+               }
+               
+               self::$coreObjectCache = CacheHandler::getInstance()->get('coreObjects-'.PACKAGE_ID);
+               self::$packageDependencies = \wcf\system\package\PackageDependencyHandler::getInstance()->getDependencies();
+       }
+       
+       /**
+        * Assigns some default variables to the template engine.
+        */
+       protected function assignDefaultTemplateVariables() {
+               self::getTPL()->registerPrefilter(array('event', 'hascontent', 'lang', 'icon'));
+               self::getTPL()->assign(array('__wcf' => $this));
+       }
+       
+       /**
+        * Wrapper for the getter methods of this class.
+        *
+        * @param       string          $name
+        * @return      mixed           value
+        */
+       public function __get($name) {
+               $method = 'get'.ucfirst($name);
+               if (method_exists($this, $method)) {
+                       return $this->$method();
+               }
+               
+               throw new exception\SystemException("method '".$method."' does not exist in class WCF");
+       }
+       
+       /**
+        * Changes the active language.
+        *
+        * @param       integer         $languageID
+        */
+       public static final function setLanguage($languageID) {
+               self::$languageObj = LanguageFactory::getInstance()->getLanguage($languageID);
+               self::getTPL()->setLanguageID(self::getLanguage()->languageID);
+       }
+       
+       /**
+        * Includes the required util or exception classes automatically.
+        *
+        * @param       string          $className
+        * @see         spl_autoload_register()
+        */
+       public static final function autoload($className) {
+               $namespaces = explode('\\', $className);
+               if (count($namespaces) > 1) {
+                       $applicationPrefix = array_shift($namespaces);
+                       if (isset(self::$autoloadDirectories[$applicationPrefix])) {
+                               $classPath = self::$autoloadDirectories[$applicationPrefix] . implode('/', $namespaces) . '.class.php';
+                               if (file_exists($classPath)) {
+                                       require_once($classPath);
+                               }
+                       }
+               }
+       }
+       
+       /**
+        * @see wcf\system\WCF::__callStatic()
+        */
+       public final function __call($name, array $arguments) {
+               // bug fix to avoid php crash, see http://bugs.php.net/bug.php?id=55020
+               if (!method_exists($this, $name)) {
+                       return self::__callStatic($name, $arguments);
+               }
+               
+               return $this->$name($arguments);
+       }
+       
+       /**
+        * Returns dynamically loaded core objects.
+        *
+        * @param       string          $name
+        * @param       array           $arguments
+        */
+       public static final function __callStatic($name, array $arguments) {
+               $className = preg_replace('~^get~', '', $name);
+               
+               if (isset(self::$coreObject[$className])) {
+                       return self::$coreObject[$className];
+               }
+               
+               $objectName = self::getCoreObject($className);
+               if ($objectName === null) {
+                       throw new exception\SystemException("Core object '".$className."' is unknown.");
+               }
+               
+               if (class_exists($objectName)) {
+                       if (!(util\ClassUtil::isInstanceOf($objectName, 'wcf\system\SingletonFactory'))) {
+                               throw new exception\SystemException("class '".$objectName."' does not implement the interface 'SingletonFactory'");
+                       }
+                       
+                       self::$coreObject[$className] = call_user_func(array($objectName, 'getInstance'));
+                       return self::$coreObject[$className];
+               }
+       }
+       
+       /**
+        * Searches for cached core object definition.
+        *
+        * @param       string          $className
+        * @return      string
+        */
+       protected static final function getCoreObject($className) {
+               foreach (self::$packageDependencies as $packageID) {
+                       if (isset(self::$coreObjectCache[$packageID][$className])) {
+                               return self::$coreObjectCache[$packageID][$className];
+                       }
+               }
+               
+               return null;
+       }
+       
+       /**
+        * Returns true if the debug mode is enabled, otherwise false.
+        * 
+        * @return boolean
+        */
+       public static function debugModeIsEnabled() {
+               if (defined('ENABLE_DEBUG_MODE') && ENABLE_DEBUG_MODE) return true;
+               return false;
+       }
+       
+       /**
+        * Returns true if benchmarking is enabled, otherwise false.
+        * 
+        * @return boolean
+        */
+       public static function benchmarkIsEnabled() {
+               // benchmarking is enabled by default
+               if (!defined('ENABLE_BENCHMARK') || ENABLE_BENCHMARK) return true;
+               return false;
+       }
+       
+       /**
+        * Returns domain path for given application.
+        * 
+        * @param       string          $abbreviation
+        * @return      string
+        */
+       public static function getPath($abbreviation = 'wcf') {
+               // workaround during WCFSetup
+               if (!PACKAGE_ID) {
+                       return '../';
+               }
+               
+               if (!isset(self::$applications[$abbreviation])) {
+                       $abbreviation = 'wcf';
+               }
+               
+               return self::$applications[$abbreviation]->domainName . self::$applications[$abbreviation]->domainPath;
+       }
+       
+       /**
+        * Returns a fully qualified anchor for current page.
+        * 
+        * @param       string          $fragment
+        * @return      string
+        */
+       public function getAnchor($fragment) {
+               // resolve path and query components
+               $scriptName = $_SERVER['SCRIPT_NAME'];
+               if (empty($_SERVER['PATH_INFO'])) {
+                       // bug fix if URL omits script name and path
+                       $scriptName = substr($scriptName, 0, strrpos($scriptName, '/'));
+               }
+               
+               $path = str_replace('/index.php', '', str_replace($scriptName, '', $_SERVER['REQUEST_URI']));
+               $baseHref = self::getTPL()->get('baseHref');
+               
+               return $baseHref . 'index.php' . $path . '#' . $fragment;
+       }
+       
+       /**
+        * Initialises the cronjobs.
+        */
+       protected function initCronjobs() {
+               if (PACKAGE_ID) {
+                       self::getTPL()->assign('executeCronjobs', CronjobScheduler::getInstance()->getNextExec() < TIME_NOW);
+               }
+       }
+}