__getSignature() public static method

Generate the auth string: "AWS AccessKey:Signature"
public static __getSignature ( string $string ) : string
$string string String to sign
return string
Exemplo n.º 1
0
 /**
  * Get the S3 response
  *
  * @return object | false
  */
 public function getResponse()
 {
     $query = '';
     if (sizeof($this->parameters) > 0) {
         $query = substr($this->uri, -1) !== '?' ? '?' : '&';
         foreach ($this->parameters as $var => $value) {
             if ($value == null || $value == '') {
                 $query .= $var . '&';
             } else {
                 $query .= $var . '=' . rawurlencode($value) . '&';
             }
         }
         $query = substr($query, 0, -1);
         $this->uri .= $query;
         if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('website', $this->parameters) || array_key_exists('logging', $this->parameters)) {
             $this->resource .= $query;
         }
     }
     $url = (S3::$useSSL ? 'https://' : 'http://') . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint) . $this->uri;
     //var_dump('bucket: ' . $this->bucket, 'uri: ' . $this->uri, 'resource: ' . $this->resource, 'url: ' . $url);
     // Basic setup
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
     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);
         if (S3::$sslKey !== null) {
             curl_setopt($curl, CURLOPT_SSLKEY, S3::$sslKey);
         }
         if (S3::$sslCert !== null) {
             curl_setopt($curl, CURLOPT_SSLCERT, S3::$sslCert);
         }
         if (S3::$sslCACert !== null) {
             curl_setopt($curl, CURLOPT_CAINFO, S3::$sslCACert);
         }
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     if (S3::$proxy != null && isset(S3::$proxy['host'])) {
         curl_setopt($curl, CURLOPT_PROXY, S3::$proxy['host']);
         curl_setopt($curl, CURLOPT_PROXYTYPE, S3::$proxy['type']);
         if (isset(S3::$proxy['user'], S3::$proxy['pass']) && S3::$proxy['user'] != null && S3::$proxy['pass'] != null) {
             curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', S3::$proxy['user'], S3::$proxy['pass']));
         }
     }
     // Headers
     $headers = array();
     $amz = array();
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->headers as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     // Collect AMZ headers for signature
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $amz[] = strtolower($header) . ':' . $value;
         }
     }
     // AMZ headers must be sorted
     if (sizeof($amz) > 0) {
         //sort($amz);
         usort($amz, array(&$this, '__sortMetaHeadersCmp'));
         $amz = "\n" . implode("\n", $amz);
     } else {
         $amz = '';
     }
     if (S3::hasAuth()) {
         // Authorization string (CloudFront stringToSign should only contain a date)
         if ($this->headers['Host'] == 'cloudfront.amazonaws.com') {
             $headers[] = 'Authorization: ' . S3::__getSignature($this->headers['Date']);
         } else {
             $headers[] = 'Authorization: ' . S3::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
         }
     }
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
     curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     // Request types
     switch ($this->verb) {
         case 'GET':
             break;
         case 'PUT':
         case 'POST':
             // POST only used for CloudFront
             if ($this->fp !== false) {
                 curl_setopt($curl, CURLOPT_PUT, true);
                 curl_setopt($curl, CURLOPT_INFILE, $this->fp);
                 if ($this->size >= 0) {
                     curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
                 }
             } elseif ($this->data !== false) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
             } else {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
             }
             break;
         case 'HEAD':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             break;
     }
     // Execute, grab errors
     if (curl_exec($curl)) {
         $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     } else {
         $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource);
     }
     @curl_close($curl);
     // Parse body into XML
     if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) {
         $this->response->body = simplexml_load_string($this->response->body);
         // Grab S3 errors
         if (!in_array($this->response->code, array(200, 204, 206)) && isset($this->response->body->Code, $this->response->body->Message)) {
             $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message);
             if (isset($this->response->body->Resource)) {
                 $this->response->error['resource'] = (string) $this->response->body->Resource;
             }
             unset($this->response->body);
         }
     }
     // Clean up file resources
     if ($this->fp !== false && is_resource($this->fp)) {
         fclose($this->fp);
     }
     return $this->response;
 }
