protected function _clearCache($options) { $url = Kwf_Config::getValue('assetsCacheUrl') . '?web=' . Kwf_Config::getValue('application.id') . '§ion=' . Kwf_Setup::getConfigSection() . '&clear'; try { $out = file_get_contents($url); $this->_output("cleared: assetsServer [" . $out . "]\n"); } catch (Exception $e) { $this->_output("cleared: assetsServer [ERROR] " . $e->getMessage() . "\n"); } }
public function __construct($config = array()) { if (isset($config['prefix'])) { $this->_prefix = $config['prefix']; } else { $this->_prefix = Zend_Registry::get('config')->application->id . '-' . Kwf_Setup::getConfigSection() . '-bench-'; } if (isset($config['memcache'])) { $this->_memcache = $config['memcache']; } }
public static function reload() { $configClass = Kwf_Setup::$configClass; $config = new $configClass(Kwf_Setup::getConfigSection()); $cacheId = 'config_' . str_replace('-', '_', Kwf_Setup::getConfigSection()); Kwf_Config_Cache::getInstance()->save($config, $cacheId); if (extension_loaded('apc')) { $apcCacheId = $cacheId . getcwd(); apc_delete($apcCacheId); apc_delete($apcCacheId . 'mtime'); } Kwf_Config_Web::clearInstances(); Kwf_Registry::set('config', $config); }
public function image($image, $alt = '', $attributes = null) { if (!$image) { return ''; } $url = $this->_getImageUrl($image); if ($url == '') { return ''; } if (Kwf_Config::getValue('assetsCacheUrl') && substr($url, 0, 8) == '/assets/') { $url = Kwf_Config::getValue('assetsCacheUrl') . '?web=' . Kwf_Config::getValue('application.id') . '§ion=' . Kwf_Setup::getConfigSection() . '&url=' . substr($url, 1); } else { if (Kwf_Setup::getBaseUrl() && substr($url, 0, 8) == '/assets/') { $url = Kwf_Setup::getBaseUrl() . $url; } } $class = ''; if (is_string($attributes)) { $class = $attributes; } if (is_string($image)) { if (file_exists(str_replace('/images/', '/images/dpr2/', $this->_getAssetPath($image)))) { $class .= ' kwfReplaceImageDpr2'; } } $class = trim($class); if (!is_array($attributes)) { $attributes = array(); } if ($class != '') { $attributes['class'] = $class; } $size = $this->_getImageSize($image); if (!isset($attributes['width'])) { $attributes['width'] = $size['width']; } if (!isset($attributes['height'])) { $attributes['height'] = $size['height']; } $attr = ''; foreach ($attributes as $k => $i) { $attr .= ' ' . $k . '="' . $i . '"'; } return "<img src=\"{$url}\"{$attr} alt=\"{$alt}\" />"; }
public static function reload() { $configClass = Kwf_Setup::$configClass; $config = new $configClass(Kwf_Setup::getConfigSection()); $cacheId = 'config_' . str_replace(array('-', '.'), '_', Kwf_Setup::getConfigSection()); Kwf_Config_Cache::getInstance()->save($config, $cacheId); if (extension_loaded('apc')) { $apcCacheId = $cacheId . getcwd(); apc_delete($apcCacheId); apc_delete($apcCacheId . 'mtime'); if (PHP_SAPI == 'cli') { Kwf_Util_Apc::callClearCacheByCli(array(array('cacheIds' => $apcCacheId . ',' . $apcCacheId . 'mtime'))); } Kwf_Cache_SimpleStatic::clear('config-'); } Kwf_Config_Web::clearInstances(); Kwf_Registry::set('config', $config); }
public static function getOther() { $ec2 = new Kwf_Util_Aws_Ec2(); $r = $ec2->describe_instances(array('Filter' => array(array('Name' => 'tag:application.id', 'Value' => Kwf_Config::getValue('application.id')), array('Name' => 'tag:config_section', 'Value' => Kwf_Setup::getConfigSection())))); if (!$r->isOK()) { throw new Kwf_Exception($r->body->asXml()); } $ownHostname = file_get_contents('http://169.254.169.254/latest/meta-data/public-hostname'); $domains = array(); foreach ($r->body->reservationSet->item as $resItem) { foreach ($resItem->instancesSet->item as $item) { $dnsName = (string) $item->dnsName; if ($dnsName && $dnsName != $ownHostname) { $domains[] = $dnsName; } } } return array_unique($domains); }
protected function setUp() { if (!($cfg = Kwf_Registry::get('testServerConfig'))) { throw new Kwf_Exception("testServerConfig not set"); } Kwf_Util_Apc::callClearCacheByCli(array('type' => 'user')); Kwf_Cache::factory('Core', 'Memcached', array('lifetime' => null, 'automatic_cleaning_factor' => false, 'automatic_serialization' => true))->clean(); $d = $this->_domain; if (!$d) { $domain = $cfg->server->domain; } else { if (!isset($cfg->kwc->domains->{$d})) { throw new Kwf_Exception("Domain '{$d}' not found in config"); } $domain = $cfg->kwc->domains->{$d}->domain; } $this->setBrowserUrl('http://' . $domain . '/'); $this->_unitTestCookie = md5(uniqid('testId', true)); $this->captureScreenshotOnFailure = Kwf_Setup::getConfigSection() == 'vivid-test-server'; $this->screenshotPath = '/mnt/screenshots'; $this->screenshotUrl = 'http://screenshots.vivid'; parent::setUp(); }
protected function _getCacheIdPrefix() { static $cacheIdPrefix; if (!isset($cacheIdPrefix)) { $cacheIdPrefix = Kwf_Config::getValue('application.id'); if (!$cacheIdPrefix) { throw new Kwf_Exception("application.id has to be set in config"); } $cacheIdPrefix .= Kwf_Setup::getConfigSection(); } return $cacheIdPrefix; }
public static function getUniquePrefix() { static $ret; if (!isset($ret)) { $ret = getcwd() . '-' . Kwf_Setup::getConfigSection() . '-'; } return $ret; }
public static function callUtil($method, $params, $options = array()) { $outputType = ''; if (isset($params['type']) && $params['type'] == 'user') { $outputType = 'apc user'; } else { if (isset($params['type']) && $params['type'] == 'file') { $outputType = 'optcode'; } } $params['password'] = self::_getHttpPassword(); $skipOtherServers = isset($options['skipOtherServers']) ? $options['skipOtherServers'] : false; $config = Kwf_Registry::get('config'); if (!$config->server->aws || $skipOtherServers) { $d = $config->server->domain; if (!$d) { if (isset($options['outputFn'])) { call_user_func($options['outputFn'], "error: {$outputType}: domain not set"); } return false; } $domains = array(array('domain' => $d)); if ($config->server->noRedirectPattern) { $domains[0]['alternative'] = str_replace(array('^', '\\', '$'), '', $config->server->noRedirectPattern); } } else { $ec2 = new Kwf_Util_Aws_Ec2(); $r = $ec2->describe_instances(array('Filter' => array(array('Name' => 'tag:application.id', 'Value' => $config->application->id), array('Name' => 'tag:config_section', 'Value' => Kwf_Setup::getConfigSection())))); if (!$r->isOK()) { throw new Kwf_Exception($r->body->asXml()); } $domains = array(); foreach ($r->body->reservationSet->item as $resItem) { foreach ($resItem->instancesSet->item as $item) { $dnsName = (string) $item->dnsName; if ($dnsName) { $domains[] = array('domain' => $dnsName); } } } } foreach ($domains as $d) { $s = microtime(true); $urlPart = "http://"; $baseUrl = Kwf_Setup::getBaseUrl(); $url = "{$urlPart}{$d['domain']}{$baseUrl}/kwf/util/apc/{$method}"; $client = new Zend_Http_Client(); $client->setMethod(Zend_Http_Client::POST); $client->setParameterPost($params); $client->setConfig(array('timeout' => 60, 'keepalive' => true)); $client->setUri($url); $body = null; $outputMessage = 'could not reach web per http'; try { $response = $client->request(); $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK'; $body = $response->getBody(); $outputMessage = $body; } catch (Exception $e) { $result = false; } $url2 = null; if (!$result && isset($d['alternative'])) { $url2 = "{$urlPart}{$d['alternative']}{$baseUrl}/kwf/util/apc/{$method}"; $client = new Zend_Http_Client(); $client->setMethod(Zend_Http_Client::POST); $client->setConfig(array('timeout' => 60, 'keepalive' => true)); $client->setUri($url2); $client->setParameterPost($params); try { $response = $client->request(); $result = !$response->isError() && substr($response->getBody(), 0, 2) == 'OK'; $body = $response->getBody(); $outputMessage = $body; } catch (Exception $e) { $result = false; } } if (isset($options['outputFn'])) { $outputUrl = $url; if ($url2) { $outputUrl .= " / {$url2}"; } $time = round((microtime(true) - $s) * 1000); if ($result) { call_user_func($options['outputFn'], "{$outputUrl} ({$time}ms) {$outputMessage} "); } else { call_user_func($options['outputFn'], "error: {$outputType} {$outputUrl} {$outputMessage}\n\n"); } } } if (isset($options['returnBody']) && $options['returnBody']) { return $body; } else { return $result; } }
protected static function _getConfigSectionsWithTestDomain() { $webConfigFull = new Zend_Config_Ini('config.ini', null); $sections = array(); $processedDomains = array(); foreach ($webConfigFull as $k => $i) { if ($k == 'dependencies') { continue; } $config = Kwf_Config_Web::getInstance($k); if ($config->server && $config->server->domain) { if (!in_array($config->server->domain, $processedDomains)) { $sections[] = $k; $processedDomains[] = $config->server->domain; } } } $sections = array_reverse($sections); $currentSection = Kwf_Setup::getConfigSection(); $ret = array(); foreach ($sections as $i) { if ($i == $currentSection) { array_unshift($ret, $i); } else { $ret[] = $i; } } return $ret; }
public static function generateCode() { $preloadIncludePaths = array(); $namespaces = (require VENDOR_PATH . '/composer/autoload_namespaces.php'); $ret = array(); foreach ($namespaces as $ns => $dirs) { $preloadIncludePaths = array_merge($preloadIncludePaths, $dirs); } $ret = "<?php\n"; $ret .= "\$cwd = getcwd();\n"; $preloadClasses = array('Kwf_Benchmark', 'Kwf_Loader', 'Kwf_Debug'); $ret .= "if (!class_exists('Kwf_Loader', false)) {\n"; $ret .= self::_generatePreloadClassesCode($preloadClasses, $preloadIncludePaths); $ret .= "}\n"; $ret .= "Kwf_Benchmark::\$startTime = microtime(true);\n"; $ret .= "\n"; //override the default cache (/tmp) dir used by Zend_Cache_Backend to have the cache per web $ret .= "\$_SERVER['TMP'] = 'cache/zend';\n"; //only replace configured value to avoid spoofing //required eg. behind load balancers if (Kwf_Config::getValueArray('server.replaceVars.remoteAddr')) { $a = Kwf_Config::getValueArray('server.replaceVars.remoteAddr'); if (substr($a['if'], -2) == '.*') { $comparison = "substr(\$_SERVER['REMOTE_ADDR'], 0, " . (strlen($a['if']) - 1) . ") == '" . substr($a['if'], 0, -1) . "'"; } else { $comparison = "\$_SERVER['REMOTE_ADDR'] == '{$a['if']}'"; } $ret .= "\nif (isset(\$_SERVER['REMOTE_ADDR']) && {$comparison} && isset(\$_SERVER['{$a['replace']}'])) {\n"; $ret .= " \$_SERVER['REMOTE_ADDR'] = \$_SERVER['{$a['replace']}'];\n"; if (isset($a['removeTrailing'])) { $ret .= " if (substr(\$_SERVER['REMOTE_ADDR'], -" . strlen($a['removeTrailing']) . ") == '" . $a['removeTrailing'] . "') {\n"; $ret .= " \$_SERVER['REMOTE_ADDR'] = substr(\$_SERVER['REMOTE_ADDR'], 0, -" . strlen($a['removeTrailing']) . ");\n"; $ret .= " }\n"; } if (isset($a['removePattern'])) { $ret .= " \$_SERVER['REMOTE_ADDR'] = preg_replace('/" . $a['removePattern'] . "/', '', \$_SERVER['REMOTE_ADDR']);\n"; } $ret .= "}\n"; } //try different values, if one spoofs this this is no security issue $ret .= "if (isset(\$_SERVER['HTTP_SSL_SESSION_ID'])) \$_SERVER['HTTPS'] = 'on';\n"; $ret .= "if (isset(\$_SERVER['HTTP_SESSION_ID_TAG'])) \$_SERVER['HTTPS'] = 'on';\n"; $ret .= "if (isset(\$_SERVER['HTTP_X_FORWARDED_PROTO']) && \$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {\n"; $ret .= " \$_SERVER['HTTPS'] = 'on';\n"; $ret .= "}\n"; if (VENDOR_PATH == '../vendor') { $kwfPath = '..'; } else { $kwfPath = VENDOR_PATH . '/koala-framework/koala-framework'; } $ret .= "if (!defined('KWF_PATH')) define('KWF_PATH', '{$kwfPath}');\n"; $ret .= "if (!defined('VENDOR_PATH')) define('VENDOR_PATH', 'vendor');\n"; $ip = array(); foreach (include VENDOR_PATH . '/composer/include_paths.php' as $p) { if (VENDOR_PATH == '../vendor') { $cwd = getcwd(); $cwd = substr($cwd, 0, strrpos($cwd, '/')); self::_verifyPathInParentPath($p, $cwd); $p = '../' . substr($p, strlen($cwd) + 1); } else { self::_verifyPathInParentPath($p, getcwd()); $p = substr($p, strlen(getcwd()) + 1); } $ip[] = "'.\$cwd.'/" . $p; } $ip[] = '.'; foreach (Kwf_Config::getValueArray('includepath') as $t => $p) { if ($p) { $ip[] = $p; } } $ip = array_unique($ip); $ret .= "Kwf_Loader::setIncludePath('" . implode(PATH_SEPARATOR, $ip) . "');\n"; $ret .= "\n"; $ret .= "\n"; if (Kwf_Exception::isDebug()) { $ret .= "error_reporting(E_ALL | E_STRICT | E_DEPRECATED);\n"; $ret .= "set_error_handler(array('Kwf_Debug', 'handleError'), E_ALL | E_STRICT | E_DEPRECATED);\n"; } else { $ret .= "error_reporting(E_ALL & ~E_STRICT);\n"; $ret .= "set_error_handler(array('Kwf_Debug', 'handleError'), E_ALL & ~E_STRICT);\n"; } $ret .= "set_exception_handler(array('Kwf_Debug', 'handleException'));\n"; $ret .= "\n"; $ret .= "\$requestUri = isset(\$_SERVER['REQUEST_URI']) ? \$_SERVER['REQUEST_URI'] : null;\n"; if (Kwf_Setup::getBaseUrl()) { $ret .= "if (\$requestUri !== null) {\n"; $ret .= " if (substr(\$requestUri, 0, " . strlen(Kwf_Setup::getBaseUrl()) . ") != '" . Kwf_Setup::getBaseUrl() . "') {\n"; $ret .= " echo 'Invalid baseUrl, expected \\'" . Kwf_Setup::getBaseUrl() . "\\'';\n"; $ret .= " exit;\n"; $ret .= " }\n"; $ret .= " \$requestUri = substr(\$requestUri, " . strlen(Kwf_Setup::getBaseUrl()) . ");\n"; $ret .= "}\n"; } $ret .= "\n"; if (Kwf_Config::getValue('debug.benchmark') || Kwf_Config::getValue('debug.benchmarklog')) { //vor registerAutoload aufrufen damit wir dort benchmarken können $ret .= "Kwf_Benchmark::enable();\n"; } else { $ret .= "if (isset(\$_REQUEST['KWF_BENCHMARK'])) {\n"; foreach (Kwf_Config::getValueArray('debug.benchmarkActivatorIp') as $activatorIp) { $ret .= " if (\$_SERVER['REMOTE_ADDR'] == '{$activatorIp}') Kwf_Benchmark::enable();\n"; } $ret .= "}\n"; } $ret .= "Kwf_Loader::registerAutoload();\n"; $ret .= "\n"; $configSection = call_user_func(array(Kwf_Setup::$configClass, 'getDefaultConfigSection')); $ret .= "Kwf_Setup::\$configSection = '" . $configSection . "';\n"; $ret .= "//here to be as fast as possible (and have no session)\n"; $ret .= "if (\$requestUri == '/kwf/json-progress-status'\n"; $ret .= ") {\n"; $ret .= " Kwf_Util_ProgressBar_DispatchStatus::dispatch();\n"; $ret .= "}\n"; $ret .= "\n"; $ret .= "//here to have less dependencies\n"; $ret .= "if (\$requestUri == '/kwf/check-config'\n"; $ret .= ") {\n"; $ret .= " Kwf_Util_Check_Config::dispatch();\n"; $ret .= "}\n"; $ret .= "if (PHP_SAPI == 'cli' && isset(\$_SERVER['argv'][1]) && \$_SERVER['argv'][1] == 'check-config') {\n"; $ret .= " Kwf_Util_Check_Config::dispatch();\n"; $ret .= "}\n"; $ret .= "\$ml = ini_get('memory_limit');\n"; $ret .= "if (strtoupper(substr(\$ml, -1)) == 'M') {\n"; $ret .= " if ((int)substr(\$ml, 0, -1) < 128) {\n"; $ret .= " ini_set('memory_limit', '128M');\n"; $ret .= " }\n"; $ret .= "}\n"; if (Kwf_Config::getValue('debug.error.log')) { $ret .= "ini_set('display_errors', false);\n"; } if (function_exists('mb_internal_encoding')) { $ret .= "mb_internal_encoding('UTF-8');\n"; } $ret .= "ini_set('default_charset', 'UTF-8');\n"; $ret .= "umask(000); //nicht 002 weil wwwrun und kwcms in unterschiedlichen gruppen\n"; //this is *NOT* recommended but still works somehow $ret .= "if (get_magic_quotes_gpc()) Kwf_Util_UndoMagicQuotes::undoMagicQuotes();\n"; if (Kwf_Config::getValue('debug.firephp') || Kwf_Config::getValue('debug.querylog')) { $ret .= "if (PHP_SAPI != 'cli') {\n"; if (Kwf_Config::getValue('debug.firephp')) { $ret .= " require_once '" . Kwf_Config::getValue('externLibraryPath.firephp') . "/FirePHPCore/FirePHP.class.php';\n"; $ret .= " FirePHP::init();\n"; $ret .= " ob_start();\n"; } $ret .= "}\n"; } $ret .= "register_shutdown_function(array('Kwf_Setup', 'shutDown'));\n"; $ret .= "if (!class_exists('Kwf_Config', false)) {\n"; $preloadClasses = array('Kwf_Config', 'Kwf_Cache_Simple', 'Kwf_Cache_SimpleStatic'); $ret .= self::_generatePreloadClassesCode($preloadClasses, $preloadIncludePaths); $ret .= " if (substr(\$requestUri, 0, 8) != '/assets/') {\n"; $preloadClasses = array(); $preloadClasses[] = 'Zend_Registry'; $preloadClasses[] = 'Kwf_Registry'; $preloadClasses[] = 'Kwf_Trl'; $preloadClasses[] = 'Kwf_Util_SessionHandler'; $preloadClasses[] = 'Zend_Session'; $preloadClasses[] = 'Kwf_Benchmark_Counter'; $preloadClasses[] = 'Kwf_Benchmark_Counter_Apc'; if (Kwf_Component_Data_Root::getComponentClass()) { //only load component related classes if it is a component web $preloadClasses[] = 'Kwf_Component_Data'; $preloadClasses[] = 'Kwf_Component_Data_Root'; $preloadClasses[] = 'Kwf_Component_Settings'; $preloadClasses[] = 'Kwf_Component_Renderer_Abstract'; $preloadClasses[] = 'Kwf_Component_Renderer'; $preloadClasses[] = 'Kwf_Component_Cache'; $preloadClasses[] = 'Kwf_Component_Cache_Mysql'; $preloadClasses[] = 'Kwf_Component_Cache_Memory'; $preloadClasses[] = 'Kwf_Component_Abstract_ContentSender_Abstract'; $preloadClasses[] = 'Kwf_Component_Abstract_ContentSender_Default'; } $ret .= self::_generatePreloadClassesCode($preloadClasses, $preloadIncludePaths); $ret .= " } else {\n"; $preloadClasses = array(); $preloadClasses[] = 'Kwf_Assets_Loader'; $preloadClasses[] = 'Kwf_Media_Output'; $ret .= self::_generatePreloadClassesCode($preloadClasses, $preloadIncludePaths); $ret .= " }\n"; $ret .= "}\n"; Kwf_Cache_Simple::$backend = null; //unset to re-calculate $ret .= "Kwf_Cache_Simple::\$backend = '" . Kwf_Cache_Simple::getBackend() . "';\n"; $cacheUniquePrefix = Kwf_Config::getValue('cachePrefix'); //can be '' when only a single web runs in memcache instance if ($cacheUniquePrefix === null) { $cacheUniquePrefix = getcwd() . '-' . Kwf_Setup::getConfigSection() . '-'; } $ret .= "Kwf_Cache_Simple::\$uniquePrefix = '" . $cacheUniquePrefix . "';\n"; if (Kwf_Config::getValue('cacheSimpleNamespace')) { $ret .= "Kwf_Cache_Simple::\$namespace = '" . Kwf_Config::getValue('cacheSimpleNamespace') . "';\n"; } unset($cacheUniquePrefix); if (Kwf_Config::getValue('server.memcache.host')) { $host = Kwf_Config::getValue('server.memcache.host'); $ret .= "Kwf_Cache_Simple::\$memcacheHost = '" . $host . "';\n"; $ret .= "Kwf_Cache_Simple::\$memcachePort = '" . Kwf_Config::getValue('server.memcache.port') . "';\n"; } if (Kwf_Config::getValue('server.redis.host')) { $host = Kwf_Config::getValue('server.redis.host'); $ret .= "Kwf_Cache_Simple::\$redisHost = '" . $host . "';\n"; $ret .= "Kwf_Cache_Simple::\$redisPort = '" . Kwf_Config::getValue('server.redis.port') . "';\n"; } $ret .= "if (substr(\$requestUri, 0, 8) == '/assets/') {\n"; $ret .= " \$url = \$requestUri;\n"; $ret .= " if (strpos(\$url, '?') !== false) {\n"; $ret .= " \$url = substr(\$url, 0, strpos(\$url, '?'));\n"; $ret .= " }\n"; $dispatcherClass = Kwf_Config::getValue('assets.dispatcherClass'); if (!$dispatcherClass) { $dispatcherClass = 'Kwf_Assets_Dispatcher'; } $ret .= " \$dispatcher = new " . $dispatcherClass . "();\n"; $ret .= " \$dispatcher->dispatch(\$url);\n"; $ret .= " Kwf_Assets_Loader::load(\$url);\n"; $ret .= "}\n"; if (Kwf_Config::getValue('debug.benchmarkCounter')) { //vor registerAutoload aufrufen damit wir dort benchmarken können $ret .= "Kwf_Benchmark::enableLog();\n"; } $ret .= "Zend_Registry::setClassName('Kwf_Registry');\n"; $ret .= "\$host = isset(\$_SERVER['HTTP_HOST']) ? \$_SERVER['HTTP_HOST'] : null;\n"; $ret .= "session_name('SESSION_" . Kwf_Config::getValue('application.id') . "');\n"; //up here to have less dependencies or broken redirect $ret .= "\n"; $ret .= "if (substr(\$requestUri, 0, 14) == '/kwf/util/apc/'\n"; $ret .= ") {\n"; $ret .= " Kwf_Util_Apc::dispatchUtils();\n"; $ret .= "}\n"; $ret .= "\n"; $ret .= "if (\$requestUri == '/kwf/check') {\n"; $ret .= " Kwf_Util_Check::dispatch();\n"; $ret .= "}\n"; $ret .= "\n"; $ret .= "session_set_cookie_params(\n"; $ret .= " 0,"; //lifetime $ret .= " '" . Kwf_Setup::getBaseUrl() . "/',"; //path $ret .= " null,"; //domain $ret .= " isset(\$_SERVER['HTTPS']),"; //secure $ret .= " true"; //httponly $ret .= ");\n"; $ret .= "\n"; //store session data in memcache if avaliable if (Kwf_Config::getValue('server.redis.host')) { $ret .= "\nif (PHP_SAPI != 'cli') {\n"; $ret .= " ini_set('session.save_handler', 'redis');\n"; $ret .= " ini_set('session.save_path', 'tcp://" . Kwf_Config::getValue('server.redis.host') . ":" . Kwf_Config::getValue('server.redis.port') . "?prefix=" . substr(md5(Kwf_Cache_Simple::getUniquePrefix()), 0, 10) . "');\n"; $ret .= "}\n"; } else { if (Kwf_Config::getValue('server.memcache.host') && Kwf_Setup::hasDb()) { $ret .= "\nif (PHP_SAPI != 'cli') Kwf_Util_SessionHandler::init();\n"; } } $ret .= "\n\$preLogin = false;\n"; // Falls redirectToDomain eingeschalten ist, umleiten if (Kwf_Config::getValue('server.redirectToDomain')) { $ret .= "if (\$host && substr(\$requestUri, 0, 8) != '/assets/') {\n"; $ret .= " \$redirect = false;\n"; if ($domains = Kwf_Config::getValueArray('kwc.domains')) { $ret .= " \$domainMatches = false;\n"; foreach ($domains as $domain) { $ret .= " if ('{$domain['domain']}' == \$host) \$domainMatches = true;\n"; if (isset($domain['preliminaryDomain'])) { $ret .= " if ('{$domain['preliminaryDomain']}' == \$host) {\n"; $ret .= " \$domainMatches = true;\n"; if (isset($domain['preliminaryDomainPreLogin'])) { //preliminaryDomainPreLogin set for this domain if ($domain['preliminaryDomainPreLogin']) { $ret .= " \$preLogin = true;\n"; } } else { if (Kwf_Config::getValue('server.preliminaryDomainPreLogin')) { //as default use global $ret .= " \$preLogin = true;\n"; } } $ret .= " }\n"; } } $ret .= " if (!\$domainMatches) {\n"; foreach ($domains as $domain) { if (isset($domain['pattern'])) { $ret .= "\n"; $ret .= " //pattern\n"; $ret .= " if (!\$domainMatches && preg_match('/{$domain['pattern']}/', \$host)) {\n"; $ret .= " \$domainMatches = true;\n"; if (isset($domain['noRedirectPattern'])) { $ret .= "\n"; $ret .= " //noRedirectPattern\n"; $ret .= " if (!preg_match('/{$domain['noRedirectPattern']}/', \$host)) {\n"; $ret .= " \$redirect = '{$domain['domain']}';\n"; $ret .= " }\n"; } else { $ret .= " \$redirect = '{$domain['domain']}';\n"; } $ret .= " }\n"; } } $ret .= " }\n"; $ret .= " if (!\$domainMatches) {\n"; $ret .= " \$redirect = '" . Kwf_Config::getValue('server.domain') . "';\n"; $ret .= " }\n"; } else { if (Kwf_Config::getValue('server.domain')) { $ret .= " if (\$host == '" . Kwf_Config::getValue('server.domain') . "') {\n"; $ret .= " //noop\n"; if (Kwf_Config::getValue('server.preliminaryDomain')) { $ret .= " } else if (\$host == '" . Kwf_Config::getValue('server.preliminaryDomain') . "') {\n"; if (Kwf_Config::getValue('server.preliminaryDomainPreLogin')) { $ret .= " \$preLogin = true;\n"; } } $ret .= " } else {\n"; if (Kwf_Config::getValue('server.noRedirectPattern')) { $ret .= " if (!preg_match('/" . Kwf_Config::getValue('server.noRedirectPattern') . "/', \$host)) {\n"; $ret .= " \$redirect = '" . Kwf_Config::getValue('server.domain') . "';\n"; $ret .= " }\n"; } else { $ret .= " \$redirect = '" . Kwf_Config::getValue('server.domain') . "';\n"; } $ret .= " }\n"; } } $ret .= " if (\$redirect) {\n"; $ret .= " \$target = Kwf_Model_Abstract::getInstance('Kwf_Util_Model_Redirects')\n"; $ret .= " ->findRedirectUrl('domainPath', \$host.\$_SERVER['REQUEST_URI']);\n"; $ret .= " if (!\$target) {\n"; $ret .= " \$target = Kwf_Model_Abstract::getInstance('Kwf_Util_Model_Redirects')\n"; $ret .= " ->findRedirectUrl('domain', \$host);\n"; $ret .= " }\n"; $ret .= " if (\$target) {\n"; $ret .= " header('Location: '.\$target, true, 301);\n"; $ret .= " } else {\n"; $ret .= " //redirect to main domain (server.domain)\n"; $ret .= " header('Location: http://'.\$redirect.\$_SERVER['REQUEST_URI'], true, 301);\n"; $ret .= " }\n"; $ret .= " exit;\n"; $ret .= " }\n"; $ret .= "}\n"; } if (Kwf_Config::getValue('server.https') !== 'unknown') { $redirectHttpsCode = " if (\$_SERVER['REQUEST_METHOD'] != 'GET') {\n"; $redirectHttpsCode .= " header('HTTP/1.1 400 Bad Request');\n"; $redirectHttpsCode .= " echo 'Invalid protocol, https required';\n"; $redirectHttpsCode .= " exit;\n"; $redirectHttpsCode .= " }\n"; $redirectHttpsCode .= " \$redirect = 'https://'.\$_SERVER['HTTP_HOST'].\$_SERVER['REQUEST_URI'];\n"; $redirectHttpsCode .= " header('Location: '.\$redirect, true, 301);\n"; $redirectHttpsCode .= " Kwf_Benchmark::shutDown();\n"; $redirectHttpsCode .= " exit;\n"; $redirectHttpCode = str_replace('https', 'http', $redirectHttpsCode); $ret .= "if (PHP_SAPI != 'cli' && isset(\$_SERVER['HTTP_HOST']) && substr(\$requestUri, 0, 7) != '/media/') {\n"; if (!Kwf_Config::getValue('server.https')) { $ret .= "if (isset(\$_SERVER['HTTPS'])) {\n"; $ret .= " {$redirectHttpCode}"; $ret .= "}\n"; } else { if ($domains = Kwf_Config::getValueArray('server.httpsDomains')) { $ret .= "\$domains = array("; foreach ($domains as $d) { if (substr($d, 0, 2) != '*.') { $ret .= "'" . $d . "'=>true, "; } } $ret .= ");\n"; $ret .= "\$supportsHttps = isset(\$domains[\$_SERVER['HTTP_HOST']]);\n"; foreach ($domains as $d) { if (substr($d, 0, 2) == '*.') { $ret .= " if (!\$supportsHttps && '" . substr($d, 1) . "' == substr(\$_SERVER['HTTP_HOST'], strpos(\$_SERVER['HTTP_HOST'], '.'))) {\n"; $ret .= " \$supportsHttps = true;\n"; $ret .= " }\n"; } } $ret .= "if (\$supportsHttps != isset(\$_SERVER['HTTPS'])) {\n"; $ret .= " if (\$supportsHttps) {\n"; $ret .= " {$redirectHttpsCode}"; $ret .= " } else {\n"; $ret .= " {$redirectHttpCode}"; $ret .= " }\n"; $ret .= "}\n"; } else { $ret .= "if (!isset(\$_SERVER['HTTPS'])) {\n"; $ret .= "{$redirectHttpsCode}"; $ret .= "}\n"; } } $ret .= "}\n"; } if (Kwf_Config::getValue('preLogin')) { $ret .= "if (PHP_SAPI != 'cli' && Kwf_Setup::getRequestPath()!==false) {\n"; $ret .= " \$preLogin = true;\n"; $ret .= "}\n"; } $ret .= "if (\$preLogin) {\n"; $ret .= " \$ignore = false;\n"; foreach (Kwf_Config::getValueArray('preLoginIgnore') as $i) { $ret .= " if (substr(\$_SERVER['REDIRECT_URL'], 0, " . strlen($i) . ") == '{$i}') \$ignore = true;\n"; } foreach (Kwf_Config::getValueArray('preLoginIgnoreIp') as $i) { if (substr($i, -1) == '*') { $i = substr($i, 0, -1); $ret .= " if (substr(\$_SERVER['REMOTE_ADDR'], 0, " . strlen($i) . ") == '{$i}') \$ignore = true;\n"; } else { if (substr($i, 0, 1) == '*') { $i = substr($i, 1); $ret .= " if (substr(\$_SERVER['REMOTE_ADDR'], -" . strlen($i) . ") == '{$i}') \$ignore = true;\n"; } else { $ret .= " if (\$_SERVER['REMOTE_ADDR'] == '{$i}') \$ignore = true;\n"; } } } $ret .= " if (!\$ignore) {\n"; $ret .= " Kwf_Setup::checkPreLogin('" . Kwf_Config::getValue('preLoginUser') . "', '" . Kwf_Config::getValue('preLoginPassword') . "');\n"; $ret .= " }\n"; $ret .= "}\n"; if ($parameters = Kwf_Config::getValueArray('parameterToCookie')) { foreach ($parameters as $parameter) { $ret .= "if (isset(\$_GET['" . $parameter . "'])) setcookie('" . $parameter . "', \$_GET['" . $parameter . "'], 0, '/');\n"; } } if ($tl = Kwf_Config::getValue('debug.timeLimit')) { $ret .= "set_time_limit({$tl});\n"; } $ret .= "if (isset(\$_GET['kwcPreview'])) {\n"; $ret .= " \$role = Kwf_Registry::get('userModel')->getAuthedUserRole();\n"; $ret .= " if (!Kwf_Registry::get('acl')->isAllowed(\$role, 'kwf_component_preview', 'view')) {\n"; $ret .= " header('Location: /admin/component/preview/redirect/?url='.urlencode(\$_SERVER['REQUEST_URI']));\n"; $ret .= " exit;\n"; $ret .= " }\n"; $ret .= " Kwf_Component_Data_Root::setShowInvisible(true);\n"; $ret .= "}\n"; return $ret; }