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

/xampp/htdocs/magento/app/code/core/Mage/Core/Model/Cookie.php

https://github.com/edmondscommerce/XAMPP-Magento-Demo-Site
PHP | 279 lines | 134 code | 23 blank | 122 comment | 22 complexity | e6d4a1d6335adcefc322246a21ae1a5b MD5 | raw file
  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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.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 = Mage::getStoreConfig(self::XML_PATH_COOKIE_DOMAIN, $this->getStore());
  95. if (empty($domain)) {
  96. $domain = $this->_getRequest()->getHttpHost();
  97. }
  98. return $domain;
  99. }
  100. /**
  101. * Retrieve Path for cookie
  102. *
  103. * @return string
  104. */
  105. public function getPath()
  106. {
  107. $path = Mage::getStoreConfig(self::XML_PATH_COOKIE_PATH, $this->getStore());
  108. if (empty($path)) {
  109. $path = $this->_getRequest()->getBasePath();
  110. }
  111. return $path;
  112. }
  113. /**
  114. * Retrieve cookie lifetime
  115. *
  116. * @return int
  117. */
  118. public function getLifetime()
  119. {
  120. if (null !== $this->_lifetime) {
  121. $lifetime = $this->_lifetime;
  122. }
  123. else {
  124. $lifetime = Mage::getStoreConfig(self::XML_PATH_COOKIE_LIFETIME, $this->getStore());
  125. }
  126. if (!is_numeric($lifetime)) {
  127. $lifetime = 3600;
  128. }
  129. return $lifetime;
  130. }
  131. /**
  132. * Set cookie lifetime
  133. *
  134. * @param int $lifetime
  135. * @return Mage_Core_Model_Cookie
  136. */
  137. public function setLifetime($lifetime)
  138. {
  139. $this->_lifetime = (int)$lifetime;
  140. return $this;
  141. }
  142. /**
  143. * Retrieve use HTTP only flag
  144. *
  145. * @return bool
  146. */
  147. public function getHttponly()
  148. {
  149. $httponly = Mage::getStoreConfig(self::XML_PATH_COOKIE_HTTPONLY, $this->getStore());
  150. if (is_null($httponly)) {
  151. return null;
  152. }
  153. return (bool)$httponly;
  154. }
  155. /**
  156. * Is https secure request
  157. * Use secure on adminhtml only
  158. *
  159. * @return bool
  160. */
  161. public function isSecure()
  162. {
  163. if ($this->getStore()->isAdmin()) {
  164. return $this->_getRequest()->isSecure();
  165. }
  166. return false;
  167. }
  168. /**
  169. * Set cookie
  170. *
  171. * @param string $name The cookie name
  172. * @param string $value The cookie value
  173. * @param int $period Lifetime period
  174. * @param string $path
  175. * @param string $domain
  176. * @param int|bool $secure
  177. * @return Mage_Core_Model_Cookie
  178. */
  179. public function set($name, $value, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)
  180. {
  181. /**
  182. * Check headers sent
  183. */
  184. if (!$this->_getResponse()->canSendHeaders(false)) {
  185. return $this;
  186. }
  187. if ($period === true) {
  188. $period = 3600 * 24 * 365;
  189. } elseif (is_null($period)) {
  190. $period = $this->getLifetime();
  191. }
  192. if ($period == 0) {
  193. $expire = 0;
  194. }
  195. else {
  196. $expire = time() + $period;
  197. }
  198. if (is_null($path)) {
  199. $path = $this->getPath();
  200. }
  201. if (is_null($domain)) {
  202. $domain = $this->getDomain();
  203. }
  204. if (is_null($secure)) {
  205. $secure = $this->isSecure();
  206. }
  207. if (is_null($httponly)) {
  208. $httponly = $this->getHttponly();
  209. }
  210. setcookie($name, $value, $expire, $path, $domain, $secure, $httponly);
  211. return $this;
  212. }
  213. /**
  214. * Retrieve cookie or false if not exists
  215. *
  216. * @param string $neme The cookie name
  217. * @return mixed
  218. */
  219. public function get($name = null)
  220. {
  221. return $this->_getRequest()->getCookie($name, false);
  222. }
  223. /**
  224. * Delete cookie
  225. *
  226. * @param string $name
  227. * @param string $path
  228. * @param string $domain
  229. * @param int|bool $secure
  230. * @param int|bool $httponly
  231. * @return Mage_Core_Model_Cookie
  232. */
  233. public function delete($name, $path = null, $domain = null, $secure = null, $httponly = null)
  234. {
  235. /**
  236. * Check headers sent
  237. */
  238. if (!$this->_getResponse()->canSendHeaders(false)) {
  239. return $this;
  240. }
  241. if (is_null($path)) {
  242. $path = $this->getPath();
  243. }
  244. if (is_null($domain)) {
  245. $domain = $this->getDomain();
  246. }
  247. if (is_null($secure)) {
  248. $secure = $this->isSecure();
  249. }
  250. if (is_null($httponly)) {
  251. $httponly = $this->getHttponly();
  252. }
  253. setcookie($name, null, null, $path, $domain, $secure, $httponly);
  254. return $this;
  255. }
  256. }