|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00026 require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php'; 00027 00031 require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php'; 00032 00036 require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php'; 00037 00041 require_once 'Zend/Service/WindowsAzure/Exception.php'; 00042 00046 require_once 'Zend/Http/Client.php'; 00047 00051 require_once 'Zend/Http/Response.php'; 00052 00060 class Zend_Service_WindowsAzure_Storage 00061 { 00065 const URL_DEV_BLOB = "127.0.0.1:10000"; 00066 const URL_DEV_QUEUE = "127.0.0.1:10001"; 00067 const URL_DEV_TABLE = "127.0.0.1:10002"; 00068 00072 const URL_CLOUD_BLOB = "blob.core.windows.net"; 00073 const URL_CLOUD_QUEUE = "queue.core.windows.net"; 00074 const URL_CLOUD_TABLE = "table.core.windows.net"; 00075 00079 const RESOURCE_UNKNOWN = "unknown"; 00080 const RESOURCE_CONTAINER = "c"; 00081 const RESOURCE_BLOB = "b"; 00082 const RESOURCE_TABLE = "t"; 00083 const RESOURCE_ENTITY = "e"; 00084 const RESOURCE_QUEUE = "q"; 00085 00091 protected $_apiVersion = '2009-04-14'; 00092 00098 protected $_host = ''; 00099 00105 protected $_accountName = ''; 00106 00112 protected $_accountKey = ''; 00113 00119 protected $_usePathStyleUri = false; 00120 00126 protected $_credentials = null; 00127 00133 protected $_retryPolicy = null; 00134 00140 protected $_httpClientChannel = null; 00141 00147 protected $_useProxy = false; 00148 00154 protected $_proxyUrl = ''; 00155 00161 protected $_proxyPort = 80; 00162 00168 protected $_proxyCredentials = ''; 00169 00179 public function __construct( 00180 $host = self::URL_DEV_BLOB, 00181 $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT, 00182 $accountKey = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY, 00183 $usePathStyleUri = false, 00184 Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null 00185 ) { 00186 $this->_host = $host; 00187 $this->_accountName = $accountName; 00188 $this->_accountKey = $accountKey; 00189 $this->_usePathStyleUri = $usePathStyleUri; 00190 00191 // Using local storage? 00192 if (!$this->_usePathStyleUri 00193 && ($this->_host == self::URL_DEV_BLOB 00194 || $this->_host == self::URL_DEV_QUEUE 00195 || $this->_host == self::URL_DEV_TABLE) 00196 ) { 00197 // Local storage 00198 $this->_usePathStyleUri = true; 00199 } 00200 00201 if (is_null($this->_credentials)) { 00202 $this->_credentials = new Zend_Service_WindowsAzure_Credentials_SharedKey( 00203 $this->_accountName, $this->_accountKey, $this->_usePathStyleUri); 00204 } 00205 00206 $this->_retryPolicy = $retryPolicy; 00207 if (is_null($this->_retryPolicy)) { 00208 $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry(); 00209 } 00210 00211 // Setup default Zend_Http_Client channel 00212 $options = array( 00213 'adapter' => 'Zend_Http_Client_Adapter_Proxy' 00214 ); 00215 if (function_exists('curl_init')) { 00216 // Set cURL options if cURL is used afterwards 00217 $options['curloptions'] = array( 00218 CURLOPT_FOLLOWLOCATION => true, 00219 CURLOPT_TIMEOUT => 120, 00220 ); 00221 } 00222 $this->_httpClientChannel = new Zend_Http_Client(null, $options); 00223 } 00224 00230 public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Proxy') 00231 { 00232 $this->_httpClientChannel->setAdapter($adapterInstance); 00233 } 00234 00240 public function setRetryPolicy(Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null) 00241 { 00242 $this->_retryPolicy = $retryPolicy; 00243 if (is_null($this->_retryPolicy)) { 00244 $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry(); 00245 } 00246 } 00247 00256 public function setProxy($useProxy = false, $proxyUrl = '', $proxyPort = 80, $proxyCredentials = '') 00257 { 00258 $this->_useProxy = $useProxy; 00259 $this->_proxyUrl = $proxyUrl; 00260 $this->_proxyPort = $proxyPort; 00261 $this->_proxyCredentials = $proxyCredentials; 00262 00263 if ($this->_useProxy) { 00264 $credentials = explode(':', $this->_proxyCredentials); 00265 if(!isset($credentials[1])) { 00266 $credentials[1] = ''; 00267 } 00268 $this->_httpClientChannel->setConfig(array( 00269 'proxy_host' => $this->_proxyUrl, 00270 'proxy_port' => $this->_proxyPort, 00271 'proxy_user' => $credentials[0], 00272 'proxy_pass' => $credentials[1], 00273 )); 00274 } else { 00275 $this->_httpClientChannel->setConfig(array( 00276 'proxy_host' => '', 00277 'proxy_port' => 8080, 00278 'proxy_user' => '', 00279 'proxy_pass' => '', 00280 )); 00281 } 00282 } 00283 00289 public function getAccountName() 00290 { 00291 return $this->_accountName; 00292 } 00293 00299 public function getBaseUrl() 00300 { 00301 if ($this->_usePathStyleUri) { 00302 return 'http://' . $this->_host . '/' . $this->_accountName; 00303 } else { 00304 return 'http://' . $this->_accountName . '.' . $this->_host; 00305 } 00306 } 00307 00313 public function setCredentials(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials) 00314 { 00315 $this->_credentials = $credentials; 00316 $this->_credentials->setAccountName($this->_accountName); 00317 $this->_credentials->setAccountkey($this->_accountKey); 00318 $this->_credentials->setUsePathStyleUri($this->_usePathStyleUri); 00319 } 00320 00326 public function getCredentials() 00327 { 00328 return $this->_credentials; 00329 } 00330 00344 protected function _performRequest( 00345 $path = '/', 00346 $queryString = '', 00347 $httpVerb = Zend_Http_Client::GET, 00348 $headers = array(), 00349 $forTableStorage = false, 00350 $rawData = null, 00351 $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN, 00352 $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ 00353 ) { 00354 // Clean path 00355 if (strpos($path, '/') !== 0) { 00356 $path = '/' . $path; 00357 } 00358 00359 // Clean headers 00360 if (is_null($headers)) { 00361 $headers = array(); 00362 } 00363 00364 // Ensure cUrl will also work correctly: 00365 // - disable Content-Type if required 00366 // - disable Expect: 100 Continue 00367 if (!isset($headers["Content-Type"])) { 00368 $headers["Content-Type"] = ''; 00369 } 00370 $headers["Expect"]= ''; 00371 00372 // Add version header 00373 $headers['x-ms-version'] = $this->_apiVersion; 00374 00375 // URL encoding 00376 $path = self::urlencode($path); 00377 $queryString = self::urlencode($queryString); 00378 00379 // Generate URL and sign request 00380 $requestUrl = $this->_credentials 00381 ->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission); 00382 $requestHeaders = $this->_credentials 00383 ->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission); 00384 00385 // Prepare request 00386 $this->_httpClientChannel->resetParameters(true); 00387 $this->_httpClientChannel->setUri($requestUrl); 00388 $this->_httpClientChannel->setHeaders($requestHeaders); 00389 $this->_httpClientChannel->setRawData($rawData); 00390 00391 // Execute request 00392 $response = $this->_retryPolicy->execute( 00393 array($this->_httpClientChannel, 'request'), 00394 array($httpVerb) 00395 ); 00396 00397 return $response; 00398 } 00399 00407 protected function _parseResponse(Zend_Http_Response $response = null) 00408 { 00409 if (is_null($response)) { 00410 throw new Zend_Service_WindowsAzure_Exception('Response should not be null.'); 00411 } 00412 00413 $xml = @simplexml_load_string($response->getBody()); 00414 00415 if ($xml !== false) { 00416 // Fetch all namespaces 00417 $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true)); 00418 00419 // Register all namespace prefixes 00420 foreach ($namespaces as $prefix => $ns) { 00421 if ($prefix != '') { 00422 $xml->registerXPathNamespace($prefix, $ns); 00423 } 00424 } 00425 } 00426 00427 return $xml; 00428 } 00429 00436 protected function _generateMetadataHeaders($metadata = array()) 00437 { 00438 // Validate 00439 if (!is_array($metadata)) { 00440 return array(); 00441 } 00442 00443 // Return headers 00444 $headers = array(); 00445 foreach ($metadata as $key => $value) { 00446 if (strpos($value, "\r") !== false || strpos($value, "\n") !== false) { 00447 throw new Zend_Service_WindowsAzure_Exception('Metadata cannot contain newline characters.'); 00448 } 00449 $headers["x-ms-meta-" . strtolower($key)] = $value; 00450 } 00451 return $headers; 00452 } 00453 00460 protected function _parseMetadataHeaders($headers = array()) 00461 { 00462 // Validate 00463 if (!is_array($headers)) { 00464 return array(); 00465 } 00466 00467 // Return metadata 00468 $metadata = array(); 00469 foreach ($headers as $key => $value) { 00470 if (substr(strtolower($key), 0, 10) == "x-ms-meta-") { 00471 $metadata[str_replace("x-ms-meta-", '', strtolower($key))] = $value; 00472 } 00473 } 00474 return $metadata; 00475 } 00476 00483 public function isoDate($timestamp = null) 00484 { 00485 $tz = @date_default_timezone_get(); 00486 @date_default_timezone_set('UTC'); 00487 00488 if (is_null($timestamp)) { 00489 $timestamp = time(); 00490 } 00491 00492 $returnValue = str_replace('+00:00', '.0000000Z', @date('c', $timestamp)); 00493 @date_default_timezone_set($tz); 00494 return $returnValue; 00495 } 00496 00503 public static function urlencode($value) 00504 { 00505 return str_replace(' ', '%20', $value); 00506 } 00507 }