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