Advertisement
Guest User

Untitled

a guest
Oct 13th, 2015
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.00 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * ES3 class file.
  5.  *
  6.  * ES3 is a wrapper for the excellent S3.php class provided by Donovan Sch�nknecht (@link http://undesigned.org.za/2007/10/22/amazon-s3-php-class)
  7.  * This wrapper contains minimal functionality as there is only so much I want to allow access to from the Yii public end
  8.  *
  9.  * @version 0.1
  10.  *
  11.  * @uses CFile
  12.  * @author Dana Luther (dana.luther@gmail.com)
  13.  * @copyright Copyright &copy; 2010 Dana Luther
  14.  */
  15.  class ES3 extends CApplicationComponent
  16. {
  17.  
  18.     private $_s3;
  19.     public $aKey; // AWS Access key
  20.     public $sKey; // AWS Secret key
  21.     public $bucket;
  22.     public $lastError='';
  23.  
  24.     public $cacheControl = 'max-age=2592000, public'; //30 dias + publico
  25.  
  26.     private function getInstance(){
  27.         if ($this->_s3 === NULL)
  28.             $this->connect();
  29.         return $this->_s3;
  30.     }
  31.  
  32.     /**
  33.      * Instance the S3 object
  34.      */
  35.     public function connect()
  36.     {
  37.         if ( $this->aKey === NULL || $this->sKey === NULL )
  38.             throw new CException('S3 Keys are not set.');
  39.  
  40.         $this->_s3 = new S3($this->aKey,$this->sKey);
  41.     }
  42.  
  43.     /**
  44.      * @param string $original File to upload - can be any valid CFile filename
  45.      * @param string $uploaded Name of the file on destination -- can include directory separators
  46.      */
  47.     public function upload( $original, $uploaded='', $contentType=null, $bucket='' )
  48.     {
  49.         $s3 = $this->getInstance();
  50.  
  51.         if( $bucket == '' )
  52.         {
  53.             $bucket = $this->bucket;
  54.         }
  55.  
  56.         if ($bucket === NULL || trim($bucket) == '')
  57.         {
  58.             throw new CException('Bucket param cannot be empty');
  59.         }
  60.  
  61.         $file = Yii::app()->file->set($original);
  62.  
  63.         if(!$file->exists)
  64.             throw new CException('Origin file not found');
  65.  
  66.         $fs1 = $file->size;
  67.  
  68.         if ( !$fs1 )
  69.         {
  70.             $this->lastError = "Attempted to upload empty file.";
  71.             return false;
  72.         }
  73.  
  74.         if (trim($uploaded) == ''){
  75.             $uploaded = $original;
  76.         }
  77.  
  78.         $requestHeaders = array();
  79.  
  80.         if(is_string($contentType))
  81.             $requestHeaders['Content-Type'] = $contentType;
  82.  
  83.         if($this->cacheControl)
  84.             $requestHeaders['Cache-Control'] = $this->cacheControl;
  85.  
  86.         if(is_array($contentType))
  87.             $requestHeaders = array_merge($requestHeaders, $contentType);
  88.  
  89.         //if (!$s3->putObject($s3->inputResource(fopen($file->getRealPath(), 'r'), $fs1), $bucket, $uploaded, S3::ACL_PUBLIC_READ))
  90.         //echo $file->getRealPath();
  91.         //if (!$s3->putObject($s3->inputResource( fopen($file->getRealPath(), 'rb'), $fs1), $bucket, $uploaded, S3::ACL_PUBLIC_READ))
  92.         if (!$s3->putObjectFile( $original, $bucket, $uploaded, S3::ACL_PUBLIC_READ, array(), $requestHeaders))
  93.         {
  94.             $this->lastError = "Unable to upload file.";
  95.             return false;
  96.         }
  97.         return true;
  98.     }
  99.  
  100.     // Testing connection :p
  101.     public function buckets()
  102.     {
  103.         $s3 = $this->getInstance();
  104.         return $this->_s3->listBuckets();
  105.     }
  106.  
  107.     // Passthru function for basic functions
  108.     public function call( $func )
  109.     {
  110.         $s3 = $this->getInstance();
  111.         return $s3->$func();
  112.     }
  113.  
  114.     public function createUrl( $uri, $bucket=null ) {
  115.         if(!$bucket) {
  116.             $bucket = $this->bucket;
  117.         }
  118.        
  119.         $schema = Yii::app()->request->getIsSecureConnection() ? 'https' : 'http';
  120.  
  121.         return "{$schema}://s3-eu-west-1.amazonaws.com/{$bucket}/{$uri}";
  122.     }
  123.  
  124.     public function delete( $uri, $bucket='' ) {
  125.         if( $bucket == '' ) {
  126.             $bucket = $this->bucket;
  127.         }
  128.  
  129.         $s3 = $this->getInstance();
  130.  
  131.         if(!$s3->deleteObject( $bucket, $uri )) {
  132.             $this->lastError = "Unable do delete object '$uri'";
  133.             return false;
  134.         }
  135.  
  136.         return true;
  137.     }
  138.  
  139.     public function copy($srcUri, $destUri, $metaHeaders = array(), $requestHeaders = array(), $acl = S3::ACL_PRIVATE, $srcBucket='', $destBucket='') {
  140.         if( $srcBucket == '' )
  141.             $srcBucket = $this->bucket;
  142.         if( $destBucket == '' )
  143.             $destBucket = $this->bucket;
  144.  
  145.         $s3 = $this->getInstance();
  146.  
  147.         return $s3->copyObject($srcBucket, $srcUri, $destBucket, $destUri, $acl, $metaHeaders, $requestHeaders);
  148.         //return $s3->copyObject($srcBucket, $srcUri, $destBucket, $destUri) !== false;
  149.     }
  150.  
  151.  
  152.     public function contents() {
  153.         $s3 = $this->getInstance();
  154.  
  155.         return $s3->getBucket($this->bucket);
  156.     }
  157. }
  158. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement