CredentialsAbstract.php 6.3 KB

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