Exemplo n.º 1
0
 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
 {
     switch ($mode) {
         case Zend_Cache::CLEANING_MODE_ALL:
             $ret = true;
             if (PHP_SAPI == 'cli') {
                 if (Kwf_Config::getValue('externalClearCacheScript')) {
                     throw new Kwf_Exception("Dont't delete complete apc-user cache via cli.");
                 }
                 $ret = Kwf_Util_Apc::callClearCacheByCli(array('type' => 'user'));
             }
             if (extension_loaded('apcu')) {
                 return $ret && apc_clear_cache('user');
             } else {
                 return $ret && apc_clear_cache();
             }
             break;
         case Zend_Cache::CLEANING_MODE_OLD:
             $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
             break;
         case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
         case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
         case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
             $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
             break;
         default:
             Zend_Cache::throwException('Invalid mode for clean() method');
             break;
     }
 }
Exemplo n.º 2
0
 public function clean($mode = Zend_Cache::CLEANING_MODE_ALL, $tags = array())
 {
     switch ($mode) {
         case Zend_Cache::CLEANING_MODE_ALL:
             $ret = true;
             if (php_sapi_name() == 'cli') {
                 $ret = Kwf_Util_Apc::callClearCacheByCli(array('type' => 'user'));
             }
             if (extension_loaded('apcu')) {
                 return $ret && apc_clear_cache('user');
             } else {
                 return $ret && apc_clear_cache();
             }
             break;
         case Zend_Cache::CLEANING_MODE_OLD:
             $this->_log("Zend_Cache_Backend_Apc::clean() : CLEANING_MODE_OLD is unsupported by the Apc backend");
             break;
         case Zend_Cache::CLEANING_MODE_MATCHING_TAG:
         case Zend_Cache::CLEANING_MODE_NOT_MATCHING_TAG:
         case Zend_Cache::CLEANING_MODE_MATCHING_ANY_TAG:
             $this->_log(self::TAGS_UNSUPPORTED_BY_CLEAN_OF_APC_BACKEND);
             break;
         default:
             Zend_Cache::throwException('Invalid mode for clean() method');
             break;
     }
 }
Exemplo n.º 3
0
 protected function _clearCache($options)
 {
     $options['outputFn'] = array($this, 'outputFn');
     if (PHP_SAPI == 'cli') {
         Kwf_Util_Apc::callClearCacheByCli(array('type' => 'file'), $options);
     } else {
         if (!extension_loaded('apcu')) {
             apc_clear_cache('file');
         }
     }
 }
Exemplo n.º 4
0
 public function getValue($name)
 {
     if (PHP_SAPI == 'cli') {
         return (int) Kwf_Util_Apc::callUtil('get-counter-value', array('name' => $name), array('returnBody' => true));
     } else {
         static $prefix;
         if (!isset($prefix)) {
             $prefix = Kwf_Cache_Simple::getUniquePrefix() . 'bench-';
         }
         return (int) apc_fetch($prefix . $name);
     }
 }
Exemplo n.º 5
0
 public static function restoreMaintenanceBootstrapSelf($output = true)
 {
     if (!file_exists('bootstrap.php.backup')) {
         throw new Kwf_Exception("maintenance bootstrap not written");
     }
     unlink('bootstrap.php');
     rename('bootstrap.php.backup', 'bootstrap.php');
     if ($output) {
         echo "\nrestored bootstrap.php\n";
     }
     Kwf_Util_Apc::callClearCacheByCli(array('files' => getcwd() . '/bootstrap.php'));
 }
Exemplo n.º 6
0
 public static function isAvailable()
 {
     static $hasApc;
     if (isset($hasApc)) {
         return $hasApc;
     }
     $hasApc = extension_loaded('apc');
     if (!$hasApc && PHP_SAPI == 'cli') {
         //apc might be enabled in webserver only, not in cli
         $hasApc = Kwf_Util_Apc::callUtil('is-loaded', array(), array('returnBody' => true)) == 'OK1';
     }
     return $hasApc;
 }
