79 lines
3.0 KiB
PHP
79 lines
3.0 KiB
PHP
<?php
|
|
|
|
require_once("./DropBoxPhp/DropboxClient.php");
|
|
|
|
class myDropBox {
|
|
|
|
private $dropbox = null;
|
|
|
|
// costruttore
|
|
public function __construct() {
|
|
// you have to create an app at https://www.dropbox.com/developers/apps and enter details below:
|
|
$this->dropbox = new DropboxClient(
|
|
array(
|
|
'app_key' => "ft0zodv89xx804e",
|
|
'app_secret' => "ut43sn7m9wufy3s",
|
|
'app_full_access' => true
|
|
), 'it');
|
|
$this->internalLoad();
|
|
}
|
|
|
|
protected function internalLoad() {
|
|
// first try to load existing access token
|
|
$access_token = $this->load_token("access");
|
|
if (!empty($access_token)) {
|
|
$this->dropbox->SetAccessToken($access_token);
|
|
//echo "loaded access token:";
|
|
//print_r($access_token);
|
|
} elseif (!empty($_GET['auth_callback'])) { // are we coming from dropbox's auth page?
|
|
// then load our previosly created request token
|
|
$request_token = $this->load_token($_GET['oauth_token']);
|
|
if (empty($request_token))
|
|
die('Request token not found!');
|
|
// get & store access token, the request token is not needed anymore
|
|
$access_token = $this->dropbox->GetAccessToken($request_token);
|
|
$this->store_token($access_token, "access");
|
|
$this->delete_token($_GET['oauth_token']);
|
|
}
|
|
// checks if access token is required
|
|
if (!$this->dropbox->IsAuthorized()) {
|
|
// redirect user to dropbox auth page
|
|
$return_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'] . "?auth_callback=1";
|
|
$auth_url = $this->dropbox->BuildAuthorizeUrl($return_url);
|
|
$request_token = $this->dropbox->GetRequestToken();
|
|
$this->store_token($request_token, $request_token['t']);
|
|
die("Authentication required. <a href='$auth_url'>Click here.</a>");
|
|
}
|
|
}
|
|
|
|
public function UploadFile($fileToUpload, $dropBoxPath) {
|
|
$ret = $this->dropbox->UploadFile($fileToUpload, $dropBoxPath);
|
|
return true;
|
|
}
|
|
|
|
public function GetLink($dropBoxPathFile) {
|
|
return $this->dropbox->GetLink($dropBoxPathFile, false, false);
|
|
}
|
|
|
|
public function CreateFolder($dropBoxPath) {
|
|
$ret = $this->dropbox->CreateFolder($dropBoxPath);
|
|
return true;
|
|
}
|
|
|
|
private function store_token($token, $name) {
|
|
if (!file_put_contents("tokens/$name.token", serialize($token)))
|
|
die('<br />Could not store token! <b>Make sure that the directory `tokens` exists and is writable!</b>');
|
|
}
|
|
|
|
private function load_token($name) {
|
|
if (!file_exists("tokens/$name.token"))
|
|
return null;
|
|
return @unserialize(@file_get_contents("tokens/$name.token"));
|
|
}
|
|
|
|
private function delete_token($name) {
|
|
@unlink("tokens/$name.token");
|
|
}
|
|
|
|
}
|