|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace B2; |
|
4
|
|
|
|
|
5
|
|
|
use B2\Files\Files; |
|
6
|
|
|
use PartnerIT\Curl\Network\CurlRequest; |
|
7
|
|
|
|
|
8
|
|
|
class B2Client |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $accountId; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $applicationKey; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $apiUrl; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $authorizationToken; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $downloadUrl; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var CurlRequest |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $CurlRequest; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var Files |
|
43
|
|
|
*/ |
|
44
|
|
|
public $Files; |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* B2Client constructor. |
|
49
|
|
|
* @param string $accountId |
|
50
|
|
|
* @param string $applicationKey |
|
51
|
|
|
*/ |
|
52
|
6 |
|
public function __construct($accountId, $applicationKey, CurlRequest $curlRequest = null) |
|
53
|
|
|
{ |
|
54
|
|
|
|
|
55
|
6 |
|
if (!$curlRequest) { |
|
56
|
5 |
|
$this->CurlRequest = new CurlRequest(); |
|
57
|
5 |
|
} else { |
|
58
|
1 |
|
$this->CurlRequest = $curlRequest; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
6 |
|
$this->accountId = $accountId; |
|
62
|
6 |
|
$this->applicationKey = $applicationKey; |
|
63
|
6 |
|
$this->Files = new Files($this); |
|
64
|
|
|
|
|
65
|
6 |
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param array $result |
|
69
|
|
|
*/ |
|
70
|
3 |
|
public function setToken($result) |
|
71
|
|
|
{ |
|
72
|
3 |
|
$this->authorizationToken = $result['authorizationToken']; |
|
73
|
3 |
|
$this->apiUrl = $result['apiUrl']; |
|
74
|
3 |
|
$this->downloadUrl = $result['downloadUrl']; |
|
75
|
3 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function requestToken() |
|
81
|
|
|
{ |
|
82
|
|
|
|
|
83
|
1 |
|
$response = $this->curl('https://api.backblaze.com/b2api/v1/b2_authorize_account', 'GET', [ |
|
84
|
1 |
|
$this->buildBasicAuthHeader() |
|
85
|
1 |
|
]); |
|
86
|
|
|
|
|
87
|
1 |
|
$data = $response->getJsonData(); |
|
88
|
1 |
|
if ($response->getStatusCode() === 200) { |
|
89
|
1 |
|
$this->setToken($data); |
|
90
|
1 |
|
return true; |
|
91
|
|
|
} else { |
|
92
|
|
|
throw new \RuntimeException('Failed to get token: ' . $data['message']); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $endpoint |
|
99
|
|
|
* @param $method |
|
100
|
|
|
* @param array $data |
|
101
|
|
|
* @return mixed |
|
102
|
|
|
* @throws \Exception |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function call($endpoint, $method, $data = []) |
|
105
|
|
|
{ |
|
106
|
|
|
|
|
107
|
2 |
|
if (empty($this->authorizationToken)) { |
|
108
|
|
|
throw new \Exception('You must set or generate a token'); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$headers = [ |
|
112
|
2 |
|
$this->buildTokenAuthHeader() |
|
113
|
2 |
|
]; |
|
114
|
|
|
|
|
115
|
2 |
|
$headers[] = 'Content-Type: application/json'; |
|
116
|
2 |
|
$headers[] = "Accept: application/json"; |
|
117
|
2 |
|
$body = json_encode($data); |
|
118
|
|
|
|
|
119
|
2 |
|
$response = $this->curl($this->apiUrl . '/b2api/v1/' . $endpoint, $method, $headers, $body); |
|
120
|
|
|
|
|
121
|
2 |
|
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) { |
|
122
|
1 |
|
return $response->getJsonData(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
1 |
|
if ($response->getStatusCode() >= 400) { |
|
126
|
1 |
|
$data = $response->getJsonData(); |
|
127
|
1 |
|
throw new \RuntimeException('Error ' . $response->getStatusCode() . ' - ' . $data['message']); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param $uri |
|
134
|
|
|
* @param string $method |
|
135
|
|
|
* @param array $headers |
|
136
|
|
|
* @param null $body |
|
137
|
|
|
* @return B2Response |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function curl($uri, $method = 'GET', $headers = [], $body = null) |
|
140
|
|
|
{ |
|
141
|
|
|
|
|
142
|
1 |
|
$response = new B2Response(); |
|
143
|
|
|
|
|
144
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_URL, $uri); |
|
145
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_CUSTOMREQUEST, $method); |
|
146
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_RETURNTRANSFER, 1); |
|
147
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_POST, 1); |
|
148
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_POSTFIELDS, $body); |
|
149
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_HTTPHEADER, $headers); |
|
150
|
1 |
|
$this->CurlRequest->setOption(CURLOPT_HEADERFUNCTION, |
|
151
|
1 |
|
function ($curl, $header) use ($response) { |
|
152
|
|
|
$response->addHeader($header); |
|
153
|
|
|
return strlen($header); |
|
154
|
1 |
|
}); |
|
155
|
|
|
|
|
156
|
1 |
|
$resp = $this->CurlRequest->execute(); |
|
157
|
1 |
|
if ($this->CurlRequest->getErrorNo() !== 0) { |
|
158
|
|
|
throw new \RuntimeException('curl error ' . $this->CurlRequest->getError() . '" - Code: ' . $this->CurlRequest->getErrorNo()); |
|
159
|
|
|
} else { |
|
160
|
1 |
|
$response->setData($resp); |
|
161
|
1 |
|
$response->setStatusCode($this->CurlRequest->getInfo(CURLINFO_HTTP_CODE)); |
|
162
|
1 |
|
return $response; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return string |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function buildBasicAuthHeader() |
|
170
|
|
|
{ |
|
171
|
1 |
|
return 'Authorization: Basic ' . base64_encode($this->accountId . ':' . $this->applicationKey); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
2 |
|
public function buildTokenAuthHeader() |
|
178
|
|
|
{ |
|
179
|
2 |
|
return 'Authorization: ' . $this->authorizationToken; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param $data |
|
184
|
|
|
* @param $sha1 |
|
185
|
|
|
* @param $fileName |
|
186
|
|
|
* @param $url |
|
187
|
|
|
* @param $token |
|
188
|
|
|
*/ |
|
189
|
|
|
public function uploadData($fileData, $fileDataSha1, $fileName, $contentType, $uploadUrl, $uploadToken) |
|
190
|
|
|
{ |
|
191
|
|
|
$headers = []; |
|
192
|
|
|
$headers[] = "Authorization: " . $uploadToken; |
|
193
|
|
|
$headers[] = "X-Bz-File-Name: " . $fileName; |
|
194
|
|
|
$headers[] = "Content-Type: " . $contentType; |
|
195
|
|
|
$headers[] = "X-Bz-Content-Sha1: " . $fileDataSha1; |
|
196
|
|
|
|
|
197
|
|
|
$response = $this->curl($uploadUrl, 'POST', $headers, $fileData); |
|
198
|
|
|
return $response->getJsonData(); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* @param $url |
|
203
|
|
|
*/ |
|
204
|
|
|
public function downloadFileByName($uri) |
|
205
|
|
|
{ |
|
206
|
|
|
|
|
207
|
|
|
$uri = $this->downloadUrl . "/file/" . $uri; |
|
208
|
|
|
$headers = [ |
|
209
|
|
|
$this->buildTokenAuthHeader() |
|
210
|
|
|
]; |
|
211
|
|
|
|
|
212
|
|
|
$response = $this->curl($uri, 'GET', $headers); |
|
213
|
|
|
if ($response->getStatusCode() === 200) { |
|
214
|
|
|
return $response->getData(); |
|
215
|
|
|
} else { |
|
216
|
|
|
throw new \RuntimeException('Download failed. ' . $response->getStatusCode()); |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* @return string |
|
222
|
|
|
*/ |
|
223
|
1 |
|
public function getDownloadUrl() |
|
224
|
|
|
{ |
|
225
|
1 |
|
return $this->downloadUrl; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
} |