/Zend/Service/WindowsAzure/Credentials/CredentialsAbstract.php

https://github.com/MontmereLimited/ZendFramework-v1 · PHP · 244 lines · 88 code · 25 blank · 131 comment · 6 complexity · 6f6653419534ce6573f67092c695405f 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. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: CredentialsAbstract.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * @see Zend_Http_Client
  23. */
  24. // // // // // // // // require_once 'Zend/Http/Client.php';
  25. /**
  26. * @see Zend_Service_WindowsAzure_Credentials_Exception
  27. */
  28. // // // // // // // // require_once 'Zend/Service/WindowsAzure/Credentials/Exception.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Service_WindowsAzure
  32. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. abstract class Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  36. {
  37. /**
  38. * Development storage account and key
  39. */
  40. const DEVSTORE_ACCOUNT = "devstoreaccount1";
  41. const DEVSTORE_KEY = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==";
  42. /**
  43. * HTTP header prefixes
  44. */
  45. const PREFIX_PROPERTIES = "x-ms-prop-";
  46. const PREFIX_METADATA = "x-ms-meta-";
  47. const PREFIX_STORAGE_HEADER = "x-ms-";
  48. /**
  49. * Permissions
  50. */
  51. const PERMISSION_READ = "r";
  52. const PERMISSION_WRITE = "w";
  53. const PERMISSION_DELETE = "d";
  54. const PERMISSION_LIST = "l";
  55. /**
  56. * Account name for Windows Azure
  57. *
  58. * @var string
  59. */
  60. protected $_accountName = '';
  61. /**
  62. * Account key for Windows Azure
  63. *
  64. * @var string
  65. */
  66. protected $_accountKey = '';
  67. /**
  68. * Use path-style URI's
  69. *
  70. * @var boolean
  71. */
  72. protected $_usePathStyleUri = false;
  73. /**
  74. * Creates a new Zend_Service_WindowsAzure_Credentials_CredentialsAbstract instance
  75. *
  76. * @param string $accountName Account name for Windows Azure
  77. * @param string $accountKey Account key for Windows Azure
  78. * @param boolean $usePathStyleUri Use path-style URI's
  79. */
  80. public function __construct(
  81. $accountName = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT,
  82. $accountKey = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY,
  83. $usePathStyleUri = false
  84. ) {
  85. $this->_accountName = $accountName;
  86. $this->_accountKey = base64_decode($accountKey);
  87. $this->_usePathStyleUri = $usePathStyleUri;
  88. }
  89. /**
  90. * Set account name for Windows Azure
  91. *
  92. * @param string $value
  93. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  94. */
  95. public function setAccountName($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_ACCOUNT)
  96. {
  97. $this->_accountName = $value;
  98. return $this;
  99. }
  100. /**
  101. * Set account key for Windows Azure
  102. *
  103. * @param string $value
  104. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  105. */
  106. public function setAccountkey($value = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::DEVSTORE_KEY)
  107. {
  108. $this->_accountKey = base64_decode($value);
  109. return $this;
  110. }
  111. /**
  112. * Set use path-style URI's
  113. *
  114. * @param boolean $value
  115. * @return Zend_Service_WindowsAzure_Credentials_CredentialsAbstract
  116. */
  117. public function setUsePathStyleUri($value = false)
  118. {
  119. $this->_usePathStyleUri = $value;
  120. return $this;
  121. }
  122. /**
  123. * Sign request URL with credentials
  124. *
  125. * @param string $requestUrl Request URL
  126. * @param string $resourceType Resource type
  127. * @param string $requiredPermission Required permission
  128. * @return string Signed request URL
  129. */
  130. abstract public function signRequestUrl(
  131. $requestUrl = '',
  132. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  133. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ
  134. );
  135. /**
  136. * Sign request headers with credentials
  137. *
  138. * @param string $httpVerb HTTP verb the request will use
  139. * @param string $path Path for the request
  140. * @param string $queryString Query string for the request
  141. * @param array $headers x-ms headers to add
  142. * @param boolean $forTableStorage Is the request for table storage?
  143. * @param string $resourceType Resource type
  144. * @param string $requiredPermission Required permission
  145. * @param mixed $rawData Raw post data
  146. * @return array Array of headers
  147. */
  148. abstract public function signRequestHeaders(
  149. $httpVerb = Zend_Http_Client::GET,
  150. $path = '/',
  151. $queryString = '',
  152. $headers = null,
  153. $forTableStorage = false,
  154. $resourceType = Zend_Service_WindowsAzure_Storage::RESOURCE_UNKNOWN,
  155. $requiredPermission = Zend_Service_WindowsAzure_Credentials_CredentialsAbstract::PERMISSION_READ,
  156. $rawData = null
  157. );
  158. /**
  159. * Prepare query string for signing
  160. *
  161. * @param string $value Original query string
  162. * @return string Query string for signing
  163. */
  164. protected function _prepareQueryStringForSigning($value)
  165. {
  166. // Return value
  167. $returnValue = array();
  168. // Prepare query string
  169. $queryParts = $this->_makeArrayOfQueryString($value);
  170. foreach ($queryParts as $key => $value) {
  171. $returnValue[] = $key . '=' . $value;
  172. }
  173. // Return
  174. if (count($returnValue) > 0) {
  175. return '?' . implode('&', $returnValue);
  176. } else {
  177. return '';
  178. }
  179. }
  180. /**
  181. * Make array of query string
  182. *
  183. * @param string $value Query string
  184. * @return array Array of key/value pairs
  185. */
  186. protected function _makeArrayOfQueryString($value)
  187. {
  188. // Returnvalue
  189. $returnValue = array();
  190. // Remove front ?
  191. if (strlen($value) > 0 && strpos($value, '?') === 0) {
  192. $value = substr($value, 1);
  193. }
  194. // Split parts
  195. $queryParts = explode('&', $value);
  196. foreach ($queryParts as $queryPart) {
  197. $queryPart = explode('=', $queryPart, 2);
  198. if ($queryPart[0] != '') {
  199. $returnValue[ $queryPart[0] ] = isset($queryPart[1]) ? $queryPart[1] : '';
  200. }
  201. }
  202. // Sort
  203. ksort($returnValue);
  204. // Return
  205. return $returnValue;
  206. }
  207. /**
  208. * Returns an array value if the key is set, otherwide returns $valueIfNotSet
  209. *
  210. * @param array $array
  211. * @param mixed $key
  212. * @param mixed $valueIfNotSet
  213. * @return mixed
  214. */
  215. protected function _issetOr($array, $key, $valueIfNotSet)
  216. {
  217. return isset($array[$key]) ? $array[$key] : $valueIfNotSet;
  218. }
  219. }