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: class Horde_SessionHandler_Storage_Sql extends Horde_SessionHandler_Storage
31: {
32: 33: 34: 35: 36:
37: protected $_db;
38:
39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50:
51: public function __construct(array $params = array())
52: {
53: if (!isset($params['db'])) {
54: throw new InvalidArgumentException('Missing db parameter.');
55: }
56: $this->_db = $params['db'];
57: unset($params['db']);
58:
59: parent::__construct(array_merge(array(
60: 'table' => 'horde_sessionhandler'
61: ), $params));
62: }
63:
64: 65:
66: public function open($save_path = null, $session_name = null)
67: {
68: }
69:
70: 71:
72: public function close()
73: {
74:
75: if ($this->_db->transactionStarted()) {
76: try {
77: $this->_db->commitDbTransaction();
78: } catch (Horde_Db_Exception $e) {
79: return false;
80: }
81: }
82: return true;
83: }
84:
85: 86:
87: public function read($id)
88: {
89:
90:
91: if (!$this->_db->transactionStarted()) {
92: $this->_db->beginDbTransaction();
93: }
94:
95:
96: $query = sprintf('SELECT session_data FROM %s WHERE session_id = ?',
97: $this->_params['table']);
98: $values = array($id);
99:
100:
101: try {
102: $columns = $this->_db->columns($this->_params['table']);
103: return $columns['session_data']->binaryToString(
104: $this->_db->selectValue($query, $values));
105: } catch (Horde_Db_Exception $e) {
106: return '';
107: }
108: }
109:
110: 111:
112: public function write($id, $session_data)
113: {
114: if (!$this->_db->isActive()) {
115: $this->_db->reconnect();
116: $this->_db->beginDbTransaction();
117: }
118:
119:
120: try {
121: $exists = $this->_db->selectValue(
122: sprintf('SELECT 1 FROM %s WHERE session_id = ?',
123: $this->_params['table']),
124: array($id));
125: } catch (Horde_Db_Exception $e) {
126: return false;
127: }
128:
129:
130: $session_data = new Horde_Db_Value_Binary($session_data);
131: try {
132: if ($exists) {
133: $query = sprintf(
134: 'UPDATE %s '
135: . 'SET session_data = ?, session_lastmodified = ? '
136: . 'WHERE session_id = ?',
137: $this->_params['table']);
138: $values = array(
139: $session_data,
140: time(),
141: $id
142: );
143: $this->_db->update($query, $values);
144: } else {
145: $query = sprintf(
146: 'INSERT INTO %s '
147: . '(session_id, session_data, session_lastmodified) '
148: . 'VALUES (?, ?, ?)',
149: $this->_params['table']);
150: $values = array(
151: $id,
152: $session_data,
153: time()
154: );
155: $this->_db->insert($query, $values);
156: }
157: $this->_db->commitDbTransaction();
158: } catch (Horde_Db_Exception $e) {
159: try {
160: $this->_db->rollbackDbTransaction();
161: } catch (Horde_Db_Exception $e) {
162: }
163: return false;
164: }
165:
166: return true;
167: }
168:
169: 170:
171: public function destroy($id)
172: {
173:
174: $query = sprintf('DELETE FROM %s WHERE session_id = ?',
175: $this->_params['table']);
176: $values = array($id);
177:
178:
179: try {
180: $this->_db->delete($query, $values);
181: $this->_db->commitDbTransaction();
182: } catch (Horde_Db_Exception $e) {
183: return false;
184: }
185:
186: return true;
187: }
188:
189: 190:
191: public function gc($maxlifetime = 300)
192: {
193:
194: $query = sprintf('DELETE FROM %s WHERE session_lastmodified < ?',
195: $this->_params['table']);
196: $values = array(time() - $maxlifetime);
197:
198:
199: try {
200: $this->_db->delete($query, $values);
201: } catch (Horde_Db_Exception $e) {
202: return false;
203: }
204:
205: return true;
206: }
207:
208: 209:
210: public function getSessionIDs()
211: {
212: $this->open();
213:
214:
215: $query = sprintf('SELECT session_id FROM %s' .
216: ' WHERE session_lastmodified >= ?',
217: $this->_params['table']);
218: $values = array(time() - ini_get('session.gc_maxlifetime'));
219:
220:
221: try {
222: return $this->_db->selectValues($query, $values);
223: } catch (Horde_Db_Exception $e) {
224: return array();
225: }
226: }
227: }
228: