PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/core/Mage/Core/Model/Cookie.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 310 lines | 148 code | 25 blank | 137 comment | 25 complexity | 8d5d42872d53b1f5698ad1b708c0d3ba MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_Core
  23. * @copyright Copyright (c) 2010 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. * Core cookie model
  28. *
  29. * @category Mage
  30. * @package Mage_Core
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Core_Model_Cookie
  34. {
  35. const XML_PATH_COOKIE_DOMAIN = 'web/cookie/cookie_domain';
  36. const XML_PATH_COOKIE_PATH = 'web/cookie/cookie_path';
  37. const XML_PATH_COOKIE_LIFETIME = 'web/cookie/cookie_lifetime';
  38. const XML_PATH_COOKIE_HTTPONLY = 'web/cookie/cookie_httponly';
  39. protected $_lifetime;
  40. /**
  41. * Store object
  42. *
  43. * @var Mage_Core_Model_Store
  44. */
  45. protected $_store;
  46. /**
  47. * Set Store object
  48. *
  49. * @param mixed $store
  50. * @return Mage_Core_Model_Cookie
  51. */
  52. public function setStore($store)
  53. {
  54. $this->_store = Mage::app()->getStore($store);
  55. return $this;
  56. }
  57. /**
  58. * Retrieve Store object
  59. *
  60. * @return Mage_Core_Model_Store
  61. */
  62. public function getStore()
  63. {
  64. if (is_null($this->_store)) {
  65. $this->_store = Mage::app()->getStore();
  66. }
  67. return $this->_store;
  68. }
  69. /**
  70. * Retrieve Request object
  71. *
  72. * @return Mage_Core_Controller_Request_Http
  73. */
  74. protected function _getRequest()
  75. {
  76. return Mage::app()->getRequest();
  77. }
  78. /**
  79. * Retrieve Response object
  80. *
  81. * @return Mage_Core_Controller_Response_Http
  82. */
  83. protected function _getResponse()
  84. {
  85. return Mage::app()->getResponse();
  86. }
  87. /**
  88. * Retrieve Domain for cookie
  89. *
  90. * @return string
  91. */
  92. public function getDomain()
  93. {
  94. $domain = $this->getConfigDomain();
  95. if (empty($domain)) {
  96. $domain = $this->_getRequest()->getHttpHost();
  97. }
  98. return $domain;
  99. }
  100. /**
  101. * Retrieve Config Domain for cookie
  102. *
  103. * @return string
  104. */
  105. public function getConfigDomain()
  106. {
  107. return (string)Mage::getStoreConfig(self::XML_PATH_COOKIE_DOMAIN, $this->getStore());
  108. }
  109. /**
  110. * Retrieve Path for cookie
  111. *
  112. * @return string
  113. */
  114. public function getPath()
  115. {
  116. $path = Mage::getStoreConfig(self::XML_PATH_COOKIE_PATH, $this->getStore());
  117. if (empty($path)) {
  118. $path = $this->_getRequest()->getBasePath();
  119. }
  120. return $path;
  121. }
  122. /**
  123. * Retrieve cookie lifetime
  124. *
  125. * @return int
  126. */
  127. public function getLifetime()
  128. {
  129. if (!is_null($this->_lifetime)) {
  130. $lifetime = $this->_lifetime;
  131. } else {
  132. $lifetime = Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME, $this->getStore());
  133. }
  134. if (!is_numeric($lifetime)) {
  135. $lifetime = 3600;
  136. }
  137. return $lifetime;
  138. }
  139. /**
  140. * Set cookie lifetime
  141. *
  142. * @param int $lifetime
  143. * @return Mage_Core_Model_Cookie
  144. */
  145. public function setLifetime($lifetime)
  146. {
  147. $this->_lifetime = (int)$lifetime;
  148. return $this;
  149. }
  150. /**
  151. * Retrieve use HTTP only flag
  152. *
  153. * @return bool
  154. */
  155. public function getHttponly()
  156. {
  157. $httponly = Mage::getStoreConfig(self::XML_PATH_COOKIE_HTTPONLY, $this->getStore());
  158. if (is_null($httponly)) {
  159. return null;
  160. }
  161. return (bool)$httponly;
  162. }
  163. /**
  164. * Is https secure request
  165. * Use secure on adminhtml only
  166. *
  167. * @return bool
  168. */
  169. public function isSecure()
  170. {
  171. if ($this->getStore()->isAdmin()) {
  172. return $this->_getRequest()->isSecure();
  173. }
  174. return false;
  175. }
  176. /**
  177. * Set cookie
  178. *
  179. * @param string $name The cookie name
  180. * @param string $value The cookie value
  181. * @param int $period Lifetime period
  182. * @param string $path
  183. * @param string $domain
  184. * @param int|bool $secure
  185. * @return Mage_Core_Model_Cookie
  186. */
  187. public function set($name, $value, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)
  188. {
  189. /**
  190. * Check headers sent
  191. */
  192. if (!$this->_getResponse()->canSendHeaders(false)) {
  193. return $this;
  194. }
  195. if ($period === true) {
  196. $period = 3600 * 24 * 365;
  197. } elseif (is_null($period)) {
  198. $period = $this->getLifetime();
  199. }
  200. if ($period == 0) {
  201. $expire = 0;
  202. }
  203. else {
  204. $expire = time() + $period;
  205. }
  206. if (is_null($path)) {
  207. $path = $this->getPath();
  208. }
  209. if (is_null($domain)) {
  210. $domain = $this->getDomain();
  211. }
  212. if (is_null($secure)) {
  213. $secure = $this->isSecure();
  214. }
  215. if (is_null($httponly)) {
  216. $httponly = $this->getHttponly();
  217. }
  218. setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
  219. return $this;
  220. }
  221. /**
  222. * Postpone cookie expiration time if cookie value defined
  223. *
  224. * @param string $name The cookie name
  225. * @param int $period Lifetime period
  226. * @param string $path
  227. * @param string $domain
  228. * @param int|bool $secure
  229. * @return Mage_Core_Model_Cookie
  230. */
  231. public function renew($name, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)
  232. {
  233. if (($period === null) && !$this->getLifetime()) {
  234. return $this;
  235. }
  236. $value = $this->_getRequest()->getCookie($name, false);
  237. if ($value !== false) {
  238. $this->set($name, $value, $period, $path, $domain, $secure, $httponly);
  239. }
  240. return $this;
  241. }
  242. /**
  243. * Retrieve cookie or false if not exists
  244. *
  245. * @param string $neme The cookie name
  246. * @return mixed
  247. */
  248. public function get($name = null)
  249. {
  250. return $this->_getRequest()->getCookie($name, false);
  251. }
  252. /**
  253. * Delete cookie
  254. *
  255. * @param string $name
  256. * @param string $path
  257. * @param string $domain
  258. * @param int|bool $secure
  259. * @param int|bool $httponly
  260. * @return Mage_Core_Model_Cookie
  261. */
  262. public function delete($name, $path = null, $domain = null, $secure = null, $httponly = null)
  263. {
  264. /**
  265. * Check headers sent
  266. */
  267. if (!$this->_getResponse()->canSendHeaders(false)) {
  268. return $this;
  269. }
  270. if (is_null($path)) {
  271. $path = $this->getPath();
  272. }
  273. if (is_null($domain)) {
  274. $domain = $this->getDomain();
  275. }
  276. if (is_null($secure)) {
  277. $secure = $this->isSecure();
  278. }
  279. if (is_null($httponly)) {
  280. $httponly = $this->getHttponly();
  281. }
  282. setcookie($name, null, null, $path, $domain, $secure, $httponly);
  283. return $this;
  284. }
  285. }