PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.3.8
UpdraftPlus: WP Backup & Migration Plugin v1.3.8
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / includes / Dropbox / OAuth / Consumer / Curl.php

Curl.php in UpdraftPlus: WP Backup & Migration Plugin 1.3.8, at includes/Dropbox/OAuth/Consumer/Curl.php

149 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * OAuth consumer using PHP cURL
5 * @author Ben Tadiar <ben@handcraftedbyben.co.uk>
6 * @link https://github.com/benthedesigner/dropbox
7 * @package Dropbox\OAuth
8 * @subpackage Consumer
9 */
10
11 class Dropbox_Curl extends Dropbox_ConsumerAbstract
12 {
13 /**
14 * Default cURL options
15 * @var array
16 */
17 private $defaultOptions = array(
18 CURLOPT_SSL_VERIFYPEER => true,
19 CURLOPT_VERBOSE => true,
20 CURLOPT_HEADER => true,
21 CURLINFO_HEADER_OUT => false,
22 CURLOPT_RETURNTRANSFER => true,
23 CURLOPT_FOLLOWLOCATION => false,
24 );
25
26 /**
27 * Set properties and begin authentication
28 * @param string $key
29 * @param string $secret
30 * @param \Dropbox\OAuth\Consumer\StorageInterface $storage
31 * @param string $callback
32 */
33 public function __construct($key, $secret, Dropbox_StorageInterface $storage, $callback = null)
34 {
35 // Check the cURL extension is loaded
36 if (!extension_loaded('curl')) {
37 throw new Dropbox_Exception('The cURL OAuth consumer requires the cURL extension');
38 }
39
40 $this->consumerKey = $key;
41 $this->consumerSecret = $secret;
42 $this->storage = $storage;
43 $this->callback = $callback;
44 $this->authenticate();
45 }
46
47 /**
48 * Execute an API call
49 * @todo Improve error handling
50 * @param string $method The HTTP method
51 * @param string $url The API endpoint
52 * @param string $call The API method to call
53 * @param array $additional Additional parameters
54 * @return string|object stdClass
55 */
56 public function fetch($method, $url, $call, array $additional = array())
57 {
58 // Get the signed request URL
59 $request = $this->getSignedRequest($method, $url, $call, $additional);
60
61 // Initialise and execute a cURL request
62 $handle = curl_init($request['url']);
63
64 // Get the default options array
65 $options = $this->defaultOptions;
66 $options[CURLOPT_CAINFO] = dirname(__FILE__) . '/ca-bundle.pem';
67
68 if ($method == 'GET' && $this->outFile) { // GET
69 $options[CURLOPT_RETURNTRANSFER] = false;
70 $options[CURLOPT_HEADER] = false;
71 $options[CURLOPT_FILE] = $this->outFile;
72 $options[CURLOPT_BINARYTRANSFER] = true;
73 $this->outFile = null;
74 } elseif ($method == 'POST') { // POST
75 $options[CURLOPT_POST] = true;
76 $options[CURLOPT_POSTFIELDS] = $request['postfields'];
77 } elseif ($method == 'PUT' && $this->inFile) { // PUT
78 $options[CURLOPT_PUT] = true;
79 $options[CURLOPT_INFILE] = $this->inFile;
80 // @todo Update so the data is not loaded into memory to get its size
81 $options[CURLOPT_INFILESIZE] = strlen(stream_get_contents($this->inFile));
82 fseek($this->inFile, 0);
83 $this->inFile = null;
84 }
85
86 // Set the cURL options at once
87 curl_setopt_array($handle, $options);
88
89 // Execute and parse the response
90 $response = curl_exec($handle);
91 curl_close($handle);
92
93 // Parse the response if it is a string
94 if (is_string($response)) {
95 $response = $this->parse($response);
96 }
97
98 // Check if an error occurred and throw an Exception
99 if (!empty($response['body']->error)) {
100 $message = $response['body']->error . ' (Status Code: ' . $response['code'] . ')';
101 throw new Dropbox_Exception($message);
102 }
103
104 return $response;
105 }
106
107 /**
108 * Parse a cURL response
109 * @param string $response
110 * @return array
111 */
112 private function parse($response)
113 {
114 // Explode the response into headers and body parts (separated by double EOL)
115 list($headers, $response) = explode("\r\n\r\n", $response, 2);
116
117 // Explode response headers
118 $lines = explode("\r\n", $headers);
119
120 // If the status code is 100, the API server must send a final response
121 // We need to explode the response again to get the actual response
122 if (preg_match('#^HTTP/1.1 100#', $lines[0])) {
123 list($headers, $response) = explode("\r\n\r\n", $response, 2);
124 $lines = explode("\r\n", $headers);
125 }
126
127 // Get the HTTP response code from the first line
128 $first = array_shift($lines);
129 $pattern = '#^HTTP/1.1 ([0-9]{3})#';
130 preg_match($pattern, $first, $matches);
131 $code = $matches[1];
132
133 // Parse the remaining headers into an associative array
134 $headers = array();
135 foreach ($lines as $line) {
136 list($k, $v) = explode(': ', $line, 2);
137 $headers[strtolower($k)] = $v;
138 }
139
140 // If the response body is not a JSON encoded string
141 // we'll return the entire response body
142 if (!$body = json_decode($response)) {
143 $body = $response;
144 }
145
146 return array('code' => $code, 'body' => $body, 'headers' => $headers);
147 }
148 }
149