forked from koala-framework/koala-framework
/
Setup.php
353 lines (324 loc) · 13.5 KB
/
Setup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<?php
//instanceof operator doesn't work for strings correctly, found on php.net
function is_instance_of($sub, $super)
{
$sub = is_object($sub) ? get_class($sub) : (string)$sub;
$sub = strpos($sub, '.') ? substr($sub, 0, strpos($sub, '.')) : $sub;
$super = is_object($super) ? get_class($super) : (string)$super;
$ret = false;
if ($sub === $super) {
$ret = true;
//} else if (is_subclass_of($sub, $super)) { disabled because of https://bugs.php.net/bug.php?id=50360, fixed for php 5.3
} else if (in_array($super, class_parents($sub))) {
$ret = true;
} else if (in_array($super, class_implements($sub))) {
$ret = true;
}
return $ret;
}
class Kwf_Setup
{
public static $configClass;
public static $configSection;
const CACHE_SETUP_VERSION = 5; //increase version if incompatible changes to generated file are made
public static function setUp($configClass = 'Kwf_Config_Web')
{
error_reporting(E_ALL & ~E_STRICT);
define('APP_PATH', getcwd());
Kwf_Setup::$configClass = $configClass;
if (PHP_SAPI == 'cli') {
//don't use cached setup on cli so clear-cache will always work even if eg. paths change
require_once dirname(__FILE__).'/../Kwf/Util/Setup.php';
Kwf_Util_Setup::minimalBootstrap();
eval(substr(Kwf_Util_Setup::generateCode(), 5));
} else if (!@include(APP_PATH.'/cache/setup'.self::CACHE_SETUP_VERSION.'.php')) {
if (!file_exists(APP_PATH.'/cache/setup'.self::CACHE_SETUP_VERSION.'.php')) {
require_once dirname(__FILE__).'/../Kwf/Util/Setup.php';
Kwf_Util_Setup::minimalBootstrapAndGenerateFile();
}
include(APP_PATH.'/cache/setup'.self::CACHE_SETUP_VERSION.'.php');
}
if (!defined('VKWF_PATH') && PHP_SAPI != 'cli' && self::getBaseUrl() === null) {
//if server.baseUrl is not set try to auto detect it and generate config.local.ini accordingly
//this code is not used if server.baseUrl is set to "" in vkwf
if (!isset($_SERVER['PHP_SELF'])) {
echo "Can't detect baseUrl, PHP_SELF is not set\n";
exit(1);
}
$baseUrl = substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['PHP_SELF'], '/'));
if (substr($baseUrl, -16) == '/kwf/maintenance') {
$baseUrl = substr($baseUrl, 0, -16);
}
$cfg = "[production]\n";
$cfg .= "server.domain = \"$_SERVER[HTTP_HOST]\"\n";
$cfg .= "server.baseUrl = \"$baseUrl\"\n";
$cfg .= "setupFinished = false\n";
if (file_exists('config.local.ini') && filesize('config.local.ini')>0) {
echo "config.local.ini already exists but server.baseUrl is not set\n";
exit(1);
}
if (!is_writable('.')) {
echo "'".getcwd()."' is not writable, can't create config.local.ini\n";
exit(1);
}
file_put_contents('config.local.ini', $cfg);
Kwf_Config_Web::reload();
Kwf_Config::deleteValueCache('server.domain');
Kwf_Config::deleteValueCache('server.baseUrl');
unlink('cache/setup'.self::CACHE_SETUP_VERSION.'.php');
echo "<h1>".Kwf_Config::getValue('application.name')."</h1>\n";
echo "<a href=\"$baseUrl/kwf/maintenance/setup\">[start setup]</a>\n";
exit;
}
if (isset($_SERVER['REQUEST_URI']) && substr($_SERVER['REQUEST_URI'], 0, 5) == '/kwf/') {
if (substr($_SERVER['REQUEST_URI'], 0, 9) == '/kwf/pma/' || $_SERVER['REQUEST_URI'] == '/kwf/pma') {
Kwf_Util_Pma::dispatch();
} else if ($_SERVER['REQUEST_URI'] == '/kwf/check') {
$ok = true;
$msg = '';
if (Kwf_Setup::hasDb()) {
$date = Kwf_Registry::get('db')->query("SELECT NOW()")->fetchColumn();
if (!$date) {
$ok = false;
$msg .= 'mysql connection failed';
}
}
if (file_exists('instance_startup')) {
//can be used while starting up autoscaling instances
$ok = false;
$msg .= 'instance startup in progress';
}
if (!$ok) {
header("HTTP/1.0 500 Error");
echo "<h1>Check failed</h1>";
echo $msg;
} else {
echo "ok";
}
exit;
}
}
Kwf_Benchmark::checkpoint('setUp');
}
public static function shutDown()
{
chdir(APP_PATH);
$error = error_get_last();
if ($error !== null) {
ini_set('memory_limit', memory_get_usage()+16*1024*1024); //in case it was an memory limit error make sure we have enough memory for error handling
$ignore = false;
if (preg_match('#^include\(\).*Failed opening \'[^\']*cache/setup\d+.php\' for inclusion#', $error['message'])) {
//ignore error that can happen before creating setup the first time
$ignore = true;
}
if (defined('E_STRICT') && $error["type"] == E_STRICT) {
$ignore = true;
}
if (defined('E_DEPRECATED') && $error["type"] == E_DEPRECATED) {
$ignore = true;
}
if (!$ignore) {
$e = new ErrorException($error["message"], 0, $error["type"], $error["file"], $error["line"]);
Kwf_Debug::handleException($e);
}
}
Kwf_Benchmark::shutDown();
}
public static function createDb()
{
$dao = Zend_Registry::get('dao');
if (!$dao) return false;
return $dao->getDb();
}
public static function hasDb()
{
static $ret;
if (isset($ret)) return $ret;
$config = Kwf_Config::getValueArray('database');
$ret = isset($config['web']) && $config['web']!==false;
return $ret;
}
public static function createDao()
{
if (!self::hasDb()) {
return null;
}
return new Kwf_Dao();
}
public static function getConfigSection()
{
if (is_null(self::$configSection)) {
throw new Kwf_Exception("Config Section not yet set");
}
return self::$configSection;
}
public static function getRequestPath()
{
static $requestPath;
if (isset($requestPath)) return $requestPath;
switch (PHP_SAPI) {
case 'apache2handler':
case 'apache':
$requestPath = $_SERVER['REQUEST_URI'];
$requestPath = strtok($requestPath, '?');
break;
case 'cli':
$requestPath = false;
break;
case 'cli-server':
$requestPath = $_SERVER['SCRIPT_NAME'];
break;
case 'cgi-fcgi':
$requestPath = $_SERVER['SCRIPT_URL'];
break;
default:
throw new Kwf_Exception("unsupported sapi: ".PHP_SAPI);
}
return $requestPath;
}
public static function getBaseUrl()
{
static $ret;
if (isset($ret)) return $ret;
$ret = Kwf_Config::getValue('server.baseUrl');
return $ret;
}
public static function dispatchKwc()
{
$requestPath = self::getRequestPath();
if ($requestPath === false) return;
$fullRequestPath = $requestPath;
$data = null;
$baseUrl = Kwf_Setup::getBaseUrl();
if ($baseUrl) {
if (substr($requestPath, 0, strlen($baseUrl)) != $baseUrl) {
throw new Kwf_Exception_NotFound();
}
$requestPath = substr($requestPath, strlen($baseUrl));
}
$uri = substr($requestPath, 1);
$i = strpos($uri, '/');
if ($i) $uri = substr($uri, 0, $i);
if ($uri == 'robots.txt') {
Kwf_Util_RobotsTxt::output();
}
if ($uri == 'sitemap.xml') {
$data = Kwf_Component_Data_Root::getInstance()->getPageByUrl('http://'.$_SERVER['HTTP_HOST'].Kwf_Setup::getBaseUrl().'/', null);
Kwf_Component_Sitemap::output($data->getDomainComponent());
}
if (!in_array($uri, array('media', 'kwf', 'admin', 'assets', 'vkwf', 'api'))) {
if (!isset($_SERVER['HTTP_HOST'])) {
$requestUrl = 'http://'.Kwf_Config::getValue('server.domain').$fullRequestPath;
} else {
$requestUrl = 'http://'.$_SERVER['HTTP_HOST'].$fullRequestPath;
}
Kwf_Trl::getInstance()->setUseUserLanguage(false);
$acceptLanguage = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null;
$root = Kwf_Component_Data_Root::getInstance();
$exactMatch = true;
$data = $root->getPageByUrl($requestUrl, $acceptLanguage, $exactMatch);
Kwf_Benchmark::checkpoint('getPageByUrl');
if (!$data) {
throw new Kwf_Exception_NotFound();
}
if (!$exactMatch) {
if (rawurldecode($data->url) == $fullRequestPath) {
throw new Kwf_Exception("getPageByUrl reported this isn't an exact match, but the urls are equal. wtf.");
}
$url = $data->url;
if (isset($_SERVER['QUERY_STRING']) && $_SERVER['QUERY_STRING']) $url .= '?' . $_SERVER['QUERY_STRING'];
if (!$url) { // e.g. firstChildPageData without child pages
throw new Kwf_Exception_NotFound();
}
header('Location: '.$url, true, 301);
exit;
}
$root->setCurrentPage($data);
Kwf_User_Autologin::processCookies();
$contentSender = Kwf_Component_Settings::getSetting($data->componentClass, 'contentSender');
$contentSender = new $contentSender($data);
$contentSender->sendContent(true);
Kwf_Benchmark::shutDown();
//TODO: ein flag oder sowas ähnliches stattdessen verwenden
if ($data instanceof Kwc_Abstract_Feed_Component || $data instanceof Kwc_Export_Xml_Component || $data instanceof Kwc_Export_Xml_Trl_Component) {
echo "<!--";
}
Kwf_Benchmark::output();
if ($data instanceof Kwc_Abstract_Feed_Component || $data instanceof Kwc_Export_Xml_Component || $data instanceof Kwc_Export_Xml_Trl_Component) {
echo "-->";
}
exit;
} else if ($requestPath == '/kwf/util/kwc/render') {
Kwf_User_Autologin::processCookies();
Kwf_Util_Component::dispatchRender();
}
}
public static function dispatchMedia()
{
$requestPath = self::getRequestPath();
if ($requestPath === false) return;
$baseUrl = Kwf_Setup::getBaseUrl();
if ($baseUrl) {
if (substr($requestPath, 0, strlen($baseUrl)) != $baseUrl) {
throw new Kwf_Exception_NotFound();
}
$requestPath = substr($requestPath, strlen($baseUrl));
}
$urlParts = explode('/', substr($requestPath, 1));
if (is_array($urlParts) && $urlParts[0] == 'media') {
if (sizeof($urlParts) != 7) {
throw new Kwf_Exception_NotFound();
}
$class = $urlParts[1];
$id = $urlParts[2];
$type = $urlParts[3];
$checksum = urlencode($urlParts[4]);
// time() wäre der 5er, wird aber nur wegen browsercache benötigt
$filename = $urlParts[6];
if ($checksum != Kwf_Media::getChecksum($class, $id, $type, $filename)) {
throw new Kwf_Exception_NotFound();
}
$class = rawurldecode($class);
Kwf_Media_Output::output(Kwf_Media::getOutput($class, $id, $type));
}
}
public static function getHost($includeProtocol = true)
{
if (isset($_SERVER['HTTP_HOST'])) {
$host = $_SERVER['HTTP_HOST'];
} else {
$host = Kwf_Config::getValue('server.domain');
}
if ($includeProtocol) $host = 'http://' . $host;
return $host;
}
/**
* Check if user is logged in (faster than directly calling user model)
*
* Only asks user model (expensive) when there is something stored in the session
*
* @return boolean if user is logged in
*/
public static function hasAuthedUser()
{
static $benchmarkEnabled;
if (!isset($benchmarkEnabled)) $benchmarkEnabled = Kwf_Benchmark::isEnabled();
if ($benchmarkEnabled) $t = microtime(true);
if (!Zend_Session::isStarted() &&
!Zend_Session::sessionExists() &&
!Kwf_Config::getValue('autologin')
) {
if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('hasAuthedUser: no session', microtime(true)-$t);
return false;
}
if (!Kwf_Auth::getInstance()->getStorage()->read()) {
if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('hasAuthedUser: storage empty', microtime(true)-$t);
return false;
}
$m = Kwf_Registry::get('userModel');
if (!$m) return false;
$ret = $m->hasAuthedUser();
if ($benchmarkEnabled) Kwf_Benchmark::subCheckpoint('hasAuthedUser: asked model', microtime(true)-$t);
return $ret;
}
}