RoaAcsRequest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. namespace Mrgoon\AliyunSmsSdk;
  21. abstract class RoaAcsRequest extends AcsRequest
  22. {
  23. protected $uriPattern;
  24. private $pathParameters = array();
  25. private $domainParameters = array();
  26. private $dateTimeFormat ="D, d M Y H:i:s \G\M\T";
  27. private static $headerSeparator = "\n";
  28. private static $querySeprator = "&";
  29. function __construct($product, $version, $actionName)
  30. {
  31. parent::__construct($product, $version, $actionName);
  32. $this->setVersion($version);
  33. $this->initialize();
  34. }
  35. private function initialize()
  36. {
  37. $this->setMethod("RAW");
  38. }
  39. public function composeUrl($iSigner, $credential, $domain)
  40. {
  41. $this->prepareHeader($iSigner);
  42. $signString = $this->getMethod().self::$headerSeparator;
  43. if(isset($this->headers["Accept"]))
  44. {
  45. $signString = $signString.$this->headers["Accept"];
  46. }
  47. $signString = $signString.self::$headerSeparator;
  48. if(isset($this->headers["Content-MD5"]))
  49. {
  50. $signString = $signString.$this->headers["Content-MD5"];
  51. }
  52. $signString = $signString.self::$headerSeparator;
  53. if(isset($this->headers["Content-Type"]))
  54. {
  55. $signString = $signString.$this->headers["Content-Type"];
  56. }
  57. $signString = $signString.self::$headerSeparator;
  58. if(isset($this->headers["Date"]))
  59. {
  60. $signString = $signString.$this->headers["Date"];
  61. }
  62. $signString = $signString.self::$headerSeparator;
  63. $uri = $this->replaceOccupiedParameters();
  64. $signString = $signString.$this->buildCanonicalHeaders();
  65. $queryString = $this->buildQueryString($uri);
  66. $signString .= $queryString;
  67. $this->headers["Authorization"] = "acs ".$credential->getAccessKeyId().":"
  68. .$iSigner->signString($signString, $credential->getAccessSecret());
  69. $requestUrl = $this->getProtocol()."://".$domain.$queryString;
  70. return $requestUrl;
  71. }
  72. private function prepareHeader($iSigner)
  73. {
  74. date_default_timezone_set("GMT");
  75. $this->headers["Date"] = date($this->dateTimeFormat);
  76. if(null == $this->acceptFormat)
  77. {
  78. $this->acceptFormat = "RAW";
  79. }
  80. $this->headers["Accept"] = $this->formatToAccept($this->getAcceptFormat());
  81. $this->headers["x-acs-signature-method"] = $iSigner->getSignatureMethod();
  82. $this->headers["x-acs-signature-version"] = $iSigner->getSignatureVersion();
  83. $this->headers["x-acs-region-id"] = $this->regionId;
  84. $content = $this->getDomainParameter();
  85. if ($content != null) {
  86. $this->headers["Content-MD5"] = base64_encode(md5(json_encode($content),true));
  87. }
  88. $this->headers["Content-Type"] = "application/octet-stream;charset=utf-8";
  89. }
  90. private function replaceOccupiedParameters()
  91. {
  92. $result = $this->uriPattern;
  93. foreach ($this->pathParameters as $pathParameterKey => $apiParameterValue)
  94. {
  95. $target = "[".$pathParameterKey."]";
  96. $result = str_replace($target,$apiParameterValue,$result);
  97. }
  98. return $result;
  99. }
  100. private function buildCanonicalHeaders()
  101. {
  102. $sortMap = array();
  103. foreach ($this->headers as $headerKey => $headerValue)
  104. {
  105. $key = strtolower($headerKey);
  106. if(strpos($key, "x-acs-") === 0)
  107. {
  108. $sortMap[$key] = $headerValue;
  109. }
  110. }
  111. ksort($sortMap);
  112. $headerString = "";
  113. foreach ($sortMap as $sortMapKey => $sortMapValue)
  114. {
  115. $headerString = $headerString.$sortMapKey.":".$sortMapValue.self::$headerSeparator;
  116. }
  117. return $headerString;
  118. }
  119. private function splitSubResource($uri)
  120. {
  121. $queIndex = strpos($uri, "?");
  122. $uriParts = array();
  123. if(null != $queIndex)
  124. {
  125. array_push($uriParts, substr($uri,0,$queIndex));
  126. array_push($uriParts, substr($uri,$queIndex+1));
  127. }
  128. else
  129. {
  130. array_push($uriParts,$uri);
  131. }
  132. return $uriParts;
  133. }
  134. private function buildQueryString($uri)
  135. {
  136. $uriParts = $this->splitSubResource($uri);
  137. $sortMap = $this->queryParameters;
  138. if(isset($uriParts[1]))
  139. {
  140. $sortMap[$uriParts[1]] = null;
  141. }
  142. $queryString = $uriParts[0];
  143. if(count($uriParts))
  144. {
  145. $queryString = $queryString."?";
  146. }
  147. ksort($sortMap);
  148. foreach ($sortMap as $sortMapKey => $sortMapValue)
  149. {
  150. $queryString = $queryString.$sortMapKey;
  151. if(isset($sortMapValue))
  152. {
  153. $queryString = $queryString."=".$sortMapValue;
  154. }
  155. $queryString = $queryString.$querySeprator;
  156. }
  157. if(null==count($sortMap))
  158. {
  159. $queryString = substr($queryString, 0, strlen($queryString)-1);
  160. }
  161. return $queryString;
  162. }
  163. private function formatToAccept($acceptFormat)
  164. {
  165. if($acceptFormat == "JSON")
  166. {
  167. return "application/json";
  168. }
  169. elseif ($acceptFormat == "XML") {
  170. return "application/xml";
  171. }
  172. return "application/octet-stream";
  173. }
  174. public function getPathParameters()
  175. {
  176. return $this->pathParameters;
  177. }
  178. public function putPathParameter($name, $value)
  179. {
  180. $this->pathParameters[$name] = $value;
  181. }
  182. public function getDomainParameter()
  183. {
  184. return $this->domainParameters;
  185. }
  186. public function putDomainParameters($name, $value)
  187. {
  188. $this->domainParameters[$name] = $value;
  189. }
  190. public function getUriPattern()
  191. {
  192. return $this->uriPattern;
  193. }
  194. public function setUriPattern($uriPattern)
  195. {
  196. return $this->uriPattern = $uriPattern;
  197. }
  198. public function setVersion($version)
  199. {
  200. $this->version = $version;
  201. $this->headers["x-acs-version"] = $version;
  202. }
  203. }