1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This program is free software: you can redistribute it and/or modify |
5
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
6
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
7
|
|
|
* (at your option) any later version. |
8
|
|
|
* |
9
|
|
|
* This program is distributed in the hope that it will be useful, |
10
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12
|
|
|
* GNU Lesser General Public License for more details. |
13
|
|
|
* |
14
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
15
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace fkooman\RemoteStorage; |
19
|
|
|
|
20
|
|
|
use fkooman\RemoteStorage\Http\Exception\HttpException; |
21
|
|
|
use fkooman\RemoteStorage\Http\Request; |
22
|
|
|
use fkooman\RemoteStorage\Http\Response; |
23
|
|
|
use fkooman\RemoteStorage\OAuth\TokenInfo; |
24
|
|
|
|
25
|
|
|
class ApiModule |
26
|
|
|
{ |
27
|
|
|
/** @var RemoteStorage */ |
28
|
|
|
private $remoteStorage; |
29
|
|
|
|
30
|
|
|
/** @var string */ |
31
|
|
|
private $serverMode; |
32
|
|
|
|
33
|
|
|
public function __construct(RemoteStorage $remoteStorage, $serverMode) |
34
|
|
|
{ |
35
|
|
|
$this->remoteStorage = $remoteStorage; |
36
|
|
|
$this->serverMode = $serverMode; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Request $request |
41
|
|
|
* @param \fkooman\RemoteStorage\OAuth\TokenInfo|false $tokenInfo |
42
|
|
|
*/ |
43
|
|
|
public function get(Request $request, $tokenInfo) |
44
|
|
|
{ |
45
|
|
|
$response = $this->getObject($request, $tokenInfo); |
46
|
|
|
$this->addNoCache($response); |
47
|
|
|
$this->addCors($response); |
48
|
|
|
|
49
|
|
|
return $response; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function head(Request $request, $tokenInfo) |
53
|
|
|
{ |
54
|
|
|
// XXX return headers only? |
55
|
|
|
$response = $this->getObject($request, $tokenInfo); |
56
|
|
|
$this->addNoCache($response); |
57
|
|
|
$this->addCors($response); |
58
|
|
|
|
59
|
|
|
return $response; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function put(Request $request, TokenInfo $tokenInfo) |
63
|
|
|
{ |
64
|
|
|
$response = $this->putDocument($request, $tokenInfo); |
65
|
|
|
$this->addCors($response); |
66
|
|
|
|
67
|
|
|
return $response; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function delete(Request $request, TokenInfo $tokenInfo) |
71
|
|
|
{ |
72
|
|
|
$response = $this->deleteDocument($request, $tokenInfo); |
73
|
|
|
$this->addCors($response); |
74
|
|
|
|
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function options(Request $request) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
$response = new Response(); |
81
|
|
|
$response->addHeader( |
82
|
|
|
'Access-Control-Allow-Methods', |
83
|
|
|
'GET, PUT, DELETE, HEAD, OPTIONS' |
84
|
|
|
); |
85
|
|
|
$response->addHeader( |
86
|
|
|
'Access-Control-Allow-Headers', |
87
|
|
|
'Authorization, Content-Length, Content-Type, Origin, X-Requested-With, If-Match, If-None-Match' |
88
|
|
|
); |
89
|
|
|
$this->addCors($response); |
90
|
|
|
|
91
|
|
|
return $response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param Request $request |
96
|
|
|
* @param TokenInfo|false $tokenInfo |
97
|
|
|
*/ |
98
|
|
|
public function getObject(Request $request, $tokenInfo) |
99
|
|
|
{ |
100
|
|
|
$path = new Path($request->getPathInfo()); |
101
|
|
|
|
102
|
|
|
// allow requests to public files (GET|HEAD) without authentication |
103
|
|
|
if ($path->getIsPublic() && $path->getIsDocument()) { |
104
|
|
|
// XXX create a getPublicDocument call instead to make sure? |
105
|
|
|
return $this->getDocument($path, $request, $tokenInfo); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// past this point we MUST be authenticated |
109
|
|
|
if (false === $tokenInfo) { |
110
|
|
|
throw new HttpException( |
111
|
|
|
'no_token', |
112
|
|
|
401, |
113
|
|
|
['WWW-Authenticate' => 'Bearer realm="remoteStorage API"'] |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($path->getIsFolder()) { |
118
|
|
|
return $this->getFolder($path, $request, $tokenInfo); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->getDocument($path, $request, $tokenInfo); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getFolder(Path $path, Request $request, TokenInfo $tokenInfo) |
125
|
|
|
{ |
126
|
|
|
if ($path->getUserId() !== $tokenInfo->getUserId()) { |
127
|
|
|
throw new HttpException('path does not match authorized subject', 403); |
128
|
|
|
} |
129
|
|
|
if (!$this->hasReadScope($tokenInfo->getScope(), $path->getModuleName())) { |
130
|
|
|
throw new HttpException('path does not match authorized scope', 403); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$folderVersion = $this->remoteStorage->getVersion($path); |
134
|
|
|
if (null === $folderVersion) { |
135
|
|
|
// folder does not exist, so we just invent this |
136
|
|
|
// ETag that will be the same for all empty folders |
137
|
|
|
$folderVersion = 'e:404'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$requestedVersion = $this->stripQuotes( |
141
|
|
|
$request->getHeader('HTTP_IF_NONE_MATCH', false, null) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
View Code Duplication |
if (null !== $requestedVersion) { |
|
|
|
|
145
|
|
|
if (in_array($folderVersion, $requestedVersion)) { |
146
|
|
|
//return new RemoteStorageResponse($request, 304, $folderVersion); |
|
|
|
|
147
|
|
|
$response = new Response(304, 'application/ld+json'); |
148
|
|
|
$response->addHeader('ETag', '"'.$folderVersion.'"'); |
149
|
|
|
|
150
|
|
|
return $response; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$rsr = new Response(200, 'application/ld+json'); |
155
|
|
|
$rsr->addHeader('ETag', '"'.$folderVersion.'"'); |
156
|
|
|
|
157
|
|
|
if ('GET' === $request->getRequestMethod()) { |
158
|
|
|
$rsr->setBody( |
159
|
|
|
$this->remoteStorage->getFolder( |
160
|
|
|
$path, |
161
|
|
|
$this->stripQuotes( |
162
|
|
|
$request->getHeader('HTTP_IF_NONE_MATCH', false, null) |
163
|
|
|
) |
164
|
|
|
) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $rsr; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function getDocument(Path $path, Request $request, $tokenInfo) |
172
|
|
|
{ |
173
|
|
|
if (false !== $tokenInfo) { |
174
|
|
|
if ($path->getUserId() !== $tokenInfo->getUserId()) { |
175
|
|
|
throw new HttpException('path does not match authorized subject', 403); |
176
|
|
|
} |
177
|
|
|
if (!$this->hasReadScope($tokenInfo->getScope(), $path->getModuleName())) { |
178
|
|
|
throw new HttpException('path does not match authorized scope', 403); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
$documentVersion = $this->remoteStorage->getVersion($path); |
182
|
|
|
if (is_null($documentVersion)) { |
183
|
|
|
throw new HttpException( |
184
|
|
|
sprintf('document "%s" not found', $path->getPath()), |
185
|
|
|
404 |
186
|
|
|
); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
$requestedVersion = $this->stripQuotes( |
190
|
|
|
$request->getHeader('HTTP_IF_NONE_MATCH', false, null) |
191
|
|
|
); |
192
|
|
|
$documentContentType = $this->remoteStorage->getContentType($path); |
193
|
|
|
|
194
|
|
View Code Duplication |
if (!is_null($requestedVersion)) { |
|
|
|
|
195
|
|
|
if (in_array($documentVersion, $requestedVersion)) { |
196
|
|
|
$response = new Response(304, $documentContentType); |
197
|
|
|
$response->addHeader('ETag', '"'.$documentVersion.'"'); |
198
|
|
|
|
199
|
|
|
return $response; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$rsr = new Response(200, $documentContentType); |
204
|
|
|
$rsr->addHeader('ETag', '"'.$documentVersion.'"'); |
205
|
|
|
|
206
|
|
|
if ('development' !== $this->serverMode) { |
207
|
|
|
$rsr->addHeader('Accept-Ranges', 'bytes'); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
if ('GET' === $request->getRequestMethod()) { |
211
|
|
|
if ('development' === $this->serverMode) { |
212
|
|
|
// use body |
213
|
|
|
$rsr->setBody( |
214
|
|
|
file_get_contents( |
215
|
|
|
$this->remoteStorage->getDocument( |
216
|
|
|
$path, |
217
|
|
|
$requestedVersion |
218
|
|
|
) |
219
|
|
|
) |
220
|
|
|
); |
221
|
|
|
$rsr->addHeader('Content-Length', (string) strlen($rsr->getBody())); |
222
|
|
|
} else { |
223
|
|
|
// use X-SendFile |
224
|
|
|
$rsr->setFile( |
225
|
|
|
$this->remoteStorage->getDocument( |
226
|
|
|
$path, |
227
|
|
|
$requestedVersion |
228
|
|
|
) |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $rsr; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function putDocument(Request $request, TokenInfo $tokenInfo) |
237
|
|
|
{ |
238
|
|
|
$path = new Path($request->getPathInfo()); |
239
|
|
|
|
240
|
|
|
if ($path->getUserId() !== $tokenInfo->getUserId()) { |
241
|
|
|
throw new HttpException('path does not match authorized subject', 403); |
242
|
|
|
} |
243
|
|
|
if (!$this->hasWriteScope($tokenInfo->getScope(), $path->getModuleName())) { |
244
|
|
|
throw new HttpException('path does not match authorized scope', 403); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
// https://tools.ietf.org/html/rfc7231#section-4.3.4 |
248
|
|
|
if (!is_null($request->getHeader('HTTP_CONTENT_RANGE', false, null))) { |
249
|
|
|
throw new HttpException('PUT MUST NOT have Content-Range', 400); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
$ifMatch = $this->stripQuotes( |
253
|
|
|
$request->getHeader('HTTP_IF_MATCH', false, null) |
254
|
|
|
); |
255
|
|
|
$ifNoneMatch = $this->stripQuotes( |
256
|
|
|
$request->getHeader('HTTP_IF_NONE_MATCH', false, null) |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
$documentVersion = $this->remoteStorage->getVersion($path); |
260
|
|
View Code Duplication |
if (null !== $ifMatch && !in_array($documentVersion, $ifMatch)) { |
|
|
|
|
261
|
|
|
throw new HttpException('version mismatch', 412); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
if (null !== $ifNoneMatch && in_array('*', $ifNoneMatch) && null !== $documentVersion) { |
265
|
|
|
throw new HttpException('document already exists', 412); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
$x = $this->remoteStorage->putDocument( |
|
|
|
|
269
|
|
|
$path, |
270
|
|
|
$request->getHeader('CONTENT_TYPE'), |
271
|
|
|
$request->getBody(), |
272
|
|
|
$ifMatch, |
273
|
|
|
$ifNoneMatch |
274
|
|
|
); |
275
|
|
|
// we have to get the version again after the PUT |
276
|
|
|
$documentVersion = $this->remoteStorage->getVersion($path); |
277
|
|
|
|
278
|
|
|
$rsr = new Response(); |
279
|
|
|
$rsr->addHeader('ETag', '"'.$documentVersion.'"'); |
280
|
|
|
$rsr->setBody($x); |
281
|
|
|
|
282
|
|
|
return $rsr; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function deleteDocument(Request $request, TokenInfo $tokenInfo) |
286
|
|
|
{ |
287
|
|
|
$path = new Path($request->getPathInfo()); |
288
|
|
|
|
289
|
|
|
if ($path->getUserId() !== $tokenInfo->getUserId()) { |
290
|
|
|
throw new HttpException('path does not match authorized subject', 403); |
291
|
|
|
} |
292
|
|
|
if (!$this->hasWriteScope($tokenInfo->getScope(), $path->getModuleName())) { |
293
|
|
|
throw new HttpException('path does not match authorized scope', 403); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
// need to get the version before the delete |
297
|
|
|
$documentVersion = $this->remoteStorage->getVersion($path); |
298
|
|
|
|
299
|
|
|
$ifMatch = $this->stripQuotes( |
300
|
|
|
$request->getHeader('HTTP_IF_MATCH', false, null) |
301
|
|
|
); |
302
|
|
|
|
303
|
|
|
// if document does not exist, and we have If-Match header set we should |
304
|
|
|
// return a 412 instead of a 404 |
305
|
|
View Code Duplication |
if (null !== $ifMatch && !in_array($documentVersion, $ifMatch)) { |
|
|
|
|
306
|
|
|
throw new HttpException('version mismatch', 412); |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
if (null === $documentVersion) { |
310
|
|
|
throw new HttpException( |
311
|
|
|
sprintf('document "%s" not found', $path->getPath()), |
312
|
|
|
404 |
313
|
|
|
); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$ifMatch = $this->stripQuotes( |
317
|
|
|
$request->getHeader('HTTP_IF_MATCH', false, null) |
318
|
|
|
); |
319
|
|
View Code Duplication |
if (null !== $ifMatch && !in_array($documentVersion, $ifMatch)) { |
|
|
|
|
320
|
|
|
throw new HttpException('version mismatch', 412); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
$x = $this->remoteStorage->deleteDocument( |
|
|
|
|
324
|
|
|
$path, |
325
|
|
|
$ifMatch |
326
|
|
|
); |
327
|
|
|
$rsr = new Response(); |
328
|
|
|
$rsr->addHeader('ETag', '"'.$documentVersion.'"'); |
329
|
|
|
$rsr->setBody($x); |
330
|
|
|
|
331
|
|
|
return $rsr; |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
/** |
335
|
|
|
* ETag/If-Match/If-None-Match are always quoted, this method removes |
336
|
|
|
* the quotes. |
337
|
|
|
*/ |
338
|
|
|
public function stripQuotes($versionHeader) |
339
|
|
|
{ |
340
|
|
|
if (null === $versionHeader) { |
341
|
|
|
return; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
$versions = []; |
345
|
|
|
|
346
|
|
|
if ('*' === $versionHeader) { |
347
|
|
|
return ['*']; |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
foreach (explode(',', $versionHeader) as $v) { |
351
|
|
|
$v = trim($v); |
352
|
|
|
$startQuote = strpos($v, '"'); |
353
|
|
|
$endQuote = strrpos($v, '"'); |
354
|
|
|
$length = strlen($v); |
355
|
|
|
|
356
|
|
|
if (0 !== $startQuote || $length - 1 !== $endQuote) { |
357
|
|
|
throw new HttpException('version header must start and end with a double quote', 400); |
358
|
|
|
} |
359
|
|
|
$versions[] = substr($v, 1, $length - 2); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
return $versions; |
363
|
|
|
} |
364
|
|
|
|
365
|
|
|
private function hasReadScope($scope, $moduleName) |
366
|
|
|
{ |
367
|
|
|
$obtainedScopes = explode(' ', $scope); |
368
|
|
|
$requiredScopes = [ |
369
|
|
|
'*:r', |
370
|
|
|
'*:rw', |
371
|
|
|
sprintf('%s:%s', $moduleName, 'r'), |
372
|
|
|
sprintf('%s:%s', $moduleName, 'rw'), |
373
|
|
|
]; |
374
|
|
|
|
375
|
|
|
foreach ($requiredScopes as $requiredScope) { |
376
|
|
|
if (in_array($requiredScope, $obtainedScopes)) { |
377
|
|
|
return true; |
378
|
|
|
} |
379
|
|
|
} |
380
|
|
|
|
381
|
|
|
return false; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
private function hasWriteScope($scope, $moduleName) |
385
|
|
|
{ |
386
|
|
|
$obtainedScopes = explode(' ', $scope); |
387
|
|
|
$requiredScopes = [ |
388
|
|
|
'*:rw', |
389
|
|
|
sprintf('%s:%s', $moduleName, 'rw'), |
390
|
|
|
]; |
391
|
|
|
|
392
|
|
|
foreach ($requiredScopes as $requiredScope) { |
393
|
|
|
if (in_array($requiredScope, $obtainedScopes)) { |
394
|
|
|
return true; |
395
|
|
|
} |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return false; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
private function addCors(Response &$response) |
402
|
|
|
{ |
403
|
|
|
$response->addHeader('Access-Control-Allow-Origin', '*'); |
404
|
|
|
$response->addHeader( |
405
|
|
|
'Access-Control-Expose-Headers', |
406
|
|
|
'ETag, Content-Length' |
407
|
|
|
); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
private function addNoCache(Response &$response) |
411
|
|
|
{ |
412
|
|
|
$response->addHeader('Expires', '0'); |
413
|
|
|
$response->addHeader('Cache-Control', 'no-cache'); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.