web/lib/Zend/Service/WindowsAzure/Storage.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Service_WindowsAzure
    16  * @package    Zend_Service_WindowsAzure
    17  * @subpackage Storage
    17  * @subpackage Storage
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    20  * @version    $Id: Storage.php 23167 2010-10-19 17:53:31Z mabe $
    20  * @version    $Id: Storage.php 24697 2012-03-23 13:11:04Z ezimuel $
    21  */
    21  */
    22 
    22 
    23 /**
    23 /**
    24  * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
    24  * @see Zend_Http_Client
    25  */
    25  */
    26 require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
    26 require_once 'Zend/Http/Client.php';
    27 
    27 
    28 /**
    28 /**
    29  * @see Zend_Service_WindowsAzure_Credentials_SharedKey
    29  * @see Zend_Service_WindowsAzure_Credentials_SharedKey
    30  */
    30  */
    31 require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php';
    31 require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php';
    32 
    32 
    33 /**
    33 /**
    34  * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
    34  * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
    35  */
    35  */
    36 require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
    36 require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
    37 
       
    38 /**
       
    39  * @see Zend_Service_WindowsAzure_Exception
       
    40  */
       
    41 require_once 'Zend/Service/WindowsAzure/Exception.php';
       
    42 
       
    43 /**
       
    44  * @see Zend_Http_Client
       
    45  */
       
    46 require_once 'Zend/Http/Client.php';
       
    47 
       
    48 /**
       
    49  * @see Zend_Http_Response
       
    50  */
       
    51 require_once 'Zend/Http/Response.php';
       
    52 
       
    53 /**
    37 /**
    54  * @category   Zend
    38  * @category   Zend
    55  * @package    Zend_Service_WindowsAzure
    39  * @package    Zend_Service_WindowsAzure
    56  * @subpackage Storage
    40  * @subpackage Storage
    57  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    41  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    58  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    42  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    59  */
    43  */
    60 class Zend_Service_WindowsAzure_Storage
    44 class Zend_Service_WindowsAzure_Storage
    61 {
    45 {
    62 	/**
    46 	/**
   203 		) {
   187 		) {
   204 			// Local storage
   188 			// Local storage
   205 			$this->_usePathStyleUri = true;
   189 			$this->_usePathStyleUri = true;
   206 		}
   190 		}
   207 		
   191 		
   208 		if ($this->_credentials === null) {
   192 		if (is_null($this->_credentials)) {
   209 		    $this->_credentials = new Zend_Service_WindowsAzure_Credentials_SharedKey(
   193 		    $this->_credentials = new Zend_Service_WindowsAzure_Credentials_SharedKey(
   210 		    	$this->_accountName, $this->_accountKey, $this->_usePathStyleUri);
   194 		    	$this->_accountName, $this->_accountKey, $this->_usePathStyleUri);
   211 		}
   195 		}
   212 		
   196 		
   213 		$this->_retryPolicy = $retryPolicy;
   197 		$this->_retryPolicy = $retryPolicy;
   214 		if ($this->_retryPolicy === null) {
   198 		if (is_null($this->_retryPolicy)) {
   215 		    $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
   199 		    $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
   216 		}
   200 		}
   217 		
   201 		
   218 		// Setup default Zend_Http_Client channel
   202 		// Setup default Zend_Http_Client channel
   219 		$options = array(
   203 		$options = array(
   236 	 */
   220 	 */
   237 	public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Proxy')
   221 	public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Proxy')
   238 	{
   222 	{
   239 		$this->_httpClientChannel->setAdapter($adapterInstance);
   223 		$this->_httpClientChannel->setAdapter($adapterInstance);
   240 	}
   224 	}
   241 
   225 	
   242     /**
   226     /**
   243      * Retrieve HTTP client channel
   227      * Retrieve HTTP client channel
   244      * 
   228      * 
   245      * @return Zend_Http_Client_Adapter_Interface
   229      * @return Zend_Http_Client_Adapter_Interface
   246      */
   230      */
   255 	 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
   239 	 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
   256 	 */
   240 	 */
   257 	public function setRetryPolicy(Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
   241 	public function setRetryPolicy(Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
   258 	{
   242 	{
   259 		$this->_retryPolicy = $retryPolicy;
   243 		$this->_retryPolicy = $retryPolicy;
   260 		if ($this->_retryPolicy === null) {
   244 		if (is_null($this->_retryPolicy)) {
   261 		    $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
   245 		    $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
   262 		}
   246 		}
   263 	}
   247 	}
   264 	
   248 	
   265 	/**
   249 	/**
   370 		if (strpos($path, '/') !== 0) {
   354 		if (strpos($path, '/') !== 0) {
   371 			$path = '/' . $path;
   355 			$path = '/' . $path;
   372 		}
   356 		}
   373 			
   357 			
   374 		// Clean headers
   358 		// Clean headers
   375 		if ($headers === null) {
   359 		if (is_null($headers)) {
   376 		    $headers = array();
   360 		    $headers = array();
   377 		}
   361 		}
   378 		
   362 		
   379 		// Ensure cUrl will also work correctly:
   363 		// Ensure cUrl will also work correctly:
   380 		//  - disable Content-Type if required
   364 		//  - disable Content-Type if required
   395 		$requestUrl     = $this->_credentials
   379 		$requestUrl     = $this->_credentials
   396 						  ->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
   380 						  ->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
   397 		$requestHeaders = $this->_credentials
   381 		$requestHeaders = $this->_credentials
   398 						  ->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission, $rawData);
   382 						  ->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission, $rawData);
   399 
   383 
   400 		// Prepare request
   384 		// Prepare request 
   401 		$this->_httpClientChannel->resetParameters(true);
   385 		$this->_httpClientChannel->resetParameters(true);
   402 		$this->_httpClientChannel->setUri($requestUrl);
   386 		$this->_httpClientChannel->setUri($requestUrl);
   403 		$this->_httpClientChannel->setHeaders($requestHeaders);
   387 		$this->_httpClientChannel->setHeaders($requestHeaders);
   404 		$this->_httpClientChannel->setRawData($rawData);
   388 		$this->_httpClientChannel->setRawData($rawData);
   405 				
   389 				
   419 	 * @return object
   403 	 * @return object
   420 	 * @throws Zend_Service_WindowsAzure_Exception
   404 	 * @throws Zend_Service_WindowsAzure_Exception
   421 	 */
   405 	 */
   422 	protected function _parseResponse(Zend_Http_Response $response = null)
   406 	protected function _parseResponse(Zend_Http_Response $response = null)
   423 	{
   407 	{
   424 		if ($response === null) {
   408 		if (is_null($response)) {
       
   409 			require_once 'Zend/Service/WindowsAzure/Exception.php';
   425 			throw new Zend_Service_WindowsAzure_Exception('Response should not be null.');
   410 			throw new Zend_Service_WindowsAzure_Exception('Response should not be null.');
   426 		}
   411 		}
   427 		
   412 		
   428         $xml = @simplexml_load_string($response->getBody());
   413         $xml = @simplexml_load_string($response->getBody());
   429         
   414         
   457 		
   442 		
   458 		// Return headers
   443 		// Return headers
   459 		$headers = array();
   444 		$headers = array();
   460 		foreach ($metadata as $key => $value) {
   445 		foreach ($metadata as $key => $value) {
   461 			if (strpos($value, "\r") !== false || strpos($value, "\n") !== false) {
   446 			if (strpos($value, "\r") !== false || strpos($value, "\n") !== false) {
   462 				throw new Zend_Service_WindowsAzure_Exception('Metadata cannot contain newline characters.');
   447                             require_once 'Zend/Service/WindowsAzure/Exception.php';
       
   448                             throw new Zend_Service_WindowsAzure_Exception('Metadata cannot contain newline characters.');
   463 			}
   449 			}
   464 			
   450 			
   465 			if (!self::isValidMetadataName($key)) {
   451 			if (!self::isValidMetadataName($key)) {
   466 		    	throw new Zend_Service_WindowsAzure_Exception('Metadata name does not adhere to metadata naming conventions. See http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx for more information.');
   452                             require_once 'Zend/Service/WindowsAzure/Exception.php';
       
   453                             throw new Zend_Service_WindowsAzure_Exception('Metadata name does not adhere to metadata naming conventions. See http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx for more information.');
   467 			}
   454 			}
   468 			
   455 			
   469 		    $headers["x-ms-meta-" . strtolower($key)] = $value;
   456 		    $headers["x-ms-meta-" . strtolower($key)] = $value;
   470 		}
   457 		}
   471 		return $headers;
   458 		return $headers;
   501 	 * @return array
   488 	 * @return array
   502 	 */
   489 	 */
   503 	protected function _parseMetadataElement($element = null)
   490 	protected function _parseMetadataElement($element = null)
   504 	{
   491 	{
   505 		// Metadata present?
   492 		// Metadata present?
   506 		if ($element !== null && isset($element->Metadata) && $element->Metadata !== null) {
   493 		if (!is_null($element) && isset($element->Metadata) && !is_null($element->Metadata)) {
   507 			return get_object_vars($element->Metadata);
   494 			return get_object_vars($element->Metadata);
   508 		}
   495 		}
   509 
   496 
   510 		return array();
   497 		return array();
   511 	}
   498 	}
   519 	public function isoDate($timestamp = null) 
   506 	public function isoDate($timestamp = null) 
   520 	{        
   507 	{        
   521 	    $tz = @date_default_timezone_get();
   508 	    $tz = @date_default_timezone_get();
   522 	    @date_default_timezone_set('UTC');
   509 	    @date_default_timezone_set('UTC');
   523 	    
   510 	    
   524 	    if ($timestamp === null) {
   511 	    if (is_null($timestamp)) {
   525 	        $timestamp = time();
   512 	        $timestamp = time();
   526 	    }
   513 	    }
   527 	        
   514 	        
   528 	    $returnValue = str_replace('+00:00', '.0000000Z', @date('c', $timestamp));
   515 	    $returnValue = str_replace('+00:00', '.0000000Z', @date('c', $timestamp));
   529 	    @date_default_timezone_set($tz);
   516 	    @date_default_timezone_set($tz);