Exemplo n.º 7
0
 protected function _clearCache($options)
 {
     $options['outputFn'] = array($this, 'outputFn');
     if (php_sapi_name() == 'cli') {
         Kwf_Util_Apc::callClearCacheByCli(array('type' => 'user'), $options);
     } else {
         if (extension_loaded('apcu')) {
             apc_clear_cache();
         } else {
             apc_clear_cache('user');
         }
     }
 }
Exemplo n.º 8
0
 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);
 }
 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();
 }
Exemplo n.º 10
0
 public static function delete($cacheIds)
 {
     if (!is_array($cacheIds)) {
         $cacheIds = array($cacheIds);
     }
     $ret = true;
     $ids = array();
     foreach ($cacheIds as $cacheId) {
         if (self::getBackend() == 'memcache') {
             $r = self::getMemcache()->delete(self::_getMemcachePrefix() . $cacheId);
         } else {
             if (self::getBackend() == 'apc') {
                 static $prefix;
                 if (!isset($prefix)) {
                     $prefix = self::getUniquePrefix() . '-';
                 }
                 $r = apc_delete($prefix . $cacheId);
                 $ids[] = $prefix . $cacheId;
             } else {
                 if (self::getBackend() == 'file') {
                     $r = true;
                     $file = self::_getFileNameForCacheId($cacheId);
                     if (!file_exists($file)) {
                         $r = false;
                     } else {
                         if (!unlink($file)) {
                             $r = false;
                         }
                     }
                 } else {
                     if (!isset(self::$_zendCache)) {
                         self::getZendCache();
                     }
                     $r = self::$_zendCache->remove(self::_processId($cacheId));
                 }
             }
         }
         if (!$r) {
             $ret = false;
         }
     }
     if (self::getBackend() == 'apc' && PHP_SAPI == 'cli' && $ids) {
         $ret = Kwf_Util_Apc::callClearCacheByCli(array('cacheIds' => implode(',', $ids)));
     }
     return $ret;
 }
Exemplo n.º 11
0
 public function getTypes()
 {
     $types = array();
     $simpleCacheBackend = Kwf_Cache_Simple::getBackend();
     if ($simpleCacheBackend == 'memcache' || $simpleCacheBackend == 'elastiCache') {
         $types[] = new Kwf_Util_ClearCache_Types_SimpleCache();
     }
     if (Kwf_Util_Apc::isAvailable()) {
         $types[] = new Kwf_Util_ClearCache_Types_ApcUser();
         $types[] = new Kwf_Util_ClearCache_Types_ApcOptcode();
     }
     foreach ($this->_getCacheDirs() as $d) {
         if ($d != 'config' && $d != 'assets' && $d != 'trl' && $d != 'view') {
             $types[] = new Kwf_Util_ClearCache_Types_Dir($d);
         }
     }
     foreach ($this->_getDbCacheTables() as $t) {
         if ($t == 'cache_component') {
             $types[] = new Kwf_Util_ClearCache_Types_TableComponentView();
         } else {
             if ($t == 'cache_component_includes') {
                 //never completely clear that table as it would break clearing fullPage cache
             } else {
                 if ($t == 'cache_users') {
                     //skip, needed during update
                 } else {
                     $types[] = new Kwf_Util_ClearCache_Types_Table($t);
                 }
             }
         }
     }
     if (Kwf_Config::getValue('assetsCacheUrl')) {
         $types[] = new Kwf_Util_ClearCache_Types_AssetsServer();
     }
     $types[] = new Kwf_Util_ClearCache_Types_Config();
     $types[] = new Kwf_Util_ClearCache_Types_Setup();
     $types[] = new Kwf_Util_ClearCache_Types_Assets();
     if (!Kwf_Config::getValue('clearCacheSkipProcessControl') && VENDOR_PATH != '../vendor') {
         $types[] = new Kwf_Util_ClearCache_Types_ProcessControl();
     }
     return $types;
 }
