1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27:
28: 29: 30: 31: 32: 33: 34:
35: class Mage_Core_Model_Cookie
36: {
37: const XML_PATH_COOKIE_DOMAIN = 'web/cookie/cookie_domain';
38: const XML_PATH_COOKIE_PATH = 'web/cookie/cookie_path';
39: const XML_PATH_COOKIE_LIFETIME = 'web/cookie/cookie_lifetime';
40: const XML_PATH_COOKIE_HTTPONLY = 'web/cookie/cookie_httponly';
41:
42: protected $_lifetime;
43:
44: 45: 46: 47: 48:
49: protected $_store;
50:
51: 52: 53: 54: 55: 56:
57: public function setStore($store)
58: {
59: $this->_store = Mage::app()->getStore($store);
60: return $this;
61: }
62:
63: 64: 65: 66: 67:
68: public function getStore()
69: {
70: if (is_null($this->_store)) {
71: $this->_store = Mage::app()->getStore();
72: }
73: return $this->_store;
74: }
75:
76: 77: 78: 79: 80:
81: protected function _getRequest()
82: {
83: return Mage::app()->getRequest();
84: }
85:
86: 87: 88: 89: 90:
91: protected function _getResponse()
92: {
93: return Mage::app()->getResponse();
94: }
95:
96: 97: 98: 99: 100:
101: public function getDomain()
102: {
103: $domain = $this->getConfigDomain();
104: if (empty($domain)) {
105: $domain = $this->_getRequest()->getHttpHost();
106: }
107: return $domain;
108: }
109:
110: 111: 112: 113: 114:
115: public function getConfigDomain()
116: {
117: return (string)Mage::getStoreConfig(self::XML_PATH_COOKIE_DOMAIN, $this->getStore());
118: }
119:
120: 121: 122: 123: 124:
125: public function getPath()
126: {
127: $path = Mage::getStoreConfig(self::XML_PATH_COOKIE_PATH, $this->getStore());
128: if (empty($path)) {
129: $path = $this->_getRequest()->getBasePath();
130: }
131: return $path;
132: }
133:
134: 135: 136: 137: 138:
139: public function getLifetime()
140: {
141: if (!is_null($this->_lifetime)) {
142: $lifetime = $this->_lifetime;
143: } else {
144: $lifetime = Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME, $this->getStore());
145: }
146: if (!is_numeric($lifetime)) {
147: $lifetime = 3600;
148: }
149: return $lifetime;
150: }
151:
152: 153: 154: 155: 156: 157:
158: public function setLifetime($lifetime)
159: {
160: $this->_lifetime = (int)$lifetime;
161: return $this;
162: }
163:
164: 165: 166: 167: 168:
169: public function getHttponly()
170: {
171: $httponly = Mage::getStoreConfig(self::XML_PATH_COOKIE_HTTPONLY, $this->getStore());
172: if (is_null($httponly)) {
173: return null;
174: }
175: return (bool)$httponly;
176: }
177:
178: 179: 180: 181: 182: 183:
184: public function isSecure()
185: {
186: if ($this->getStore()->isAdmin()) {
187: return $this->_getRequest()->isSecure();
188: }
189: return false;
190: }
191:
192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203:
204: public function set($name, $value, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)
205: {
206: 207: 208:
209: if (!$this->_getResponse()->canSendHeaders(false)) {
210: return $this;
211: }
212:
213: if ($period === true) {
214: $period = 3600 * 24 * 365;
215: } elseif (is_null($period)) {
216: $period = $this->getLifetime();
217: }
218:
219: if ($period == 0) {
220: $expire = 0;
221: }
222: else {
223: $expire = time() + $period;
224: }
225: if (is_null($path)) {
226: $path = $this->getPath();
227: }
228: if (is_null($domain)) {
229: $domain = $this->getDomain();
230: }
231: if (is_null($secure)) {
232: $secure = $this->isSecure();
233: }
234: if (is_null($httponly)) {
235: $httponly = $this->getHttponly();
236: }
237:
238: setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
239:
240: return $this;
241: }
242:
243: 244: 245: 246: 247: 248: 249: 250: 251: 252:
253: public function renew($name, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)
254: {
255: if (($period === null) && !$this->getLifetime()) {
256: return $this;
257: }
258: $value = $this->_getRequest()->getCookie($name, false);
259: if ($value !== false) {
260: $this->set($name, $value, $period, $path, $domain, $secure, $httponly);
261: }
262: return $this;
263: }
264:
265: 266: 267: 268: 269: 270:
271: public function get($name = null)
272: {
273: return $this->_getRequest()->getCookie($name, false);
274: }
275:
276: 277: 278: 279: 280: 281: 282: 283: 284: 285:
286: public function delete($name, $path = null, $domain = null, $secure = null, $httponly = null)
287: {
288: 289: 290:
291: if (!$this->_getResponse()->canSendHeaders(false)) {
292: return $this;
293: }
294:
295: if (is_null($path)) {
296: $path = $this->getPath();
297: }
298: if (is_null($domain)) {
299: $domain = $this->getDomain();
300: }
301: if (is_null($secure)) {
302: $secure = $this->isSecure();
303: }
304: if (is_null($httponly)) {
305: $httponly = $this->getHttponly();
306: }
307:
308: setcookie($name, null, null, $path, $domain, $secure, $httponly);
309: return $this;
310: }
311: }
312: