API.php
4.87 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
<?php
namespace NF_FU_VENDOR\Polevaultweb\WPDropboxAPI;
class API
{
const API_URL_BASE = 'https://api.dropboxapi.com/2/';
const CONTENT_URL_BASE = 'https://content.dropboxapi.com/2/';
protected $access_token;
public function __construct($access_token = null)
{
$this->access_token = $access_token;
}
public function get_account_info()
{
$endpoint = 'users/get_current_account';
$args = array('headers' => array('Content-Type' => 'application/json'), 'body' => \json_encode(null));
return $this->post($endpoint, $args, \false);
}
public function put_file($file, $filename, $path)
{
$endpoint = 'files/upload';
$path = trailingslashit($path) . $filename;
$path = '/' . \ltrim($path, '/');
$data = array('path' => $path, 'mode' => 'overwrite');
$resource = @\fopen($file, 'r');
$file_size = \filesize($file);
$file_data = \fread($resource, $file_size);
$args = array('headers' => array('Content-Type' => 'application/octet-stream', 'Dropbox-API-Arg' => \json_encode($data)), 'body' => $file_data, 'timeout' => 60);
return $this->post($endpoint, $args);
}
/**
* @param string $file_data
* @param bool $close
*
* @return bool|UploadSessionCursor
*/
public function upload_session_start($file_data, $close = \false)
{
$endpoint = 'files/upload_session/start';
$arguments = \compact('close');
$args = array('headers' => array('Content-Type' => 'application/octet-stream', 'Dropbox-API-Arg' => \json_encode($arguments)), 'body' => $file_data, 'timeout' => 60);
$response = $this->post($endpoint, $args);
if (empty($response) || !isset($response->session_id)) {
return \false;
}
return new UploadSessionCursor($response->session_id, \strlen($file_data));
}
/**
* @param UploadSessionCursor $cursor
* @param string $file_data
* @param bool $close
*
* @return bool|mixed
*/
public function upload_session_append($cursor, $file_data, $close = \false)
{
$endpoint = 'files/upload_session/append_v2';
$arguments = \compact('cursor', 'close');
$args = array('headers' => array('Content-Type' => 'application/octet-stream', 'Dropbox-API-Arg' => \json_encode($arguments)), 'body' => $file_data, 'timeout' => 60);
$response = $this->post($endpoint, $args);
$cursor->offset += \strlen($file_data);
return $cursor;
}
/**
* @param UploadSessionCursor $cursor
* @param string $path
* @param string $mode
* @param bool $autorename
* @param bool $mute
*
* @return bool
*/
public function upload_session_finish($cursor, $path, $mode = 'add', $autorename = \false, $mute = \false)
{
$endpoint = 'files/upload_session/finish';
$arguments = \compact('cursor');
$arguments['commit'] = \compact('path', 'mode', 'autorename', 'mute');
$args = array('headers' => array('Content-Type' => 'application/octet-stream', 'Dropbox-API-Arg' => \json_encode($arguments)), 'timeout' => 60);
$response = $this->post($endpoint, $args);
return $response;
}
public function get_url($path)
{
$endpoint = 'files/get_temporary_link';
$path = '/' . \ltrim($path, '/');
$args = array('headers' => array('Content-Type' => 'application/json'), 'body' => \json_encode(array('path' => $path)));
return $this->post($endpoint, $args, \false);
}
public function access_token_upgrade($app_key, $app_secret, $token, $token_secret)
{
$endpoint = 'auth/token/from_oauth1';
$args = array('headers' => array('Content-Type' => 'application/json', 'Authorization' => 'Basic ' . \base64_encode($app_key . ':' . $app_secret)), 'body' => \json_encode(array('oauth1_token' => $token, 'oauth1_token_secret' => $token_secret)));
$new_token = $this->post($endpoint, $args, \false, \false);
if ($new_token && isset($new_token->oauth2_token)) {
return $new_token->oauth2_token;
}
return \false;
}
protected function post($endpoint, $args, $content_api = \true, $auth = \true)
{
$url = ($content_api ? self::CONTENT_URL_BASE : self::API_URL_BASE) . $endpoint;
$defaults = array();
if ($auth) {
$defaults = array('headers' => array('Authorization' => 'Bearer ' . $this->access_token));
}
$args = \array_merge_recursive($args, $defaults);
$result = wp_remote_post($url, $args);
$response = wp_remote_retrieve_body($result);
if (empty($response)) {
return \false;
}
$body = \json_decode($response);
if (empty($body)) {
return \false;
}
return $body;
}
}