Exemplo n.º 12
0
 private static function _clearComponentSettingsCache($componentClasses, $setting = null)
 {
     Kwf_Component_Abstract::resetSettingsCache();
     $settings = Kwf_Component_Settings::_getSettingsCached();
     $dependenciesChanged = false;
     $generatorssChanged = false;
     $dimensionsChanged = false;
     $menuConfigChanged = false;
     foreach ($componentClasses as $c) {
         Kwf_Component_Settings::$_rebuildingSettings = true;
         if ($setting) {
             //a single setting changed
             $newSettings = $settings[$c];
             $newSettings[$setting] = Kwc_Abstract::getSetting($c, $setting);
         } else {
             //all settings might have changed
             $newSettings = Kwf_Component_Settings::_getSettingsIncludingPreComputed($c);
         }
         Kwf_Component_Settings::$_rebuildingSettings = false;
         if ($newSettings['assets'] != $settings[$c]['assets'] || $newSettings['assetsAdmin'] != $settings[$c]['assetsAdmin']) {
             $dependenciesChanged = true;
         }
         if ($newSettings['generators'] != $settings[$c]['generators']) {
             $generatorssChanged = true;
             $oldChildComponentClasses = self::_getComponentClassesFromGeneratorsSetting($settings[$c]['generators']);
             $newChildComponentClasses = self::_getComponentClassesFromGeneratorsSetting($newSettings['generators']);
         }
         if (isset($newSettings['dimensions']) && $newSettings['dimensions'] != $settings[$c]['dimensions']) {
             $dimensionsChanged = true;
         }
         if (isset($newSettings['menuConfig']) && $newSettings['menuConfig'] != $settings[$c]['menuConfig']) {
             $menuConfigChanged = true;
         }
         $settings[$c] = $newSettings;
     }
     echo "refreshed component settings cache...\n";
     if ($dependenciesChanged) {
         echo "assets changed...\n";
         self::_clearAssetsDependencies();
         self::_clearAssetsAll();
     }
     $clearCacheSimpleStatic = array('has-', 'cs-', 'flag-', 'componentClasses-', 'recCCGen-', 'genInst-', 'childComponentClasses-');
     if ($generatorssChanged) {
         echo "generators changed...\n";
         echo count(Kwc_Abstract::getComponentClasses()) . " component classes (previously)\n";
         $m = Kwf_Component_Cache::getInstance()->getModel('url');
         foreach ($m->getRows() as $r) {
             Kwf_Cache_Simple::delete('url-' . $r->url);
             $r->delete();
         }
         foreach ($newChildComponentClasses as $cmpClass) {
             if (!in_array($cmpClass, Kwc_Abstract::getComponentClasses())) {
                 self::_loadSettingsRecursive($settings, $cmpClass);
             }
         }
         $removedComponentClasses = array_diff($oldChildComponentClasses, $newChildComponentClasses);
         foreach ($removedComponentClasses as $removedCls) {
             self::_removeSettingsRecursive($settings, $removedCls);
         }
     }
     file_put_contents('build/component/settings', serialize($settings));
     echo "cleared component settings apc cache...\n";
     if (Kwf_Util_Apc::isAvailable()) {
         self::_clearApcCache(array('clearCacheSimpleStatic' => $clearCacheSimpleStatic));
     } else {
         foreach ($clearCacheSimpleStatic as $i) {
             Kwf_Cache_SimpleStatic::clear($i);
         }
     }
     echo "\n";
     if ($dimensionsChanged) {
         echo "dimensions changed...\n";
         $clearCacheSimple = array();
         foreach ($componentClasses as $c) {
             $idPrefix = str_replace(array('.', '>'), array('___', '____'), $c) . '_';
             $clearCacheSimple[] = 'media-output-' . $idPrefix;
             $clearCacheSimple[] = 'media-output-mtime-' . $idPrefix;
             foreach (glob('cache/media/' . $idPrefix . '*') as $f) {
                 echo $f . " [DELETED]\n";
                 unlink($f);
             }
         }
         Kwf_Cache_Simple::delete($clearCacheSimple);
         echo "cleared media cache...\n";
     }
     if ($menuConfigChanged) {
         echo "menu config changed...\n";
         Kwf_Acl::clearCache();
         echo "cleared acl cache...\n";
     }
     $dependentComponentClasses = array();
     foreach (Kwc_Abstract::getComponentClasses() as $c) {
         if (strpos($c, '.') !== false) {
             $params = substr($c, strpos($c, '.') + 1);
             foreach ($componentClasses as $i) {
                 if (strpos($params, $i) !== false) {
                     $dependentComponentClasses[] = $c;
                 }
             }
         }
     }
     if ($dependentComponentClasses) {
         echo "dependent componentClasses: " . count($dependentComponentClasses) . " (Cc, Trl)\n";
         echo implode(', ', $dependentComponentClasses) . "\n";
         self::_clearComponentSettingsCache($dependentComponentClasses, $setting);
     }
 }
