acd7e9aaea1dc236569a4a982d185374b7706667
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / WCFACP.class.php
1 <?php
2
3 namespace wcf\system;
4
5 use Laminas\Diactoros\Uri;
6 use wcf\system\application\ApplicationHandler;
7 use wcf\system\cache\builder\ACPSearchProviderCacheBuilder;
8 use wcf\system\event\EventHandler;
9 use wcf\system\request\RouteHandler;
10 use wcf\system\session\ACPSessionFactory;
11 use wcf\system\session\SessionHandler;
12 use wcf\system\template\ACPTemplateEngine;
13 use wcf\util\FileUtil;
14 use wcf\util\HeaderUtil;
15
16 /**
17 * Extends WCF class with functions for the ACP.
18 *
19 * @author Marcel Werk
20 * @copyright 2001-2019 WoltLab GmbH
21 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
22 */
23 class WCFACP extends WCF
24 {
25 /**
26 * rescue mode
27 */
28 protected static bool $inRescueMode;
29
30 /**
31 * URL to WCF within rescue mode
32 */
33 protected static string $rescueModePageURL;
34
35 /** @noinspection PhpMissingParentConstructorInspection */
36
37 /**
38 * Calls all init functions of the WCF and the WCFACP class.
39 */
40 public function __construct()
41 {
42 // define tmp directory
43 if (!\defined('TMP_DIR')) {
44 \define('TMP_DIR', FileUtil::getTempFolder());
45 }
46
47 // start initialization
48 $this->initDB();
49 $this->loadOptions();
50 $this->initSession();
51 $this->initLanguage();
52 $this->initTPL();
53 $this->initCoreObjects();
54
55 // prevent application loading during setup
56 if (PACKAGE_ID) {
57 $this->initApplications();
58 }
59
60 $this->runBootstrappers();
61
62 $this->initAuth();
63
64 EventHandler::getInstance()->fireAction($this, 'initialized');
65 }
66
67 /**
68 * Returns true if ACP is currently in rescue mode.
69 */
70 public static function inRescueMode(): bool
71 {
72 if (!isset(self::$inRescueMode)) {
73 self::$inRescueMode = false;
74
75 if (\PACKAGE_ID && isset($_SERVER['HTTP_HOST'])) {
76 self::$inRescueMode = true;
77
78 $activeApplication = ApplicationHandler::getInstance()->getApplicationByID(\PACKAGE_ID);
79 if ($activeApplication->domainName === $_SERVER['HTTP_HOST']) {
80 self::$inRescueMode = false;
81 }
82
83 if (!self::$inRescueMode) {
84 if ($activeApplication->domainPath !== RouteHandler::getPath(['acp'])) {
85 self::$inRescueMode = true;
86 }
87 }
88
89 if (self::$inRescueMode) {
90 self::$rescueModePageURL = RouteHandler::getProtocol() . $_SERVER['HTTP_HOST'] . RouteHandler::getPath(['acp']);
91 }
92 }
93 }
94
95 return self::$inRescueMode;
96 }
97
98 /**
99 * Returns URL for rescue mode page.
100 */
101 public static function getRescueModePageURL(): string
102 {
103 if (self::inRescueMode()) {
104 return self::$rescueModePageURL;
105 }
106
107 return '';
108 }
109
110 /**
111 * Does the user authentication.
112 */
113 protected function initAuth(): void
114 {
115 // this is a work-around since neither RequestHandler
116 // nor RouteHandler are populated right now
117 $pathInfo = RouteHandler::getPathInfo();
118
119 if (self::inRescueMode()) {
120 if (!\preg_match('~^/?rescue-mode/~', $pathInfo)) {
121 if (\PACKAGE_ID != 1) {
122 $uri = new Uri(self::$rescueModePageURL);
123 $uri = $uri->withPath(FileUtil::getRealPath($uri->getPath() . 'acp/' . \RELATIVE_WCF_DIR));
124 $pageURL = $uri->__toString();
125 } else {
126 $pageURL = self::$rescueModePageURL;
127 }
128
129 $redirectURI = $pageURL . 'acp/index.php?rescue-mode/';
130
131 HeaderUtil::redirect($redirectURI);
132
133 exit;
134 }
135 }
136 }
137
138 /**
139 * @since 6.0
140 */
141 public static function overrideDebugMode(): void
142 {
143 self::$overrideDebugMode = true;
144 }
145
146 /**
147 * @inheritDoc
148 */
149 protected function initSession(): void
150 {
151 self::$sessionObj = SessionHandler::getInstance();
152
153 $factory = new ACPSessionFactory();
154 $factory->load();
155 }
156
157 /**
158 * @inheritDoc
159 */
160 protected function initTPL(): void
161 {
162 self::$tplObj = ACPTemplateEngine::getInstance();
163 self::getTPL()->setLanguageID(self::getLanguage()->languageID);
164 $this->assignDefaultTemplateVariables();
165 }
166
167 /**
168 * @inheritDoc
169 */
170 protected function assignDefaultTemplateVariables(): void
171 {
172 parent::assignDefaultTemplateVariables();
173
174 // base tag is determined on runtime
175 $host = RouteHandler::getHost();
176 $path = RouteHandler::getPath();
177
178 // available acp search providers
179 $availableAcpSearchProviders = [];
180 foreach (ACPSearchProviderCacheBuilder::getInstance()->getData() as $searchProvider) {
181 $availableAcpSearchProviders[$searchProvider->providerName] = self::getLanguage()->get(
182 'wcf.acp.search.provider.' . $searchProvider->providerName
183 );
184 }
185 \asort($availableAcpSearchProviders);
186
187 self::getTPL()->assign([
188 'baseHref' => $host . $path,
189 'availableAcpSearchProviders' => $availableAcpSearchProviders,
190 ]);
191 }
192 }