PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/vendor/Zend/Service/WindowsAzure/Storage.php

https://github.com/skoop/Gesichtbuch
PHP | 507 lines | 220 code | 56 blank | 231 comment | 30 complexity | 31fc419b471c33bb2db04bacaccd716a MD5 | raw file
  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-2010 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 6352 2010-08-16 15:02:45Z tpater $
  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-2010 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,
  162. Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
  163. ) {
  164. $this->_host = $host;
  165. $this->_accountName = $accountName;
  166. $this->_accountKey = $accountKey;
  167. $this->_usePathStyleUri = $usePathStyleUri;
  168. // Using local storage?
  169. if (!$this->_usePathStyleUri
  170. && ($this->_host == self::URL_DEV_BLOB
  171. || $this->_host == self::URL_DEV_QUEUE
  172. || $this->_host == self::URL_DEV_TABLE)
  173. ) {
  174. // Local storage
  175. $this->_usePathStyleUri = true;
  176. }
  177. if (is_null($this->_credentials)) {
  178. $this->_credentials = new Zend_Service_WindowsAzure_Credentials_SharedKey(
  179. $this->_accountName, $this->_accountKey, $this->_usePathStyleUri);
  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. $options = array(
  187. 'adapter' => 'Zend_Http_Client_Adapter_Proxy'
  188. );
  189. if (function_exists('curl_init')) {
  190. // Set cURL options if cURL is used afterwards
  191. $options['curloptions'] = array(
  192. CURLOPT_FOLLOWLOCATION => true,
  193. CURLOPT_TIMEOUT => 120,
  194. );
  195. }
  196. $this->_httpClientChannel = new Zend_Http_Client(null, $options);
  197. }
  198. /**
  199. * Set the HTTP client channel to use
  200. *
  201. * @param Zend_Http_Client_Adapter_Interface|string $adapterInstance Adapter instance or adapter class name.
  202. */
  203. public function setHttpClientChannel($adapterInstance = 'Zend_Http_Client_Adapter_Proxy')
  204. {
  205. $this->_httpClientChannel->setAdapter($adapterInstance);
  206. }
  207. /**
  208. * Set retry policy to use when making requests
  209. *
  210. * @param Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
  211. */
  212. public function setRetryPolicy(Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null)
  213. {
  214. $this->_retryPolicy = $retryPolicy;
  215. if (is_null($this->_retryPolicy)) {
  216. $this->_retryPolicy = Zend_Service_WindowsAzure_RetryPolicy_RetryPolicyAbstract::noRetry();
  217. }
  218. }
  219. /**
  220. * Set proxy
  221. *
  222. * @param boolean $useProxy Use proxy?
  223. * @param string $proxyUrl Proxy URL
  224. * @param int $proxyPort Proxy port
  225. * @param string $proxyCredentials Proxy credentials
  226. */
  227. public function setProxy($useProxy = false, $proxyUrl = '', $proxyPort = 80, $proxyCredentials = '')
  228. {
  229. $this->_useProxy = $useProxy;
  230. $this->_proxyUrl = $proxyUrl;
  231. $this->_proxyPort = $proxyPort;
  232. $this->_proxyCredentials = $proxyCredentials;
  233. if ($this->_useProxy) {
  234. $credentials = explode(':', $this->_proxyCredentials);
  235. if(!isset($credentials[1])) {
  236. $credentials[1] = '';
  237. }
  238. $this->_httpClientChannel->setConfig(array(
  239. 'proxy_host' => $this->_proxyUrl,
  240. 'proxy_port' => $this->_proxyPort,
  241. 'proxy_user' => $credentials[0],
  242. 'proxy_pass' => $credentials[1],
  243. ));
  244. } else {
  245. $this->_httpClientChannel->setConfig(array(
  246. 'proxy_host' => '',
  247. 'proxy_port' => 8080,
  248. 'proxy_user' => '',
  249. 'proxy_pass' => '',
  250. ));
  251. }
  252. }
  253. /**
  254. * Returns the Windows Azure account name
  255. *
  256. * @return string
  257. */
  258. public function getAccountName()
  259. {
  260. return $this->_accountName;
  261. }
  262. /**
  263. * Get base URL for creating requests
  264. *
  265. * @return string
  266. */
  267. public function getBaseUrl()
  268. {
  269. if ($this->_usePathStyleUri) {
  270. return 'http://' . $this->_host . '/' . $this->_accountName;
  271. } else {
  272. return 'http://' . $this->_accountName . '.' . $this->_host;
  273. }
  274. }
  275. /**
  276. * Set Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  277. *
  278. * @param Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance to use for request signing.
  279. */
  280. public function setCredentials(Zend_Service_WindowsAzure_Credentials_CredentialsAbstract $credentials)
  281. {
  282. $this->_credentials = $credentials;
  283. $this->_credentials->setAccountName($this->_accountName);
  284. $this->_credentials->setAccountkey($this->_accountKey);
  285. $this->_credentials->setUsePathStyleUri($this->_usePathStyleUri);
  286. }
  287. /**
  288. * Get Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  289. *
  290. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  291. */
  292. public function getCredentials()
  293. {
  294. return $this->_credentials;
  295. }
  296. /**
  297. * Perform request using Zend_Http_Client channel
  298. *
  299. * @param string $path Path
  300. * @param string $queryString Query string
  301. * @param string $httpVerb HTTP verb the request will use
  302. * @param array $headers x-ms headers to add
  303. * @param boolean $forTableStorage Is the request for table storage?
  304. * @param mixed $rawData Optional RAW HTTP data to be sent over the wire
  305. * @param string $resourceType Resource type
  306. * @param string $requiredPermission Required permission
  307. * @return Zend_Http_Response
  308. */
  309. protected function _performRequest(
  310. $path = '/',
  311. $queryString = '',
  312. $httpVerb = Zend_Http_Client::GET,
  313. $headers = array(),
  314. $forTableStorage = false,
  315. $rawData = null,
  316. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  317. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  318. ) {
  319. // Clean path
  320. if (strpos($path, '/') !== 0) {
  321. $path = '/' . $path;
  322. }
  323. // Clean headers
  324. if (is_null($headers)) {
  325. $headers = array();
  326. }
  327. // Ensure cUrl will also work correctly:
  328. // - disable Content-Type if required
  329. // - disable Expect: 100 Continue
  330. if (!isset($headers["Content-Type"])) {
  331. $headers["Content-Type"] = '';
  332. }
  333. $headers["Expect"]= '';
  334. // Add version header
  335. $headers['x-ms-version'] = $this->_apiVersion;
  336. // URL encoding
  337. $path = self::urlencode($path);
  338. $queryString = self::urlencode($queryString);
  339. // Generate URL and sign request
  340. $requestUrl = $this->_credentials
  341. ->signRequestUrl($this->getBaseUrl() . $path . $queryString, $resourceType, $requiredPermission);
  342. $requestHeaders = $this->_credentials
  343. ->signRequestHeaders($httpVerb, $path, $queryString, $headers, $forTableStorage, $resourceType, $requiredPermission);
  344. // Prepare request
  345. $this->_httpClientChannel->resetParameters(true);
  346. $this->_httpClientChannel->setUri($requestUrl);
  347. $this->_httpClientChannel->setHeaders($requestHeaders);
  348. $this->_httpClientChannel->setRawData($rawData);
  349. // Execute request
  350. $response = $this->_retryPolicy->execute(
  351. array($this->_httpClientChannel, 'request'),
  352. array($httpVerb)
  353. );
  354. return $response;
  355. }
  356. /**
  357. * Parse result from Zend_Http_Response
  358. *
  359. * @param Zend_Http_Response $response Response from HTTP call
  360. * @return object
  361. * @throws Zend_Service_WindowsAzure_Exception
  362. */
  363. protected function _parseResponse(Zend_Http_Response $response = null)
  364. {
  365. if (is_null($response)) {
  366. throw new Zend_Service_WindowsAzure_Exception('Response should not be null.');
  367. }
  368. $xml = @simplexml_load_string($response->getBody());
  369. if ($xml !== false) {
  370. // Fetch all namespaces
  371. $namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true));
  372. // Register all namespace prefixes
  373. foreach ($namespaces as $prefix => $ns) {
  374. if ($prefix != '') {
  375. $xml->registerXPathNamespace($prefix, $ns);
  376. }
  377. }
  378. }
  379. return $xml;
  380. }
  381. /**
  382. * Generate metadata headers
  383. *
  384. * @param array $metadata
  385. * @return HTTP headers containing metadata
  386. */
  387. protected function _generateMetadataHeaders($metadata = array())
  388. {
  389. // Validate
  390. if (!is_array($metadata)) {
  391. return array();
  392. }
  393. // Return headers
  394. $headers = array();
  395. foreach ($metadata as $key => $value) {
  396. if (strpos($value, "\r") !== false || strpos($value, "\n") !== false) {
  397. throw new Zend_Service_WindowsAzure_Exception('Metadata cannot contain newline characters.');
  398. }
  399. $headers["x-ms-meta-" . strtolower($key)] = $value;
  400. }
  401. return $headers;
  402. }
  403. /**
  404. * Parse metadata errors
  405. *
  406. * @param array $headers HTTP headers containing metadata
  407. * @return array
  408. */
  409. protected function _parseMetadataHeaders($headers = array())
  410. {
  411. // Validate
  412. if (!is_array($headers)) {
  413. return array();
  414. }
  415. // Return metadata
  416. $metadata = array();
  417. foreach ($headers as $key => $value) {
  418. if (substr(strtolower($key), 0, 10) == "x-ms-meta-") {
  419. $metadata[str_replace("x-ms-meta-", '', strtolower($key))] = $value;
  420. }
  421. }
  422. return $metadata;
  423. }
  424. /**
  425. * Generate ISO 8601 compliant date string in UTC time zone
  426. *
  427. * @param int $timestamp
  428. * @return string
  429. */
  430. public function isoDate($timestamp = null)
  431. {
  432. $tz = @date_default_timezone_get();
  433. @date_default_timezone_set('UTC');
  434. if (is_null($timestamp)) {
  435. $timestamp = time();
  436. }
  437. $returnValue = str_replace('+00:00', '.0000000Z', @date('c', $timestamp));
  438. @date_default_timezone_set($tz);
  439. return $returnValue;
  440. }
  441. /**
  442. * URL encode function
  443. *
  444. * @param string $value Value to encode
  445. * @return string Encoded value
  446. */
  447. public static function urlencode($value)
  448. {
  449. return str_replace(' ', '%20', $value);
  450. }
  451. }