-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropboxClient.php
368 lines (293 loc) · 10.4 KB
/
DropboxClient.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<?php
/**
* DropPHP - A simple Dropbox client that works without cURL.
*
* http://fabi.me/en/php-projects/dropphp-dropbox-api-client/
*
* @author Fabian Schlieper <[email protected]>
* @copyright Fabian Schlieper 2012
* @version 1.0
* @license See license.txt
*
*/
require_once(dirname(__FILE__)."/OAuthSimple.php");
class DropboxClient {
const API_URL = "https://api.dropbox.com/1/";
const API_CONTENT_URL = "http://api-content.dropbox.com/1/";
const BUFFER_SIZE = 524288;
private $appParams;
private $consumerToken;
private $requestToken;
private $accessToken;
private $locale;
private $rootPath;
function __construct ($app_params, $locale = "en"){
$this->appParams = $app_params;
$this->consumerToken = array('t' => $this->appParams['app_key'], 's' => $this->appParams['app_secret']);
$this->locale = $locale;
$this->rootPath = empty($app_params['app_full_access']) ? "sandbox" : "dropbox";
$this->requestToken = null;
$this->accessToken = null;
}
// ##################################################
// Authorization
function GetRequestToken()
{
if(!empty($this->requestToken))
return $this->requestToken;
$rt = $this->authCall("oauth/request_token");
if(empty($rt))
throw new DropboxException('Could not get request token!');
return ($this->requestToken = array('t'=>$rt['oauth_token'], 's'=>$rt['oauth_token_secret']));
}
function GetAccessToken($request_token)
{
if(!empty($this->accessToken)) return $this->accessToken;
if(empty($request_token)) throw new DropboxException('Passed request token is invalid.');
$at = $this->authCall("oauth/access_token", $request_token);
if(empty($at))
throw new DropboxException('Could not get access token!');
return ($this->accessToken = array('t'=>$at['oauth_token'], 's'=>$at['oauth_token_secret']));
}
function SetAccessToken($token)
{
if(empty($token['t']) || empty($token['s'])) throw new DropboxException('Passed invalid access token.');
$this->accessToken = $token;
}
function IsAuthorized()
{
if(empty($this->accessToken)) return false;
return true;
}
function BuildAuthorizeUrl($return_url)
{
$rt = $this->GetRequestToken();
if(empty($rt)) return false;
return "https://www.dropbox.com/1/oauth/authorize?oauth_token=".$rt['t']."&oauth_callback=".urlencode($return_url);
}
function GetAccountInfo()
{
return $this->apiCall("account/info", "GET", array('hash'=>true));
}
// ##################################################
// API Functions
/**
* Get file list of a dropbox folder.
*
* @access public
* @param string $dropbox_path Dropbox path of the folder
*/
public function GetFiles($dropbox_path='', $recursive=false)
{
if(is_object($dropbox_path) && !empty($dropbox_path->path)) $dropbox_path = $dropbox_path->path;
return $this->getFileTree($dropbox_path, $recursive ? 1000 : 0);
}
/**
* Get file or folder metadata
*
* @access public
* @param dropbox_path (String) Dropbox path of the file or folder
*/
public function GetMetadata($dropbox_path)
{
if(is_object($dropbox_path) && !empty($dropbox_path->path)) $dropbox_path = $dropbox_path->path;
return $this->apiCall("metadata/$this->rootPath/$dropbox_path", "GET");
}
public function GetMedia($dropbox_path)
{
//print_r ($dropbox_path);
//print_r ($dropbox_path->path);
if(is_object($dropbox_path) && !empty($dropbox_path->path)) $dropbox_path = $dropbox_path->path;
//print_r("$this->rootPath/$dropbox_path");
return $this->apiCall("media/$this->rootPath/$dropbox_path", "POST");
}
/**
* Download a file to the webserver
*
* @access public
* @param string|object $dropbox_file Dropbox path or metadata object of the file to download.
* @param string $dest_path Local path for destination
* @param int $revision Optional. The revision of the file to retrieve. This defaults to the most recent revision.
* @return object Dropbox file metadata
*/
public function DownloadFile($dropbox_file, $dest_path='', $revision=-1)
{
if(is_object($dropbox_file) && !empty($dropbox_file->path)) $dropbox_file = $dropbox_file->path;
if(empty($dest_path)) $dest_path = basename($dropbox_file);
$url = $this->cleanUrl(self::API_CONTENT_URL."/files/$this->rootPath/$dropbox_file");
$content = ($revision != -1) ? http_build_query(array('rev' => $revision)) : null;
$context = $this->createRequestContext($url, "GET", $content);
$rh = @fopen($url, 'rb', false, $context); // read binary
if($rh === false)
throw new DropboxException("HTTP request to $url failed!");
$fh = @fopen($dest_path, 'wb'); // write binary
if($fh === false) {
@fclose($rh);
throw new DropboxException("Could not create file $dest_path !");
}
$size = 0;
while (!feof($rh)) {
if(($s=fwrite($fh, fread($rh, self::BUFFER_SIZE))) === false) {
@fclose($rh);
@fclose($fh);
throw new DropboxException("Writing to file $dest_path failed!'");
}
$size += $s;
}
// get file meta from HTTP header
$s_meta = stream_get_meta_data($rh);
$meta = json_decode(substr(reset(array_filter($s_meta['wrapper_data'], create_function('$s', 'return strpos($s, "x-dropbox-metadata:") === 0;'))), 20));
fclose($rh);
fclose($fh);
if($meta->bytes != $size)
throw new DropboxException("Download size mismatch!");
return $meta;
}
public function DownloadThumbnail($dropbox_file, $dest_path='', $revision=-1)
{
echo $dropbox_file;
echo $dest_path;
if(is_object($dropbox_file) && !empty($dropbox_file->path)) $dropbox_file = $dropbox_file->path;
if($dest_path == '')
{
$pieces = explode(".", basename($dropbox_file));
$dest_path = "images/thumbs/Image_$pieces[0].jpeg";
print_r ($dest_path);
}
$url = $this->cleanUrl(self::API_CONTENT_URL."/thumbnails/$this->rootPath/$dropbox_file");
$content = ($revision != -1) ? http_build_query(array('rev' => $revision)) : null;
$context = $this->createRequestContext($url, "GET", $content);
echo "<br>";
echo $url;
$rh = @fopen($url, 'rb', false, $context); // read binary
if($rh === false)
throw new DropboxException("HTTP request to $url failed!");
$fh = @fopen($dest_path, 'wb'); // write binary
if($fh === false) {
@fclose($rh);
throw new DropboxException("Could not create file $dest_path !");
}
$size = 0;
while (!feof($rh)) {
if(($s=fwrite($fh, fread($rh, self::BUFFER_SIZE))) === false) {
@fclose($rh);
@fclose($fh);
throw new DropboxException("Writing to file $dest_path failed!'");
}
$size += $s;
}
// get file meta from HTTP header
$s_meta = stream_get_meta_data($rh);
$meta = json_decode(substr(reset(array_filter($s_meta['wrapper_data'], create_function('$s', 'return strpos($s, "x-dropbox-metadata:") === 0;'))), 20));
fclose($rh);
fclose($fh);
//if($meta->bytes != $size)
//throw new DropboxException("Download size mismatch!");
return $meta;
}
/**
* Upload a file to dropbox
*
* @access public
* @param src_path (String) Local file to upload
* @param dropbox_path (String) Dropbox path for destionation
* @return object Dropbox file metadata
*/
public function UploadFile($src_file, $dropbox_path='')
{
if(empty($dropbox_path)) $dropbox_path = basename($src_file);
elseif(is_object($dropbox_path) && !empty($dropbox_path->path)) $dropbox_path = $dropbox_path->path;
//$params = array('locale' => $this->locale); //, 'overwrite' => $overwrite ? 'true' : 'false');
//$query = http_build_query($params);
$url = $this->cleanUrl(self::API_CONTENT_URL."/files_put/$this->rootPath/$dropbox_path"); // ?$query TODO causes a 403 forbidden error..
$content =& file_get_contents($src_file);
if(strlen($content) == 0) throw new DropboxException("Could not read file $src_file !");
$context = $this->createRequestContext($url, "PUT", $content);
return json_decode(file_get_contents($url, false, $context));
}
function getFileTree($path="", $max_depth = 0, $depth=0)
{
static $files;
if($depth == 0) $files = array();
$dir = $this->apiCall("metadata/$this->rootPath/$path", "GET");
foreach($dir->contents as $item)
{
$files[trim($item->path,'/')] = $item;
if($item->is_dir && $depth < $max_depth)
{
$this->getFileTree($item->path, $max_depth, $depth+1);
}
}
return $files;
}
function createRequestContext($url, $method, &$content, $oauth_token=-1)
{
if($oauth_token === -1)
$oauth_token = $this->accessToken;
$oauth = new OAuthSimple($this->consumerToken['t'],$this->consumerToken['s']);
$params = array();
if(empty($oauth_token) && !empty($this->accessToken))
$oauth_token = $this->accessToken;
if(!empty($oauth_token)) {
$oauth->signatures(array('oauth_secret'=>$oauth_token['s']));
$params['oauth_token'] = $oauth_token['t'];
}
$signed = $oauth->sign(array(
'action' => $method,
'path'=> $url,
'parameters'=> $params));
$header = "Authorization: ".$signed['header']."\r\n";
//echo "<br><br>SBS: $signed[sbs]<br><br>";
$http_context = array(
'method'=>$method,
'header'=> $header);
if(!empty($content)) {
$http_context['header'] .= "Content-Length: ".strlen($content)."\r\n";
$http_context['header'] .= "Content-Type: application/octet-stream\r\n";
$http_context['content'] =& $content;
}
return stream_context_create(array('http'=>$http_context));
}
function authCall($path, $request_token=null)
{
$url = $this->cleanUrl(self::API_URL.$path);
$dummy = null;
$context = $this->createRequestContext($url, "POST", $dummy, $request_token);
$data = array();
parse_str(file_get_contents($url, false, $context), $data);
return $data;
}
function apiCall($path, $method, $params=array())
{
$url = $this->cleanUrl(self::API_URL.$path);
$content = http_build_query(array_merge(array('locale'=>$this->locale), $params));
$context = $this->createRequestContext($url, $method, $content);
return json_decode(file_get_contents($url, false, $context));
}
function cleanUrl($url) {
return substr($url,0,8).str_replace('//','/', str_replace('\\','/',substr($url,8)));
}
}
class DropboxException extends Exception {
public function __construct($err, $isDebug = FALSE)
{
$this->message = $err;
self::log_error($err);
if ($isDebug)
{
self::display_error($err, TRUE);
}
}
public static function log_error($err)
{
error_log($err, 0);
}
public static function display_error($err, $kill = FALSE)
{
print_r($err);
if ($kill === FALSE)
{
die();
}
}
}