Permalink
Browse files

Merge branch 'aws-update' of http://github.com/farazdagi/zf2 into mer…

…ges/farazdagi-aws

Conflicts:
	library/Zend/Service/Amazon/AbstractService.php
  • Loading branch information...
2 parents 7722417 + 2f2c7e5 commit d638836f83bdbfe2786cbbcfad6b3f5bf839a409 @weierophinney weierophinney committed Nov 2, 2010
Showing with 2,586 additions and 115 deletions.
  1. +0 −114 library/Zend/Service/Amazon/AbstractService.php
  2. +87 −0 library/Zend/Service/Amazon/Authentication/Authentication.php
  3. +37 −0 library/Zend/Service/Amazon/Authentication/Exception.php
  4. +40 −0 library/Zend/Service/Amazon/Authentication/Exception/InvalidArgumentException.php
  5. +40 −0 library/Zend/Service/Amazon/Authentication/Exception/RuntimeException.php
  6. +107 −0 library/Zend/Service/Amazon/Authentication/S3.php
  7. +104 −0 library/Zend/Service/Amazon/Authentication/V1.php
  8. +134 −0 library/Zend/Service/Amazon/Authentication/V2.php
  9. +1 −1 library/Zend/Service/Amazon/S3/S3.php
  10. +113 −0 library/Zend/Service/Amazon/SimpleDb/Attribute.php
  11. +37 −0 library/Zend/Service/Amazon/SimpleDb/Exception.php
  12. +40 −0 library/Zend/Service/Amazon/SimpleDb/Exception/InvalidArgumentException.php
  13. +40 −0 library/Zend/Service/Amazon/SimpleDb/Exception/RuntimeException.php
  14. +97 −0 library/Zend/Service/Amazon/SimpleDb/Page.php
  15. +190 −0 library/Zend/Service/Amazon/SimpleDb/Response.php
  16. +555 −0 library/Zend/Service/Amazon/SimpleDb/SimpleDb.php
  17. +185 −0 tests/Zend/Service/Amazon/Authentication/S3Test.php
  18. +86 −0 tests/Zend/Service/Amazon/Authentication/V1Test.php
  19. +102 −0 tests/Zend/Service/Amazon/Authentication/V2Test.php
  20. +4 −0 tests/Zend/Service/Amazon/Authentication/_files/ec2_v2_return.txt
  21. +4 −0 tests/Zend/Service/Amazon/Authentication/_files/sqs_v2_get_return.txt
  22. +46 −0 tests/Zend/Service/Amazon/SimpleDb/OfflineTest.php
  23. +473 −0 tests/Zend/Service/Amazon/SimpleDb/OnlineTest.php
  24. +64 −0 working/PHPNamespacer-MappedClasses.xml
