Example #1
0
 /**
  * Get the SCS 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;
         }
         */
         $single_filter_list = array('acl', 'location', 'torrent', 'website', 'logging', 'relax', 'meta', 'uploads', 'part', 'copy', 'multipart');
         $double_filter_list = array('uploadId', 'ip', 'partNumber');
         foreach ($this->parameters as $var => $value) {
             if (in_array($var, $single_filter_list)) {
                 $this->resource .= '?' . $var;
                 break;
             }
         }
         $query_for_sign_list = array();
         foreach ($this->parameters as $var => $value) {
             if (in_array($var, $double_filter_list)) {
                 $query_for_sign_list[$var] = $value;
             }
         }
         if (count($query_for_sign_list) > 0) {
             ksort($query_for_sign_list);
             $query_for_sign = '';
             foreach ($query_for_sign_list as $key => $value) {
                 $query_for_sign .= $key . '=' . rawurlencode($value) . '&';
             }
             $query_for_sign = substr($query_for_sign, 0, -1);
             if ($query_for_sign) {
                 $this->resource .= (strpos($this->resource, '?') === false ? '?' : '&') . $query_for_sign;
             }
         }
     }
     $url = (SCS::$useSSL ? 'https://' : 'http://') . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint) . $this->uri;
     /* @TODO delete */
     //$url = (SCS::$useSSL ? 'https://' : 'http://') . '58.63.236.206' . $this->uri;
     //var_dump('bucket: ' . $this->bucket, 'uri: ' . $this->uri, 'resource: ' . $this->resource, 'url: ' . $url);
     // Basic setup
     $curl = curl_init();
     if (defined("DEBUG")) {
         curl_setopt($curl, CURLOPT_VERBOSE, 1);
     }
     curl_setopt($curl, CURLOPT_USERAGENT, 'SCS/console');
     if (SCS::$useSSL) {
         // SSL Validation can now be optional for those with broken OpenSSL installations
         curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, SCS::$useSSLValidation ? 2 : 0);
         curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, SCS::$useSSLValidation ? 1 : 0);
         if (SCS::$sslKey !== null) {
             curl_setopt($curl, CURLOPT_SSLKEY, SCS::$sslKey);
         }
         if (SCS::$sslCert !== null) {
             curl_setopt($curl, CURLOPT_SSLCERT, SCS::$sslCert);
         }
         if (SCS::$sslCACert !== null) {
             curl_setopt($curl, CURLOPT_CAINFO, SCS::$sslCACert);
         }
     }
     curl_setopt($curl, CURLOPT_URL, $url);
     if (SCS::$proxy != null && isset(SCS::$proxy['host'])) {
         curl_setopt($curl, CURLOPT_PROXY, SCS::$proxy['host']);
         curl_setopt($curl, CURLOPT_PROXYTYPE, SCS::$proxy['type']);
         if (isset(SCS::$proxy['user'], SCS::$proxy['pass']) && SCS::$proxy['user'] != null && SCS::$proxy['pass'] != null) {
             curl_setopt($curl, CURLOPT_PROXYUSERPWD, sprintf('%s:%s', SCS::$proxy['user'], SCS::$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 (SCS::hasAuth()) {
         // Authorization string (CloudFront stringToSign should only contain a date)
         if ($this->headers['Host'] == 'cloudfront.amazonaws.com') {
             $headers[] = 'Authorization: ' . SCS::__getSignature($this->headers['Date']);
         } else {
             if (isset($this->headers['s-sina-sha1'])) {
                 $this->headers['Content-MD5'] = $this->headers['s-sina-sha1'];
             }
             $headers[] = 'Authorization: ' . SCS::__getSignature($this->verb . "\n" . $this->headers['Content-MD5'] . "\n" . $this->headers['Content-Type'] . "\n" . $this->headers['Date'] . $amz . "\n" . $this->resource);
         }
     }
     /* @todo delete */
     //$headers[] = 'Host: ' . ($this->headers['Host'] !== '' ? $this->headers['Host'] : $this->endpoint);
     //print_r($headers);
     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_CONNECTTIMEOUT, 60);
     		curl_setopt($curl, CURLOPT_TIMEOUT, 1200);
     		*/
     // 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);
     }
     //echo $this->response->body;
     @curl_close($curl);
     // Parse body into XML | JSON
     if ($this->response->error === false && isset($this->response->headers['type']) && ($this->response->headers['type'] == 'application/xml' || $this->response->headers['type'] == 'application/json') && isset($this->response->body)) {
         if ($this->response->headers['type'] == 'application/json') {
             $this->response->body = json_decode($this->response->body);
         } else {
             $this->response->body = simplexml_load_string($this->response->body);
         }
         // Grab SCS 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;
 }