/libraries/Zend/Service/WindowsAzure/SessionHandler.php

https://github.com/kiranatama/sagalaya · PHP · 189 lines · 94 code · 16 blank · 79 comment · 1 complexity · 211af702f581c38060b6c8fd10a1f67e MD5 · raw file

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