Storage.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. * @subpackage Storage
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Storage.php 35835 2009-12-17 09:40:36Z unknown $
  21. */
  22. /**
  23. * @see Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  24. */
  25. require_once 'Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php';
  26. /**
  27. * @see Zend_Service_WindowsAzure_Credentials_SharedKey
  28. */
  29. require_once 'Zend/Service/WindowsAzure/Credentials/SharedKey.php';
  30. /**
  31. * @see Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
  32. */
  33. require_once 'Zend/Service/WindowsAzure/RetryPolicy/RetryPolicyAbstract.php';
  34. /**
  35. * @see Zend_Service_WindowsAzure_Exception
  36. */
  37. require_once 'Zend/Service/WindowsAzure/Exception.php';
  38. /**
  39. * @see Zend_Http_Client
  40. */
  41. require_once 'Zend/Http/Client.php';
  42. /**
  43. * @see Zend_Http_Response
  44. */
  45. require_once 'Zend/Http/Response.php';
  46. /**
  47. * @category Zend
  48. * @package Zend_Service_WindowsAzure
  49. * @subpackage Storage
  50. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  51. * @license http://framework.zend.com/license/new-bsd New BSD License
  52. */
  53. class Zend_Service_WindowsAzure_Storage
  54. {
  55. /**
  56. * Development storage URLS
  57. */
  58. const URL_DEV_BLOB = "127.0.0.1:10000";
  59. const URL_DEV_QUEUE = "127.0.0.1:10001";
  60. const URL_DEV_TABLE = "127.0.0.1:10002";
  61. /**
  62. * Live storage URLS
  63. */
  64. const URL_CLOUD_BLOB = "blob.core.windows.net";
  65. const URL_CLOUD_QUEUE = "queue.core.windows.net";
  66. const URL_CLOUD_TABLE = "table.core.windows.net";
  67. /**
  68. * Resource types
  69. */
  70. const RESOURCE_UNKNOWN = "unknown";
  71. const RESOURCE_CONTAINER = "c";
  72. const RESOURCE_BLOB = "b";
  73. const RESOURCE_TABLE = "t";
  74. const RESOURCE_ENTITY = "e";
  75. const RESOURCE_QUEUE = "q";
  76. /**
  77. * Current API version
  78. *
  79. * @var string
  80. */
  81. protected $_apiVersion = '2009-04-14';
  82. /**
  83. * Storage host name
  84. *
  85. * @var string
  86. */
  87. protected $_host = '';
  88. /**
  89. * Account name for Windows Azure
  90. *
  91. * @var string
  92. */
  93. protected $_accountName = '';
  94. /**
  95. * Account key for Windows Azure
  96. *
  97. * @var string
  98. */
  99. protected $_accountKey = '';
  100. /**
  101. * Use path-style URI's
  102. *
  103. * @var boolean
  104. */
  105. protected $_usePathStyleUri = false;
  106. /**
  107. * Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  108. *
  109. * @var Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  110. */
  111. protected $_credentials = null;
  112. /**
  113. * Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
  114. *
  115. * @var Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract
  116. */
  117. protected $_retryPolicy = null;
  118. /**
  119. * Zend_Http_Client channel used for communication with REST services
  120. *
  121. * @var Zend_Http_Client
  122. */
  123. protected $_httpClientChannel = null;
  124. /**
  125. * Use proxy?
  126. *
  127. * @var boolean
  128. */
  129. protected $_useProxy = false;
  130. /**
  131. * Proxy url
  132. *
  133. * @var string
  134. */
  135. protected $_proxyUrl = '';
  136. /**
  137. * Proxy port
  138. *
  139. * @var int
  140. */
  141. protected $_proxyPort = 80;
  142. /**
  143. * Proxy credentials
  144. *
  145. * @var string
  146. */
  147. protected $_proxyCredentials = '';
  148. /**
  149. * Creates a new Zend_Service_WindowsAzure_Storage instance
  150. *
  151. * @param string $host Storage host name
  152. * @param string $accountName Account name for Windows Azure
  153. * @param string $accountKey Account key for Windows Azure
  154. * @param boolean $usePathStyleUri Use path-style URI's
  155. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  156. */
  157. public function __construct(
  158. $host = self::URL_DEV_BLOB,
  159. $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
  160. $accountKey = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
  161. $usePathStyleUri = false, Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
  162. ) {
  163. $this->_host = $host;
  164. $this->_accountName = $accountName;
  165. $this->_accountKey = $accountKey;
  166. $this->_usePathStyleUri = $usePathStyleUri;
  167. // Using local storage?
  168. if (!$this->_usePathStyleUri
  169. && ($this->_host == self::URL_DEV_BLOB
  170. || $this->_host == self::URL_DEV_QUEUE
  171. || $this->_host == self::URL_DEV_TABLE)
  172. ) {
  173. // Local storage
  174. $this->_usePathStyleUri = true;
  175. }
  176. if (is_null($this->_credentials)) {
  177. $this->_credentials = new Zend_Service_WindowsAzure_Credentials_SharedKey(
  178. $this->_accountName, $this->_accountKey, $this->_usePathStyleUri
  179. );
  180. }
  181. $this->_retryPolicy = $retryPolicy;
  182. if (is_null($this->_retryPolicy)) {
  183. $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
  184. }
  185. // Setup default Zend_Http_Client channel
  186. $this->_httpClientChannel = new Zend_Http_Client(
  187. null,
  188. array(
  189. 'adapter' => 'Zend_Http_Client_Adapter_Proxy',
  190. 'curloptions' => array(
  191. CURLOPT_FOLLOWLOCATION => true,
  192. CURLOPT_TIMEOUT => 120,
  193. )
  194. )
  195. );
  196. }
  197. /**
  198. * Set the HTTP client channel to use
  199. *
  200. * @param Zend_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name.
  201. */
  202. public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Proxy')
  203. {
  204. $this->_httpClientChannel->setAdapter($adapterInstance);
  205. }
  206. /**
  207. * Set retry policy to use when making requests
  208. *
  209. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  210. */
  211. public function setRetryPolicy(Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
  212. {
  213. $this->_retryPolicy = $retryPolicy;
  214. if (is_null($this->_retryPolicy)) {
  215. $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
  216. }
  217. }
  218. /**
  219. * Set proxy
  220. *
  221. * @param boolean $useProxy Use proxy?
  222. * @param string $proxyUrl Proxy URL
  223. * @param int $proxyPort Proxy port
  224. * @param string $proxyCredentials Proxy credentials
  225. */
  226. public function setProxy($useProxy = false, $proxyUrl = '', $proxyPort = 80, $proxyCredentials = '')
  227. {
  228. $this->_useProxy = $useProxy;
  229. $this->_proxyUrl = $proxyUrl;
  230. $this->_proxyPort = $proxyPort;
  231. $this->_proxyCredentials = $proxyCredentials;
  232. if ($this->_useProxy) {
  233. $credentials = explode(':', $this->_proxyCredentials);
  234. $this->_httpClientChannel->setConfig(array(
  235. 'proxy_host' => $this->_proxyUrl,
  236. 'proxy_port' => $this->_proxyPort,
  237. 'proxy_user' => $credentials[0],
  238. 'proxy_pass' => $credentials[1],
  239. ));
  240. } else {
  241. $this->_httpClientChannel->setConfig(array(
  242. 'proxy_host' => '',
  243. 'proxy_port' => 8080,
  244. 'proxy_user' => '',
  245. 'proxy_pass' => '',
  246. ));
  247. }
  248. }
  249. /**
  250. * Returns the Windows Azure account name
  251. *
  252. * @return string
  253. */
  254. public function getAccountName()
  255. {
  256. return $this->_accountName;
  257. }
  258. /**
  259. * Get base URL for creating requests
  260. *
  261. * @return string
  262. */
  263. public function getBaseUrl()
  264. {
  265. if ($this->_usePathStyleUri) {
  266. return 'http://' . $this->_host . '/' . $this->_accountName;
  267. }
  268. return 'http://' . $this->_accountName . '.' . $this->_host;
  269. }
  270. /**
  271. * Set Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  272. *
  273. * @param Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance to use for request signing.
  274. */
  275. public function setCredentials(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials)
  276. {
  277. $this->_credentials = $credentials;
  278. $this->_credentials->setAccountName($this->_accountName);
  279. $this->_credentials->setAccountkey($this->_accountKey);
  280. $this->_credentials->setUsePathStyleUri($this->_usePathStyleUri);
  281. }
  282. /**
  283. * Get Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  284. *
  285. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  286. */
  287. public function getCredentials()
  288. {
  289. return $this->_credentials;
  290. }
  291. /**
  292. * Perform request using Zend_Http_Client channel
  293. *
  294. * @param string $path Path
  295. * @param string $queryString Query string
  296. * @param string $httpVerb HTTP verb the request will use
  297. * @param array $headers x-ms headers to add
  298. * @param boolean $forTableStorage Is the request for table storage?
  299. * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  300. * @param string $resourceType Resource type
  301. * @param string $requiredPermission Required permission
  302. * @return Zend_Http_Response
  303. */
  304. protected function _performRequest(
  305. $path = '/',
  306. $queryString = '',
  307. $httpVerb = Zend_Http_Client::GET,
  308. $headers = array(),
  309. $forTableStorage = false,
  310. $rawData = null,
  311. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  312. $requiredPermission = Zend_Service_WindowsAzure_Credentials::PERMISSION_READ
  313. ) {
  314. // Clean path
  315. if (strpos($path, '/') !== 0) {
  316. $path = '/' . $path;
  317. }
  318. // Clean headers
  319. if (is_null($headers)) {
  320. $headers = array();
  321. }
  322. // Ensure cUrl will also work correctly:
  323. // - disable Content-Type if required
  324. // - disable Expect: 100 Continue
  325. if (!isset($headers["Content-Type"])) {
  326. $headers["Content-Type"] = '';
  327. }
  328. $headers["Expect"]= '';
  329. // Add version header
  330. $headers['x-ms-version'] = $this->_apiVersion;
  331. // URL encoding
  332. $path = self::urlencode($path);
  333. $queryString = self::urlencode($queryString);
  334. // Generate URL and sign request
  335. $requestUrl = $this->_credentials
  336. ->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
  337. $requestHeaders = $this->_credentials
  338. ->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission);
  339. // Prepare request
  340. $this->_httpClientChannel->resetParameters(true);
  341. $this->_httpClientChannel->setUri($requestUrl);
  342. $this->_httpClientChannel->setHeaders($requestHeaders);
  343. $this->_httpClientChannel->setRawData($rawData);
  344. // Execute request
  345. $response = $this->_retryPolicy->execute(
  346. array($this->_httpClientChannel, 'request'),
  347. array($httpVerb)
  348. );
  349. return $response;
  350. }
  351. /**
  352. * Parse result from Zend_Http_Response
  353. *
  354. * @param Zend_Http_Response $response Response from HTTP call
  355. * @return object
  356. * @throws Zend_Service_WindowsAzure_Exception
  357. */
  358. protected function _parseResponse(Zend_Http_Response $response = null)
  359. {
  360. if (is_null($response)) {
  361. throw new Zend_Service_WindowsAzure_Exception('Response should not be null.');
  362. }
  363. $xml = @simplexml_load_string($response->getBody());
  364. if ($xml !== false) {
  365. // Fetch all namespaces
  366. $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true));
  367. // Register all namespace prefixes
  368. foreach ($namespaces as $prefix => $ns) {
  369. if ($prefix != '') {
  370. $xml->registerXPathNamespace($prefix, $ns);
  371. }
  372. }
  373. }
  374. return $xml;
  375. }
  376. /**
  377. * Generate ISO 8601 compliant date string in UTC time zone
  378. *
  379. * @param int $timestamp
  380. * @return string
  381. */
  382. public function isoDate($timestamp = null)
  383. {
  384. $tz = @date_default_timezone_get();
  385. @date_default_timezone_set('UTC');
  386. if (is_null($timestamp)) {
  387. $timestamp = time();
  388. }
  389. $returnValue = str_replace('+00:00', '.0000000Z', @date('c', $timestamp));
  390. @date_default_timezone_set($tz);
  391. return $returnValue;
  392. }
  393. /**
  394. * URL encode function
  395. *
  396. * @param string $value Value to encode
  397. * @return string Encoded value
  398. */
  399. public static function urlencode($value)
  400. {
  401. return str_replace(' ', '%20', $value);
  402. }
  403. }