Exemplo n.º 13
0
 public static function delete($cacheIds)
 {
     if (!is_array($cacheIds)) {
         $cacheIds = array($cacheIds);
     }
     if (self::getBackend() == 'redis') {
         foreach ($cacheIds as &$id) {
             $id = 'simple:' . self::$namespace . ':' . $id;
         }
         return self::getRedis()->delete($cacheIds);
     }
     $ret = true;
     foreach ($cacheIds as $cacheId) {
         if (self::getBackend() == 'memcache') {
             $r = self::getMemcache()->delete(self::_getMemcachePrefix() . md5($cacheId));
         } else {
             if (self::getBackend() == 'apc') {
                 static $prefix;
                 if (!isset($prefix)) {
                     $prefix = self::$uniquePrefix . '-';
                 }
                 $r = apc_delete($prefix . $cacheId);
             } else {
                 if (self::getBackend() == 'apcu') {
                     static $prefix;
                     if (!isset($prefix)) {
                         $prefix = self::$uniquePrefix . '-';
                     }
                     $r = apcu_delete($prefix . $cacheId);
                 } else {
                     if (self::getBackend() == 'file') {
                         $r = true;
                         $file = self::_getFileNameForCacheId($cacheId);
                         if (!file_exists($file)) {
                             $r = false;
                         } else {
                             if (!unlink($file)) {
                                 $r = false;
                             }
                         }
                     } else {
                         if (!isset(self::$_zendCache)) {
                             self::getZendCache();
                         }
                         $r = self::$_zendCache->remove(self::_processId($cacheId));
                     }
                 }
             }
         }
         if (!$r) {
             $ret = false;
         }
     }
     if ((self::getBackend() == 'apc' || self::getBackend() == 'apcu') && PHP_SAPI == 'cli') {
         $ret = Kwf_Util_Apc::callClearCacheByCli(array('deleteCacheSimple' => implode(',', $cacheIds)));
     }
     return $ret;
 }
 /**
  * Delete static cache, don't use except in unittests
  *
  * @internal
  */
 public static function _delete($cacheIds)
 {
     if (!is_array($cacheIds)) {
         $cacheIds = array($cacheIds);
     }
     $ret = true;
     if (!extension_loaded('apc') || PHP_SAPI == 'cli') {
         foreach ($cacheIds as $cacheId) {
             unset(self::$_cache[$cacheId]);
             $file = self::_getFileNameForCacheId($cacheId);
             if (!file_exists($file)) {
                 $ret = false;
             } else {
                 if (!unlink($file)) {
                     $ret = false;
                 }
             }
         }
         if (extension_loaded('apc')) {
             $result = Kwf_Util_Apc::callClearCacheByCli(array('clearCacheSimpleStatic' => implode(',', $cacheIds)));
             if (!$result['result']) {
                 $ret = false;
             }
         }
     } else {
         $prefix = Kwf_Cache_Simple::$uniquePrefix . '-';
         foreach ($cacheIds as $cacheId) {
             if (!apc_delete($prefix . $cacheId)) {
                 $ret = false;
             }
         }
     }
     return $ret;
 }
