From f6592b9439f955a01be64094dab2d82663b8c833 Mon Sep 17 00:00:00 2001 From: Adrian Greeve Date: Tue, 15 Mar 2016 14:07:17 +0800 Subject: [PATCH] MDL-53467 repositories: Upgrade s3 to v0.5.1 --- repository/s3/README_MOODLE.txt | 1 - repository/s3/S3.php | 50 +++++++++++++++++++++++++------- repository/s3/lib.php | 5 ++++ repository/s3/thirdpartylibs.xml | 2 +- repository/s3/version.php | 2 +- 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/repository/s3/README_MOODLE.txt b/repository/s3/README_MOODLE.txt index b387765b5d0..4f44dc36d9a 100644 --- a/repository/s3/README_MOODLE.txt +++ b/repository/s3/README_MOODLE.txt @@ -3,7 +3,6 @@ Amazon S3 PHP Class Cloned from git://github.com/tpyo/amazon-s3-php-class.git -At commit 8413f6f70ad3bb79ae756958d4ba2238514b00af https://github.com/tpyo/amazon-s3-php-class http://undesigned.org.za/2007/10/22/amazon-s3-php-class diff --git a/repository/s3/S3.php b/repository/s3/S3.php index 0b1564b20ca..d8048c799dd 100644 --- a/repository/s3/S3.php +++ b/repository/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 { @@ -56,7 +56,7 @@ class S3 * @static */ private static $__accessKey = null; - + /** * AWS Secret Key * @@ -65,7 +65,7 @@ class S3 * @static */ private static $__secretKey = null; - + /** * SSL Client key * @@ -74,7 +74,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 +91,7 @@ class S3 * @static */ public static $endpoint = 's3.amazonaws.com'; - + /** * Proxy information * @@ -92,7 +100,7 @@ class S3 * @static */ public static $proxy = null; - + /** * Connect using SSL? * @@ -101,7 +109,7 @@ class S3 * @static */ public static $useSSL = false; - + /** * Use SSL validation? * @@ -110,7 +118,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 +219,7 @@ class S3 self::$endpoint = $host; } + /** * Set AWS access key and secret key * @@ -416,6 +434,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 +580,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 +654,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 +820,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); @@ -2117,6 +2141,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 +2319,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; diff --git a/repository/s3/lib.php b/repository/s3/lib.php index efb99094f96..ea87af0f347 100644 --- a/repository/s3/lib.php +++ b/repository/s3/lib.php @@ -26,6 +26,11 @@ require_once($CFG->dirroot . '/repository/lib.php'); require_once($CFG->dirroot . '/repository/s3/S3.php'); +// This constant is not defined in php 5.4. Set it to avoid errors. +if (!defined('CURL_SSLVERSION_TLSv1')) { + define('CURL_SSLVERSION_TLSv1', 1); +} + /** * This is a repository class used to browse Amazon S3 content. * diff --git a/repository/s3/thirdpartylibs.xml b/repository/s3/thirdpartylibs.xml index f53ed95d845..04bb560a566 100644 --- a/repository/s3/thirdpartylibs.xml +++ b/repository/s3/thirdpartylibs.xml @@ -4,7 +4,7 @@ S3.php S3 BSD - 0.5.1-dev + 0.5.1 diff --git a/repository/s3/version.php b/repository/s3/version.php index df6d1fa3a0b..8cba240b0eb 100644 --- a/repository/s3/version.php +++ b/repository/s3/version.php @@ -26,6 +26,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015111600; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2015111601; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2015111000; // Requires this Moodle version $plugin->component = 'repository_s3'; // Full name of the plugin (used for diagnostics)