File indexing completed on 2024-06-23 05:55:44

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Service_WindowsAzure
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 
0023 /**
0024  * @category   Zend
0025  * @package    Zend_Service_WindowsAzure
0026  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0027  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0028  */ 
0029 abstract class Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
0030 {
0031   /**
0032    * Development storage account and key
0033    */
0034   const DEVSTORE_ACCOUNT       = "devstoreaccount1";
0035   const DEVSTORE_KEY           = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
0036   
0037   /**
0038    * HTTP header prefixes
0039    */
0040   const PREFIX_PROPERTIES      = "x-ms-prop-";
0041   const PREFIX_METADATA        = "x-ms-meta-";
0042   const PREFIX_STORAGE_HEADER  = "x-ms-";
0043   
0044   /**
0045    * Permissions
0046    */
0047   const PERMISSION_READ        = "r";
0048   const PERMISSION_WRITE       = "w";
0049   const PERMISSION_DELETE      = "d";
0050   const PERMISSION_LIST        = "l";
0051 
0052   /**
0053    * Account name for Windows Azure
0054    *
0055    * @var string
0056    */
0057   protected $_accountName = '';
0058   
0059   /**
0060    * Account key for Windows Azure
0061    *
0062    * @var string
0063    */
0064   protected $_accountKey = '';
0065   
0066   /**
0067    * Use path-style URI's
0068    *
0069    * @var boolean
0070    */
0071   protected $_usePathStyleUri = false;
0072   
0073   /**
0074    * Creates a new Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
0075    *
0076    * @param string $accountName Account name for Windows Azure
0077    * @param string $accountKey Account key for Windows Azure
0078    * @param boolean $usePathStyleUri Use path-style URI's
0079    */
0080   public function __construct(
0081     $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
0082     $accountKey  = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
0083     $usePathStyleUri = false
0084   ) {
0085     $this->_accountName = $accountName;
0086     $this->_accountKey = base64_decode($accountKey);
0087     $this->_usePathStyleUri = $usePathStyleUri;
0088   }
0089   
0090   /**
0091    * Set account name for Windows Azure
0092    *
0093    * @param  string $value
0094    * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
0095    */
0096   public function setAccountName($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT)
0097   {
0098     $this->_accountName = $value;
0099     return $this;
0100   }
0101   
0102   /**
0103    * Set account key for Windows Azure
0104    *
0105    * @param  string $value
0106    * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
0107    */
0108   public function setAccountkey($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY)
0109   {
0110     $this->_accountKey = base64_decode($value);
0111     return $this;
0112   }
0113   
0114   /**
0115    * Set use path-style URI's
0116    *
0117    * @param  boolean $value
0118    * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
0119    */
0120   public function setUsePathStyleUri($value = false)
0121   {
0122     $this->_usePathStyleUri = $value;
0123     return $this;
0124   }
0125   
0126   /**
0127    * Sign request URL with credentials
0128    *
0129    * @param string $requestUrl Request URL
0130    * @param string $resourceType Resource type
0131    * @param string $requiredPermission Required permission
0132    * @return string Signed request URL
0133    */
0134   abstract public function signRequestUrl(
0135     $requestUrl = '',
0136     $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
0137     $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
0138   );
0139   
0140   /**
0141    * Sign request headers with credentials
0142    *
0143    * @param string $httpVerb HTTP verb the request will use
0144    * @param string $path Path for the request
0145    * @param string $queryString Query string for the request
0146    * @param array $headers x-ms headers to add
0147    * @param boolean $forTableStorage Is the request for table storage?
0148    * @param string $resourceType Resource type
0149    * @param string $requiredPermission Required permission
0150    * @param mixed  $rawData Raw post data
0151    * @return array Array of headers
0152    */
0153   abstract public function signRequestHeaders(
0154     $httpVerb = Zend_Http_Client::GET,
0155     $path = '/',
0156     $queryString = '',
0157     $headers = null,
0158     $forTableStorage = false,
0159     $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
0160     $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
0161     $rawData = null
0162   );
0163   
0164   
0165   /**
0166    * Prepare query string for signing
0167    * 
0168    * @param  string $value Original query string
0169    * @return string        Query string for signing
0170    */
0171   protected function _prepareQueryStringForSigning($value)
0172   {
0173       // Return value
0174       $returnValue = array();
0175       
0176       // Prepare query string
0177       $queryParts = $this->_makeArrayOfQueryString($value);
0178       foreach ($queryParts as $key => $value) {
0179         $returnValue[] = $key . '=' . $value;
0180       }
0181       
0182       // Return
0183       if (count($returnValue) > 0) {
0184         return '?' . implode('&', $returnValue);
0185       } else {
0186         return '';
0187       }
0188   }
0189   
0190   /**
0191    * Make array of query string
0192    * 
0193    * @param  string $value Query string
0194    * @return array         Array of key/value pairs
0195    */
0196   protected function _makeArrayOfQueryString($value)
0197   {
0198     // Returnvalue
0199     $returnValue = array();
0200     
0201       // Remove front ?     
0202       if (strlen($value) > 0 && strpos($value, '?') === 0) {
0203         $value = substr($value, 1);
0204       }
0205         
0206       // Split parts
0207       $queryParts = explode('&', $value);
0208       foreach ($queryParts as $queryPart) {
0209         $queryPart = explode('=', $queryPart, 2);
0210         
0211         if ($queryPart[0] != '') {
0212           $returnValue[ $queryPart[0] ] = isset($queryPart[1]) ? $queryPart[1] : '';
0213         }
0214       }
0215       
0216       // Sort
0217       ksort($returnValue);
0218 
0219       // Return
0220     return $returnValue;
0221   }
0222   
0223   /**
0224    * Returns an array value if the key is set, otherwide returns $valueIfNotSet
0225    * 
0226    * @param array $array
0227    * @param mixed $key
0228    * @param mixed $valueIfNotSet
0229    * @return mixed
0230    */
0231   protected function _issetOr($array, $key, $valueIfNotSet)
0232   {
0233     return isset($array[$key]) ? $array[$key] : $valueIfNotSet;
0234   }
0235 }