PageRenderTime 108ms CodeModel.GetById 29ms RepoModel.GetById 8ms app.codeStats 0ms

/wp-content/plugins/backwpup/vendor/WindowsAzure/Common/Internal/ServiceRestProxy.php

https://gitlab.com/pankajmohale/chef2go
PHP | 347 lines | 178 code | 39 blank | 130 comment | 18 complexity | 3c0726b201896035354f6b8491c47fa4 MD5 | raw file
  1. <?php
  2. /**
  3. * LICENSE: Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. * http://www.apache.org/licenses/LICENSE-2.0
  7. *
  8. * Unless required by applicable law or agreed to in writing, software
  9. * distributed under the License is distributed on an "AS IS" BASIS,
  10. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. * See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. *
  14. * PHP version 5
  15. *
  16. * @category Microsoft
  17. * @package WindowsAzure\Common\Internal
  18. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  19. * @copyright 2012 Microsoft Corporation
  20. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  21. * @link https://github.com/windowsazure/azure-sdk-for-php
  22. */
  23. namespace WindowsAzure\Common\Internal;
  24. use WindowsAzure\Common\Internal\Resources;
  25. use WindowsAzure\Common\Internal\Validate;
  26. use WindowsAzure\Common\Internal\Utilities;
  27. use WindowsAzure\Common\Internal\RestProxy;
  28. use WindowsAzure\Common\Internal\Http\Url;
  29. use WindowsAzure\Common\Internal\Http\HttpCallContext;
  30. /**
  31. * Base class for all services rest proxies.
  32. *
  33. * @category Microsoft
  34. * @package WindowsAzure\Common\Internal
  35. * @author Azure PHP SDK <azurephpsdk@microsoft.com>
  36. * @copyright 2012 Microsoft Corporation
  37. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  38. * @version Release: 0.4.1_2015-03
  39. * @link https://github.com/windowsazure/azure-sdk-for-php
  40. */
  41. class ServiceRestProxy extends RestProxy
  42. {
  43. /**
  44. * @var string
  45. */
  46. private $_accountName;
  47. /**
  48. * Initializes new ServiceRestProxy object.
  49. *
  50. * @param IHttpClient $channel The HTTP client used to send HTTP requests.
  51. * @param string $uri The storage account uri.
  52. * @param string $accountName The name of the account.
  53. * @param ISerializer $dataSerializer The data serializer.
  54. */
  55. public function __construct($channel, $uri, $accountName, $dataSerializer)
  56. {
  57. parent::__construct($channel, $dataSerializer, $uri);
  58. $this->_accountName = $accountName;
  59. }
  60. /**
  61. * Gets the account name.
  62. *
  63. * @return string
  64. */
  65. public function getAccountName()
  66. {
  67. return $this->_accountName;
  68. }
  69. /**
  70. * Sends HTTP request with the specified HTTP call context.
  71. *
  72. * @param WindowsAzure\Common\Internal\Http\HttpCallContext $context The HTTP
  73. * call context.
  74. *
  75. * @return \HTTP_Request2_Response
  76. */
  77. protected function sendContext($context)
  78. {
  79. $context->setUri($this->getUri());
  80. return parent::sendContext($context);
  81. }
  82. /**
  83. * Sends HTTP request with the specified parameters.
  84. *
  85. * @param string $method HTTP method used in the request
  86. * @param array $headers HTTP headers.
  87. * @param array $queryParams URL query parameters.
  88. * @param array $postParameters The HTTP POST parameters.
  89. * @param string $path URL path
  90. * @param int $statusCode Expected status code received in the response
  91. * @param string $body Request body
  92. *
  93. * @return \HTTP_Request2_Response
  94. */
  95. protected function send(
  96. $method,
  97. $headers,
  98. $queryParams,
  99. $postParameters,
  100. $path,
  101. $statusCode,
  102. $body = Resources::EMPTY_STRING
  103. ) {
  104. $context = new HttpCallContext();
  105. $context->setBody($body);
  106. $context->setHeaders($headers);
  107. $context->setMethod($method);
  108. $context->setPath($path);
  109. $context->setQueryParameters($queryParams);
  110. $context->setPostParameters($postParameters);
  111. if (is_array($statusCode)) {
  112. $context->setStatusCodes($statusCode);
  113. } else {
  114. $context->addStatusCode($statusCode);
  115. }
  116. return $this->sendContext($context);
  117. }
  118. /**
  119. * Adds optional header to headers if set
  120. *
  121. * @param array $headers The array of request headers.
  122. * @param AccessCondition $accessCondition The access condition object.
  123. *
  124. * @return array
  125. */
  126. public function addOptionalAccessConditionHeader($headers, $accessCondition)
  127. {
  128. if (!is_null($accessCondition)) {
  129. $header = $accessCondition->getHeader();
  130. if ($header != Resources::EMPTY_STRING) {
  131. $value = $accessCondition->getValue();
  132. if ($value instanceof \DateTime) {
  133. $value = gmdate(
  134. Resources::AZURE_DATE_FORMAT,
  135. $value->getTimestamp()
  136. );
  137. }
  138. $headers[$header] = $value;
  139. }
  140. }
  141. return $headers;
  142. }
  143. /**
  144. * Adds optional header to headers if set
  145. *
  146. * @param array $headers The array of request headers.
  147. * @param AccessCondition $accessCondition The access condition object.
  148. *
  149. * @return array
  150. */
  151. public function addOptionalSourceAccessConditionHeader(
  152. $headers,
  153. $accessCondition
  154. ) {
  155. if (!is_null($accessCondition)) {
  156. $header = $accessCondition->getHeader();
  157. $headerName = null;
  158. if (!empty($header)) {
  159. switch($header) {
  160. case Resources::IF_MATCH:
  161. $headerName = Resources::X_MS_SOURCE_IF_MATCH;
  162. break;
  163. case Resources::IF_UNMODIFIED_SINCE:
  164. $headerName = Resources::X_MS_SOURCE_IF_UNMODIFIED_SINCE;
  165. break;
  166. case Resources::IF_MODIFIED_SINCE:
  167. $headerName = Resources::X_MS_SOURCE_IF_MODIFIED_SINCE;
  168. break;
  169. case Resources::IF_NONE_MATCH:
  170. $headerName = Resources::X_MS_SOURCE_IF_NONE_MATCH;
  171. break;
  172. default:
  173. throw new \Exception(Resources::INVALID_ACH_MSG);
  174. break;
  175. }
  176. }
  177. $value = $accessCondition->getValue();
  178. if ($value instanceof \DateTime) {
  179. $value = gmdate(
  180. Resources::AZURE_DATE_FORMAT,
  181. $value->getTimestamp()
  182. );
  183. }
  184. $this->addOptionalHeader($headers, $headerName, $value);
  185. }
  186. return $headers;
  187. }
  188. /**
  189. * Adds HTTP POST parameter to the specified
  190. *
  191. * @param array $postParameters An array of HTTP POST parameters.
  192. * @param string $key The key of a HTTP POST parameter.
  193. * @param string $value the value of a HTTP POST parameter.
  194. *
  195. * @return array
  196. */
  197. public function addPostParameter(
  198. $postParameters,
  199. $key,
  200. $value
  201. ) {
  202. Validate::isArray($postParameters, 'postParameters');
  203. $postParameters[$key] = $value;
  204. return $postParameters;
  205. }
  206. /**
  207. * Groups set of values into one value separated with Resources::SEPARATOR
  208. *
  209. * @param array $values array of values to be grouped.
  210. *
  211. * @return string
  212. */
  213. public function groupQueryValues($values)
  214. {
  215. Validate::isArray($values, 'values');
  216. $joined = Resources::EMPTY_STRING;
  217. foreach ($values as $value) {
  218. if (!is_null($value) && !empty($value)) {
  219. $joined .= $value . Resources::SEPARATOR;
  220. }
  221. }
  222. return trim($joined, Resources::SEPARATOR);
  223. }
  224. /**
  225. * Adds metadata elements to headers array
  226. *
  227. * @param array $headers HTTP request headers
  228. * @param array $metadata user specified metadata
  229. *
  230. * @return array
  231. */
  232. protected function addMetadataHeaders($headers, $metadata)
  233. {
  234. $this->validateMetadata($metadata);
  235. $metadata = $this->generateMetadataHeaders($metadata);
  236. $headers = array_merge($headers, $metadata);
  237. return $headers;
  238. }
  239. /**
  240. * Generates metadata headers by prefixing each element with 'x-ms-meta'.
  241. *
  242. * @param array $metadata user defined metadata.
  243. *
  244. * @return array.
  245. */
  246. public function generateMetadataHeaders($metadata)
  247. {
  248. $metadataHeaders = array();
  249. if (is_array($metadata) && !is_null($metadata)) {
  250. foreach ($metadata as $key => $value) {
  251. $headerName = Resources::X_MS_META_HEADER_PREFIX;
  252. if ( strpos($value, "\r") !== false
  253. || strpos($value, "\n") !== false
  254. ) {
  255. throw new \InvalidArgumentException(Resources::INVALID_META_MSG);
  256. }
  257. $headerName .= strtolower($key);
  258. $metadataHeaders[$headerName] = $value;
  259. }
  260. }
  261. return $metadataHeaders;
  262. }
  263. /**
  264. * Gets metadata array by parsing them from given headers.
  265. *
  266. * @param array $headers HTTP headers containing metadata elements.
  267. *
  268. * @return array.
  269. */
  270. public function getMetadataArray($headers)
  271. {
  272. $metadata = array();
  273. foreach ($headers as $key => $value) {
  274. $isMetadataHeader = Utilities::startsWith(
  275. strtolower($key),
  276. Resources::X_MS_META_HEADER_PREFIX
  277. );
  278. if ($isMetadataHeader) {
  279. $MetadataName = str_replace(
  280. Resources::X_MS_META_HEADER_PREFIX,
  281. Resources::EMPTY_STRING,
  282. strtolower($key)
  283. );
  284. $metadata[$MetadataName] = $value;
  285. }
  286. }
  287. return $metadata;
  288. }
  289. /**
  290. * Validates the provided metadata array.
  291. *
  292. * @param mix $metadata The metadata array.
  293. *
  294. * @return none
  295. */
  296. public function validateMetadata($metadata)
  297. {
  298. if (!is_null($metadata)) {
  299. Validate::isArray($metadata, 'metadata');
  300. } else {
  301. $metadata = array();
  302. }
  303. foreach ($metadata as $key => $value) {
  304. Validate::isString($key, 'metadata key');
  305. Validate::isString($value, 'metadata value');
  306. }
  307. }
  308. }