Skip to content

Commit

Permalink
MDL-53467 repositories: Upgrade s3 to v0.5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
abgreeve committed Mar 29, 2016
1 parent fb75b07 commit f6592b9
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
1 change: 0 additions & 1 deletion repository/s3/README_MOODLE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
50 changes: 39 additions & 11 deletions repository/s3/S3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -56,7 +56,7 @@ class S3
* @static
*/
private static $__accessKey = null;

/**
* AWS Secret Key
*
Expand All @@ -65,7 +65,7 @@ class S3
* @static
*/
private static $__secretKey = null;

/**
* SSL Client key
*
Expand All @@ -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
*
Expand All @@ -83,7 +91,7 @@ class S3
* @static
*/
public static $endpoint = 's3.amazonaws.com';

/**
* Proxy information
*
Expand All @@ -92,7 +100,7 @@ class S3
* @static
*/
public static $proxy = null;

/**
* Connect using SSL?
*
Expand All @@ -101,7 +109,7 @@ class S3
* @static
*/
public static $useSSL = false;

/**
* Use SSL validation?
*
Expand All @@ -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?
*
Expand Down Expand Up @@ -202,6 +219,7 @@ public function setEndpoint($host)
self::$endpoint = $host;
}


/**
* Set AWS access key and secret key
*
Expand Down Expand Up @@ -416,6 +434,7 @@ public static function getBucket($bucket, $prefix = null, $marker = null, $maxKe
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');
Expand Down Expand Up @@ -561,6 +580,7 @@ public static function inputFile($file, $md5sum = true)
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))) : '');
}
Expand Down Expand Up @@ -634,15 +654,18 @@ public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE
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;

Expand Down Expand Up @@ -797,7 +820,8 @@ public static function copyObject($srcBucket, $srcUri, $bucket, $uri, $acl = sel
{
$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);
Expand Down Expand Up @@ -2117,6 +2141,9 @@ public function getResponse()

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);
Expand Down Expand Up @@ -2292,6 +2319,7 @@ private function __responseWriteCallback(&$curl, &$data)
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;
Expand Down
5 changes: 5 additions & 0 deletions repository/s3/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 1 addition & 1 deletion repository/s3/thirdpartylibs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<location>S3.php</location>
<name>S3</name>
<license>BSD</license>
<version>0.5.1-dev</version>
<version>0.5.1</version>
<licenseversion></licenseversion>
</library>
</libraries>
2 changes: 1 addition & 1 deletion repository/s3/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit f6592b9

Please sign in to comment.