View
114 library/Zend/Service/Amazon/AbstractService.php
@@ -1,114 +0,0 @@
-<?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-2010 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
-
-/**
- * Abstract Amazon class that handles the credentials for any of the Web Services that
- * Amazon offers
- *
- * @uses Zend\Service\AbstractService
- * @uses Zend_Service_Amazon_Exception
- * @category Zend
- * @package Zend_Service
- * @subpackage Amazon
- * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
- * @license http://framework.zend.com/license/new-bsd New BSD License
- */
-abstract class Zend_Service_Amazon_AbstractService extends \Zend\Service\AbstractService
-{
- /**
- * @var string Amazon Access Key
- */
- protected static $_defaultAccessKey = null;
-
- /**
- * @var string Amazon Secret Key
- */
- protected static $_defaultSecretKey = null;
-
- /**
- * @var string Amazon Secret Key
- */
- protected $_secretKey;
-
- /**
- * @var string Amazon Access Key
- */
- protected $_accessKey;
-
-
- /**
- * 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;
- }
-
- /**
- * Create Amazon client.
- *
- * @param string $access_key Override the default Access Key
- * @param string $secret_key Override the default Secret Key
- * @return void
- */
- public function __construct($accessKey=null, $secretKey=null)
- {
- if(!$accessKey) {
- $accessKey = self::$_defaultAccessKey;
- }
- if(!$secretKey) {
- $secretKey = self::$_defaultSecretKey;
- }
-
- if(!$accessKey || !$secretKey) {
- throw new Zend_Service_Amazon_Exception("AWS keys were not supplied");
- }
- $this->_accessKey = $accessKey;
- $this->_secretKey = $secretKey;
- }
-
-
-
- /**
- * 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;
- }
-}
View
87 library/Zend/Service/Amazon/Authentication/Authentication.php
@@ -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_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+abstract class Authentication
+{
+ protected $_accessKey;
+ protected $_secretKey;
+ protected $_apiVersion;
+
+ /**
+ * Constructor
+ *
+ * @param string $accessKey
+ * @param string $secretKey
+ * @param string $apiVersion
+ * @return void
+ */
+ public function __construct($accessKey, $secretKey, $apiVersion)
+ {
+ $this->setAccessKey($accessKey);
+ $this->setSecretKey($secretKey);
+ $this->setApiVersion($apiVersion);
+ }
+
+ /**
+ * Set access key
+ *
+ * @param string $accessKey
+ * @return void
+ */
+ public function setAccessKey($accessKey)
+ {
+ $this->_accessKey = $accessKey;
+ }
+
+ /**
+ * Set secret key
+ *
+ * @param string $secretKey
+ * @return void
+ */
+ public function setSecretKey($secretKey)
+ {
+ $this->_secretKey = $secretKey;
+ }
+
+ /**
+ * Set API version
+ *
+ * @param string $apiVersion
+ * @return void
+ */
+ public function setApiVersion($apiVersion)
+ {
+ $this->_apiVersion = $apiVersion;
+ }
+}
View
37 library/Zend/Service/Amazon/Authentication/Exception.php
@@ -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_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication;
+
+/**
+ * @uses Zend\Service\Amazon\Exception
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+interface Exception
+ extends \Zend\Service\Amazon\Exception
+{}
View
40 library/Zend/Service/Amazon/Authentication/Exception/InvalidArgumentException.php
@@ -0,0 +1,40 @@
+<?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.
+ *
+ * @uses \Zend\Service\Amazon\Authentication\Exception
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication\Exception;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class InvalidArgumentException
+ extends \InvalidArgumentException
+ implements \Zend\Service\Amazon\Authentication\Exception
+{}
+
+
View
40 library/Zend/Service/Amazon/Authentication/Exception/RuntimeException.php
@@ -0,0 +1,40 @@
+<?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.
+ *
+ * @uses \Zend\Service\Amazon\Authentication\Exception
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication\Exception;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class RuntimeException
+ extends \RuntimeException
+ implements \Zend\Service\Amazon\Authentication\Exception
+{}
+
+
View
107 library/Zend/Service/Amazon/Authentication/S3.php
@@ -0,0 +1,107 @@
+<?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 Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication;
+use Zend\Crypt;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class S3 extends Authentication
+{
+ /**
+ * Add the S3 Authorization signature to the request headers
+ *
+ * @param string $method
+ * @param string $path
+ * @param array &$headers
+ * @return string
+ */
+ public function generateSignature($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(Crypt\Hmac::compute($this->_secretKey, 'sha1', utf8_encode($sig_str), Crypt\Hmac::BINARY));
+ $headers['Authorization'] = 'AWS ' . $this->_accessKey . ':' . $signature;
+
+ return $sig_str;
+ }
+}
View
104 library/Zend/Service/Amazon/Authentication/V1.php
@@ -0,0 +1,104 @@
+<?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 Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication;
+use Zend\Crypt;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class V1 extends Authentication
+{
+ /**
+ * Signature Version
+ */
+ protected $_signatureVersion = '1';
+
+ /**
+ * Signature Encoding Method
+ */
+ protected $_signatureMethod = 'HmacSHA256';
+
+ /**
+ * Generate the required attributes for the signature
+ * @param string $url
+ * @param array $parameters
+ * @return string
+ */
+ public function generateSignature($url, array &$parameters)
+ {
+ $parameters['AWSAccessKeyId'] = $this->_accessKey;
+ $parameters['SignatureVersion'] = $this->_signatureVersion;
+ $parameters['Version'] = $this->_apiVersion;
+ if(!isset($parameters['Timestamp'])) {
+ $parameters['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z', time()+10);
+ }
+
+ $data = $this->_signParameters($url, $parameters);
+
+ return $data;
+ }
+
+ /**
+ * 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 string $queue_url Queue URL
+ * @param array $parameters the parameters for which to get the signature.
+ *
+ * @return string the signed data.
+ */
+ protected function _signParameters($url, array &$paramaters)
+ {
+ $data = '';
+
+ uksort($paramaters, 'strcasecmp');
+ unset($paramaters['Signature']);
+
+ foreach($paramaters as $key => $value) {
+ $data .= $key . $value;
+ }
+
+ $hmac = Crypt\Hmac::compute($this->_secretKey, 'SHA1', $data, Crypt\Hmac::BINARY);
+
+ $paramaters['Signature'] = base64_encode($hmac);
+
+ return $data;
+ }
+}
View
134 library/Zend/Service/Amazon/Authentication/V2.php
@@ -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 Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\Authentication;
+use Zend\Crypt;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage Authentication
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class V2 extends Authentication
+{
+ /**
+ * Signature Version
+ */
+ protected $_signatureVersion = '2';
+
+ /**
+ * Signature Encoding Method
+ */
+ protected $_signatureMethod = 'HmacSHA256';
+
+ /**
+ * Type of http request
+ * @var string
+ */
+ protected $_httpMethod = "POST";
+
+ /**
+ * Generate the required attributes for the signature
+ * @param string $url
+ * @param array $parameters
+ * @return string
+ */
+ public function generateSignature($url, array &$parameters)
+ {
+ $parameters['AWSAccessKeyId'] = $this->_accessKey;
+ $parameters['SignatureVersion'] = $this->_signatureVersion;
+ $parameters['Version'] = $this->_apiVersion;
+ $parameters['SignatureMethod'] = $this->_signatureMethod;
+ if(!isset($parameters['Timestamp'])) {
+ $parameters['Timestamp'] = gmdate('Y-m-d\TH:i:s\Z', time()+10);
+ }
+
+ $data = $this->_signParameters($url, $parameters);
+
+ return $data;
+ }
+
+ /**
+ * Set http request type to POST or GET
+ * @param $method string
+ */
+ public function setHttpMethod($method = "POST") {
+ $this->_httpMethod = strtoupper($method);
+ }
+
+ /**
+ * Get the current http request type
+ * @return string
+ */
+ public function getHttpMethod()
+ {
+ return $this->_httpMethod;
+ }
+
+ /**
+ * 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 string $queue_url Queue URL
+ * @param array $parameters the parameters for which to get the signature.
+ *
+ * @return string the signed data.
+ */
+ protected function _signParameters($url, array &$paramaters)
+ {
+ $data = $this->_httpMethod . "\n";
+ $data .= parse_url($url, PHP_URL_HOST) . "\n";
+ $data .= ('' == $path = parse_url($url, PHP_URL_PATH)) ? '/' : $path;
+ $data .= "\n";
+
+ uksort($paramaters, 'strcmp');
+ unset($paramaters['Signature']);
+
+ $arrData = array();
+ foreach($paramaters as $key => $value) {
+ $arrData[] = $key . '=' . str_replace('%7E', '~', rawurlencode($value));
+ }
+
+ $data .= implode('&', $arrData);
+
+ $hmac = Crypt\Hmac::compute($this->_secretKey, 'SHA256', $data, Crypt\Hmac::BINARY);
+
+ $paramaters['Signature'] = base64_encode($hmac);
+
+ return $data;
+ }
+}
View
2 library/Zend/Service/Amazon/S3/S3.php
@@ -24,7 +24,7 @@
*/
namespace Zend\Service\Amazon\S3;
use Zend\Service\Amazon,
- Zend\Service\Amazon\Sqs\Exception,
+ Zend\Service\Amazon\S3\Exception,
Zend\Crypt;
/**
View
113 library/Zend/Service/Amazon/SimpleDb/Attribute.php
@@ -0,0 +1,113 @@
+<?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 SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: Response.php 17539 2009-08-10 22:51:26Z mikaelkael $
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Attribute
+{
+ protected $_itemName;
+ protected $_name;
+ protected $_values;
+
+ /**
+ * Constructor
+ *
+ * @param string $itemName
+ * @param string $name
+ * @param array $values
+ * @return void
+ */
+ function __construct($itemName, $name, $values)
+ {
+ $this->_itemName = $itemName;
+ $this->_name = $name;
+
+ if (!is_array($values)) {
+ $this->_values = array($values);
+ } else {
+ $this->_values = $values;
+ }
+ }
+
+ /**
+ * Return the item name to which the attribute belongs
+ *
+ * @return string
+ */
+ public function getItemName ()
+ {
+ return $this->_itemName;
+ }
+
+ /**
+ * Retrieve attribute values
+ *
+ * @return array
+ */
+ public function getValues()
+ {
+ return $this->_values;
+ }
+
+ /**
+ * Retrieve the attribute name
+ *
+ * @return string
+ */
+ public function getName ()
+ {
+ return $this->_name;
+ }
+
+ /**
+ * Add value
+ *
+ * @param mixed $value
+ * @return void
+ */
+ public function addValue($value)
+ {
+ if (is_array($value)) {
+ $this->_values += $value;
+ } else {
+ $this->_values[] = $value;
+ }
+ }
+
+ public function setValues($values)
+ {
+ if (!is_array($values)) {
+ $values = array($values);
+ }
+ $this->_values = $values;
+ }
+}
View
37 library/Zend/Service/Amazon/SimpleDb/Exception.php
@@ -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_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb;
+
+/**
+ * @uses Zend\Service\Amazon\Exception
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+interface Exception
+ extends \Zend\Service\Amazon\Exception
+{}
View
40 library/Zend/Service/Amazon/SimpleDb/Exception/InvalidArgumentException.php
@@ -0,0 +1,40 @@
+<?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.
+ *
+ * @uses \Zend\Service\Amazon\SimpleDb\Exception
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb\Exception;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class InvalidArgumentException
+ extends \InvalidArgumentException
+ implements \Zend\Service\Amazon\SimpleDb\Exception
+{}
+
+
View
40 library/Zend/Service/Amazon/SimpleDb/Exception/RuntimeException.php
@@ -0,0 +1,40 @@
+<?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.
+ *
+ * @uses \Zend\Service\Amazon\SimpleDb\Exception
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb\Exception;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class RuntimeException
+ extends \RuntimeException
+ implements \Zend\Service\Amazon\SimpleDb\Exception
+{}
+
+
View
97 library/Zend/Service/Amazon/SimpleDb/Page.php
@@ -0,0 +1,97 @@
+<?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 SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb;
+
+/**
+ * The Custom Exception class that allows you to have access to the AWS Error Code.
+ *
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Page
+{
+ /** @var string Page data */
+ protected $_data;
+
+ /** @var string|null Token identifying page */
+ protected $_token;
+
+ /**
+ * Constructor
+ *
+ * @param string $data
+ * @param string|null $token
+ * @return void
+ */
+ public function __construct($data, $token = null)
+ {
+ $this->_data = $data;
+ $this->_token = $token;
+ }
+
+ /**
+ * Retrieve page data
+ *
+ * @return string
+ */
+ public function getData()
+ {
+ return $this->_data;
+ }
+
+ /**
+ * Retrieve token
+ *
+ * @return string|null
+ */
+ public function getToken()
+ {
+ return $this->_token;
+ }
+
+ /**
+ * Determine whether this is the last page of data
+ *
+ * @return void
+ */
+ public function isLast()
+ {
+ return (null === $this->_token);
+ }
+
+ /**
+ * Cast to string
+ *
+ * @return string
+ */
+ public function __toString()
+ {
+ return "Page with token: " . $this->_token
+ . "\n and data: " . $this->_data;
+ }
+}
View
190 library/Zend/Service/Amazon/SimpleDb/Response.php
@@ -0,0 +1,190 @@
+<?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 SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb;
+use Zend\Http;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class Response
+{
+ /**
+ * XML namespace used for SimpleDB responses.
+ */
+ protected $_xmlNamespace = 'http://sdb.amazonaws.com/doc/2009-04-15/';
+
+ /**
+ * 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 SimpleDB response object
+ *
+ * @param Zend\Http\Response $httpResponse the HTTP response.
+ * @return void
+ */
+ public function __construct(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('sdb',
+ $this->getNamespace());
+ }
+ }
+
+ return $this->_xpath;
+ }
+
+ /**
+ * Gets the SimpleXML document object for this response
+ *
+ * @return SimpleXMLElement
+ */
+ public function getSimpleXMLDocument()
+ {
+ try {
+ $body = $this->_httpResponse->getBody();
+ } catch (Http\Exception $e) {
+ $body = false;
+ }
+
+ return simplexml_load_string($body);
+ }
+
+ /**
+ * Get HTTP response object
+ *
+ * @return Zend\Http\Response
+ */
+ public function getHttpResponse()
+ {
+ return $this->_httpResponse;
+ }
+
+ /**
+ * Gets the document object for this response
+ *
+ * @return DOMDocument the DOM Document for this response.
+ */
+ public function getDocument()
+ {
+ try {
+ $body = $this->_httpResponse->getBody();
+ } catch (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;
+ }
+
+ /**
+ * Return the current set XML Namespace.
+ *
+ * @return string
+ */
+ public function getNamespace()
+ {
+ return $this->_xmlNamespace;
+ }
+
+ /**
+ * Set a new XML Namespace
+ *
+ * @param string $namespace
+ */
+ public function setNamespace($namespace)
+ {
+ $this->_xmlNamespace = $namespace;
+ }
+}
View
555 library/Zend/Service/Amazon/SimpleDb/SimpleDb.php
@@ -0,0 +1,555 @@
+<?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 SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+
+/**
+ * @namespace
+ */
+namespace Zend\Service\Amazon\SimpleDb;
+use Zend\Http,
+ Zend\Crypt;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon
+ * @subpackage SimpleDb
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class SimpleDb extends \Zend\Service\Amazon\AbstractAmazon
+{
+ /* Notes */
+ // TODO SSL is required
+
+ /**
+ * The HTTP query server
+ */
+ protected $_sdbEndpoint = 'sdb.amazonaws.com/';
+
+ /**
+ * Period after which HTTP request will timeout in seconds
+ */
+ protected $_httpTimeout = 10;
+
+ /**
+ * The API version to use
+ */
+ protected $_sdbApiVersion = '2009-04-15';
+
+ /**
+ * Signature Version
+ */
+ protected $_signatureVersion = '2';
+
+ /**
+ * Signature Encoding Method
+ */
+ protected $_signatureMethod = 'HmacSHA256';
+
+ /**
+ * Create Amazon SimpleDB 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, $secretKey)
+ {
+ parent::__construct($accessKey, $secretKey);
+ $this->setEndpoint("https://" . $this->_sdbEndpoint);
+ }
+
+ /**
+ * Set SimpleDB endpoint to use
+ *
+ * @param string|Zend\Uri\Http $endpoint
+ * @return Zend\Service\Amazon\SimpleDb\SimpleDb
+ */
+ public function setEndpoint($endpoint)
+ {
+ if(!($endpoint instanceof \Zend\Uri\Url)) {
+ $endpoint = new \Zend\Uri\Url($endpoint);
+ }
+ if(!$endpoint->isValid()) {
+ throw new Exception\InvalidArgumentException("Invalid endpoint supplied");
+ }
+ $this->_endpoint = $endpoint;
+ return $this;
+ }
+
+ /**
+ * Get SimpleDB endpoint
+ *
+ * @return Zend\Uri\Http
+ */
+ public function getEndpoint()
+ {
+ return $this->_endpoint;
+ }
+
+ /**
+ * Get attributes API method
+ *
+ * @param string $domainName Domain name within database
+ * @param string
+ */
+ public function getAttributes(
+ $domainName, $itemName, $attributeName = null
+ ) {
+ $params = array();
+ $params['Action'] = 'GetAttributes';
+ $params['DomainName'] = $domainName;
+ $params['ItemName'] = $itemName;
+
+ if (isset($attributeName)) {
+ $params['AttributeName'] = $attributeName;
+ }
+
+ $response = $this->_sendRequest($params);
+ $document = $response->getSimpleXMLDocument();
+
+ $attributeNodes = $document->GetAttributesResult->Attribute;
+
+ // Return an array of arrays
+ $attributes = array();
+ foreach($attributeNodes as $attributeNode) {
+ $name = (string)$attributeNode->Name;
+ $valueNodes = $attributeNode->Value;
+ $data = null;
+ if (is_array($valueNodes) && !empty($valueNodes)) {
+ $data = array();
+ foreach($valueNodes as $valueNode) {
+ $data[] = (string)$valueNode;
+ }
+ } elseif (isset($valueNodes)) {
+ $data = (string)$valueNodes;
+ }
+ if (isset($attributes[$name])) {
+ $attributes[$name]->addValue($data);
+ } else {
+ $attributes[$name] = new Attribute($itemName, $name, $data);
+ }
+ }
+ return $attributes;
+ }
+
+ /**
+ * Push attributes
+ *
+ * @param string $domainName
+ * @param string $itemName
+ * @param array|Traverable $attributes
+ * @param array $replace
+ * @return void
+ */
+ public function putAttributes(
+ $domainName, $itemName, $attributes, $replace = array()
+ ) {
+ $params = array();
+ $params['Action'] = 'PutAttributes';
+ $params['DomainName'] = $domainName;
+ $params['ItemName'] = $itemName;
+
+ $index = 0;
+ foreach ($attributes as $attribute) {
+ $attributeName = $attribute->getName();
+ foreach ($attribute->getValues() as $value) {
+ $params['Attribute.' . $index . '.Name'] = $attributeName;
+ $params['Attribute.' . $index . '.Value'] = $value;
+
+ // Check if it should be replaced
+ if(array_key_exists($attributeName, $replace) && $replace[$attributeName]) {
+ $params['Attribute.' . $index . '.Replace'] = 'true';
+ }
+ $index++;
+ }
+ }
+
+ // Exception should get thrown if there's an error
+ $response = $this->_sendRequest($params);
+ }
+
+ /**
+ * Add many attributes at once
+ *
+ * @param array $items
+ * @param string $domainName
+ * @param array $replace
+ * @return void
+ */
+ public function batchPutAttributes($items, $domainName, array $replace = array())
+ {
+
+ $params = array();
+ $params['Action'] = 'BatchPutAttributes';
+ $params['DomainName'] = $domainName;
+
+ $itemIndex = 0;
+ foreach ($items as $name => $attributes) {
+ $params['Item.' . $itemIndex . '.ItemName'] = $name;
+ $attributeIndex = 0;
+ foreach ($attributes as $attribute) {
+ // attribute value cannot be array, so when several items are passed
+ // they are treated as separate values with the same attribute name
+ foreach($attribute->getValues() as $value) {
+ $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Name'] = $attribute->getName();
+ $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Value'] = $value;
+ if (isset($replace[$name])
+ && isset($replace[$name][$attribute->getName()])
+ && $replace[$name][$attribute->getName()]
+ ) {
+ $params['Item.' . $itemIndex . '.Attribute.' . $attributeIndex . '.Replace'] = 'true';
+ }
+ $attributeIndex++;
+ }
+ }
+ $itemIndex++;
+ }
+
+ $response = $this->_sendRequest($params);
+ }
+
+ /**
+ * Delete attributes
+ *
+ * @param string $domainName
+ * @param string $itemName
+ * @param array $attributes
+ * @return void
+ */
+ public function deleteAttributes($domainName, $itemName, array $attributes = array())
+ {
+ $params = array();
+ $params['Action'] = 'DeleteAttributes';
+ $params['DomainName'] = $domainName;
+ $params['ItemName'] = $itemName;
+
+ $attributeIndex = 0;
+ foreach ($attributes as $attribute) {
+ foreach ($attribute->getValues() as $value) {
+ $params['Attribute.' . $attributeIndex . '.Name'] = $attribute->getName();
+ $params['Attribute.' . $attributeIndex . '.Value'] = $value;
+ $attributeIndex++;
+ }
+ }
+
+ $response = $this->_sendRequest($params);
+
+ return true;
+ }
+
+ /**
+ * List domains
+ *
+ * @param $maxNumberOfDomains int
+ * @param $nextToken int
+ * @return array 0 or more domain names
+ */
+ public function listDomains($maxNumberOfDomains = 100, $nextToken = null)
+ {
+ $params = array();
+ $params['Action'] = 'ListDomains';
+ $params['MaxNumberOfDomains'] = $maxNumberOfDomains;
+
+ if (null !== $nextToken) {
+ $params['NextToken'] = $nextToken;
+ }
+ $response = $this->_sendRequest($params);
+
+ $domainNodes = $response->getSimpleXMLDocument()->ListDomainsResult->DomainName;
+
+ $data = array();
+ foreach ($domainNodes as $domain) {
+ $data[] = (string)$domain;
+ }
+
+ $nextTokenNode = $response->getSimpleXMLDocument()->ListDomainsResult->NextToken;
+ $nextToken = (string)$nextTokenNode;
+ $nextToken = (trim($nextToken) === '') ? null : $nextToken;
+
+ return new Page($data, $nextToken);
+ }
+
+ /**
+ * Retrieve domain metadata
+ *
+ * @param $domainName string Name of the domain for which metadata will be requested
+ * @return array Key/value array of metadatum names and values.
+ */
+ public function domainMetadata($domainName)
+ {
+ $params = array();
+ $params['Action'] = 'DomainMetadata';
+ $params['DomainName'] = $domainName;
+ $response = $this->_sendRequest($params);
+
+ $document = $response->getSimpleXMLDocument();
+
+ $metadataNodes = $document->DomainMetadataResult->children();
+ $metadata = array();
+ foreach ($metadataNodes as $metadataNode) {
+ $name = $metadataNode->getName();
+ $metadata[$name] = (string)$metadataNode;
+ }
+
+ return $metadata;
+ }
+
+ /**
+ * Create a new domain
+ *
+ * @param $domainName string Valid domain name of the domain to create
+ * @return boolean True if successful, false if not
+ */
+ public function createDomain($domainName)
+ {
+ $params = array();
+ $params['Action'] = 'CreateDomain';
+ $params['DomainName'] = $domainName;
+ $response = $this->_sendRequest($params);
+ return $response->getHttpResponse()->isSuccessful();
+ }
+
+ /**
+ * Delete a domain
+ *
+ * @param $domainName string Valid domain name of the domain to delete
+ * @return boolean True if successful, false if not
+ */
+ public function deleteDomain($domainName)
+ {
+ $params = array();
+ $params['Action'] = 'DeleteDomain';
+ $params['DomainName'] = $domainName;
+ $response = $this->_sendRequest($params);
+ return $response->getHttpResponse()->isSuccessful();
+ }
+
+ /**
+ * Select items from the database
+ *
+ * @param string $selectExpression
+ * @param null|string $nextToken
+ * @return Zend\Service\Amazon\SimpleDb\Page
+ */
+ public function select($selectExpression, $nextToken = null)
+ {
+ $params = array();
+ $params['Action'] = 'Select';
+ $params['SelectExpression'] = $selectExpression;
+
+ if (null !== $nextToken) {
+ $params['NextToken'] = $nextToken;
+ }
+
+ $response = $this->_sendRequest($params);
+ $xml = $response->getSimpleXMLDocument();
+
+ $attributes = array();
+ foreach ($xml->SelectResult->Item as $item) {
+ $itemName = (string)$item->Name;
+
+ foreach ($item->Attribute as $attribute) {
+ $attributeName = (string)$attribute->Name;
+
+ $values = array();
+ foreach ($attribute->Value as $value) {
+ $values[] = (string)$value;
+ }
+ $attributes[$itemName][$attributeName] = new Attribute($itemName, $attributeName, $values);
+ }
+ }
+
+ $nextToken = (string)$xml->NextToken;
+
+ return new Page($attributes, $nextToken);
+ }
+
+ /**
+ * Quote SDB value
+ *
+ * Wraps it in ''
+ *
+ * @param string $value
+ * @return string
+ */
+ public function quote($value)
+ {
+ // wrap in single quotes and convert each ' inside to ''
+ return "'" . str_replace("'", "''", $value) . "'";
+ }
+
+ /**
+ * Quote SDB column or table name
+ *
+ * Wraps it in ``
+ * @param string $name
+ * @return string
+ */
+ public function quoteName($name)
+ {
+ if (preg_match('/^[a-z_$][a-z0-9_$-]*$/i', $name) == false) {
+ throw new Exception\InvalidArgumentException("Invalid name: can contain only alphanumeric characters, \$ and _");
+ }
+ return "`$name`";
+ }
+
+ /**
+ * Sends a HTTP request to the SimpleDB service using Zend\Http\Client
+ *
+ * @param array $params List of parameters to send with the request
+ * @return Zend\Service\Amazon\SimpleDb\Response
+ * @throws Zend\Service\Amazon\SimpleDb\Exception
+ */
+ protected function _sendRequest(array $params = array())
+ {
+ // UTF-8 encode all parameters and replace '+' characters
+ foreach ($params as $name => $value) {
+ unset($params[$name]);
+ $params[utf8_encode($name)] = $value;
+ }
+
+ $params = $this->_addRequiredParameters($params);
+
+ try {
+ $request = self::getHttpClient();
+ $request->resetParameters();
+
+ $request->setConfig(array(
+ 'timeout' => $this->_httpTimeout
+ ));
+
+
+ $request->setUri($this->getEndpoint());
+ $request->setMethod(Http\Client::POST);
+ foreach ($params as $key => $value) {
+ $params_out[] = rawurlencode($key)."=".rawurlencode($value);
+ }
+ $request->setRawData(join('&', $params_out), Http\Client::ENC_URLENCODED);
+ $httpResponse = $request->request();
+ } catch (Http\Client\Exception $zhce) {
+ $message = 'Error in request to AWS service: ' . $zhce->getMessage();
+ throw new Exception\RuntimeException($message, $zhce->getCode(), $zhce);
+ }
+
+ $response = new 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'] = $this->_signatureVersion;
+ $parameters['Timestamp'] = gmdate('c');
+ $parameters['Version'] = $this->_sdbApiVersion;
+ $parameters['SignatureMethod'] = $this->_signatureMethod;
+ $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->getEndpoint()->getHost() . "\n";
+ $data .= "/\n";
+
+ uksort($paramaters, 'strcmp');
+ unset($paramaters['Signature']);
+
+ $arrData = array();
+ foreach ($paramaters as $key => $value) {
+ $value = urlencode($value);
+ $value = str_replace("%7E", "~", $value);
+ $value = str_replace("+", "%20", $value);
+ $arrData[] = urlencode($key) . '=' . $value;
+ }
+
+ $data .= implode('&', $arrData);
+
+ $hmac = Crypt\Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Crypt\Hmac::BINARY);
+
+ return base64_encode($hmac);
+ }
+
+ /**
+ * Checks for errors responses from Amazon
+ *
+ * @param Zend\Service\Amazon\SimpleDb\Response $response the response object to
+ * check.
+ *
+ * @return void
+ *
+ * @throws Zend\Service\Amazon\SimpleDb\Exception if one or more errors are
+ * returned from Amazon.
+ */
+ private function _checkForErrors(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 Exception\RuntimeException($message, (double)$code);
+ }
+ }
+}
View
185 tests/Zend/Service/Amazon/Authentication/S3Test.php
@@ -0,0 +1,185 @@
+<?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_Authentication
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: AllTests.php 11973 2008-10-15 16:00:56Z matthew $
+ */
+
+/**
+ * @namespace
+ */
+namespace ZendTest\Service\Amazon\Authentication;
+
+use Zend\Service\Amazon\Authentication,
+ Zend\Service\Amazon\Authentication\Exception;
+
+/**
+ * S3 authentication test case
+ *
+ * @category Zend
+ * @package Zend_Service_Amazon_Authentication
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class S3Test extends \PHPUnit_Framework_TestCase
+{
+
+ /**
+ * @var Zend\Service\Amazon\Authentication\S3
+ */
+ private $_amazon;
+
+ /**
+ * Prepares the environment before running a test.
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ // TODO Auto-generated Zend_Service_Amazon_Authentication_S3Test::setUp()
+
+
+ $this->_amazon = new Authentication\S3('0PN5J17HBGZHT7JJ3X82', 'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o', '2006-03-01');
+
+ }
+
+ /**
+ * Cleans up the environment after running a test.
+ */
+ protected function tearDown()
+ {
+ // TODO Auto-generated Zend_Service_Amazon_Authentication_S3Test::tearDown()
+
+ $this->_amazon = null;
+
+ parent::tearDown();
+ }
+
+
+ public function testGetGeneratesCorrectSignature()
+ {
+ $headers = array();
+ $headers['Date'] = "Tue, 27 Mar 2007 19:36:42 +0000";
+
+ $ret = $this->_amazon->generateSignature('GET', 'http://s3.amazonaws.com/johnsmith/photos/puppy.jpg', $headers);
+
+ $this->assertEquals('AWS 0PN5J17HBGZHT7JJ3X82:soqB4L9flQ6AHG4d5FVnKj26D2s=', $headers['Authorization']);
+ $this->assertEquals($ret, "GET
+
+
+Tue, 27 Mar 2007 19:36:42 +0000
+//johnsmith/photos/puppy.jpg");
+ }
+
+ public function testPutGeneratesCorrectSignature()
+ {
+ $headers = array();
+ $headers['Date'] = "Tue, 27 Mar 2007 21:15:45 +0000";
+ $headers['Content-Type'] = "image/jpeg";
+ $headers['Content-Length'] = 94328;
+
+ $ret = $this->_amazon->generateSignature('PUT', 'http://s3.amazonaws.com/johnsmith/photos/puppy.jpg', $headers);
+
+ $this->assertEquals('AWS 0PN5J17HBGZHT7JJ3X82:88cf7BdpjrBlCsIiWWLn8wLpWzI=', $headers['Authorization']);
+ $this->assertEquals($ret, "PUT
+
+image/jpeg
+Tue, 27 Mar 2007 21:15:45 +0000
+//johnsmith/photos/puppy.jpg");
+ }
+
+ public function testListGeneratesCorrectSignature()
+ {
+ $headers = array();
+ $headers['Date'] = "Tue, 27 Mar 2007 19:42:41 +0000";
+
+ $ret = $this->_amazon->generateSignature('GET', 'http://s3.amazonaws.com/johnsmith/?prefix=photos&max-keys=50&marker=puppy', $headers);
+
+ $this->assertEquals('AWS 0PN5J17HBGZHT7JJ3X82:pm3Adv2BIFCCJiUSikcLcGYFtiA=', $headers['Authorization']);
+ $this->assertEquals($ret, "GET
+
+
+Tue, 27 Mar 2007 19:42:41 +0000
+//johnsmith/");
+ }
+
+ public function testFetchGeneratesCorrectSignature()
+ {
+ $headers = array();
+ $headers['Date'] = "Tue, 27 Mar 2007 19:44:46 +0000";
+
+ $ret = $this->_amazon->generateSignature('GET', 'http://s3.amazonaws.com/johnsmith/?acl', $headers);
+
+ $this->assertEquals('AWS 0PN5J17HBGZHT7JJ3X82:TCNlZPuxY41veihZbxjnjw8P93w=', $headers['Authorization']);
+ $this->assertEquals($ret, "GET
+
+
+Tue, 27 Mar 2007 19:44:46 +0000
+//johnsmith/?acl");
+ }
+
+ public function testDeleteGeneratesCorrectSignature()
+ {
+
+ $headers = array();
+ $headers['x-amz-date'] = "Tue, 27 Mar 2007 21:20:26 +0000";
+
+ $ret = $this->_amazon->generateSignature('DELETE', 'http://s3.amazonaws.com/johnsmith/photos/puppy.jpg', $headers);
+
+ $this->assertEquals('AWS 0PN5J17HBGZHT7JJ3X82:O9AsSXUIowhjTiJC5escAqjsAyk=', $headers['Authorization']);
+ $this->assertEquals($ret, "DELETE
+
+
+
+x-amz-date:Tue, 27 Mar 2007 21:20:26 +0000
+//johnsmith/photos/puppy.jpg");
+ }
+
+ public function testUploadGeneratesCorrectSignature()
+ {
+ $headers = array();
+ $headers['Date'] = "Tue, 27 Mar 2007 21:06:08 +0000";
+ $headers['x-amz-acl'] = "public-read";
+ $headers['content-type'] = "application/x-download";
+ $headers['Content-MD5'] = "4gJE4saaMU4BqNR0kLY+lw==";
+ $headers['X-Amz-Meta-ReviewedBy'][] = "joe@johnsmith.net";
+ $headers['X-Amz-Meta-ReviewedBy'][] = "jane@johnsmith.net";
+ $headers['X-Amz-Meta-FileChecksum'] = "0x02661779";
+ $headers['X-Amz-Meta-ChecksumAlgorithm'] = "crc32";
+ $headers['Content-Disposition'] = "attachment; filename=database.dat";
+ $headers['Content-Encoding'] = "gzip";
+ $headers['Content-Length'] = "5913339";
+
+
+ $ret = $this->_amazon->generateSignature('PUT', 'http://s3.amazonaws.com/static.johnsmith.net/db-backup.dat.gz', $headers);
+
+ $this->assertEquals('AWS 0PN5J17HBGZHT7JJ3X82:IQh2zwCpX2xqRgP2rbIkXL/GVbA=', $headers['Authorization']);
+ $this->assertEquals($ret, "PUT
+4gJE4saaMU4BqNR0kLY+lw==
+application/x-download
+Tue, 27 Mar 2007 21:06:08 +0000
+x-amz-acl:public-read
+x-amz-meta-checksumalgorithm:crc32
+x-amz-meta-filechecksum:0x02661779
+x-amz-meta-reviewedby:joe@johnsmith.net,jane@johnsmith.net
+//static.johnsmith.net/db-backup.dat.gz");
+ }
+
+}
+
View
86 tests/Zend/Service/Amazon/Authentication/V1Test.php
@@ -0,0 +1,86 @@
+<?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_Authentication
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: AllTests.php 11973 2008-10-15 16:00:56Z matthew $
+ */
+
+
+/**
+ * @namespace
+ */
+namespace ZendTest\Service\Amazon\Authentication;
+
+use Zend\Service\Amazon\Authentication,
+ Zend\Service\Amazon\Authentication\Exception;
+
+/**
+ * Amazon V1 authentication test case
+ *
+ * @category Zend
+ * @package Zend_Service_Amazon_Authentication
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class V1Test extends \PHPUnit_Framework_TestCase
+{
+
+ /**
+ * @var Authentication\V1
+ */
+ private $_amazon;
+
+ /**
+ * Prepares the environment before running a test.
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->_amazon = new Authentication\V1('0PN5J17HBGZHT7JJ3X82', 'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o', '2007-12-01');
+ }
+
+ /**
+ * Cleans up the environment after running a test.
+ */
+ protected function tearDown()
+ {
+ $this->_amazon = null;
+
+ parent::tearDown();
+ }
+
+ /**
+ * Tests Authentication\V1::generateSignature()
+ */
+ public function testGenerateDevPaySignature()
+ {
+ $url = "https://ls.amazonaws.com/";
+ $params = array();
+ $params['Action'] = "ActivateHostedProduct";
+ $params['Timestamp'] = "2009-11-11T13:52:38Z";
+
+ $ret = $this->_amazon->generateSignature($url, $params);
+
+ $this->assertEquals('31Q2YlgABM5X3GkYQpGErcL10Xc=', $params['Signature']);
+ $this->assertEquals("ActionActivateHostedProductAWSAccessKeyId0PN5J17HBGZHT7JJ3X82SignatureVersion1Timestamp2009-11-11T13:52:38ZVersion2007-12-01", $ret);
+ }
+
+}
+
View
102 tests/Zend/Service/Amazon/Authentication/V2Test.php
@@ -0,0 +1,102 @@
+<?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_Authentication
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id: AllTests.php 11973 2008-10-15 16:00:56Z matthew $
+ */
+
+/**
+ * @namespace
+ */
+namespace ZendTest\Service\Amazon\Authentication;
+
+use Zend\Service\Amazon\Authentication,
+ Zend\Service\Amazon\Authentication\Exception;
+
+/**
+ * Amazon V2 authentication test case
+ *
+ * @category Zend
+ * @package Zend_Service_Amazon_Authentication
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class V2Test extends \PHPUnit_Framework_TestCase
+{
+
+ /**
+ * @var Authentication\V2
+ */
+ private $_amazon;
+
+ /**
+ * Prepares the environment before running a test.
+ */
+ protected function setUp()
+ {
+ parent::setUp();
+
+ $this->_amazon = new Authentication\V2('0PN5J17HBGZHT7JJ3X82', 'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o', '2009-07-15');
+ }
+
+ /**
+ * Cleans up the environment after running a test.
+ */
+ protected function tearDown()
+ {
+ $this->_amazon = null;
+
+ parent::tearDown();
+ }
+
+ /**
+ * Tests Authentication\V2::generateSignature()
+ */
+ public function testGenerateEc2PostSignature()
+ {
+ $url = "https://ec2.amazonaws.com/";
+ $params = array();
+ $params['Action'] = "DescribeImages";
+ $params['ImageId.1'] = "ami-2bb65342";
+ $params['Timestamp'] = "2009-11-11T13:52:38Z";
+
+ $ret = $this->_amazon->generateSignature($url, $params);
+
+ $this->assertEquals('8B2cxwK/dfezT49KEzD+wjo1ZbJCddyFOLA0RNZobbc=', $params['Signature']);
+ $this->assertEquals(file_get_contents(dirname(__FILE__) . '/_files/ec2_v2_return.txt'), $ret);
+ }
+
+ public function testGenerateSqsGetSignature()
+ {
+ $url = "https://queue.amazonaws.com/770098461991/queue2";
+ $params = array();
+ $params['Action'] = "SetQueueAttributes";
+ $params['Attribute.Name'] = "VisibilityTimeout";
+ $params['Attribute.Value'] = "90";
+ $params['Timestamp'] = "2009-11-11T13:52:38Z";
+
+ $this->_amazon->setHttpMethod('GET');
+ $ret = $this->_amazon->generateSignature($url, $params);
+
+ $this->assertEquals('YSw7HXDqokM/A6DhLz8kG+sd+oD5eMjqx3a02A0+GkE=', $params['Signature']);
+ $this->assertEquals(file_get_contents(dirname(__FILE__) . '/_files/sqs_v2_get_return.txt'), $ret);
+ }
+
+}
+
View
4 tests/Zend/Service/Amazon/Authentication/_files/ec2_v2_return.txt
@@ -0,0 +1,4 @@
+POST
+ec2.amazonaws.com
+/
+AWSAccessKeyId=0PN5J17HBGZHT7JJ3X82&Action=DescribeImages&ImageId.1=ami-2bb65342&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2009-11-11T13%3A52%3A38Z&Version=2009-07-15
View
4 tests/Zend/Service/Amazon/Authentication/_files/sqs_v2_get_return.txt
@@ -0,0 +1,4 @@
+GET
+queue.amazonaws.com
+/770098461991/queue2
+AWSAccessKeyId=0PN5J17HBGZHT7JJ3X82&Action=SetQueueAttributes&Attribute.Name=VisibilityTimeout&Attribute.Value=90&SignatureMethod=HmacSHA256&SignatureVersion=2&Timestamp=2009-11-11T13%3A52%3A38Z&Version=2009-07-15
View
46 tests/Zend/Service/Amazon/SimpleDb/OfflineTest.php
@@ -0,0 +1,46 @@
+<?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_SimpleDb
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @version $Id$
+ */
+
+
+/**
+ * @namespace
+ */
+namespace ZendTest\Service\Amazon\SimpleDb;
+
+/**
+ * @category Zend
+ * @package Zend_Service_Amazon_SimpleDb
+ * @subpackage UnitTests
+ * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ */
+class OfflineTest extends \PHPUnit_Framework_TestCase
+{
+ public function setUp()
+ {
+ $this->markTestSkipped('No offline tests for Zend_Service_Amazon_SimpleDb');
+ }
+
+ public function testNothing()
+ {
+ }
+}