/** * Checks whether the response was cached and set the body accordingly. * * @param CakeEvent $event containing the request and response object * @return CakeResponse with cached content if found, null otherwise */ public function beforeDispatch(CakeEvent $event) { if (Configure::read('Cache.check') !== true) { return; } $path = $event->data['request']->here(); if ($path === '/') { $path = 'home'; } $prefix = Configure::read('Cache.viewPrefix'); if ($prefix) { $path = $prefix . '_' . $path; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { $controller = null; $view = new View($controller); $result = $view->renderCache($filename, microtime(true)); if ($result !== false) { $event->stopPropagation(); $event->data['response']->body($result); return $event->data['response']; } } }
/** * Checks whether the response was cached and set the body accordingly. * * @param CakeEvent $event containing the request and response object * @return CakeResponse with cached content if found, null otherwise */ public function beforeDispatch(CakeEvent $event) { if (Configure::read('Cache.check') !== true) { return null; } // CUSTOMIZE 2014/08/11 ryuring // $this->request->here で、URLを取得する際、URL末尾の 「index」の有無に関わらず // 同一ファイルを参照すべきだが、別々のURLを出力してしまう為、 // 正規化された URLを取得するメソッドに変更 // >>> //$path = $event->data['request']->here(); // --- $path = $event->data['request']->normalizedHere(); // <<< if ($path === '/') { $path = 'index'; } $prefix = Configure::read('Cache.viewPrefix'); if ($prefix) { $path = $prefix . '_' . $path; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { $controller = null; $view = new View($controller); $view->response = $event->data['response']; $result = $view->renderCache($filename, microtime(true)); if ($result !== false) { $event->stopPropagation(); $event->data['response']->body($result); return $event->data['response']; } } }
/** * Test renderCache method * * @return void */ public function testRenderCache() { $this->skipIf(!is_writable(CACHE . 'views' . DS), 'CACHE/views dir is not writable, cannot test renderCache.'); $view = 'test_view'; $View = new View($this->PostsController); $path = CACHE . 'views' . DS . 'view_cache_' . $view; $cacheText = '<!--cachetime:' . time() . '-->some cacheText'; $f = fopen($path, 'w+'); fwrite($f, $cacheText); fclose($f); $result = $View->renderCache($path, '+1 second'); $this->assertFalse($result); if (file_exists($path)) { unlink($path); } $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText'; $f = fopen($path, 'w+'); fwrite($f, $cacheText); fclose($f); $result = $View->renderCache($path, '+1 second'); $this->assertRegExp('/^some cacheText/', $result); if (file_exists($path)) { unlink($path); } }
/** * Outputs cached dispatch view cache * * @param string $path Requested URL path with any query string parameters * @return string|boolean False if is not cached or output */ public function cached($path) { if (Configure::read('Cache.check') === true) { if ($path == '/') { $path = 'home'; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { $controller = null; $view = new View($controller); return $view->renderCache($filename, microtime(true)); } } return false; }
/** * testRenderCache method * * @access public * @return void */ function testRenderCache() { $view = 'test_view'; $View = new View($this->PostsController); $path = CACHE . 'views' . DS . 'view_cache_' . $view; $cacheText = '<!--cachetime:' . time() . '-->some cacheText'; $f = fopen($path, 'w+'); fwrite($f, $cacheText); fclose($f); $result = $View->renderCache($path, '+1 second'); $this->assertFalse($result); @unlink($path); $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText'; $f = fopen($path, 'w+'); fwrite($f, $cacheText); fclose($f); ob_start(); $View->renderCache($path, '+1 second'); $result = ob_get_clean(); $this->assertFalse(empty($result)); @unlink($path); }
} } else { if (empty($_GET['url'])) { $url = null; } else { $url = $_GET['url']; } } if (strpos($url, 'ccss/') === 0) { include WWW_ROOT . DS . 'css.php'; die; } Configure::write('debug', DEBUG); require CAKE . 'dispatcher.php'; if (defined('CACHE_CHECK') && CACHE_CHECK === true) { if (empty($uri)) { $uri = setUri(); } $filename = CACHE . 'views' . DS . convertSlash($uri) . '.php'; if (file_exists($filename)) { uses(DS . 'controller' . DS . 'component', DS . 'view' . DS . 'view'); $v = null; $view = new View($v); $view->renderCache($filename, $TIME_START); } elseif (file_exists(CACHE . 'views' . DS . convertSlash($uri) . '_index.php')) { uses(DS . 'controller' . DS . 'component', DS . 'view' . DS . 'view'); $v = null; $view = new View($v); $view->renderCache(CACHE . 'views' . DS . convertSlash($uri) . '_index.php', $TIME_START); } }
/** * testRenderCache method * * @access public * @return void */ function testRenderCache() { $writable = is_writable(CACHE . 'views' . DS); if ($this->skipIf(!$writable, 'CACHE/views dir is not writable, cannot test renderCache. %s')) { return; } $view = 'test_view'; $View = new View($this->PostsController); $path = CACHE . 'views' . DS . 'view_cache_' . $view; $cacheText = '<!--cachetime:' . time() . '-->some cacheText'; $f = fopen($path, 'w+'); fwrite($f, $cacheText); fclose($f); $result = $View->renderCache($path, '+1 second'); $this->assertFalse($result); @unlink($path); $cacheText = '<!--cachetime:' . (time() + 10) . '-->some cacheText'; $f = fopen($path, 'w+'); fwrite($f, $cacheText); fclose($f); ob_start(); $View->renderCache($path, '+1 second'); $result = ob_get_clean(); $expected = 'some cacheText'; $this->assertPattern('/^some cacheText/', $result); @unlink($path); }
/** * Outputs cached dispatch for js, css, img, view cache * * @param string $url Requested URL * @access public */ function cached($url) { if (strpos($url, 'css/') !== false || strpos($url, 'js/') !== false || strpos($url, 'img/') !== false) { if (strpos($url, 'ccss/') === 0) { include WWW_ROOT . DS . Configure::read('Asset.filter.css'); $this->_stop(); } elseif (strpos($url, 'cjs/') === 0) { include WWW_ROOT . DS . Configure::read('Asset.filter.js'); $this->_stop(); } $isAsset = false; $assets = array('js' => 'text/javascript', 'css' => 'text/css', 'gif' => 'image/gif', 'jpg' => 'image/jpeg', 'png' => 'image/png'); $ext = array_pop(explode('.', $url)); foreach ($assets as $type => $contentType) { if ($type === $ext) { if ($type === 'css' || $type === 'js') { $pos = strpos($url, $type . '/'); } else { $pos = strpos($url, 'img/'); } $isAsset = true; break; } } if ($isAsset === true) { $ob = @ini_get("zlib.output_compression") !== true && extension_loaded("zlib") && strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false; if ($ob && Configure::read('Asset.compress')) { ob_start(); ob_start('ob_gzhandler'); } $assetFile = null; $paths = array(); if ($pos > 0) { $plugin = substr($url, 0, $pos - 1); $url = str_replace($plugin . '/', '', $url); $pluginPaths = Configure::read('pluginPaths'); $count = count($pluginPaths); for ($i = 0; $i < $count; $i++) { $paths[] = $pluginPaths[$i] . $plugin . DS . 'vendors' . DS; } } $paths = array_merge($paths, Configure::read('vendorPaths')); foreach ($paths as $path) { if (is_file($path . $url) && file_exists($path . $url)) { $assetFile = $path . $url; break; } } if ($assetFile !== null) { $fileModified = filemtime($assetFile); header("Date: " . date("D, j M Y G:i:s ", $fileModified) . 'GMT'); header('Content-type: ' . $assets[$type]); header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT"); header("Cache-Control: cache"); header("Pragma: cache"); include $assetFile; if (Configure::read('Asset.compress')) { header("Content-length: " . ob_get_length()); ob_end_flush(); } $this->_stop(); } } } if (Configure::read('Cache.check') === true) { $path = $this->here; if ($this->here == '/') { $path = 'home'; } $path = Inflector::slug($path); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { if (!class_exists('View')) { App::import('Core', 'View'); } $controller = null; $view = new View($controller, false); return $view->renderCache($filename, getMicrotime()); } } return false; }
/** * Outputs cached dispatch for js, css, view cache * * @param string $url Requested URL * @access public */ function cached($url) { if (strpos($url, 'ccss/') === 0) { include WWW_ROOT . DS . 'css.php'; exit; } $folders = array('js' => 'text/javascript', 'css' => 'text/css'); $requestPath = explode('/', $url); if (in_array($requestPath[0], array_keys($folders))) { if (file_exists(VENDORS . join(DS, $requestPath))) { $fileModified = filemtime(VENDORS . join(DS, $requestPath)); header("Date: " . date("D, j M Y G:i:s ", $fileModified) . 'GMT'); header('Content-type: ' . $folders[$requestPath[0]]); header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT"); header("Cache-Control: cache"); header("Pragma: cache"); include VENDORS . join(DS, $requestPath); exit; } } if (Configure::read('Cache.check') === true) { $filename = CACHE . 'views' . DS . convertSlash($url) . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . convertSlash($url) . '_index.php'; } if (file_exists($filename)) { uses('controller' . DS . 'component', DS . 'view' . DS . 'view'); $v = null; $view = new View($v); $view->renderCache($filename, getMicrotime()); } } }
/** * Outputs cached dispatch view cache * * @param string $url Requested URL * @access public */ function cached($url) { if (Configure::read('Cache.check') === true) { $path = $this->here; if ($this->here == '/') { $path = 'home'; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { if (!class_exists('View')) { App::import('View', 'View', false); } $controller = null; $view = new View($controller); $return = $view->renderCache($filename, getMicrotime()); if (!$return) { ClassRegistry::removeObject('view'); } return $return; } } return false; }
/** * Outputs cached dispatch for js, css, img, view cache * * @param string $url Requested URL * @access public */ public function cached($url) { if (strpos($url, '..') === false && strpos($url, '.')) { if (strpos($url, 'ccss/') === 0) { include WWW_ROOT . DS . Configure::read('Asset.filter.css'); $this->_stop(); } elseif (strpos($url, 'cjs/') === 0) { include WWW_ROOT . DS . Configure::read('Asset.filter.js'); $this->_stop(); } App::import('View', 'Media', false); $controller = null; $Media = new MediaView($controller); $ext = array_pop(explode('.', $url)); if (isset($Media->mimeType[$ext])) { $pos = 0; $parts = explode('/', $url); if ($parts[0] === 'theme') { $pos = strlen($parts[0] . $parts[1]) + 1; } elseif (count($parts) > 2) { $pos = strlen($parts[0]); } $ob = @ini_get("zlib.output_compression") !== '1' && extension_loaded("zlib") && strpos(env('HTTP_ACCEPT_ENCODING'), 'gzip') !== false; if ($ob && Configure::read('Asset.compress')) { ob_start(); ob_start('ob_gzhandler'); } $assetFile = null; $paths = array(); $matched = false; if ($pos > 0) { $plugin = substr($url, 0, $pos); $url = preg_replace('/^' . preg_quote($plugin, '/') . '\\//i', '', $url); if (strpos($plugin, '/') !== false) { list($plugin, $theme) = explode('/', $plugin); $themePaths = App::path('views'); foreach ($themePaths as $viewPath) { $path = $viewPath . 'themed' . DS . $theme . DS . 'webroot' . DS; if ($plugin === 'theme' && (is_file($path . $url) && file_exists($path . $url))) { $assetFile = $path . $url; $matched = true; break; } } } if ($matched === false) { $paths[] = App::pluginPath($plugin) . 'webroot' . DS; } } if ($matched === false) { foreach ($paths as $path) { if (is_file($path . $url) && file_exists($path . $url)) { $assetFile = $path . $url; break; } } } if ($assetFile !== null) { $fileModified = filemtime($assetFile); header("Date: " . date("D, j M Y G:i:s ", $fileModified) . 'GMT'); header('Content-type: ' . $Media->mimeType[$ext]); header("Expires: " . gmdate("D, j M Y H:i:s", time() + DAY) . " GMT"); header("Cache-Control: cache"); header("Pragma: cache"); if ($ext === 'css' || $ext === 'js') { include $assetFile; } else { readfile($assetFile); } if (Configure::read('Asset.compress')) { ob_end_flush(); } return true; } } } if (Configure::read('Cache.check') === true) { $path = $this->here; if ($this->here == '/') { $path = 'home'; } $path = strtolower(Inflector::slug($path)); $filename = CACHE . 'views' . DS . $path . '.php'; if (!file_exists($filename)) { $filename = CACHE . 'views' . DS . $path . '_index.php'; } if (file_exists($filename)) { if (!class_exists('View')) { App::import('View', 'View', false); } $controller = null; $view = new View($controller, false); return $view->renderCache($filename, microtime(true)); } } return false; }