PageRenderTime 39ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Service/Amazon/Ec2/Abstract.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 277 lines | 100 code | 35 blank | 142 comment | 7 complexity | 30c208716176fd2f4f39ac51180aa59e 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Service_Amazon_Abstract
  24. */
  25. require_once 'Zend/Service/Amazon/Abstract.php';
  26. /**
  27. * @see Zend_Service_Amazon_Ec2_Response
  28. */
  29. require_once 'Zend/Service/Amazon/Ec2/Response.php';
  30. /**
  31. * @see Zend_Service_Amazon_Ec2_Exception
  32. */
  33. require_once 'Zend/Service/Amazon/Ec2/Exception.php';
  34. /**
  35. * Provides the basic functionality to send a request to the Amazon Ec2 Query API
  36. *
  37. * @category Zend
  38. * @package Zend_Service_Amazon
  39. * @subpackage Ec2
  40. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. */
  43. abstract class Zend_Service_Amazon_Ec2_Abstract extends Zend_Service_Amazon_Abstract
  44. {
  45. /**
  46. * The HTTP query server
  47. */
  48. protected $_ec2Endpoint = 'ec2.amazonaws.com';
  49. /**
  50. * The API version to use
  51. */
  52. protected $_ec2ApiVersion = '2009-04-04';
  53. /**
  54. * Signature Version
  55. */
  56. protected $_ec2SignatureVersion = '2';
  57. /**
  58. * Signature Encoding Method
  59. */
  60. protected $_ec2SignatureMethod = 'HmacSHA256';
  61. /**
  62. * Period after which HTTP request will timeout in seconds
  63. */
  64. protected $_httpTimeout = 10;
  65. /**
  66. * @var string Amazon Region
  67. */
  68. protected static $_defaultRegion = null;
  69. /**
  70. * @var string Amazon Region
  71. */
  72. protected $_region;
  73. /**
  74. * An array that contains all the valid Amazon Ec2 Regions.
  75. *
  76. * @var array
  77. */
  78. protected static $_validEc2Regions = array('eu-west-1', 'us-east-1');
  79. /**
  80. * Create Amazon client.
  81. *
  82. * @param string $access_key Override the default Access Key
  83. * @param string $secret_key Override the default Secret Key
  84. * @param string $region Sets the AWS Region
  85. * @return void
  86. */
  87. public function __construct($accessKey=null, $secretKey=null, $region=null)
  88. {
  89. if(!$region) {
  90. $region = self::$_defaultRegion;
  91. } else {
  92. // make rue the region is valid
  93. if(!empty($region) && !in_array(strtolower($region), self::$_validEc2Regions, true)) {
  94. require_once 'Zend/Service/Amazon/Exception.php';
  95. throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
  96. }
  97. }
  98. $this->_region = $region;
  99. parent::__construct($accessKey, $secretKey);
  100. }
  101. /**
  102. * Set which region you are working in. It will append the
  103. * end point automaticly
  104. *
  105. * @param string $region
  106. */
  107. public static function setRegion($region)
  108. {
  109. if(in_array(strtolower($region), self::$_validEc2Regions, true)) {
  110. self::$_defaultRegion = $region;
  111. } else {
  112. require_once 'Zend/Service/Amazon/Exception.php';
  113. throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
  114. }
  115. }
  116. /**
  117. * Method to fetch the AWS Region
  118. *
  119. * @return string
  120. */
  121. protected function _getRegion()
  122. {
  123. return (!empty($this->_region)) ? $this->_region . '.' : '';
  124. }
  125. /**
  126. * Sends a HTTP request to the queue service using Zend_Http_Client
  127. *
  128. * @param array $params List of parameters to send with the request
  129. * @return Zend_Service_Amazon_Ec2_Response
  130. * @throws Zend_Service_Amazon_Ec2_Exception
  131. */
  132. protected function sendRequest(array $params = array())
  133. {
  134. $url = 'https://' . $this->_getRegion() . $this->_ec2Endpoint . '/';
  135. $params = $this->addRequiredParameters($params);
  136. try {
  137. /* @var $request Zend_Http_Client */
  138. $request = self::getHttpClient();
  139. $request->resetParameters();
  140. $request->setConfig(array(
  141. 'timeout' => $this->_httpTimeout
  142. ));
  143. $request->setUri($url);
  144. $request->setMethod(Zend_Http_Client::POST);
  145. $request->setParameterPost($params);
  146. $httpResponse = $request->request();
  147. } catch (Zend_Http_Client_Exception $zhce) {
  148. $message = 'Error in request to AWS service: ' . $zhce->getMessage();
  149. throw new Zend_Service_Amazon_Ec2_Exception($message, $zhce->getCode(), $zhce);
  150. }
  151. $response = new Zend_Service_Amazon_Ec2_Response($httpResponse);
  152. $this->checkForErrors($response);
  153. return $response;
  154. }
  155. /**
  156. * Adds required authentication and version parameters to an array of
  157. * parameters
  158. *
  159. * The required parameters are:
  160. * - AWSAccessKey
  161. * - SignatureVersion
  162. * - Timestamp
  163. * - Version and
  164. * - Signature
  165. *
  166. * If a required parameter is already set in the <tt>$parameters</tt> array,
  167. * it is overwritten.
  168. *
  169. * @param array $parameters the array to which to add the required
  170. * parameters.
  171. *
  172. * @return array
  173. */
  174. protected function addRequiredParameters(array $parameters)
  175. {
  176. $parameters['AWSAccessKeyId'] = $this->_getAccessKey();
  177. $parameters['SignatureVersion'] = $this->_ec2SignatureVersion;
  178. $parameters['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z');
  179. $parameters['Version'] = $this->_ec2ApiVersion;
  180. $parameters['SignatureMethod'] = $this->_ec2SignatureMethod;
  181. $parameters['Signature'] = $this->signParameters($parameters);
  182. return $parameters;
  183. }
  184. /**
  185. * Computes the RFC 2104-compliant HMAC signature for request parameters
  186. *
  187. * This implements the Amazon Web Services signature, as per the following
  188. * specification:
  189. *
  190. * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  191. * excluding <tt>Signature</tt>, the value of which is being created),
  192. * ignoring case.
  193. *
  194. * 2. Iterate over the sorted list and append the parameter name (in its
  195. * original case) and then its value. Do not URL-encode the parameter
  196. * values before constructing this string. Do not use any separator
  197. * characters when appending strings.
  198. *
  199. * @param array $parameters the parameters for which to get the signature.
  200. * @param string $secretKey the secret key to use to sign the parameters.
  201. *
  202. * @return string the signed data.
  203. */
  204. protected function signParameters(array $paramaters)
  205. {
  206. $data = "POST\n";
  207. $data .= $this->_getRegion() . $this->_ec2Endpoint . "\n";
  208. $data .= "/\n";
  209. uksort($paramaters, 'strcmp');
  210. unset($paramaters['Signature']);
  211. $arrData = array();
  212. foreach($paramaters as $key => $value) {
  213. $arrData[] = $key . '=' . str_replace("%7E", "~", rawurlencode($value));
  214. }
  215. $data .= implode('&', $arrData);
  216. require_once 'Zend/Crypt/Hmac.php';
  217. $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
  218. return base64_encode($hmac);
  219. }
  220. /**
  221. * Checks for errors responses from Amazon
  222. *
  223. * @param Zend_Service_Amazon_Ec2_Response $response the response object to
  224. * check.
  225. *
  226. * @return void
  227. *
  228. * @throws Zend_Service_Amazon_Ec2_Exception if one or more errors are
  229. * returned from Amazon.
  230. */
  231. private function checkForErrors(Zend_Service_Amazon_Ec2_Response $response)
  232. {
  233. $xpath = new DOMXPath($response->getDocument());
  234. $list = $xpath->query('//Error');
  235. if ($list->length > 0) {
  236. $node = $list->item(0);
  237. $code = $xpath->evaluate('string(Code/text())', $node);
  238. $message = $xpath->evaluate('string(Message/text())', $node);
  239. throw new Zend_Service_Amazon_Ec2_Exception($message, 0, $code);
  240. }
  241. }
  242. }