1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_PageCache
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Page cache data helper
29: *
30: * @category Mage
31: * @package Mage_PageCache
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_PageCache_Helper_Data extends Mage_Core_Helper_Abstract
35: {
36: /**
37: * Pathes to external cache config options
38: */
39: const XML_PATH_EXTERNAL_CACHE_ENABLED = 'system/external_page_cache/enabled';
40: const XML_PATH_EXTERNAL_CACHE_LIFETIME = 'system/external_page_cache/cookie_lifetime';
41: const XML_PATH_EXTERNAL_CACHE_CONTROL = 'system/external_page_cache/control';
42:
43: /**
44: * Path to external cache controls
45: */
46: const XML_PATH_EXTERNAL_CACHE_CONTROLS = 'global/external_cache/controls';
47:
48: /**
49: * Cookie name for disabling external caching
50: *
51: * @var string
52: */
53: const NO_CACHE_COOKIE = 'external_no_cache';
54:
55: /**
56: * Check whether external cache is enabled
57: *
58: * @return bool
59: */
60: public function isEnabled()
61: {
62: return (bool)Mage::getStoreConfig(self::XML_PATH_EXTERNAL_CACHE_ENABLED);
63: }
64:
65: /**
66: * Return all available external cache controls
67: *
68: * @return array
69: */
70: public function getCacheControls()
71: {
72: $controls = Mage::app()->getConfig()->getNode(self::XML_PATH_EXTERNAL_CACHE_CONTROLS);
73: return $controls->asCanonicalArray();
74: }
75:
76: /**
77: * Initialize proper external cache control model
78: *
79: * @throws Mage_Core_Exception
80: * @return Mage_PageCache_Model_Control_Interface
81: */
82: public function getCacheControlInstance()
83: {
84: $usedControl = Mage::getStoreConfig(self::XML_PATH_EXTERNAL_CACHE_CONTROL);
85: if ($usedControl) {
86: foreach ($this->getCacheControls() as $control => $info) {
87: if ($control == $usedControl && !empty($info['class'])) {
88: return Mage::getSingleton($info['class']);
89: }
90: }
91: }
92: Mage::throwException($this->__('Failed to load external cache control'));
93: }
94:
95: /**
96: * Disable caching on external storage side by setting special cookie
97: *
98: * @return void
99: */
100: public function setNoCacheCookie()
101: {
102: $cookie = Mage::getSingleton('core/cookie');
103: $lifetime = Mage::getStoreConfig(self::XML_PATH_EXTERNAL_CACHE_LIFETIME);
104: $noCache = $cookie->get(self::NO_CACHE_COOKIE);
105:
106: if ($noCache) {
107: $cookie->renew(self::NO_CACHE_COOKIE, $lifetime);
108: } else {
109: $cookie->set(self::NO_CACHE_COOKIE, 1, $lifetime);
110: }
111: }
112:
113: /**
114: * Returns a lifetime of cookie for external cache
115: *
116: * @return string Time in seconds
117: */
118: public function getNoCacheCookieLifetime()
119: {
120: return Mage::getStoreConfig(self::XML_PATH_EXTERNAL_CACHE_LIFETIME);
121: }
122: }
123: