DbTable.php
17.5 KB
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-webat this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Session
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: DbTable.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_Session
*/
require_once 'Zend/Session.php';
/**
* @see Zend_Db_Table_Abstract
*/
require_once 'Zend/Db/Table/Abstract.php';
/**
* @see Zend_Db_Table_Row_Abstract
*/
require_once 'Zend/Db/Table/Row/Abstract.php';
/**
* @see Zend_Config
*/
require_once 'Zend/Config.php';
/**
* Zend_Session_SaveHandler_DbTable
*
* @category Zend
* @package Zend_Session
* @subpackage SaveHandler
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Session_SaveHandler_DbTable
extends Zend_Db_Table_Abstract
implements Zend_Session_SaveHandler_Interface
{
const PRIMARY_ASSIGNMENT = 'primaryAssignment';
const PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH = 'sessionSavePath';
const PRIMARY_ASSIGNMENT_SESSION_NAME = 'sessionName';
const PRIMARY_ASSIGNMENT_SESSION_ID = 'sessionId';
const MODIFIED_COLUMN = 'modifiedColumn';
const LIFETIME_COLUMN = 'lifetimeColumn';
const DATA_COLUMN = 'dataColumn';
const LIFETIME = 'lifetime';
const OVERRIDE_LIFETIME = 'overrideLifetime';
const PRIMARY_TYPE_NUM = 'PRIMARY_TYPE_NUM';
const PRIMARY_TYPE_PRIMARYNUM = 'PRIMARY_TYPE_PRIMARYNUM';
const PRIMARY_TYPE_ASSOC = 'PRIMARY_TYPE_ASSOC';
const PRIMARY_TYPE_WHERECLAUSE = 'PRIMARY_TYPE_WHERECLAUSE';
/**
* Session table primary key value assignment
*
* @var array
*/
protected $_primaryAssignment = null;
/**
* Session table last modification time column
*
* @var string
*/
protected $_modifiedColumn = null;
/**
* Session table lifetime column
*
* @var string
*/
protected $_lifetimeColumn = null;
/**
* Session table data column
*
* @var string
*/
protected $_dataColumn = null;
/**
* Session lifetime
*
* @var int
*/
protected $_lifetime = false;
/**
* Whether or not the lifetime of an existing session should be overridden
*
* @var boolean
*/
protected $_overrideLifetime = false;
/**
* Session save path
*
* @var string
*/
protected $_sessionSavePath;
/**
* Session name
*
* @var string
*/
protected $_sessionName;
/**
* Constructor
*
* $config is an instance of Zend_Config or an array of key/value pairs containing configuration options for
* Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract. These are the configuration options for
* Zend_Session_SaveHandler_DbTable:
*
* primaryAssignment => (string|array) Session table primary key value assignment
* (optional; default: 1 => sessionId) You have to assign a value to each primary key of your session table.
* The value of this configuration option is either a string if you have only one primary key or an array if
* you have multiple primary keys. The array consists of numeric keys starting at 1 and string values. There
* are some values which will be replaced by session information:
*
* sessionId => The id of the current session
* sessionName => The name of the current session
* sessionSavePath => The save path of the current session
*
* NOTE: One of your assignments MUST contain 'sessionId' as value!
*
* modifiedColumn => (string) Session table last modification time column
*
* lifetimeColumn => (string) Session table lifetime column
*
* dataColumn => (string) Session table data column
*
* lifetime => (integer) Session lifetime (optional; default: ini_get('session.gc_maxlifetime'))
*
* overrideLifetime => (boolean) Whether or not the lifetime of an existing session should be overridden
* (optional; default: false)
*
* @param Zend_Config|array $config User-provided configuration
* @return void
* @throws Zend_Session_SaveHandler_Exception
*/
public function __construct($config)
{
if ($config instanceof Zend_Config) {
$config = $config->toArray();
} else if (!is_array($config)) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception(
'$config must be an instance of Zend_Config or array of key/value pairs containing '
. 'configuration options for Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract.');
}
foreach ($config as $key => $value) {
do {
switch ($key) {
case self::PRIMARY_ASSIGNMENT:
$this->_primaryAssignment = $value;
break;
case self::MODIFIED_COLUMN:
$this->_modifiedColumn = (string) $value;
break;
case self::LIFETIME_COLUMN:
$this->_lifetimeColumn = (string) $value;
break;
case self::DATA_COLUMN:
$this->_dataColumn = (string) $value;
break;
case self::LIFETIME:
$this->setLifetime($value);
break;
case self::OVERRIDE_LIFETIME:
$this->setOverrideLifetime($value);
break;
default:
// unrecognized options passed to parent::__construct()
break 2;
}
unset($config[$key]);
} while (false);
}
parent::__construct($config);
}
/**
* Destructor
*
* @return void
*/
public function __destruct()
{
Zend_Session::writeClose();
}
/**
* Set session lifetime and optional whether or not the lifetime of an existing session should be overridden
*
* $lifetime === false resets lifetime to session.gc_maxlifetime
*
* @param int $lifetime
* @param boolean $overrideLifetime (optional)
* @return Zend_Session_SaveHandler_DbTable
*/
public function setLifetime($lifetime, $overrideLifetime = null)
{
if ($lifetime < 0) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception();
} else if (empty($lifetime)) {
$this->_lifetime = (int) ini_get('session.gc_maxlifetime');
} else {
$this->_lifetime = (int) $lifetime;
}
if ($overrideLifetime != null) {
$this->setOverrideLifetime($overrideLifetime);
}
return $this;
}
/**
* Retrieve session lifetime
*
* @return int
*/
public function getLifetime()
{
return $this->_lifetime;
}
/**
* Set whether or not the lifetime of an existing session should be overridden
*
* @param boolean $overrideLifetime
* @return Zend_Session_SaveHandler_DbTable
*/
public function setOverrideLifetime($overrideLifetime)
{
$this->_overrideLifetime = (boolean) $overrideLifetime;
return $this;
}
/**
* Retrieve whether or not the lifetime of an existing session should be overridden
*
* @return boolean
*/
public function getOverrideLifetime()
{
return $this->_overrideLifetime;
}
/**
* Open Session
*
* @param string $save_path
* @param string $name
* @return boolean
*/
public function open($save_path, $name)
{
$this->_sessionSavePath = $save_path;
$this->_sessionName = $name;
return true;
}
/**
* Close session
*
* @return boolean
*/
public function close()
{
return true;
}
/**
* Read session data
*
* @param string $id
* @return string
*/
public function read($id)
{
$return = '';
$rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
if (count($rows)) {
if ($this->_getExpirationTime($row = $rows->current()) > time()) {
$return = $row->{$this->_dataColumn};
} else {
$this->destroy($id);
}
}
return $return;
}
/**
* Write session data
*
* @param string $id
* @param string $data
* @return boolean
*/
public function write($id, $data)
{
$return = false;
$data = array($this->_modifiedColumn => time(),
$this->_dataColumn => (string) $data);
$rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
if (count($rows)) {
$data[$this->_lifetimeColumn] = $this->_getLifetime($rows->current());
if ($this->update($data, $this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
$return = true;
}
} else {
$data[$this->_lifetimeColumn] = $this->_lifetime;
if ($this->insert(array_merge($this->_getPrimary($id, self::PRIMARY_TYPE_ASSOC), $data))) {
$return = true;
}
}
return $return;
}
/**
* Destroy session
*
* @param string $id
* @return boolean
*/
public function destroy($id)
{
$return = false;
if ($this->delete($this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
$return = true;
}
return $return;
}
/**
* Garbage Collection
*
* @param int $maxlifetime
* @return true
*/
public function gc($maxlifetime)
{
$this->delete($this->getAdapter()->quoteIdentifier($this->_modifiedColumn, true) . ' + '
. $this->getAdapter()->quoteIdentifier($this->_lifetimeColumn, true) . ' < '
. $this->getAdapter()->quote(time()));
return true;
}
/**
* Calls other protected methods for individual setup tasks and requirement checks
*
* @return void
*/
protected function _setup()
{
parent::_setup();
$this->_setupPrimaryAssignment();
$this->setLifetime($this->_lifetime);
$this->_checkRequiredColumns();
}
/**
* Initialize table and schema names
*
* @return void
* @throws Zend_Session_SaveHandler_Exception
*/
protected function _setupTableName()
{
if (empty($this->_name) && basename(($this->_name = session_save_path())) != $this->_name) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception('session.save_path is a path and not a table name.');
}
if (strpos($this->_name, '.')) {
list($this->_schema, $this->_name) = explode('.', $this->_name);
}
}
/**
* Initialize session table primary key value assignment
*
* @return void
* @throws Zend_Session_SaveHandler_Exception
*/
protected function _setupPrimaryAssignment()
{
if ($this->_primaryAssignment === null) {
$this->_primaryAssignment = array(1 => self::PRIMARY_ASSIGNMENT_SESSION_ID);
} else if (!is_array($this->_primaryAssignment)) {
$this->_primaryAssignment = array(1 => (string) $this->_primaryAssignment);
} else if (isset($this->_primaryAssignment[0])) {
array_unshift($this->_primaryAssignment, null);
unset($this->_primaryAssignment[0]);
}
if (count($this->_primaryAssignment) !== count($this->_primary)) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception(
"Value for configuration option '" . self::PRIMARY_ASSIGNMENT . "' must have an assignment "
. "for each session table primary key.");
} else if (!in_array(self::PRIMARY_ASSIGNMENT_SESSION_ID, $this->_primaryAssignment)) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception(
"Value for configuration option '" . self::PRIMARY_ASSIGNMENT . "' must have an assignment "
. "for the session id ('" . self::PRIMARY_ASSIGNMENT_SESSION_ID . "').");
}
}
/**
* Check for required session table columns
*
* @return void
* @throws Zend_Session_SaveHandler_Exception
*/
protected function _checkRequiredColumns()
{
if ($this->_modifiedColumn === null) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception(
"Configuration must define '" . self::MODIFIED_COLUMN . "' which names the "
. "session table last modification time column.");
} else if ($this->_lifetimeColumn === null) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception(
"Configuration must define '" . self::LIFETIME_COLUMN . "' which names the "
. "session table lifetime column.");
} else if ($this->_dataColumn === null) {
/**
* @see Zend_Session_SaveHandler_Exception
*/
require_once 'Zend/Session/SaveHandler/Exception.php';
throw new Zend_Session_SaveHandler_Exception(
"Configuration must define '" . self::DATA_COLUMN . "' which names the "
. "session table data column.");
}
}
/**
* Retrieve session table primary key values
*
* @param string $id
* @param string $type (optional; default: self::PRIMARY_TYPE_NUM)
* @return array
*/
protected function _getPrimary($id, $type = null)
{
$this->_setupPrimaryKey();
if ($type === null) {
$type = self::PRIMARY_TYPE_NUM;
}
$primaryArray = array();
foreach ($this->_primary as $index => $primary) {
switch ($this->_primaryAssignment[$index]) {
case self::PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH:
$value = $this->_sessionSavePath;
break;
case self::PRIMARY_ASSIGNMENT_SESSION_NAME:
$value = $this->_sessionName;
break;
case self::PRIMARY_ASSIGNMENT_SESSION_ID:
$value = (string) $id;
break;
default:
$value = (string) $this->_primaryAssignment[$index];
break;
}
switch ((string) $type) {
case self::PRIMARY_TYPE_PRIMARYNUM:
$primaryArray[$index] = $value;
break;
case self::PRIMARY_TYPE_ASSOC:
$primaryArray[$primary] = $value;
break;
case self::PRIMARY_TYPE_WHERECLAUSE:
$primaryArray[] = $this->getAdapter()->quoteIdentifier($primary, true) . ' = '
. $this->getAdapter()->quote($value);
break;
case self::PRIMARY_TYPE_NUM:
default:
$primaryArray[] = $value;
break;
}
}
return $primaryArray;
}
/**
* Retrieve session lifetime considering Zend_Session_SaveHandler_DbTable::OVERRIDE_LIFETIME
*
* @param Zend_Db_Table_Row_Abstract $row
* @return int
*/
protected function _getLifetime(Zend_Db_Table_Row_Abstract $row)
{
$return = $this->_lifetime;
if (!$this->_overrideLifetime) {
$return = (int) $row->{$this->_lifetimeColumn};
}
return $return;
}
/**
* Retrieve session expiration time
*
* @param Zend_Db_Table_Row_Abstract $row
* @return int
*/
protected function _getExpirationTime(Zend_Db_Table_Row_Abstract $row)
{
return (int) $row->{$this->_modifiedColumn} + $this->_getLifetime($row);
}
}