コード例 #1
0
ファイル: S3.php プロジェクト: NareshChennuri/pyng
 /**
  * Set access information
  *
  * @param string $accessKey Access key
  * @param string $secretKey Secret key
  * @return void
  */
 public static function setAuth($accessKey, $secretKey)
 {
     self::$__accessKey = $accessKey;
     self::$__secretKey = $secretKey;
 }
コード例 #2
0
ファイル: S3request.php プロジェクト: NareshChennuri/pyng
 /**
  * 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: ' . Helper_Amazon_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;
 }