Exemplo n.º 15
0
 public function getTypes()
 {
     $types = array();
     $simpleCacheBackend = Kwf_Cache_Simple::getBackend();
     if ($simpleCacheBackend == 'memcache' || $simpleCacheBackend == 'redis') {
         $types[] = new Kwf_Util_ClearCache_Types_SimpleCache();
     }
     if (Kwf_Util_Apc::isAvailable()) {
         $types[] = new Kwf_Util_ClearCache_Types_ApcUser();
         $types[] = new Kwf_Util_ClearCache_Types_ApcOptcode();
     }
     foreach ($this->_getCacheDirs() as $d) {
         if ($d != 'config' && $d != 'assets' && $d != 'trl' && $d != 'view') {
             $types[] = new Kwf_Util_ClearCache_Types_Dir($d);
         }
     }
     if (Kwf_Component_Data_Root::getInstance()) {
         $types[] = new Kwf_Util_ClearCache_Types_ComponentView();
         $types[] = new Kwf_Util_ClearCache_Types_ComponentUrl();
     }
     foreach ($this->_getDbCacheTables() as $t) {
         if ($t == 'cache_component' || $t == 'cache_component_includes' || $t == 'cache_component_url') {
             //never completely clear that table as it would break clearing fullPage cache
         } else {
             if ($t == 'cache_users') {
                 //skip, needed during update
             } else {
                 $types[] = new Kwf_Util_ClearCache_Types_Table($t);
             }
         }
     }
     $types[] = new Kwf_Util_ClearCache_Types_Config();
     $types[] = new Kwf_Util_ClearCache_Types_Setup();
     $types[] = new Kwf_Util_ClearCache_Types_Assets();
     if (file_exists('symfony/bin/console')) {
         $types[] = new Kwf_Util_ClearCache_Types_Symfony();
     }
     if (!Kwf_Config::getValue('clearCacheSkipProcessControl') && VENDOR_PATH != '../vendor') {
         $types[] = new Kwf_Util_ClearCache_Types_ProcessControl();
     }
     $ev = new Kwf_Events_Event_FetchClearCacheTypes(get_class($this));
     Kwf_Events_Dispatcher::fireEvent($ev);
     $types = array_merge($types, $ev->types);
     return $types;
 }
Exemplo n.º 16
0
 /**
  * Delete static cache, don't use except in unittests
  *
  * @internal
  */
 public static function _delete($cacheIds)
 {
     if (!is_array($cacheIds)) {
         $cacheIds = array($cacheIds);
     }
     $ret = true;
     if (!extension_loaded('apc')) {
         foreach ($cacheIds as $cacheId) {
             $file = self::_getFileNameForCacheId($cacheId);
             if (!file_exists($file)) {
                 $ret = false;
             } else {
                 if (!unlink($file)) {
                     $ret = false;
                 }
             }
         }
     } else {
         $prefix = Kwf_Cache_Simple::getUniquePrefix() . '-';
         $ids = array();
         foreach ($cacheIds as $cacheId) {
             if (!apc_delete($prefix . $cacheId)) {
                 $ret = false;
             }
             $ids[] = $prefix . $cacheId;
         }
         if (PHP_SAPI == 'cli' && $ids) {
             $result = Kwf_Util_Apc::callClearCacheByCli(array('cacheIds' => implode(',', $ids)));
             if (!$result['result']) {
                 $ret = false;
             }
         }
     }
     return $ret;
 }
