예제 #1
0
파일: S3.php 프로젝트: comdan66/TaipeiTowns
 public static function getBucketLocation($bucket)
 {
     $rest = new S3Request('GET', $bucket, '');
     $rest->setParameter('location', null);
     $rest = $rest->getResponse();
     if ($rest->error !== false || $rest->code !== 200) {
         throw new Exception(sprintf("S3::getBucketLocation(%s): [%s] %s", $bucket, $rest->code, 'Unexpected HTTP status'));
     }
     return isset($rest->body[0]) && (string) $rest->body[0] !== '' ? (string) $rest->body[0] : 'US';
 }
예제 #2
0
 /**
  * Delete a CloudFront distribution
  *
  * @param array $dist Distribution array info identical to output of getDistribution()
  * @return boolean
  */
 public static function deleteDistribution($dist)
 {
     if (!extension_loaded('openssl')) {
         self::__triggerError(sprintf("S3::deleteDistribution({$dist['id']}): %s", "CloudFront functionality requires SSL"), __FILE__, __LINE__);
         return false;
     }
     $useSSL = self::$useSSL;
     self::$useSSL = true;
     // CloudFront requires SSL
     $rest = new S3Request('DELETE', '', '2008-06-30/distribution/' . $dist['id'], 'cloudfront.amazonaws.com');
     $rest->setHeader('If-Match', $dist['hash']);
     $rest = self::__getCloudFrontResponse($rest);
     self::$useSSL = $useSSL;
     if ($rest->error === false && $rest->code !== 204) {
         $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
     }
     if ($rest->error !== false) {
         self::__triggerError(sprintf("S3::deleteDistribution({$dist['id']}): [%s] %s", $rest->error['code'], $rest->error['message']), __FILE__, __LINE__);
         return false;
     }
     return true;
 }
