Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/zend/Zend/Service/Amazon/Ec2/Abstract.php
Go to the documentation of this file.
00001 <?php
00026 require_once 'Zend/Service/Amazon/Abstract.php';
00027 
00031 require_once 'Zend/Service/Amazon/Ec2/Response.php';
00032 
00036 require_once 'Zend/Service/Amazon/Ec2/Exception.php';
00037 
00047 abstract class Zend_Service_Amazon_Ec2_Abstract extends Zend_Service_Amazon_Abstract
00048 {
00052     protected $_ec2Endpoint = 'ec2.amazonaws.com';
00053 
00057     protected $_ec2ApiVersion = '2009-04-04';
00058 
00062     protected $_ec2SignatureVersion = '2';
00063 
00067     protected $_ec2SignatureMethod = 'HmacSHA256';
00068 
00072     protected $_httpTimeout = 10;
00073 
00077     protected static $_defaultRegion = null;
00078 
00082     protected $_region;
00083 
00089     protected static $_validEc2Regions = array('eu-west-1', 'us-east-1');
00090 
00099     public function __construct($accessKey=null, $secretKey=null, $region=null)
00100     {
00101         if(!$region) {
00102             $region = self::$_defaultRegion;
00103         } else {
00104             // make rue the region is valid
00105             if(!empty($region) && !in_array(strtolower($region), self::$_validEc2Regions, true)) {
00106                 require_once 'Zend/Service/Amazon/Exception.php';
00107                 throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
00108             }
00109         }
00110 
00111         $this->_region = $region;
00112 
00113         parent::__construct($accessKey, $secretKey);
00114     }
00115 
00122     public static function setRegion($region)
00123     {
00124         if(in_array(strtolower($region), self::$_validEc2Regions, true)) {
00125             self::$_defaultRegion = $region;
00126         } else {
00127             require_once 'Zend/Service/Amazon/Exception.php';
00128             throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
00129         }
00130     }
00131 
00137     protected function _getRegion()
00138     {
00139         return (!empty($this->_region)) ? $this->_region . '.' : '';
00140     }
00141 
00149     protected function sendRequest(array $params = array())
00150     {
00151         $url = 'https://' . $this->_getRegion() . $this->_ec2Endpoint . '/';
00152 
00153         $params = $this->addRequiredParameters($params);
00154 
00155         try {
00156             /* @var $request Zend_Http_Client */
00157             $request = self::getHttpClient();
00158             $request->resetParameters();
00159 
00160             $request->setConfig(array(
00161                 'timeout' => $this->_httpTimeout
00162             ));
00163 
00164             $request->setUri($url);
00165             $request->setMethod(Zend_Http_Client::POST);
00166             $request->setParameterPost($params);
00167 
00168             $httpResponse = $request->request();
00169 
00170 
00171         } catch (Zend_Http_Client_Exception $zhce) {
00172             $message = 'Error in request to AWS service: ' . $zhce->getMessage();
00173             throw new Zend_Service_Amazon_Ec2_Exception($message, $zhce->getCode(), $zhce);
00174         }
00175         $response = new Zend_Service_Amazon_Ec2_Response($httpResponse);
00176         $this->checkForErrors($response);
00177 
00178         return $response;
00179     }
00180 
00200     protected function addRequiredParameters(array $parameters)
00201     {
00202         $parameters['AWSAccessKeyId']   = $this->_getAccessKey();
00203         $parameters['SignatureVersion'] = $this->_ec2SignatureVersion;
00204         $parameters['Timestamp']        = gmdate('Y-m-d\TH:i:s\Z');
00205         $parameters['Version']          = $this->_ec2ApiVersion;
00206         $parameters['SignatureMethod']  = $this->_ec2SignatureMethod;
00207         $parameters['Signature']        = $this->signParameters($parameters);
00208 
00209         return $parameters;
00210     }
00211 
00232     protected function signParameters(array $paramaters)
00233     {
00234         $data = "POST\n";
00235         $data .= $this->_getRegion() . $this->_ec2Endpoint . "\n";
00236         $data .= "/\n";
00237 
00238         uksort($paramaters, 'strcmp');
00239         unset($paramaters['Signature']);
00240 
00241         $arrData = array();
00242         foreach($paramaters as $key => $value) {
00243             $arrData[] = $key . '=' . str_replace("%7E", "~", rawurlencode($value));
00244         }
00245 
00246         $data .= implode('&', $arrData);
00247 
00248         require_once 'Zend/Crypt/Hmac.php';
00249         $hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
00250 
00251         return base64_encode($hmac);
00252     }
00253 
00265     private function checkForErrors(Zend_Service_Amazon_Ec2_Response $response)
00266     {
00267         $xpath = new DOMXPath($response->getDocument());
00268         $list  = $xpath->query('//Error');
00269         if ($list->length > 0) {
00270             $node    = $list->item(0);
00271             $code    = $xpath->evaluate('string(Code/text())', $node);
00272             $message = $xpath->evaluate('string(Message/text())', $node);
00273             throw new Zend_Service_Amazon_Ec2_Exception($message, 0, $code);
00274         }
00275 
00276     }
00277 }
 All Data Structures Namespaces Files Functions Variables Enumerations