web/lib/Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Service_WindowsAzure
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: CredentialsAbstract.php 22773 2010-08-03 07:18:27Z maartenba $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Http_Client
       
    24  */
       
    25 require_once 'Zend/Http/Client.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Service_WindowsAzure_Credentials_Exception
       
    29  */
       
    30 require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
       
    31 
       
    32 /**
       
    33  * @category   Zend
       
    34  * @package    Zend_Service_WindowsAzure
       
    35  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    37  */ 
       
    38 abstract class Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
       
    39 {
       
    40 	/**
       
    41 	 * Development storage account and key
       
    42 	 */
       
    43 	const DEVSTORE_ACCOUNT       = "devstoreaccount1";
       
    44 	const DEVSTORE_KEY           = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
       
    45 	
       
    46 	/**
       
    47 	 * HTTP header prefixes
       
    48 	 */
       
    49 	const PREFIX_PROPERTIES      = "x-ms-prop-";
       
    50 	const PREFIX_METADATA        = "x-ms-meta-";
       
    51 	const PREFIX_STORAGE_HEADER  = "x-ms-";
       
    52 	
       
    53 	/**
       
    54 	 * Permissions
       
    55 	 */
       
    56 	const PERMISSION_READ        = "r";
       
    57 	const PERMISSION_WRITE       = "w";
       
    58 	const PERMISSION_DELETE      = "d";
       
    59 	const PERMISSION_LIST        = "l";
       
    60 
       
    61 	/**
       
    62 	 * Account name for Windows Azure
       
    63 	 *
       
    64 	 * @var string
       
    65 	 */
       
    66 	protected $_accountName = '';
       
    67 	
       
    68 	/**
       
    69 	 * Account key for Windows Azure
       
    70 	 *
       
    71 	 * @var string
       
    72 	 */
       
    73 	protected $_accountKey = '';
       
    74 	
       
    75 	/**
       
    76 	 * Use path-style URI's
       
    77 	 *
       
    78 	 * @var boolean
       
    79 	 */
       
    80 	protected $_usePathStyleUri = false;
       
    81 	
       
    82 	/**
       
    83 	 * Creates a new Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
       
    84 	 *
       
    85 	 * @param string $accountName Account name for Windows Azure
       
    86 	 * @param string $accountKey Account key for Windows Azure
       
    87 	 * @param boolean $usePathStyleUri Use path-style URI's
       
    88 	 */
       
    89 	public function __construct(
       
    90 		$accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
       
    91 		$accountKey  = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
       
    92 		$usePathStyleUri = false
       
    93 	) {
       
    94 		$this->_accountName = $accountName;
       
    95 		$this->_accountKey = base64_decode($accountKey);
       
    96 		$this->_usePathStyleUri = $usePathStyleUri;
       
    97 	}
       
    98 	
       
    99 	/**
       
   100 	 * Set account name for Windows Azure
       
   101 	 *
       
   102 	 * @param  string $value
       
   103 	 * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
       
   104 	 */
       
   105 	public function setAccountName($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT)
       
   106 	{
       
   107 		$this->_accountName = $value;
       
   108 		return $this;
       
   109 	}
       
   110 	
       
   111 	/**
       
   112 	 * Set account key for Windows Azure
       
   113 	 *
       
   114 	 * @param  string $value
       
   115 	 * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
       
   116 	 */
       
   117 	public function setAccountkey($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY)
       
   118 	{
       
   119 		$this->_accountKey = base64_decode($value);
       
   120 		return $this;
       
   121 	}
       
   122 	
       
   123 	/**
       
   124 	 * Set use path-style URI's
       
   125 	 *
       
   126 	 * @param  boolean $value
       
   127 	 * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
       
   128 	 */
       
   129 	public function setUsePathStyleUri($value = false)
       
   130 	{
       
   131 		$this->_usePathStyleUri = $value;
       
   132 		return $this;
       
   133 	}
       
   134 	
       
   135 	/**
       
   136 	 * Sign request URL with credentials
       
   137 	 *
       
   138 	 * @param string $requestUrl Request URL
       
   139 	 * @param string $resourceType Resource type
       
   140 	 * @param string $requiredPermission Required permission
       
   141 	 * @return string Signed request URL
       
   142 	 */
       
   143 	abstract public function signRequestUrl(
       
   144 		$requestUrl = '',
       
   145 		$resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
       
   146 		$requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
       
   147 	);
       
   148 	
       
   149 	/**
       
   150 	 * Sign request headers with credentials
       
   151 	 *
       
   152 	 * @param string $httpVerb HTTP verb the request will use
       
   153 	 * @param string $path Path for the request
       
   154 	 * @param string $queryString Query string for the request
       
   155 	 * @param array $headers x-ms headers to add
       
   156 	 * @param boolean $forTableStorage Is the request for table storage?
       
   157 	 * @param string $resourceType Resource type
       
   158 	 * @param string $requiredPermission Required permission
       
   159 	 * @param mixed  $rawData Raw post data
       
   160 	 * @return array Array of headers
       
   161 	 */
       
   162 	abstract public function signRequestHeaders(
       
   163 		$httpVerb = Zend_Http_Client::GET,
       
   164 		$path = '/',
       
   165 		$queryString = '',
       
   166 		$headers = null,
       
   167 		$forTableStorage = false,
       
   168 		$resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
       
   169 		$requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
       
   170 		$rawData = null
       
   171 	);
       
   172 	
       
   173 	
       
   174 	/**
       
   175 	 * Prepare query string for signing
       
   176 	 * 
       
   177 	 * @param  string $value Original query string
       
   178 	 * @return string        Query string for signing
       
   179 	 */
       
   180 	protected function _prepareQueryStringForSigning($value)
       
   181 	{
       
   182 	    // Return value
       
   183 	    $returnValue = array();
       
   184 	    
       
   185 	    // Prepare query string
       
   186 	    $queryParts = $this->_makeArrayOfQueryString($value);
       
   187 	    foreach ($queryParts as $key => $value) {
       
   188 	    	$returnValue[] = $key . '=' . $value;
       
   189 	    }
       
   190 	    
       
   191 	    // Return
       
   192 	    if (count($returnValue) > 0) {
       
   193 	    	return '?' . implode('&', $returnValue);
       
   194 	    } else {
       
   195 	    	return '';
       
   196 	    }
       
   197 	}
       
   198 	
       
   199 	/**
       
   200 	 * Make array of query string
       
   201 	 * 
       
   202 	 * @param  string $value Query string
       
   203 	 * @return array         Array of key/value pairs
       
   204 	 */
       
   205 	protected function _makeArrayOfQueryString($value)
       
   206 	{
       
   207 		// Returnvalue
       
   208 		$returnValue = array();
       
   209 		
       
   210 	    // Remove front ?     
       
   211    		if (strlen($value) > 0 && strpos($value, '?') === 0) {
       
   212     		$value = substr($value, 1);
       
   213     	}
       
   214     		
       
   215     	// Split parts
       
   216     	$queryParts = explode('&', $value);
       
   217     	foreach ($queryParts as $queryPart) {
       
   218     		$queryPart = explode('=', $queryPart, 2);
       
   219     		
       
   220     		if ($queryPart[0] != '') {
       
   221     			$returnValue[ $queryPart[0] ] = isset($queryPart[1]) ? $queryPart[1] : '';
       
   222     		}
       
   223     	}
       
   224     	
       
   225     	// Sort
       
   226     	ksort($returnValue);
       
   227 
       
   228     	// Return
       
   229 		return $returnValue;
       
   230 	}
       
   231 	
       
   232 	/**
       
   233 	 * Returns an array value if the key is set, otherwide returns $valueIfNotSet
       
   234 	 * 
       
   235 	 * @param array $array
       
   236 	 * @param mixed $key
       
   237 	 * @param mixed $valueIfNotSet
       
   238 	 * @return mixed
       
   239 	 */
       
   240 	protected function _issetOr($array, $key, $valueIfNotSet)
       
   241 	{
       
   242 		return isset($array[$key]) ? $array[$key] : $valueIfNotSet;
       
   243 	}
       
   244 }