dropbox.php
7.09 KB
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
<?php
require_once(NINJA_FORMS_UPLOADS_DIR. "/includes/lib/dropbox/API.php");
require_once(NINJA_FORMS_UPLOADS_DIR. "/includes/lib/dropbox/OAuth/Consumer/ConsumerAbstract.php");
require_once(NINJA_FORMS_UPLOADS_DIR. "/includes/lib/dropbox/OAuth/Consumer/Curl.php");
class nf_dropbox
{
const CONSUMER_KEY = 'g80jscev5iosghi';
const CONSUMER_SECRET = 'hsy0xtrr3gjkd0i';
const RETRY_COUNT = 3;
private static $instance = null;
private
$dropbox,
$request_token,
$access_token,
$oauth_state,
$oauth,
$account_info_cache,
$settings,
$directory_cache = array()
;
public function __construct()
{
$this->init();
}
public function init()
{
$this->settings = get_option( 'ninja_forms_settings' );
if (!extension_loaded('curl')) {
throw new Exception(sprintf(
__('The cURL extension is not loaded. %sPlease ensure its installed and activated.%s', 'ninja-forms-upload'),
'<a href="http://php.net/manual/en/curl.installation.php">',
'</a>'
));
}
$this->oauth = new Dropbox_OAuth_Consumer_Curl(self::CONSUMER_KEY, self::CONSUMER_SECRET);
$this->oauth_state = $this->get_option('dropbox_oauth_state');
$this->request_token = $this->get_token('request');
$this->access_token = $this->get_token('access');
if ($this->oauth_state == 'request') {
//If we have not got an access token then we need to grab one
try {
$this->oauth->setToken($this->request_token);
$this->access_token = $this->oauth->getAccessToken();
$this->oauth_state = 'access';
$this->oauth->setToken($this->access_token);
$this->save_tokens();
//Supress the error because unlink, then init should be called
} catch (Exception $e) {}
} elseif ($this->oauth_state == 'access') {
$this->oauth->setToken($this->access_token);
} else {
//If we don't have an acess token then lets setup a new request
$this->request_token = $this->oauth->getRequestToken();
$this->oauth->setToken($this->request_token);
$this->oauth_state = 'request';
$this->save_tokens();
}
$this->dropbox = new Dropbox_API($this->oauth);
}
private function get_token($type)
{
$token = $this->get_option("dropbox_{$type}_token");
$token_secret = $this->get_option("dropbox_{$type}_token_secret");
$ret = new stdClass;
$ret->oauth_token = null;
$ret->oauth_token_secret = null;
if ($token && $token_secret) {
$ret = new stdClass;
$ret->oauth_token = $token;
$ret->oauth_token_secret = $token_secret;
}
return $ret;
}
public function is_authorized()
{
try {
$this->get_account_info();
return true;
} catch (Exception $e) {
return false;
}
}
public function get_authorize_url( $callback_url )
{
return $this->oauth->getAuthoriseUrl( $callback_url );
}
public function get_account_info()
{
if (!isset($this->account_info_cache)) {
$response = $this->dropbox->accountInfo();
$this->account_info_cache = $response['body'];
}
return $this->account_info_cache;
}
private function save_tokens()
{
$this->set_option('dropbox_oauth_state', $this->oauth_state);
if ($this->request_token) {
$this->set_option('dropbox_request_token', $this->request_token->oauth_token);
$this->set_option('dropbox_request_token_secret', $this->request_token->oauth_token_secret);
} else {
$this->set_option('dropbox_request_token', null);
$this->set_option('dropbox_request_token_secret', null);
}
if ($this->access_token) {
$this->set_option('dropbox_access_token', $this->access_token->oauth_token);
$this->set_option('dropbox_access_token_secret', $this->access_token->oauth_token_secret);
} else {
$this->set_option('dropbox_access_token', null);
$this->set_option('dropbox_access_token_secret', null);
}
$this->save_options();
return $this;
}
private function get_option( $key ) {
if ( isset ( $key ) && isset( $this->settings[$key] ) ) {
return $this->settings[$key];
} else {
return false;
}
}
private function set_option( $key, $value ) {
$this->settings[$key] = $value;
}
private function save_options() {
update_option( 'ninja_forms_settings', $this->settings );
}
public function upload_file( $file, $filename, $path = '' ) {
if ( $filename === '' ) {
$filename = $this->remove_secret( $file );
}
$i = 0;
while ( $i ++ < self::RETRY_COUNT ) {
try {
return $this->dropbox->putFile( $file, $filename, $path );
} catch ( Exception $e ) {
}
}
throw $e;
}
public function get_link( $path ) {
$response = $this->dropbox->media( $path );
if ( $response['code'] == 200 ) {
return $response['body']->url;
}
return false;
}
public function chunk_upload_file($path, $file, $processed_file)
{
$offest = $upload_id = null;
if ($processed_file) {
$offest = $processed_file->offset;
$upload_id = $processed_file->uploadid;
}
return $this->dropbox->chunkedUpload($file, $this->remove_secret($file), $path, true, $offest, $upload_id);
}
public function delete_file($file)
{
return $this->dropbox->delete($file);
}
public function create_directory($path)
{
try {
$this->dropbox->create($path);
} catch (Exception $e) {}
}
public function get_directory_contents($path)
{
if (!isset($this->directory_cache[$path])) {
try {
$this->directory_cache[$path] = array();
$response = $this->dropbox->metaData($path);
foreach ($response['body']->contents as $val) {
if (!$val->is_dir) {
$this->directory_cache[$path][] = basename($val->path);
}
}
} catch (Exception $e) {
$this->create_directory($path);
}
}
return $this->directory_cache[$path];
}
public function unlink_account()
{
$this->oauth->resetToken();
$this->request_token = null;
$this->access_token = null;
$this->oauth_state = null;
return $this->save_tokens();
}
public static function remove_secret($file, $basename = true)
{
if (preg_match('/-nf-secret$/', $file))
$file = substr($file, 0, strrpos($file, '.'));
if ($basename)
return basename($file);
return $file;
}
}