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

Source for file Storage.php

Documentation is available at Storage.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 Storage
  31.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  32.  * @license    http://phpazure.codeplex.com/license
  33.  * @version    $Id: Storage.php 28585 2009-09-07 12:12:56Z unknown $
  34.  */
  35.  
  36. /**
  37.  * @see Microsoft_WindowsAzure_Credentials
  38.  */
  39. require_once 'Microsoft/WindowsAzure/Credentials.php';
  40.  
  41. /**
  42.  * @see Microsoft_WindowsAzure_SharedKeyCredentials
  43.  */
  44. require_once 'Microsoft/WindowsAzure/SharedKeyCredentials.php';
  45.  
  46. /**
  47.  * @see Microsoft_WindowsAzure_RetryPolicy
  48.  */
  49. require_once 'Microsoft/WindowsAzure/RetryPolicy.php';
  50.  
  51. /**
  52.  * @see Microsoft_WindowsAzure_Exception
  53.  */
  54. require_once 'Microsoft/WindowsAzure/Exception.php';
  55.  
  56. /**
  57.  * @see Microsoft_Http_Transport
  58.  */
  59. require_once 'Microsoft/Http/Transport.php';
  60.  
  61. /**
  62.  * @see Microsoft_Http_Response
  63.  */
  64. require_once 'Microsoft/Http/Response.php';
  65.  
  66. /**
  67.  * @category   Microsoft
  68.  * @package    Microsoft_WindowsAzure
  69.  * @subpackage Storage
  70.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  71.  * @license    http://phpazure.codeplex.com/license
  72.  */
  73. {
  74.     /**
  75.      * Development storage URLS
  76.      */
  77.     const URL_DEV_BLOB      "127.0.0.1:10000";
  78.     const URL_DEV_QUEUE     "127.0.0.1:10001";
  79.     const URL_DEV_TABLE     "127.0.0.1:10002";
  80.     
  81.     /**
  82.      * Live storage URLS
  83.      */
  84.     const URL_CLOUD_BLOB    "blob.core.windows.net";
  85.     const URL_CLOUD_QUEUE   "queue.core.windows.net";
  86.     const URL_CLOUD_TABLE   "table.core.windows.net";
  87.     
  88.     /**
  89.      * Resource types
  90.      */
  91.     const RESOURCE_UNKNOWN     "unknown";
  92.     const RESOURCE_CONTAINER   "c";
  93.     const RESOURCE_BLOB        "b";
  94.     const RESOURCE_TABLE       "t";
  95.     const RESOURCE_ENTITY      "e";
  96.     const RESOURCE_QUEUE       "q";
  97.     
  98.     /**
  99.      * Current API version
  100.      * 
  101.      * @var string 
  102.      */
  103.     protected $_apiVersion = '2009-04-14';
  104.     
  105.     /**
  106.      * Storage host name
  107.      *
  108.      * @var string 
  109.      */
  110.     protected $_host = '';
  111.     
  112.     /**
  113.      * Account name for Windows Azure
  114.      *
  115.      * @var string 
  116.      */
  117.     protected $_accountName = '';
  118.     
  119.     /**
  120.      * Account key for Windows Azure
  121.      *
  122.      * @var string 
  123.      */
  124.     protected $_accountKey = '';
  125.     
  126.     /**
  127.      * Use path-style URI's
  128.      *
  129.      * @var boolean 
  130.      */
  131.     protected $_usePathStyleUri = false;
  132.     
  133.     /**
  134.      * Microsoft_WindowsAzure_Credentials instance
  135.      *
  136.      * @var Microsoft_WindowsAzure_Credentials 
  137.      */
  138.     protected $_credentials = null;
  139.     
  140.     /**
  141.      * Microsoft_WindowsAzure_RetryPolicy instance
  142.      * 
  143.      * @var Microsoft_WindowsAzure_RetryPolicy 
  144.      */
  145.     protected $_retryPolicy = null;
  146.     
  147.     /**
  148.      * Use proxy?
  149.      * 
  150.      * @var boolean 
  151.      */
  152.     protected $_useProxy = false;
  153.     
  154.     /**
  155.      * Proxy url
  156.      * 
  157.      * @var string 
  158.      */
  159.     protected $_proxyUrl = '';
  160.     
  161.     /**
  162.      * Proxy port
  163.      * 
  164.      * @var int 
  165.      */
  166.     protected $_proxyPort = 80;
  167.     
  168.     /**
  169.      * Proxy credentials
  170.      * 
  171.      * @var string 
  172.      */
  173.     protected $_proxyCredentials = '';
  174.     
  175.     /**
  176.      * Creates a new Microsoft_WindowsAzure_Storage instance
  177.      *
  178.      * @param string $host Storage host name
  179.      * @param string $accountName Account name for Windows Azure
  180.      * @param string $accountKey Account key for Windows Azure
  181.      * @param boolean $usePathStyleUri Use path-style URI's
  182.      * @param Microsoft_WindowsAzure_RetryPolicy $retryPolicy Retry policy to use when making requests
  183.      */
  184.     public function __construct($host self::URL_DEV_BLOB$accountName Microsoft_WindowsAzure_Credentials::DEVSTORE_ACCOUNT$accountKey Microsoft_WindowsAzure_Credentials::DEVSTORE_KEY$usePathStyleUri falseMicrosoft_WindowsAzure_RetryPolicy $retryPolicy null)
  185.     {
  186.         $this->_host = $host;
  187.         $this->_accountName = $accountName;
  188.         $this->_accountKey = $accountKey;
  189.         $this->_usePathStyleUri = $usePathStyleUri;
  190.         
  191.         // Using local storage?
  192.         if (!$this->_usePathStyleUri && ($this->_host == self::URL_DEV_BLOB || $this->_host == self::URL_DEV_QUEUE || $this->_host == self::URL_DEV_TABLE)) // Local storage
  193.             $this->_usePathStyleUri = true;
  194.         
  195.         if (is_null($this->_credentials))
  196.             $this->_credentials = new Microsoft_WindowsAzure_SharedKeyCredentials($this->_accountName$this->_accountKey$this->_usePathStyleUri);
  197.         
  198.         $this->_retryPolicy = $retryPolicy;
  199.         if (is_null($this->_retryPolicy))
  200.             $this->_retryPolicy = Microsoft_WindowsAzure_RetryPolicy::noRetry();
  201.     }
  202.     
  203.     /**
  204.      * Set retry policy to use when making requests
  205.      *
  206.      * @param Microsoft_WindowsAzure_RetryPolicy $retryPolicy Retry policy to use when making requests
  207.      */
  208.     public function setRetryPolicy(Microsoft_WindowsAzure_RetryPolicy $retryPolicy null)
  209.     {
  210.         $this->_retryPolicy = $retryPolicy;
  211.         if (is_null($this->_retryPolicy))
  212.             $this->_retryPolicy = Microsoft_WindowsAzure_RetryPolicy::noRetry();
  213.     }
  214.     
  215.     /**
  216.      * Set proxy
  217.      * 
  218.      * @param boolean $useProxy         Use proxy?
  219.      * @param string  $proxyUrl         Proxy URL
  220.      * @param int     $proxyPort        Proxy port
  221.      * @param string  $proxyCredentials Proxy credentials
  222.      */
  223.     public function setProxy($useProxy false$proxyUrl ''$proxyPort 80$proxyCredentials '')
  224.     {
  225.         $this->_useProxy = $useProxy;
  226.         $this->_proxyUrl = $proxyUrl;
  227.         $this->_proxyPort = $proxyPort;
  228.         $this->_proxyCredentials = $proxyCredentials;
  229.     }
  230.     
  231.     /**
  232.      * Returns the Windows Azure account name
  233.      * 
  234.      * @return string 
  235.      */
  236.     public function getAccountName()
  237.     {
  238.         return $this->_accountName;
  239.     }
  240.     
  241.     /**
  242.      * Get base URL for creating requests
  243.      *
  244.      * @return string 
  245.      */
  246.     public function getBaseUrl()
  247.     {
  248.         if ($this->_usePathStyleUri)
  249.             return 'http://' $this->_host . '/' $this->_accountName;
  250.         else
  251.             return 'http://' $this->_accountName . '.' $this->_host;
  252.     }
  253.     
  254.     /**
  255.      * Set Microsoft_WindowsAzure_Credentials instance
  256.      * 
  257.      * @param Microsoft_WindowsAzure_Credentials $credentials Microsoft_WindowsAzure_Credentials instance to use for request signing.
  258.      */
  259.     public function setCredentials(Microsoft_WindowsAzure_Credentials $credentials)
  260.     {
  261.         $this->_credentials = $credentials;
  262.         $this->_credentials->setAccountName($this->_accountName);
  263.         $this->_credentials->setAccountkey($this->_accountKey);
  264.         $this->_credentials->setUsePathStyleUri($this->_usePathStyleUri);
  265.     }
  266.     
  267.     /**
  268.      * Get Microsoft_WindowsAzure_Credentials instance
  269.      * 
  270.      * @return Microsoft_WindowsAzure_Credentials 
  271.      */
  272.     public function getCredentials()
  273.     {
  274.         return $this->_credentials;
  275.     }
  276.     
  277.     /**
  278.      * Perform request using Microsoft_Http_Transport channel
  279.      *
  280.      * @param string $path Path
  281.      * @param string $queryString Query string
  282.      * @param string $httpVerb HTTP verb the request will use
  283.      * @param array $headers x-ms headers to add
  284.      * @param boolean $forTableStorage Is the request for table storage?
  285.      * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  286.      * @param string $resourceType Resource type
  287.      * @param string $requiredPermission Required permission
  288.      * @return Microsoft_Http_Response 
  289.      */
  290.     protected function performRequest($path '/'$queryString ''$httpVerb Microsoft_Http_Transport::VERB_GET$headers array()$forTableStorage false$rawData null$resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN$requiredPermission Microsoft_WindowsAzure_Credentials::PERMISSION_READ)
  291.     {
  292.         // Clean path
  293.         if (strpos($path'/'!== 0
  294.             $path '/' $path;
  295.             
  296.         // Clean headers
  297.         if (is_null($headers))
  298.             $headers array();
  299.             
  300.         // Add version header
  301.         $headers['x-ms-version'$this->_apiVersion;
  302.             
  303.         // URL encoding
  304.         $path           self::urlencode($path);
  305.         $queryString    self::urlencode($queryString);
  306.  
  307.         // Generate URL and sign request
  308.         $requestUrl     $this->_credentials->signRequestUrl($this->getBaseUrl($path $queryString$resourceType$requiredPermission);
  309.         $requestHeaders $this->_credentials->signRequestHeaders($httpVerb$path$queryString$headers$forTableStorage$resourceType$requiredPermission);
  310.  
  311.         $requestClient  Microsoft_Http_Transport::createChannel();
  312.         if ($this->_useProxy)
  313.         {
  314.             $requestClient->setProxy($this->_useProxy$this->_proxyUrl$this->_proxyPort$this->_proxyCredentials);
  315.         }
  316.         $response $this->_retryPolicy->execute(
  317.             array($requestClient'request'),
  318.             array($httpVerb$requestUrlarray()$requestHeaders$rawData)
  319.         );
  320.         
  321.         $requestClient null;
  322.         unset($requestClient);
  323.         
  324.         return $response;
  325.     }
  326.     
  327.     /** 
  328.      * Parse result from Microsoft_Http_Response
  329.      *
  330.      * @param Microsoft_Http_Response $response Response from HTTP call
  331.      * @return object 
  332.      * @throws Microsoft_WindowsAzure_Exception
  333.      */
  334.     protected function parseResponse(Microsoft_Http_Response $response null)
  335.     {
  336.         if (is_null($response))
  337.             throw new Microsoft_WindowsAzure_Exception('Response should not be null.');
  338.         
  339.         $xml @simplexml_load_string($response->getBody());
  340.         
  341.         if ($xml !== false)
  342.         {
  343.             // Fetch all namespaces 
  344.             $namespaces array_merge($xml->getNamespaces(true)$xml->getDocNamespaces(true))
  345.             
  346.             // Register all namespace prefixes
  347.             foreach ($namespaces as $prefix => $ns
  348.                 if ($prefix != '')
  349.                     $xml->registerXPathNamespace($prefix$ns)
  350.             
  351.         }
  352.         
  353.         return $xml;
  354.     }
  355.     
  356.     /**
  357.      * Generate ISO 8601 compliant date string in UTC time zone
  358.      * 
  359.      * @param int $timestamp 
  360.      * @return string 
  361.      */
  362.     public function isoDate($timestamp null
  363.     {        
  364.         $tz @date_default_timezone_get();
  365.         @date_default_timezone_set('UTC');
  366.         
  367.         if (is_null($timestamp))
  368.             $timestamp time();
  369.             
  370.         $returnValue str_replace('+00:00''.0000000Z'@date('c'$timestamp));
  371.         @date_default_timezone_set($tz);
  372.         return $returnValue;
  373.     }
  374.     
  375.     /**
  376.      * URL encode function
  377.      * 
  378.      * @param  string $value Value to encode
  379.      * @return string        Encoded value
  380.      */
  381.     public static function urlencode($value)
  382.     {
  383.         return str_replace(' ''%20'$value);
  384.     }
  385. }

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