-
Notifications
You must be signed in to change notification settings - Fork 9
/
Dropbox.class.php
294 lines (262 loc) · 11 KB
/
Dropbox.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
session_start();
/**
* Dropbox API
*
* This is a simple PHP plaintext OAuth 1.0 API for Dropbox
*
* @author Sean Thomas Burke <http://www.seantburke.com/>
*/
class Dropbox
{
//app variables, be sure to set these to your app settings before continuing.
//they can be found at https://www.dropbox.com/developers/apps
private static $APP_KEY = 'XXXXXXXXXXXXXXX';
private static $APP_SECRET = 'XXXXXXXXXXXXXXX';
private static $CALLBACK_URL = 'http://www.seantburke.com/github/Dropbox-PHP-API/example.php';
//OAuth 1.0 variables
private $request_token_url; //url to dropbox.com to get authorization
private $oauth_token_secret; //reponse secret from initial request
private $oauth_request_token; //initial response token
private $oauth_access_token; //store this in your database
private $oauth_signature; //store this in your database, they need to be used in every API call
private $uid; //the uid returned as $_GET['uid']
/**
* Dropbox()
* creates the object and decides based on the $_SESSION whether to request() or processCallBack()
*
* @author Sean Thomas Burke <http://www.seantburke.com/>
*/
public function __construct()
{
//store session variables from a request()
//this won't do anything until the processCallBack() method is called after the request()
$this->oauth_token_secret = $_SESSION['oauth_token_secret'];
$this->oauth_request_token = $_SESSION['oauth_request_token'];
$this->oauth_access_token = $_SESSION['oauth_access_token'];
$this->oauth_signature = $_SESSION['oauth_signature'];
$this->uid = $_SESSION['uid'];
//if the required variables are not set, then decide whether to make a request or process the $_SESSION
if(!$this->oauth_signature || !$this->oauth_access_token)
{
//if the following are not set, then a request needs to be made
//the fallback decision should be to request for a new token, and not to process the callback method
if($_GET['uid'] && $_GET['oauth_token'] && $this->oauth_token_secret && $this->oauth_request_token)
{
$this->processCallBack();
}
else
{
$this->request();
}
}
}
/**
* request()
*
* sends a request to get OAuth request token and secret, builds the request_token_url
* Step 1: call for request
* @link https://www.dropbox.com/developers/reference/api#request-token
* @link https://www.dropbox.com/developers/reference/api#authorize
* @author Sean Thomas Burke <http://www.seantburke.com/>
*/
private function request()
{
// initiate a cURL; if you don't know what curl is, look it up at http://curl.haxx.se/
$ch = curl_init();
//Dropbox uses plaintext OAuth 1.0; make the header for this request
$headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_signature="'.self::$APP_SECRET.'&"');
// set cURL options and execute
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.dropbox.com/1/oauth/request_token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$request_token_response = curl_exec($ch);
// parse the returned data which has the format:
// "oauth_token=<access-token>&oauth_token_secret=<access-token-secret>"
parse_str($request_token_response, $parsed_request_token);
//check for any errors
$json_access = json_decode($request_token_response);
if($json_access->error)
{
echo '<br><br>FATAL ERROR: '.$json_access->error.'<br><br>';
}
//set these variables in a $_SESSION variable
$_SESSION['oauth_token_secret'] = $parsed_request_token['oauth_token_secret'];
$_SESSION['oauth_request_token'] = $parsed_request_token['oauth_token'];
//also store them in the object (unnecessary, but helps understand concept)
$this->oauth_token_secret = $parsed_request_token['oauth_token_secret'];
$this->oauth_request_token = $parsed_request_token['oauth_token'];
//get the request URL; this is where you send the user to authorize your request. Be sure to set the CALLBACK_URL before doing this.
$this->request_token_url = 'https://www.dropbox.com/1/oauth/authorize?oauth_token='.$parsed_request_token['oauth_token'].'&oauth_callback='.self::$CALLBACK_URL;
}
/**
* processCallBack()
*
* call this function when the user returns from the request_token_url at dropbox.com
* Step 2: Process Request and get Signature and Access Token
* @link https://www.dropbox.com/developers/reference/api#request-token
* @author Sean Thomas Burke <http://www.seantburke.com>
*/
private function processCallBack()
{
//Now we must process the request
//same steps as before, but now the header is modified to include the response variables that were stored in the session
//notice the signature is a concatenation of the app_secret and the token_secret
$ch = curl_init();
$headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this->oauth_request_token.'", oauth_signature="'.self::$APP_SECRET.'&'.$this->oauth_token_secret.'"');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, "https://api.dropbox.com/1/oauth/access_token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//execute and parse
$access_token_response = curl_exec($ch);
parse_str($access_token_response, $parsed_access_token);
//check for errors
$json_access = json_decode($access_token_response);
if($json_access->error)
{
echo '<br><br>FATAL ERROR: '.$json_access->error.'<br><br>';
}
//it is unnecessary to keep the oauth_token_secret and oauth_request_token at this point
//clear the $_SESSION
session_unset();
//store oauth_access_token and oauth_signature responses in $_SESSION
//again, oauth_signature is a concatenation of the APP_SECRET and the oauth_token_secret response
//these 2 variables are what you need to make API requests
$_SESSION['oauth_access_token'] = $parsed_access_token['oauth_token'];
$_SESSION['oauth_signature'] = self::$APP_SECRET.'&'.$parsed_access_token['oauth_token_secret'];
//dropbox also gives you uid, store it
$_SESSION['uid'] = $_GET['uid'];
//also store variables in the object for future reference
$this->oauth_access_token = $parsed_access_token['oauth_token'];
$this->oauth_signature = self::$APP_SECRET.'&'.$parsed_access_token['oauth_token_secret'];
$this->uid = $_GET['uid'];
}
/**
* get($url)
*
* Using the REST api, make a call to a REST URL, and it will return the array
* Step 3: Make an API call
*
* @link https://www.dropbox.com/developers/reference/api
* @author Sean Thomas Burke <http://www.seantburke.com>
*
* @param $url REST URL
* @return array decoded from JSON response
*/
function get($url)
{
$ch = curl_init();
$headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this->oauth_access_token.'", oauth_signature="'.$this->oauth_signature.'"');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$api_response = curl_exec($ch);
return $api_response;
}
/**
* call($url)
*
* Using the REST api, make a call to a REST URL, and it will return the array
* Step 3: Make an API call
*
* @link https://www.dropbox.com/developers/reference/api
* @author Sean Thomas Burke <http://www.seantburke.com>
*
* @param $url REST URL
* @return array decoded from JSON response
*/
function put($url)
{
$ch = curl_init();
$headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this->oauth_access_token.'", oauth_signature="'.$this->oauth_signature.'"');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$api_response = curl_exec($ch);
return json_decode($api_response);
}
/**
* getAccessURL()
* returns the URL used for requests
* @link https://www.dropbox.com/developers/reference/api#authorize
* @author Sean Thomas Burke <http://www.seantburke.com>
*
* @return string of URL for requesting OAuth Token
*/
function getAccessURL()
{
//get the Request URL
return $this->request_token_url;
}
/**
* hasAccess()
* check to see if the user has access already
*
* @author Sean Thomas Burke <http://www.seantburke.com>
* @return boolean of 3 required variables (uid is not required, but it helps)
*/
function hasAccess()
{
return ($this->oauth_access_token && $this->oauth_signature && $this->uid);
}
/**
* getFile()
* get a file from the dropbox
* @link https://www.dropbox.com/developers/reference/api#files-GET
* @author Sean Thomas Burke <http://www.seantburke.com>
*
* @param $root {sandbox, dropbox} $path {url path to document}
* @return files contents
*/
function getFile($root, $path)
{
return $this->get('https://api-content.dropbox.com/1/files/'.$root.'/'.$path);
}
/**
* putFile
* get a file from the dropbox
* @link https://www.dropbox.com/developers/reference/api#files-GET
* @author Sean Thomas Burke <http://www.seantburke.com>
*
* @param $root {sandbox, dropbox} $path {url path to document}
* @return boolean of 3 required variables (uid is not required, but it helps)
*/
function putFile($root,$path)
{
//TODO still needs implementation
return $this->put('https://api-content.dropbox.com/1/files_put/'.$root.'/'.$path.'?param=val');
}
/**
* test()
* test the dropbox upload function
* @link https://www.dropbox.com/developers/reference/api#files-GET
* @author Sean Thomas Burke <http://www.seantburke.com>
*
* @param $root either "sandbox" or "dropbox" $path {url path to document}
* @return boolean of 3 required variables (uid is not required, but it helps)
*/
function test($root)
{
$url = 'https://api-content.dropbox.com/1/files_put/'.$root.'/test2.txt?overwrite=true&locale=en';
$body = file_get_contents('test.txt');
//echo "file_contents: ".$body;
$fp = fopen('php://temp/maxmemory:256000', 'w');
if (!$fp) {
die('could not open temp memory data');
}
fwrite($fp, $body);
fseek($fp, 0);
$ch = curl_init();
$headers = array('Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.self::$APP_KEY.'", oauth_token="'.$this->oauth_access_token.'", oauth_signature="'.$this->oauth_signature.'"');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//echo '<a href="'.$url.'">'.$url.'</a>';
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_INFILE, $fp); // file pointer
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
return $api_response = curl_exec($ch);
}
}