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.      * Object destructor
  123.      */
  124.     public function __destruct({
  125.         session_write_close();
  126.     }
  127.     
  128.     /**
  129.      * Registers the current session handler as PHP's session handler
  130.      * 
  131.      * @return boolean 
  132.      */
  133.     public function register()
  134.     {
  135.         return session_set_save_handler(array($this'open'),
  136.                                         array($this'close'),
  137.                                         array($this'read'),
  138.                                         array($this'write'),
  139.                                         array($this'destroy'),
  140.                                         array($this'gc')
  141.         );
  142.     }
  143.     
  144.     /**
  145.      * Open the session store
  146.      * 
  147.      * @return bool 
  148.      */
  149.     public function open()
  150.     {
  151.         // Make sure storage container exists
  152.         if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  153.             $this->_storage->createTableIfNotExists($this->_sessionContainer);
  154.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  155.             $this->_storage->createContainerIfNotExists($this->_sessionContainer);
  156.         }
  157.         
  158.         // Ok!
  159.         return true;
  160.     }
  161.  
  162.     /**
  163.      * Close the session store
  164.      * 
  165.      * @return bool 
  166.      */
  167.     public function close()
  168.     {
  169.         return true;
  170.     }
  171.     
  172.     /**
  173.      * Read a specific session
  174.      * 
  175.      * @param int $id Session Id
  176.      * @return string 
  177.      */
  178.     public function read($id)
  179.     {
  180.         // Read data
  181.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  182.             // In table storage
  183.             try
  184.             {
  185.                 $sessionRecord $this->_storage->retrieveEntityById(
  186.                     $this->_sessionContainer,
  187.                     $this->_sessionContainerPartition,
  188.                     $id
  189.                 );
  190.                 return unserialize(base64_decode($sessionRecord->serializedData));
  191.             }
  192.             catch (Microsoft_WindowsAzure_Exception $ex)
  193.             {
  194.                 return '';
  195.             }
  196.            else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  197.             // In blob storage
  198.             try
  199.             {
  200.                 $data $this->_storage->getBlobData(
  201.                     $this->_sessionContainer,
  202.                     $this->_sessionContainerPartition . '/' $id
  203.                 );
  204.                 return unserialize(base64_decode($data));
  205.             }
  206.             catch (Microsoft_WindowsAzure_Exception $ex)
  207.             {
  208.                 return false;
  209.             }
  210.         }
  211.     }
  212.     
  213.     /**
  214.      * Write a specific session
  215.      * 
  216.      * @param int $id Session Id
  217.      * @param string $serializedData Serialized PHP object
  218.      * @throws Exception
  219.      */
  220.     public function write($id$serializedData)
  221.     {
  222.         // Encode data
  223.         $serializedData base64_encode(serialize($serializedData));
  224.         if (strlen($serializedData>= self::MAX_TS_PROPERTY_SIZE && $this->_storageType == self::STORAGE_TYPE_TABLE{
  225.             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.');
  226.         }
  227.         
  228.         // Store data
  229.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  230.             // In table storage
  231.                $sessionRecord new Microsoft_WindowsAzure_Storage_DynamicTableEntity($this->_sessionContainerPartition$id);
  232.             $sessionRecord->sessionExpires time();
  233.             $sessionRecord->serializedData $serializedData;
  234.             
  235.             $sessionRecord->setAzurePropertyType('sessionExpires''Edm.Int32');
  236.     
  237.             try
  238.             {
  239.                 $this->_storage->updateEntity($this->_sessionContainer$sessionRecord);
  240.             }
  241.             catch (Microsoft_WindowsAzure_Exception $unknownRecord)
  242.             {
  243.                 $this->_storage->insertEntity($this->_sessionContainer$sessionRecord);
  244.             }
  245.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  246.             // In blob storage
  247.             $this->_storage->putBlobData(
  248.                 $this->_sessionContainer,
  249.                 $this->_sessionContainerPartition . '/' $id,
  250.                 $serializedData,
  251.                 array('sessionexpires' => time())
  252.             );
  253.         }
  254.     }
  255.     
  256.     /**
  257.      * Destroy a specific session
  258.      * 
  259.      * @param int $id Session Id
  260.      * @return boolean 
  261.      */
  262.     public function destroy($id)
  263.     {
  264.         // Destroy data
  265.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  266.             // In table storage
  267.                try
  268.             {
  269.                 $sessionRecord $this->_storage->retrieveEntityById(
  270.                     $this->_sessionContainer,
  271.                     $this->_sessionContainerPartition,
  272.                     $id
  273.                 );
  274.                 $this->_storage->deleteEntity($this->_sessionContainer$sessionRecord);
  275.                 
  276.                 return true;
  277.             }
  278.             catch (Microsoft_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.                 $this->_storage->deleteBlob(
  287.                     $this->_sessionContainer,
  288.                     $this->_sessionContainerPartition . '/' $id
  289.                 );
  290.                 
  291.                 return true;
  292.             }
  293.             catch (Microsoft_WindowsAzure_Exception $ex)
  294.             {
  295.                 return false;
  296.             }
  297.         }
  298.     }
  299.     
  300.     /**
  301.      * Garbage collector
  302.      * 
  303.      * @param int $lifeTime Session maximal lifetime
  304.      * @see session.gc_divisor  100
  305.      * @see session.gc_maxlifetime 1440
  306.      * @see session.gc_probability 1
  307.      * @usage Execution rate 1/100 (session.gc_probability/session.gc_divisor)
  308.      * @return boolean 
  309.      */
  310.     public function gc($lifeTime)
  311.     {
  312.            if ($this->_storageType == self::STORAGE_TYPE_TABLE{
  313.             // In table storage
  314.                try
  315.             {
  316.                 $result $this->_storage->retrieveEntities($this->_sessionContainer'PartitionKey eq \'' $this->_sessionContainerPartition . '\' and sessionExpires lt ' (time($lifeTime));
  317.                 foreach ($result as $sessionRecord)
  318.                 {
  319.                     $this->_storage->deleteEntity($this->_sessionContainer$sessionRecord);
  320.                 }
  321.                 return true;
  322.             }
  323.             catch (Microsoft_WindowsAzure_exception $ex)
  324.             {
  325.                 return false;
  326.             }
  327.         else if ($this->_storageType == self::STORAGE_TYPE_BLOB{
  328.             // In blob storage
  329.             try
  330.             {
  331.                 $result $this->_storage->listBlobs($this->_sessionContainer$this->_sessionContainerPartition''nullnull'metadata');
  332.                 foreach ($result as $sessionRecord)
  333.                 {
  334.                     if ($sessionRecord->Metadata['sessionexpires'(time($lifeTime)) {
  335.                         $this->_storage->deleteBlob($this->_sessionContainer$sessionRecord->Name);
  336.                     }
  337.                 }
  338.                 return true;
  339.             }
  340.             catch (Microsoft_WindowsAzure_exception $ex)
  341.             {
  342.                 return false;
  343.             }
  344.         }
  345.     }
  346. }

Documentation generated on Sat, 03 Dec 2011 13:59:36 +0100 by phpDocumentor 1.4.3