PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/app/code/core/Mage/Persistent/Model/Session.php

https://github.com/chrisroseuk/magento19_dev
PHP | 246 lines | 108 code | 24 blank | 114 comment | 9 complexity | 5719cf3e2cd9fd78c7d4d48e65b50019 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  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_Persistent
  23. * @copyright Copyright (c) 2014 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. * Persistent Session Model
  28. *
  29. * @category Mage
  30. * @package Mage_Persistent
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Persistent_Model_Session extends Mage_Core_Model_Abstract
  34. {
  35. const KEY_LENGTH = 50;
  36. const COOKIE_NAME = 'persistent_shopping_cart';
  37. /**
  38. * Fields which model does not save into `info` db field
  39. *
  40. * @var array
  41. */
  42. protected $_unserializableFields = array('persistent_id', 'key', 'customer_id', 'website_id', 'info', 'updated_at');
  43. /**
  44. * If model loads expired sessions
  45. *
  46. * @var bool
  47. */
  48. protected $_loadExpired = false;
  49. /**
  50. * Define resource model
  51. */
  52. protected function _construct()
  53. {
  54. $this->_init('persistent/session');
  55. }
  56. /**
  57. * Set if load expired persistent session
  58. *
  59. * @param bool $loadExpired
  60. * @return Mage_Persistent_Model_Session
  61. */
  62. public function setLoadExpired($loadExpired = true)
  63. {
  64. $this->_loadExpired = $loadExpired;
  65. return $this;
  66. }
  67. /**
  68. * Get if model loads expired sessions
  69. *
  70. * @return bool
  71. */
  72. public function getLoadExpired()
  73. {
  74. return $this->_loadExpired;
  75. }
  76. /**
  77. * Get date-time before which persistent session is expired
  78. *
  79. * @param int|string|Mage_Core_Model_Store $store
  80. * @return string
  81. */
  82. public function getExpiredBefore($store = null)
  83. {
  84. return gmdate('Y-m-d H:i:s', time() - Mage::helper('persistent')->getLifeTime($store));
  85. }
  86. /**
  87. * Serialize info for Resource Model to save
  88. * For new model check and set available cookie key
  89. *
  90. * @return Mage_Persistent_Model_Session
  91. */
  92. protected function _beforeSave()
  93. {
  94. parent::_beforeSave();
  95. // Setting info
  96. $info = array();
  97. foreach ($this->getData() as $index => $value) {
  98. if (!in_array($index, $this->_unserializableFields)) {
  99. $info[$index] = $value;
  100. }
  101. }
  102. $this->setInfo(Mage::helper('core')->jsonEncode($info));
  103. if ($this->isObjectNew()) {
  104. $this->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
  105. // Setting cookie key
  106. do {
  107. $this->setKey(Mage::helper('core')->getRandomString(self::KEY_LENGTH));
  108. } while (!$this->getResource()->isKeyAllowed($this->getKey()));
  109. }
  110. return $this;
  111. }
  112. /**
  113. * Set model data from info field
  114. *
  115. * @return Mage_Persistent_Model_Session
  116. */
  117. protected function _afterLoad()
  118. {
  119. parent::_afterLoad();
  120. $info = Mage::helper('core')->jsonDecode($this->getInfo());
  121. if (is_array($info)) {
  122. foreach ($info as $key => $value) {
  123. $this->setData($key, $value);
  124. }
  125. }
  126. return $this;
  127. }
  128. /**
  129. * Get persistent session by cookie key
  130. *
  131. * @param string $key
  132. * @return Mage_Persistent_Model_Session
  133. */
  134. public function loadByCookieKey($key = null)
  135. {
  136. if (is_null($key)) {
  137. $key = Mage::getSingleton('core/cookie')->get(Mage_Persistent_Model_Session::COOKIE_NAME);
  138. }
  139. if ($key) {
  140. $this->load($key, 'key');
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Load session model by specified customer id
  146. *
  147. * @param int $id
  148. * @return Mage_Core_Model_Abstract
  149. */
  150. public function loadByCustomerId($id)
  151. {
  152. return $this->load($id, 'customer_id');
  153. }
  154. /**
  155. * Delete customer persistent session by customer id
  156. *
  157. * @param int $customerId
  158. * @param bool $clearCookie
  159. * @return Mage_Persistent_Model_Session
  160. */
  161. public function deleteByCustomerId($customerId, $clearCookie = true)
  162. {
  163. if ($clearCookie) {
  164. $this->removePersistentCookie();
  165. }
  166. $this->getResource()->deleteByCustomerId($customerId);
  167. return $this;
  168. }
  169. /**
  170. * Remove persistent cookie
  171. *
  172. * @return Mage_Persistent_Model_Session
  173. */
  174. public function removePersistentCookie()
  175. {
  176. Mage::getSingleton('core/cookie')->delete(Mage_Persistent_Model_Session::COOKIE_NAME);
  177. return $this;
  178. }
  179. /**
  180. * Delete expired persistent sessions for the website
  181. *
  182. * @param null|int $websiteId
  183. * @return Mage_Persistent_Model_Session
  184. */
  185. public function deleteExpired($websiteId = null)
  186. {
  187. if (is_null($websiteId)) {
  188. $websiteId = Mage::app()->getStore()->getWebsiteId();
  189. }
  190. $lifetime = Mage::getConfig()->getNode(
  191. Mage_Persistent_Helper_Data::XML_PATH_LIFE_TIME,
  192. 'website',
  193. intval($websiteId)
  194. );
  195. if ($lifetime) {
  196. $this->getResource()->deleteExpired(
  197. $websiteId,
  198. gmdate('Y-m-d H:i:s', time() - $lifetime)
  199. );
  200. }
  201. return $this;
  202. }
  203. /**
  204. * Delete 'persistent' cookie
  205. *
  206. * @return Mage_Core_Model_Abstract
  207. */
  208. protected function _afterDeleteCommit() {
  209. Mage::getSingleton('core/cookie')->delete(Mage_Persistent_Model_Session::COOKIE_NAME);
  210. return parent::_afterDeleteCommit();
  211. }
  212. /**
  213. * Set `updated_at` to be always changed
  214. *
  215. * @return Mage_Persistent_Model_Session
  216. */
  217. public function save()
  218. {
  219. $this->setUpdatedAt(gmdate('Y-m-d H:i:s'));
  220. return parent::save();
  221. }
  222. }