00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 class Mage_Core_Model_Mysql4_Session implements Zend_Session_SaveHandler_Interface
00036 {
00037
00038
00039
00040
00041
00042 protected $_lifeTime;
00043
00044
00045
00046
00047
00048
00049 protected $_sessionTable;
00050
00051
00052
00053
00054
00055
00056 protected $_read;
00057
00058
00059
00060
00061
00062
00063 protected $_write;
00064
00065 public function __construct()
00066 {
00067 $this->_sessionTable = Mage::getSingleton('core/resource')->getTableName('core/session');
00068 $this->_read = Mage::getSingleton('core/resource')->getConnection('core_read');
00069 $this->_write = Mage::getSingleton('core/resource')->getConnection('core_write');
00070 }
00071
00072 public function __destruct()
00073 {
00074 session_write_close();
00075 }
00076
00077 public function getLifeTime()
00078 {
00079 if (is_null($this->_lifeTime)) {
00080 $this->_lifeTime = ini_get('session.gc_maxlifetime');
00081 if (!$this->_lifeTime) {
00082 $this->_lifeTime = 3600;
00083 }
00084 }
00085 return $this->_lifeTime;
00086 }
00087
00088
00089
00090
00091
00092
00093 public function hasConnection()
00094 {
00095 if (!$this->_read) {
00096 return false;
00097 }
00098 $tables = $this->_read->fetchAssoc('show tables');
00099 if (!isset($tables[$this->_sessionTable])) {
00100 return false;
00101 }
00102
00103 return true;
00104 }
00105
00106 public function setSaveHandler()
00107 {
00108 if ($this->hasConnection()) {
00109 session_set_save_handler(
00110 array($this, 'open'),
00111 array($this, 'close'),
00112 array($this, 'read'),
00113 array($this, 'write'),
00114 array($this, 'destroy'),
00115 array($this, 'gc')
00116 );
00117 } else {
00118 session_save_path(Mage::getBaseDir('session'));
00119 }
00120 return $this;
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130 public function open($savePath, $sessName)
00131 {
00132 return true;
00133 }
00134
00135
00136
00137
00138
00139
00140 public function close()
00141 {
00142 $this->gc($this->getLifeTime());
00143
00144 return true;
00145 }
00146
00147
00148
00149
00150
00151
00152
00153 public function read($sessId)
00154 {
00155 $data = $this->_read->fetchOne(
00156 "SELECT session_data FROM $this->_sessionTable
00157 WHERE session_id = ? AND session_expires > ?",
00158 array($sessId, time())
00159 );
00160
00161 return $data;
00162 }
00163
00164
00165
00166
00167
00168
00169
00170
00171 public function write($sessId, $sessData)
00172 {
00173 $bind = array(
00174 'session_expires'=>time() + $this->getLifeTime(),
00175 'session_data'=>$sessData
00176 );
00177
00178 $exists = $this->_write->fetchOne(
00179 "SELECT session_id FROM `{$this->_sessionTable}`
00180 WHERE session_id = ?", array($sessId)
00181 );
00182
00183 if ($exists) {
00184 $where = $this->_write->quoteInto('session_id=?', $sessId);
00185 $this->_write->update($this->_sessionTable, $bind, $where);
00186 } else {
00187 $bind['session_id'] = $sessId;
00188 $this->_write->insert($this->_sessionTable, $bind);
00189 }
00190
00191 return true;
00192 }
00193
00194
00195
00196
00197
00198
00199
00200 public function destroy($sessId)
00201 {
00202 $this->_write->query("DELETE FROM `{$this->_sessionTable}` WHERE `session_id` = ?", array($sessId));
00203 return true;
00204 }
00205
00206
00207
00208
00209
00210
00211
00212 public function gc($sessMaxLifeTime)
00213 {
00214 $this->_write->query("DELETE FROM `{$this->_sessionTable}` WHERE `session_expires` < ?", array(time()));
00215 return true;
00216 }
00217 }