PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Service/Amazon/Ec2/Abstract.php

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