예제 #3
0
 /**
  * Invalidates files in a CloudFront distribution
  *
  * @param string $distributionId Distribution ID from listDistributions()
  * @param string $path Path to file to be invalidated
  * @return boolean
  */
 public static function invalidate($distributionId, $path)
 {
     self::$useSSL = true;
     // CloudFront requires SSL
     $rest = new S3Request('POST', '', '2010-08-01/distribution/' . $distributionId . '/invalidation', 'cloudfront.amazonaws.com');
     $rest->data = self::__getCloudFrontInvalidationBatchXML($path);
     $rest->size = strlen($rest->data);
     $rest->setHeader('Content-Type', 'text/xml');
     $rest = self::__getCloudFrontResponse($rest);
     if ($rest->error === false && $rest->code !== 201) {
         $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
     }
     if ($rest->error !== false) {
         trigger_error(sprintf("S3::invalidate(): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING);
         return false;
     }
     return true;
 }
예제 #4
0
파일: S3.php 프로젝트: rczamor/Activ8
 /**
  * Delete a CloudFront distribution
  *
  * @param array $dist Distribution array info identical to output of getDistribution()
  * @return boolean
  */
 public static function deleteDistribution($dist)
 {
     self::$useSSL = true;
     // CloudFront requires SSL
     $rest = new S3Request('DELETE', '', '2008-06-30/distribution/' . $dist['id'], 'cloudfront.amazonaws.com');
     $rest->setHeader('If-Match', $dist['hash']);
     $rest = self::__getCloudFrontResponse($rest);
     if ($rest->error === false && $rest->code !== 204) {
         $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
     }
     if ($rest->error !== false) {
         trigger_error(sprintf("S3::deleteDistribution({$dist['id']}): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING);
         return false;
     }
     return true;
 }
예제 #5
0
 /**
  * Creates invalidation bath
  *
  * @static
  * @param integer $distributionId
  * @param array $paths
  * @return array|bool
  */
 public static function createInvalidation($distributionId, $paths)
 {
     self::$use_ssl = true;
     // CloudFront requires SSL
     $rest = new S3Request('POST', '', '2010-11-01/distribution/' . $distributionId . '/invalidation', 'cloudfront.amazonaws.com');
     $rest->data = self::__getCloudFrontInvalidationBath($paths);
     $rest->size = strlen($rest->data);
     $rest->setHeader('Content-Type', 'application/xml');
     $rest = self::__getCloudFrontResponse($rest);
     if ($rest->error === false && $rest->code !== 201) {
         $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
     }
     if ($rest->error !== false) {
         trigger_error(sprintf("S3::createInvalidation(%d, '%s'): [%s] %s", $distributionId, implode(', ', $paths), $rest->error['code'], $rest->error['message']), E_USER_WARNING);
         return false;
     } elseif ($rest->body instanceof SimpleXMLElement) {
         return self::__parseCloudFrontInvalidation($rest->body);
     }
     return false;
 }
예제 #6
0
 /**
  * Get an object
  *
  * @param string $bucket Bucket name
  * @param string $uri Object URI
  * @param mixed &$saveTo Filename or resource to write to
  * @return mixed
  */
 public static function getObject($bucket = '', $uri = '', $saveTo = false)
 {
     $rest = new S3Request('GET', $bucket, $uri);
     if ($saveTo !== false) {
         if (is_resource($saveTo)) {
             $rest->fp =& $saveTo;
         } else {
             if (($rest->fp = @fopen($saveTo, 'wb')) == false) {
                 $rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: ' . $saveTo);
             }
         }
     }
     if ($rest->response->error === false) {
         $rest->getResponse();
     }
     if ($rest->response->error === false && $rest->response->code !== 200) {
         $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status');
     }
     if ($rest->response->error !== false) {
         trigger_error(sprintf("S3::getObject({$bucket}, {$uri}): [%s] %s", $rest->response->error['code'], $rest->response->error['message']), E_USER_WARNING);
         return false;
     }
     $rest->file = realpath($saveTo);
     return $rest->response;
 }
예제 #7
0
 /**
  * Get object or bucket Access Control Policy
  *
  * Currently this will trigger an error if there is no ACL on an object (will fix soon)
  *
  * @param string $bucket Bucket name
  * @param string $uri Object URI
  * @return mixed | false
  */
 public static function getAccessControlPolicy($bucket, $uri = '')
 {
     $rest = new S3Request('GET', $bucket, $uri);
     $rest->setParameter('acl', null);
     $rest = $rest->getResponse();
     if ($rest->error === false && $rest->code !== 200) {
         $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
     }
     if ($rest->error !== false) {
         trigger_error(sprintf("S3::getAccessControlPolicy({$bucket}, {$uri}): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING);
         return false;
     }
     $acp = array();
     if (isset($rest->body->Owner, $rest->body->Owner->ID, $rest->body->Owner->DisplayName)) {
         $acp['owner'] = array('id' => (string) $rest->body->Owner->ID, 'name' => (string) $rest->body->Owner->DisplayName);
     }
     if (isset($rest->body->AccessControlList)) {
         $acp['acl'] = array();
         foreach ($rest->body->AccessControlList->Grant as $grant) {
             foreach ($grant->Grantee as $grantee) {
                 if (isset($grantee->ID, $grantee->DisplayName)) {
                     // CanonicalUser
                     $acp['acl'][] = array('type' => 'CanonicalUser', 'id' => (string) $grantee->ID, 'name' => (string) $grantee->DisplayName, 'permission' => (string) $grant->Permission);
                 } elseif (isset($grantee->EmailAddress)) {
                     // AmazonCustomerByEmail
                     $acp['acl'][] = array('type' => 'AmazonCustomerByEmail', 'email' => (string) $grantee->EmailAddress, 'permission' => (string) $grant->Permission);
                 } elseif (isset($grantee->URI)) {
                     // Group
                     $acp['acl'][] = array('type' => 'Group', 'uri' => (string) $grantee->URI, 'permission' => (string) $grant->Permission);
                 } else {
                     continue;
                 }
             }
         }
     }
     return $acp;
 }
예제 #8
0
파일: s3.php 프로젝트: nikels/HeavyMetal
	/**
	* Copy an object
	*
	* @param string $bucket Source bucket name
	* @param string $uri Source object URI
	* @param string $bucket Destination bucket name
	* @param string $uri Destination object URI
	* @param constant $acl ACL constant
	* @return mixed | false
	*/
	public function copy($srcBucket, $srcUri, $bucket, $uri, $acl = StorageManager::ACL_PRIVATE) 
	{
		$rest = new S3Request($this->id,$this->secret,$this->useSSL,'PUT', $bucket, $uri);
		$rest->setAmzHeader('x-amz-acl', $acl);
		$rest->setAmzHeader('x-amz-copy-source', sprintf('/%s/%s', $srcBucket, $srcUri));
		$rest = $rest->getResponse();
	
		if ($rest->error === false && $rest->code !== 200)
			$rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status');
	
		if ($rest->error !== false) 
			throw new AWSException(sprintf("S3::copyObject({$srcBucket}, {$srcUri}, {$bucket}, {$uri}): [%s] %s", $rest->error['code'], $rest->error['message']));
		
		return isset($rest->body->LastModified, $rest->body->ETag) ? array(
			'time' => strtotime((string)$rest->body->LastModified),
			'hash' => substr((string)$rest->body->ETag, 1, -1)
		) : false;
	}
예제 #9
0
 private function amazon_upload_file($data)
 {
     $response =& $this->request->post['last'];
     $s3 =& $data['s3'];
     $buckets = $s3->listBuckets();
     $cssjs_browser_cache = 0;
     $images_browser_cache = 0;
     if (getNitroPersistence('BrowserCache.Enabled') && getNitroPersistence('BrowserCache.Headers.Pages.Expires')) {
         if (getNitroPersistence('BrowserCache.CSSJS.Period')) {
             switch (getNitroPersistence('BrowserCache.CSSJS.Period')) {
                 case '1 week':
                     $cssjs_browser_cache = 7 * 24 * 3600;
                     break;
                 case '1 month':
                     $cssjs_browser_cache = 30 * 24 * 3600;
                     break;
                 case '6 months':
                     $cssjs_browser_cache = 6 * 30 * 24 * 3600;
                     break;
                 case '1 year':
                     $cssjs_browser_cache = 365 * 24 * 3600;
                     break;
                 default:
                     $cssjs_browser_cache = 0;
             }
         }
         if (getNitroPersistence('BrowserCache.Images.Period')) {
             switch (getNitroPersistence('BrowserCache.Images.Period')) {
                 case '1 week':
                     $images_browser_cache = 7 * 24 * 3600;
                     break;
                 case '1 month':
                     $images_browser_cache = 30 * 24 * 3600;
                     break;
                 case '6 months':
                     $images_browser_cache = 6 * 30 * 24 * 3600;
                     break;
                 case '1 year':
                     $images_browser_cache = 365 * 24 * 3600;
                     break;
                 default:
                     $images_browser_cache = 0;
             }
         }
     }
     $source = $data['file']['realpath'];
     $destination = $data['file']['file'];
     $ext = strtolower(pathinfo($source, PATHINFO_EXTENSION));
     if (getNitroPersistence('CDNAmazon.SyncCSS') && in_array($ext, unserialize(NITRO_EXTENSIONS_CSS))) {
         $bucket = getNitroPersistence('CDNAmazon.CSSBucket');
         if (is_array($buckets) && in_array($bucket, $buckets)) {
             $compress = getNitroPersistence('Compress.CSS') && getNitroPersistence('Compress.CSSLevel') ? (int) getNitroPersistence('Compress.CSSLevel') : false;
             $cache_time = $cssjs_browser_cache;
         } else {
             throw new Exception('The CSS bucket does not exist. Please create it.');
         }
     }
     if (getNitroPersistence('CDNAmazon.SyncJavaScript') && in_array($ext, unserialize(NITRO_EXTENSIONS_JS))) {
         $bucket = getNitroPersistence('CDNAmazon.JavaScriptBucket');
         if (is_array($buckets) && in_array($bucket, $buckets)) {
             $compress = getNitroPersistence('Compress.JS') && getNitroPersistence('Compress.JSLevel') ? (int) getNitroPersistence('Compress.JSLevel') : false;
             $cache_time = $cssjs_browser_cache;
         } else {
             throw new Exception('The JS bucket does not exist. Please create it.');
         }
     }
     if (getNitroPersistence('CDNAmazon.SyncImages') && in_array($ext, unserialize(NITRO_EXTENSIONS_IMG))) {
         $bucket = getNitroPersistence('CDNAmazon.ImageBucket');
         if (is_array($buckets) && in_array($bucket, $buckets)) {
             $compress = false;
             $cache_time = $images_browser_cache;
         } else {
             throw new Exception('The images bucket does not exist. Please create it.');
         }
     }
     if (empty($bucket)) {
         return;
     }
     $req = new S3Request('HEAD', $bucket, $destination);
     $res = $req->getResponse();
     $to_upload = $res->code != 200 || $res->code == 200 && ((!empty($compress) xor !empty($res->headers['x-amz-meta-compressed'])) || (!empty($cache_time) xor !empty($res->headers['x-amz-meta-expires-time'])) || !empty($cache_time) && !empty($res->headers['x-amz-meta-expires-time']) && (int) $cache_time != $res->headers['x-amz-meta-expires-time']);
     if ($to_upload) {
         $headers = array();
         $meta_headers = array();
         if (!empty($cache_time)) {
             $headers['Expires'] = gmdate('D, d M Y H:i:s \\G\\M\\T', time() + $cache_time);
             $meta_headers['expires-time'] = $cache_time;
         }
         switch ($ext) {
             case 'css':
                 $headers['Content-Type'] = 'text/css';
                 break;
             case 'js':
                 $headers['Content-Type'] = 'text/javascript';
                 break;
         }
         if (!empty($compress) && is_readable($source) && !empty($headers['Content-Type'])) {
             $headers['Content-Encoding'] = 'gzip';
             $meta_headers['compressed'] = '1';
             $contents = gzencode(file_get_contents($source), $compress);
             $temp_file = tempnam(sys_get_temp_dir(), 'Nitro');
             $temp_handle = fopen($temp_file, "w");
             if (empty($temp_handle) || !fwrite($temp_handle, $contents)) {
                 throw new Exception('There was a problem writing to ' . $temp_file);
             }
             fclose($temp_handle);
             $source = $temp_file;
         }
         if ($s3->putObject($s3->inputFile($source), $bucket, $destination, S3::ACL_PUBLIC_READ, $meta_headers, $headers)) {
             if (!empty($temp_file)) {
                 unlink($temp_file);
                 unset($temp_file);
             }
         } else {
             throw new Exception('Could not upload ' . $destination);
         }
     }
 }