/** * test convertSlash() * * @access public * @return void */ function testConvertSlash() { $result = convertSlash('\\path\\to\\location\\'); $expected = '\\path\\to\\location\\'; $this->assertEqual($result, $expected); $result = convertSlash('/path/to/location/'); $expected = 'path_to_location'; $this->assertEqual($result, $expected); }
/** * Write a cached version of the file * * @param string $content * @param sting $timestamp * @return cached view * @access private */ function __writeFile($content, $timestamp) { $now = time(); if (is_numeric($timestamp)) { $cacheTime = $now + $timestamp; } else { $cacheTime = strtotime($timestamp, $now); } $cache = convertSlash($this->here); if (empty($cache)) { return; } $cache = $cache . '.php'; $file = '<!--cachetime:' . $cacheTime . '--><?php'; if (empty($this->plugin)) { $file .= ' loadController(\'' . $this->view->name . '\'); loadModels(); '; } else { $file .= ' if (!class_exists(\'AppController\')) { if (file_exists(\'' . APP . 'app_controller.php\')) { require(\'' . APP . 'app_controller.php\'); } else { require(\'' . CAKE . 'app_controller.php\'); } } loadPluginController(\'' . $this->plugin . '\',\'' . $this->view->name . '\'); loadPluginModels(\'' . $this->plugin . '\'); '; } $file .= '$this->controller = new ' . $this->view->name . 'Controller(); $this->controller->plugin = \'' . $this->plugin . '\'; $this->controller->_initComponents(); $this->helpers = unserialize(\'' . serialize($this->view->helpers) . '\'); $this->base = \'' . $this->view->base . '\'; $this->layout = \'' . $this->view->layout . '\'; $this->webroot = \'' . $this->view->webroot . '\'; $this->here = \'' . $this->view->here . '\'; $this->params = unserialize(stripslashes(\'' . addslashes(serialize($this->view->params)) . '\')); $this->action = unserialize(\'' . serialize($this->view->action) . '\'); $this->data = unserialize(stripslashes(\'' . addslashes(serialize($this->view->data)) . '\')); $this->themeWeb = \'' . $this->view->themeWeb . '\'; $this->plugin = \'' . $this->view->plugin . '\'; $loadedHelpers = array(); $loadedHelpers = $this->_loadHelpers($loadedHelpers, $this->helpers); foreach(array_keys($loadedHelpers) as $helper) { $replace = strtolower(substr($helper, 0, 1)); $camelBackedHelper = preg_replace(\'/\\w/\', $replace, $helper, 1); ${$camelBackedHelper} =& $loadedHelpers[$helper]; if(isset(${$camelBackedHelper}->helpers) && is_array(${$camelBackedHelper}->helpers)) { foreach(${$camelBackedHelper}->helpers as $subHelper) { ${$camelBackedHelper}->{$subHelper} =& $loadedHelpers[$subHelper]; } } $this->loaded[$camelBackedHelper] = (${$camelBackedHelper}); } ?>' . $content; return cache('views' . DS . $cache, $file, $timestamp); }
} } 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); } }
/** * Wrapper for View::renderElement(); * * @param string $name Name of template file in the/app/views/elements/ folder * @param array $params Array of data to be made available to the for rendered view (i.e. the Element) * @return string View::renderElement() * @access public */ function element($name, $params = array()) { if (isset($params['cache'])) { $expires = '+1 day'; $key = null; if (is_array($params['cache'])) { $expires = $params['cache']['time']; $key = convertSlash($params['cache']['key']); } elseif ($params['cache'] !== true) { $expires = $params['cache']; $key = implode('_', array_keys($params)); } if ($expires) { $plugin = null; if (isset($params['plugin'])) { $plugin = $params['plugin'] . '_'; } $cacheFile = 'element_' . $key . '_' . $plugin . convertSlash($name); $cache = cache('views' . DS . $cacheFile, null, $expires); if (is_string($cache)) { return $cache; } else { $element = $this->renderElement($name, $params); cache('views' . DS . $cacheFile, $element, $expires); return $element; } } } return $this->renderElement($name, $params); }
/** * Function to check if an url is our homepage. * * @return bool */ public function isHome() { $here = convertSlash($this->request->here); $installDir = convertSlash($this->request->base); if ($here == Configure::read('server_id') || $here == $installDir || $here == $installDir . '_' . Configure::read('server_id') || preg_match('/pages_home/', $here)) { return true; } else { 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()); } } }
/** * test convertSlash() * * @return void */ public function testConvertSlash() { $result = convertSlash('\path\to\location\\'); $expected = '\path\to\location\\'; $this->assertEquals($expected, $result); $result = convertSlash('/path/to/location/'); $expected = 'path_to_location'; $this->assertEquals($expected, $result); }
function testElementCache() { $View = new TestView($this->PostsController); $element = 'element_name'; $result = $View->element($element); $this->assertEqual($result, $element); $cached = false; $result = $View->element($element, array('cache' => '+1 second')); if (file_exists(CACHE . 'views' . DS . 'element_cache_' . $element)) { $cached = true; unlink(CACHE . 'views' . DS . 'element_cache_' . $element); } $this->assertTrue($cached); $cached = false; $result = $View->element($element, array('cache' => '+1 second', 'other_param' => true, 'anotherParam' => true)); if (file_exists(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_' . $element)) { $cached = true; unlink(CACHE . 'views' . DS . 'element_cache_other_param_anotherParam_' . $element); } $this->assertTrue($cached); $cached = false; $result = $View->element($element, array('cache' => array('time' => '+1 second', 'key' => '/whatever/here'))); if (file_exists(CACHE . 'views' . DS . 'element_' . convertSlash('/whatever/here') . '_' . $element)) { $cached = true; unlink(CACHE . 'views' . DS . 'element_' . convertSlash('/whatever/here') . '_' . $element); } $this->assertTrue($cached); $cached = false; $result = $View->element($element, array('cache' => array('time' => '+1 second', 'key' => 'whatever_here'))); if (file_exists(CACHE . 'views' . DS . 'element_whatever_here_' . $element)) { $cached = true; unlink(CACHE . 'views' . DS . 'element_whatever_here_' . $element); } $this->assertTrue($cached); $this->assertEqual($result, $element); }