PageRenderTime 49ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Session/SaveHandler/DbTable.php

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