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, 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, 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. /** Microsoft_WindowsAzure_Storage_Table */
  37. require_once 'Microsoft/WindowsAzure/Storage/Table.php';
  38.  
  39. /**
  40.  * @see Microsoft_WindowsAzure_Exception
  41.  */
  42. require_once 'Microsoft/WindowsAzure/Exception.php';
  43.  
  44. /**
  45.  * @category   Microsoft
  46.  * @package    Microsoft_WindowsAzure
  47.  * @subpackage Session
  48.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  49.  * @license    http://phpazure.codeplex.com/license
  50.  */
  51. {
  52.     /**
  53.      * Table storage
  54.      * 
  55.      * @var Microsoft_WindowsAzure_Storage_Table 
  56.      */
  57.     protected $_tableStorage;
  58.     
  59.     /**
  60.      * Session table name
  61.      * 
  62.      * @var string 
  63.      */
  64.     protected $_sessionTable;
  65.     
  66.     /**
  67.      * Session table partition
  68.      * 
  69.      * @var string 
  70.      */
  71.     protected $_sessionTablePartition;
  72.     
  73.     /**
  74.      * Creates a new Microsoft_WindowsAzure_SessionHandler instance
  75.      * 
  76.      * @param Microsoft_WindowsAzure_Storage_Table $tableStorage Table storage
  77.      * @param string $sessionTable Session table name
  78.      * @param string $sessionTablePartition Session table partition
  79.      */
  80.     public function __construct(Microsoft_WindowsAzure_Storage_Table $tableStorage$sessionTable 'php-sessions'$sessionTablePartition 'sessions')
  81.     {
  82.         // Set properties
  83.         $this->_tableStorage = $tableStorage;
  84.         $this->_sessionTable = $sessionTable;
  85.         $this->_sessionTablePartition = $sessionTablePartition;
  86.     }
  87.     
  88.     /**
  89.      * Registers the current session handler as PHP's session handler
  90.      * 
  91.      * @return boolean 
  92.      */
  93.     public function register()
  94.     {
  95.         return session_set_save_handler(array($this'open'),
  96.                                         array($this'close'),
  97.                                         array($this'read'),
  98.                                         array($this'write'),
  99.                                         array($this'destroy'),
  100.                                         array($this'gc')
  101.         );
  102.     }
  103.     
  104.     /**
  105.      * Open the session store
  106.      * 
  107.      * @return bool 
  108.      */
  109.     public function open()
  110.     {
  111.         // Make sure table exists
  112.         $tableExists $this->_tableStorage->tableExists($this->_sessionTable);
  113.         if (!$tableExists)
  114.         {
  115.             $this->_tableStorage->createTable($this->_sessionTable);
  116.         }
  117.         
  118.         // Ok!
  119.         return true;
  120.     }
  121.  
  122.     /**
  123.      * Close the session store
  124.      * 
  125.      * @return bool 
  126.      */
  127.     public function close()
  128.     {
  129.         return true;
  130.     }
  131.     
  132.     /**
  133.      * Read a specific session
  134.      * 
  135.      * @param int $id Session Id
  136.      * @return string 
  137.      */
  138.     public function read($id)
  139.     {
  140.         try
  141.         {
  142.             $sessionRecord $this->_tableStorage->retrieveEntityById(
  143.                 $this->_sessionTable,
  144.                 $this->_sessionTablePartition,
  145.                 $id
  146.             );
  147.             return base64_decode($sessionRecord->serializedData);
  148.         }
  149.         catch (Microsoft_WindowsAzure_Exception $ex)
  150.         {
  151.             return '';
  152.         }
  153.     }
  154.     
  155.     /**
  156.      * Write a specific session
  157.      * 
  158.      * @param int $id Session Id
  159.      * @param string $serializedData Serialized PHP object
  160.      */
  161.     public function write($id$serializedData)
  162.     {
  163.         $sessionRecord new Microsoft_WindowsAzure_Storage_DynamicTableEntity($this->_sessionTablePartition$id);
  164.         $sessionRecord->sessionExpires time();
  165.         $sessionRecord->serializedData base64_encode($serializedData);
  166.         
  167.         $sessionRecord->setAzurePropertyType('sessionExpires''Edm.Int32');
  168.  
  169.         try
  170.         {
  171.             $this->_tableStorage->updateEntity($this->_sessionTable$sessionRecord);
  172.         }
  173.         catch (Microsoft_WindowsAzure_Exception $unknownRecord)
  174.         {
  175.             $this->_tableStorage->insertEntity($this->_sessionTable$sessionRecord);
  176.         }
  177.     }
  178.     
  179.     /**
  180.      * Destroy a specific session
  181.      * 
  182.      * @param int $id Session Id
  183.      * @return boolean 
  184.      */
  185.     public function destroy($id)
  186.     {
  187.         try
  188.         {
  189.             $sessionRecord $this->_tableStorage->retrieveEntityById(
  190.                 $this->_sessionTable,
  191.                 $this->_sessionTablePartition,
  192.                 $id
  193.             );
  194.             $this->_tableStorage->deleteEntity($this->_sessionTable$sessionRecord);
  195.             
  196.             return true;
  197.         }
  198.         catch (Microsoft_WindowsAzure_Exception $ex)
  199.         {
  200.             return false;
  201.         }
  202.     }
  203.     
  204.     /**
  205.      * Garbage collector
  206.      * 
  207.      * @param int $lifeTime Session maximal lifetime
  208.      * @see session.gc_divisor  100
  209.      * @see session.gc_maxlifetime 1440
  210.      * @see session.gc_probability 1
  211.      * @usage Execution rate 1/100 (session.gc_probability/session.gc_divisor)
  212.      * @return boolean 
  213.      */
  214.     public function gc($lifeTime)
  215.     {
  216.         try
  217.         {
  218.             $result $this->_tableStorage->retrieveEntities($this->_sessionTable'PartitionKey eq \'' $this->_sessionTablePartition . '\' and sessionExpires lt ' (time($lifeTime));
  219.             foreach ($result as $sessionRecord)
  220.             {
  221.                 $this->_tableStorage->deleteEntity($this->_sessionTable$sessionRecord);
  222.             }
  223.             return true;
  224.         }
  225.         catch (Microsoft_WindowsAzure_exception $ex)
  226.         {
  227.             return false;
  228.         }
  229.     }
  230. }

Documentation generated on Thu, 26 Nov 2009 08:05:17 +0100 by phpDocumentor 1.4.3