This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
/
class.dropboxAccessDriver.php
159 lines (137 loc) · 5.75 KB
/
class.dropboxAccessDriver.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
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*
*/
namespace Pydio\Access\Driver\StreamProvider\Dropbox;
use Pydio\Access\Core\Model\Repository;
use Pydio\Access\Driver\StreamProvider\FS\fsAccessDriver;
use Pydio\Core\Exception\PydioUserAlertException;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Utils\Utils;
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Plugin to access a dropbox account
* @package AjaXplorer_Plugins
* @subpackage Access
*/
class dropboxAccessDriver extends fsAccessDriver
{
/**
* @var Repository
*/
public $repository;
public $driverConf;
protected $wrapperClassName;
protected $urlBase;
public function initRepository()
{
if (is_array($this->pluginConf)) {
$this->driverConf = $this->pluginConf;
} else {
$this->driverConf = array();
}
$this->detectStreamWrapper(true);
$this->urlBase = "pydio://".$this->repository->getId();
if (!Utils::searchIncludePath('HTTP/OAuth/Consumer.php')) {
$this->logError("Dropbox", "The PEAR HTTP_OAuth package must be installed!");
return;
}
$consumerKey = $this->repository->getOption("CONSUMER_KEY");
$consumerSecret = $this->repository->getOption("CONSUMER_SECRET");
$oauth = new \Dropbox_OAuth_PEAR($consumerKey, $consumerSecret);
// TOKENS IN SESSION?
if(!empty($_SESSION["OAUTH_DROPBOX_TOKENS"])) return;
// TOKENS IN FILE ?
$tokens = $this->getTokens();
if (!empty($tokens)) {
$_SESSION["OAUTH_DROPBOX_TOKENS"] = $tokens;
return;
}
// OAUTH NEGOCIATION
if (isset($_SESSION['DROPBOX_NEGOCIATION_STATE'])) {
$state = $_SESSION['DROPBOX_NEGOCIATION_STATE'];
} else {
$state = 1;
}
switch ($state) {
case 1 :
$tokens = $oauth->getRequestToken();
//print_r($tokens);
// Note that if you want the user to automatically redirect back, you can
// add the 'callback' argument to getAuthorizeUrl.
//echo "Step 2: You must now redirect the user to:\n";
$_SESSION['DROPBOX_NEGOCIATION_STATE'] = 2;
$_SESSION['oauth_tokens'] = $tokens;
throw new PydioUserAlertException("Please go to <a style=\"text-decoration:underline;\" target=\"_blank\" href=\"".$oauth->getAuthorizeUrl()."\">".$oauth->getAuthorizeUrl()."</a> to authorize the access to your dropbox. Then try again to switch to this workspace.");
case 2 :
$oauth->setToken($_SESSION['oauth_tokens']);
try{
$tokens = $oauth->getAccessToken();
}catch(\Exception $oauthEx){
throw new PydioUserAlertException($oauthEx->getMessage() . ". Please go to <a style=\"text-decoration:underline;\" target=\"_blank\" href=\"".$oauth->getAuthorizeUrl()."\">".$oauth->getAuthorizeUrl()."</a> to authorize the access to your dropbox. Then try again to switch to this workspace.");
}
$_SESSION['DROPBOX_NEGOCIATION_STATE'] = 3;
$_SESSION['OAUTH_DROPBOX_TOKENS'] = $tokens;
$this->setTokens($tokens);
return;
}
throw new \Exception("Impossible to find the dropbox tokens for accessing this workspace");
}
public function performChecks()
{
if (!Utils::searchIncludePath('HTTP/OAuth/Consumer.php')) {
throw new \Exception("The PEAR HTTP_OAuth package must be installed!");
}
}
public function isWriteable($dir, $type = "dir")
{
return true;
}
public function getTokens()
{
if($this->repository->getOption("DROPBOX_OAUTH_TOKENS") !== null && is_array($this->repository->getOption("DROPBOX_OAUTH_TOKENS"))){
return $this->repository->getOption("DROPBOX_OAUTH_TOKENS");
}
$repositoryId = $this->repository->getId();
if(AuthService::usersEnabled()) {
$u = AuthService::getLoggedUser();
$userId = $u->getId();
if($u->getResolveAsParent()){
$userId = $u->getParent();
}
}else {
$userId = "shared";
}
return Utils::loadSerialFile(AJXP_DATA_PATH."/plugins/access.dropbox/".$repositoryId."_".$userId."_tokens");
}
public function setTokens($oauth_tokens)
{
$repositoryId = $this->repository->getId();
if(AuthService::usersEnabled()) $userId = AuthService::getLoggedUser()->getId();
else $userId = "shared";
Utils::saveSerialFile(AJXP_DATA_PATH."/plugins/access.dropbox/".$repositoryId."_".$userId."_tokens", $oauth_tokens, true);
}
public function makeSharedRepositoryOptions($httpVars, $repository)
{
$newOptions = parent::makeSharedRepositoryOptions($httpVars, $repository);
$newOptions["DROPBOX_OAUTH_TOKENS"] = $this->getTokens();
return $newOptions;
}
}