Exemplo n.º 2
0
 public function getResponse()
 {
     $query = '';
     if (sizeof($this->parameters) > 0) {
         $query = substr($this->uri, -1) !== '?' ? '?' : '&';
         foreach ($this->parameters as $var => $value) {
             $query .= $value == null || $value == '' ? $var . '&' : $var . '=' . rawurlencode($value) . '&';
         }
         $query = substr($query, 0, -1);
         $this->uri .= $query;
         if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('logging', $this->parameters)) {
             $this->resource .= $query;
         }
     }
     $url = (S3::$use_ssl && extension_loaded('openssl') ? 'https://' : 'http://') . $this->headers['Host'] . $this->uri;
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
     if (S3::$use_ssl) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
         if (S3::$verify_peer) {
             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
         } else {
             curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
         }
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     $headers = $amz = array();
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->headers as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $amz[] = strtolower($header) . ':' . $value;
         }
     }
     if (sizeof($amz) > 0) {
         sort($amz);
         $amz = "\n" . implode("\n", $amz);
     } else {
         $amz = '';
     }
     $headers[] = 'Authorization: ' . S3::__getSignature($this->headers['Host'] == 'cloudfront.amazonaws.com' ? $this->headers['Date'] : $this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
     curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     switch ($this->verb) {
         case 'PUT':
         case 'POST':
             if ($this->fp !== false) {
                 curl_setopt($curl, CURLOPT_PUT, true);
                 curl_setopt($curl, CURLOPT_INFILE, $this->fp);
                 if ($this->size >= 0) {
                     curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
                 }
             } elseif ($this->data !== false) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
                 if ($this->size >= 0) {
                     curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size);
                 }
             } else {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
             }
             break;
         case 'HEAD':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         case 'GET':
         default:
             break;
     }
     if (curl_exec($curl)) {
         $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     } else {
         $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource);
     }
     @curl_close($curl);
     if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) {
         $this->response->body = simplexml_load_string($this->response->body);
         if (!in_array($this->response->code, array(200, 204)) && isset($this->response->body->Code, $this->response->body->Message)) {
             $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message);
             if (isset($this->response->body->Resource)) {
                 $this->response->error['resource'] = (string) $this->response->body->Resource;
             }
             unset($this->response->body);
         }
     }
     if ($this->fp !== false && is_resource($this->fp)) {
         fclose($this->fp);
     }
     return $this->response;
 }
Exemplo n.º 3
0
 /**
  * Get the S3 response
  *
  * @return object | false
  */
 public function getResponse()
 {
     $query = '';
     if (sizeof($this->parameters) > 0) {
         $query = substr($this->uri, -1) !== '?' ? '?' : '&';
         foreach ($this->parameters as $var => $value) {
             if ($value == null || $value == '') {
                 $query .= $var . '&';
             } else {
                 $query .= $var . '=' . rawurlencode($value) . '&';
             }
         }
         $query = substr($query, 0, -1);
         $this->uri .= $query;
         if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters) || array_key_exists('logging', $this->parameters)) {
             $this->resource .= $query;
         }
     }
     $url = (S3::$useSSL && extension_loaded('openssl') ? 'https://' : 'http://') . $this->headers['Host'] . $this->uri;
     //var_dump($this->bucket, $this->uri, $this->resource, $url);
     // Basic setup
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
     if (S3::$useSSL) {
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 1);
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     // Headers
     $headers = array();
     $amz = array();
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->headers as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     // Collect AMZ headers for signature
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $amz[] = strToLower($header) . ':' . $value;
         }
     }
     // AMZ headers must be sorted
     if (sizeof($amz) > 0) {
         sort($amz);
         $amz = "\n" . implode("\n", $amz);
     } else {
         $amz = '';
     }
     // Authorization string (CloudFront stringToSign should only contain a date)
     $headers[] = 'Authorization: ' . S3::__getSignature($this->headers['Host'] == 'cloudfront.amazonaws.com' ? $this->headers['Date'] : $this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
     curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     // Request types
     switch ($this->verb) {
         case 'GET':
             break;
         case 'PUT':
         case 'POST':
             // POST only used for CloudFront
             if ($this->fp !== false) {
                 curl_setopt($curl, CURLOPT_PUT, true);
                 curl_setopt($curl, CURLOPT_INFILE, $this->fp);
                 if ($this->size > 0) {
                     curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
                 }
             } elseif ($this->data !== false) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
                 if ($this->size > 0) {
                     curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size);
                 }
             } else {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $this->verb);
             }
             break;
         case 'HEAD':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             break;
     }
     // Execute, grab errors
     if (curl_exec($curl)) {
         $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     } else {
         $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource);
     }
     @curl_close($curl);
     // Parse body into XML
     if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) {
         $this->response->body = simplexml_load_string($this->response->body);
         // Grab S3 errors
         if (!in_array($this->response->code, array(200, 204)) && isset($this->response->body->Code, $this->response->body->Message)) {
             $this->response->error = array('code' => (string) $this->response->body->Code, 'message' => (string) $this->response->body->Message);
             if (isset($this->response->body->Resource)) {
                 $this->response->error['resource'] = (string) $this->response->body->Resource;
             }
             unset($this->response->body);
         }
     }
     // Clean up file resources
     if ($this->fp !== false && is_resource($this->fp)) {
         fclose($this->fp);
     }
     return $this->response;
 }
