2
0

DbTable.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-webat this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Session
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * @see Zend_Session
  23. */
  24. require_once 'Zend/Session.php';
  25. /**
  26. * @see Zend_Db_Table_Abstract
  27. */
  28. require_once 'Zend/Db/Table/Abstract.php';
  29. /**
  30. * @see Zend_Db_Table_Row_Abstract
  31. */
  32. require_once 'Zend/Db/Table/Row/Abstract.php';
  33. /**
  34. * @see Zend_Config
  35. */
  36. require_once 'Zend/Config.php';
  37. /**
  38. * Zend_Session_SaveHandler_DbTable
  39. *
  40. * @category Zend
  41. * @package Zend_Session
  42. * @subpackage SaveHandler
  43. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  44. * @license http://framework.zend.com/license/new-bsd New BSD License
  45. */
  46. class Zend_Session_SaveHandler_DbTable extends Zend_Db_Table_Abstract implements Zend_Session_SaveHandler_Interface
  47. {
  48. const PRIMARY_ASSIGNMENT = 'primaryAssignment';
  49. const PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH = 'sessionSavePath';
  50. const PRIMARY_ASSIGNMENT_SESSION_NAME = 'sessionName';
  51. const PRIMARY_ASSIGNMENT_SESSION_ID = 'sessionId';
  52. const MODIFIED_COLUMN = 'modifiedColumn';
  53. const LIFETIME_COLUMN = 'lifetimeColumn';
  54. const DATA_COLUMN = 'dataColumn';
  55. const LIFETIME = 'lifetime';
  56. const OVERRIDE_LIFETIME = 'overrideLifetime';
  57. const PRIMARY_TYPE_NUM = 'PRIMARY_TYPE_NUM';
  58. const PRIMARY_TYPE_PRIMARYNUM = 'PRIMARY_TYPE_PRIMARYNUM';
  59. const PRIMARY_TYPE_ASSOC = 'PRIMARY_TYPE_ASSOC';
  60. const PRIMARY_TYPE_WHERECLAUSE = 'PRIMARY_TYPE_WHERECLAUSE';
  61. /**
  62. * Session table primary key value assignment
  63. *
  64. * @var array
  65. */
  66. protected $_primaryAssignment = null;
  67. /**
  68. * Session table last modification time column
  69. *
  70. * @var string
  71. */
  72. protected $_modifiedColumn = null;
  73. /**
  74. * Session table lifetime column
  75. *
  76. * @var string
  77. */
  78. protected $_lifetimeColumn = null;
  79. /**
  80. * Session table data column
  81. *
  82. * @var string
  83. */
  84. protected $_dataColumn = null;
  85. /**
  86. * Session lifetime
  87. *
  88. * @var int
  89. */
  90. protected $_lifetime = false;
  91. /**
  92. * Whether or not the lifetime of an existing session should be overridden
  93. *
  94. * @var boolean
  95. */
  96. protected $_overrideLifetime = false;
  97. /**
  98. * Session save path
  99. *
  100. * @var string
  101. */
  102. protected $_sessionSavePath;
  103. /**
  104. * Session name
  105. *
  106. * @var string
  107. */
  108. protected $_sessionName;
  109. /**
  110. * Constructor
  111. *
  112. * $config is an instance of Zend_Config or an array of key/value pairs containing configuration options for
  113. * Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract. These are the configuration options for
  114. * Zend_Session_SaveHandler_DbTable:
  115. *
  116. * primaryAssignment => (string|array) Session table primary key value assignment
  117. * (optional; default: 1 => sessionId) You have to assign a value to each primary key of your session table.
  118. * The value of this configuration option is either a string if you have only one primary key or an array if
  119. * you have multiple primary keys. The array consists of numeric keys starting at 1 and string values. There
  120. * are some values which will be replaced by session information:
  121. *
  122. * sessionId => The id of the current session
  123. * sessionName => The name of the current session
  124. * sessionSavePath => The save path of the current session
  125. *
  126. * NOTE: One of your assignments MUST contain 'sessionId' as value!
  127. *
  128. * modifiedColumn => (string) Session table last modification time column
  129. *
  130. * lifetimeColumn => (string) Session table lifetime column
  131. *
  132. * dataColumn => (string) Session table data column
  133. *
  134. * lifetime => (integer) Session lifetime (optional; default: ini_get('session.gc_maxlifetime'))
  135. *
  136. * overrideLifetime => (boolean) Whether or not the lifetime of an existing session should be overridden
  137. * (optional; default: false)
  138. *
  139. * @param Zend_Config|array $config User-provided configuration
  140. * @return void
  141. * @throws Zend_Session_SaveHandler_Exception
  142. */
  143. public function __construct($config)
  144. {
  145. if ($config instanceof Zend_Config) {
  146. $config = $config->toArray();
  147. } else if (!is_array($config)) {
  148. /**
  149. * @see Zend_Session_SaveHandler_Exception
  150. */
  151. require_once 'Zend/Session/SaveHandler/Exception.php';
  152. throw new Zend_Session_SaveHandler_Exception(
  153. '$config must be an instance of Zend_Config or array of key/value pairs containing '
  154. . 'configuration options for Zend_Session_SaveHandler_DbTable and Zend_Db_Table_Abstract.');
  155. }
  156. foreach ($config as $key => $value) {
  157. do {
  158. switch ($key) {
  159. case self::PRIMARY_ASSIGNMENT:
  160. $this->_primaryAssignment = $value;
  161. break;
  162. case self::MODIFIED_COLUMN:
  163. $this->_modifiedColumn = (string) $value;
  164. break;
  165. case self::LIFETIME_COLUMN:
  166. $this->_lifetimeColumn = (string) $value;
  167. break;
  168. case self::DATA_COLUMN:
  169. $this->_dataColumn = (string) $value;
  170. break;
  171. case self::LIFETIME:
  172. $this->setLifetime($value);
  173. break;
  174. case self::OVERRIDE_LIFETIME:
  175. $this->setOverrideLifetime($value);
  176. break;
  177. default:
  178. // unrecognized options passed to parent::__construct()
  179. break 2;
  180. }
  181. unset($config[$key]);
  182. } while (false);
  183. }
  184. parent::__construct($config);
  185. }
  186. /**
  187. * Destructor
  188. *
  189. * @return void
  190. */
  191. public function __destruct()
  192. {
  193. Zend_Session::writeClose();
  194. }
  195. /**
  196. * Set session lifetime and optional whether or not the lifetime of an existing session should be overridden
  197. *
  198. * $lifetime === false resets lifetime to session.gc_maxlifetime
  199. *
  200. * @param int $lifetime
  201. * @param boolean $overrideLifetime (optional)
  202. * @return Zend_Session_SaveHandler_DbTable
  203. */
  204. public function setLifetime($lifetime, $overrideLifetime = null)
  205. {
  206. if ($lifetime < 0) {
  207. /**
  208. * @see Zend_Session_SaveHandler_Exception
  209. */
  210. require_once 'Zend/Session/SaveHandler/Exception.php';
  211. throw new Zend_Session_SaveHandler_Exception();
  212. } else if (empty($lifetime)) {
  213. $this->_lifetime = (int) ini_get('session.gc_maxlifetime');
  214. } else {
  215. $this->_lifetime = (int) $lifetime;
  216. }
  217. if ($overrideLifetime != null) {
  218. $this->setOverrideLifetime($overrideLifetime);
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Retrieve session lifetime
  224. *
  225. * @return int
  226. */
  227. public function getLifetime()
  228. {
  229. return $this->_lifetime;
  230. }
  231. /**
  232. * Set whether or not the lifetime of an existing session should be overridden
  233. *
  234. * @param boolean $overrideLifetime
  235. * @return Zend_Session_SaveHandler_DbTable
  236. */
  237. public function setOverrideLifetime($overrideLifetime)
  238. {
  239. $this->_overrideLifetime = (boolean) $overrideLifetime;
  240. return $this;
  241. }
  242. /**
  243. * Retrieve whether or not the lifetime of an existing session should be overridden
  244. *
  245. * @return boolean
  246. */
  247. public function getOverrideLifetime()
  248. {
  249. return $this->_overrideLifetime;
  250. }
  251. /**
  252. * Open Session
  253. *
  254. * @param string $save_path
  255. * @param string $name
  256. * @return boolean
  257. */
  258. public function open($save_path, $name)
  259. {
  260. $this->_sessionSavePath = $save_path;
  261. $this->_sessionName = $name;
  262. return true;
  263. }
  264. /**
  265. * Close session
  266. *
  267. * @return boolean
  268. */
  269. public function close()
  270. {
  271. return true;
  272. }
  273. /**
  274. * Read session data
  275. *
  276. * @param string $id
  277. * @return string
  278. */
  279. public function read($id)
  280. {
  281. $return = '';
  282. $rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
  283. if (count($rows)) {
  284. if ($this->_getExpirationTime($row = $rows->current()) > time()) {
  285. $return = $row->{$this->_dataColumn};
  286. } else {
  287. $this->destroy($id);
  288. }
  289. }
  290. return $return;
  291. }
  292. /**
  293. * Write session data
  294. *
  295. * @param string $id
  296. * @param string $data
  297. * @return boolean
  298. */
  299. public function write($id, $data)
  300. {
  301. $return = false;
  302. $data = array($this->_modifiedColumn => time(),
  303. $this->_dataColumn => (string) $data);
  304. $rows = call_user_func_array(array(&$this, 'find'), $this->_getPrimary($id));
  305. if (count($rows)) {
  306. $data[$this->_lifetimeColumn] = $this->_getLifetime($rows->current());
  307. if ($this->update($data, $this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
  308. $return = true;
  309. }
  310. } else {
  311. $data[$this->_lifetimeColumn] = $this->_lifetime;
  312. if ($this->insert(array_merge($this->_getPrimary($id, self::PRIMARY_TYPE_ASSOC), $data))) {
  313. $return = true;
  314. }
  315. }
  316. return $return;
  317. }
  318. /**
  319. * Destroy session
  320. *
  321. * @param string $id
  322. * @return boolean
  323. */
  324. public function destroy($id)
  325. {
  326. $return = false;
  327. if ($this->delete($this->_getPrimary($id, self::PRIMARY_TYPE_WHERECLAUSE))) {
  328. $return = true;
  329. }
  330. return $return;
  331. }
  332. /**
  333. * Garbage Collection
  334. *
  335. * @param int $maxlifetime
  336. * @return true
  337. */
  338. public function gc($maxlifetime)
  339. {
  340. $this->delete($this->getAdapter()->quoteIdentifier($this->_modifiedColumn) . ' + '
  341. . $this->getAdapter()->quoteIdentifier($this->_lifetimeColumn) . ' < '
  342. . $this->getAdapter()->quote(time()));
  343. return true;
  344. }
  345. /**
  346. * Calls other protected methods for individual setup tasks and requirement checks
  347. *
  348. * @return void
  349. */
  350. protected function _setup()
  351. {
  352. parent::_setup();
  353. $this->_setupPrimaryAssignment();
  354. $this->setLifetime($this->_lifetime);
  355. $this->_checkRequiredColumns();
  356. }
  357. /**
  358. * Initialize table and schema names
  359. *
  360. * @return void
  361. * @throws Zend_Session_SaveHandler_Exception
  362. */
  363. protected function _setupTableName()
  364. {
  365. if (empty($this->_name) && basename(($this->_name = session_save_path())) != $this->_name) {
  366. /**
  367. * @see Zend_Session_SaveHandler_Exception
  368. */
  369. require_once 'Zend/Session/SaveHandler/Exception.php';
  370. throw new Zend_Session_SaveHandler_Exception('session.save_path is a path and not a table name.');
  371. }
  372. if (strpos($this->_name, '.')) {
  373. list($this->_schema, $this->_name) = explode('.', $this->_name);
  374. }
  375. }
  376. /**
  377. * Initialize session table primary key value assignment
  378. *
  379. * @return void
  380. * @throws Zend_Session_SaveHandler_Exception
  381. */
  382. protected function _setupPrimaryAssignment()
  383. {
  384. if ($this->_primaryAssignment === null) {
  385. $this->_primaryAssignment = array(1 => self::PRIMARY_ASSIGNMENT_SESSION_ID);
  386. } else if (!is_array($this->_primaryAssignment)) {
  387. $this->_primaryAssignment = array(1 => (string) $this->_primaryAssignment);
  388. } else if (isset($this->_primaryAssignment[0])) {
  389. array_unshift($this->_primaryAssignment, null);
  390. unset($this->_primaryAssignment[0]);
  391. }
  392. if (count($this->_primaryAssignment) !== count($this->_primary)) {
  393. /**
  394. * @see Zend_Session_SaveHandler_Exception
  395. */
  396. require_once 'Zend/Session/SaveHandler/Exception.php';
  397. throw new Zend_Session_SaveHandler_Exception(
  398. "Value for configuration option '" . self::PRIMARY_ASSIGNMENT . "' must have an assignment "
  399. . "for each session table primary key.");
  400. } else if (!in_array(self::PRIMARY_ASSIGNMENT_SESSION_ID, $this->_primaryAssignment)) {
  401. /**
  402. * @see Zend_Session_SaveHandler_Exception
  403. */
  404. require_once 'Zend/Session/SaveHandler/Exception.php';
  405. throw new Zend_Session_SaveHandler_Exception(
  406. "Value for configuration option '" . self::PRIMARY_ASSIGNMENT . "' must have an assignment "
  407. . "for the session id ('" . self::PRIMARY_ASSIGNMENT_SESSION_ID . "').");
  408. }
  409. }
  410. /**
  411. * Check for required session table columns
  412. *
  413. * @return void
  414. * @throws Zend_Session_SaveHandler_Exception
  415. */
  416. protected function _checkRequiredColumns()
  417. {
  418. if ($this->_modifiedColumn === null) {
  419. /**
  420. * @see Zend_Session_SaveHandler_Exception
  421. */
  422. require_once 'Zend/Session/SaveHandler/Exception.php';
  423. throw new Zend_Session_SaveHandler_Exception(
  424. "Configuration must define '" . self::MODIFIED_COLUMN . "' which names the "
  425. . "session table last modification time column.");
  426. } else if ($this->_lifetimeColumn === null) {
  427. /**
  428. * @see Zend_Session_SaveHandler_Exception
  429. */
  430. require_once 'Zend/Session/SaveHandler/Exception.php';
  431. throw new Zend_Session_SaveHandler_Exception(
  432. "Configuration must define '" . self::LIFETIME_COLUMN . "' which names the "
  433. . "session table lifetime column.");
  434. } else if ($this->_dataColumn === null) {
  435. /**
  436. * @see Zend_Session_SaveHandler_Exception
  437. */
  438. require_once 'Zend/Session/SaveHandler/Exception.php';
  439. throw new Zend_Session_SaveHandler_Exception(
  440. "Configuration must define '" . self::DATA_COLUMN . "' which names the "
  441. . "session table data column.");
  442. }
  443. }
  444. /**
  445. * Retrieve session table primary key values
  446. *
  447. * @param string $id
  448. * @param string $type (optional; default: self::PRIMARY_TYPE_NUM)
  449. * @return array
  450. */
  451. protected function _getPrimary($id, $type = null)
  452. {
  453. $this->_setupPrimaryKey();
  454. if ($type === null) {
  455. $type = self::PRIMARY_TYPE_NUM;
  456. }
  457. $primaryArray = array();
  458. foreach ($this->_primary as $index => $primary) {
  459. switch ($this->_primaryAssignment[$index]) {
  460. case self::PRIMARY_ASSIGNMENT_SESSION_SAVE_PATH:
  461. $value = $this->_sessionSavePath;
  462. break;
  463. case self::PRIMARY_ASSIGNMENT_SESSION_NAME:
  464. $value = $this->_sessionName;
  465. break;
  466. case self::PRIMARY_ASSIGNMENT_SESSION_ID:
  467. $value = (string) $id;
  468. break;
  469. default:
  470. $value = (string) $this->_primaryAssignment[$index];
  471. break;
  472. }
  473. switch ((string) $type) {
  474. case self::PRIMARY_TYPE_PRIMARYNUM:
  475. $primaryArray[$index] = $value;
  476. break;
  477. case self::PRIMARY_TYPE_ASSOC:
  478. $primaryArray[$primary] = $value;
  479. break;
  480. case self::PRIMARY_TYPE_WHERECLAUSE:
  481. $primaryArray[] = $this->getAdapter()->quoteIdentifier($primary) . ' = '
  482. . $this->getAdapter()->quote($value);
  483. break;
  484. case self::PRIMARY_TYPE_NUM:
  485. default:
  486. $primaryArray[] = $value;
  487. break;
  488. }
  489. }
  490. return $primaryArray;
  491. }
  492. /**
  493. * Retrieve session lifetime considering Zend_Session_SaveHandler_DbTable::OVERRIDE_LIFETIME
  494. *
  495. * @param Zend_Db_Table_Row_Abstract $row
  496. * @return int
  497. */
  498. protected function _getLifetime(Zend_Db_Table_Row_Abstract $row)
  499. {
  500. $return = $this->_lifetime;
  501. if (!$this->_overrideLifetime) {
  502. $return = (int) $row->{$this->_lifetimeColumn};
  503. }
  504. return $return;
  505. }
  506. /**
  507. * Retrieve session expiration time
  508. *
  509. * @param Zend_Db_Table_Row_Abstract $row
  510. * @return int
  511. */
  512. protected function _getExpirationTime(Zend_Db_Table_Row_Abstract $row)
  513. {
  514. return (int) $row->{$this->_modifiedColumn} + $this->_getLifetime($row);
  515. }
  516. }