| 1 |
<?php |
| 2 |
class Dropbox { |
| 3 |
const API_URL = 'https://api.dropbox.com/'; |
| 4 |
const API_CONTENT_URL = 'https://api-content.dropbox.com/'; |
| 5 |
const API_WWW_URL = 'https://www.dropbox.com/'; |
| 6 |
const API_VERSION_URL = '1/'; |
| 7 |
|
| 8 |
private $root; |
| 9 |
private $ProgressFunction = false; |
| 10 |
private $oauth_app_key; |
| 11 |
private $oauth_app_secret; |
| 12 |
private $oauth_token; |
| 13 |
private $oauth_token_secret; |
| 14 |
|
| 15 |
public function __construct($oauth_app_key, $oauth_app_secret, $dropbox=false) { |
| 16 |
$this->oauth_app_key = $oauth_app_key; |
| 17 |
$this->oauth_app_secret = $oauth_app_secret; |
| 18 |
|
| 19 |
if ($dropbox) |
| 20 |
$this->root = 'dropbox'; |
| 21 |
else |
| 22 |
$this->root = 'sandbox'; |
| 23 |
} |
| 24 |
|
| 25 |
public function setOAuthTokens($token,$secret) { |
| 26 |
$this->oauth_token = $token; |
| 27 |
$this->oauth_token_secret = $secret; |
| 28 |
} |
| 29 |
|
| 30 |
public function setProgressFunction($function=null) { |
| 31 |
if (function_exists($function)) |
| 32 |
$this->ProgressFunction = $function; |
| 33 |
else |
| 34 |
$this->ProgressFunction = false; |
| 35 |
} |
| 36 |
|
| 37 |
public function accountInfo(){ |
| 38 |
$url = self::API_URL.self::API_VERSION_URL.'account/info'; |
| 39 |
return $this->request($url); |
| 40 |
} |
| 41 |
|
| 42 |
public function upload($file, $path = '',$overwrite=true){ |
| 43 |
$file = str_replace("\\", "/",$file); |
| 44 |
if (!is_readable($file) or !is_file($file)) |
| 45 |
throw new DropboxException("Error: File \"$file\" is not readable or doesn't exist."); |
| 46 |
$filesize=filesize($file); |
| 47 |
if ($filesize<(1024*1024*50)) { //chunk transfer on bigger uploads <50MB |
| 48 |
$filehandle = fopen($file,'r'); |
| 49 |
$url = self::API_CONTENT_URL.self::API_VERSION_URL.'files_put/'.$this->root.'/'.trim($path, '/'); |
| 50 |
$output = $this->request($url, array('overwrite' => ($overwrite)? 'true' : 'false'), 'PUT', $filehandle, $filesize); |
| 51 |
fclose($filehandle); |
| 52 |
} else { |
| 53 |
$output = $this->chunked_upload($file, $path,$overwrite); |
| 54 |
} |
| 55 |
return $output; |
| 56 |
} |
| 57 |
|
| 58 |
public function chunked_upload($file, $path = '',$overwrite=true){ |
| 59 |
$file = str_replace("\\", "/",$file); |
| 60 |
if (!is_readable($file) or !is_file($file)) |
| 61 |
throw new DropboxException("Error: File \"$file\" is not readable or doesn't exist."); |
| 62 |
$file_handle=fopen($file,'r'); |
| 63 |
$uploadid=null; |
| 64 |
$offset=0; |
| 65 |
$ProgressFunction=null; |
| 66 |
while ($data=fread($file_handle,(1024*1024*30))) { // 30MB chunk size |
| 67 |
$chunkHandle = fopen('php://memory', 'rw'); |
| 68 |
fwrite($chunkHandle,$data); |
| 69 |
rewind($chunkHandle); |
| 70 |
//overwrite progress function |
| 71 |
if (!empty($this->ProgressFunction) and function_exists($this->ProgressFunction)) { |
| 72 |
$ProgressFunction=$this->ProgressFunction; |
| 73 |
$this->ProgressFunction=false; |
| 74 |
} |
| 75 |
$url = self::API_CONTENT_URL.self::API_VERSION_URL.'chunked_upload'; |
| 76 |
$output = $this->request($url, array('upload_id' => $uploadid,'offset'=>$offset), 'PUT', $chunkHandle, strlen($data)); |
| 77 |
fclose($chunkHandle); |
| 78 |
if ($ProgressFunction) { |
| 79 |
call_user_func($ProgressFunction,0,0,0,$offset); |
| 80 |
$this->ProgressFunction=$ProgressFunction; |
| 81 |
} |
| 82 |
//args for next chunk |
| 83 |
$offset=$output['offset']; |
| 84 |
$uploadid=$output['upload_id']; |
| 85 |
fseek($file_handle,$offset); |
| 86 |
} |
| 87 |
fclose($file_handle); |
| 88 |
$url = self::API_CONTENT_URL.self::API_VERSION_URL.'commit_chunked_upload/'.$this->root.'/'.trim($path, '/'); |
| 89 |
return $this->request($url, array('overwrite' => ($overwrite)? 'true' : 'false','upload_id'=>$uploadid), 'POST'); |
| 90 |
} |
| 91 |
|
| 92 |
public function download($path,$echo=false){ |
| 93 |
$url = self::API_CONTENT_URL.self::API_VERSION_URL.'files/'.$this->root.'/'.trim($path,'/'); |
| 94 |
if (!$echo) |
| 95 |
return $this->request($url); |
| 96 |
else |
| 97 |
$this->request($url,'','GET','','',true); |
| 98 |
} |
| 99 |
|
| 100 |
public function metadata($path = '', $listContents = true, $fileLimit = 10000){ |
| 101 |
$url = self::API_URL.self::API_VERSION_URL.'metadata/'.$this->root.'/'.trim($path,'/'); |
| 102 |
return $this->request($url, array('list' => ($listContents)? 'true' : 'false', 'file_limit' => $fileLimit)); |
| 103 |
} |
| 104 |
|
| 105 |
public function search($path = '', $query , $fileLimit = 1000){ |
| 106 |
if (strlen($query)>=3) |
| 107 |
throw new DropboxException("Error: Query \"$query\" must three characters long."); |
| 108 |
$url = self::API_URL.self::API_VERSION_URL.'search/'.$this->root.'/'.trim($path,'/'); |
| 109 |
return $this->request($url, array('query' => $query, 'file_limit' => $fileLimit)); |
| 110 |
} |
| 111 |
|
| 112 |
public function shares($path = ''){ |
| 113 |
$url = self::API_URL.self::API_VERSION_URL.'shares/'.$this->root.'/'.trim($path,'/'); |
| 114 |
return $this->request($url); |
| 115 |
} |
| 116 |
|
| 117 |
public function media($path = ''){ |
| 118 |
$url = self::API_URL.self::API_VERSION_URL.'media/'.$this->root.'/'.trim($path,'/'); |
| 119 |
return $this->request($url); |
| 120 |
} |
| 121 |
|
| 122 |
public function fileopsDelete($path){ |
| 123 |
$url = self::API_URL.self::API_VERSION_URL.'fileops/delete'; |
| 124 |
return $this->request($url, array('path' => '/'.trim($path,'/'), 'root' => $this->root)); |
| 125 |
} |
| 126 |
|
| 127 |
public function fileopsCreate_folder($path){ |
| 128 |
$url = self::API_URL.self::API_VERSION_URL.'fileops/create_folder'; |
| 129 |
return $this->request($url, array('path' => '/'.trim($path,'/'), 'root' => $this->root)); |
| 130 |
} |
| 131 |
|
| 132 |
public function oAuthAuthorize($callback_url) { |
| 133 |
$headers[] = 'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.$this->oauth_app_key.'", oauth_signature="'.$this->oauth_app_secret.'&"'; |
| 134 |
$ch = curl_init(); |
| 135 |
curl_setopt($ch, CURLOPT_URL, self::API_URL . self::API_VERSION_URL . 'oauth/request_token' ); |
| 136 |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
| 137 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 138 |
curl_setopt($ch, CURLOPT_SSLVERSION,3); |
| 139 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 140 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 141 |
if (is_file(dirname(__FILE__).'/aws/lib/requestcore/cacert.pem')) |
| 142 |
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/aws/lib/requestcore/cacert.pem'); |
| 143 |
curl_setopt($ch, CURLOPT_AUTOREFERER , true); |
| 144 |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); |
| 145 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 146 |
$content = curl_exec($ch); |
| 147 |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 148 |
if ($status>=200 and $status<300 and 0==curl_errno($ch) ) { |
| 149 |
parse_str($content, $oauth_token); |
| 150 |
} else { |
| 151 |
$output = json_decode($content, true); |
| 152 |
if(isset($output['error']) && is_string($output['error'])) $message = $output['error']; |
| 153 |
elseif(isset($output['error']['hash']) && $output['error']['hash'] != '') $message = (string) $output['error']['hash']; |
| 154 |
elseif (0!=curl_errno($ch)) $message = '('.curl_errno($ch).') '.curl_error($ch); |
| 155 |
else $message = '('.$status.') Invalid response.'; |
| 156 |
throw new DropboxException($message); |
| 157 |
} |
| 158 |
curl_close($ch); |
| 159 |
return array( 'authurl' => self::API_WWW_URL . self::API_VERSION_URL . 'oauth/authorize?oauth_token='.$oauth_token['oauth_token'].'&oauth_callback='.urlencode($callback_url), |
| 160 |
'oauth_token' => $oauth_token['oauth_token'], |
| 161 |
'oauth_token_secret'=> $oauth_token['oauth_token_secret'] ); |
| 162 |
} |
| 163 |
|
| 164 |
public function oAuthAccessToken($oauth_token, $oauth_token_secret) { |
| 165 |
$headers[] = 'Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.$this->oauth_app_key.'", oauth_token="'.$oauth_token.'", oauth_signature="'.$this->oauth_app_secret.'&'.$oauth_token_secret.'"'; |
| 166 |
$ch = curl_init(); |
| 167 |
curl_setopt($ch, CURLOPT_URL, self::API_URL . self::API_VERSION_URL . 'oauth/access_token' ); |
| 168 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 169 |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
| 170 |
curl_setopt($ch, CURLOPT_SSLVERSION,3); |
| 171 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 172 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 173 |
if (is_file(dirname(__FILE__).'/aws/lib/requestcore/cacert.pem')) |
| 174 |
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/aws/lib/requestcore/cacert.pem'); |
| 175 |
curl_setopt($ch, CURLOPT_AUTOREFERER , true); |
| 176 |
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers ); |
| 177 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 178 |
$content = curl_exec($ch); |
| 179 |
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 180 |
if ($status>=200 and $status<300 and 0==curl_errno($ch)) { |
| 181 |
parse_str($content, $oauth_token); |
| 182 |
$this->setOAuthTokens($oauth_token['oauth_token'], $oauth_token['oauth_token_secret']); |
| 183 |
return $oauth_token; |
| 184 |
} else { |
| 185 |
$output = json_decode($content, true); |
| 186 |
if(isset($output['error']) && is_string($output['error'])) $message = $output['error']; |
| 187 |
elseif(isset($output['error']['hash']) && $output['error']['hash'] != '') $message = (string) $output['error']['hash']; |
| 188 |
elseif (0!=curl_errno($ch)) $message = '('.curl_errno($ch).') '.curl_error($ch); |
| 189 |
else $message = '('.$status.') Invalid response.'; |
| 190 |
throw new DropboxException($message); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
private function request($url, $args = null, $method = 'GET', $filehandle = null, $filesize=0, $echo=false){ |
| 195 |
$args = (is_array($args)) ? $args : array(); |
| 196 |
$url = $this->url_encode($url); |
| 197 |
|
| 198 |
/* Header*/ |
| 199 |
$headers[]='Authorization: OAuth oauth_version="1.0", oauth_signature_method="PLAINTEXT", oauth_consumer_key="'.$this->oauth_app_key.'", oauth_token="'.$this->oauth_token.'", oauth_signature="'.$this->oauth_app_secret.'&'.$this->oauth_token_secret.'"'; |
| 200 |
$headers[]='Expect:'; |
| 201 |
|
| 202 |
/* Build cURL Request */ |
| 203 |
$ch = curl_init(); |
| 204 |
if ($method == 'POST') { |
| 205 |
curl_setopt($ch, CURLOPT_POST, true); |
| 206 |
curl_setopt($ch, CURLOPT_POSTFIELDS, $args); |
| 207 |
curl_setopt($ch, CURLOPT_URL, $url); |
| 208 |
} elseif ($method == 'PUT') { |
| 209 |
curl_setopt($ch,CURLOPT_PUT,true); |
| 210 |
curl_setopt($ch,CURLOPT_INFILE,$filehandle); |
| 211 |
curl_setopt($ch,CURLOPT_INFILESIZE,$filesize); |
| 212 |
$args = (is_array($args)) ? '?'.http_build_query($args, '', '&') : $args; |
| 213 |
curl_setopt($ch, CURLOPT_URL, $url.$args); |
| 214 |
} else { |
| 215 |
$args = (is_array($args)) ? '?'.http_build_query($args, '', '&') : $args; |
| 216 |
curl_setopt($ch, CURLOPT_URL, $url.$args); |
| 217 |
} |
| 218 |
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); |
| 219 |
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
| 220 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
| 221 |
curl_setopt($ch, CURLOPT_SSLVERSION,3); |
| 222 |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); |
| 223 |
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 224 |
if (is_file(dirname(__FILE__).'/aws/lib/requestcore/cacert.pem')) |
| 225 |
curl_setopt($ch, CURLOPT_CAINFO, dirname(__FILE__).'/aws/lib/requestcore/cacert.pem'); |
| 226 |
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
| 227 |
curl_setopt($ch, CURLINFO_HEADER_OUT, true); |
| 228 |
if (!empty($this->ProgressFunction) and function_exists($this->ProgressFunction) and defined('CURLOPT_PROGRESSFUNCTION') and $method == 'PUT') { |
| 229 |
curl_setopt($ch, CURLOPT_NOPROGRESS, false); |
| 230 |
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, $this->ProgressFunction); |
| 231 |
curl_setopt($ch, CURLOPT_BUFFERSIZE, 512); |
| 232 |
} |
| 233 |
if ($echo) { |
| 234 |
echo curl_exec($ch); |
| 235 |
$output=''; |
| 236 |
} else { |
| 237 |
$content = curl_exec($ch); |
| 238 |
$output = json_decode($content, true); |
| 239 |
} |
| 240 |
$status = curl_getinfo($ch); |
| 241 |
|
| 242 |
if (isset($output['error']) or $status['http_code']>=300 or $status['http_code']<200 or curl_errno($ch)>0) { |
| 243 |
if(isset($output['error']) && is_string($output['error'])) $message = '('.$status['http_code'].') '.$output['error']; |
| 244 |
elseif(isset($output['error']['hash']) && $output['error']['hash'] != '') $message = (string) '('.$status['http_code'].') '.$output['error']['hash']; |
| 245 |
elseif (0!=curl_errno($ch)) $message = '('.curl_errno($ch).') '.curl_error($ch); |
| 246 |
elseif ($status['http_code']==304) $message = '(304) The folder contents have not changed (relies on hash parameter).'; |
| 247 |
elseif ($status['http_code']==400) $message = '(400) Bad input parameter: '.strip_tags($content); |
| 248 |
elseif ($status['http_code']==401) $message = '(401) Bad or expired token. This can happen if the user or Dropbox revoked or expired an access token. To fix, you should re-authenticate the user.'; |
| 249 |
elseif ($status['http_code']==403) $message = '(403) Bad OAuth request (wrong consumer key, bad nonce, expired timestamp, ...). Unfortunately, reauthenticating the user won\'t help here.'; |
| 250 |
elseif ($status['http_code']==404) $message = '(404) The file was not found at the specified path, or was not found at the specified rev.'; |
| 251 |
elseif ($status['http_code']==405) $message = '(405) Request method not expected (generally should be GET,PUT or POST).'; |
| 252 |
elseif ($status['http_code']==406) $message = '(406) There are too many file entries to return.'; |
| 253 |
elseif ($status['http_code']==411) $message = '(411) Chunked encoding was attempted for this upload, but is not supported by Dropbox.'; |
| 254 |
elseif ($status['http_code']==415) $message = '(415) The image is invalid and cannot be thumbnailed.'; |
| 255 |
elseif ($status['http_code']==503) $message = '(503) Your app is making too many requests and is being rate limited. 503s can trigger on a per-app or per-user basis.'; |
| 256 |
elseif ($status['http_code']==507) $message = '(507) User is over Dropbox storage quota.'; |
| 257 |
else $message = '('.$status['http_code'].') Invalid response.'; |
| 258 |
throw new DropboxException($message); |
| 259 |
} else { |
| 260 |
curl_close($ch); |
| 261 |
if (!is_array($output)) |
| 262 |
return $content; |
| 263 |
else |
| 264 |
return $output; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
private function url_encode($string) { |
| 269 |
$string = str_replace('?','%3F',$string); |
| 270 |
$string = str_replace('=','%3D',$string); |
| 271 |
$string = str_replace(' ','%20',$string); |
| 272 |
$string = str_replace('(','%28',$string); |
| 273 |
$string = str_replace(')','%29',$string); |
| 274 |
$string = str_replace('&','%26',$string); |
| 275 |
$string = str_replace('@','%40',$string); |
| 276 |
return $string; |
| 277 |
} |
| 278 |
|
| 279 |
} |
| 280 |
|
| 281 |
class DropboxException extends Exception { |
| 282 |
} |
| 283 |
?> |
| 284 |
|