Exemplo n.º 4
0
 public function _getResponse(S3 $s3, $url, $headers, $amz)
 {
     $req = new HTTP_Request2($url, $this->verb);
     if (!$s3->useSSLValidation) {
         $req->setConfig('ssl_verify_host', false);
         $req->setConfig('ssl_verify_peer', false);
     }
     if ($s3->proxy != null && isset($s3->proxy['host'])) {
         $req->setConfig('proxy_host');
         $req->setConfig('proxy_host', $s3->proxy['host']);
         if (isset($s3->proxy['user'], $s3->proxy['pass']) && $proxy['user'] != null && $proxy['pass'] != null) {
             $req->setConfig('proxy_user', $s3->proxy['user']);
             $req->setConfig('proxy_password', $s3->proxy['pass']);
         }
     }
     if ($s3->hasAuth()) {
         // Authorization string (CloudFront stringToSign should only contain a date)
         $headers[] = 'Authorization: ' . $s3->__getSignature($this->headers['Host'] == 'cloudfront.amazonaws.com' ? $this->headers['Date'] : $this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
     }
     $req->setHeader($headers);
     $req->setConfig('follow_redirects', true);
     // Request types
     switch ($this->verb) {
         case 'GET':
             break;
         case 'PUT':
         case 'POST':
             // POST only used for CloudFront
             if ($this->fp !== false) {
                 $req->setBody($this->fp);
             } elseif ($this->data !== false) {
                 $req->setBody($this->data);
             }
             break;
     }
     try {
         $res = $req->send();
     } catch (HTTP_Request2_Exception $e) {
         $this->response->error = array('code' => $e->getCode(), 'message' => $e->getMessage(), 'resource' => $this->resource);
     }
     $this->response->code = $res->getStatus();
     $this->response->body = $res->getBody();
     $this->response->headers = $this->modHeaders($res->getHeader());
     return $this->response;
 }
Exemplo n.º 5
0
 /**
  * Get the S3 response
  *
  * @return object | false
  */
 public function getResponse()
 {
     $query = '';
     if (sizeof($this->parameters) > 0) {
         $query = substr($this->uri, -1) !== '?' ? '?' : '&';
         foreach ($this->parameters as $var => $value) {
             if ($value == null || $value == '') {
                 $query .= $var . '&';
             } else {
                 $query .= $var . '=' . $value . '&';
             }
         }
         $query = substr($query, 0, -1);
         $this->uri .= $query;
         if (array_key_exists('acl', $this->parameters) || array_key_exists('location', $this->parameters) || array_key_exists('torrent', $this->parameters)) {
             $this->resource .= $query;
         }
     }
     $url = (extension_loaded('openssl') ? 'https://' : 'http://') . $this->headers['Host'] . $this->uri;
     //var_dump($this->bucket, $this->uri, $this->resource, $url);
     // Basic setup
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
     // Headers
     $headers = array();
     $amz = array();
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->headers as $header => $value) {
         if (strlen($value) > 0) {
             $headers[] = $header . ': ' . $value;
         }
     }
     foreach ($this->amzHeaders as $header => $value) {
         if (strlen($value) > 0) {
             $amz[] = strToLower($header) . ':' . $value;
         }
     }
     // AMZ headers must be sorted (thanks Malone)
     if (sizeof($amz) > 0) {
         sort($amz);
         $amz = "\n" . implode("\n", $amz);
     } else {
         $amz = '';
     }
     // Authorization string
     $headers[] = 'Authorization: ' . S3::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
     curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($curl, CURLOPT_HEADER, false);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
     curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($curl, CURLOPT_TIMEOUT, 1200);
     // 20 minutes
     // Request types
     switch ($this->verb) {
         case 'GET':
             break;
         case 'PUT':
             if ($this->fp !== false) {
                 curl_setopt($curl, CURLOPT_PUT, true);
                 curl_setopt($curl, CURLOPT_INFILE, $this->fp);
                 if ($this->size > 0) {
                     curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
                 }
             } elseif ($this->data !== false) {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
                 curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
                 if ($this->size > 0) {
                     curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size);
                 }
             } else {
                 curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
             }
             break;
         case 'HEAD':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
             curl_setopt($curl, CURLOPT_NOBODY, true);
             break;
         case 'DELETE':
             curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
         default:
             break;
     }
     // Execute, grab errors
     if (curl_exec($curl)) {
         $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
     } else {
         $this->response->error = array('code' => curl_errno($curl), 'message' => curl_error($curl), 'resource' => $this->resource);
     }
     @curl_close($curl);
     // Parse body into XML
     if ($this->response->error === false && isset($this->response->headers['type']) && $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) {
         $dom = new DOMDocument();
         $dom->loadXML($this->response->body);
         $xp = new DOMXPath($dom);
         $codeXp = $xp->query('/Error/Code');
         $messageXp = $xp->query('/Error/Message');
         $resourceXp = $xp->query('/Error/Resource');
         $code = $codeXp->length == 0 ? null : $codeXp->item(0)->nodeValue;
         $message = $messageXp->length == 0 ? null : $messageXp->item(0)->nodeValue;
         $resource = $resourceXp->length == 0 ? null : $resourceXp->item(0)->nodeValue;
         // Grab S3 errors
         if (!in_array($this->response->code, array(200, 204)) && !is_null($code) && !is_null($message)) {
             $this->response->error = array('code' => (string) $code, 'message' => (string) $message, 'resource' => (string) $resource);
             unset($this->response->body);
         }
     }
     // Clean up file resources
     if ($this->fp !== false && is_resource($this->fp)) {
         fclose($this->fp);
     }
     return $this->response;
 }
