import v1.1.0_beta1 | 2009-08-21
This commit is contained in:
167
libs/Zend/Service/Amazon/Abstract.php
Normal file
167
libs/Zend/Service/Amazon/Abstract.php
Normal file
@ -0,0 +1,167 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Abstract.php';
|
||||
|
||||
/**
|
||||
* Abstract Amazon class that handles the credentials for any of the Web Services that
|
||||
* Amazon offers
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
|
||||
{
|
||||
/**
|
||||
* @var string Amazon Access Key
|
||||
*/
|
||||
protected static $_defaultAccessKey = null;
|
||||
|
||||
/**
|
||||
* @var string Amazon Secret Key
|
||||
*/
|
||||
protected static $_defaultSecretKey = null;
|
||||
|
||||
/**
|
||||
* @var string Amazon Region
|
||||
*/
|
||||
protected static $_defaultRegion = null;
|
||||
|
||||
/**
|
||||
* @var string Amazon Secret Key
|
||||
*/
|
||||
protected $_secretKey;
|
||||
|
||||
/**
|
||||
* @var string Amazon Access Key
|
||||
*/
|
||||
protected $_accessKey;
|
||||
|
||||
/**
|
||||
* @var string Amazon Region
|
||||
*/
|
||||
protected $_region;
|
||||
|
||||
/**
|
||||
* An array that contains all the valid Amazon Ec2 Regions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $_validEc2Regions = array('eu-west-1', 'us-east-1');
|
||||
|
||||
/**
|
||||
* Set the keys to use when accessing SQS.
|
||||
*
|
||||
* @param string $access_key Set the default Access Key
|
||||
* @param string $secret_key Set the default Secret Key
|
||||
* @return void
|
||||
*/
|
||||
public static function setKeys($accessKey, $secretKey)
|
||||
{
|
||||
self::$_defaultAccessKey = $accessKey;
|
||||
self::$_defaultSecretKey = $secretKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set which region you are working in. It will append the
|
||||
* end point automaticly
|
||||
*
|
||||
* @param string $region
|
||||
*/
|
||||
public static function setRegion($region)
|
||||
{
|
||||
if(in_array(strtolower($region), self::$_validEc2Regions, true)) {
|
||||
self::$_defaultRegion = $region;
|
||||
} else {
|
||||
require_once 'Zend/Service/Amazon/Exception.php';
|
||||
throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Amazon Sqs client.
|
||||
*
|
||||
* @param string $access_key Override the default Access Key
|
||||
* @param string $secret_key Override the default Secret Key
|
||||
* @param string $region Sets the AWS Region
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($accessKey=null, $secretKey=null, $region=null)
|
||||
{
|
||||
if(!$accessKey) {
|
||||
$accessKey = self::$_defaultAccessKey;
|
||||
}
|
||||
if(!$secretKey) {
|
||||
$secretKey = self::$_defaultSecretKey;
|
||||
}
|
||||
if(!$region) {
|
||||
$region = self::$_defaultRegion;
|
||||
} else {
|
||||
// make rue the region is valid
|
||||
if(!empty($region) && !in_array(strtolower($region), self::$_validEc2Regions, true)) {
|
||||
require_once 'Zend/Service/Amazon/Exception.php';
|
||||
throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
|
||||
}
|
||||
}
|
||||
|
||||
if(!$accessKey || !$secretKey) {
|
||||
require_once 'Zend/Service/Amazon/Exception.php';
|
||||
throw new Zend_Service_Amazon_Exception("AWS keys were not supplied");
|
||||
}
|
||||
$this->_accessKey = $accessKey;
|
||||
$this->_secretKey = $secretKey;
|
||||
$this->_region = $region;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch the AWS Region
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _getRegion()
|
||||
{
|
||||
return (!empty($this->_region)) ? $this->_region . '.' : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch the Access Key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _getAccessKey()
|
||||
{
|
||||
return $this->_accessKey;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to fetch the Secret AWS Key
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _getSecretKey()
|
||||
{
|
||||
return $this->_secretKey;
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Accessories.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: Accessories.php 12662 2008-11-15 15:29:58Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,16 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_Accessories
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $ASIN;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Title;
|
||||
|
||||
/**
|
||||
* Assigns values to properties relevant to Accessories
|
||||
*
|
||||
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: CustomerReview.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: CustomerReview.php 12662 2008-11-15 15:29:58Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,41 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_CustomerReview
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Rating;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $HelpfulVotes;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $CustomerId;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $TotalVotes;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Date;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Summary;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Content;
|
||||
|
||||
/**
|
||||
* Assigns values to properties relevant to CustomerReview
|
||||
*
|
||||
|
87
libs/Zend/Service/Amazon/Ec2.php
Normal file
87
libs/Zend/Service/Amazon/Ec2.php
Normal file
@ -0,0 +1,87 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
/**
|
||||
* Amazon Ec2 Interface to allow easy creation of the Ec2 Components
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2
|
||||
{
|
||||
/**
|
||||
* Factory method to fetch what you want to work with.
|
||||
*
|
||||
* @param string $section Create the method that you want to work with
|
||||
* @param string $key Override the default aws key
|
||||
* @param string $secret_key Override the default aws secretkey
|
||||
* @throws Zend_Service_Amazon_Ec2_Exception
|
||||
* @return object
|
||||
*/
|
||||
public static function factory($section, $key = null, $secret_key = null)
|
||||
{
|
||||
switch(strtolower($section)) {
|
||||
case 'keypair':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Keypair';
|
||||
break;
|
||||
case 'eip':
|
||||
// break left out
|
||||
case 'elasticip':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Elasticip';
|
||||
break;
|
||||
case 'ebs':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Ebs';
|
||||
break;
|
||||
case 'availabilityzones':
|
||||
// break left out
|
||||
case 'zones':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Availabilityzones';
|
||||
break;
|
||||
case 'ami':
|
||||
// break left out
|
||||
case 'image':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Image';
|
||||
break;
|
||||
case 'instance':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Instance';
|
||||
break;
|
||||
case 'security':
|
||||
// break left out
|
||||
case 'securitygroups':
|
||||
$class = 'Zend_Service_Amazon_Ec2_Securitygroups';
|
||||
break;
|
||||
default:
|
||||
throw new Zend_Service_Amazon_Ec2_Exception('Invalid Section: ' . $section);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!class_exists($class)) {
|
||||
require_once 'Zend/Loader.php';
|
||||
Zend_Loader::loadClass($class);
|
||||
}
|
||||
return new $class($key, $secret_key);
|
||||
}
|
||||
}
|
||||
|
201
libs/Zend/Service/Amazon/Ec2/Abstract.php
Normal file
201
libs/Zend/Service/Amazon/Ec2/Abstract.php
Normal file
@ -0,0 +1,201 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Abstract.php';
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Response.php';
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Exception.php';
|
||||
|
||||
/**
|
||||
* Provides the basic functionality to send a request to the Amazon Ec2 Query API
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
abstract class Zend_Service_Amazon_Ec2_Abstract extends Zend_Service_Amazon_Abstract
|
||||
{
|
||||
/**
|
||||
* The HTTP query server
|
||||
*/
|
||||
const EC2_ENDPOINT = 'ec2.amazonaws.com';
|
||||
|
||||
/**
|
||||
* The API version to use
|
||||
*/
|
||||
const EC2_API_VERSION = '2008-12-01';
|
||||
|
||||
/**
|
||||
* Signature Version
|
||||
*/
|
||||
const EC2_SIGNATURE_VERSION = '2';
|
||||
|
||||
/**
|
||||
* Signature Encoding Method
|
||||
*/
|
||||
const EC2_SIGNATURE_METHOD = 'HmacSHA256';
|
||||
|
||||
/**
|
||||
* Period after which HTTP request will timeout in seconds
|
||||
*/
|
||||
const HTTP_TIMEOUT = 10;
|
||||
|
||||
/**
|
||||
* Sends a HTTP request to the queue service using Zend_Http_Client
|
||||
*
|
||||
* @param array $params List of parameters to send with the request
|
||||
* @return Zend_Service_Amazon_Ec2_Response
|
||||
* @throws Zend_Service_Amazon_Ec2_Exception
|
||||
*/
|
||||
protected function sendRequest(array $params = array())
|
||||
{
|
||||
$url = 'https://' . $this->_getRegion() . self::EC2_ENDPOINT . '/';
|
||||
|
||||
$params = $this->addRequiredParameters($params);
|
||||
|
||||
try {
|
||||
/* @var $request Zend_Http_Client */
|
||||
$request = self::getHttpClient();
|
||||
$request->resetParameters();
|
||||
|
||||
$request->setConfig(array(
|
||||
'timeout' => self::HTTP_TIMEOUT
|
||||
));
|
||||
|
||||
$request->setUri($url);
|
||||
$request->setMethod(Zend_Http_Client::POST);
|
||||
$request->setParameterPost($params);
|
||||
|
||||
$httpResponse = $request->request();
|
||||
|
||||
|
||||
} catch (Zend_Http_Client_Exception $zhce) {
|
||||
$message = 'Error in request to AWS service: ' . $zhce->getMessage();
|
||||
throw new Zend_Service_Amazon_Ec2_Exception($message, $zhce->getCode());
|
||||
}
|
||||
|
||||
$response = new Zend_Service_Amazon_Ec2_Response($httpResponse);
|
||||
$this->checkForErrors($response);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds required authentication and version parameters to an array of
|
||||
* parameters
|
||||
*
|
||||
* The required parameters are:
|
||||
* - AWSAccessKey
|
||||
* - SignatureVersion
|
||||
* - Timestamp
|
||||
* - Version and
|
||||
* - Signature
|
||||
*
|
||||
* If a required parameter is already set in the <tt>$parameters</tt> array,
|
||||
* it is overwritten.
|
||||
*
|
||||
* @param array $parameters the array to which to add the required
|
||||
* parameters.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function addRequiredParameters(array $parameters)
|
||||
{
|
||||
$parameters['AWSAccessKeyId'] = $this->_getAccessKey();
|
||||
$parameters['SignatureVersion'] = self::EC2_SIGNATURE_VERSION;
|
||||
$parameters['Expires'] = gmdate('c');
|
||||
$parameters['Version'] = self::EC2_API_VERSION;
|
||||
$parameters['SignatureMethod'] = self::EC2_SIGNATURE_METHOD;
|
||||
$parameters['Signature'] = $this->signParameters($parameters);
|
||||
|
||||
return $parameters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes the RFC 2104-compliant HMAC signature for request parameters
|
||||
*
|
||||
* This implements the Amazon Web Services signature, as per the following
|
||||
* specification:
|
||||
*
|
||||
* 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
|
||||
* excluding <tt>Signature</tt>, the value of which is being created),
|
||||
* ignoring case.
|
||||
*
|
||||
* 2. Iterate over the sorted list and append the parameter name (in its
|
||||
* original case) and then its value. Do not URL-encode the parameter
|
||||
* values before constructing this string. Do not use any separator
|
||||
* characters when appending strings.
|
||||
*
|
||||
* @param array $parameters the parameters for which to get the signature.
|
||||
* @param string $secretKey the secret key to use to sign the parameters.
|
||||
*
|
||||
* @return string the signed data.
|
||||
*/
|
||||
protected function signParameters(array $paramaters)
|
||||
{
|
||||
$data = "POST\n";
|
||||
$data .= $this->_getRegion() . self::EC2_ENDPOINT . "\n";
|
||||
$data .= "/\n";
|
||||
|
||||
uksort($paramaters, 'strcmp');
|
||||
unset($paramaters['Signature']);
|
||||
|
||||
$arrData = array();
|
||||
foreach($paramaters as $key => $value) {
|
||||
$arrData[] = $key . '=' . str_replace("%7E", "~", urlencode($value));
|
||||
}
|
||||
|
||||
$data .= implode('&', $arrData);
|
||||
|
||||
require_once 'Zend/Crypt/Hmac.php';
|
||||
$hmac = Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Zend_Crypt_Hmac::BINARY);
|
||||
|
||||
return base64_encode($hmac);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks for errors responses from Amazon
|
||||
*
|
||||
* @param Zend_Service_Amazon_Ec2_Response $response the response object to
|
||||
* check.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws Zend_Service_Amazon_Ec2_Exception if one or more errors are
|
||||
* returned from Amazon.
|
||||
*/
|
||||
private function checkForErrors(Zend_Service_Amazon_Ec2_Response $response)
|
||||
{
|
||||
$xpath = new DOMXPath($response->getDocument());
|
||||
$list = $xpath->query('//Error');
|
||||
if ($list->length > 0) {
|
||||
$node = $list->item(0);
|
||||
$code = $xpath->evaluate('string(Code/text())', $node);
|
||||
$message = $xpath->evaluate('string(Message/text())', $node);
|
||||
throw new Zend_Service_Amazon_Ec2_Exception($message, 0, $code);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
73
libs/Zend/Service/Amazon/Ec2/Availabilityzones.php
Normal file
73
libs/Zend/Service/Amazon/Ec2/Availabilityzones.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface to query which Availibity Zones your account has access to.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Availabilityzones extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Describes availability zones that are currently available to the account
|
||||
* and their states.
|
||||
*
|
||||
* @param string|array $zoneName Name of an availability zone.
|
||||
* @return array An array that contains all the return items. Keys: zoneName and zoneState.
|
||||
*/
|
||||
public function describe($zoneName = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeAvailabilityZones';
|
||||
|
||||
if(is_array($zoneName) && !empty($zoneName)) {
|
||||
foreach($zoneName as $k=>$name) {
|
||||
$params['ZoneName.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($zoneName) {
|
||||
$params['ZoneName.1'] = $zoneName;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$nodes = $xpath->query('//ec2:item');
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $k => $node) {
|
||||
$item = array();
|
||||
$item['zoneName'] = $xpath->evaluate('string(ec2:zoneName/text())', $node);
|
||||
$item['zoneState'] = $xpath->evaluate('string(ec2:zoneState/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
340
libs/Zend/Service/Amazon/Ec2/Ebs.php
Normal file
340
libs/Zend/Service/Amazon/Ec2/Ebs.php
Normal file
@ -0,0 +1,340 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface to create, describe, attach, detach and delete Elastic Block
|
||||
* Storage Volumes and Snaphsots.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Ebs extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance.
|
||||
*
|
||||
* You must specify an availability zone when creating a volume. The volume and
|
||||
* any instance to which it attaches must be in the same availability zone.
|
||||
*
|
||||
* @param string $size The size of the volume, in GiB.
|
||||
* @param string $availabilityZone The availability zone in which to create the new volume.
|
||||
* @return array
|
||||
*/
|
||||
public function createNewVolume($size, $availabilityZone)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'CreateVolume';
|
||||
$params['AvailabilityZone'] = $availabilityZone;
|
||||
$params['Size'] = $size;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())');
|
||||
$return['size'] = $xpath->evaluate('string(//ec2:size/text())');
|
||||
$return['status'] = $xpath->evaluate('string(//ec2:status/text())');
|
||||
$return['createTime'] = $xpath->evaluate('string(//ec2:createTime/text())');
|
||||
$return['availabilityZone'] = $xpath->evaluate('string(//ec2:availabilityZone/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new Amazon EBS volume that you can mount from any Amazon EC2 instance.
|
||||
*
|
||||
* You must specify an availability zone when creating a volume. The volume and
|
||||
* any instance to which it attaches must be in the same availability zone.
|
||||
*
|
||||
* @param string $snapshotId The snapshot from which to create the new volume.
|
||||
* @param string $availabilityZone The availability zone in which to create the new volume.
|
||||
* @return array
|
||||
*/
|
||||
public function createVolumeFromSnapshot($snapshotId, $availabilityZone)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'CreateVolume';
|
||||
$params['AvailabilityZone'] = $availabilityZone;
|
||||
$params['SnapshotId'] = $snapshotId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())');
|
||||
$return['size'] = $xpath->evaluate('string(//ec2:size/text())');
|
||||
$return['status'] = $xpath->evaluate('string(//ec2:status/text())');
|
||||
$return['createTime'] = $xpath->evaluate('string(//ec2:createTime/text())');
|
||||
$return['availabilityZone'] = $xpath->evaluate('string(//ec2:availabilityZone/text())');
|
||||
$return['snapshotId'] = $xpath->evaluate('string(//ec2:snapshotId/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists one or more Amazon EBS volumes that you own, If you do not
|
||||
* specify any volumes, Amazon EBS returns all volumes that you own.
|
||||
*
|
||||
* @param string|array $volumeId The ID or array of ID's of the volume(s) to list
|
||||
* @return array
|
||||
*/
|
||||
public function describeVolume($volumeId = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeVolumes';
|
||||
|
||||
if(is_array($volumeId) && !empty($volumeId)) {
|
||||
foreach($volumeId as $k=>$name) {
|
||||
$params['VolumeId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($volumeId) {
|
||||
$params['VolumeId.1'] = $volumeId;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$nodes = $xpath->query('//ec2:volumeSet/ec2:item', $response->getDocument());
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $node) {
|
||||
$item = array();
|
||||
|
||||
$item['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $node);
|
||||
$item['size'] = $xpath->evaluate('string(ec2:size/text())', $node);
|
||||
$item['status'] = $xpath->evaluate('string(ec2:status/text())', $node);
|
||||
$item['createTime'] = $xpath->evaluate('string(ec2:createTime/text())', $node);
|
||||
|
||||
$attachmentSet = $xpath->query('ec2:attachmentSet/ec2:item', $node);
|
||||
if($attachmentSet->length == 1) {
|
||||
$_as = $attachmentSet->item(0);
|
||||
$as = array();
|
||||
$as['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $_as);
|
||||
$as['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $_as);
|
||||
$as['device'] = $xpath->evaluate('string(ec2:device/text())', $_as);
|
||||
$as['status'] = $xpath->evaluate('string(ec2:status/text())', $_as);
|
||||
$as['attachTime'] = $xpath->evaluate('string(ec2:attachTime/text())', $_as);
|
||||
$item['attachmentSet'] = $as;
|
||||
}
|
||||
|
||||
$return[] = $item;
|
||||
unset($item, $node);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function describeAttachedVolumes($instanceId)
|
||||
{
|
||||
$volumes = $this->describeVolume();
|
||||
|
||||
$return = array();
|
||||
foreach($volumes as $vol) {
|
||||
if(isset($vol['attachmentSet']) && $vol['attachmentSet']['instanceId'] == $instanceId) {
|
||||
$return[] = $vol;
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attaches an Amazon EBS volume to an instance
|
||||
*
|
||||
* @param string $volumeId The ID of the Amazon EBS volume
|
||||
* @param string $instanceId The ID of the instance to which the volume attaches
|
||||
* @param string $device Specifies how the device is exposed to the instance (e.g., /dev/sdh).
|
||||
* @return array
|
||||
*/
|
||||
public function attachVolume($volumeId, $instanceId, $device)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'AttachVolume';
|
||||
$params['VolumeId'] = $volumeId;
|
||||
$params['InstanceId'] = $instanceId;
|
||||
$params['Device'] = $device;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())');
|
||||
$return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())');
|
||||
$return['device'] = $xpath->evaluate('string(//ec2:device/text())');
|
||||
$return['status'] = $xpath->evaluate('string(//ec2:status/text())');
|
||||
$return['attachTime'] = $xpath->evaluate('string(//ec2:attachTime/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Detaches an Amazon EBS volume from an instance
|
||||
*
|
||||
* @param string $volumeId The ID of the Amazon EBS volume
|
||||
* @param string $instanceId The ID of the instance from which the volume will detach
|
||||
* @param string $device The device name
|
||||
* @param boolean $force Forces detachment if the previous detachment attempt did not occur cleanly
|
||||
* (logging into an instance, unmounting the volume, and detaching normally).
|
||||
* This option can lead to data loss or a corrupted file system. Use this option
|
||||
* only as a last resort to detach an instance from a failed instance. The
|
||||
* instance will not have an opportunity to flush file system caches nor
|
||||
* file system meta data.
|
||||
* @return array
|
||||
*/
|
||||
public function detachVolume($volumeId, $instanceId = null, $device = null, $force = false)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DetachVolume';
|
||||
$params['VolumeId'] = $volumeId;
|
||||
$params['InstanceId'] = $instanceId;
|
||||
$params['Device'] = $device;
|
||||
$params['Force'] = $force;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())');
|
||||
$return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())');
|
||||
$return['device'] = $xpath->evaluate('string(//ec2:device/text())');
|
||||
$return['status'] = $xpath->evaluate('string(//ec2:status/text())');
|
||||
$return['attachTime'] = $xpath->evaluate('string(//ec2:attachTime/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes an Amazon EBS volume
|
||||
*
|
||||
* @param string $volumeId The ID of the volume to delete
|
||||
* @return boolean
|
||||
*/
|
||||
public function deleteVolume($volumeId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DeleteVolume';
|
||||
$params['volumeId'] = $volumeId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a snapshot of an Amazon EBS volume and stores it in Amazon S3. You can use snapshots for backups,
|
||||
* to launch instances from identical snapshots, and to save data before shutting down an instance
|
||||
*
|
||||
* @param string $volumeId The ID of the Amazon EBS volume to snapshot
|
||||
* @return array
|
||||
*/
|
||||
public function createSnapshot($volumeId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'CreateSnapshot';
|
||||
$params['VolumeId'] = $volumeId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['snapshotId'] = $xpath->evaluate('string(//ec2:snapshotId/text())');
|
||||
$return['volumeId'] = $xpath->evaluate('string(//ec2:volumeId/text())');
|
||||
$return['status'] = $xpath->evaluate('string(//ec2:status/text())');
|
||||
$return['startTime'] = $xpath->evaluate('string(//ec2:startTime/text())');
|
||||
$return['progress'] = $xpath->evaluate('string(//ec2:progress/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Describes the status of Amazon EBS snapshots
|
||||
*
|
||||
* @param string|array $snapshotId The ID or arry of ID's of the Amazon EBS snapshot
|
||||
* @return array
|
||||
*/
|
||||
public function describeSnapshot($snapshotId = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeSnapshots';
|
||||
|
||||
if(is_array($snapshotId) && !empty($snapshotId)) {
|
||||
foreach($snapshotId as $k=>$name) {
|
||||
$params['SnapshotId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($snapshotId) {
|
||||
$params['SnapshotId.1'] = $snapshotId;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$nodes = $xpath->query('//ec2:snapshotSet/ec2:item', $response->getDocument());
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $node) {
|
||||
$item = array();
|
||||
|
||||
$item['snapshotId'] = $xpath->evaluate('string(ec2:snapshotId/text())', $node);
|
||||
$item['volumeId'] = $xpath->evaluate('string(ec2:volumeId/text())', $node);
|
||||
$item['status'] = $xpath->evaluate('string(ec2:status/text())', $node);
|
||||
$item['startTime'] = $xpath->evaluate('string(ec2:startTime/text())', $node);
|
||||
$item['progress'] = $xpath->evaluate('string(ec2:progress/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item, $node);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a snapshot of an Amazon EBS volume that is stored in Amazon S3
|
||||
*
|
||||
* @param string $snapshotId The ID of the Amazon EBS snapshot to delete
|
||||
* @return boolean
|
||||
*/
|
||||
public function deleteSnapshot($snapshotId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DeleteSnapshot';
|
||||
$params['SnapshotId'] = $snapshotId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
}
|
155
libs/Zend/Service/Amazon/Ec2/Elasticip.php
Normal file
155
libs/Zend/Service/Amazon/Ec2/Elasticip.php
Normal file
@ -0,0 +1,155 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface to allocate, associate, describe and release Elastic IP address
|
||||
* from your account.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Elasticip extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Acquires an elastic IP address for use with your account
|
||||
*
|
||||
* @return string Returns the newly Allocated IP Address
|
||||
*/
|
||||
public function allocate()
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'AllocateAddress';
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$ip = $xpath->evaluate('string(//ec2:publicIp/text())');
|
||||
|
||||
return $ip;
|
||||
}
|
||||
|
||||
/**
|
||||
* Lists elastic IP addresses assigned to your account.
|
||||
*
|
||||
* @param string|array $publicIp Elastic IP or list of addresses to describe.
|
||||
* @return array
|
||||
*/
|
||||
public function describe($publicIp = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeAddresses';
|
||||
|
||||
if(is_array($publicIp) && !empty($publicIp)) {
|
||||
foreach($publicIp as $k=>$name) {
|
||||
$params['PublicIp.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($publicIp) {
|
||||
$params['PublicIp.1'] = $publicIp;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$nodes = $xpath->query('//ec2:item');
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $k => $node) {
|
||||
$item = array();
|
||||
$item['publicIp'] = $xpath->evaluate('string(ec2:publicIp/text())', $node);
|
||||
$item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Releases an elastic IP address that is associated with your account
|
||||
*
|
||||
* @param string $publicIp IP address that you are releasing from your account.
|
||||
* @return boolean
|
||||
*/
|
||||
public function release($publicIp)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'ReleaseAddress';
|
||||
$params['PublicIp'] = $publicIp;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Associates an elastic IP address with an instance
|
||||
*
|
||||
* @param string $instanceId The instance to which the IP address is assigned
|
||||
* @param string $publicIp IP address that you are assigning to the instance.
|
||||
* @return boolean
|
||||
*/
|
||||
public function associate($instanceId, $publicIp)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'AssociateAddress';
|
||||
$params['PublicIp'] = $publicIp;
|
||||
$params['InstanceId'] = $instanceId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Disassociates the specified elastic IP address from the instance to which it is assigned.
|
||||
* This is an idempotent operation. If you enter it more than once, Amazon EC2 does not return an error.
|
||||
*
|
||||
* @param string $publicIp IP address that you are disassociating from the instance.
|
||||
* @return boolean
|
||||
*/
|
||||
public function disassocate($publicIp)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DisssociateAddress';
|
||||
$params['PublicIp'] = $publicIp;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
}
|
48
libs/Zend/Service/Amazon/Ec2/Exception.php
Normal file
48
libs/Zend/Service/Amazon/Ec2/Exception.php
Normal file
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Exception.php';
|
||||
|
||||
/**
|
||||
* The Custom Exception class that allows you to have access to the AWS Error Code.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Exception extends Zend_Service_Amazon_Exception
|
||||
{
|
||||
private $awsErrorCode = '';
|
||||
|
||||
public function __construct($message, $code = 0, $awsErrorCode = '')
|
||||
{
|
||||
parent::__construct($message, $code);
|
||||
$this->awsErrorCode = $awsErrorCode;
|
||||
}
|
||||
|
||||
public function getErrorCode()
|
||||
{
|
||||
return $this->awsErrorCode;
|
||||
}
|
||||
}
|
330
libs/Zend/Service/Amazon/Ec2/Image.php
Normal file
330
libs/Zend/Service/Amazon/Ec2/Image.php
Normal file
@ -0,0 +1,330 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface to register, describe and deregister Amamzon Machine Instances (AMI)
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Image extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Registers an AMI with Amazon EC2. Images must be registered before
|
||||
* they can be launched.
|
||||
*
|
||||
* Each AMI is associated with an unique ID which is provided by the Amazon
|
||||
* EC2 service through the RegisterImage operation. During registration, Amazon
|
||||
* EC2 retrieves the specified image manifest from Amazon S3 and verifies that
|
||||
* the image is owned by the user registering the image.
|
||||
*
|
||||
* The image manifest is retrieved once and stored within the Amazon EC2.
|
||||
* Any modifications to an image in Amazon S3 invalidates this registration.
|
||||
* If you make changes to an image, deregister the previous image and register
|
||||
* the new image. For more information, see DeregisterImage.
|
||||
*
|
||||
* @param string $imageLocation Full path to your AMI manifest in Amazon S3 storage.
|
||||
* @return string The ami fro the newly registred image;
|
||||
*/
|
||||
public function register($imageLocation)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'RegisterImage';
|
||||
$params['ImageLocation']= $imageLocation;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$amiId = $xpath->evaluate('string(//ec2:imageId/text())');
|
||||
|
||||
return $amiId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about AMIs, AKIs, and ARIs available to the user.
|
||||
* Information returned includes image type, product codes, architecture,
|
||||
* and kernel and RAM disk IDs. Images available to the user include public
|
||||
* images available for any user to launch, private images owned by the user
|
||||
* making the request, and private images owned by other users for which the
|
||||
* user has explicit launch permissions.
|
||||
*
|
||||
* Launch permissions fall into three categories:
|
||||
* public: The owner of the AMI granted launch permissions for the AMI
|
||||
* to the all group. All users have launch permissions for these AMIs.
|
||||
* explicit: The owner of the AMI granted launch permissions to a specific user.
|
||||
* implicit: A user has implicit launch permissions for all AMIs he or she owns.
|
||||
*
|
||||
* The list of AMIs returned can be modified by specifying AMI IDs, AMI owners,
|
||||
* or users with launch permissions. If no options are specified, Amazon EC2 returns
|
||||
* all AMIs for which the user has launch permissions.
|
||||
*
|
||||
* If you specify one or more AMI IDs, only AMIs that have the specified IDs are returned.
|
||||
* If you specify an invalid AMI ID, a fault is returned. If you specify an AMI ID for which
|
||||
* you do not have access, it will not be included in the returned results.
|
||||
*
|
||||
* If you specify one or more AMI owners, only AMIs from the specified owners and for
|
||||
* which you have access are returned. The results can include the account IDs of the
|
||||
* specified owners, amazon for AMIs owned by Amazon or self for AMIs that you own.
|
||||
*
|
||||
* If you specify a list of executable users, only users that have launch permissions
|
||||
* for the AMIs are returned. You can specify account IDs (if you own the AMI(s)), self
|
||||
* for AMIs for which you own or have explicit permissions, or all for public AMIs.
|
||||
*
|
||||
* @param string|array $imageId A list of image descriptions
|
||||
* @param string|array $owner Owners of AMIs to describe.
|
||||
* @param string|array $executableBy AMIs for which specified users have access.
|
||||
* @return array
|
||||
*/
|
||||
public function describe($imageId = null, $owner = null, $executableBy = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeImages';
|
||||
|
||||
if(is_array($imageId) && !empty($imageId)) {
|
||||
foreach($imageId as $k=>$name) {
|
||||
$params['ImageId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($imageId) {
|
||||
$params['ImageId.1'] = $imageId;
|
||||
}
|
||||
|
||||
if(is_array($owner) && !empty($owner)) {
|
||||
foreach($owner as $k=>$name) {
|
||||
$params['Owner.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($owner) {
|
||||
$params['Owner.1'] = $owner;
|
||||
}
|
||||
|
||||
if(is_array($executableBy) && !empty($executableBy)) {
|
||||
foreach($executableBy as $k=>$name) {
|
||||
$params['ExecutableBy.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($executableBy) {
|
||||
$params['ExecutableBy.1'] = $executableBy;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$nodes = $xpath->query('//ec2:imagesSet/ec2:item');
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $node) {
|
||||
$item = array();
|
||||
|
||||
$item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $node);
|
||||
$item['imageLocation'] = $xpath->evaluate('string(ec2:imageLocation/text())', $node);
|
||||
$item['imageState'] = $xpath->evaluate('string(ec2:imageState/text())', $node);
|
||||
$item['imageOwnerId'] = $xpath->evaluate('string(ec2:imageOwnerId/text())', $node);
|
||||
$item['isPublic'] = $xpath->evaluate('string(ec2:isPublic/text())', $node);
|
||||
$item['architecture'] = $xpath->evaluate('string(ec2:architecture/text())', $node);
|
||||
$item['imageType'] = $xpath->evaluate('string(ec2:imageType/text())', $node);
|
||||
$item['kernelId'] = $xpath->evaluate('string(ec2:kernelId/text())', $node);
|
||||
$item['ramdiskId'] = $xpath->evaluate('string(ec2:ramdiskId/text())', $node);
|
||||
$item['platform'] = $xpath->evaluate('string(ec2:platform/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item, $node);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deregisters an AMI. Once deregistered, instances of the AMI can no longer be launched.
|
||||
*
|
||||
* @param string $imageId Unique ID of a machine image, returned by a call
|
||||
* to RegisterImage or DescribeImages.
|
||||
* @return boolean
|
||||
*/
|
||||
public function deregister($imageId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DeregisterImage';
|
||||
$params['ImageId'] = $imageId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Modifies an attribute of an AMI.
|
||||
*
|
||||
* Valid Attributes:
|
||||
* launchPermission: Controls who has permission to launch the AMI. Launch permissions
|
||||
* can be granted to specific users by adding userIds.
|
||||
* To make the AMI public, add the all group.
|
||||
* productCodes: Associates a product code with AMIs. This allows developers to
|
||||
* charge users for using AMIs. The user must be signed up for the
|
||||
* product before they can launch the AMI. This is a write once attribute;
|
||||
* after it is set, it cannot be changed or removed.
|
||||
*
|
||||
* @param string $imageId AMI ID to modify.
|
||||
* @param string $attribute Specifies the attribute to modify. See the preceding
|
||||
* attributes table for supported attributes.
|
||||
* @param string $operationType Specifies the operation to perform on the attribute.
|
||||
* See the preceding attributes table for supported operations for attributes.
|
||||
* Valid Values: add | remove
|
||||
* Required for launchPermssion Attribute
|
||||
*
|
||||
* @param string|array $userId User IDs to add to or remove from the launchPermission attribute.
|
||||
* Required for launchPermssion Attribute
|
||||
* @param string|array $userGroup User groups to add to or remove from the launchPermission attribute.
|
||||
* Currently, the all group is available, which will make it a public AMI.
|
||||
* Required for launchPermssion Attribute
|
||||
* @param string $productCode Attaches a product code to the AMI. Currently only one product code
|
||||
* can be associated with an AMI. Once set, the product code cannot be changed or reset.
|
||||
* Required for productCodes Attribute
|
||||
* @return boolean
|
||||
*/
|
||||
public function modifyAttribute($imageId, $attribute, $operationType = 'add', $userId = null, $userGroup = null, $productCode = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'ModifyImageAttribute';
|
||||
$parmas['ImageId'] = $imageId;
|
||||
$params['Attribute'] = $attribute;
|
||||
|
||||
switch($attribute) {
|
||||
case 'launchPermission':
|
||||
// break left out
|
||||
case 'launchpermission':
|
||||
$params['Attribute'] = 'launchPermission';
|
||||
$params['OperationType'] = $operationType;
|
||||
|
||||
if(is_array($userId) && !empty($userId)) {
|
||||
foreach($userId as $k=>$name) {
|
||||
$params['UserId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($userId) {
|
||||
$params['UserId.1'] = $userId;
|
||||
}
|
||||
|
||||
if(is_array($userGroup) && !empty($userGroup)) {
|
||||
foreach($userGroup as $k=>$name) {
|
||||
$params['UserGroup.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($userGroup) {
|
||||
$params['UserGroup.1'] = $userGroup;
|
||||
}
|
||||
|
||||
break;
|
||||
case 'productCodes':
|
||||
// break left out
|
||||
case 'productcodes':
|
||||
$params['Attribute'] = 'productCodes';
|
||||
$parmas['ProductCode.1'] = $productCode;
|
||||
break;
|
||||
default:
|
||||
require_once 'Zend/Service/Amazon/Ec2/Exception.php';
|
||||
throw new Zend_Service_Amazon_Ec2_Exception('Invalid Attribute Passed In. Valid Image Attributes are launchPermission and productCode.');
|
||||
break;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about an attribute of an AMI. Only one attribute can be specified per call.
|
||||
*
|
||||
* @param string $imageId ID of the AMI for which an attribute will be described.
|
||||
* @param string $attribute Specifies the attribute to describe. Valid Attributes are
|
||||
* launchPermission, productCodes
|
||||
*/
|
||||
public function describeAttribute($imageId, $attribute)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeImageAttribute';
|
||||
$params['ImageId'] = $imageId;
|
||||
$params['Attribute'] = $attribute;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['imageId'] = $xpath->evaluate('string(//ec2:imageId/text())');
|
||||
|
||||
// check for launchPermission
|
||||
if($attribute == 'launchPermission') {
|
||||
$lPnodes = $xpath->query('//ec2:launchPermission/ec2:item');
|
||||
|
||||
if($lPnodes->length > 0) {
|
||||
$return['launchPermission'] = array();
|
||||
foreach($lPnodes as $node) {
|
||||
$return['launchPermission'][] = $xpath->evaluate('string(ec2:userId/text())', $node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check for product codes
|
||||
if($attribute == 'productCodes') {
|
||||
$pCnodes = $xpath->query('//ec2:productCodes/ec2:item');
|
||||
if($pCnodes->length > 0) {
|
||||
$return['productCodes'] = array();
|
||||
foreach($pCnodes as $node) {
|
||||
$return['productCodes'][] = $xpath->evaluate('string(ec2:productCode/text())', $node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets an attribute of an AMI to its default value. The productCodes attribute cannot be reset
|
||||
*
|
||||
* @param string $imageId ID of the AMI for which an attribute will be reset.
|
||||
* @param String $attribute Specifies the attribute to reset. Currently, only launchPermission is supported.
|
||||
* In the case of launchPermission, all public and explicit launch permissions for
|
||||
* the AMI are revoked.
|
||||
* @return boolean
|
||||
*/
|
||||
public function resetAttribute($imageId, $attribute)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'ResetImageAttribute';
|
||||
$params['ImageId'] = $imageId;
|
||||
$params['Attribute'] = $attribute;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
}
|
444
libs/Zend/Service/Amazon/Ec2/Instance.php
Normal file
444
libs/Zend/Service/Amazon/Ec2/Instance.php
Normal file
@ -0,0 +1,444 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface that allows yout to run, terminate, reboot and describe Amazon
|
||||
* Ec2 Instances.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Instance extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Constant for Small Instance TYpe
|
||||
*/
|
||||
const SMALL = 'm1.small';
|
||||
|
||||
/**
|
||||
* Constant for Large Instance TYpe
|
||||
*/
|
||||
const LARGE = 'm1.large';
|
||||
|
||||
/**
|
||||
* Constant for X-Large Instance TYpe
|
||||
*/
|
||||
const XLARGE = 'm1.xlarge';
|
||||
|
||||
/**
|
||||
* Constant for High CPU Medium Instance TYpe
|
||||
*/
|
||||
const HCPU_MEDIUM = 'c1.medium';
|
||||
|
||||
/**
|
||||
* Constant for High CPU X-Large Instance TYpe
|
||||
*/
|
||||
const HCPU_XLARGE = 'c1.xlarge';
|
||||
|
||||
|
||||
/**
|
||||
* Launches a specified number of Instances.
|
||||
*
|
||||
* If Amazon EC2 cannot launch the minimum number AMIs you request, no
|
||||
* instances launch. If there is insufficient capacity to launch the
|
||||
* maximum number of AMIs you request, Amazon EC2 launches as many
|
||||
* as possible to satisfy the requested maximum values.
|
||||
*
|
||||
* Every instance is launched in a security group. If you do not specify
|
||||
* a security group at launch, the instances start in your default security group.
|
||||
* For more information on creating security groups, see CreateSecurityGroup.
|
||||
*
|
||||
* An optional instance type can be specified. For information
|
||||
* about instance types, see Instance Types.
|
||||
*
|
||||
* You can provide an optional key pair ID for each image in the launch request
|
||||
* (for more information, see CreateKeyPair). All instances that are created
|
||||
* from images that use this key pair will have access to the associated public
|
||||
* key at boot. You can use this key to provide secure access to an instance of an
|
||||
* image on a per-instance basis. Amazon EC2 public images use this feature to
|
||||
* provide secure access without passwords.
|
||||
*
|
||||
* Launching public images without a key pair ID will leave them inaccessible.
|
||||
*
|
||||
* @param array $options An array that contins the options to start an instance.
|
||||
* Required Values:
|
||||
* imageId string ID of the AMI with which to launch instances.
|
||||
* Optional Values:
|
||||
* minCount integer Minimum number of instances to launch.
|
||||
* maxCount integer Maximum number of instances to launch.
|
||||
* keyName string Name of the key pair with which to launch instances.
|
||||
* securityGruop string|array Names of the security groups with which to associate the instances.
|
||||
* userData string The user data available to the launched instances. This should not be Base64 encoded.
|
||||
* instanceType constant Specifies the instance type.
|
||||
* placement string Specifies the availability zone in which to launch the instance(s). By default, Amazon EC2 selects an availability zone for you.
|
||||
* kernelId string The ID of the kernel with which to launch the instance.
|
||||
* ramdiskId string The ID of the RAM disk with which to launch the instance.
|
||||
* blockDeviceVirtualName string Specifies the virtual name to map to the corresponding device name. For example: instancestore0
|
||||
* blockDeviceName string Specifies the device to which you are mapping a virtual name. For example: sdb
|
||||
* @return array
|
||||
*/
|
||||
public function run(array $options)
|
||||
{
|
||||
$_defaultOptions = array(
|
||||
'minCount' => 1,
|
||||
'maxCount' => 1,
|
||||
'instanceType' => Zend_Service_Amazon_Ec2_Instance::SMALL
|
||||
);
|
||||
|
||||
// set / override the defualt optoins if they are not passed into the array;
|
||||
$options = array_merge($_defaultOptions, $options);
|
||||
|
||||
if(!isset($options['imageId'])) {
|
||||
require_once 'Zend/Service/Amazon/Ec2/Exception.php';
|
||||
throw new Zend_Service_Amazon_Ec2_Exception('No Image Id Provided');
|
||||
}
|
||||
|
||||
|
||||
$params = array();
|
||||
$params['Action'] = 'RunInstances';
|
||||
$params['ImageId'] = $options['imageId'];
|
||||
$params['MinCount'] = $options['minCount'];
|
||||
$params['MaxCount'] = $options['maxCount'];
|
||||
|
||||
if(isset($options['keyName'])) {
|
||||
$params['KeyName'] = $options['keyName'];
|
||||
}
|
||||
|
||||
if(is_array($options['securityGroup']) && !empty($options['securityGroup'])) {
|
||||
foreach($options['securityGroup'] as $k=>$name) {
|
||||
$params['SecurityGroup.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif(isset($options['securityGroup'])) {
|
||||
$params['SecurityGroup.1'] = $options['securityGroup'];
|
||||
}
|
||||
|
||||
if(isset($options['userData'])) {
|
||||
$params['UserData'] = base64_encode($options['userData']);
|
||||
}
|
||||
|
||||
if(isset($options['instanceType'])) {
|
||||
$params['InstanceType'] = $options['instanceType'];
|
||||
}
|
||||
|
||||
if(isset($options['placement'])) {
|
||||
$params['Placement.AvailabilityZone'] = $options['placement'];
|
||||
}
|
||||
|
||||
if(isset($options['kernelId'])) {
|
||||
$params['KernelId'] = $options['kernelId'];
|
||||
}
|
||||
|
||||
if(isset($options['ramdiskId'])) {
|
||||
$params['RamdiskId'] = $options['ramdiskId'];
|
||||
}
|
||||
|
||||
if(isset($options['blockDeviceVirtualName']) && isset($options['blockDeviceName'])) {
|
||||
$params['BlockDeviceMapping.n.VirtualName'] = $options['blockDeviceVirtualName'];
|
||||
$params['BlockDeviceMapping.n.DeviceName'] = $options['blockDeviceName'];
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
|
||||
$return['reservationId'] = $xpath->evaluate('string(//ec2:reservationId/text())');
|
||||
$return['ownerId'] = $xpath->evaluate('string(//ec2:ownerId/text())');
|
||||
|
||||
$gs = $xpath->query('//ec2:groupSet/ec2:item');
|
||||
foreach($gs as $gs_node) {
|
||||
$return['groupSet'][] = $xpath->evaluate('string(ec2:groupId/text())', $gs_node);
|
||||
unset($gs_node);
|
||||
}
|
||||
unset($gs);
|
||||
|
||||
$is = $xpath->query('//ec2:instancesSet/ec2:item');
|
||||
foreach($is as $is_node) {
|
||||
$item = array();
|
||||
|
||||
$item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $is_node);
|
||||
$item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $is_node);
|
||||
$item['instanceState']['code'] = $xpath->evaluate('string(ec2:instanceState/ec2:code/text())', $is_node);
|
||||
$item['instanceState']['name'] = $xpath->evaluate('string(ec2:instanceState/ec2:name/text())', $is_node);
|
||||
$item['privateDnsName'] = $xpath->evaluate('string(ec2:privateDnsName/text())', $is_node);
|
||||
$item['dnsName'] = $xpath->evaluate('string(ec2:dnsName/text())', $is_node);
|
||||
$item['keyName'] = $xpath->evaluate('string(ec2:keyName/text())', $is_node);
|
||||
$item['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $is_node);
|
||||
$item['amiLaunchIndex'] = $xpath->evaluate('string(ec2:amiLaunchIndex/text())', $is_node);
|
||||
$item['launchTime'] = $xpath->evaluate('string(ec2:launchTime/text())', $is_node);
|
||||
$item['availabilityZone'] = $xpath->evaluate('string(ec2:placement/ec2:availabilityZone/text())', $is_node);
|
||||
|
||||
$return['instances'][] = $item;
|
||||
unset($item);
|
||||
unset($is_node);
|
||||
}
|
||||
unset($is);
|
||||
|
||||
return $return;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about instances that you own.
|
||||
*
|
||||
* If you specify one or more instance IDs, Amazon EC2 returns information
|
||||
* for those instances. If you do not specify instance IDs, Amazon EC2
|
||||
* returns information for all relevant instances. If you specify an invalid
|
||||
* instance ID, a fault is returned. If you specify an instance that you do
|
||||
* not own, it will not be included in the returned results.
|
||||
*
|
||||
* Recently terminated instances might appear in the returned results.
|
||||
* This interval is usually less than one hour.
|
||||
*
|
||||
* @param string|array $instaceId Set of instances IDs of which to get the status.
|
||||
* @param boolean Ture to ignore Terminated Instances.
|
||||
* @return array
|
||||
*/
|
||||
public function describe($instanceId = null, $ignoreTerminated = false)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeInstances';
|
||||
|
||||
if(is_array($instanceId) && !empty($instanceId)) {
|
||||
foreach($instanceId as $k=>$name) {
|
||||
$params['InstanceId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($instanceId) {
|
||||
$params['InstanceId.1'] = $instanceId;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$nodes = $xpath->query('//ec2:reservationSet/ec2:item');
|
||||
|
||||
$return = array();
|
||||
|
||||
foreach($nodes as $node) {
|
||||
$return['reservationId'] = $xpath->evaluate('string(ec2:reservationId/text())', $node);
|
||||
$return['ownerId'] = $xpath->evaluate('string(ec2:ownerId/text())', $node);
|
||||
|
||||
$gs = $xpath->query('ec2:groupSet/ec2:item', $node);
|
||||
foreach($gs as $gs_node) {
|
||||
$return['groupSet'][] = $xpath->evaluate('string(ec2:groupId/text())', $gs_node);
|
||||
unset($gs_node);
|
||||
}
|
||||
unset($gs);
|
||||
|
||||
$is = $xpath->query('ec2:instancesSet/ec2:item', $node);
|
||||
$return['instances'] = array();
|
||||
foreach($is as $is_node) {
|
||||
if($xpath->evaluate('string(ec2:instanceState/ec2:code/text())', $is_node) == 48 && $ignoreTerminated) continue;
|
||||
|
||||
$item = array();
|
||||
|
||||
$item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $is_node);
|
||||
$item['imageId'] = $xpath->evaluate('string(ec2:imageId/text())', $is_node);
|
||||
$item['instanceState']['code'] = $xpath->evaluate('string(ec2:instanceState/ec2:code/text())', $is_node);
|
||||
$item['instanceState']['name'] = $xpath->evaluate('string(ec2:instanceState/ec2:name/text())', $is_node);
|
||||
$item['privateDnsName'] = $xpath->evaluate('string(ec2:privateDnsName/text())', $is_node);
|
||||
$item['dnsName'] = $xpath->evaluate('string(ec2:dnsName/text())', $is_node);
|
||||
$item['keyName'] = $xpath->evaluate('string(ec2:keyName/text())', $is_node);
|
||||
$item['productCode'] = $xpath->evaluate('string(ec2:productCodesSet/ec2:item/ec2:productCode/text())', $is_node);
|
||||
$item['instanceType'] = $xpath->evaluate('string(ec2:instanceType/text())', $is_node);
|
||||
$item['launchTime'] = $xpath->evaluate('string(ec2:launchTime/text())', $is_node);
|
||||
$item['availabilityZone'] = $xpath->evaluate('string(ec2:placement/ec2:availabilityZone/text())', $is_node);
|
||||
$item['kernelId'] = $xpath->evaluate('string(ec2:kernelId/text())', $is_node);
|
||||
$item['ramediskId'] = $xpath->evaluate('string(ec2:ramediskId/text())', $is_node);
|
||||
|
||||
$return['instances'][] = $item;
|
||||
unset($item);
|
||||
unset($is_node);
|
||||
}
|
||||
unset($is);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about instances that you own that were started from
|
||||
* a specific imageId
|
||||
*
|
||||
* Recently terminated instances might appear in the returned results.
|
||||
* This interval is usually less than one hour.
|
||||
*
|
||||
* @param string $imageId The imageId used to start the Instance.
|
||||
* @param boolean Ture to ignore Terminated Instances.
|
||||
* @return array
|
||||
*/
|
||||
public function describeByImageId($imageId, $ignoreTerminated = false)
|
||||
{
|
||||
$arrInstances = $this->describe(null, $ignoreTerminated);
|
||||
|
||||
$return = array();
|
||||
|
||||
foreach($arrInstances['instances'] as $k => $instance) {
|
||||
if($instance['imageId'] !== $imageId) continue;
|
||||
$instance['groupSet'] = $arrInstances['groupSet'][$k];
|
||||
$return[] = $instance;
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shuts down one or more instances. This operation is idempotent; if you terminate
|
||||
* an instance more than once, each call will succeed.
|
||||
*
|
||||
* Terminated instances will remain visible after termination (approximately one hour).
|
||||
*
|
||||
* @param string|array $instanceId One or more instance IDs returned.
|
||||
* @return array
|
||||
*/
|
||||
public function terminate($instanceId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'TerminateInstances';
|
||||
|
||||
if(is_array($instanceId) && !empty($instanceId)) {
|
||||
foreach($instanceId as $k=>$name) {
|
||||
$params['InstanceId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($instanceId) {
|
||||
$params['InstanceId.1'] = $instanceId;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$nodes = $xpath->query('//ec2:instancesSet/ec2:item');
|
||||
|
||||
$return = array();
|
||||
foreach($nodes as $node) {
|
||||
$item = array();
|
||||
|
||||
$item['instanceId'] = $xpath->evaluate('string(ec2:instanceId/text())', $node);
|
||||
$item['shutdownState']['code'] = $xpath->evaluate('string(ec2:shutdownState/ec2:code/text())', $node);
|
||||
$item['shutdownState']['name'] = $xpath->evaluate('string(ec2:shutdownState/ec2:name/text())', $node);
|
||||
$item['previousState']['code'] = $xpath->evaluate('string(ec2:previousState/ec2:code/text())', $node);
|
||||
$item['previousState']['name'] = $xpath->evaluate('string(ec2:previousState/ec2:name/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Requests a reboot of one or more instances.
|
||||
*
|
||||
* This operation is asynchronous; it only queues a request to reboot the specified instance(s). The operation
|
||||
* will succeed if the instances are valid and belong to the user. Requests to reboot terminated instances are ignored.
|
||||
*
|
||||
* @param string|array $instanceId One or more instance IDs.
|
||||
* @return boolean
|
||||
*/
|
||||
public function reboot($instanceId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'RebootInstances';
|
||||
|
||||
if(is_array($instanceId) && !empty($instanceId)) {
|
||||
foreach($instanceId as $k=>$name) {
|
||||
$params['InstanceId.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($instanceId) {
|
||||
$params['InstanceId.1'] = $instanceId;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($return === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves console output for the specified instance.
|
||||
*
|
||||
* Instance console output is buffered and posted shortly after instance boot, reboot, and termination.
|
||||
* Amazon EC2 preserves the most recent 64 KB output which will be available for at least one hour after the most recent post.
|
||||
*
|
||||
* @param string $instanceId An instance ID
|
||||
* @return array
|
||||
*/
|
||||
public function consoleOutput($instanceId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'GetConsoleOutput';
|
||||
$params['InstanceId'] = $instanceId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
|
||||
$return['instanceId'] = $xpath->evaluate('string(//ec2:instanceId/text())');
|
||||
$return['timestamp'] = $xpath->evaluate('string(//ec2:timestamp/text())');
|
||||
$return['output'] = base64_decode($xpath->evaluate('string(//ec2:output/text())'));
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the specified product code is attached to the specified instance.
|
||||
* The operation returns false if the product code is not attached to the instance.
|
||||
*
|
||||
* The confirmProduct operation can only be executed by the owner of the AMI.
|
||||
* This feature is useful when an AMI owner is providing support and wants to
|
||||
* verify whether a user's instance is eligible.
|
||||
*
|
||||
* @param string $productCode The product code to confirm.
|
||||
* @param string $instanceId The instance for which to confirm the product code.
|
||||
* @return array|boolean An array if the product code is attached to the instance, false if it is not.
|
||||
*/
|
||||
public function confirmProduct($productCode, $instanceId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'ConfirmProductInstance';
|
||||
$params['ProductCode'] = $productCode;
|
||||
$params['InstanceId'] = $instanceId;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$result = $xpath->evaluate('string(//ec2:result/text())');
|
||||
|
||||
if($result === "true") {
|
||||
$return['result'] = true;
|
||||
$return['ownerId'] = $xpath->evaluate('string(//ec2:ownerId/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
134
libs/Zend/Service/Amazon/Ec2/Keypair.php
Normal file
134
libs/Zend/Service/Amazon/Ec2/Keypair.php
Normal file
@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface to create, delete and describe Ec2 KeyPairs.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Keypair extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Creates a new 2048 bit RSA key pair and returns a unique ID that can
|
||||
* be used to reference this key pair when launching new instances.
|
||||
*
|
||||
* @param string $keyName A unique name for the key pair.
|
||||
* @throws Zend_Service_Amazon_Ec2_Exception
|
||||
* @return array
|
||||
*/
|
||||
public function create($keyName)
|
||||
{
|
||||
$params = array();
|
||||
|
||||
$params['Action'] = 'CreateKeyPair';
|
||||
|
||||
if(!$keyName) {
|
||||
require_once 'Zend/Service/Amazon/Ec2/Exception.php';
|
||||
throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
|
||||
}
|
||||
|
||||
$params['KeyName'] = $keyName;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
$return['keyName'] = $xpath->evaluate('string(//ec2:keyName/text())');
|
||||
$return['keyFingerprint'] = $xpath->evaluate('string(//ec2:keyFingerprint/text())');
|
||||
$return['keyMaterial'] = $xpath->evaluate('string(//ec2:keyMaterial/text())');
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about key pairs available to you. If you specify
|
||||
* key pairs, information about those key pairs is returned. Otherwise,
|
||||
* information for all registered key pairs is returned.
|
||||
*
|
||||
* @param string|rarray $keyName Key pair IDs to describe.
|
||||
* @return array
|
||||
*/
|
||||
public function describe($keyName = null)
|
||||
{
|
||||
$params = array();
|
||||
|
||||
$params['Action'] = 'DescribeKeyPairs';
|
||||
if(is_array($keyName) && !empty($keyName)) {
|
||||
foreach($keyName as $k=>$name) {
|
||||
$params['KeyName.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($keyName) {
|
||||
$params['KeyName.1'] = $keyName;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$nodes = $xpath->query('//ec2:keySet/ec2:item');
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $k => $node) {
|
||||
$item = array();
|
||||
$item['keyName'] = $xpath->evaluate('string(ec2:keyName/text())', $node);
|
||||
$item['keyFingerprint'] = $xpath->evaluate('string(ec2:keyFingerprint/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a key pair
|
||||
*
|
||||
* @param string $keyName Name of the key pair to delete.
|
||||
* @throws Zend_Service_Amazon_Ec2_Exception
|
||||
* @return boolean Return true or false from the deletion.
|
||||
*/
|
||||
public function delete($keyName)
|
||||
{
|
||||
$params = array();
|
||||
|
||||
$params['Action'] = 'DeleteKeyPair';
|
||||
|
||||
if(!$keyName) {
|
||||
require_once 'Zend/Service/Amazon/Ec2/Exception.php';
|
||||
throw new Zend_Service_Amazon_Ec2_Exception('Invalid Key Name');
|
||||
}
|
||||
|
||||
$params['KeyName'] = $keyName;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($success === "true");
|
||||
}
|
||||
}
|
73
libs/Zend/Service/Amazon/Ec2/Region.php
Normal file
73
libs/Zend/Service/Amazon/Ec2/Region.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
/**
|
||||
* An Amazon EC2 interface to query which Regions your account has access to.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Region extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
|
||||
/**
|
||||
* Describes availability zones that are currently available to the account
|
||||
* and their states.
|
||||
*
|
||||
* @param string|array $region Name of an region.
|
||||
* @return array An array that contains all the return items. Keys: regionName and regionUrl.
|
||||
*/
|
||||
public function describe($region = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeRegions';
|
||||
|
||||
if(is_array($region) && !empty($region)) {
|
||||
foreach($region as $k=>$name) {
|
||||
$params['Region.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($region) {
|
||||
$params['Region.1'] = $region;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
|
||||
$xpath = $response->getXPath();
|
||||
$nodes = $xpath->query('//ec2:item');
|
||||
|
||||
$return = array();
|
||||
foreach ($nodes as $k => $node) {
|
||||
$item = array();
|
||||
$item['regionName'] = $xpath->evaluate('string(ec2:regionName/text())', $node);
|
||||
$item['regionUrl'] = $xpath->evaluate('string(ec2:regionUrl/text())', $node);
|
||||
|
||||
$return[] = $item;
|
||||
unset($item);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
139
libs/Zend/Service/Amazon/Ec2/Response.php
Normal file
139
libs/Zend/Service/Amazon/Ec2/Response.php
Normal file
@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Http/Response.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Response {
|
||||
/**
|
||||
* XML namespace used for EC2 responses.
|
||||
*/
|
||||
const XML_NAMESPACE = 'http://ec2.amazonaws.com/doc/2008-12-01/';
|
||||
|
||||
/**
|
||||
* The original HTTP response
|
||||
*
|
||||
* This contains the response body and headers.
|
||||
*
|
||||
* @var Zend_Http_Response
|
||||
*/
|
||||
private $_httpResponse = null;
|
||||
|
||||
/**
|
||||
* The response document object
|
||||
*
|
||||
* @var DOMDocument
|
||||
*/
|
||||
private $_document = null;
|
||||
|
||||
/**
|
||||
* The response XPath
|
||||
*
|
||||
* @var DOMXPath
|
||||
*/
|
||||
private $_xpath = null;
|
||||
|
||||
/**
|
||||
* Last error code
|
||||
*
|
||||
* @var integer
|
||||
*/
|
||||
private $_errorCode = 0;
|
||||
|
||||
/**
|
||||
* Last error message
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $_errorMessage = '';
|
||||
|
||||
/**
|
||||
* Creates a new high-level EC2 response object
|
||||
*
|
||||
* @param Zend_Http_Response $httpResponse the HTTP response.
|
||||
*/
|
||||
public function __construct(Zend_Http_Response $httpResponse)
|
||||
{
|
||||
$this->_httpResponse = $httpResponse;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the XPath object for this response
|
||||
*
|
||||
* @return DOMXPath the XPath object for response.
|
||||
*/
|
||||
public function getXPath()
|
||||
{
|
||||
if ($this->_xpath === null) {
|
||||
$document = $this->getDocument();
|
||||
if ($document === false) {
|
||||
$this->_xpath = false;
|
||||
} else {
|
||||
$this->_xpath = new DOMXPath($document);
|
||||
$this->_xpath->registerNamespace('ec2',
|
||||
self::XML_NAMESPACE);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_xpath;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the document object for this response
|
||||
*
|
||||
* @return DOMDocument the DOM Document for this response.
|
||||
*/
|
||||
public function getDocument()
|
||||
{
|
||||
try {
|
||||
$body = $this->_httpResponse->getBody();
|
||||
} catch (Zend_Http_Exception $e) {
|
||||
$body = false;
|
||||
}
|
||||
|
||||
if ($this->_document === null) {
|
||||
if ($body !== false) {
|
||||
// turn off libxml error handling
|
||||
$errors = libxml_use_internal_errors();
|
||||
|
||||
$this->_document = new DOMDocument();
|
||||
if (!$this->_document->loadXML($body)) {
|
||||
$this->_document = false;
|
||||
}
|
||||
|
||||
// reset libxml error handling
|
||||
libxml_clear_errors();
|
||||
libxml_use_internal_errors($errors);
|
||||
} else {
|
||||
$this->_document = false;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_document;
|
||||
}
|
||||
}
|
288
libs/Zend/Service/Amazon/Ec2/Securitygroups.php
Normal file
288
libs/Zend/Service/Amazon/Ec2/Securitygroups.php
Normal file
@ -0,0 +1,288 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id$
|
||||
*/
|
||||
|
||||
require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
|
||||
|
||||
/**
|
||||
* An Amazon EC2 interface to create, delete, describe, grand and revoke sercurity permissions.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service_Amazon
|
||||
* @subpackage Ec2
|
||||
* @copyright Copyright (c) 22005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Ec2_Securitygroups extends Zend_Service_Amazon_Ec2_Abstract
|
||||
{
|
||||
/**
|
||||
* Creates a new security group.
|
||||
*
|
||||
* Every instance is launched in a security group. If no security group is specified
|
||||
* during launch, the instances are launched in the default security group. Instances
|
||||
* within the same security group have unrestricted network access to each other.
|
||||
* Instances will reject network access attempts from other instances in a different
|
||||
* security group. As the owner of instances you can grant or revoke specific permissions
|
||||
* using the {@link authorizeIp}, {@link authorizeGroup}, {@link revokeGroup} and
|
||||
* {$link revokeIp} operations.
|
||||
*
|
||||
* @param string $name Name of the new security group.
|
||||
* @param string $description Description of the new security group.
|
||||
* @return boolean
|
||||
*/
|
||||
public function create($name, $description)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'CreateSecurityGroup';
|
||||
$params['GroupName'] = $name;
|
||||
$params['GroupDescription'] = $description;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($success === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns information about security groups that you own.
|
||||
*
|
||||
* If you specify security group names, information about those security group is returned.
|
||||
* Otherwise, information for all security group is returned. If you specify a group
|
||||
* that does not exist, a fault is returned.
|
||||
*
|
||||
* @param string|array $name List of security groups to describe
|
||||
* @return array
|
||||
*/
|
||||
public function describe($name = null)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DescribeSecurityGroups';
|
||||
if(is_array($name) && !empty($name)) {
|
||||
foreach($name as $k=>$name) {
|
||||
$params['GroupName.' . ($k+1)] = $name;
|
||||
}
|
||||
} elseif($name) {
|
||||
$params['GroupName.1'] = $name;
|
||||
}
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
|
||||
$return = array();
|
||||
|
||||
$nodes = $xpath->query('//ec2:securityGroupInfo/ec2:item');
|
||||
|
||||
foreach($nodes as $node) {
|
||||
$item = array();
|
||||
|
||||
$item['ownerId'] = $xpath->evaluate('string(ec2:ownerId/text())', $node);
|
||||
$item['groupName'] = $xpath->evaluate('string(ec2:groupName/text())', $node);
|
||||
$item['groupDescription'] = $xpath->evaluate('string(ec2:groupDescription/text())', $node);
|
||||
|
||||
$ip_nodes = $xpath->query('ec2:ipPermissions/ec2:item', $node);
|
||||
|
||||
foreach($ip_nodes as $ip_node) {
|
||||
$sItem = array();
|
||||
|
||||
$sItem['ipProtocol'] = $xpath->evaluate('string(ec2:ipProtocol/text())', $ip_node);
|
||||
$sItem['fromPort'] = $xpath->evaluate('string(ec2:fromPort/text())', $ip_node);
|
||||
$sItem['toPort'] = $xpath->evaluate('string(ec2:toPort/text())', $ip_node);
|
||||
$sItem['ipRanges'] = $xpath->evaluate('string(ec2:ipRanges/ec2:item/ec2:cidrIp/text())', $ip_node);
|
||||
|
||||
$item['ipPermissions'][] = $sItem;
|
||||
unset($ip_node, $sItem);
|
||||
}
|
||||
|
||||
$return[] = $item;
|
||||
|
||||
unset($item, $node);
|
||||
}
|
||||
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a security group.
|
||||
*
|
||||
* If you attempt to delete a security group that contains instances, a fault is returned.
|
||||
* If you attempt to delete a security group that is referenced by another security group,
|
||||
* a fault is returned. For example, if security group B has a rule that allows access
|
||||
* from security group A, security group A cannot be deleted until the allow rule is removed.
|
||||
*
|
||||
* @param string $name Name of the security group to delete.
|
||||
* @return boolean
|
||||
*/
|
||||
public function delete($name)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'DeleteSecurityGroup';
|
||||
$params['GroupName'] = $name;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($success === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds permissions to a security group
|
||||
*
|
||||
* Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request
|
||||
* (by IP range or an Amazon EC2 user-group pair), the source and destination port ranges
|
||||
* (for TCP and UDP), and the ICMP codes and types (for ICMP). When authorizing ICMP, -1
|
||||
* can be used as a wildcard in the type and code fields.
|
||||
*
|
||||
* Permission changes are propagated to instances within the security group as quickly as
|
||||
* possible. However, depending on the number of instances, a small delay might occur.
|
||||
*
|
||||
*
|
||||
* @param string $name Name of the group to modify.
|
||||
* @param string $ipProtocol IP protocol to authorize access to when operating on a CIDR IP.
|
||||
* @param integer $fromPort Bottom of port range to authorize access to when operating on a CIDR IP.
|
||||
* This contains the ICMP type if ICMP is being authorized.
|
||||
* @param integer $toPort Top of port range to authorize access to when operating on a CIDR IP.
|
||||
* This contains the ICMP code if ICMP is being authorized.
|
||||
* @param string $cidrIp CIDR IP range to authorize access to when operating on a CIDR IP.
|
||||
* @return boolean
|
||||
*/
|
||||
public function authorizeIp($name, $ipProtocol, $fromPort, $toPort, $cidrIp)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'AuthorizeSecurityGroupIngress';
|
||||
$params['GroupName'] = $name;
|
||||
$params['IpProtocol'] = $ipProtocol;
|
||||
$params['FromPort'] = $fromPort;
|
||||
$params['ToPort'] = $toPort;
|
||||
$params['CidrIp'] = $cidrIp;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($success === "true");
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds permissions to a security group
|
||||
*
|
||||
* When authorizing a user/group pair permission, GroupName, SourceSecurityGroupName and
|
||||
* SourceSecurityGroupOwnerId must be specified.
|
||||
*
|
||||
* Permission changes are propagated to instances within the security group as quickly as
|
||||
* possible. However, depending on the number of instances, a small delay might occur.
|
||||
*
|
||||
* @param string $name Name of the group to modify.
|
||||
* @param string $groupName Name of security group to authorize access to when operating on a user/group pair.
|
||||
* @param string $ownerId Owner of security group to authorize access to when operating on a user/group pair.
|
||||
* @return boolean
|
||||
*/
|
||||
public function authorizeGroup($name, $groupName, $ownerId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'AuthorizeSecurityGroupIngress';
|
||||
$params['GroupName'] = $name;
|
||||
$params['SourceSecurityGroupName'] = $groupName;
|
||||
$params['SourceSecurityGroupOwnerId'] = $ownerId;
|
||||
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
|
||||
return ($success === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Revokes permissions from a security group. The permissions used to revoke must be specified
|
||||
* using the same values used to grant the permissions.
|
||||
*
|
||||
* Permissions are specified by the IP protocol (TCP, UDP or ICMP), the source of the request
|
||||
* (by IP range or an Amazon EC2 user-group pair), the source and destination port ranges
|
||||
* (for TCP and UDP), and the ICMP codes and types (for ICMP). When authorizing ICMP, -1
|
||||
* can be used as a wildcard in the type and code fields.
|
||||
*
|
||||
* Permission changes are propagated to instances within the security group as quickly as
|
||||
* possible. However, depending on the number of instances, a small delay might occur.
|
||||
*
|
||||
*
|
||||
* @param string $name Name of the group to modify.
|
||||
* @param string $ipProtocol IP protocol to revoke access to when operating on a CIDR IP.
|
||||
* @param integer $fromPort Bottom of port range to revoke access to when operating on a CIDR IP.
|
||||
* This contains the ICMP type if ICMP is being revoked.
|
||||
* @param integer $toPort Top of port range to revoked access to when operating on a CIDR IP.
|
||||
* This contains the ICMP code if ICMP is being revoked.
|
||||
* @param string $cidrIp CIDR IP range to revoke access to when operating on a CIDR IP.
|
||||
* @return boolean
|
||||
*/
|
||||
public function revokeIp($name, $ipProtocol, $fromPort, $toPort, $cidrIp)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'RevokeSecurityGroupIngress';
|
||||
$params['GroupName'] = $name;
|
||||
$params['IpProtocol'] = $ipProtocol;
|
||||
$params['FromPort'] = $fromPort;
|
||||
$params['ToPort'] = $toPort;
|
||||
$params['CidrIp'] = $cidrIp;
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
return ($success === "true");
|
||||
}
|
||||
|
||||
/**
|
||||
* Revokes permissions from a security group. The permissions used to revoke must be specified
|
||||
* using the same values used to grant the permissions.
|
||||
*
|
||||
* Permission changes are propagated to instances within the security group as quickly as
|
||||
* possible. However, depending on the number of instances, a small delay might occur.
|
||||
*
|
||||
* When revoking a user/group pair permission, GroupName, SourceSecurityGroupName and
|
||||
* SourceSecurityGroupOwnerId must be specified.
|
||||
*
|
||||
* @param string $name Name of the group to modify.
|
||||
* @param string $groupName Name of security group to revoke access to when operating on a user/group pair.
|
||||
* @param string $ownerId Owner of security group to revoke access to when operating on a user/group pair.
|
||||
* @return boolean
|
||||
*/
|
||||
public function revokeGroup($name, $groupName, $ownerId)
|
||||
{
|
||||
$params = array();
|
||||
$params['Action'] = 'RevokeSecurityGroupIngress';
|
||||
$params['GroupName'] = $name;
|
||||
$params['SourceSecurityGroupName'] = $groupName;
|
||||
$params['SourceSecurityGroupOwnerId'] = $ownerId;
|
||||
|
||||
|
||||
$response = $this->sendRequest($params);
|
||||
$xpath = $response->getXPath();
|
||||
$success = $xpath->evaluate('string(//ec2:return/text())');
|
||||
|
||||
|
||||
return ($success === "true");
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: EditorialReview.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: EditorialReview.php 12662 2008-11-15 15:29:58Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,16 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_EditorialReview
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Source;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Content;
|
||||
|
||||
/**
|
||||
* Assigns values to properties relevant to EditorialReview
|
||||
*
|
||||
|
35
libs/Zend/Service/Amazon/Exception.php
Executable file
35
libs/Zend/Service/Amazon/Exception.php
Executable file
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
*/
|
||||
|
||||
/**
|
||||
* Zend_Service_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Exception.php';
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_Exception extends Zend_Service_Exception
|
||||
{}
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Item.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: Item.php 12666 2008-11-15 16:41:02Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,20 +31,80 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_Item
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $ASIN;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $DetailPageURL;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $SalesRank;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $TotalReviews;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $AverageRating;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $SmallImage;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $MediumImage;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $LargeImage;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Subjects;
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_OfferSet
|
||||
*/
|
||||
public $Offers;
|
||||
public $CustomerReviews;
|
||||
public $SimilarProducts;
|
||||
public $Accessories;
|
||||
public $Tracks;
|
||||
public $ListmaniaLists;
|
||||
public $PromotionalTag;
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_CustomerReview[]
|
||||
*/
|
||||
public $CustomerReviews = array();
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_SimilarProducts[]
|
||||
*/
|
||||
public $SimilarProducts = array();
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_Accessories[]
|
||||
*/
|
||||
public $Accessories = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $Tracks = array();
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_ListmaniaLists[]
|
||||
*/
|
||||
public $ListmaniaLists = array();
|
||||
|
||||
protected $_dom;
|
||||
|
||||
@ -153,7 +213,9 @@ class Zend_Service_Amazon_Item
|
||||
if ($result->length > 1) {
|
||||
foreach ($result as $disk) {
|
||||
foreach ($xpath->query('./*/text()', $disk) as $t) {
|
||||
$this->Tracks[$disk->getAttribute('number')] = (string) $t->data;
|
||||
// TODO: For consistency in a bugfix all tracks are appended to one single array
|
||||
// Erroreous line: $this->Tracks[$disk->getAttribute('number')] = (string) $t->data;
|
||||
$this->Tracks[] = (string) $t->data;
|
||||
}
|
||||
}
|
||||
} else if ($result->length == 1) {
|
||||
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: ListmaniaList.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: ListmaniaList.php 12662 2008-11-15 15:29:58Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,16 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_ListmaniaList
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $ListId;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $ListName;
|
||||
|
||||
/**
|
||||
* Assigns values to properties relevant to ListmaniaList
|
||||
*
|
||||
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Offer.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: Offer.php 14534 2009-03-30 11:14:53Z yoshida@zend.co.jp $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,46 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_Offer
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $MerchantId;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $GlancePage;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Condition;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $OfferListingId;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Price;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $CurrencyCode;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Availability;
|
||||
|
||||
/**
|
||||
* @var boolean
|
||||
*/
|
||||
public $IsEligibleForSuperSaverShipping = false;
|
||||
|
||||
/**
|
||||
* Parse the given Offer element
|
||||
*
|
||||
@ -47,7 +87,10 @@ class Zend_Service_Amazon_Offer
|
||||
$this->OfferListingId = (string) $xpath->query('./az:OfferListing/az:OfferListingId/text()', $dom)->item(0)->data;
|
||||
$this->Price = (int) $xpath->query('./az:OfferListing/az:Price/az:Amount/text()', $dom)->item(0)->data;
|
||||
$this->CurrencyCode = (string) $xpath->query('./az:OfferListing/az:Price/az:CurrencyCode/text()', $dom)->item(0)->data;
|
||||
$this->Availability = (string) $xpath->query('./az:OfferListing/az:Availability/text()', $dom)->item(0)->data;
|
||||
$availability = $xpath->query('./az:OfferListing/az:Availability/text()', $dom)->item(0);
|
||||
if($availability instanceof DOMText) {
|
||||
$this->Availability = (string) $availability->data;
|
||||
}
|
||||
$result = $xpath->query('./az:OfferListing/az:IsEligibleForSuperSaverShipping/text()', $dom);
|
||||
if ($result->length >= 1) {
|
||||
$this->IsEligibleForSuperSaverShipping = (bool) $result->item(0)->data;
|
||||
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: OfferSet.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: OfferSet.php 12662 2008-11-15 15:29:58Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,51 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_OfferSet
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $LowestNewPrice;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $LowestNewPriceCurrency;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $LowestUsedPrice;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $LowestUsedPriceCurrency;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $TotalNew;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $TotalUsed;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $TotalCollectible;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
public $TotalRefurbished;
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_Offer[]
|
||||
*/
|
||||
public $Offers;
|
||||
|
||||
/**
|
||||
* Parse the given Offer Set Element
|
||||
*
|
||||
|
802
libs/Zend/Service/Amazon/S3.php
Executable file
802
libs/Zend/Service/Amazon/S3.php
Executable file
@ -0,0 +1,802 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon_S3
|
||||
* @copyright Copyright (c) 2009, Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: S3.php 14190 2009-02-28 04:32:18Z jplock $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Service_Amazon_Abstract
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/Abstract.php';
|
||||
|
||||
/**
|
||||
* @see Zend_Crypt_Hmac
|
||||
*/
|
||||
require_once 'Zend/Crypt/Hmac.php';
|
||||
|
||||
/**
|
||||
* Amazon S3 PHP connection class
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon_S3
|
||||
* @copyright Copyright (c) 2005-2008, Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @see http://docs.amazonwebservices.com/AmazonS3/2006-03-01/
|
||||
*/
|
||||
class Zend_Service_Amazon_S3 extends Zend_Service_Amazon_Abstract
|
||||
{
|
||||
/**
|
||||
* Store for stream wrapper clients
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $_wrapperClients = array();
|
||||
/**
|
||||
* Endpoint for the service
|
||||
*
|
||||
* @var Zend_Uri_Http
|
||||
*/
|
||||
protected $_endpoint;
|
||||
|
||||
const S3_ENDPOINT = 's3.amazonaws.com';
|
||||
|
||||
const S3_ACL_PRIVATE = 'private';
|
||||
const S3_ACL_PUBLIC_READ = 'public-read';
|
||||
const S3_ACL_PUBLIC_WRITE = 'public-read-write';
|
||||
const S3_ACL_AUTH_READ = 'authenticated-read';
|
||||
|
||||
const S3_REQUESTPAY_HEADER = 'x-amz-request-payer';
|
||||
const S3_ACL_HEADER = 'x-amz-acl';
|
||||
const S3_CONTENT_TYPE_HEADER = 'Content-Type';
|
||||
|
||||
/**
|
||||
* Set S3 endpoint to use
|
||||
*
|
||||
* @param string|Zend_Uri_Http $endpoint
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
public function setEndpoint($endpoint)
|
||||
{
|
||||
if(!($endpoint instanceof Zend_Uri_Http)) {
|
||||
$endpoint = Zend_Uri::factory($endpoint);
|
||||
}
|
||||
if(!$endpoint->valid()) {
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Invalid endpoint supplied");
|
||||
}
|
||||
$this->_endpoint = $endpoint;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current S3 endpoint
|
||||
*
|
||||
* @return Zend_Uri_Http
|
||||
*/
|
||||
public function getEndpoint()
|
||||
{
|
||||
return $this->_endpoint;
|
||||
}
|
||||
|
||||
public function __construct($accessKey=null, $secretKey=null, $region=null)
|
||||
{
|
||||
parent::__construct($accessKey, $secretKey, $region);
|
||||
$this->setEndpoint("http://".self::S3_ENDPOINT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify if the bucket name is valid
|
||||
*
|
||||
* @param string $bucket
|
||||
* @return boolean
|
||||
*/
|
||||
public function _validBucketName($bucket)
|
||||
{
|
||||
$len = strlen($bucket);
|
||||
if ($len < 3 || $len > 255) {
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Bucket name \"$bucket\" must be between 3 and 255 characters long");
|
||||
}
|
||||
|
||||
if (preg_match('/[^a-z0-9\._-]/', $bucket)) {
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Bucket name \"$bucket\" contains invalid characters");
|
||||
}
|
||||
|
||||
if (preg_match('/(\d){1,3}\.(\d){1,3}\.(\d){1,3}\.(\d){1,3}/', $bucket)) {
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Bucket name \"$bucket\" cannot be an IP address");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a new bucket
|
||||
*
|
||||
* @param string $bucket
|
||||
* @return boolean
|
||||
*/
|
||||
public function createBucket($bucket, $location = null)
|
||||
{
|
||||
$this->_validBucketName($bucket);
|
||||
|
||||
if($location) {
|
||||
$data = "<CreateBucketConfiguration><LocationConstraint>$location</LocationConstraint></CreateBucketConfiguration>";
|
||||
} else {
|
||||
$data = null;
|
||||
}
|
||||
$response = $this->_makeRequest('PUT', $bucket, null, array(), $data);
|
||||
|
||||
return ($response->getStatus() == 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given bucket name is available
|
||||
*
|
||||
* @param string $bucket
|
||||
* @return boolean
|
||||
*/
|
||||
public function isBucketAvailable($bucket)
|
||||
{
|
||||
$response = $this->_makeRequest('HEAD', $bucket, array('max-keys'=>0));
|
||||
|
||||
return ($response->getStatus() != 404);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a given object exists
|
||||
*
|
||||
* @param string $object
|
||||
* @return boolean
|
||||
*/
|
||||
public function isObjectAvailable($object)
|
||||
{
|
||||
$response = $this->_makeRequest('HEAD', $object);
|
||||
|
||||
return ($response->getStatus() == 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a given bucket. All objects in the bucket must be removed prior
|
||||
* to removing the bucket.
|
||||
*
|
||||
* @param string $bucket
|
||||
* @return boolean
|
||||
*/
|
||||
public function removeBucket($bucket)
|
||||
{
|
||||
$response = $this->_makeRequest('DELETE', $bucket);
|
||||
|
||||
// Look for a 204 No Content response
|
||||
return ($response->getStatus() == 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metadata information for a given object
|
||||
*
|
||||
* @param string $object
|
||||
* @return array|false
|
||||
*/
|
||||
public function getInfo($object)
|
||||
{
|
||||
$info = array();
|
||||
|
||||
$object = $this->_fixupObjectName($object);
|
||||
$response = $this->_makeRequest('HEAD', $object);
|
||||
|
||||
if ($response->getStatus() == 200) {
|
||||
$info['type'] = $response->getHeader('Content-type');
|
||||
$info['size'] = $response->getHeader('Content-length');
|
||||
$info['mtime'] = strtotime($response->getHeader('Last-modified'));
|
||||
$info['etag'] = $response->getHeader('ETag');
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $info;
|
||||
}
|
||||
|
||||
/**
|
||||
* List the S3 buckets
|
||||
*
|
||||
* @return array|false
|
||||
*/
|
||||
public function getBuckets()
|
||||
{
|
||||
$response = $this->_makeRequest('GET');
|
||||
|
||||
if ($response->getStatus() != 200) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$xml = new SimpleXMLElement($response->getBody());
|
||||
|
||||
$buckets = array();
|
||||
foreach ($xml->Buckets->Bucket as $bucket) {
|
||||
$buckets[] = (string)$bucket->Name;
|
||||
}
|
||||
|
||||
return $buckets;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all objects in the bucket.
|
||||
*
|
||||
* @param string $bucket
|
||||
* @return boolean
|
||||
*/
|
||||
public function cleanBucket($bucket)
|
||||
{
|
||||
$objects = $this->getObjectsByBucket($bucket);
|
||||
if (!$objects) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($objects as $object) {
|
||||
$this->removeObject("$bucket/$object");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* List the objects in a bucket.
|
||||
*
|
||||
* Provides the list of object keys that are contained in the bucket.
|
||||
*
|
||||
* @param string $bucket
|
||||
* @return array|false
|
||||
*/
|
||||
public function getObjectsByBucket($bucket)
|
||||
{
|
||||
$response = $this->_makeRequest('GET', $bucket);
|
||||
|
||||
if ($response->getStatus() != 200) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$xml = new SimpleXMLElement($response->getBody());
|
||||
|
||||
$objects = array();
|
||||
if (isset($xml->Contents)) {
|
||||
foreach ($xml->Contents as $contents) {
|
||||
foreach ($contents->Key as $object) {
|
||||
$objects[] = (string)$object;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $objects;
|
||||
}
|
||||
|
||||
protected function _fixupObjectName($object)
|
||||
{
|
||||
$nameparts = explode('/', $object);
|
||||
|
||||
$this->_validBucketName($nameparts[0]);
|
||||
|
||||
$firstpart = array_shift($nameparts);
|
||||
if(count($nameparts) == 0) {
|
||||
return $firstpart;
|
||||
}
|
||||
|
||||
return $firstpart.'/'.join("/", array_map('rawurlencode',$nameparts));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an object
|
||||
*
|
||||
* @param string $object
|
||||
* @param bool $paidobject This is "requestor pays" object
|
||||
* @return string|false
|
||||
*/
|
||||
public function getObject($object, $paidobject = false)
|
||||
{
|
||||
$object = $this->_fixupObjectName($object);
|
||||
if($paidobject) {
|
||||
$response = $this->_makeRequest('GET', $object, null, array(self::S3_REQUESTPAY_HEADER => "requester"));
|
||||
} else {
|
||||
$response = $this->_makeRequest('GET', $object);
|
||||
}
|
||||
|
||||
if ($response->getStatus() != 200) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $response->getBody();
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload an object by a PHP string
|
||||
*
|
||||
* @param string $object Object name
|
||||
* @param string $data Object data
|
||||
* @param array $meta Metadata
|
||||
* @return boolean
|
||||
*/
|
||||
public function putObject($object, $data, $meta=null)
|
||||
{
|
||||
$object = $this->_fixupObjectName($object);
|
||||
$headers = (is_array($meta)) ? $meta : array();
|
||||
|
||||
$headers['Content-MD5'] = base64_encode(md5($data, true));
|
||||
$headers['Expect'] = '100-continue';
|
||||
|
||||
if (!isset($headers[self::S3_CONTENT_TYPE_HEADER])) {
|
||||
$headers[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($object);
|
||||
}
|
||||
|
||||
$response = $this->_makeRequest('PUT', $object, null, $headers, $data);
|
||||
|
||||
// Check the MD5 Etag returned by S3 against and MD5 of the buffer
|
||||
if ($response->getStatus() == 200) {
|
||||
// It is escaped by double quotes for some reason
|
||||
$etag = str_replace('"', '', $response->getHeader('Etag'));
|
||||
|
||||
if ($etag == md5($data)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Put file to S3 as object
|
||||
*
|
||||
* @param string $path File name
|
||||
* @param string $object Object name
|
||||
* @param array $meta Metadata
|
||||
* @return boolean
|
||||
*/
|
||||
public function putFile($path, $object, $meta=null)
|
||||
{
|
||||
$data = @file_get_contents($path);
|
||||
if ($data === false) {
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Cannot read file $path");
|
||||
}
|
||||
|
||||
if (!is_array($meta)) {
|
||||
$meta = array();
|
||||
}
|
||||
|
||||
if (!isset($meta[self::S3_CONTENT_TYPE_HEADER])) {
|
||||
$meta[self::S3_CONTENT_TYPE_HEADER] = self::getMimeType($path);
|
||||
}
|
||||
|
||||
return $this->putObject($object, $data, $meta);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a given object
|
||||
*
|
||||
* @param string $object
|
||||
* @return boolean
|
||||
*/
|
||||
public function removeObject($object)
|
||||
{
|
||||
$object = $this->_fixupObjectName($object);
|
||||
$response = $this->_makeRequest('DELETE', $object);
|
||||
|
||||
// Look for a 204 No Content response
|
||||
return ($response->getStatus() == 204);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a request to Amazon S3
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $path
|
||||
* @param array $params
|
||||
* @param array $headers
|
||||
* @param string $data
|
||||
* @return Zend_Http_Response
|
||||
*/
|
||||
public function _makeRequest($method, $path='', $params=null, $headers=array(), $data=null)
|
||||
{
|
||||
$retry_count = 0;
|
||||
|
||||
if (!is_array($headers)) {
|
||||
$headers = array($headers);
|
||||
}
|
||||
|
||||
$headers['Date'] = gmdate(DATE_RFC1123, time());
|
||||
|
||||
// build the end point out
|
||||
$parts = explode('/', $path, 2);
|
||||
$endpoint = clone($this->_endpoint);
|
||||
if($parts[0]) {
|
||||
// prepend bucket name to the hostname
|
||||
$endpoint->setHost($parts[0].".".$endpoint->getHost());
|
||||
}
|
||||
if(!empty($parts[1])) {
|
||||
$endpoint->setPath('/'.$parts[1]);
|
||||
} else {
|
||||
$endpoint->setPath('/');
|
||||
if($parts[0]) {
|
||||
$path = $parts[0]."/";
|
||||
}
|
||||
}
|
||||
|
||||
self::addSignature($method, $path, $headers);
|
||||
|
||||
$client = self::getHttpClient();
|
||||
|
||||
$client->resetParameters();
|
||||
// Work around buglet in HTTP client - it doesn't clean headers
|
||||
// Remove when ZHC is fixed
|
||||
$client->setHeaders(array('Content-MD5' => null,
|
||||
'Expect' => null,
|
||||
'Range' => null,
|
||||
'x-amz-acl' => null));
|
||||
|
||||
$client->setUri($endpoint);
|
||||
$client->setHeaders($headers);
|
||||
|
||||
if (is_array($params)) {
|
||||
foreach ($params as $name=>$value) {
|
||||
$client->setParameterGet($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
if (($method == 'PUT') && ($data !== null)) {
|
||||
if (!isset($headers['Content-type'])) {
|
||||
$headers['Content-type'] = self::getMimeType($path);
|
||||
}
|
||||
$client->setRawData($data, $headers['Content-type']);
|
||||
}
|
||||
do {
|
||||
$retry = false;
|
||||
|
||||
$response = $client->request($method);
|
||||
$response_code = $response->getStatus();
|
||||
|
||||
// Some 5xx errors are expected, so retry automatically
|
||||
if ($response_code >= 500 && $response_code < 600 && $retry_count <= 5) {
|
||||
$retry = true;
|
||||
$retry_count++;
|
||||
sleep($retry_count / 4 * $retry_count);
|
||||
}
|
||||
else if ($response_code == 307) {
|
||||
// Need to redirect, new S3 endpoint given
|
||||
// This should never happen as Zend_Http_Client will redirect automatically
|
||||
}
|
||||
else if ($response_code == 100) {
|
||||
// echo 'OK to Continue';
|
||||
}
|
||||
} while ($retry);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the S3 Authorization signature to the request headers
|
||||
*
|
||||
* @param string $method
|
||||
* @param string $path
|
||||
* @param array &$headers
|
||||
* @return string
|
||||
*/
|
||||
protected function addSignature($method, $path, &$headers)
|
||||
{
|
||||
if (!is_array($headers)) {
|
||||
$headers = array($headers);
|
||||
}
|
||||
|
||||
$type = $md5 = $date = '';
|
||||
|
||||
// Search for the Content-type, Content-MD5 and Date headers
|
||||
foreach ($headers as $key=>$val) {
|
||||
if (strcasecmp($key, 'content-type') == 0) {
|
||||
$type = $val;
|
||||
}
|
||||
else if (strcasecmp($key, 'content-md5') == 0) {
|
||||
$md5 = $val;
|
||||
}
|
||||
else if (strcasecmp($key, 'date') == 0) {
|
||||
$date = $val;
|
||||
}
|
||||
}
|
||||
|
||||
// If we have an x-amz-date header, use that instead of the normal Date
|
||||
if (isset($headers['x-amz-date']) && isset($date)) {
|
||||
$date = '';
|
||||
}
|
||||
|
||||
$sig_str = "$method\n$md5\n$type\n$date\n";
|
||||
// For x-amz- headers, combine like keys, lowercase them, sort them
|
||||
// alphabetically and remove excess spaces around values
|
||||
$amz_headers = array();
|
||||
foreach ($headers as $key=>$val) {
|
||||
$key = strtolower($key);
|
||||
if (substr($key, 0, 6) == 'x-amz-') {
|
||||
if (is_array($val)) {
|
||||
$amz_headers[$key] = $val;
|
||||
}
|
||||
else {
|
||||
$amz_headers[$key][] = preg_replace('/\s+/', ' ', $val);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!empty($amz_headers)) {
|
||||
ksort($amz_headers);
|
||||
foreach ($amz_headers as $key=>$val) {
|
||||
$sig_str .= $key.':'.implode(',', $val)."\n";
|
||||
}
|
||||
}
|
||||
|
||||
$sig_str .= '/'.parse_url($path, PHP_URL_PATH);
|
||||
if (strpos($path, '?location') !== false) {
|
||||
$sig_str .= '?location';
|
||||
}
|
||||
else if (strpos($path, '?acl') !== false) {
|
||||
$sig_str .= '?acl';
|
||||
}
|
||||
else if (strpos($path, '?torrent') !== false) {
|
||||
$sig_str .= '?torrent';
|
||||
}
|
||||
|
||||
$signature = base64_encode(Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
|
||||
$headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;
|
||||
|
||||
return $sig_str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to get the content-type of a file based on the extension
|
||||
*
|
||||
* TODO: move this to Zend_Mime
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function getMimeType($path)
|
||||
{
|
||||
$ext = substr(strrchr($path, '.'), 1);
|
||||
|
||||
if(!$ext) {
|
||||
// shortcut
|
||||
return 'binary/octet-stream';
|
||||
}
|
||||
|
||||
switch ($ext) {
|
||||
case 'xls':
|
||||
$content_type = 'application/excel';
|
||||
break;
|
||||
case 'hqx':
|
||||
$content_type = 'application/macbinhex40';
|
||||
break;
|
||||
case 'doc':
|
||||
case 'dot':
|
||||
case 'wrd':
|
||||
$content_type = 'application/msword';
|
||||
break;
|
||||
case 'pdf':
|
||||
$content_type = 'application/pdf';
|
||||
break;
|
||||
case 'pgp':
|
||||
$content_type = 'application/pgp';
|
||||
break;
|
||||
case 'ps':
|
||||
case 'eps':
|
||||
case 'ai':
|
||||
$content_type = 'application/postscript';
|
||||
break;
|
||||
case 'ppt':
|
||||
$content_type = 'application/powerpoint';
|
||||
break;
|
||||
case 'rtf':
|
||||
$content_type = 'application/rtf';
|
||||
break;
|
||||
case 'tgz':
|
||||
case 'gtar':
|
||||
$content_type = 'application/x-gtar';
|
||||
break;
|
||||
case 'gz':
|
||||
$content_type = 'application/x-gzip';
|
||||
break;
|
||||
case 'php':
|
||||
case 'php3':
|
||||
case 'php4':
|
||||
$content_type = 'application/x-httpd-php';
|
||||
break;
|
||||
case 'js':
|
||||
$content_type = 'application/x-javascript';
|
||||
break;
|
||||
case 'ppd':
|
||||
case 'psd':
|
||||
$content_type = 'application/x-photoshop';
|
||||
break;
|
||||
case 'swf':
|
||||
case 'swc':
|
||||
case 'rf':
|
||||
$content_type = 'application/x-shockwave-flash';
|
||||
break;
|
||||
case 'tar':
|
||||
$content_type = 'application/x-tar';
|
||||
break;
|
||||
case 'zip':
|
||||
$content_type = 'application/zip';
|
||||
break;
|
||||
case 'mid':
|
||||
case 'midi':
|
||||
case 'kar':
|
||||
$content_type = 'audio/midi';
|
||||
break;
|
||||
case 'mp2':
|
||||
case 'mp3':
|
||||
case 'mpga':
|
||||
$content_type = 'audio/mpeg';
|
||||
break;
|
||||
case 'ra':
|
||||
$content_type = 'audio/x-realaudio';
|
||||
break;
|
||||
case 'wav':
|
||||
$content_type = 'audio/wav';
|
||||
break;
|
||||
case 'bmp':
|
||||
$content_type = 'image/bitmap';
|
||||
break;
|
||||
case 'gif':
|
||||
$content_type = 'image/gif';
|
||||
break;
|
||||
case 'iff':
|
||||
$content_type = 'image/iff';
|
||||
break;
|
||||
case 'jb2':
|
||||
$content_type = 'image/jb2';
|
||||
break;
|
||||
case 'jpg':
|
||||
case 'jpe':
|
||||
case 'jpeg':
|
||||
$content_type = 'image/jpeg';
|
||||
break;
|
||||
case 'jpx':
|
||||
$content_type = 'image/jpx';
|
||||
break;
|
||||
case 'png':
|
||||
$content_type = 'image/png';
|
||||
break;
|
||||
case 'tif':
|
||||
case 'tiff':
|
||||
$content_type = 'image/tiff';
|
||||
break;
|
||||
case 'wbmp':
|
||||
$content_type = 'image/vnd.wap.wbmp';
|
||||
break;
|
||||
case 'xbm':
|
||||
$content_type = 'image/xbm';
|
||||
break;
|
||||
case 'css':
|
||||
$content_type = 'text/css';
|
||||
break;
|
||||
case 'txt':
|
||||
$content_type = 'text/plain';
|
||||
break;
|
||||
case 'htm':
|
||||
case 'html':
|
||||
$content_type = 'text/html';
|
||||
break;
|
||||
case 'xml':
|
||||
$content_type = 'text/xml';
|
||||
break;
|
||||
case 'xsl':
|
||||
$content_type = 'text/xsl';
|
||||
break;
|
||||
case 'mpg':
|
||||
case 'mpe':
|
||||
case 'mpeg':
|
||||
$content_type = 'video/mpeg';
|
||||
break;
|
||||
case 'qt':
|
||||
case 'mov':
|
||||
$content_type = 'video/quicktime';
|
||||
break;
|
||||
case 'avi':
|
||||
$content_type = 'video/x-ms-video';
|
||||
break;
|
||||
case 'eml':
|
||||
$content_type = 'message/rfc822';
|
||||
break;
|
||||
default:
|
||||
$content_type = 'binary/octet-stream';
|
||||
break;
|
||||
}
|
||||
|
||||
return $content_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this object as stream wrapper client
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
public function registerAsClient($name)
|
||||
{
|
||||
self::$_wrapperClients[$name] = $this;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister this object as stream wrapper client
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
public function unregisterAsClient($name)
|
||||
{
|
||||
unset(self::$_wrapperClients[$name]);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get wrapper client for stream type
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
public static function getWrapperClient($name)
|
||||
{
|
||||
return self::$_wrapperClients[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this object as stream wrapper
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
public function registerStreamWrapper($name='s3')
|
||||
{
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Stream
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Stream.php';
|
||||
|
||||
stream_register_wrapper($name, 'Zend_Service_Amazon_S3_Stream');
|
||||
$this->registerAsClient($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister this object as stream wrapper
|
||||
*
|
||||
* @param string $name
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
public function unregisterStreamWrapper($name='s3')
|
||||
{
|
||||
stream_wrapper_unregister($name);
|
||||
$this->unregisterAsClient($name);
|
||||
}
|
||||
}
|
37
libs/Zend/Service/Amazon/S3/Exception.php
Executable file
37
libs/Zend/Service/Amazon/S3/Exception.php
Executable file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon_S3
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: Exception.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Zend_Service_Amazon_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/Exception.php';
|
||||
|
||||
|
||||
/**
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_S3_Exception extends Zend_Service_Amazon_Exception
|
||||
{}
|
497
libs/Zend/Service/Amazon/S3/Stream.php
Executable file
497
libs/Zend/Service/Amazon/S3/Stream.php
Executable file
@ -0,0 +1,497 @@
|
||||
<?php
|
||||
/**
|
||||
* Zend Framework
|
||||
*
|
||||
* LICENSE
|
||||
*
|
||||
* This source file is subject to the new BSD license that is bundled
|
||||
* with this package in the file LICENSE.txt.
|
||||
* It is also available through the world-wide-web at this URL:
|
||||
* http://framework.zend.com/license/new-bsd
|
||||
* If you did not receive a copy of the license and are unable to
|
||||
* obtain it through the world-wide-web, please send an email
|
||||
* to license@zend.com so we can send you a copy immediately.
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon_S3
|
||||
* @copyright Copyright (c) 2005-2008, Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: S3.php 9786 2008-06-24 23:50:25Z jplock $
|
||||
*/
|
||||
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3.php';
|
||||
|
||||
/**
|
||||
* Amazon S3 PHP stream wrapper
|
||||
*
|
||||
* @category Zend
|
||||
* @package Zend_Service
|
||||
* @subpackage Amazon_S3
|
||||
* @copyright Copyright (c) 2005-2008, Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
*/
|
||||
class Zend_Service_Amazon_S3_Stream
|
||||
{
|
||||
/**
|
||||
* @var boolean Write the buffer on fflush()?
|
||||
*/
|
||||
private $_writeBuffer = false;
|
||||
|
||||
/**
|
||||
* @var integer Current read/write position
|
||||
*/
|
||||
private $_position = 0;
|
||||
|
||||
/**
|
||||
* @var integer Total size of the object as returned by S3 (Content-length)
|
||||
*/
|
||||
private $_objectSize = 0;
|
||||
|
||||
/**
|
||||
* @var string File name to interact with
|
||||
*/
|
||||
private $_objectName = null;
|
||||
|
||||
/**
|
||||
* @var string Current read/write buffer
|
||||
*/
|
||||
private $_objectBuffer = null;
|
||||
|
||||
/**
|
||||
* @var array Available buckets
|
||||
*/
|
||||
private $_bucketList = array();
|
||||
|
||||
/**
|
||||
* @var Zend_Service_Amazon_S3
|
||||
*/
|
||||
private $_s3 = null;
|
||||
|
||||
/**
|
||||
* Retrieve client for this stream type
|
||||
*
|
||||
* @param string $path
|
||||
* @return Zend_Service_Amazon_S3
|
||||
*/
|
||||
protected function _getS3Client($path)
|
||||
{
|
||||
if ($this->_s3 === null) {
|
||||
$url = explode(':', $path);
|
||||
|
||||
if (!$url) {
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Unable to parse URL $path");
|
||||
}
|
||||
|
||||
$this->_s3 = Zend_Service_Amazon_S3::getWrapperClient($url[0]);
|
||||
if (!$this->_s3) {
|
||||
/**
|
||||
* @see Zend_Service_Amazon_S3_Exception
|
||||
*/
|
||||
require_once 'Zend/Service/Amazon/S3/Exception.php';
|
||||
throw new Zend_Service_Amazon_S3_Exception("Unknown client for wrapper {$url[0]}");
|
||||
}
|
||||
}
|
||||
|
||||
return $this->_s3;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract object name from URL
|
||||
*
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
protected function _getNamePart($path)
|
||||
{
|
||||
$url = parse_url($path);
|
||||
if ($url['host']) {
|
||||
return !empty($url['path']) ? $url['host'].$url['path'] : $url['host'];
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
/**
|
||||
* Open the stream
|
||||
*
|
||||
* @param string $path
|
||||
* @param string $mode
|
||||
* @param integer $options
|
||||
* @param string $opened_path
|
||||
* @return boolean
|
||||
*/
|
||||
public function stream_open($path, $mode, $options, $opened_path)
|
||||
{
|
||||
$name = $this->_getNamePart($path);
|
||||
// If we open the file for writing, just return true. Create the object
|
||||
// on fflush call
|
||||
if (strpbrk($mode, 'wax')) {
|
||||
$this->_objectName = $name;
|
||||
$this->_objectBuffer = null;
|
||||
$this->_objectSize = 0;
|
||||
$this->_position = 0;
|
||||
$this->_writeBuffer = true;
|
||||
$this->_getS3Client($path);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
// Otherwise, just see if the file exists or not
|
||||
$info = $this->_getS3Client($path)->getInfo($name);
|
||||
if ($info) {
|
||||
$this->_objectName = $name;
|
||||
$this->_objectBuffer = null;
|
||||
$this->_objectSize = $info['size'];
|
||||
$this->_position = 0;
|
||||
$this->_writeBuffer = false;
|
||||
$this->_getS3Client($path);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close the stream
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function stream_close()
|
||||
{
|
||||
$this->_objectName = null;
|
||||
$this->_objectBuffer = null;
|
||||
$this->_objectSize = 0;
|
||||
$this->_position = 0;
|
||||
$this->_writeBuffer = false;
|
||||
unset($this->_s3);
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from the stream
|
||||
*
|
||||
* @param integer $count
|
||||
* @return string
|
||||
*/
|
||||
public function stream_read($count)
|
||||
{
|
||||
if (!$this->_objectName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$range_start = $this->_position;
|
||||
$range_end = $this->_position+$count;
|
||||
|
||||
// Only fetch more data from S3 if we haven't fetched any data yet (postion=0)
|
||||
// OR, the range end position is greater than the size of the current object
|
||||
// buffer AND if the range end position is less than or equal to the object's
|
||||
// size returned by S3
|
||||
if (($this->_position == 0) || (($range_end > strlen($this->_objectBuffer)) && ($range_end <= $this->_objectSize))) {
|
||||
|
||||
$headers = array(
|
||||
'Range' => "$range_start-$range_end"
|
||||
);
|
||||
|
||||
$response = $this->_s3->_makeRequest('GET', $this->_objectName, null, $headers);
|
||||
|
||||
if ($response->getStatus() == 200) {
|
||||
$this->_objectBuffer .= $response->getBody();
|
||||
}
|
||||
}
|
||||
|
||||
$data = substr($this->_objectBuffer, $this->_position, $count);
|
||||
$this->_position += strlen($data);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to the stream
|
||||
*
|
||||
* @param string $data
|
||||
* @return integer
|
||||
*/
|
||||
public function stream_write($data)
|
||||
{
|
||||
if (!$this->_objectName) {
|
||||
return 0;
|
||||
}
|
||||
$len = strlen($data);
|
||||
$this->_objectBuffer .= $data;
|
||||
$this->_objectSize += $len;
|
||||
// TODO: handle current position for writing!
|
||||
return $len;
|
||||
}
|
||||
|
||||
/**
|
||||
* End of the stream?
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function stream_eof()
|
||||
{
|
||||
if (!$this->_objectName) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return ($this->_position >= $this->_objectSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* What is the current read/write position of the stream
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function stream_tell()
|
||||
{
|
||||
return $this->_position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the read/write position of the stream
|
||||
*
|
||||
* @param integer $offset
|
||||
* @param integer $whence
|
||||
* @return boolean
|
||||
*/
|
||||
public function stream_seek($offset, $whence)
|
||||
{
|
||||
if (!$this->_objectName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
switch ($whence) {
|
||||
case SEEK_CUR:
|
||||
// Set position to current location plus $offset
|
||||
$new_pos = $this->_position + $offset;
|
||||
break;
|
||||
case SEEK_END:
|
||||
// Set position to end-of-file plus $offset
|
||||
$new_pos = $this->_objectSize + $offset;
|
||||
break;
|
||||
case SEEK_SET:
|
||||
default:
|
||||
// Set position equal to $offset
|
||||
$new_pos = $offset;
|
||||
break;
|
||||
}
|
||||
$ret = ($new_pos >= 0 && $new_pos <= $this->_objectSize);
|
||||
if ($ret) {
|
||||
$this->_position = $new_pos;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flush current cached stream data to storage
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function stream_flush()
|
||||
{
|
||||
// If the stream wasn't opened for writing, just return false
|
||||
if (!$this->_writeBuffer) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ret = $this->_s3->putObject($this->_objectName, $this->_objectBuffer);
|
||||
|
||||
$this->_objectBuffer = null;
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data array of stream variables
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function stream_stat()
|
||||
{
|
||||
if (!$this->_objectName) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$stat = array();
|
||||
$stat['dev'] = 0;
|
||||
$stat['ino'] = 0;
|
||||
$stat['mode'] = 0777;
|
||||
$stat['nlink'] = 0;
|
||||
$stat['uid'] = 0;
|
||||
$stat['gid'] = 0;
|
||||
$stat['rdev'] = 0;
|
||||
$stat['size'] = 0;
|
||||
$stat['atime'] = 0;
|
||||
$stat['mtime'] = 0;
|
||||
$stat['ctime'] = 0;
|
||||
$stat['blksize'] = 0;
|
||||
$stat['blocks'] = 0;
|
||||
|
||||
if(($slash = strchr($this->_objectName, '/')) === false || $slash == strlen($this->_objectName)-1) {
|
||||
/* bucket */
|
||||
$stat['mode'] |= 040000;
|
||||
} else {
|
||||
$stat['mode'] |= 0100000;
|
||||
}
|
||||
$info = $this->_s3->getInfo($this->_objectName);
|
||||
if (!empty($info)) {
|
||||
$stat['size'] = $info['size'];
|
||||
$stat['atime'] = time();
|
||||
$stat['mtime'] = $info['mtime'];
|
||||
}
|
||||
|
||||
return $stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to delete the item
|
||||
*
|
||||
* @param string $path
|
||||
* @return boolean
|
||||
*/
|
||||
public function unlink($path)
|
||||
{
|
||||
return $this->_getS3Client($path)->removeObject($this->_getNamePart($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to rename the item
|
||||
*
|
||||
* @param string $path_from
|
||||
* @param string $path_to
|
||||
* @return boolean False
|
||||
*/
|
||||
public function rename($path_from, $path_to)
|
||||
{
|
||||
// TODO: Renaming isn't supported, always return false
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new directory
|
||||
*
|
||||
* @param string $path
|
||||
* @param integer $mode
|
||||
* @param integer $options
|
||||
* @return boolean
|
||||
*/
|
||||
public function mkdir($path, $mode, $options)
|
||||
{
|
||||
return $this->_getS3Client($path)->createBucket(parse_url($path, PHP_URL_HOST));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a directory
|
||||
*
|
||||
* @param string $path
|
||||
* @param integer $options
|
||||
* @return boolean
|
||||
*/
|
||||
public function rmdir($path, $options)
|
||||
{
|
||||
return $this->_getS3Client($path)->removeBucket(parse_url($path, PHP_URL_HOST));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to open a directory
|
||||
*
|
||||
* @param string $path
|
||||
* @param integer $options
|
||||
* @return boolean
|
||||
*/
|
||||
public function dir_opendir($path, $options)
|
||||
{
|
||||
|
||||
if (preg_match('@^([a-z0-9+.]|-)+://$@', $path)) {
|
||||
$this->_bucketList = $this->_getS3Client($path)->getBuckets();
|
||||
}
|
||||
else {
|
||||
$url = parse_url($path);
|
||||
$this->_bucketList = $this->_getS3Client($path)->getObjectsByBucket($url["host"]);
|
||||
}
|
||||
|
||||
return ($this->_bucketList !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return array of URL variables
|
||||
*
|
||||
* @param string $path
|
||||
* @param integer $flags
|
||||
* @return array
|
||||
*/
|
||||
public function url_stat($path, $flags)
|
||||
{
|
||||
$stat = array();
|
||||
$stat['dev'] = 0;
|
||||
$stat['ino'] = 0;
|
||||
$stat['mode'] = 0777;
|
||||
$stat['nlink'] = 0;
|
||||
$stat['uid'] = 0;
|
||||
$stat['gid'] = 0;
|
||||
$stat['rdev'] = 0;
|
||||
$stat['size'] = 0;
|
||||
$stat['atime'] = 0;
|
||||
$stat['mtime'] = 0;
|
||||
$stat['ctime'] = 0;
|
||||
$stat['blksize'] = 0;
|
||||
$stat['blocks'] = 0;
|
||||
|
||||
$name = $this->_getNamePart($path);
|
||||
if(($slash = strchr($name, '/')) === false || $slash == strlen($name)-1) {
|
||||
/* bucket */
|
||||
$stat['mode'] |= 040000;
|
||||
} else {
|
||||
$stat['mode'] |= 0100000;
|
||||
}
|
||||
$info = $this->_getS3Client($path)->getInfo($name);
|
||||
|
||||
if (!empty($info)) {
|
||||
$stat['size'] = $info['size'];
|
||||
$stat['atime'] = time();
|
||||
$stat['mtime'] = $info['mtime'];
|
||||
}
|
||||
|
||||
return $stat;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the next filename in the directory
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function dir_readdir()
|
||||
{
|
||||
$object = current($this->_bucketList);
|
||||
if ($object !== false) {
|
||||
next($this->_bucketList);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the directory pointer
|
||||
*
|
||||
* @return boolean True
|
||||
*/
|
||||
public function dir_rewinddir()
|
||||
{
|
||||
reset($this->_bucketList);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Close a directory
|
||||
*
|
||||
* @return boolean True
|
||||
*/
|
||||
public function dir_closedir()
|
||||
{
|
||||
$this->_bucketList = array();
|
||||
return true;
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@
|
||||
* @subpackage Amazon
|
||||
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
||||
* @license http://framework.zend.com/license/new-bsd New BSD License
|
||||
* @version $Id: SimilarProduct.php 8064 2008-02-16 10:58:39Z thomas $
|
||||
* @version $Id: SimilarProduct.php 12667 2008-11-15 16:51:39Z beberlei $
|
||||
*/
|
||||
|
||||
|
||||
@ -31,6 +31,16 @@
|
||||
*/
|
||||
class Zend_Service_Amazon_SimilarProduct
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $ASIN;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $Title;
|
||||
|
||||
/**
|
||||
* Assigns values to properties relevant to SimilarProduct
|
||||
*
|
||||
@ -42,7 +52,10 @@ class Zend_Service_Amazon_SimilarProduct
|
||||
$xpath = new DOMXPath($dom->ownerDocument);
|
||||
$xpath->registerNamespace('az', 'http://webservices.amazon.com/AWSECommerceService/2005-10-05');
|
||||
foreach (array('ASIN', 'Title') as $el) {
|
||||
$this->$el = (string) $xpath->query("./az:$el/text()", $dom)->item(0)->data;
|
||||
$text = $xpath->query("./az:$el/text()", $dom)->item(0);
|
||||
if($text instanceof DOMText) {
|
||||
$this->$el = (string)$text->data;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user