8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-webat this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
17 * @package Zend_Session
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: DbTable.php 16933 2009-07-21 20:24:35Z matthew $
26 require_once 'Zend/Session.php';
29 * @see Zend_Db_Table_Abstract
31 require_once 'Zend/Db/Table/Abstract.php';
34 * @see Zend_Db_Table_Row_Abstract
36 require_once 'Zend/Db/Table/Row/Abstract.php';
41 require_once 'Zend/Config.php';
44 * Zend_Session_SaveHandler_DbTable
47 * @package Zend_Session
48 * @subpackage SaveHandler
49 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
50 * @license http://framework.zend.com/license/new-bsd New BSD License
52 class Zend_Session_SaveHandler_DbTable
53 extends Zend_Db_Table_Abstract
54 implements Zend_Session_SaveHandler_Interface
56 const PRIMARY_ASSIGNMENT
= 'primaryAssignment';
57 const PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH
= 'sessionSavePath';
58 const PRIMARY_ASSIGNMENT_SESSION_NAME
= 'sessionName';
59 const PRIMARY_ASSIGNMENT_SESSION_ID
= 'sessionId';
61 const MODIFIED_COLUMN
= 'modifiedColumn';
62 const LIFETIME_COLUMN
= 'lifetimeColumn';
63 const DATA_COLUMN
= 'dataColumn';
65 const LIFETIME
= 'lifetime';
66 const OVERRIDE_LIFETIME
= 'overrideLifetime';
68 const PRIMARY_TYPE_NUM
= 'PRIMARY_TYPE_NUM';
69 const PRIMARY_TYPE_PRIMARYNUM
= 'PRIMARY_TYPE_PRIMARYNUM';
70 const PRIMARY_TYPE_ASSOC
= 'PRIMARY_TYPE_ASSOC';
71 const PRIMARY_TYPE_WHERECLAUSE
= 'PRIMARY_TYPE_WHERECLAUSE';
74 * Session table primary key value assignment
78 protected $_primaryAssignment = null;
81 * Session table last modification time column
85 protected $_modifiedColumn = null;
88 * Session table lifetime column
92 protected $_lifetimeColumn = null;
95 * Session table data column
99 protected $_dataColumn = null;
106 protected $_lifetime = false;
109 * Whether or not the lifetime of an existing session should be overridden
113 protected $_overrideLifetime = false;
120 protected $_sessionSavePath;
127 protected $_sessionName;
132 * $config is an instance of Zend_Config or an array of key/value pairs containing configuration options for
133 * Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract. These are the configuration options for
134 * Zend_Session_SaveHandler_DbTable:
136 * primaryAssignment => (string|array) Session table primary key value assignment
137 * (optional; default: 1 => sessionId) You have to assign a value to each primary key of your session table.
138 * The value of this configuration option is either a string if you have only one primary key or an array if
139 * you have multiple primary keys. The array consists of numeric keys starting at 1 and string values. There
140 * are some values which will be replaced by session information:
142 * sessionId => The id of the current session
143 * sessionName => The name of the current session
144 * sessionSavePath => The save path of the current session
146 * NOTE: One of your assignments MUST contain 'sessionId' as value!
148 * modifiedColumn => (string) Session table last modification time column
150 * lifetimeColumn => (string) Session table lifetime column
152 * dataColumn => (string) Session table data column
154 * lifetime => (integer) Session lifetime (optional; default: ini_get('session.gc_maxlifetime'))
156 * overrideLifetime => (boolean) Whether or not the lifetime of an existing session should be overridden
157 * (optional; default: false)
159 * @param Zend_Config|array $config User-provided configuration
161 * @throws Zend_Session_SaveHandler_Exception
163 public function __construct($config)
165 if ($config instanceof Zend_Config
) {
166 $config = $config->toArray();
167 } else if (!is_array($config)) {
169 * @see Zend_Session_SaveHandler_Exception
171 require_once 'Zend/Session/SaveHandler/Exception.php';
173 throw new Zend_Session_SaveHandler_Exception(
174 '$config must be an instance of Zend_Config or array of key/value pairs containing '
175 . 'configuration options for Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract.');
178 foreach ($config as $key => $value) {
181 case self
::PRIMARY_ASSIGNMENT
:
182 $this->_primaryAssignment
= $value;
184 case self
::MODIFIED_COLUMN
:
185 $this->_modifiedColumn
= (string) $value;
187 case self
::LIFETIME_COLUMN
:
188 $this->_lifetimeColumn
= (string) $value;
190 case self
::DATA_COLUMN
:
191 $this->_dataColumn
= (string) $value;
194 $this->setLifetime($value);
196 case self
::OVERRIDE_LIFETIME
:
197 $this->setOverrideLifetime($value);
200 // unrecognized options passed to parent::__construct()
203 unset($config[$key]);
207 parent
::__construct($config);
215 public function __destruct()
217 Zend_Session
::writeClose();
221 * Set session lifetime and optional whether or not the lifetime of an existing session should be overridden
223 * $lifetime === false resets lifetime to session.gc_maxlifetime
225 * @param int $lifetime
226 * @param boolean $overrideLifetime (optional)
227 * @return Zend_Session_SaveHandler_DbTable
229 public function setLifetime($lifetime, $overrideLifetime = null)
233 * @see Zend_Session_SaveHandler_Exception
235 require_once 'Zend/Session/SaveHandler/Exception.php';
236 throw new Zend_Session_SaveHandler_Exception();
237 } else if (empty($lifetime)) {
238 $this->_lifetime
= (int) ini_get('session.gc_maxlifetime');
240 $this->_lifetime
= (int) $lifetime;
243 if ($overrideLifetime != null) {
244 $this->setOverrideLifetime($overrideLifetime);
251 * Retrieve session lifetime
255 public function getLifetime()
257 return $this->_lifetime
;
261 * Set whether or not the lifetime of an existing session should be overridden
263 * @param boolean $overrideLifetime
264 * @return Zend_Session_SaveHandler_DbTable
266 public function setOverrideLifetime($overrideLifetime)
268 $this->_overrideLifetime
= (boolean
) $overrideLifetime;
274 * Retrieve whether or not the lifetime of an existing session should be overridden
278 public function getOverrideLifetime()
280 return $this->_overrideLifetime
;
286 * @param string $save_path
287 * @param string $name
290 public function open($save_path, $name)
292 $this->_sessionSavePath
= $save_path;
293 $this->_sessionName
= $name;
303 public function close()
314 public function read($id)
318 $rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
321 if ($this->_getExpirationTime($row = $rows->current()) > time()) {
322 $return = $row->{$this->_dataColumn
};
335 * @param string $data
338 public function write($id, $data)
342 $data = array($this->_modifiedColumn
=> time(),
343 $this->_dataColumn
=> (string) $data);
345 $rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
348 $data[$this->_lifetimeColumn
] = $this->_getLifetime($rows->current());
350 if ($this->update($data, $this->_getPrimary($id, self
::PRIMARY_TYPE_WHERECLAUSE
))) {
354 $data[$this->_lifetimeColumn
] = $this->_lifetime
;
356 if ($this->insert(array_merge($this->_getPrimary($id, self
::PRIMARY_TYPE_ASSOC
), $data))) {
370 public function destroy($id)
374 if ($this->delete($this->_getPrimary($id, self
::PRIMARY_TYPE_WHERECLAUSE
))) {
384 * @param int $maxlifetime
387 public function gc($maxlifetime)
389 $this->delete($this->getAdapter()->quoteIdentifier($this->_modifiedColumn
) . ' + '
390 . $this->getAdapter()->quoteIdentifier($this->_lifetimeColumn
) . ' < '
391 . $this->getAdapter()->quote(time()));
397 * Calls other protected methods for individual setup tasks and requirement checks
401 protected function _setup()
405 $this->_setupPrimaryAssignment();
406 $this->setLifetime($this->_lifetime
);
408 $this->_checkRequiredColumns();
412 * Initialize table and schema names
415 * @throws Zend_Session_SaveHandler_Exception
417 protected function _setupTableName()
419 if (empty($this->_name
) && basename(($this->_name
= session_save_path())) != $this->_name
) {
421 * @see Zend_Session_SaveHandler_Exception
423 require_once 'Zend/Session/SaveHandler/Exception.php';
425 throw new Zend_Session_SaveHandler_Exception('session.save_path is a path and not a table name.');
428 if (strpos($this->_name
, '.')) {
429 list($this->_schema
, $this->_name
) = explode('.', $this->_name
);
434 * Initialize session table primary key value assignment
437 * @throws Zend_Session_SaveHandler_Exception
439 protected function _setupPrimaryAssignment()
441 if ($this->_primaryAssignment
=== null) {
442 $this->_primaryAssignment
= array(1 => self
::PRIMARY_ASSIGNMENT_SESSION_ID
);
443 } else if (!is_array($this->_primaryAssignment
)) {
444 $this->_primaryAssignment
= array(1 => (string) $this->_primaryAssignment
);
445 } else if (isset($this->_primaryAssignment
[0])) {
446 array_unshift($this->_primaryAssignment
, null);
448 unset($this->_primaryAssignment
[0]);
451 if (count($this->_primaryAssignment
) !== count($this->_primary
)) {
453 * @see Zend_Session_SaveHandler_Exception
455 require_once 'Zend/Session/SaveHandler/Exception.php';
457 throw new Zend_Session_SaveHandler_Exception(
458 "Value for configuration option '" . self
::PRIMARY_ASSIGNMENT
. "' must have an assignment "
459 . "for each session table primary key.");
460 } else if (!in_array(self
::PRIMARY_ASSIGNMENT_SESSION_ID
, $this->_primaryAssignment
)) {
462 * @see Zend_Session_SaveHandler_Exception
464 require_once 'Zend/Session/SaveHandler/Exception.php';
466 throw new Zend_Session_SaveHandler_Exception(
467 "Value for configuration option '" . self
::PRIMARY_ASSIGNMENT
. "' must have an assignment "
468 . "for the session id ('" . self
::PRIMARY_ASSIGNMENT_SESSION_ID
. "').");
473 * Check for required session table columns
476 * @throws Zend_Session_SaveHandler_Exception
478 protected function _checkRequiredColumns()
480 if ($this->_modifiedColumn
=== null) {
482 * @see Zend_Session_SaveHandler_Exception
484 require_once 'Zend/Session/SaveHandler/Exception.php';
486 throw new Zend_Session_SaveHandler_Exception(
487 "Configuration must define '" . self
::MODIFIED_COLUMN
. "' which names the "
488 . "session table last modification time column.");
489 } else if ($this->_lifetimeColumn
=== null) {
491 * @see Zend_Session_SaveHandler_Exception
493 require_once 'Zend/Session/SaveHandler/Exception.php';
495 throw new Zend_Session_SaveHandler_Exception(
496 "Configuration must define '" . self
::LIFETIME_COLUMN
. "' which names the "
497 . "session table lifetime column.");
498 } else if ($this->_dataColumn
=== null) {
500 * @see Zend_Session_SaveHandler_Exception
502 require_once 'Zend/Session/SaveHandler/Exception.php';
504 throw new Zend_Session_SaveHandler_Exception(
505 "Configuration must define '" . self
::DATA_COLUMN
. "' which names the "
506 . "session table data column.");
511 * Retrieve session table primary key values
514 * @param string $type (optional; default: self::PRIMARY_TYPE_NUM)
517 protected function _getPrimary($id, $type = null)
519 $this->_setupPrimaryKey();
521 if ($type === null) {
522 $type = self
::PRIMARY_TYPE_NUM
;
525 $primaryArray = array();
527 foreach ($this->_primary
as $index => $primary) {
528 switch ($this->_primaryAssignment
[$index]) {
529 case self
::PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH
:
530 $value = $this->_sessionSavePath
;
532 case self
::PRIMARY_ASSIGNMENT_SESSION_NAME
:
533 $value = $this->_sessionName
;
535 case self
::PRIMARY_ASSIGNMENT_SESSION_ID
:
536 $value = (string) $id;
539 $value = (string) $this->_primaryAssignment
[$index];
543 switch ((string) $type) {
544 case self
::PRIMARY_TYPE_PRIMARYNUM
:
545 $primaryArray[$index] = $value;
547 case self
::PRIMARY_TYPE_ASSOC
:
548 $primaryArray[$primary] = $value;
550 case self
::PRIMARY_TYPE_WHERECLAUSE
:
551 $primaryArray[] = $this->getAdapter()->quoteIdentifier($primary) . ' = '
552 . $this->getAdapter()->quote($value);
554 case self
::PRIMARY_TYPE_NUM
:
556 $primaryArray[] = $value;
561 return $primaryArray;
565 * Retrieve session lifetime considering Zend_Session_SaveHandler_DbTable::OVERRIDE_LIFETIME
567 * @param Zend_Db_Table_Row_Abstract $row
570 protected function _getLifetime(Zend_Db_Table_Row_Abstract
$row)
572 $return = $this->_lifetime
;
574 if (!$this->_overrideLifetime
) {
575 $return = (int) $row->{$this->_lifetimeColumn
};
582 * Retrieve session expiration time
584 * @param Zend_Db_Table_Row_Abstract $row
587 protected function _getExpirationTime(Zend_Db_Table_Row_Abstract
$row)
589 return (int) $row->{$this->_modifiedColumn
} +
$this->_getLifetime($row);