forked from phalcon/incubator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HandlerSocket.php
286 lines (249 loc) · 6.83 KB
/
HandlerSocket.php
1
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/**
* HandlerSocket session handler
* Table schema :
* CREATE TABLE `php_session` (
* `id` varchar(32) NOT NULL DEFAULT '',
* `modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
* `data` text,
* PRIMARY KEY (`id`),
* KEY `modified` (`modified`)
* ) ENGINE=InnoDB DEFAULT CHARACTER SET utf8;
*/
namespace Phalcon\Session\Adapter;
use Phalcon\Session\Adapter;
use Phalcon\Session\AdapterInterface;
use Phalcon\Session\Exception;
class HandlerSocket extends Adapter implements AdapterInterface
{
/**
* Default Values
*/
const DEFAULT_HOST = 'localhost';
const DEFAULT_PORT = 9999;
const DEFAULT_DBNAME = 'session';
const DEFAULT_DBTABLE = 'php_session';
/**
* Database fileds and index
*/
const DB_FIELDS = 'id,modified,data';
const DB_GC_INDEX = 'modified';
/**
* Available options
* =====> (string) cookie_path :
* the path for which the cookie is valid.
* =====> (string) cookie_domain :
* the domain for which the cookie is valid.
* =====> (int) lifetime :
* session lifetime in seconds
* =====> (array) server :
* an array of mysql handlersocket server :
* 'host' => (string) : the name of the mysql handlersocket server
* 'port' => (int) : the port of the mysql handlersocket server
* 'dbname' => (string) : the database name of the mysql handlersocket server
* 'dbtable' => (string) : the table name of the mysql handlersocket server
*/
protected $options = [
'cookie_path' => '/',
'cookie_domain' => '',
'lifetime' => 3600,
'server' => [
'host' => self::DEFAULT_HOST,
'port' => self::DEFAULT_PORT,
'dbname' => self::DEFAULT_DBNAME,
'dbtable' => self::DEFAULT_DBTABLE
],
];
/**
* HandlerSocket object
*
* @var \HandlerSocket
*/
protected $hs;
/**
* HandlerSocket index number
*
* @var integer
*/
protected $hsIndex = 1;
/**
* Stores session data results
*
* @var array
*/
protected $fields = [];
/**
* Class constructor.
*
* @param array $options associative array of options
* @throws \Phalcon\Session\Exception
*/
public function __construct($options = [])
{
// initialize the handlersocket database
if (empty($options)) {
$this->init($this->options);
} else {
$this->init($options);
}
//set object as the save handler
session_set_save_handler(
[$this, 'open'],
[$this, 'close'],
[$this, 'read'],
[$this, 'write'],
[$this, 'destroy'],
[$this, 'gc']
);
}
/**
* Destructor
*
* @return void
*/
public function __destruct()
{
session_write_close();
}
/**
* {@inheritdoc}
*
* @param array $options associative array of options
* @return void
*/
public function start($options = [])
{
$object = new self($options);
//start it up
session_start();
}
/**
*{@inheritdoc}
*
* @param string $save_path
* @param string $name
* @return boolean
*/
public function open($save_path, $name)
{
return true;
}
/**
* {@inheritdoc}
*
* @return boolean
*/
public function close()
{
return true;
}
/**
* {@inheritdoc}
*
* @param string $id
* @return string
*/
public function read($id)
{
$retval = $this->hs->executeSingle($this->hsIndex, '=', [$id], 1, 0);
if (!isset($retval[0], $retval[0][2])) {
return '';
}
$this->fields['id'] = $retval[0][0];
$this->fields['modified'] = $retval[0][1];
$this->fields['data'] = '';
return $retval[0][2];
}
/**
* {@inheritdoc}
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data)
{
if (isset($this->fields['id']) && $this->fields['id'] != $id) {
$this->fields = [];
}
if (empty($this->fields)) {
$this->hs->executeInsert($this->hsIndex, [$id, date('Y-m-d H:i:s'), $data]);
} else {
$this->hs->executeUpdate($this->hsIndex, '=', [$id], [$id, date('Y-m-d H:i:s'), $data], 1, 0);
}
return true;
}
/**
* {@inheritdoc}
*
* @param string $id
* @return boolean
*/
public function destroy($id)
{
$this->hs->executeDelete($this->hsIndex, '=', [$id], 1, 0);
return true;
}
/**
* {@inheritdoc}
*
* @param integer $maxlifetime
* @return boolean
*/
public function gc($maxlifetime)
{
$time = date('Y-m-d H:i:s', strtotime("- $maxlifetime seconds"));
$index = $this->hsIndex + 1;
$this->hs->openIndex(
$index,
$this->options['server']['dbname'],
$this->options['server']['dbtable'],
self::DB_GC_INDEX,
''
);
$this->hs->executeDelete($index, '<', [$time], 1000, 0);
return true;
}
/**
* Initialize HandlerSocket.
*
* @param array $options associative array of options
* @throws \Phalcon\Session\Exception
*/
protected function init($options)
{
if (empty($options['server'])) {
$options['server'] = [];
}
if (empty($options['server']['host'])) {
$options['server']['host'] = self::DEFAULT_HOST;
}
if (empty($options['server']['port'])) {
$options['server']['port'] = self::DEFAULT_PORT;
}
if (empty($options['server']['dbname'])) {
$options['server']['dbname'] = self::DEFAULT_DBNAME;
}
if (empty($options['server']['dbtable'])) {
$options['server']['dbtable'] = self::DEFAULT_DBTABLE;
}
//update options
$this->options = $options;
if (!extension_loaded('handlersocket')) {
throw new Exception('The handlersocket extension must be loaded for using session!');
}
// load handlersocket server
$this->hs = new \HandlerSocket($options['server']['host'], $options['server']['port']);
// open handlersocket index
$result = $this->hs->openIndex(
$this->hsIndex,
$options['server']['dbname'],
$options['server']['dbtable'],
\HandlerSocket::PRIMARY,
self::DB_FIELDS
);
if (!$result) {
throw new Exception('The HandlerSocket database specified in the options does not exist.');
}
}
}