<?php
/**
* Copyright (c) 2007, Donovan Schonknecht. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
// debug info
var $err_code;
var $err_message;
var $isTruncated; // boolean
/** ***********************************************************************
* Constructor, set AWS access key and secret key
*
* @param string $accessKey Access key
* @param string $secretKey Secret key
* @param string $magicFile MIME Magic file (for Fileinfo if you use it)
* @return void
*/
function __construct($accessKey = null, $secretKey = null) {
if (!extension_loaded('curl') && !@dl(PHP_SHLIB_SUFFIX == 'so' ? 'curl.so' : 'php_curl.dll'))
throw new Exception('Unable to load CURL extension');
if ($accessKey !== null) self::$accessKey = $accessKey;
if ($secretKey !== null) self::$secretKey = $secretKey;
}
/** ***********************************************************************
* Get a list of buckets
*
* @return array | false
*/
public function listBuckets() {
$get = new S3Object('GET', '', '');
$get = $get->getResponse(true);
//var_dump($get);
if (!isset($get->body)) return false;
$results = array();
if (isset($get->body->Buckets))
foreach ($get->body->Buckets->Bucket as $b)
$results[] = (string)$b->Name;
if ($get->code == 200)
{
return $results;
}
else
{
$this->err_code = $get->body->Code;
$this->err_message = $get->body->Message;
return false;
}
}
/** ***********************************************************************
* Get contents for a bucket
*
* In the case of truncation (more than a 1000 keys), the marker should be
* set to the key that was the last one returned from the previous retrieve.
*
* @param string $bucket Bucket name
* @param string $prefix Prefix
* @param string $marker Marker
* @param string $maxKeys Max keys
* @return array | false
*/
public function getBucket($bucket, $prefix = null, $marker = null, $maxKeys = null) {
$get = new S3Object('GET', $bucket, '');
if ($prefix !== null) $get->setParameter('prefix', $prefix);
if ($marker !== null) $get->setParameter('marker', $marker);
if ($maxKeys !== null) $get->setParameter('max-keys', $maxKeys);
$get = $get->getResponse(true);
if (!isset($get->body)) return false;
$results = array();
if (isset($get->body->IsTruncated))
{
if ($get->body->IsTruncated == "true")
{
$this->IsTruncated = true;
}
else
{
$this->IsTruncated = false;
}
}
if (isset($get->body->Contents))
foreach ($get->body->Contents as $c)
$results[(string)$c->Key] = array(
'time' => strToTime((string)$c->LastModified),
'hash' => substr((string)$c->ETag, 1, -1),
'size' => (int)$c->Size
);
if ($get->code == 200)
{
return $results;
}
else
{
$this->err_code = $get->body->Code;
$this->err_message = $get->body->Message;
return false;
}
}
/** ***********************************************************************
* Put a bucket
*
* @param string $bucket Bucket name
* @param constant $acl ACL flag
* @return mixed
*/
public function putBucket($bucket, $acl = self::ACL_PRIVATE) {
$put = new S3Object('PUT', $bucket, '');
$put->setAmzHeader('x-amz-acl', $acl);
$put = $put->getResponse(true);
if ($put->code == 200)
{
return true;
}
else
{
$this->err_code = $put->body->Code;
$this->err_message = $put->body->Message;
return false;
}
}
/** ***********************************************************************
* Delete a bucket
*
* @param string $bucket Bucket name
* @return boolean
*/
public function deleteBucket($bucket = '') {
$delete = new S3Object('DELETE', $bucket);
$delete = $delete->getResponse(true);
if ($delete->code == 204)
{
return true;
}
else
{
$this->err_code = $delete->body->Code;
$this->err_message = $delete->body->Message;
return false;
}
}
/** ***********************************************************************
* Get an object
*
* @param string $bucket Bucket name
* @param string $uri Object URI
* @return mixed
*/
public function getObject($bucket = '', $uri = '') {
$get = new S3Object('GET', $bucket, $uri);
$get = $get->getResponse(true);
if ($get->code == 200)
{
return $get;
}
else
{
$this->err_code = $get->body->Code;
$this->err_message = $get->body->Message;
return false;
}
}
/** ***********************************************************************
* Get an object ACL
*
* @param string $bucket Bucket name
* @param string $uri Object URI
* @return mixed
*/
public function getObjectACL($bucket = '', $uri = '') {
$get = new S3Object('GET', $bucket, $uri."?acl");
$get = $get->getResponse(true);
if ($get->code == 200)
{
return $get;
}
else
{
$this->err_code = $get->body->Code;
$this->err_message = $get->body->Message;
return false;
}
}