PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/tine20/library/Zend/Service/Amazon/Ec2/Abstract.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 200 lines | 71 code | 27 blank | 102 comment | 1 complexity | bbd8a774c505f253bacad56c044a6fdb MD5 | raw file
  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_Amazon
  17. * @subpackage Ec2
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  21. */
  22. require_once 'Zend/Service/Amazon/Abstract.php';
  23. require_once 'Zend/Service/Amazon/Ec2/Response.php';
  24. require_once 'Zend/Service/Amazon/Ec2/Exception.php';
  25. /**
  26. * Provides the basic functionality to send a request to the Amazon Ec2 Query API
  27. *
  28. * @category Zend
  29. * @package Zend_Service_Amazon
  30. * @subpackage Ec2
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. abstract class Zend_Service_Amazon_Ec2_Abstract extends Zend_Service_Amazon_Abstract
  35. {
  36. /**
  37. * The HTTP query server
  38. */
  39. protected $_ec2Endpoint = 'ec2.amazonaws.com';
  40. /**
  41. * The API version to use
  42. */
  43. protected $_ec2ApiVersion = '2009-04-04';
  44. /**
  45. * Signature Version
  46. */
  47. protected $_ec2SignatureVersion = '2';
  48. /**
  49. * Signature Encoding Method
  50. */
  51. protected $_ec2SignatureMethod = 'HmacSHA256';
  52. /**
  53. * Period after which HTTP request will timeout in seconds
  54. */
  55. protected $_httpTimeout = 10;
  56. /**
  57. * Sends a HTTP request to the queue service using Zend_Http_Client
  58. *
  59. * @param array $params List of parameters to send with the request
  60. * @return Zend_Service_Amazon_Ec2_Response
  61. * @throws Zend_Service_Amazon_Ec2_Exception
  62. */
  63. protected function sendRequest(array $params = array())
  64. {
  65. $url = 'https://' . $this->_getRegion() . $this->_ec2Endpoint . '/';
  66. $params = $this->addRequiredParameters($params);
  67. try {
  68. /* @var $request Zend_Http_Client */
  69. $request = self::getHttpClient();
  70. $request->resetParameters();
  71. $request->setConfig(array(
  72. 'timeout' => $this->_httpTimeout
  73. ));
  74. $request->setUri($url);
  75. $request->setMethod(Zend_Http_Client::POST);
  76. $request->setParameterPost($params);
  77. $httpResponse = $request->request();
  78. } catch (Zend_Http_Client_Exception $zhce) {
  79. $message = 'Error in request to AWS service: ' . $zhce->getMessage();
  80. throw new Zend_Service_Amazon_Ec2_Exception($message, $zhce->getCode());
  81. }
  82. $response = new Zend_Service_Amazon_Ec2_Response($httpResponse);
  83. $this->checkForErrors($response);
  84. return $response;
  85. }
  86. /**
  87. * Adds required authentication and version parameters to an array of
  88. * parameters
  89. *
  90. * The required parameters are:
  91. * - AWSAccessKey
  92. * - SignatureVersion
  93. * - Timestamp
  94. * - Version and
  95. * - Signature
  96. *
  97. * If a required parameter is already set in the <tt>$parameters</tt> array,
  98. * it is overwritten.
  99. *
  100. * @param array $parameters the array to which to add the required
  101. * parameters.
  102. *
  103. * @return array
  104. */
  105. protected function addRequiredParameters(array $parameters)
  106. {
  107. $parameters['AWSAccessKeyId'] = $this->_getAccessKey();
  108. $parameters['SignatureVersion'] = $this->_ec2SignatureVersion;
  109. $parameters['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  110. $parameters['Version'] = $this->_ec2ApiVersion;
  111. $parameters['SignatureMethod'] = $this->_ec2SignatureMethod;
  112. $parameters['Signature'] = $this->signParameters($parameters);
  113. return $parameters;
  114. }
  115. /**
  116. * Computes the RFC 2104-compliant HMAC signature for request parameters
  117. *
  118. * This implements the Amazon Web Services signature, as per the following
  119. * specification:
  120. *
  121. * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  122. * excluding <tt>Signature</tt>, the value of which is being created),
  123. * ignoring case.
  124. *
  125. * 2. Iterate over the sorted list and append the parameter name (in its
  126. * original case) and then its value. Do not URL-encode the parameter
  127. * values before constructing this string. Do not use any separator
  128. * characters when appending strings.
  129. *
  130. * @param array $parameters the parameters for which to get the signature.
  131. * @param string $secretKey the secret key to use to sign the parameters.
  132. *
  133. * @return string the signed data.
  134. */
  135. protected function signParameters(array $paramaters)
  136. {
  137. $data = "POST\n";
  138. $data .= $this->_getRegion() . $this->_ec2Endpoint . "\n";
  139. $data .= "/\n";
  140. uksort($paramaters, 'strcmp');
  141. unset($paramaters['Signature']);
  142. $arrData = array();
  143. foreach($paramaters as $key => $value) {
  144. $arrData[] = $key . '=' . str_replace("%7E", "~", urlencode($value));
  145. }
  146. $data .= implode('&', $arrData);
  147. require_once 'Zend/Crypt/Hmac.php';
  148. $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
  149. return base64_encode($hmac);
  150. }
  151. /**
  152. * Checks for errors responses from Amazon
  153. *
  154. * @param Zend_Service_Amazon_Ec2_Response $response the response object to
  155. * check.
  156. *
  157. * @return void
  158. *
  159. * @throws Zend_Service_Amazon_Ec2_Exception if one or more errors are
  160. * returned from Amazon.
  161. */
  162. private function checkForErrors(Zend_Service_Amazon_Ec2_Response $response)
  163. {
  164. $xpath = new DOMXPath($response->getDocument());
  165. $list = $xpath->query('//Error');
  166. if ($list->length > 0) {
  167. $node = $list->item(0);
  168. $code = $xpath->evaluate('string(Code/text())', $node);
  169. $message = $xpath->evaluate('string(Message/text())', $node);
  170. throw new Zend_Service_Amazon_Ec2_Exception($message, 0, $code);
  171. }
  172. }
  173. }