getRequest(); // set debug header if (Mage::helper('varnishcache')->isDebug()) { $this->setDebugHeader(); } // check for disable caching vars if ($disableCachingVars = trim(Mage::getStoreConfig(self::XML_PATH_VARNISH_CACHE_DISABLE_CACHING_VARS))) { foreach (explode(',', $disableCachingVars) as $param) { if ($request->getParam(trim($param))) { $this->setNoCacheHeader(); return $this->setNoCacheCookie(); } } } // renew no-cache cookie $this->setNoCacheCookie(true); // disable page caching for POSTs and no_cache parameters if ($request->isPost() || $request->getParam('no_cache') || !in_array(Mage::app()->getResponse()->getHttpResponseCode(), array(200, 301, 404))) { return $this->setNoCacheHeader(); } // disable page caching because of configuration if (Mage::getStoreConfigFlag(self::XML_PATH_VARNISH_CACHE_DISABLE_CACHING)) { return $this->setNoCacheHeader(); } /** * Check for ruleset depending on request path * * see: Mage_Core_Controller_Varien_Action::getFullActionName() */ $fullActionName = $request->getRequestedRouteName().'_'. $request->getRequestedControllerName().'_'. $request->getRequestedActionName(); Mage::app()->getResponse()->setHeader("PW-MageDebug-Route", $fullActionName, true); // check caching blacklist for request routes $disableRoutes = explode("\n", trim(Mage::getStoreConfig(self::XML_PATH_VARNISH_CACHE_DISABLE_ROUTES))); foreach ($disableRoutes as $route) { $route = trim($route); // if route is found at first position we have a hit if (!empty($route) && strpos($fullActionName, $route) === 0) { return $this->setNoCacheHeader(); } } // set TTL header $regexp = null; $value = null; $routesTtl = unserialize(Mage::getStoreConfig(self::XML_PATH_VARNISH_CACHE_ROUTES_TTL)); if (is_array($routesTtl)) { foreach ($routesTtl as $routeTtl) { extract($routeTtl, EXTR_OVERWRITE); $regexp = trim($regexp); if (!empty($regexp) && strpos($fullActionName, $regexp) === 0) { break; } $value = null; } } if (!isset($value)) { $value = Mage::getStoreConfig(self::XML_PATH_VARNISH_CACHE_TTL); } Mage::app()->getResponse()->setHeader("PW-MageDebug-TTL", $value, true); $this->setTtlHeader(intval($value)); return $this; } /** * Check for a NO_CACHE cookie and if found force a TTL=0 for this * page. * * @return void */ public static function sanitizeCacheControlHeader() { $cookie = self::getCookie(); if ($cookie->get(self::NO_CACHE_COOKIE)) { self::setNoCacheHeader(); } } /** * Disable caching of this and all future request for this visitor * * @return Phoenix_VarnishCache_Helper_Cache */ public function setNoCacheCookie($renewOnly = false) { if ($this->getCookie()->get(self::NO_CACHE_COOKIE)) { $this->getCookie()->renew(self::NO_CACHE_COOKIE); } elseif (!$renewOnly) { $this->getCookie()->set(self::NO_CACHE_COOKIE, 1); } return $this; } /** * Disable caching for this request * * @return Phoenix_VarnishCache_Helper_Cache */ public static function setNoCacheHeader() { return self::setTtlHeader(0); } /** * Set debug flag in HTTP header * * @return Phoenix_VarnishCache_Helper_Cache */ public function setDebugHeader() { $el = explode(':', self::DEBUG_HEADER, 2); Mage::app()->getResponse()->setHeader($el[0], $el[1], true); return $this; } /** * Set TTL HTTP header for cache * * For mod_expires it is important to have "Expires" header. However for * Varnish it is easier to deal with "Cache-Control: s-maxage=xx" as it * is relative to its system time and not depending on timezone settings. * * Magento normaly doesn't set any Cache-Control or Expires headers. If they * appear the are set by PHP's setcookie() function. * * @param int Time to life in seconds. Value greater than 0 means "cacheable". * @return void */ public static function setTtlHeader($ttl) { Mage::app()->getResponse()->setHeader("X-VARNISH-TTL", $ttl, true); $maxAge = 's-maxage=' . (($ttl < 0) ? 0 : $ttl); $cacheControlValue = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, '.$maxAge; // retrieve existing "Cache-Control" header $response = Mage::app()->getResponse(); $headers = $response->getHeaders(); foreach ($headers as $key => $header) { if ('Cache-Control' == $header['name'] && !empty($header['value'])) { // replace existing "max-age" value if (strpos($header['value'], 'age=') !== false) { $cacheControlValue = preg_replace('/(s-)?max[-]?age=[0-9]+/', $maxAge, $header['value']); Mage::app()->getResponse()->setHeader("PW-MageDebug-cacheControl1", $cacheControlValue . ' -- ' . $maxAge . ' -- ' . $header['value'], true); } else { $cacheControlValue .= $header['value'].', '.$maxAge; Mage::app()->getResponse()->setHeader("PW-MageDebug-cacheControl2", $cacheControlValue . ' -- ' . $maxAge . ' -- ' . $header['value'], true); } Mage::app()->getResponse()->setHeader("PW-MageDebug-cacheControl3", $header['name'], true); } } Mage::app()->getResponse()->setHeader("PW-MageDebug-cacheControl4", $cacheControlValue, true); // set "Cache-Control" header with "s-maxage" value $response->setHeader('Cache-Control', $cacheControlValue, true); // set "Expires" header in the past to keep mod_expires from applying it's ruleset $response->setHeader('Expires', 'Mon, 31 Mar 2008 10:00:00 GMT', true); // set "Pragma: no-cache" - just in case $response->setHeader('Pragma', 'no-cache', true); } /** * Find all domains for store * * @return string */ public function getStoreDomainList($storeId = 0, $seperator = '|') { $storeIds = array($storeId); // if $store is empty or 0 get all store ids if (empty($storeId)) { $storeIds = Mage::getResourceModel('core/store_collection')->getAllIds(); } $domains = array(); $urlTypes = array( Mage_Core_Model_Store::URL_TYPE_LINK, Mage_Core_Model_Store::URL_TYPE_DIRECT_LINK, Mage_Core_Model_Store::URL_TYPE_WEB, Mage_Core_Model_Store::URL_TYPE_SKIN, Mage_Core_Model_Store::URL_TYPE_JS, Mage_Core_Model_Store::URL_TYPE_MEDIA ); foreach ($storeIds as $storeId) { $store = Mage::getModel('core/store')->load($storeId); foreach ($urlTypes as $urlType) { // get non-secure store domain $domains[] = Zend_Uri::factory($store->getBaseUrl($urlType, false))->getHost(); // get secure store domain $domains[] = Zend_Uri::factory($store->getBaseUrl($urlType, true))->getHost(); } } // get only unique values $domains = array_unique($domains); return implode($seperator, $domains); } }