/**
  * Builds the request string.
  * 
  * The files array can be a combination of the following (either data or file):
  * 
  * file => "path/to/file", filename=, mime=, data=
  *
  * @param array params		(name => value) (all names and values should be urlencoded)
  * @param array files		(name => filedesc) (not urlencoded)
  * @return array (headers, body)
  */
 static function encodeBody($params, $files)
 {
     $headers = array();
     $body = '';
     $boundary = 'OAuthRequester_' . md5(uniqid('multipart') . microtime());
     $headers['Content-Type'] = 'multipart/form-data; boundary=' . $boundary;
     // 1. Add the parameters to the post
     if (!empty($params)) {
         foreach ($params as $name => $value) {
             $body .= '--' . $boundary . "\r\n";
             $body .= 'Content-Disposition: form-data; name="' . OAuthMultipartFormdata::encodeParameterName(rawurldecode($name)) . '"';
             $body .= "\r\n\r\n";
             $body .= urldecode($value);
             $body .= "\r\n";
         }
     }
     // 2. Add all the files to the post
     if (!empty($files)) {
         $untitled = 1;
         foreach ($files as $name => $f) {
             $data = false;
             $filename = false;
             if (isset($f['filename'])) {
                 $filename = $f['filename'];
             }
             if (!empty($f['file'])) {
                 $data = @file_get_contents($f['file']);
                 if ($data === false) {
                     throw new OAuthException(sprintf('Could not read the file "%s" for form-data part', $f['file']));
                 }
                 if (empty($filename)) {
                     $filename = basename($f['file']);
                 }
             } else {
                 if (isset($f['data'])) {
                     $data = $f['data'];
                 }
             }
             // When there is data, add it as a form-data part, otherwise silently skip the upload
             if ($data !== false) {
                 if (empty($filename)) {
                     $filename = sprintf('untitled-%d', $untitled++);
                 }
                 $mime = !empty($f['mime']) ? $f['mime'] : 'application/octet-stream';
                 $body .= '--' . $boundary . "\r\n";
                 $body .= 'Content-Disposition: form-data; name="' . OAuthMultipartFormdata::encodeParameterName($name) . '"; filename="' . OAuthMultipartFormdata::encodeParameterName($filename) . '"' . "\r\n";
                 $body .= 'Content-Type: ' . $mime;
                 $body .= "\r\n\r\n";
                 $body .= $data;
                 $body .= "\r\n";
             }
         }
     }
     $body .= '--' . $boundary . "--\r\n";
     $headers['Content-Length'] = strlen($body);
     return array($headers, $body);
 }
Example #2
0
 /**
  * Perform the request, returns the response code, headers and body.
  * 
  * @param int usr_id		optional user id for which we make the request
  * @param array curl_options
  * @exception OAuthException when authentication not accepted
  * @exception OAuthException when signing was not possible
  * @return array (code=>int, headers=>array(), body=>string)
  */
 function doRequest($usr_id = 0, $curl_options = array())
 {
     if (!empty($this->files)) {
         list($extra_headers, $body) = OAuthMultipartFormdata::encodeBody($this->param, $this->files);
         $this->setBody($body);
         $this->param = array();
         $curl_options = $this->prepareCurlOptions($curl_options, $extra_headers);
     }
     $this->sign($usr_id);
     $text = $this->curl_raw($curl_options);
     return $this->curl_parse($text);
 }