1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Xoops\Core\Session;
13:
14: use Xoops\Core\Database\Connection;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Handler implements \SessionHandlerInterface
29: {
30: 31: 32:
33: private $db;
34:
35: 36: 37:
38: private $sessionTable = 'system_session';
39:
40: 41: 42:
43: public function __construct()
44: {
45: $this->db = \Xoops::getInstance()->db();
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55:
56: public function open($save_path, $name)
57: {
58: return true;
59: }
60:
61: 62: 63: 64: 65:
66: public function close()
67: {
68: return true;
69: }
70:
71: 72: 73: 74: 75: 76: 77:
78: public function read($session_id)
79: {
80: $qb = $this->db->createXoopsQueryBuilder();
81: $eb = $qb->expr();
82: $qb ->select('s.session_data')
83: ->fromPrefix($this->sessionTable, 's')
84: ->where($eb->eq('s.session_id', ':sessid'))
85: ->andWhere($eb->gt('s.expires_at', ':expires'))
86: ->setParameter(':sessid', $session_id, \PDO::PARAM_STR)
87: ->setParameter(':expires', time(), \PDO::PARAM_INT);
88:
89: $session_data = '';
90: if ($result = $qb->execute()) {
91: if ($row = $result->fetch(\PDO::FETCH_NUM)) {
92: list ($session_data) = $row;
93: }
94: }
95:
96:
97:
98:
99:
100:
101: return $session_data;
102: }
103:
104: 105: 106: 107: 108: 109: 110: 111:
112: public function write($session_id, $session_data)
113: {
114: $expires = (isset($_SESSION['SESSION_MANAGER_EXPIRES']))
115: ? (int)($_SESSION['SESSION_MANAGER_EXPIRES'])
116: : time() + (session_cache_expire() * 60);
117: $oldIsolation = $this->db->getTransactionIsolation();
118: $this->db->setTransactionIsolation(Connection::TRANSACTION_REPEATABLE_READ);
119: $this->db->beginTransaction();
120: $qb = $this->db->createXoopsQueryBuilder();
121: $eb = $qb->expr();
122: $qb ->updatePrefix($this->sessionTable)
123: ->set('expires_at', ':expires')
124: ->set('session_data', ':sessdata')
125: ->where($eb->eq('session_id', ':sessid'))
126: ->setParameter(':sessid', $session_id, \PDO::PARAM_STR)
127: ->setParameter(':expires', $expires, \PDO::PARAM_INT)
128: ->setParameter(':sessdata', $session_data, \PDO::PARAM_STR);
129: $this->db->setForce(true);
130: $result = $qb->execute();
131: if ($result<=0) {
132: $qb = $this->db->createXoopsQueryBuilder();
133: $qb ->insertPrefix($this->sessionTable)
134: ->values(array(
135: 'session_id' => ':sessid',
136: 'expires_at' => ':expires',
137: 'session_data' => ':sessdata',
138: ))
139: ->setParameter(':sessid', $session_id, \PDO::PARAM_STR)
140: ->setParameter(':expires', $expires, \PDO::PARAM_INT)
141: ->setParameter(':sessdata', $session_data, \PDO::PARAM_STR);
142: $this->db->setForce(true);
143: $result = $qb->execute();
144: }
145: $this->db->commit();
146: $this->db->setTransactionIsolation($oldIsolation);
147:
148: return (boolean) ($result>0);
149: }
150:
151: 152: 153: 154: 155: 156: 157:
158: public function destroy($session_id)
159: {
160: $qb = $this->db->createXoopsQueryBuilder();
161: $eb = $qb->expr();
162: $qb ->deletePrefix($this->sessionTable)
163: ->where($eb->eq('session_id', ':sessid'))
164: ->setParameter(':sessid', $session_id, \PDO::PARAM_STR);
165: $this->db->setForce(true);
166: $result = $qb->execute();
167: return (boolean) ($result>0);
168: }
169:
170: 171: 172: 173: 174: 175: 176:
177: public function gc($maxlifetime)
178: {
179: $mintime = time();
180: $qb = $this->db->createXoopsQueryBuilder();
181: $eb = $qb->expr();
182: $qb ->deletePrefix($this->sessionTable)
183: ->where($eb->lt('expires_at', ':expires'))
184: ->setParameter(':expires', $mintime, \PDO::PARAM_INT);
185: $this->db->setForce(true);
186: $result = $qb->execute();
187: return (boolean) ($result>0);
188: }
189: }
190: