Edit File by line
/home/barbar84/www/wp-conte.../plugins/updraftp.../includes/Dropbox2/OAuth/Consumer
File: Curl.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/**
[2] Fix | Delete
* OAuth consumer using PHP cURL
[3] Fix | Delete
* @author Ben Tadiar <ben@handcraftedbyben.co.uk>
[4] Fix | Delete
* @link https://github.com/benthedesigner/dropbox
[5] Fix | Delete
* @package Dropbox\OAuth
[6] Fix | Delete
* @subpackage Consumer
[7] Fix | Delete
*/
[8] Fix | Delete
[9] Fix | Delete
class Dropbox_Curl extends Dropbox_ConsumerAbstract
[10] Fix | Delete
{
[11] Fix | Delete
/**
[12] Fix | Delete
* Default cURL options
[13] Fix | Delete
* @var array
[14] Fix | Delete
*/
[15] Fix | Delete
protected $defaultOptions = array(
[16] Fix | Delete
CURLOPT_VERBOSE => true,
[17] Fix | Delete
CURLOPT_HEADER => true,
[18] Fix | Delete
CURLINFO_HEADER_OUT => false,
[19] Fix | Delete
CURLOPT_RETURNTRANSFER => true,
[20] Fix | Delete
CURLOPT_FOLLOWLOCATION => false,
[21] Fix | Delete
);
[22] Fix | Delete
[23] Fix | Delete
/**
[24] Fix | Delete
* Store the last response form the API
[25] Fix | Delete
* @var mixed
[26] Fix | Delete
*/
[27] Fix | Delete
protected $lastResponse = null;
[28] Fix | Delete
[29] Fix | Delete
/**
[30] Fix | Delete
* Set properties and begin authentication
[31] Fix | Delete
* @param string $key
[32] Fix | Delete
* @param string $secret
[33] Fix | Delete
* @param \Dropbox\OAuth\Consumer\StorageInterface $storage
[34] Fix | Delete
* @param string $callback
[35] Fix | Delete
*/
[36] Fix | Delete
public function __construct($key, $oauth2_id, $secret, Dropbox_StorageInterface $storage, $callback = null, $callbackhome = null, $deauthenticate = false, $instance_id = '')
[37] Fix | Delete
{
[38] Fix | Delete
// Check the cURL extension is loaded
[39] Fix | Delete
if (!extension_loaded('curl')) {
[40] Fix | Delete
throw new Dropbox_Exception('The cURL OAuth consumer requires the cURL extension. Please speak to your web hosting provider so that this missing PHP component can be installed.');
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
$this->consumerKey = $key;
[44] Fix | Delete
$this->oauth2_id = $oauth2_id;
[45] Fix | Delete
$this->consumerSecret = $secret;
[46] Fix | Delete
$this->storage = $storage;
[47] Fix | Delete
$this->callback = $callback;
[48] Fix | Delete
$this->callbackhome = $callbackhome;
[49] Fix | Delete
$this->instance_id = $instance_id;
[50] Fix | Delete
[51] Fix | Delete
if ($deauthenticate) {
[52] Fix | Delete
$this->deauthenticate();
[53] Fix | Delete
} else {
[54] Fix | Delete
$this->authenticate();
[55] Fix | Delete
}
[56] Fix | Delete
}
[57] Fix | Delete
[58] Fix | Delete
/**
[59] Fix | Delete
* Execute an API call
[60] Fix | Delete
* @todo Improve error handling
[61] Fix | Delete
* @param string $method The HTTP method
[62] Fix | Delete
* @param string $url The API endpoint
[63] Fix | Delete
* @param string $call The API method to call
[64] Fix | Delete
* @param array $additional Additional parameters
[65] Fix | Delete
* @return string|object stdClass
[66] Fix | Delete
*/
[67] Fix | Delete
public function fetch($method, $url, $call, array $additional = array(), $retry_with_header = false)
[68] Fix | Delete
{
[69] Fix | Delete
// Get the signed request URL
[70] Fix | Delete
$request = $this->getSignedRequest($method, $url, $call, $additional);
[71] Fix | Delete
[72] Fix | Delete
// Initialise and execute a cURL request
[73] Fix | Delete
$handle = curl_init($request['url']);
[74] Fix | Delete
[75] Fix | Delete
// Get the default options array
[76] Fix | Delete
$options = $this->defaultOptions;
[77] Fix | Delete
if (!UpdraftPlus_Options::get_updraft_option('updraft_ssl_useservercerts')) {
[78] Fix | Delete
$options[CURLOPT_CAINFO] = UPDRAFTPLUS_DIR.'/includes/cacert.pem';
[79] Fix | Delete
}
[80] Fix | Delete
if (UpdraftPlus_Options::get_updraft_option('updraft_ssl_disableverify')) {
[81] Fix | Delete
$options[CURLOPT_SSL_VERIFYPEER] = false;
[82] Fix | Delete
} else {
[83] Fix | Delete
$options[CURLOPT_SSL_VERIFYPEER] = true;
[84] Fix | Delete
}
[85] Fix | Delete
[86] Fix | Delete
if (!class_exists('WP_HTTP_Proxy')) require_once(ABSPATH.WPINC.'/class-http.php');
[87] Fix | Delete
$proxy = new WP_HTTP_Proxy();
[88] Fix | Delete
[89] Fix | Delete
if ($proxy->is_enabled()) {
[90] Fix | Delete
# WP_HTTP_Proxy returns empty strings if nothing is set
[91] Fix | Delete
$user = $proxy->username();
[92] Fix | Delete
$pass = $proxy->password();
[93] Fix | Delete
$host = $proxy->host();
[94] Fix | Delete
$port = (int)$proxy->port();
[95] Fix | Delete
if (empty($port)) $port = 8080;
[96] Fix | Delete
if (!empty($host) && $proxy->send_through_proxy($request['url'])) {
[97] Fix | Delete
$options[CURLOPT_PROXY] = $host;
[98] Fix | Delete
$options[CURLOPT_PROXYTYPE] = CURLPROXY_HTTP;
[99] Fix | Delete
$options[CURLOPT_PROXYPORT] = $port;
[100] Fix | Delete
if (!empty($user) && !empty($pass)) {
[101] Fix | Delete
$options[CURLOPT_PROXYAUTH] = CURLAUTH_ANY;
[102] Fix | Delete
$options[CURLOPT_PROXYUSERPWD] = sprintf('%s:%s', $user, $pass);
[103] Fix | Delete
}
[104] Fix | Delete
}
[105] Fix | Delete
}
[106] Fix | Delete
[107] Fix | Delete
/*
[108] Fix | Delete
Add check to see if it's an API v2 call if so then json encode the contents. This is so that it is backwards compatible with API v1 endpoints.
[109] Fix | Delete
*/
[110] Fix | Delete
if (isset($additional['api_v2']) && !empty($request['postfields'])) {
[111] Fix | Delete
$request['postfields'] = json_encode($request['postfields']);
[112] Fix | Delete
}
[113] Fix | Delete
[114] Fix | Delete
if (isset($request['headers']) && !empty($request['headers'])) $options[CURLOPT_HTTPHEADER] = $request['headers'];
[115] Fix | Delete
[116] Fix | Delete
if ($method == 'GET' && $this->outFile) { // GET
[117] Fix | Delete
$options[CURLOPT_RETURNTRANSFER] = false;
[118] Fix | Delete
$options[CURLOPT_HEADER] = false;
[119] Fix | Delete
$options[CURLOPT_FILE] = $this->outFile;
[120] Fix | Delete
$options[CURLOPT_BINARYTRANSFER] = true;
[121] Fix | Delete
$options[CURLOPT_FAILONERROR] = true;
[122] Fix | Delete
$this->outFile = null;
[123] Fix | Delete
} elseif ($method == 'POST' && $this->outFile) { // POST
[124] Fix | Delete
$options[CURLOPT_POST] = true;
[125] Fix | Delete
$options[CURLOPT_RETURNTRANSFER] = false;
[126] Fix | Delete
$options[CURLOPT_HEADER] = false;
[127] Fix | Delete
$options[CURLOPT_FILE] = $this->outFile;
[128] Fix | Delete
$options[CURLOPT_BINARYTRANSFER] = true;
[129] Fix | Delete
$options[CURLOPT_FAILONERROR] = true;
[130] Fix | Delete
$this->outFile = null;
[131] Fix | Delete
} elseif ($method == 'POST' && $this->inFile) { // POST
[132] Fix | Delete
$options[CURLOPT_POST] = true;
[133] Fix | Delete
$options[CURLOPT_POSTFIELDS] = $this->inFile;
[134] Fix | Delete
} elseif ($method == 'POST') { // POST
[135] Fix | Delete
$options[CURLOPT_POST] = true;
[136] Fix | Delete
if (!empty($request['postfields'])) {
[137] Fix | Delete
$options[CURLOPT_POSTFIELDS] = $request['postfields'];
[138] Fix | Delete
} elseif (empty($additional['content_upload'])) {
[139] Fix | Delete
// JSON representation of nullity
[140] Fix | Delete
$options[CURLOPT_POSTFIELDS] = 'null';
[141] Fix | Delete
} elseif ($retry_with_header) {
[142] Fix | Delete
// It's a content upload, and there's no data. Versions of php-curl differ as to whether they add a Content-Length header automatically or not. Dropbox complains if it's not there. Here we have had a Dropbox 400 bad request returned so we try again with the header
[143] Fix | Delete
$options[CURLOPT_HTTPHEADER] = array_merge($options[CURLOPT_HTTPHEADER], array('Content-Length: 0'));
[144] Fix | Delete
}
[145] Fix | Delete
} elseif ($method == 'PUT' && $this->inFile) { // PUT
[146] Fix | Delete
$options[CURLOPT_PUT] = true;
[147] Fix | Delete
$options[CURLOPT_INFILE] = $this->inFile;
[148] Fix | Delete
// @todo Update so the data is not loaded into memory to get its size
[149] Fix | Delete
$options[CURLOPT_INFILESIZE] = strlen(stream_get_contents($this->inFile));
[150] Fix | Delete
fseek($this->inFile, 0);
[151] Fix | Delete
$this->inFile = null;
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
if (isset($additional['timeout'])) {
[155] Fix | Delete
$options[CURLOPT_TIMEOUT] = $additional['timeout'];
[156] Fix | Delete
}
[157] Fix | Delete
[158] Fix | Delete
// Set the cURL options at once
[159] Fix | Delete
curl_setopt_array($handle, $options);
[160] Fix | Delete
// Execute, get any error and close
[161] Fix | Delete
$response = curl_exec($handle);
[162] Fix | Delete
$error = curl_error($handle);
[163] Fix | Delete
$getinfo = curl_getinfo($handle);
[164] Fix | Delete
[165] Fix | Delete
curl_close($handle);
[166] Fix | Delete
[167] Fix | Delete
//Check if a cURL error has occured
[168] Fix | Delete
if ($response === false) {
[169] Fix | Delete
throw new Dropbox_CurlException($error);
[170] Fix | Delete
} else {
[171] Fix | Delete
// Parse the response if it is a string
[172] Fix | Delete
if (is_string($response)) {
[173] Fix | Delete
$response = $this->parse($response);
[174] Fix | Delete
}
[175] Fix | Delete
[176] Fix | Delete
// Set the last response
[177] Fix | Delete
$this->lastResponse = $response;
[178] Fix | Delete
[179] Fix | Delete
$code = (!empty($response['code'])) ? $response['code'] : $getinfo['http_code'];
[180] Fix | Delete
[181] Fix | Delete
// The API doesn't return an error message for the 304 status code...
[182] Fix | Delete
// 304's are only returned when the path supplied during metadata calls has not been modified
[183] Fix | Delete
if ($code == 304) {
[184] Fix | Delete
$response['body'] = new stdClass;
[185] Fix | Delete
$response['body']->error = 'The folder contents have not changed';
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
// Check if an error occurred and throw an Exception
[189] Fix | Delete
if (!empty($response['body']->error) || $code >= 400) {
[190] Fix | Delete
// Dropbox returns error messages inconsistently...
[191] Fix | Delete
if (!empty($response['body']->error) && $response['body']->error instanceof stdClass) {
[192] Fix | Delete
$array = array_values((array) $response['body']->error);
[193] Fix | Delete
//Dropbox API v2 only throws 409 errors if this error is a incorrect_offset then we need the entire error array not just the message. PHP Exception messages have to be a string so JSON encode the array.
[194] Fix | Delete
if (strpos($array[0] , 'incorrect_offset') !== false) {
[195] Fix | Delete
$message = json_encode($array);
[196] Fix | Delete
} elseif (strpos($array[0] , 'lookup_failed') !== false ) {
[197] Fix | Delete
// re-structure the array so it is correctly formatted for API
[198] Fix | Delete
// Note: Dropbox v2 returns different errors at different stages hence this fix
[199] Fix | Delete
$correctOffset = array(
[200] Fix | Delete
'0' => $array[1]->{'.tag'},
[201] Fix | Delete
);
[202] Fix | Delete
// the lookup_failed response doesn't always return a correct_offset this happens when the lookup fails because the session has been closed e.g the file has already been uploaded but the response didn't make it back to the client so we try again
[203] Fix | Delete
if (isset($array[1]->correct_offset)) $correctOffset['1'] = $array[1]->correct_offset;
[204] Fix | Delete
[205] Fix | Delete
$message = json_encode($correctOffset);
[206] Fix | Delete
} else {
[207] Fix | Delete
$message = $array[0];
[208] Fix | Delete
}
[209] Fix | Delete
} elseif (!empty($response['body']->error)) {
[210] Fix | Delete
$message = $response['body']->error;
[211] Fix | Delete
} elseif (is_string($response['body'])) {
[212] Fix | Delete
// 31 Mar 2017 - This case has been found to exist; though the docs imply that there's always an 'error' property and that what is returned in JSON, we found a case of this being returned just as a simple string, but detectable via an HTTP 400: Error in call to API function "files/upload_session/append_v2": HTTP header "Dropbox-API-Arg": cursor.offset: expected integer, got string
[213] Fix | Delete
$message = $response['body'];
[214] Fix | Delete
} else {
[215] Fix | Delete
$message = "HTTP bad response code: $code";
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
// Throw an Exception with the appropriate with the appropriate message and code
[219] Fix | Delete
switch ($code) {
[220] Fix | Delete
case 304:
[221] Fix | Delete
throw new Dropbox_NotModifiedException($message, 304);
[222] Fix | Delete
case 400:
[223] Fix | Delete
if (!$retry_with_header) return $this->fetch($method, $url, $call, $additional, true);
[224] Fix | Delete
throw new Dropbox_BadRequestException($message, 400);
[225] Fix | Delete
case 404:
[226] Fix | Delete
throw new Dropbox_NotFoundException($message, 404);
[227] Fix | Delete
case 406:
[228] Fix | Delete
throw new Dropbox_NotAcceptableException($message, 406);
[229] Fix | Delete
case 415:
[230] Fix | Delete
throw new Dropbox_UnsupportedMediaTypeException($message, 415);
[231] Fix | Delete
case 401:
[232] Fix | Delete
//401 means oauth token is expired continue to manually handle the exception depending on the situation
[233] Fix | Delete
break;
[234] Fix | Delete
case 409:
[235] Fix | Delete
//409 in API V2 every error will return with a 409 to find out what the error is the error description should be checked.
[236] Fix | Delete
throw new Dropbox_Exception($message, $code);
[237] Fix | Delete
default:
[238] Fix | Delete
throw new Dropbox_Exception($message, $code);
[239] Fix | Delete
}
[240] Fix | Delete
}
[241] Fix | Delete
[242] Fix | Delete
return $response;
[243] Fix | Delete
}
[244] Fix | Delete
}
[245] Fix | Delete
[246] Fix | Delete
/**
[247] Fix | Delete
* Parse a cURL response
[248] Fix | Delete
* @param string $response
[249] Fix | Delete
* @return array
[250] Fix | Delete
*/
[251] Fix | Delete
private function parse($response)
[252] Fix | Delete
{
[253] Fix | Delete
// Explode the response into headers and body parts (separated by double EOL)
[254] Fix | Delete
list($headers, $response) = explode("\r\n\r\n", $response, 2);
[255] Fix | Delete
[256] Fix | Delete
// Explode response headers
[257] Fix | Delete
$lines = explode("\r\n", $headers);
[258] Fix | Delete
[259] Fix | Delete
// If the status code is 100, the API server must send a final response
[260] Fix | Delete
// We need to explode the response again to get the actual response
[261] Fix | Delete
if (preg_match('#^HTTP/[\.\d]+ 100#i', $lines[0])) {
[262] Fix | Delete
list($headers, $response) = explode("\r\n\r\n", $response, 2);
[263] Fix | Delete
$lines = explode("\r\n", $headers);
[264] Fix | Delete
}
[265] Fix | Delete
[266] Fix | Delete
// Get the HTTP response code from the first line
[267] Fix | Delete
$first = array_shift($lines);
[268] Fix | Delete
$pattern = '#^HTTP/[\.\d]+ ([0-9]{3})#i';
[269] Fix | Delete
preg_match($pattern, $first, $matches);
[270] Fix | Delete
$code = $matches[1];
[271] Fix | Delete
[272] Fix | Delete
// Parse the remaining headers into an associative array
[273] Fix | Delete
$headers = array();
[274] Fix | Delete
foreach ($lines as $line) {
[275] Fix | Delete
list($k, $v) = explode(': ', $line, 2);
[276] Fix | Delete
$headers[strtolower($k)] = $v;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
// If the response body is not a JSON encoded string
[280] Fix | Delete
// we'll return the entire response body
[281] Fix | Delete
if (!$body = json_decode($response)) {
[282] Fix | Delete
$body = $response;
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
if (is_string($body)) {
[286] Fix | Delete
$body_lines = explode("\r\n", $body);
[287] Fix | Delete
if (preg_match('#^HTTP/[\.\d]+ 100#i', $body_lines[0]) && preg_match('#^HTTP/\d#i', $body_lines[2])) {
[288] Fix | Delete
return $this->parse($body);
[289] Fix | Delete
}
[290] Fix | Delete
}
[291] Fix | Delete
[292] Fix | Delete
return array('code' => $code, 'body' => $body, 'headers' => $headers);
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
/**
[296] Fix | Delete
* Return the response for the last API request
[297] Fix | Delete
* @return mixed
[298] Fix | Delete
*/
[299] Fix | Delete
public function getlastResponse()
[300] Fix | Delete
{
[301] Fix | Delete
return $this->lastResponse;
[302] Fix | Delete
}
[303] Fix | Delete
}
[304] Fix | Delete
[305] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function