Exemplo n.º 17
0
 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";
         }
         $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";
     $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";
     $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";
     }
     if (PHP_VERSION_ID < 50600) {
         if (function_exists('iconv_set_encoding')) {
             $ret .= "iconv_set_encoding('internal_encoding', 'utf-8');\n";
         }
     } else {
         $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[] = 'Kwf_Util_Memcache';
     $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";
     if (Kwf_Config::getValue('server.memcache.host')) {
         $host = Kwf_Config::getValue('server.memcache.host');
         if ($host == '%webserverHostname%') {
             if (PHP_SAPI == 'cli') {
                 $host = Kwf_Util_Apc::callUtil('get-hostname', array(), array('returnBody' => true, 'skipCache' => true));
             } else {
                 $host = php_uname('n');
             }
         }
         $ret .= "Kwf_Cache_Simple::\$memcacheHost = '" . $host . "';\n";
         $ret .= "Kwf_Cache_Simple::\$memcachePort = '" . Kwf_Config::getValue('server.memcache.port') . "';\n";
     }
     $configSection = call_user_func(array(Kwf_Setup::$configClass, 'getDefaultConfigSection'));
     $ret .= "Kwf_Setup::\$configSection = '" . $configSection . "';\n";
     $ret .= "if (substr(\$requestUri, 0, 8) == '/assets/') {\n";
     $ret .= "    Kwf_Assets_Loader::load(\$requestUri);\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";
     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') {\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) {
                     $ret .= "'" . $d . "'=>true, ";
                 }
                 $ret .= ");\n";
                 $ret .= "\$supportsHttps = isset(\$_SERVER['HTTP_HOST']) && isset(\$domains[\$_SERVER['HTTP_HOST']]);\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";
     }
     $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.memcache.host') || Kwf_Config::getValue('aws.simpleCacheCluster')) && Kwf_Setup::hasDb()) {
         $ret .= "\nif (PHP_SAPI != 'cli') Kwf_Util_SessionHandler::init();\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";
     // Falls redirectToDomain eingeschalten ist, umleiten
     if (Kwf_Config::getValue('server.redirectToDomain')) {
         $ret .= "if (\$host && substr(\$requestUri, 0, 17) != '/kwf/maintenance/' && 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";
             }
             $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";
                 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('preLogin')) {
         $ret .= "if (PHP_SAPI != 'cli' && Kwf_Setup::getRequestPath()!==false) {\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 && (empty(\$_SERVER['PHP_AUTH_USER'])\n";
         $ret .= "           || empty(\$_SERVER['PHP_AUTH_PW'])\n";
         $ret .= "            || \$_SERVER['PHP_AUTH_USER']!='" . Kwf_Config::getValue('preLoginUser') . "'\n";
         $ret .= "           || \$_SERVER['PHP_AUTH_PW']!='" . Kwf_Config::getValue('preLoginPassword') . "')\n";
         $ret .= "    ) {\n";
         $ret .= "        \$realm = 'Testserver';\n";
         $ret .= "        header('WWW-Authenticate: Basic realm=\"'.\$realm.'\"');\n";
         $ret .= "        throw new Kwf_Exception_AccessDenied();\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 (substr(\$requestUri, 0, 9) == '/kwf/pma/' || \$requestUri == '/kwf/pma') {\n";
     $ret .= "    Kwf_Util_Pma::dispatch();\n";
     $ret .= "}\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;
 }
Exemplo n.º 18
0
 protected function _refreshCache($options)
 {
     file_put_contents('cache/setup' . Kwf_Setup::CACHE_SETUP_VERSION . '.php', Kwf_Util_Setup::generateCode(Kwf_Setup::$configClass));
     Kwf_Util_Apc::callClearCacheByCli(array('files' => getcwd() . '/cache/setup' . Kwf_Setup::CACHE_SETUP_VERSION . '.php'));
 }