[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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 [email protected] so we can send you a copy immediately. 14 * 15 * @category Zend 16 * @package Zend_Service_WindowsAzure 17 * @subpackage Storage 18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 19 * @license http://framework.zend.com/license/new-bsd New BSD License 20 * @version $Id$ 21 */ 22 23 /** 24 * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract 25 */ 26 require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php'; 27 28 /** 29 * @see Zend_Service_WindowsAzure_Credentials_SharedKey 30 */ 31 require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php'; 32 33 /** 34 * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract 35 */ 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 /** 54 * @category Zend 55 * @package Zend_Service_WindowsAzure 56 * @subpackage Storage 57 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) 58 * @license http://framework.zend.com/license/new-bsd New BSD License 59 */ 60 class Zend_Service_WindowsAzure_Storage 61 { 62 /** 63 * Development storage URLS 64 */ 65 const URL_DEV_BLOB = "127.0.0.1:10000"; 66 const URL_DEV_QUEUE = "127.0.0.1:10001"; 67 const URL_DEV_TABLE = "127.0.0.1:10002"; 68 69 /** 70 * Live storage URLS 71 */ 72 const URL_CLOUD_BLOB = "blob.core.windows.net"; 73 const URL_CLOUD_QUEUE = "queue.core.windows.net"; 74 const URL_CLOUD_TABLE = "table.core.windows.net"; 75 76 /** 77 * Resource types 78 */ 79 const RESOURCE_UNKNOWN = "unknown"; 80 const RESOURCE_CONTAINER = "c"; 81 const RESOURCE_BLOB = "b"; 82 const RESOURCE_TABLE = "t"; 83 const RESOURCE_ENTITY = "e"; 84 const RESOURCE_QUEUE = "q"; 85 86 /** 87 * Current API version 88 * 89 * @var string 90 */ 91 protected $_apiVersion = '2009-04-14'; 92 93 /** 94 * Storage host name 95 * 96 * @var string 97 */ 98 protected $_host = ''; 99 100 /** 101 * Account name for Windows Azure 102 * 103 * @var string 104 */ 105 protected $_accountName = ''; 106 107 /** 108 * Account key for Windows Azure 109 * 110 * @var string 111 */ 112 protected $_accountKey = ''; 113 114 /** 115 * Use path-style URI's 116 * 117 * @var boolean 118 */ 119 protected $_usePathStyleUri = false; 120 121 /** 122 * Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance 123 * 124 * @var Zend_Service_WindowsAzure_Credentials_CredentialsAbstract 125 */ 126 protected $_credentials = null; 127 128 /** 129 * Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance 130 * 131 * @var Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract 132 */ 133 protected $_retryPolicy = null; 134 135 /** 136 * Zend_Http_Client channel used for communication with REST services 137 * 138 * @var Zend_Http_Client 139 */ 140 protected $_httpClientChannel = null; 141 142 /** 143 * Use proxy? 144 * 145 * @var boolean 146 */ 147 protected $_useProxy = false; 148 149 /** 150 * Proxy url 151 * 152 * @var string 153 */ 154 protected $_proxyUrl = ''; 155 156 /** 157 * Proxy port 158 * 159 * @var int 160 */ 161 protected $_proxyPort = 80; 162 163 /** 164 * Proxy credentials 165 * 166 * @var string 167 */ 168 protected $_proxyCredentials = ''; 169 170 /** 171 * Creates a new Zend_Service_WindowsAzure_Storage instance 172 * 173 * @param string $host Storage host name 174 * @param string $accountName Account name for Windows Azure 175 * @param string $accountKey Account key for Windows Azure 176 * @param boolean $usePathStyleUri Use path-style URI's 177 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests 178 */ 179 public function __construct( 180 $host = self::URL_DEV_BLOB, 181 $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT, 182 $accountKey = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY, 183 $usePathStyleUri = false, 184 Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $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 193 && ($this->_host == self::URL_DEV_BLOB 194 || $this->_host == self::URL_DEV_QUEUE 195 || $this->_host == self::URL_DEV_TABLE) 196 ) { 197 // Local storage 198 $this->_usePathStyleUri = true; 199 } 200 201 if (is_null($this->_credentials)) { 202 $this->_credentials = new Zend_Service_WindowsAzure_Credentials_SharedKey( 203 $this->_accountName, $this->_accountKey, $this->_usePathStyleUri); 204 } 205 206 $this->_retryPolicy = $retryPolicy; 207 if (is_null($this->_retryPolicy)) { 208 $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry(); 209 } 210 211 // Setup default Zend_Http_Client channel 212 $options = array( 213 'adapter' => 'Zend_Http_Client_Adapter_Proxy' 214 ); 215 if (function_exists('curl_init')) { 216 // Set cURL options if cURL is used afterwards 217 $options['curloptions'] = array( 218 CURLOPT_FOLLOWLOCATION => true, 219 CURLOPT_TIMEOUT => 120, 220 ); 221 } 222 $this->_httpClientChannel = new Zend_Http_Client(null, $options); 223 } 224 225 /** 226 * Set the HTTP client channel to use 227 * 228 * @param Zend_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name. 229 */ 230 public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Proxy') 231 { 232 $this->_httpClientChannel->setAdapter($adapterInstance); 233 } 234 235 /** 236 * Set retry policy to use when making requests 237 * 238 * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests 239 */ 240 public function setRetryPolicy(Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null) 241 { 242 $this->_retryPolicy = $retryPolicy; 243 if (is_null($this->_retryPolicy)) { 244 $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry(); 245 } 246 } 247 248 /** 249 * Set proxy 250 * 251 * @param boolean $useProxy Use proxy? 252 * @param string $proxyUrl Proxy URL 253 * @param int $proxyPort Proxy port 254 * @param string $proxyCredentials Proxy credentials 255 */ 256 public function setProxy($useProxy = false, $proxyUrl = '', $proxyPort = 80, $proxyCredentials = '') 257 { 258 $this->_useProxy = $useProxy; 259 $this->_proxyUrl = $proxyUrl; 260 $this->_proxyPort = $proxyPort; 261 $this->_proxyCredentials = $proxyCredentials; 262 263 if ($this->_useProxy) { 264 $credentials = explode(':', $this->_proxyCredentials); 265 if(!isset($credentials[1])) { 266 $credentials[1] = ''; 267 } 268 $this->_httpClientChannel->setConfig(array( 269 'proxy_host' => $this->_proxyUrl, 270 'proxy_port' => $this->_proxyPort, 271 'proxy_user' => $credentials[0], 272 'proxy_pass' => $credentials[1], 273 )); 274 } else { 275 $this->_httpClientChannel->setConfig(array( 276 'proxy_host' => '', 277 'proxy_port' => 8080, 278 'proxy_user' => '', 279 'proxy_pass' => '', 280 )); 281 } 282 } 283 284 /** 285 * Returns the Windows Azure account name 286 * 287 * @return string 288 */ 289 public function getAccountName() 290 { 291 return $this->_accountName; 292 } 293 294 /** 295 * Get base URL for creating requests 296 * 297 * @return string 298 */ 299 public function getBaseUrl() 300 { 301 if ($this->_usePathStyleUri) { 302 return 'http://' . $this->_host . '/' . $this->_accountName; 303 } else { 304 return 'http://' . $this->_accountName . '.' . $this->_host; 305 } 306 } 307 308 /** 309 * Set Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance 310 * 311 * @param Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance to use for request signing. 312 */ 313 public function setCredentials(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials) 314 { 315 $this->_credentials = $credentials; 316 $this->_credentials->setAccountName($this->_accountName); 317 $this->_credentials->setAccountkey($this->_accountKey); 318 $this->_credentials->setUsePathStyleUri($this->_usePathStyleUri); 319 } 320 321 /** 322 * Get Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance 323 * 324 * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract 325 */ 326 public function getCredentials() 327 { 328 return $this->_credentials; 329 } 330 331 /** 332 * Perform request using Zend_Http_Client channel 333 * 334 * @param string $path Path 335 * @param string $queryString Query string 336 * @param string $httpVerb HTTP verb the request will use 337 * @param array $headers x-ms headers to add 338 * @param boolean $forTableStorage Is the request for table storage? 339 * @param mixed $rawData Optional RAW HTTP data to be sent over the wire 340 * @param string $resourceType Resource type 341 * @param string $requiredPermission Required permission 342 * @return Zend_Http_Response 343 */ 344 protected function _performRequest( 345 $path = '/', 346 $queryString = '', 347 $httpVerb = Zend_Http_Client::GET, 348 $headers = array(), 349 $forTableStorage = false, 350 $rawData = null, 351 $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, 352 $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ 353 ) { 354 // Clean path 355 if (strpos($path, '/') !== 0) { 356 $path = '/' . $path; 357 } 358 359 // Clean headers 360 if (is_null($headers)) { 361 $headers = array(); 362 } 363 364 // Ensure cUrl will also work correctly: 365 // - disable Content-Type if required 366 // - disable Expect: 100 Continue 367 if (!isset($headers["Content-Type"])) { 368 $headers["Content-Type"] = ''; 369 } 370 $headers["Expect"]= ''; 371 372 // Add version header 373 $headers['x-ms-version'] = $this->_apiVersion; 374 375 // URL encoding 376 $path = self::urlencode($path); 377 $queryString = self::urlencode($queryString); 378 379 // Generate URL and sign request 380 $requestUrl = $this->_credentials 381 ->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission); 382 $requestHeaders = $this->_credentials 383 ->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission); 384 385 // Prepare request 386 $this->_httpClientChannel->resetParameters(true); 387 $this->_httpClientChannel->setUri($requestUrl); 388 $this->_httpClientChannel->setHeaders($requestHeaders); 389 $this->_httpClientChannel->setRawData($rawData); 390 391 // Execute request 392 $response = $this->_retryPolicy->execute( 393 array($this->_httpClientChannel, 'request'), 394 array($httpVerb) 395 ); 396 397 return $response; 398 } 399 400 /** 401 * Parse result from Zend_Http_Response 402 * 403 * @param Zend_Http_Response $response Response from HTTP call 404 * @return object 405 * @throws Zend_Service_WindowsAzure_Exception 406 */ 407 protected function _parseResponse(Zend_Http_Response $response = null) 408 { 409 if (is_null($response)) { 410 throw new Zend_Service_WindowsAzure_Exception('Response should not be null.'); 411 } 412 413 $xml = @simplexml_load_string($response->getBody()); 414 415 if ($xml !== false) { 416 // Fetch all namespaces 417 $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true)); 418 419 // Register all namespace prefixes 420 foreach ($namespaces as $prefix => $ns) { 421 if ($prefix != '') { 422 $xml->registerXPathNamespace($prefix, $ns); 423 } 424 } 425 } 426 427 return $xml; 428 } 429 430 /** 431 * Generate metadata headers 432 * 433 * @param array $metadata 434 * @return HTTP headers containing metadata 435 */ 436 protected function _generateMetadataHeaders($metadata = array()) 437 { 438 // Validate 439 if (!is_array($metadata)) { 440 return array(); 441 } 442 443 // Return headers 444 $headers = array(); 445 foreach ($metadata as $key => $value) { 446 if (strpos($value, "\r") !== false || strpos($value, "\n") !== false) { 447 throw new Zend_Service_WindowsAzure_Exception('Metadata cannot contain newline characters.'); 448 } 449 $headers["x-ms-meta-" . strtolower($key)] = $value; 450 } 451 return $headers; 452 } 453 454 /** 455 * Parse metadata errors 456 * 457 * @param array $headers HTTP headers containing metadata 458 * @return array 459 */ 460 protected function _parseMetadataHeaders($headers = array()) 461 { 462 // Validate 463 if (!is_array($headers)) { 464 return array(); 465 } 466 467 // Return metadata 468 $metadata = array(); 469 foreach ($headers as $key => $value) { 470 if (substr(strtolower($key), 0, 10) == "x-ms-meta-") { 471 $metadata[str_replace("x-ms-meta-", '', strtolower($key))] = $value; 472 } 473 } 474 return $metadata; 475 } 476 477 /** 478 * Generate ISO 8601 compliant date string in UTC time zone 479 * 480 * @param int $timestamp 481 * @return string 482 */ 483 public function isoDate($timestamp = null) 484 { 485 $tz = @date_default_timezone_get(); 486 @date_default_timezone_set('UTC'); 487 488 if (is_null($timestamp)) { 489 $timestamp = time(); 490 } 491 492 $returnValue = str_replace('+00:00', '.0000000Z', @date('c', $timestamp)); 493 @date_default_timezone_set($tz); 494 return $returnValue; 495 } 496 497 /** 498 * URL encode function 499 * 500 * @param string $value Value to encode 501 * @return string Encoded value 502 */ 503 public static function urlencode($value) 504 { 505 return str_replace(' ', '%20', $value); 506 } 507 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |