*prechod na novsiu verziu ZF
[sport-group.git] / library / Zend / Session / SaveHandler / DbTable.php
blob84a7f731809b3291d36ac28a0e6bdcbe5c5cffbf
1 <?php
3 /**
4 * Zend Framework
6 * LICENSE
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.
16 * @category Zend
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 $
23 /**
24 * @see Zend_Session
26 require_once 'Zend/Session.php';
28 /**
29 * @see Zend_Db_Table_Abstract
31 require_once 'Zend/Db/Table/Abstract.php';
33 /**
34 * @see Zend_Db_Table_Row_Abstract
36 require_once 'Zend/Db/Table/Row/Abstract.php';
38 /**
39 * @see Zend_Config
41 require_once 'Zend/Config.php';
43 /**
44 * Zend_Session_SaveHandler_DbTable
46 * @category Zend
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';
73 /**
74 * Session table primary key value assignment
76 * @var array
78 protected $_primaryAssignment = null;
80 /**
81 * Session table last modification time column
83 * @var string
85 protected $_modifiedColumn = null;
87 /**
88 * Session table lifetime column
90 * @var string
92 protected $_lifetimeColumn = null;
94 /**
95 * Session table data column
97 * @var string
99 protected $_dataColumn = null;
102 * Session lifetime
104 * @var int
106 protected $_lifetime = false;
109 * Whether or not the lifetime of an existing session should be overridden
111 * @var boolean
113 protected $_overrideLifetime = false;
116 * Session save path
118 * @var string
120 protected $_sessionSavePath;
123 * Session name
125 * @var string
127 protected $_sessionName;
130 * Constructor
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
160 * @return void
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) {
179 do {
180 switch ($key) {
181 case self::PRIMARY_ASSIGNMENT:
182 $this->_primaryAssignment = $value;
183 break;
184 case self::MODIFIED_COLUMN:
185 $this->_modifiedColumn = (string) $value;
186 break;
187 case self::LIFETIME_COLUMN:
188 $this->_lifetimeColumn = (string) $value;
189 break;
190 case self::DATA_COLUMN:
191 $this->_dataColumn = (string) $value;
192 break;
193 case self::LIFETIME:
194 $this->setLifetime($value);
195 break;
196 case self::OVERRIDE_LIFETIME:
197 $this->setOverrideLifetime($value);
198 break;
199 default:
200 // unrecognized options passed to parent::__construct()
201 break 2;
203 unset($config[$key]);
204 } while (false);
207 parent::__construct($config);
211 * Destructor
213 * @return void
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)
231 if ($lifetime < 0) {
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');
239 } else {
240 $this->_lifetime = (int) $lifetime;
243 if ($overrideLifetime != null) {
244 $this->setOverrideLifetime($overrideLifetime);
247 return $this;
251 * Retrieve session lifetime
253 * @return int
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;
270 return $this;
274 * Retrieve whether or not the lifetime of an existing session should be overridden
276 * @return boolean
278 public function getOverrideLifetime()
280 return $this->_overrideLifetime;
284 * Open Session
286 * @param string $save_path
287 * @param string $name
288 * @return boolean
290 public function open($save_path, $name)
292 $this->_sessionSavePath = $save_path;
293 $this->_sessionName = $name;
295 return true;
299 * Close session
301 * @return boolean
303 public function close()
305 return true;
309 * Read session data
311 * @param string $id
312 * @return string
314 public function read($id)
316 $return = '';
318 $rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
320 if (count($rows)) {
321 if ($this->_getExpirationTime($row = $rows->current()) > time()) {
322 $return = $row->{$this->_dataColumn};
323 } else {
324 $this->destroy($id);
328 return $return;
332 * Write session data
334 * @param string $id
335 * @param string $data
336 * @return boolean
338 public function write($id, $data)
340 $return = false;
342 $data = array($this->_modifiedColumn => time(),
343 $this->_dataColumn => (string) $data);
345 $rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
347 if (count($rows)) {
348 $data[$this->_lifetimeColumn] = $this->_getLifetime($rows->current());
350 if ($this->update($data, $this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
351 $return = true;
353 } else {
354 $data[$this->_lifetimeColumn] = $this->_lifetime;
356 if ($this->insert(array_merge($this->_getPrimary($id, self::PRIMARY_TYPE_ASSOC), $data))) {
357 $return = true;
361 return $return;
365 * Destroy session
367 * @param string $id
368 * @return boolean
370 public function destroy($id)
372 $return = false;
374 if ($this->delete($this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
375 $return = true;
378 return $return;
382 * Garbage Collection
384 * @param int $maxlifetime
385 * @return true
387 public function gc($maxlifetime)
389 $this->delete($this->getAdapter()->quoteIdentifier($this->_modifiedColumn) . ' + '
390 . $this->getAdapter()->quoteIdentifier($this->_lifetimeColumn) . ' < '
391 . $this->getAdapter()->quote(time()));
393 return true;
397 * Calls other protected methods for individual setup tasks and requirement checks
399 * @return void
401 protected function _setup()
403 parent::_setup();
405 $this->_setupPrimaryAssignment();
406 $this->setLifetime($this->_lifetime);
408 $this->_checkRequiredColumns();
412 * Initialize table and schema names
414 * @return void
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
436 * @return void
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
475 * @return void
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
513 * @param string $id
514 * @param string $type (optional; default: self::PRIMARY_TYPE_NUM)
515 * @return array
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;
531 break;
532 case self::PRIMARY_ASSIGNMENT_SESSION_NAME:
533 $value = $this->_sessionName;
534 break;
535 case self::PRIMARY_ASSIGNMENT_SESSION_ID:
536 $value = (string) $id;
537 break;
538 default:
539 $value = (string) $this->_primaryAssignment[$index];
540 break;
543 switch ((string) $type) {
544 case self::PRIMARY_TYPE_PRIMARYNUM:
545 $primaryArray[$index] = $value;
546 break;
547 case self::PRIMARY_TYPE_ASSOC:
548 $primaryArray[$primary] = $value;
549 break;
550 case self::PRIMARY_TYPE_WHERECLAUSE:
551 $primaryArray[] = $this->getAdapter()->quoteIdentifier($primary) . ' = '
552 . $this->getAdapter()->quote($value);
553 break;
554 case self::PRIMARY_TYPE_NUM:
555 default:
556 $primaryArray[] = $value;
557 break;
561 return $primaryArray;
565 * Retrieve session lifetime considering Zend_Session_SaveHandler_DbTable::OVERRIDE_LIFETIME
567 * @param Zend_Db_Table_Row_Abstract $row
568 * @return int
570 protected function _getLifetime(Zend_Db_Table_Row_Abstract $row)
572 $return = $this->_lifetime;
574 if (!$this->_overrideLifetime) {
575 $return = (int) $row->{$this->_lifetimeColumn};
578 return $return;
582 * Retrieve session expiration time
584 * @param Zend_Db_Table_Row_Abstract $row
585 * @return int
587 protected function _getExpirationTime(Zend_Db_Table_Row_Abstract $row)
589 return (int) $row->{$this->_modifiedColumn} + $this->_getLifetime($row);