RpcAcsRequest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. abstract class RpcAcsRequest extends AcsRequest
  21. {
  22. private $dateTimeFormat = 'Y-m-d\TH:i:s\Z';
  23. private $domainParameters = array();
  24. function __construct($product, $version, $actionName)
  25. {
  26. parent::__construct($product, $version, $actionName);
  27. $this->initialize();
  28. }
  29. private function initialize()
  30. {
  31. $this->setMethod("GET");
  32. $this->setAcceptFormat("JSON");
  33. }
  34. private function prepareValue($value)
  35. {
  36. if (is_bool($value)) {
  37. if ($value) {
  38. return "true";
  39. } else {
  40. return "false";
  41. }
  42. } else {
  43. return $value;
  44. }
  45. }
  46. public function composeUrl($iSigner, $credential, $domain)
  47. {
  48. $apiParams = parent::getQueryParameters();
  49. foreach ($apiParams as $key => $value) {
  50. $apiParams[$key] = $this->prepareValue($value);
  51. }
  52. $apiParams["RegionId"] = $this->getRegionId();
  53. $apiParams["AccessKeyId"] = $credential->getAccessKeyId();
  54. $apiParams["Format"] = $this->getAcceptFormat();
  55. $apiParams["SignatureMethod"] = $iSigner->getSignatureMethod();
  56. $apiParams["SignatureVersion"] = $iSigner->getSignatureVersion();
  57. $apiParams["SignatureNonce"] = uniqid();
  58. date_default_timezone_set("GMT");
  59. $apiParams["Timestamp"] = date($this->dateTimeFormat);
  60. $apiParams["Action"] = $this->getActionName();
  61. $apiParams["Version"] = $this->getVersion();
  62. $apiParams["Signature"] = $this->computeSignature($apiParams, $credential->getAccessSecret(), $iSigner);
  63. if(parent::getMethod() == "POST") {
  64. $requestUrl = $this->getProtocol()."://". $domain . "/";
  65. foreach ($apiParams as $apiParamKey => $apiParamValue)
  66. {
  67. $this->putDomainParameters($apiParamKey,$apiParamValue);
  68. }
  69. return $requestUrl;
  70. }
  71. else {
  72. $requestUrl = $this->getProtocol()."://". $domain . "/?";
  73. foreach ($apiParams as $apiParamKey => $apiParamValue)
  74. {
  75. $requestUrl .= "$apiParamKey=" . urlencode($apiParamValue) . "&";
  76. }
  77. return substr($requestUrl, 0, -1);
  78. }
  79. }
  80. private function computeSignature($parameters, $accessKeySecret, $iSigner)
  81. {
  82. ksort($parameters);
  83. $canonicalizedQueryString = '';
  84. foreach($parameters as $key => $value)
  85. {
  86. $canonicalizedQueryString .= '&' . $this->percentEncode($key). '=' . $this->percentEncode($value);
  87. }
  88. $stringToSign = parent::getMethod().'&%2F&' . $this->percentencode(substr($canonicalizedQueryString, 1));
  89. $signature = $iSigner->signString($stringToSign, $accessKeySecret."&");
  90. return $signature;
  91. }
  92. protected function percentEncode($str)
  93. {
  94. $res = urlencode($str);
  95. $res = preg_replace('/\+/', '%20', $res);
  96. $res = preg_replace('/\*/', '%2A', $res);
  97. $res = preg_replace('/%7E/', '~', $res);
  98. return $res;
  99. }
  100. public function getDomainParameter()
  101. {
  102. return $this->domainParameters;
  103. }
  104. public function putDomainParameters($name, $value)
  105. {
  106. $this->domainParameters[$name] = $value;
  107. }
  108. }