SessionHandler.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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-2015 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. /**
  23. * @category Zend
  24. * @package Zend_Service_WindowsAzure
  25. * @subpackage Session
  26. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Service_WindowsAzure_SessionHandler
  30. {
  31. /**
  32. * Maximal property size in table storage.
  33. *
  34. * @var int
  35. * @see http://msdn.microsoft.com/en-us/library/dd179338.aspx
  36. */
  37. const MAX_TS_PROPERTY_SIZE = 65536;
  38. /** Storage backend type */
  39. const STORAGE_TYPE_TABLE = 'table';
  40. const STORAGE_TYPE_BLOB = 'blob';
  41. /**
  42. * Storage back-end
  43. *
  44. * @var Zend_Service_WindowsAzure_Storage_Table|Zend_Service_WindowsAzure_Storage_Blob
  45. */
  46. protected $_storage;
  47. /**
  48. * Storage backend type
  49. *
  50. * @var string
  51. */
  52. protected $_storageType;
  53. /**
  54. * Session container name
  55. *
  56. * @var string
  57. */
  58. protected $_sessionContainer;
  59. /**
  60. * Session container partition
  61. *
  62. * @var string
  63. */
  64. protected $_sessionContainerPartition;
  65. /**
  66. * Creates a new Zend_Service_WindowsAzure_SessionHandler instance
  67. *
  68. * @param Zend_Service_WindowsAzure_Storage_Table|Zend_Service_WindowsAzure_Storage_Blob $storage Storage back-end, can be table storage and blob storage
  69. * @param string $sessionContainer Session container name
  70. * @param string $sessionContainerPartition Session container partition
  71. */
  72. public function __construct(Zend_Service_WindowsAzure_Storage $storage, $sessionContainer = 'phpsessions', $sessionContainerPartition = 'sessions')
  73. {
  74. // Validate $storage
  75. if (!($storage instanceof Zend_Service_WindowsAzure_Storage_Table || $storage instanceof Zend_Service_WindowsAzure_Storage_Blob)) {
  76. require_once 'Zend/Service/WindowsAzure/Exception.php';
  77. throw new Zend_Service_WindowsAzure_Exception('Invalid storage back-end given. Storage back-end should be of type Zend_Service_WindowsAzure_Storage_Table or Zend_Service_WindowsAzure_Storage_Blob.');
  78. }
  79. // Validate other parameters
  80. if ($sessionContainer == '' || $sessionContainerPartition == '') {
  81. require_once 'Zend/Service/WindowsAzure/Exception.php';
  82. throw new Zend_Service_WindowsAzure_Exception('Session container and session partition should be specified.');
  83. }
  84. // Determine storage type
  85. $storageType = self::STORAGE_TYPE_TABLE;
  86. if ($storage instanceof Zend_Service_WindowsAzure_Storage_Blob) {
  87. $storageType = self::STORAGE_TYPE_BLOB;
  88. }
  89. // Set properties
  90. $this->_storage = $storage;
  91. $this->_storageType = $storageType;
  92. $this->_sessionContainer = $sessionContainer;
  93. $this->_sessionContainerPartition = $sessionContainerPartition;
  94. }
  95. /**
  96. * Registers the current session handler as PHP's session handler
  97. *
  98. * @return boolean
  99. */
  100. public function register()
  101. {
  102. return session_set_save_handler(array($this, 'open'),
  103. array($this, 'close'),
  104. array($this, 'read'),
  105. array($this, 'write'),
  106. array($this, 'destroy'),
  107. array($this, 'gc')
  108. );
  109. }
  110. /**
  111. * Open the session store
  112. *
  113. * @return bool
  114. */
  115. public function open()
  116. {
  117. // Make sure storage container exists
  118. if ($this->_storageType == self::STORAGE_TYPE_TABLE) {
  119. $this->_storage->createTableIfNotExists($this->_sessionContainer);
  120. } else if ($this->_storageType == self::STORAGE_TYPE_BLOB) {
  121. $this->_storage->createContainerIfNotExists($this->_sessionContainer);
  122. }
  123. // Ok!
  124. return true;
  125. }
  126. /**
  127. * Close the session store
  128. *
  129. * @return bool
  130. */
  131. public function close()
  132. {
  133. return true;
  134. }
  135. /**
  136. * Read a specific session
  137. *
  138. * @param int $id Session Id
  139. * @return string
  140. */
  141. public function read($id)
  142. {
  143. // Read data
  144. if ($this->_storageType == self::STORAGE_TYPE_TABLE) {
  145. // In table storage
  146. try
  147. {
  148. $sessionRecord = $this->_storage->retrieveEntityById(
  149. $this->_sessionContainer,
  150. $this->_sessionContainerPartition,
  151. $id
  152. );
  153. return unserialize(base64_decode($sessionRecord->serializedData));
  154. }
  155. catch (Zend_Service_WindowsAzure_Exception $ex)
  156. {
  157. return '';
  158. }
  159. } else if ($this->_storageType == self::STORAGE_TYPE_BLOB) {
  160. // In blob storage
  161. try
  162. {
  163. $data = $this->_storage->getBlobData(
  164. $this->_sessionContainer,
  165. $this->_sessionContainerPartition . '/' . $id
  166. );
  167. return unserialize(base64_decode($data));
  168. }
  169. catch (Zend_Service_WindowsAzure_Exception $ex)
  170. {
  171. return false;
  172. }
  173. }
  174. }
  175. /**
  176. * Write a specific session
  177. *
  178. * @param int $id Session Id
  179. * @param string $serializedData Serialized PHP object
  180. * @throws Exception
  181. */
  182. public function write($id, $serializedData)
  183. {
  184. // Encode data
  185. $serializedData = base64_encode(serialize($serializedData));
  186. if (strlen($serializedData) >= self::MAX_TS_PROPERTY_SIZE && $this->_storageType == self::STORAGE_TYPE_TABLE) {
  187. throw new Zend_Service_WindowsAzure_Exception('Session data exceeds the maximum allowed size of ' . self::MAX_TS_PROPERTY_SIZE . ' bytes that can be stored using table storage. Consider switching to a blob storage back-end or try reducing session data size.');
  188. }
  189. // Store data
  190. if ($this->_storageType == self::STORAGE_TYPE_TABLE) {
  191. // In table storage
  192. $sessionRecord = new Zend_Service_WindowsAzure_Storage_DynamicTableEntity($this->_sessionContainerPartition, $id);
  193. $sessionRecord->sessionExpires = time();
  194. $sessionRecord->serializedData = $serializedData;
  195. $sessionRecord->setAzurePropertyType('sessionExpires', 'Edm.Int32');
  196. try
  197. {
  198. $this->_storage->updateEntity($this->_sessionContainer, $sessionRecord);
  199. }
  200. catch (Zend_Service_WindowsAzure_Exception $unknownRecord)
  201. {
  202. $this->_storage->insertEntity($this->_sessionContainer, $sessionRecord);
  203. }
  204. } else if ($this->_storageType == self::STORAGE_TYPE_BLOB) {
  205. // In blob storage
  206. $this->_storage->putBlobData(
  207. $this->_sessionContainer,
  208. $this->_sessionContainerPartition . '/' . $id,
  209. $serializedData,
  210. array('sessionexpires' => time())
  211. );
  212. }
  213. }
  214. /**
  215. * Destroy a specific session
  216. *
  217. * @param int $id Session Id
  218. * @return boolean
  219. */
  220. public function destroy($id)
  221. {
  222. // Destroy data
  223. if ($this->_storageType == self::STORAGE_TYPE_TABLE) {
  224. // In table storage
  225. try
  226. {
  227. $sessionRecord = $this->_storage->retrieveEntityById(
  228. $this->_sessionContainer,
  229. $this->_sessionContainerPartition,
  230. $id
  231. );
  232. $this->_storage->deleteEntity($this->_sessionContainer, $sessionRecord);
  233. return true;
  234. }
  235. catch (Zend_Service_WindowsAzure_Exception $ex)
  236. {
  237. return false;
  238. }
  239. } else if ($this->_storageType == self::STORAGE_TYPE_BLOB) {
  240. // In blob storage
  241. try
  242. {
  243. $this->_storage->deleteBlob(
  244. $this->_sessionContainer,
  245. $this->_sessionContainerPartition . '/' . $id
  246. );
  247. return true;
  248. }
  249. catch (Zend_Service_WindowsAzure_Exception $ex)
  250. {
  251. return false;
  252. }
  253. }
  254. }
  255. /**
  256. * Garbage collector
  257. *
  258. * @param int $lifeTime Session maximal lifetime
  259. * @see session.gc_divisor 100
  260. * @see session.gc_maxlifetime 1440
  261. * @see session.gc_probability 1
  262. * @usage Execution rate 1/100 (session.gc_probability/session.gc_divisor)
  263. * @return boolean
  264. */
  265. public function gc($lifeTime)
  266. {
  267. if ($this->_storageType == self::STORAGE_TYPE_TABLE) {
  268. // In table storage
  269. try
  270. {
  271. $result = $this->_storage->retrieveEntities($this->_sessionContainer, 'PartitionKey eq \'' . $this->_sessionContainerPartition . '\' and sessionExpires lt ' . (time() - $lifeTime));
  272. foreach ($result as $sessionRecord)
  273. {
  274. $this->_storage->deleteEntity($this->_sessionContainer, $sessionRecord);
  275. }
  276. return true;
  277. }
  278. catch (Zend_Service_WindowsAzure_exception $ex)
  279. {
  280. return false;
  281. }
  282. } else if ($this->_storageType == self::STORAGE_TYPE_BLOB) {
  283. // In blob storage
  284. try
  285. {
  286. $result = $this->_storage->listBlobs($this->_sessionContainer, $this->_sessionContainerPartition, '', null, null, 'metadata');
  287. foreach ($result as $sessionRecord)
  288. {
  289. if ($sessionRecord->Metadata['sessionexpires'] < (time() - $lifeTime)) {
  290. $this->_storage->deleteBlob($this->_sessionContainer, $sessionRecord->Name);
  291. }
  292. }
  293. return true;
  294. }
  295. catch (Zend_Service_WindowsAzure_exception $ex)
  296. {
  297. return false;
  298. }
  299. }
  300. }
  301. }