This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
/
class.dropboxWrapper.php
291 lines (256 loc) · 7.98 KB
/
class.dropboxWrapper.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
<?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/>.
*
*/
defined('AJXP_EXEC') or die( 'Access not allowed');
require_once (AJXP_INSTALL_PATH.'/plugins/access.dropbox/dropbox-php/autoload.php');
require_once (AJXP_BIN_FOLDER.'/interface.AjxpWrapper.php');
/**
* AjxpWrapper encapsulation the PHP Dropbox client
* @package AjaXplorer_Plugins
* @subpackage Access
*/
class dropboxWrapper implements AjxpWrapper
{
/**
*
* @var Dropbox_API
*/
private static $dropbox;
private static $oauth;
private static $crtDirContent = array();
private static $crtDirIndex = 0;
private static $crtHandle;
private static $crtTmpFile;
private static $crtWritePath;
public function __construct()
{
}
public function initPath($ajxpPath)
{
$repo = ConfService::getRepository();
if (empty(self::$dropbox)) {
$consumerKey = $repo->getOption('CONSUMER_KEY');
$consumerSecret = $repo->getOption('CONSUMER_SECRET');
self::$oauth = new Dropbox_OAuth_PEAR($consumerKey, $consumerSecret);
self::$oauth->setToken($_SESSION["OAUTH_DROPBOX_TOKENS"]);
self::$dropbox = new Dropbox_API(self::$oauth);
}
$basePath = $repo->getOption("PATH");
if(empty($basePath)) $basePath = "";
$parts = AJXP_Utils::safeParseUrl($ajxpPath);
$path = $basePath."/".ltrim($parts["path"], "/");
if($path == "") return "/";
return $path;
}
public static function staticInitPath($ajxpPath)
{
$tmpObject = new dropboxWrapper();
return $tmpObject->initPath($ajxpPath);
}
protected function metadataToStat($metaEntry)
{
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Stating ", $metaEntry);
$mode = 0666;
if(intval($metaEntry["is_dir"]) == 1) $mode += 0040000;
else $mode += 0100000;
$time = strtotime($metaEntry["modified"]);
$size = intval($metaEntry["bytes"]);
$keys = array(
'dev' => 0,
'ino' => 0,
'mode' => $mode,
'nlink' => 0,
'uid' => 0,
'gid' => 0,
'rdev' => 0,
'size' => $size,
'atime' => $time,
'mtime' => $time,
'ctime' => $time,
'blksize' => 0,
'blocks' => 0
);
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Stat value", $keys);
return $keys;
}
public static function copyFileInStream($path, $stream)
{
$path = self::staticInitPath($path);
$data = self::$dropbox->getFile($path);
fwrite($stream, $data, strlen($data));
}
public static function isRemote()
{
return true;
}
public static function getRealFSReference($path)
{
$tmpFile = AJXP_Utils::getAjxpTmpDir()."/".rand();
$path = self::staticInitPath($path);
file_put_contents($tmpFile, self::$dropbox->getFile($path));
return $tmpFile;
}
public static function changeMode($path, $chmodValue)
{
}
public function rename($path_from, $path_to)
{
$path1 = $this->initPath($path_from);
$path2 = $this->initPath($path_to);
self::$dropbox->copy($path1, $path2);
self::$dropbox->delete($path1);
}
public function mkdir($path, $mode, $options)
{
$path = $this->initPath($path);
try {
self::$dropbox->createFolder($path);
} catch (Dropbox_Exception $e) {
return false;
}
return true;
}
public function rmdir($path, $options)
{
$path = $this->initPath($path);
try {
self::$dropbox->delete($path);
} catch (Dropbox_Exception $e) {
return false;
}
return true;
}
public function unlink($path)
{
$path = $this->initPath($path);
try {
self::$dropbox->delete($path);
} catch (Dropbox_Exception $e) {
return false;
}
return true;
}
public function url_stat($path, $flags)
{
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"STATING $path");
$path = $this->initPath($path);
$meta = null;
if (self::$crtDirContent != null) {
foreach (self::$crtDirContent as $metaEntry) {
if ($metaEntry["path"] == $path) {
$metaEntry = $meta;
break;
}
}
}
if (empty($meta)) {
try {
$meta = self::$dropbox->getMetaData($path);
} catch (Dropbox_Exception_NotFound $nf) {
return false;
}
}
return $this->metadataToStat($meta);
}
public function dir_opendir($path, $options)
{
$path = $this->initPath($path);
$metadata = self::$dropbox->getMetaData($path);
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"CONTENT for $path", $metadata);
self::$crtDirContent = $metadata["contents"];
if (!is_array(self::$crtDirContent)) {
return false;
}
return true;
}
public function dir_readdir()
{
//return false;
if(self::$crtDirIndex == count(self::$crtDirContent)) return false;
$meta = self::$crtDirContent[self::$crtDirIndex];
self::$crtDirIndex ++;
return basename($meta["path"]);
}
public function dir_rewinddir()
{
self::$crtDirIndex = 0;
}
public function dir_closedir()
{
self::$crtDirContent = array();
self::$crtDirIndex = 0;
}
public function stream_flush()
{
return fflush(self::$crtHandle);
}
public function stream_read($count)
{
return fread(self::$crtHandle, $count);
}
public function stream_seek($offset, $whence = SEEK_SET)
{
return fseek(self::$crtHandle, $offset, $whence);
}
public function stream_write($data)
{
return fwrite(self::$crtHandle, $data);
}
public function stream_close()
{
$res = fclose(self::$crtHandle);
if (self::$crtWritePath != null) {
$path = $this->initPath(self::$crtWritePath);
try {
$postRes = self::$dropbox->putFile($path, self::$crtTmpFile);
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Post to $path succeeded:");
} catch (Dropbox_Exception $dE) {
AJXP_Logger::debug(__CLASS__,__FUNCTION__,"Post to $path failed :".$dE->getMessage());
}
}
unlink(self::$crtTmpFile);
return $res;
}
public function stream_tell()
{
return ftell(self::$crtHandle);
}
public function stream_eof()
{
return feof(self::$crtHandle);
}
public function stream_stat()
{
return true;
}
public function stream_open($path, $mode, $options, &$opened_path)
{
if (strstr($mode, "r") !== false) {
self::$crtTmpFile = self::getRealFSReference($path);
self::$crtWritePath = null;
} else {
self::$crtTmpFile = AJXP_Utils::getAjxpTmpDir()."/".rand();
self::$crtWritePath = $path;
}
self::$crtHandle = fopen(self::$crtTmpFile, $mode);
return true;
}
}