/**
  * Open and close a curl session passing all the options to the curl libs
  * 
  * @param array opts the curl options.
  * @exception OAuthException2 when temporary file for PUT operation could not be created
  * @return string the result of the curl action
  */
 protected function curl_raw($opts = array())
 {
     if (isset($opts[CURLOPT_HTTPHEADER])) {
         $header = $opts[CURLOPT_HTTPHEADER];
     } else {
         $header = array();
     }
     $ch = curl_init();
     $method = $this->getMethod();
     $url = $this->getRequestUrl();
     $header[] = $this->getAuthorizationHeader();
     $query = $this->getQueryString();
     $body = $this->getBody();
     $has_content_type = false;
     foreach ($header as $h) {
         if (strncasecmp($h, 'Content-Type:', 13) == 0) {
             $has_content_type = true;
         }
     }
     if (!is_null($body)) {
         if ($method == 'TRACE') {
             throw new OAuthException2('A body can not be sent with a TRACE operation');
         }
         // PUT and POST allow a request body
         if (!empty($query)) {
             $url .= '?' . $query;
         }
         // Make sure that the content type of the request is ok
         if (!$has_content_type) {
             $header[] = 'Content-Type: application/octet-stream';
             $has_content_type = true;
         }
         // When PUTting, we need to use an intermediate file (because of the curl implementation)
         if ($method == 'PUT') {
             /*
             if (version_compare(phpversion(), '5.2.0') >= 0)
             {
             	// Use the data wrapper to create the file expected by the put method
             	$put_file = fopen('data://application/octet-stream;base64,'.base64_encode($body));
             }
             */
             $put_file = @tmpfile();
             if (!$put_file) {
                 throw new OAuthException2('Could not create tmpfile for PUT operation');
             }
             fwrite($put_file, $body);
             fseek($put_file, 0);
             curl_setopt($ch, CURLOPT_PUT, true);
             curl_setopt($ch, CURLOPT_INFILE, $put_file);
             curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
         } else {
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
         }
     } else {
         // a 'normal' request, no body to be send
         if ($method == 'POST') {
             if (!$has_content_type) {
                 $header[] = 'Content-Type: application/x-www-form-urlencoded; charset=utf-8';
                 $has_content_type = true;
             }
             curl_setopt($ch, CURLOPT_POST, true);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
         } else {
             if (!empty($query)) {
                 $url .= '?' . $query;
             }
             if ($method != 'GET') {
                 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
             }
         }
     }
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
     curl_setopt($ch, CURLOPT_USERAGENT, 'anyMeta/OAuth 1.0 - ($LastChangedRevision: 174 $)');
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 90);
     foreach ($opts as $k => $v) {
         if ($k != CURLOPT_HTTPHEADER) {
             curl_setopt($ch, $k, $v);
         }
     }
     $txt = curl_exec($ch);
     if ($txt === false) {
         $error = curl_error($ch);
         curl_close($ch);
         throw new OAuthException2('CURL error: ' . $error);
     }
     curl_close($ch);
     if (!empty($put_file)) {
         fclose($put_file);
     }
     // Tell the logger what we requested and what we received back
     $data = $method . " {$url}\n" . implode("\n", $header);
     if (is_string($body)) {
         $data .= "\n\n" . $body;
     } else {
         if ($method == 'POST') {
             $data .= "\n\n" . $query;
         }
     }
     LingotekOAuthRequestLogger::setSent($data, $body);
     LingotekOAuthRequestLogger::setReceived($txt);
     return $txt;
 }
예제 #2
0
 /**
  * Try to fetch an XRDS file at the given location.  Sends an accept header preferring the xrds file.
  * 
  * @param string uri
  * @return array	(head,body), false on an error
  */
 protected static function curl($uri)
 {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/xrds+xml, */*;q=0.1'));
     curl_setopt($ch, CURLOPT_USERAGENT, 'anyMeta/OAuth 1.0 - (OAuth Discovery $LastChangedRevision: 45 $)');
     curl_setopt($ch, CURLOPT_URL, $uri);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_HEADER, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, 30);
     $txt = curl_exec($ch);
     curl_close($ch);
     // Tell the logger what we requested and what we received back
     $data = "GET {$uri}";
     LingotekOAuthRequestLogger::setSent($data, "");
     LingotekOAuthRequestLogger::setReceived($txt);
     return $txt;
 }