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_Resource_Session implements Zend_Session_SaveHandler_Interface
36: {
37: 38: 39:
40: const SEESION_MAX_COOKIE_LIFETIME = 3155692600;
41:
42: 43: 44: 45: 46:
47: protected $_lifeTime;
48:
49: 50: 51: 52: 53:
54: protected $_sessionTable;
55:
56: 57: 58: 59: 60:
61: protected $_read;
62:
63: 64: 65: 66: 67:
68: protected $_write;
69:
70: 71: 72: 73: 74: 75: 76:
77: protected $_automaticCleaningFactor = 50;
78:
79: 80: 81: 82:
83: public function __construct()
84: {
85: $resource = Mage::getSingleton('core/resource');
86: $this->_sessionTable = $resource->getTableName('core/session');
87: $this->_read = $resource->getConnection('core_read');
88: $this->_write = $resource->getConnection('core_write');
89: }
90:
91: 92: 93: 94:
95: public function __destruct()
96: {
97: session_write_close();
98: }
99:
100: 101: 102: 103: 104:
105: public function getLifeTime()
106: {
107: if (is_null($this->_lifeTime)) {
108: $configNode = Mage::app()->getStore()->isAdmin() ?
109: 'admin/security/session_cookie_lifetime' : 'web/cookie/cookie_lifetime';
110: $this->_lifeTime = (int) Mage::getStoreConfig($configNode);
111:
112: if ($this->_lifeTime < 60) {
113: $this->_lifeTime = ini_get('session.gc_maxlifetime');
114: }
115:
116: if ($this->_lifeTime < 60) {
117: $this->_lifeTime = 3600;
118: }
119:
120: if ($this->_lifeTime > self::SEESION_MAX_COOKIE_LIFETIME) {
121: $this->_lifeTime = self::SEESION_MAX_COOKIE_LIFETIME;
122: }
123: }
124: return $this->_lifeTime;
125: }
126:
127: 128: 129: 130: 131:
132: public function hasConnection()
133: {
134: if (!$this->_read) {
135: return false;
136: }
137: if (!$this->_read->isTableExists($this->_sessionTable)) {
138: return false;
139: }
140:
141: return true;
142: }
143:
144: 145: 146: 147: 148:
149: public function setSaveHandler()
150: {
151: if ($this->hasConnection()) {
152: session_set_save_handler(
153: array($this, 'open'),
154: array($this, 'close'),
155: array($this, 'read'),
156: array($this, 'write'),
157: array($this, 'destroy'),
158: array($this, 'gc')
159: );
160: } else {
161: session_save_path(Mage::getBaseDir('session'));
162: }
163: return $this;
164: }
165:
166: 167: 168: 169: 170: 171: 172:
173: public function open($savePath, $sessName)
174: {
175: return true;
176: }
177:
178: 179: 180: 181: 182:
183: public function close()
184: {
185: $this->gc($this->getLifeTime());
186:
187: return true;
188: }
189:
190: 191: 192: 193: 194: 195:
196: public function read($sessId)
197: {
198: $select = $this->_read->select()
199: ->from($this->_sessionTable, array('session_data'))
200: ->where('session_id = :session_id')
201: ->where('session_expires > :session_expires');
202: $bind = array(
203: 'session_id' => $sessId,
204: 'session_expires' => Varien_Date::toTimestamp(true)
205: );
206:
207: $data = $this->_read->fetchOne($select, $bind);
208:
209: return $data;
210: }
211:
212: 213: 214: 215: 216: 217: 218:
219: public function write($sessId, $sessData)
220: {
221: $bindValues = array(
222: 'session_id' => $sessId
223: );
224: $select = $this->_write->select()
225: ->from($this->_sessionTable)
226: ->where('session_id = :session_id');
227: $exists = $this->_read->fetchOne($select, $bindValues);
228:
229: $bind = array(
230: 'session_expires' => Varien_Date::toTimestamp(true) + $this->getLifeTime(),
231: 'session_data' => $sessData
232: );
233: if ($exists) {
234: $where = array(
235: 'session_id=?' => $sessId
236: );
237: $this->_write->update($this->_sessionTable, $bind, $where);
238: } else {
239: $bind['session_id'] = $sessId;
240: $this->_write->insert($this->_sessionTable, $bind);
241: }
242:
243: return true;
244: }
245:
246: 247: 248: 249: 250: 251:
252: public function destroy($sessId)
253: {
254: $where = array('session_id = ?' => $sessId);
255: $this->_write->delete($this->_sessionTable, $where);
256: return true;
257: }
258:
259: 260: 261: 262: 263: 264:
265: public function gc($sessMaxLifeTime)
266: {
267: if ($this->_automaticCleaningFactor > 0) {
268: if ($this->_automaticCleaningFactor == 1 ||
269: rand(1, $this->_automaticCleaningFactor) == 1) {
270: $where = array('session_expires < ?' => Varien_Date::toTimestamp(true));
271: $this->_write->delete($this->_sessionTable, $where);
272: }
273: }
274: return true;
275: }
276: }
277: