web/lib/Zend/Service/Amazon/S3.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
--- a/web/lib/Zend/Service/Amazon/S3.php	Thu Mar 21 17:31:31 2013 +0100
+++ b/web/lib/Zend/Service/Amazon/S3.php	Thu Mar 21 19:50:53 2013 +0100
@@ -15,9 +15,9 @@
  * @category   Zend
  * @package    Zend_Service
  * @subpackage Amazon_S3
- * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  * @license    http://framework.zend.com/license/new-bsd     New BSD License
- * @version    $Id: S3.php 23224 2010-10-22 13:45:57Z matthew $
+ * @version    $Id: S3.php 24593 2012-01-05 20:35:02Z matthew $
  */
 
 /**
@@ -36,7 +36,7 @@
  * @category   Zend
  * @package    Zend_Service
  * @subpackage Amazon_S3
- * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
+ * @copyright  Copyright (c) 2005-2012 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/
  */
@@ -157,14 +157,15 @@
     public function createBucket($bucket, $location = null)
     {
         $this->_validBucketName($bucket);
-
+        $headers=array();
         if($location) {
             $data = '<CreateBucketConfiguration><LocationConstraint>'.$location.'</LocationConstraint></CreateBucketConfiguration>';
-        }
-        else {
+            $headers[self::S3_CONTENT_TYPE_HEADER]= 'text/plain';
+            $headers['Content-size']= strlen($data);
+        } else {
             $data = null;
         }
-        $response = $this->_makeRequest('PUT', $bucket, null, array(), $data);
+        $response = $this->_makeRequest('PUT', $bucket, null, $headers, $data);
 
         return ($response->getStatus() == 200);
     }
@@ -273,9 +274,16 @@
             return false;
         }
 
-        foreach ($objects as $object) {
-            $this->removeObject("$bucket/$object");
+        while (!empty($objects)) {
+            foreach ($objects as $object) {
+                $this->removeObject("$bucket/$object");
+            }
+            $params= array (
+                'marker' => $objects[count($objects)-1]
+            );
+            $objects = $this->getObjectsByBucket($bucket,$params);
         }
+        
         return true;
     }
 
@@ -313,7 +321,52 @@
 
         return $objects;
     }
+     /**
+     * List the objects and common prefixes in a bucket.
+     *
+     * Provides the list of object keys and common prefixes that are contained in the bucket.  Valid params include the following.
+     * prefix - Limits the response to keys which begin with the indicated prefix. You can use prefixes to separate a bucket into different sets of keys in a way similar to how a file system uses folders.
+     * marker - Indicates where in the bucket to begin listing. The list will only include keys that occur lexicographically after marker. This is convenient for pagination: To get the next page of results use the last key of the current page as the marker.
+     * max-keys - The maximum number of keys you'd like to see in the response body. The server might return fewer than this many keys, but will not return more.
+     * delimiter - Causes keys that contain the same string between the prefix and the first occurrence of the delimiter to be rolled up into a single result element in the CommonPrefixes collection. These rolled-up keys are not returned elsewhere in the response.
+     *
+     * @see ZF-11401
+     * @param  string $bucket
+     * @param array $params S3 GET Bucket Paramater
+     * @return array|false
+     */
+    public function getObjectsAndPrefixesByBucket($bucket, $params = array())
+    {
+        $response = $this->_makeRequest('GET', $bucket, $params);
 
+        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;
+                }
+            }
+        }
+        $prefixes = array();
+        if (isset($xml->CommonPrefixes)) {
+            foreach ($xml->CommonPrefixes as $prefix) {
+                foreach ($prefix->Prefix as $object) {
+                    $prefixes[] = (string)$object;
+                }
+            }
+        }
+
+        return array(
+            'objects'  => $objects,
+            'prefixes' => $prefixes
+        );
+    }
     /**
      * Make sure the object name is valid
      *
@@ -482,7 +535,7 @@
         }
 
         if(!isset($meta['Content-MD5'])) {
-            $headers['Content-MD5'] = base64_encode(md5_file($path, true));
+            $meta['Content-MD5'] = base64_encode(md5_file($path, true));
         }
 
         return $this->putObject($object, $data, $meta);
@@ -557,11 +610,11 @@
     /**
      * Make a request to Amazon S3
      *
-     * @param  string $method	Request method
-     * @param  string $path		Path to requested object
-     * @param  array  $params	Request parameters
-     * @param  array  $headers	HTTP headers
-     * @param  string|resource $data		Request data
+     * @param  string $method    Request method
+     * @param  string $path        Path to requested object
+     * @param  array  $params    Request parameters
+     * @param  array  $headers    HTTP headers
+     * @param  string|resource $data        Request data
      * @return Zend_Http_Response
      */
     public function _makeRequest($method, $path='', $params=null, $headers=array(), $data=null)
@@ -590,7 +643,11 @@
             $endpoint->setHost($parts[0].'.'.$endpoint->getHost());
         }
         if (!empty($parts[1])) {
-            $endpoint->setPath('/'.$parts[1]);
+            // ZF-10218, ZF-10122
+            $pathparts = explode('?',$parts[1]);
+            $endpath = $pathparts[0];
+            $endpoint->setPath('/'.$endpath);
+            
         }
         else {
             $endpoint->setPath('/');
@@ -598,16 +655,16 @@
                 $path = $parts[0].'/';
             }
         }
-
         self::addSignature($method, $path, $headers);
 
         $client = self::getHttpClient();
 
-        $client->resetParameters();
+        $client->resetParameters(true);
         $client->setUri($endpoint);
         $client->setAuth(false);
         // Work around buglet in HTTP client - it doesn't clean headers
         // Remove when ZHC is fixed
+        /*
         $client->setHeaders(array('Content-MD5'              => null,
                                   'Content-Encoding'         => null,
                                   'Expect'                   => null,
@@ -615,7 +672,7 @@
                                   'x-amz-acl'                => null,
                                   'x-amz-copy-source'        => null,
                                   'x-amz-metadata-directive' => null));
-
+        */
         $client->setHeaders($headers);
 
         if (is_array($params)) {
@@ -629,7 +686,7 @@
                  $headers['Content-type'] = self::getMimeType($path);
              }
              $client->setRawData($data, $headers['Content-type']);
-         }
+         } 
          do {
             $retry = false;
 
@@ -720,6 +777,9 @@
         else if (strpos($path, '?torrent') !== false) {
             $sig_str .= '?torrent';
         }
+        else if (strpos($path, '?versions') !== false) {
+            $sig_str .= '?versions';
+        }
 
         $signature = base64_encode(Zend_Crypt_Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), Zend_Crypt_Hmac::BINARY));
         $headers['Authorization'] = 'AWS '.$this->_getAccessKey().':'.$signature;