RoaAcsRequest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. /**
  21. * @deprecated See: https://github.com/aliyun/openapi-sdk-php
  22. * Class RoaAcsRequest
  23. */
  24. abstract class RoaAcsRequest extends AcsRequest
  25. {
  26. /**
  27. * @var string
  28. */
  29. protected $uriPattern;
  30. /**
  31. * @var array
  32. */
  33. protected $pathParameters = array();
  34. /**
  35. * @var array
  36. */
  37. private $domainParameters = array();
  38. /**
  39. * @var string
  40. */
  41. private $dateTimeFormat = "D, d M Y H:i:s \G\M\T";
  42. /**
  43. * @var string
  44. */
  45. private static $headerSeparator = "\n";
  46. /**
  47. * @var string
  48. */
  49. private static $querySeparator = '&';
  50. /**
  51. * @var string
  52. */
  53. protected $method = 'RAW';
  54. /**
  55. * @var string
  56. */
  57. protected $acceptFormat = 'JSON';
  58. /**
  59. * @param $iSigner
  60. * @param $credential
  61. * @param $domain
  62. *
  63. * @return mixed|string
  64. */
  65. public function composeUrl($iSigner, $credential, $domain)
  66. {
  67. $this->headers['x-acs-version'] = &$this->version;
  68. $this->prepareHeader($iSigner, $credential);
  69. $signString = $this->getMethod() . self::$headerSeparator;
  70. if (isset($this->headers['Accept'])) {
  71. $signString .= $this->headers['Accept'];
  72. }
  73. $signString .= self::$headerSeparator;
  74. if (isset($this->headers['Content-MD5'])) {
  75. $signString .= $this->headers['Content-MD5'];
  76. }
  77. $signString .= self::$headerSeparator;
  78. if (isset($this->headers['Content-Type'])) {
  79. $signString .= $this->headers['Content-Type'];
  80. }
  81. $signString .= self::$headerSeparator;
  82. if (isset($this->headers['Date'])) {
  83. $signString .= $this->headers['Date'];
  84. }
  85. $signString .= self::$headerSeparator;
  86. $uri = $this->replaceOccupiedParameters();
  87. $signString .= $this->buildCanonicalHeaders();
  88. $queryString = $this->buildQueryString($uri);
  89. if (substr($queryString, -1) === '?') {
  90. $queryString = substr($queryString, 0, -1);
  91. }
  92. $signString .= $queryString;
  93. $this->stringToBeSigned = $signString;
  94. $this->headers['Authorization'] = 'acs ' . $credential->getAccessKeyId() . ':'
  95. . $iSigner->signString($signString, $credential->getAccessSecret());
  96. $requestUrl = $this->getProtocol() . '://' . $domain . $queryString;
  97. return $requestUrl;
  98. }
  99. /**
  100. * @return string
  101. */
  102. private function concatQueryString()
  103. {
  104. $sortMap = $this->queryParameters;
  105. if (null == $sortMap || count($sortMap) == 0) {
  106. return '';
  107. }
  108. $queryString = '';
  109. ksort($sortMap);
  110. foreach ($sortMap as $sortMapKey => $sortMapValue) {
  111. $queryString .= $sortMapKey;
  112. if (isset($sortMapValue)) {
  113. $queryString = $queryString . '=' . urlencode($sortMapValue);
  114. }
  115. $queryString .= self::$querySeparator;
  116. }
  117. if (count($sortMap) > 0) {
  118. $queryString = substr($queryString, 0, -1);
  119. }
  120. return '?' . $queryString;
  121. }
  122. /**
  123. * @param $iSigner
  124. * @param $credential
  125. */
  126. private function prepareHeader($iSigner, $credential)
  127. {
  128. $this->headers['Date'] = gmdate($this->dateTimeFormat);
  129. if (null == $this->acceptFormat) {
  130. $this->acceptFormat = 'RAW';
  131. }
  132. $this->headers['Accept'] = $this->formatToAccept($this->getAcceptFormat());
  133. $this->headers['x-acs-signature-method'] = $iSigner->getSignatureMethod();
  134. $this->headers['x-acs-signature-version'] = $iSigner->getSignatureVersion();
  135. if ($iSigner->getSignatureType() != null) {
  136. $this->headers['x-acs-signature-type'] = $iSigner->getSignatureType();
  137. }
  138. $this->headers['x-acs-region-id'] = $this->regionId;
  139. $content = $this->getDomainParameter();
  140. if ($content != null) {
  141. $this->headers['Content-MD5'] = base64_encode(md5(json_encode($content), true));
  142. }
  143. if ($this->acceptFormat === 'JSON') {
  144. $this->headers['Content-Type'] = 'application/json;charset=utf-8';
  145. } else {
  146. $this->headers['Content-Type'] = 'application/octet-stream;charset=utf-8';
  147. }
  148. if ($credential->getSecurityToken() != null) {
  149. $this->headers['x-acs-security-token'] = $credential->getSecurityToken();
  150. }
  151. if ($credential instanceof BearerTokenCredential) {
  152. $this->headers['x-acs-bearer-token'] = $credential->getBearerToken();
  153. }
  154. }
  155. /**
  156. * @return mixed|string
  157. */
  158. private function replaceOccupiedParameters()
  159. {
  160. $result = $this->uriPattern;
  161. foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue) {
  162. $target = '[' . $pathParameterKey . ']';
  163. $result = str_replace($target, $apiParameterValue, $result);
  164. }
  165. return $result;
  166. }
  167. /**
  168. * @return string
  169. */
  170. private function buildCanonicalHeaders()
  171. {
  172. $sortMap = array();
  173. foreach ($this->headers as $headerKey => $headerValue) {
  174. $key = strtolower($headerKey);
  175. if (strpos($key, 'x-acs-') === 0) {
  176. $sortMap[$key] = $headerValue;
  177. }
  178. }
  179. ksort($sortMap);
  180. $headerString = '';
  181. foreach ($sortMap as $sortMapKey => $sortMapValue) {
  182. $headerString = $headerString . $sortMapKey . ':' . $sortMapValue . self::$headerSeparator;
  183. }
  184. return $headerString;
  185. }
  186. /**
  187. * @param $uri
  188. *
  189. * @return array
  190. */
  191. private function splitSubResource($uri)
  192. {
  193. $queIndex = strpos($uri, '?');
  194. $uriParts = array();
  195. if (null != $queIndex) {
  196. $uriParts[] = substr($uri, 0, $queIndex);
  197. $uriParts[] = substr($uri, $queIndex + 1);
  198. } else {
  199. $uriParts[] = $uri;
  200. }
  201. return $uriParts;
  202. }
  203. /**
  204. * @param $uri
  205. *
  206. * @return bool|mixed|string
  207. */
  208. private function buildQueryString($uri)
  209. {
  210. $uriParts = $this->splitSubResource($uri);
  211. $sortMap = $this->queryParameters;
  212. if (isset($uriParts[1])) {
  213. $sortMap[$uriParts[1]] = null;
  214. }
  215. $queryString = $uriParts[0];
  216. if (count($uriParts)) {
  217. $queryString .= '?';
  218. }
  219. ksort($sortMap);
  220. foreach ($sortMap as $sortMapKey => $sortMapValue) {
  221. $queryString .= $sortMapKey;
  222. if (isset($sortMapValue)) {
  223. $queryString = $queryString . '=' . $sortMapValue;
  224. }
  225. $queryString .= self::$querySeparator;
  226. }
  227. if (0 < count($sortMap)) {
  228. $queryString = substr($queryString, 0, -1);
  229. }
  230. return $queryString;
  231. }
  232. /**
  233. * @param $acceptFormat
  234. *
  235. * @return string
  236. */
  237. private function formatToAccept($acceptFormat)
  238. {
  239. if ($acceptFormat === 'JSON') {
  240. return 'application/json';
  241. }
  242. if ($acceptFormat === 'XML') {
  243. return 'application/xml';
  244. }
  245. return 'application/octet-stream';
  246. }
  247. /**
  248. * @return array
  249. */
  250. public function getPathParameters()
  251. {
  252. return $this->pathParameters;
  253. }
  254. /**
  255. * @param $name
  256. * @param $value
  257. */
  258. public function putPathParameter($name, $value)
  259. {
  260. $this->pathParameters[$name] = $value;
  261. }
  262. /**
  263. * @return array
  264. */
  265. public function getDomainParameter()
  266. {
  267. return $this->domainParameters;
  268. }
  269. /**
  270. * @param $name
  271. * @param $value
  272. */
  273. public function putDomainParameters($name, $value)
  274. {
  275. $this->domainParameters[$name] = $value;
  276. }
  277. /**
  278. * @return string
  279. */
  280. public function getUriPattern()
  281. {
  282. return $this->uriPattern;
  283. }
  284. /**
  285. * @param $uriPattern
  286. *
  287. * @return mixed
  288. */
  289. public function setUriPattern($uriPattern)
  290. {
  291. return $this->uriPattern = $uriPattern;
  292. }
  293. }