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

Source for file CredentialsAbstract.php

Documentation is available at CredentialsAbstract.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.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  31.  * @license    http://phpazure.codeplex.com/license
  32.  * @version    $Id: SharedKeyCredentials.php 14561 2009-05-07 08:05:12Z unknown $
  33.  */
  34.  
  35. /**
  36.  * @see Microsoft_AutoLoader
  37.  */
  38. require_once dirname(__FILE__'/../../AutoLoader.php';
  39.  
  40. /**
  41.  * @category   Microsoft
  42.  * @package    Microsoft_WindowsAzure
  43.  * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
  44.  * @license    http://phpazure.codeplex.com/license
  45.  */ 
  46. {
  47.     /**
  48.      * Development storage account and key
  49.      */
  50.     const DEVSTORE_ACCOUNT       "devstoreaccount1";
  51.     const DEVSTORE_KEY           "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
  52.     
  53.     /**
  54.      * HTTP header prefixes
  55.      */
  56.     const PREFIX_PROPERTIES      "x-ms-prop-";
  57.     const PREFIX_METADATA        "x-ms-meta-";
  58.     const PREFIX_STORAGE_HEADER  "x-ms-";
  59.     
  60.     /**
  61.      * Permissions
  62.      */
  63.     const PERMISSION_READ        "r";
  64.     const PERMISSION_WRITE       "w";
  65.     const PERMISSION_DELETE      "d";
  66.     const PERMISSION_LIST        "l";
  67.  
  68.     /**
  69.      * Account name for Windows Azure
  70.      *
  71.      * @var string 
  72.      */
  73.     protected $_accountName = '';
  74.     
  75.     /**
  76.      * Account key for Windows Azure
  77.      *
  78.      * @var string 
  79.      */
  80.     protected $_accountKey = '';
  81.     
  82.     /**
  83.      * Use path-style URI's
  84.      *
  85.      * @var boolean 
  86.      */
  87.     protected $_usePathStyleUri = false;
  88.     
  89.     /**
  90.      * Creates a new Microsoft_WindowsAzure_Credentials_CredentialsAbstract instance
  91.      *
  92.      * @param string $accountName Account name for Windows Azure
  93.      * @param string $accountKey Account key for Windows Azure
  94.      * @param boolean $usePathStyleUri Use path-style URI's
  95.      */
  96.     public function __construct(
  97.         $accountName Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
  98.         $accountKey  Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
  99.         $usePathStyleUri false
  100.     {
  101.         $this->_accountName = $accountName;
  102.         $this->_accountKey = base64_decode($accountKey);
  103.         $this->_usePathStyleUri = $usePathStyleUri;
  104.     }
  105.     
  106.     /**
  107.      * Set account name for Windows Azure
  108.      *
  109.      * @param  string $value 
  110.      * @return Microsoft_WindowsAzure_Credentials_CredentialsAbstract 
  111.      */
  112.     public function setAccountName($value Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT)
  113.     {
  114.         $this->_accountName = $value;
  115.         return $this;
  116.     }
  117.     
  118.     /**
  119.      * Set account key for Windows Azure
  120.      *
  121.      * @param  string $value 
  122.      * @return Microsoft_WindowsAzure_Credentials_CredentialsAbstract 
  123.      */
  124.     public function setAccountkey($value Microsoft_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY)
  125.     {
  126.         $this->_accountKey = base64_decode($value);
  127.         return $this;
  128.     }
  129.     
  130.     /**
  131.      * Set use path-style URI's
  132.      *
  133.      * @param  boolean $value 
  134.      * @return Microsoft_WindowsAzure_Credentials_CredentialsAbstract 
  135.      */
  136.     public function setUsePathStyleUri($value false)
  137.     {
  138.         $this->_usePathStyleUri = $value;
  139.         return $this;
  140.     }
  141.     
  142.     /**
  143.      * Sign request URL with credentials
  144.      *
  145.      * @param string $requestUrl Request URL
  146.      * @param string $resourceType Resource type
  147.      * @param string $requiredPermission Required permission
  148.      * @return string Signed request URL
  149.      */
  150.     abstract public function signRequestUrl(
  151.         $requestUrl '',
  152.         $resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  153.         $requiredPermission Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  154.     );
  155.     
  156.     /**
  157.      * Sign request headers with credentials
  158.      *
  159.      * @param string $httpVerb HTTP verb the request will use
  160.      * @param string $path Path for the request
  161.      * @param string $queryString Query string for the request
  162.      * @param array $headers x-ms headers to add
  163.      * @param boolean $forTableStorage Is the request for table storage?
  164.      * @param string $resourceType Resource type
  165.      * @param string $requiredPermission Required permission
  166.      * @param mixed  $rawData Raw post data
  167.      * @return array Array of headers
  168.      */
  169.     abstract public function signRequestHeaders(
  170.         $httpVerb Microsoft_Http_Client::GET,
  171.         $path '/',
  172.         $queryString '',
  173.         $headers null,
  174.         $forTableStorage false,
  175.         $resourceType Microsoft_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  176.         $requiredPermission Microsoft_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  177.         $rawData null
  178.     );
  179.     
  180.     
  181.     /**
  182.      * Prepare query string for signing
  183.      * 
  184.      * @param  string $value Original query string
  185.      * @return string        Query string for signing
  186.      */
  187.     protected function _prepareQueryStringForSigning($value)
  188.     {
  189.         // Return value
  190.         $returnValue array();
  191.         
  192.         // Prepare query string
  193.         $queryParts $this->_makeArrayOfQueryString($value);
  194.         foreach ($queryParts as $key => $value{
  195.             $returnValue[$key '=' $value;
  196.         }
  197.         
  198.         // Return
  199.         if (count($returnValue0{
  200.             return '?' implode('&'$returnValue);
  201.         else {
  202.             return '';
  203.         }
  204.     }
  205.     
  206.     /**
  207.      * Make array of query string
  208.      * 
  209.      * @param  string $value Query string
  210.      * @return array         Array of key/value pairs
  211.      */
  212.     protected function _makeArrayOfQueryString($value)
  213.     {
  214.         // Returnvalue
  215.         $returnValue array();
  216.         
  217.         // Remove front ?     
  218.            if (strlen($value&& strpos($value'?'=== 0{
  219.             $value substr($value1);
  220.         }
  221.             
  222.         // Split parts
  223.         $queryParts explode('&'$value);
  224.         foreach ($queryParts as $queryPart{
  225.             $queryPart explode('='$queryPart2);
  226.             
  227.             if ($queryPart[0!= ''{
  228.                 $returnValue$queryPart[0] ] = isset($queryPart[1]$queryPart[1'';
  229.             }
  230.         }
  231.         
  232.         // Sort
  233.         ksort($returnValue);
  234.  
  235.         // Return
  236.         return $returnValue;
  237.     }
  238.     
  239.     /**
  240.      * Returns an array value if the key is set, otherwide returns $valueIfNotSet
  241.      * 
  242.      * @param array $array 
  243.      * @param mixed $key 
  244.      * @param mixed $valueIfNotSet 
  245.      * @return mixed 
  246.      */
  247.     protected function _issetOr($array$key$valueIfNotSet)
  248.     {
  249.         return isset($array[$key]$array[$key$valueIfNotSet;
  250.     }
  251. }

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