Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file SessionHandler.php

Documentation is available at SessionHandler.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009 - 2011, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_WindowsAzure
  30.  * @subpackage Session
  31.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  32.  * @license    http://phpazure.codeplex.com/license
  33.  * @version    $Id: Storage.php 21617 2009-06-12 10:46:31Z unknown $
  34.  */
  35.  
  36. /**
  37.  * @see Microsoft_AutoLoader
  38.  */
  39. require_once dirname(__FILE__'/../AutoLoader.php';
  40.  
  41. /**
  42.  * @category   Microsoft
  43.  * @package    Microsoft_WindowsAzure
  44.  * @subpackage Session
  45.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  46.  * @license    http://phpazure.codeplex.com/license
  47.  */
  48. {
  49.     /**
  50.      * Maximal property size in table storage.
  51.      * 
  52.      * @var int 
  53.      * @see http://msdn.microsoft.com/en-us/library/dd179338.aspx
  54.      */
  55.     const MAX_TS_PROPERTY_SIZE 65536;
  56.     
  57.     /** Storage backend type */
  58.     const STORAGE_TYPE_TABLE 'table';
  59.     const STORAGE_TYPE_BLOB 'blob';
  60.     
  61.     /**
  62.      * Storage back-end
  63.      * 
  64.      * @var Microsoft_WindowsAzure_Storage_Table|Microsoft_WindowsAzure_Storage_Blob
  65.      */
  66.     protected $_storage;
  67.     
  68.     /**
  69.      * Storage backend type
  70.      * 
  71.      * @var string 
  72.      */
  73.     protected $_storageType;
  74.     
  75.     /**
  76.      * Session container name
  77.      * 
  78.      * @var string 
  79.      */
  80.     protected $_sessionContainer;
  81.     
  82.     /**
  83.      * Session container partition
  84.      * 
  85.      * @var string 
  86.      */
  87.     protected $_sessionContainerPartition;
  88.     
  89.     /**
  90.      * Creates a new Microsoft_WindowsAzure_SessionHandler instance
  91.      * 
  92.      * @param Microsoft_WindowsAzure_Storage_Table|Microsoft_WindowsAzure_Storage_Blob$storage Storage back-end, can be table storage and blob storage
  93.      * @param string $sessionContainer Session container name
  94.      * @param string $sessionContainerPartition Session container partition
  95.      */
  96.     public function __construct(Microsoft_WindowsAzure_Storage $storage$sessionContainer 'phpsessions'$sessionContainerPartition 'sessions')
  97.     {
  98.         // Validate $storage
  99.         if (!($storage instanceof Microsoft_WindowsAzure_Storage_Table || $storage instanceof Microsoft_WindowsAzure_Storage_Blob)) {
  100.             throw new Microsoft_WindowsAzure_Exception('Invalid storage back-end given. Storage back-end should be of type Microsoft_WindowsAzure_Storage_Table or Microsoft_WindowsAzure_Storage_Blob.');
  101.         }
  102.         
  103.         // Validate other parameters
  104.         if ($sessionContainer == '' || $sessionContainerPartition == ''{
  105.             throw new Microsoft_WindowsAzure_Exception('Session container and session partition should be specified.');
  106.         }
  107.         
  108.         // Determine storage type
  109.         $storageType self::STORAGE_TYPE_TABLE;
  110.         if ($storage instanceof Microsoft_WindowsAzure_Storage_Blob{
  111.             $storageType self::STORAGE_TYPE_BLOB;
  112.         }
  113.         
  114.         // Set properties
  115.         $this->_storage = $storage;
  116.         $this->_storageType = $storageType;
  117.         $this->_sessionContainer = $sessionContainer;
  118.         $this->_sessionContainerPartition = $sessionContainerPartition;
  119.     }
  120.     
  121.     /**
  122.      * Registers the current session handler as PHP's session handler
  123.      * 
  124.      * @return boolean 
  125.      */
  126.     public function register()
  127.     {
  128.         return session_set_save_handler(array($this'open'),
  129.                                         array($this'close'),
  130.                                         array($this'read'),
  131.                                         array($this'write'),
  132.                                         array($this'destroy'),
  133.                                         array($this'gc')
  134.         );
  135.     }
  136.     
  137.     /**
  138.      * Open the session store
  139.      * 
  140.      * @return bool 
  141.      */
  142.     public function open()
  143.     {
  144.         // Make sure storage container exists
  145.         if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  146.             $this->_storage->createTableIfNotExists($this->_sessionContainer);
  147.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  148.             $this->_storage->createContainerIfNotExists($this->_sessionContainer);
  149.         }
  150.         
  151.         // Ok!
  152.         return true;
  153.     }
  154.  
  155.     /**
  156.      * Close the session store
  157.      * 
  158.      * @return bool 
  159.      */
  160.     public function close()
  161.     {
  162.         return true;
  163.     }
  164.     
  165.     /**
  166.      * Read a specific session
  167.      * 
  168.      * @param int $id Session Id
  169.      * @return string 
  170.      */
  171.     public function read($id)
  172.     {
  173.         // Read data
  174.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  175.             // In table storage
  176.             try
  177.             {
  178.                 $sessionRecord $this->_storage->retrieveEntityById(
  179.                     $this->_sessionContainer,
  180.                     $this->_sessionContainerPartition,
  181.                     $id
  182.                 );
  183.                 return unserialize(base64_decode($sessionRecord->serializedData));
  184.             }
  185.             catch (Microsoft_WindowsAzure_Exception $ex)
  186.             {
  187.                 return '';
  188.             }
  189.            else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  190.             // In blob storage
  191.             try
  192.             {
  193.                 $data $this->_storage->getBlobData(
  194.                     $this->_sessionContainer,
  195.                     $this->_sessionContainerPartition . '/' $id
  196.                 );
  197.                 return unserialize(base64_decode($data));
  198.             }
  199.             catch (Microsoft_WindowsAzure_Exception $ex)
  200.             {
  201.                 return false;
  202.             }
  203.         }
  204.     }
  205.     
  206.     /**
  207.      * Write a specific session
  208.      * 
  209.      * @param int $id Session Id
  210.      * @param string $serializedData Serialized PHP object
  211.      * @throws Exception
  212.      */
  213.     public function write($id$serializedData)
  214.     {
  215.         // Encode data
  216.         $serializedData base64_encode(serialize($serializedData));
  217.         if (strlen($serializedData>= self::MAX_TS_PROPERTY_SIZE && $this->_storageType == self::STORAGE_TYPE_TABLE{
  218.             throw new Microsoft_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.');
  219.         }
  220.         
  221.         // Store data
  222.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  223.             // In table storage
  224.                $sessionRecord new Microsoft_WindowsAzure_Storage_DynamicTableEntity($this->_sessionContainerPartition$id);
  225.             $sessionRecord->sessionExpires time();
  226.             $sessionRecord->serializedData $serializedData;
  227.             
  228.             $sessionRecord->setAzurePropertyType('sessionExpires''Edm.Int32');
  229.     
  230.             try
  231.             {
  232.                 $this->_storage->updateEntity($this->_sessionContainer$sessionRecord);
  233.             }
  234.             catch (Microsoft_WindowsAzure_Exception $unknownRecord)
  235.             {
  236.                 $this->_storage->insertEntity($this->_sessionContainer$sessionRecord);
  237.             }
  238.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  239.             // In blob storage
  240.             $this->_storage->putBlobData(
  241.                 $this->_sessionContainer,
  242.                 $this->_sessionContainerPartition . '/' $id,
  243.                 $serializedData,
  244.                 array('sessionexpires' => time())
  245.             );
  246.         }
  247.     }
  248.     
  249.     /**
  250.      * Destroy a specific session
  251.      * 
  252.      * @param int $id Session Id
  253.      * @return boolean 
  254.      */
  255.     public function destroy($id)
  256.     {
  257.         // Destroy data
  258.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  259.             // In table storage
  260.                try
  261.             {
  262.                 $sessionRecord $this->_storage->retrieveEntityById(
  263.                     $this->_sessionContainer,
  264.                     $this->_sessionContainerPartition,
  265.                     $id
  266.                 );
  267.                 $this->_storage->deleteEntity($this->_sessionContainer$sessionRecord);
  268.                 
  269.                 return true;
  270.             }
  271.             catch (Microsoft_WindowsAzure_Exception $ex)
  272.             {
  273.                 return false;
  274.             }
  275.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  276.             // In blob storage
  277.             try
  278.             {
  279.                 $this->_storage->deleteBlob(
  280.                     $this->_sessionContainer,
  281.                     $this->_sessionContainerPartition . '/' $id
  282.                 );
  283.                 
  284.                 return true;
  285.             }
  286.             catch (Microsoft_WindowsAzure_Exception $ex)
  287.             {
  288.                 return false;
  289.             }
  290.         }
  291.     }
  292.     
  293.     /**
  294.      * Garbage collector
  295.      * 
  296.      * @param int $lifeTime Session maximal lifetime
  297.      * @see session.gc_divisor  100
  298.      * @see session.gc_maxlifetime 1440
  299.      * @see session.gc_probability 1
  300.      * @usage Execution rate 1/100 (session.gc_probability/session.gc_divisor)
  301.      * @return boolean 
  302.      */
  303.     public function gc($lifeTime)
  304.     {
  305.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  306.             // In table storage
  307.                try
  308.             {
  309.                 $result $this->_storage->retrieveEntities($this->_sessionContainer'PartitionKey eq \'' $this->_sessionContainerPartition . '\' and sessionExpires lt ' (time($lifeTime));
  310.                 foreach ($result as $sessionRecord)
  311.                 {
  312.                     $this->_storage->deleteEntity($this->_sessionContainer$sessionRecord);
  313.                 }
  314.                 return true;
  315.             }
  316.             catch (Microsoft_WindowsAzure_exception $ex)
  317.             {
  318.                 return false;
  319.             }
  320.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  321.             // In blob storage
  322.             try
  323.             {
  324.                 $result $this->_storage->listBlobs($this->_sessionContainer$this->_sessionContainerPartition''nullnull'metadata');
  325.                 foreach ($result as $sessionRecord)
  326.                 {
  327.                     if ($sessionRecord->Metadata['sessionexpires'(time($lifeTime)) {
  328.                         $this->_storage->deleteBlob($this->_sessionContainer$sessionRecord->Name);
  329.                     }
  330.                 }
  331.                 return true;
  332.             }
  333.             catch (Microsoft_WindowsAzure_exception $ex)
  334.             {
  335.                 return false;
  336.             }
  337.         }
  338.     }
  339. }

Documentation generated on Wed, 18 May 2011 12:06:49 +0200 by phpDocumentor 1.4.3