-
Notifications
You must be signed in to change notification settings - Fork 12
/
sdk_opendrive.php
482 lines (382 loc) · 14.7 KB
/
sdk_opendrive.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
// OpenDrive SDK class.
// (C) 2016 CubicleSoft. All Rights Reserved.
// Load dependencies.
if (!class_exists("HTTP", false)) require_once str_replace("\\", "/", dirname(__FILE__)) . "/http.php";
if (!class_exists("WebBrowser", false)) require_once str_replace("\\", "/", dirname(__FILE__)) . "/web_browser.php";
// NOTE: Does not support user and group APIs at this time. Consequently, it does not support 'access_folder_id'. Feel free to start a discussion on the forums.
class OpenDrive
{
private $web, $sessionid;
const FOLDER_MODE_PRIVATE = 0;
const FOLDER_MODE_PUBLIC = 1;
const FOLDER_MODE_HIDDEN = 2;
public function __construct()
{
$this->web = new WebBrowser();
$this->sessionid = false;
}
public function Login($username, $password)
{
$options = array(
"username" => (string)$username,
"passwd" => (string)$password,
"version" => "10",
"partner_id" => ""
);
$result = $this->RunAPI("POST", "session/login.json", $options);
if (!$result["success"]) return $result;
$this->sessionid = $result["body"]["SessionID"];
return $result;
}
public function GetFolderList($folderid = "0")
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
return $this->RunAPI("GET", "folder/list.json/" . $this->sessionid . "/" . $folderid);
}
public function GetObjectIDByName($folderid, $name)
{
$result = $this->GetFolderList($folderid);
if (!$result["success"]) return $result;
$info = false;
if (isset($result["body"]["Folders"]))
{
foreach ($result["body"]["Folders"] as $info2)
{
if ($info2["Name"] === $name) $info = $info2;
}
}
if (isset($result["body"]["Files"]))
{
foreach ($result["body"]["Files"] as $info2)
{
if ($info2["Name"] === $name) $info = $info2;
}
}
return array("success" => true, "info" => $info);
}
public function CreateFolder($folderid, $name, $description = "", $mode = self::FOLDER_MODE_PRIVATE, $publicupload = false, $publicdisplay = false, $publicdownload = false)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_name" => (string)$name,
"folder_sub_parent" => (string)$folderid,
"folder_is_public" => (string)$mode,
"folder_public_upl" => (string)(int)$publicupload,
"folder_public_display" => (string)(int)$publicdisplay,
"folder_public_dnl" => (string)(int)$publicdownload,
"folder_description" => (string)$description,
);
return $this->RunAPI("POST", "folder.json", $options);
}
public function CopyFolder($srcid, $destid)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$srcid,
"dst_folder_id" => (string)$destid,
"move" => "false"
);
return $this->RunAPI("POST", "folder/move_copy.json", $options);
}
public function MoveFolder($srcid, $destid)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$srcid,
"dst_folder_id" => (string)$destid,
"move" => "true"
);
return $this->RunAPI("POST", "folder/move_copy.json", $options);
}
public function RenameFolder($id, $newname)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$id,
"folder_name" => (string)$newname
);
return $this->RunAPI("POST", "folder/rename.json", $options);
}
public function RemoveTrashedFolder($id)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$id
);
return $this->RunAPI("POST", "folder/remove.json", $options);
}
public function TrashFolder($id, $remove = false)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$id
);
$result = $this->RunAPI("POST", "folder/trash.json", $options);
if (!$result["success"]) return $result;
if ($remove) $result = $this->RemoveTrashedFolder($id);
return $result;
}
public function RestoreTrashedFolder($id)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$id
);
return $this->RunAPI("POST", "folder/restore.json", $options);
}
public function UploadFile($folderid, $filename, $dataorfp, $size = false, $fileid = false, $callback = false)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
if ($size === false) $size = (is_resource($dataorfp) ? self::RawFileSize($dataorfp) : strlen($dataorfp));
if (is_resource($dataorfp))
{
@fseek($dataorfp, 0, SEEK_SET);
$data = @fread($dataorfp, 1048576);
}
else
{
if ($size > strlen($dataorfp)) $size = strlen($dataorfp);
$data = substr($dataorfp, 0, $size);
$dataorfp = "";
}
if ($fileid === false)
{
// Create the file.
$options = array(
"session_id" => $this->sessionid,
"folder_id" => (string)$folderid,
"file_name" => (string)$filename,
"file_size" => (string)$size,
"access_folder_id" => ""
);
$result = $this->RunAPI("POST", "upload/create_file.json", $options);
if ($result["success"]) $fileid = $result["body"]["FileId"];
else
{
// Error code 409 means the file already exists.
if (!isset($result["info"]) || !isset($result["info"]["response"]) || $result["info"]["response"]["code"] != 409) return $result;
// Try to find the existing file.
$result2 = $this->GetObjectIDByName($folderid, $filename);
if (!$result2["success"]) return $result2;
if ($result2["info"] === false || !isset($result2["info"]["FileId"])) return $result;
$fileid = $result2["info"]["FileId"];
}
}
// Open the file.
$options = array(
"session_id" => $this->sessionid,
"file_id" => (string)$fileid,
"file_size" => (string)$size
);
$result = $this->RunAPI("POST", "upload/open_file_upload.json", $options);
if (!$result["success"]) return $result;
$templocation = $result["body"]["TempLocation"];
// Upload the new data to the file.
$pos = 0;
while ($data !== false && $data !== "")
{
$options = array(
"postvars" => array(
"session_id" => $this->sessionid,
"file_id" => (string)$fileid,
"temp_location" => (string)$templocation,
"chunk_offset" => $pos,
"chunk_size" => strlen($data)
),
"files" => array(
array(
"name" => "file_data",
"filename" => $filename,
"type" => "application/octet-stream",
"data" => $data
)
)
);
$result = $this->RunAPI("POST", "upload/upload_file_chunk.json", $options, 200, false);
if (!$result["success"]) return $result;
if (is_callable($callback)) call_user_func_array($callback, array($fileid, $pos, strlen($data), $size));
$pos += strlen($data);
if (is_resource($dataorfp)) $data = @fread($dataorfp, 1048576);
else $data = "";
}
// Close the file.
$options = array(
"session_id" => $this->sessionid,
"file_id" => (string)$fileid,
"temp_location" => (string)$templocation,
"file_size" => (string)$size,
"file_time" => (string)time(),
"access_folder_id" => ""
);
$result = $this->RunAPI("POST", "upload/close_file_upload.json", $options);
if (!$result["success"]) return $result;
$result["file_id"] = $fileid;
$result["file_size"] = $size;
return $result;
}
public function DownloadFile__Internal($response, $body, &$opts)
{
fwrite($opts["fp"], $body);
if (is_callable($opts["callback"])) call_user_func_array($opts["callback"], array(&$opts));
return true;
}
// Callback option only used when destination is a file.
public function DownloadFile($destfileorfp, $fileid, $callback = false)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
if ($destfileorfp === false) $options = array();
else
{
$fp = (is_resource($destfileorfp) ? $destfileorfp : fopen($destfileorfp, "wb"));
if ($fp === false) return array("success" => false, "error" => self::OD_Translate("Invalid destination filename or handle."), "errorcode" => "invalid_filename_or_handle");
$options = array(
"read_body_callback" => array($this, "DownloadFile__Internal"),
"read_body_callback_opts" => array("fp" => $fp, "fileid" => $fileid, "callback" => $callback)
);
}
$result = $this->RunAPI("GET", "download/file.json/" . urlencode($fileid) . "?session_id=" . $this->sessionid, $options, 200, true, false);
if ($destfileorfp !== false && !is_resource($destfileorfp)) fclose($fp);
return $result;
}
public function GetThumbnail($file_id)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
return $this->RunAPI("GET", "file/thumb.json/" . urlencode($file_id) . "?session_id=" . $this->sessionid, array(), 200, true, false);
}
public function CopyFileToFolder($srcfileid, $destfolderid, $overwrite = true)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"src_file_id" => (string)$srcfileid,
"dst_folder_id" => (string)$destfolderid,
"move" => "false",
"overwrite_if_exists" => ($overwrite ? "true" : "false"),
"src_access_folder_id" => "",
"dst_access_folder_id" => "",
);
return $this->RunAPI("POST", "file/move_copy.json", $options);
}
public function MoveFileToFolder($srcfileid, $destfolderid, $overwrite = true)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"src_file_id" => (string)$srcfileid,
"dst_folder_id" => (string)$destfolderid,
"move" => "true",
"overwrite_if_exists" => ($overwrite ? "true" : "false"),
"src_access_folder_id" => "",
"dst_access_folder_id" => "",
);
return $this->RunAPI("POST", "file/move_copy.json", $options);
}
public function RenameFile($id, $newname)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"file_id" => (string)$id,
"new_file_name" => (string)$newname,
"access_folder_id" => ""
);
return $this->RunAPI("POST", "file/rename.json", $options);
}
public function RemoveTrashedFile($id)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
return $this->RunAPI("DELETE", "file.json/" . $this->sessionid . "/" . $id);
}
public function TrashFile($id, $remove = false)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"file_id" => (string)$id,
"access_folder_id" => ""
);
$result = $this->RunAPI("POST", "file/trash.json", $options);
if (!$result["success"]) return $result;
if ($remove) $result = $this->RemoveTrashedFile($id);
return $result;
}
public function RestoreTrashedFile($id)
{
if ($this->sessionid === false) return array("success" => false, "error" => self::OD_Translate("Not logged into OpenDrive."), "errorcode" => "no_login");
$options = array(
"session_id" => $this->sessionid,
"file_id" => (string)$id
);
return $this->RunAPI("POST", "file/restore.json", $options);
}
public static function RawFileSize($fp)
{
$pos = 0;
$size = 1073741824;
fseek($fp, 0, SEEK_SET);
while ($size > 1)
{
fseek($fp, $size, SEEK_CUR);
if (fgetc($fp) === false)
{
fseek($fp, -$size, SEEK_CUR);
$size = (int)($size / 2);
}
else
{
fseek($fp, -1, SEEK_CUR);
$pos += $size;
}
}
while (fgetc($fp) !== false) $pos++;
return $pos;
}
private static function OD_Translate()
{
$args = func_get_args();
if (!count($args)) return "";
return call_user_func_array((defined("CS_TRANSLATE_FUNC") && function_exists(CS_TRANSLATE_FUNC) ? CS_TRANSLATE_FUNC : "sprintf"), $args);
}
private function RunAPI($method, $apipath, $options = array(), $expected = 200, $encodejson = true, $decodebody = true)
{
$options2 = array(
"method" => $method
);
if ($method === "GET")
{
foreach ($options as $key => $val) $options2[$key] = $val;
}
else
{
if ($encodejson)
{
$options2["headers"] = array("Content-Type" => "application/json");
$options2["body"] = json_encode($options);
}
else
{
$options2 = array_merge($options2, $options);
}
}
$retries = 3;
do
{
$result = $this->web->Process("https://dev.opendrive.com/api/v1/" . $apipath, $options2);
if (!$result["success"]) sleep(1);
$retries--;
} while (!$result["success"] && $retries > 0);
if (!$result["success"]) return $result;
if ($result["response"]["code"] != $expected) return array("success" => false, "error" => self::OD_Translate("Expected a %d response from OpenDrive. Received '%s'.", $expected, $result["response"]["line"]), "errorcode" => "unexpected_opendrive_response", "info" => $result);
if ($decodebody) $result["body"] = json_decode($result["body"], true);
return $result;
}
}
?>