Sfoglia il codice sorgente

#525 update Amazon S3 PHP library

Alex Trofimov 10 anni fa
parent
commit
2976d8e7d8
2 ha cambiato i file con 43 aggiunte e 12 eliminazioni
  1. 1 0
      inc/classes/BxDolStorage.php
  2. 42 12
      plugins/amazon-s3/S3.php

+ 1 - 0
inc/classes/BxDolStorage.php

@@ -315,6 +315,7 @@ abstract class BxDolStorage extends BxDolFactory implements iBxDolFactoryObject
      */
     public function getErrorString()
     {
+        bx_import('BxDolLanguages');
         $a = array (
             1000 => '_sys_storage_err_no_input_method',
             1001 => '_sys_storage_err_no_file',

+ 42 - 12
plugins/amazon-s3/S3.php

@@ -32,7 +32,7 @@
 * Amazon S3 PHP class
 *
 * @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class
-* @version 0.5.1-dev
+* @version 0.5.1
 */
 class S3
 {
@@ -44,6 +44,7 @@ class S3
 
 	const STORAGE_CLASS_STANDARD = 'STANDARD';
 	const STORAGE_CLASS_RRS = 'REDUCED_REDUNDANCY';
+	const STORAGE_CLASS_STANDARD_IA = 'STANDARD_IA';
 
 	const SSE_NONE = '';
 	const SSE_AES256 = 'AES256';
@@ -56,7 +57,7 @@ class S3
 	 * @static
 	 */
 	private static $__accessKey = null;
-	
+
 	/**
 	 * AWS Secret Key
 	 *
@@ -65,7 +66,7 @@ class S3
 	 * @static
 	 */
 	private static $__secretKey = null;
-	
+
 	/**
 	 * SSL Client key
 	 *
@@ -74,7 +75,15 @@ class S3
 	 * @static
 	 */
 	private static $__sslKey = null;
-	
+
+	/**
+	 * Default delimiter to be used, for example while getBucket().
+	 * @var string
+	 * @access public
+	 * @static 
+	 */
+	public static $defDelimiter = null;
+
 	/**
 	 * AWS URI
 	 *
@@ -83,7 +92,7 @@ class S3
 	 * @static
 	 */
 	public static $endpoint = 's3.amazonaws.com';
-	
+
 	/**
 	 * Proxy information
 	 *
@@ -92,7 +101,7 @@ class S3
 	 * @static
 	 */
 	public static $proxy = null;
-	
+
 	/**
 	 * Connect using SSL?
 	 *
@@ -101,7 +110,7 @@ class S3
 	 * @static
 	 */
 	public static $useSSL = false;
-	
+
 	/**
 	 * Use SSL validation?
 	 *
@@ -110,7 +119,16 @@ class S3
 	 * @static
 	 */
 	public static $useSSLValidation = true;
-	
+
+	/**
+	 * Use SSL version
+	 *
+	 * @var const
+	 * @access public
+	 * @static
+	 */
+	public static $useSSLVersion = CURL_SSLVERSION_TLSv1;
+
 	/**
 	 * Use PHP exceptions?
 	 *
@@ -202,6 +220,7 @@ class S3
 		self::$endpoint = $host;
 	}
 
+
 	/**
 	* Set AWS access key and secret key
 	*
@@ -416,6 +435,7 @@ class S3
 		if ($marker !== null && $marker !== '') $rest->setParameter('marker', $marker);
 		if ($maxKeys !== null && $maxKeys !== '') $rest->setParameter('max-keys', $maxKeys);
 		if ($delimiter !== null && $delimiter !== '') $rest->setParameter('delimiter', $delimiter);
+		else if (!empty(self::$defDelimiter)) $rest->setParameter('delimiter', self::$defDelimiter);
 		$response = $rest->getResponse();
 		if ($response->error === false && $response->code !== 200)
 			$response->error = array('code' => $response->code, 'message' => 'Unexpected HTTP status');
@@ -561,6 +581,7 @@ class S3
 			self::__triggerError('S3::inputFile(): Unable to open input file: '.$file, __FILE__, __LINE__);
 			return false;
 		}
+		clearstatcache(false, $file);
 		return array('file' => $file, 'size' => filesize($file), 'md5sum' => $md5sum !== false ?
 		(is_string($md5sum) ? $md5sum : base64_encode(md5_file($file, true))) : '');
 	}
@@ -634,15 +655,18 @@ class S3
 		if (isset($input['size']) && $input['size'] >= 0)
 			$rest->size = $input['size'];
 		else {
-			if (isset($input['file']))
+			if (isset($input['file'])) {
+				clearstatcache(false, $input['file']);
 				$rest->size = filesize($input['file']);
+			}
 			elseif (isset($input['data']))
 				$rest->size = strlen($input['data']);
 		}
 
 		// Custom request headers (Content-Type, Content-Disposition, Content-Encoding)
 		if (is_array($requestHeaders))
-			foreach ($requestHeaders as $h => $v) $rest->setHeader($h, $v);
+			foreach ($requestHeaders as $h => $v)
+				strpos($h, 'x-amz-') === 0 ? $rest->setAmzHeader($h, $v) : $rest->setHeader($h, $v);
 		elseif (is_string($requestHeaders)) // Support for legacy contentType parameter
 			$input['type'] = $requestHeaders;
 
@@ -797,7 +821,8 @@ class S3
 	{
 		$rest = new S3Request('PUT', $bucket, $uri, self::$endpoint);
 		$rest->setHeader('Content-Length', 0);
-		foreach ($requestHeaders as $h => $v) $rest->setHeader($h, $v);
+		foreach ($requestHeaders as $h => $v)
+				strpos($h, 'x-amz-') === 0 ? $rest->setAmzHeader($h, $v) : $rest->setHeader($h, $v);
 		foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v);
 		if ($storageClass !== self::STORAGE_CLASS_STANDARD) // Storage class
 			$rest->setAmzHeader('x-amz-storage-class', $storageClass);
@@ -1896,7 +1921,7 @@ final class S3Request
 	 * AWS URI
 	 *
 	 * @var string
-	 * @access pricate
+	 * @access private
 	 */
 	private $endpoint;
 	
@@ -2117,6 +2142,9 @@ final class S3Request
 
 		if (S3::$useSSL)
 		{
+			// Set protocol version
+			curl_setopt($curl, CURLOPT_SSLVERSION, S3::$useSSLVersion);
+
 			// SSL Validation can now be optional for those with broken OpenSSL installations
 			curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, S3::$useSSLValidation ? 2 : 0);
 			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, S3::$useSSLValidation ? 1 : 0);
@@ -2292,6 +2320,7 @@ final class S3Request
 	private function __dnsBucketName($bucket)
 	{
 		if (strlen($bucket) > 63 || preg_match("/[^a-z0-9\.-]/", $bucket) > 0) return false;
+		if (S3::$useSSL && strstr($bucket, '.') !== false) return false;
 		if (strstr($bucket, '-.') !== false) return false;
 		if (strstr($bucket, '..') !== false) return false;
 		if (!preg_match("/^[0-9a-z]/", $bucket)) return false;
@@ -2358,3 +2387,4 @@ class S3Exception extends Exception {
 		$this->line = $line;
 	}
 }
+