SessionHandler.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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-web at 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_Service_WindowsAzure
  17. * @subpackage Session
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** Zend_Service_WindowsAzure_Storage_Table */
  23. require_once 'Zend/Service/WindowsAzure/Storage/Table.php';
  24. /**
  25. * @see Zend_Service_WindowsAzure_Exception
  26. */
  27. require_once 'Zend/Service/WindowsAzure/Exception.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_Service_WindowsAzure
  31. * @subpackage Session
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Service_WindowsAzure_SessionHandler
  36. {
  37. /**
  38. * Table storage
  39. *
  40. * @var Zend_Service_WindowsAzure_Storage_Table
  41. */
  42. protected $_tableStorage;
  43. /**
  44. * Session table name
  45. *
  46. * @var string
  47. */
  48. protected $_sessionTable;
  49. /**
  50. * Session table partition
  51. *
  52. * @var string
  53. */
  54. protected $_sessionTablePartition;
  55. /**
  56. * Creates a new Zend_Service_WindowsAzure_SessionHandler instance
  57. *
  58. * @param Zend_Service_WindowsAzure_Storage_Table $tableStorage Table storage
  59. * @param string $sessionTable Session table name
  60. * @param string $sessionTablePartition Session table partition
  61. */
  62. public function __construct(Zend_Service_WindowsAzure_Storage_Table $tableStorage, $sessionTable = 'phpsessions', $sessionTablePartition = 'sessions')
  63. {
  64. // Set properties
  65. $this->_tableStorage = $tableStorage;
  66. $this->_sessionTable = $sessionTable;
  67. $this->_sessionTablePartition = $sessionTablePartition;
  68. }
  69. /**
  70. * Registers the current session handler as PHP's session handler
  71. *
  72. * @return boolean
  73. */
  74. public function register()
  75. {
  76. return session_set_save_handler(array($this, 'open'),
  77. array($this, 'close'),
  78. array($this, 'read'),
  79. array($this, 'write'),
  80. array($this, 'destroy'),
  81. array($this, 'gc')
  82. );
  83. }
  84. /**
  85. * Open the session store
  86. *
  87. * @return bool
  88. */
  89. public function open()
  90. {
  91. // Make sure table exists
  92. $tableExists = $this->_tableStorage->tableExists($this->_sessionTable);
  93. if (!$tableExists) {
  94. $this->_tableStorage->createTable($this->_sessionTable);
  95. }
  96. // Ok!
  97. return true;
  98. }
  99. /**
  100. * Close the session store
  101. *
  102. * @return bool
  103. */
  104. public function close()
  105. {
  106. return true;
  107. }
  108. /**
  109. * Read a specific session
  110. *
  111. * @param int $id Session Id
  112. * @return string
  113. */
  114. public function read($id)
  115. {
  116. try
  117. {
  118. $sessionRecord = $this->_tableStorage->retrieveEntityById(
  119. $this->_sessionTable,
  120. $this->_sessionTablePartition,
  121. $id
  122. );
  123. return base64_decode($sessionRecord->serializedData);
  124. }
  125. catch (Zend_Service_WindowsAzure_Exception $ex)
  126. {
  127. return '';
  128. }
  129. }
  130. /**
  131. * Write a specific session
  132. *
  133. * @param int $id Session Id
  134. * @param string $serializedData Serialized PHP object
  135. */
  136. public function write($id, $serializedData)
  137. {
  138. $sessionRecord = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity($this->_sessionTablePartition, $id);
  139. $sessionRecord->sessionExpires = time();
  140. $sessionRecord->serializedData = base64_encode($serializedData);
  141. $sessionRecord->setAzurePropertyType('sessionExpires', 'Edm.Int32');
  142. try
  143. {
  144. $this->_tableStorage->updateEntity($this->_sessionTable, $sessionRecord);
  145. }
  146. catch (Zend_Service_WindowsAzure_Exception $unknownRecord)
  147. {
  148. $this->_tableStorage->insertEntity($this->_sessionTable, $sessionRecord);
  149. }
  150. }
  151. /**
  152. * Destroy a specific session
  153. *
  154. * @param int $id Session Id
  155. * @return boolean
  156. */
  157. public function destroy($id)
  158. {
  159. try
  160. {
  161. $sessionRecord = $this->_tableStorage->retrieveEntityById(
  162. $this->_sessionTable,
  163. $this->_sessionTablePartition,
  164. $id
  165. );
  166. $this->_tableStorage->deleteEntity($this->_sessionTable, $sessionRecord);
  167. return true;
  168. }
  169. catch (Zend_Service_WindowsAzure_Exception $ex)
  170. {
  171. return false;
  172. }
  173. }
  174. /**
  175. * Garbage collector
  176. *
  177. * @param int $lifeTime Session maximal lifetime
  178. * @see session.gc_divisor 100
  179. * @see session.gc_maxlifetime 1440
  180. * @see session.gc_probability 1
  181. * @usage Execution rate 1/100 (session.gc_probability/session.gc_divisor)
  182. * @return boolean
  183. */
  184. public function gc($lifeTime)
  185. {
  186. try
  187. {
  188. $result = $this->_tableStorage->retrieveEntities($this->_sessionTable, 'PartitionKey eq \'' . $this->_sessionTablePartition . '\' and sessionExpires lt ' . (time() - $lifeTime));
  189. foreach ($result as $sessionRecord)
  190. {
  191. $this->_tableStorage->deleteEntity($this->_sessionTable, $sessionRecord);
  192. }
  193. return true;
  194. }
  195. catch (Zend_Service_WindowsAzure_exception $ex)
  196. {
  197. return false;
  198. }
  199. }
  200. }