Exemplo n.º 6
0
	/**
	* Get the S3 response
	*
	* @return object | false
	*/
	public function getResponse() {
		$query = '';
		if (sizeof($this->parameters) > 0) {
			$query = substr($this->uri, -1) !== '?' ? '?' : '&';
			foreach ($this->parameters as $var => $value)
				if ($value == null || $value == '') $query .= $var.'&';
				else $query .= $var.'='.$value.'&';
			$query = substr($query, 0, -1);
			$this->uri .= $query;
			if (isset($this->parameters['acl']) || !isset($this->parameters['logging']))
				$this->resource .= $query;
		}
		$url = (extension_loaded('openssl')?'https://':'http://').$this->headers['Host'].$this->uri;
		//var_dump($this->bucket, $this->uri, $this->resource, $url);

		// Basic setup
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php');
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_URL, $url);

		// Headers
		$headers = array(); $amz = array();
		foreach ($this->amzHeaders as $header => $value)
			if (strlen($value) > 0) $headers[] = $header.': '.$value;
		foreach ($this->headers as $header => $value)
			if (strlen($value) > 0) $headers[] = $header.': '.$value;
		foreach ($this->amzHeaders as $header => $value)
			if (strlen($value) > 0) $amz[] = strToLower($header).':'.$value;
		$amz = (sizeof($amz) > 0) ? "\n".implode("\n", $amz) : '';

		// Authorization string
		$headers[] = 'Authorization: ' . S3::__getSignature(
			$this->verb."\n".
			$this->headers['Content-MD5']."\n".
			$this->headers['Content-Type']."\n".
			$this->headers['Date'].$amz."\n".$this->resource
		);

		curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($curl, CURLOPT_HEADER, false);
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, false);
		curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback'));
		curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback'));

		// Request types
		switch ($this->verb) {
			case 'GET': break;
			case 'PUT':
				if ($this->fp !== false) {
					curl_setopt($curl, CURLOPT_PUT, true);
					curl_setopt($curl, CURLOPT_INFILE, $this->fp);
					if ($this->size > 0)
						curl_setopt($curl, CURLOPT_INFILESIZE, $this->size);
				} elseif ($this->data !== false) {
					curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
					curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data);
					if ($this->size > 0)
						curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size);
				} else
					curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
			break;
			case 'HEAD':
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
				curl_setopt($curl, CURLOPT_NOBODY, true);
			break;
			case 'DELETE':
				curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
			break;
			default: break;
		}

		// Execute, grab errors
		if (curl_exec($curl))
			$this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
		else
			$this->response->error = array(
				'code' => curl_errno($curl),
				'message' => curl_error($curl),
				'resource' => $this->resource
			);

		@curl_close($curl);

		// Parse body into XML
		if ($this->response->error === false && isset($this->response->headers['type']) &&
		$this->response->headers['type'] == 'application/xml' && isset($this->response->body)) {
			$this->response->body = simplexml_load_string($this->response->body);

			// Grab S3 errors
			if (!in_array($this->response->code, array(200, 204)) &&
			isset($this->response->body->Code, $this->response->body->Message)) {
				$this->response->error = array(
					'code' => (string)$this->response->body->Code,
					'message' => (string)$this->response->body->Message
				);
				if (isset($this->response->body->Resource))
					$this->response->error['resource'] = (string)$this->response->body->Resource;
				unset($this->response->body);
			}
		}

		// Clean up file resources
		if ($this->fp !== false && is_resource($this->fp)) fclose($this->fp);

		return $this->response;
	}