1
|
|
|
<?php |
2
|
|
|
namespace Dkd\PhpCmis\Bindings\Browser; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of php-cmis-lib. |
6
|
|
|
* |
7
|
|
|
* (c) Sascha Egerer <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
use Dkd\PhpCmis\Bindings\BindingSessionInterface; |
14
|
|
|
use Dkd\PhpCmis\Bindings\CmisBindingsHelper; |
15
|
|
|
use Dkd\PhpCmis\Bindings\LinkAccessInterface; |
16
|
|
|
use Dkd\PhpCmis\Constants; |
17
|
|
|
use Dkd\PhpCmis\Data\AclInterface; |
18
|
|
|
use Dkd\PhpCmis\Data\PropertiesInterface; |
19
|
|
|
use Dkd\PhpCmis\DataObjects\RepositoryInfo; |
20
|
|
|
use Dkd\PhpCmis\DataObjects\RepositoryInfoBrowserBinding; |
21
|
|
|
use Dkd\PhpCmis\Definitions\TypeDefinitionInterface; |
22
|
|
|
use Dkd\PhpCmis\Enum\DateTimeFormat; |
23
|
|
|
use Dkd\PhpCmis\Exception\CmisBaseException; |
24
|
|
|
use Dkd\PhpCmis\Exception\CmisConnectionException; |
25
|
|
|
use Dkd\PhpCmis\Exception\CmisConstraintException; |
26
|
|
|
use Dkd\PhpCmis\Exception\CmisInvalidArgumentException; |
27
|
|
|
use Dkd\PhpCmis\Exception\CmisNotSupportedException; |
28
|
|
|
use Dkd\PhpCmis\Exception\CmisObjectNotFoundException; |
29
|
|
|
use Dkd\PhpCmis\Exception\CmisPermissionDeniedException; |
30
|
|
|
use Dkd\PhpCmis\Exception\CmisProxyAuthenticationException; |
31
|
|
|
use Dkd\PhpCmis\Exception\CmisRuntimeException; |
32
|
|
|
use Dkd\PhpCmis\Exception\CmisUnauthorizedException; |
33
|
|
|
use Dkd\PhpCmis\SessionParameter; |
34
|
|
|
use Guzzle\Stream\StreamInterface; |
35
|
|
|
use GuzzleHttp\Exception\RequestException; |
36
|
|
|
use League\Url\Url; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Base class for all Browser Binding client services. |
40
|
|
|
*/ |
41
|
|
|
abstract class AbstractBrowserBindingService implements LinkAccessInterface |
42
|
|
|
{ |
43
|
|
|
/** |
44
|
|
|
* @var BindingSessionInterface |
45
|
|
|
*/ |
46
|
|
|
protected $session; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var boolean |
50
|
|
|
*/ |
51
|
|
|
protected $succinct; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var CmisBindingsHelper |
55
|
|
|
*/ |
56
|
|
|
protected $cmisBindingsHelper; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var DateTimeFormat |
60
|
|
|
*/ |
61
|
|
|
protected $dateTimeFormat; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param BindingSessionInterface $session |
65
|
|
|
* @param CmisBindingsHelper|null $cmisBindingsHelper |
66
|
|
|
*/ |
67
|
168 |
|
public function __construct(BindingSessionInterface $session, $cmisBindingsHelper = null) |
68
|
|
|
{ |
69
|
168 |
|
$this->setCmisBindingsHelper($cmisBindingsHelper); |
70
|
168 |
|
$this->setSession($session); |
71
|
168 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set cmis binding helper property |
75
|
|
|
* |
76
|
|
|
* @param CmisBindingsHelper|null $cmisBindingsHelper The cmis binding helper that should be defined. |
77
|
|
|
* If <code>null</code> is given a new instance of CmisBindingsHelper will be created. |
78
|
|
|
*/ |
79
|
168 |
|
protected function setCmisBindingsHelper($cmisBindingsHelper = null) |
80
|
|
|
{ |
81
|
168 |
|
$this->cmisBindingsHelper = ($cmisBindingsHelper === null) ? new CmisBindingsHelper() : $cmisBindingsHelper; |
82
|
168 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the url for an object |
86
|
|
|
* |
87
|
|
|
* @param string $repositoryId |
88
|
|
|
* @param string $objectId |
89
|
|
|
* @param string|null $selector |
90
|
|
|
* @throws CmisConnectionException |
91
|
|
|
* @throws CmisObjectNotFoundException |
92
|
|
|
* @return Url |
93
|
|
|
*/ |
94
|
2 |
View Code Duplication |
protected function getObjectUrl($repositoryId, $objectId, $selector = null) |
95
|
|
|
{ |
96
|
2 |
|
$result = $this->getRepositoryUrlCache()->getObjectUrl($repositoryId, $objectId, $selector); |
97
|
|
|
|
98
|
2 |
|
if ($result === null) { |
99
|
1 |
|
$this->getRepositoriesInternal($repositoryId); |
100
|
1 |
|
$result = $this->getRepositoryUrlCache()->getObjectUrl($repositoryId, $objectId, $selector); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
2 |
|
if ($result === null) { |
104
|
1 |
|
throw new CmisObjectNotFoundException( |
105
|
1 |
|
sprintf( |
106
|
1 |
|
'Unknown Object! Repository: "%s" | Object: "%s" | Selector: "%s"', |
107
|
1 |
|
$repositoryId, |
108
|
1 |
|
$objectId, |
109
|
|
|
$selector |
110
|
1 |
|
) |
111
|
1 |
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns the repository URL cache or creates a new cache if it doesn't |
119
|
|
|
* exist. |
120
|
|
|
* |
121
|
|
|
* @return RepositoryUrlCache |
122
|
|
|
*/ |
123
|
2 |
|
protected function getRepositoryUrlCache() |
124
|
|
|
{ |
125
|
2 |
|
$repositoryUrlCache = $this->getSession()->get(SessionParameter::REPOSITORY_URL_CACHE); |
126
|
2 |
|
if ($repositoryUrlCache === null) { |
127
|
1 |
|
$repositoryUrlCache = new RepositoryUrlCache(); |
128
|
1 |
|
$this->getSession()->put(SessionParameter::REPOSITORY_URL_CACHE, $repositoryUrlCache); |
129
|
1 |
|
} |
130
|
|
|
|
131
|
2 |
|
return $repositoryUrlCache; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get current session |
136
|
|
|
* |
137
|
|
|
* @return BindingSessionInterface |
138
|
|
|
*/ |
139
|
66 |
|
public function getSession() |
140
|
|
|
{ |
141
|
66 |
|
return $this->session; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Sets the current session. |
146
|
|
|
* |
147
|
|
|
* @param BindingSessionInterface $session |
148
|
|
|
*/ |
149
|
168 |
|
protected function setSession(BindingSessionInterface $session) |
150
|
|
|
{ |
151
|
168 |
|
$this->session = $session; |
152
|
168 |
|
$succinct = $session->get(SessionParameter::BROWSER_SUCCINCT); |
153
|
168 |
|
$this->succinct = ($succinct === null ? true : (boolean) $succinct); |
154
|
|
|
|
155
|
168 |
|
$this->dateTimeFormat = DateTimeFormat::cast($session->get(SessionParameter::BROWSER_DATETIME_FORMAT)); |
156
|
168 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Retrieves the the repository info objects. |
160
|
|
|
* |
161
|
|
|
* @param string|null $repositoryId |
162
|
|
|
* @throws CmisConnectionException |
163
|
|
|
* @return RepositoryInfo[] Returns ALL Repository Infos that are available and not just the one requested by id. |
164
|
|
|
*/ |
165
|
8 |
|
protected function getRepositoriesInternal($repositoryId = null) |
166
|
|
|
{ |
167
|
8 |
|
$repositoryUrlCache = $this->getRepositoryUrlCache(); |
168
|
|
|
|
169
|
8 |
|
if ($repositoryId === null) { |
170
|
|
|
// no repository id provided -> get all |
171
|
6 |
|
$url = $repositoryUrlCache->buildUrl($this->getServiceUrl()); |
172
|
6 |
|
} else { |
173
|
|
|
// use URL of the specified repository |
174
|
2 |
|
$url = $repositoryUrlCache->getRepositoryUrl($repositoryId, Constants::SELECTOR_REPOSITORY_INFO); |
175
|
2 |
|
if ($url === null) { |
176
|
|
|
// repository infos haven't been fetched yet -> get them all |
177
|
1 |
|
$url = $repositoryUrlCache->buildUrl($this->getServiceUrl()); |
178
|
1 |
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
8 |
|
$repositoryInfos = array(); |
182
|
8 |
|
$result = $this->read($url)->json(); |
183
|
8 |
|
if (!is_array($result)) { |
184
|
1 |
|
throw new CmisConnectionException( |
185
|
1 |
|
'Could not fetch repository info! Response is not a valid JSON.', |
186
|
|
|
1416343166 |
187
|
1 |
|
); |
188
|
|
|
} |
189
|
7 |
|
foreach ($result as $item) { |
190
|
7 |
|
if (is_array($item)) { |
191
|
6 |
|
$repositoryInfo = $this->getJsonConverter()->convertRepositoryInfo($item); |
192
|
|
|
|
193
|
6 |
|
if ($repositoryInfo instanceof RepositoryInfoBrowserBinding) { |
194
|
6 |
|
$id = $repositoryInfo->getId(); |
195
|
6 |
|
$repositoryUrl = $repositoryInfo->getRepositoryUrl(); |
196
|
6 |
|
$rootUrl = $repositoryInfo->getRootUrl(); |
197
|
|
|
|
198
|
6 |
|
if (empty($id) || empty($repositoryUrl) || empty($rootUrl)) { |
199
|
3 |
|
throw new CmisConnectionException( |
200
|
3 |
|
sprintf('Found invalid Repository Info! (id: %s)', $id), |
201
|
|
|
1415187765 |
202
|
3 |
|
); |
203
|
|
|
} |
204
|
|
|
|
205
|
3 |
|
$this->getRepositoryUrlCache()->addRepository($id, $repositoryUrl, $rootUrl); |
206
|
|
|
|
207
|
3 |
|
$repositoryInfos[] = $repositoryInfo; |
208
|
3 |
|
} |
209
|
3 |
|
} else { |
210
|
1 |
|
throw new CmisConnectionException( |
211
|
1 |
|
sprintf( |
212
|
|
|
'Found invalid repository info! Value of type "array" was expected' |
213
|
1 |
|
. 'but value of type "%s" was given.', |
214
|
1 |
|
gettype($item) |
215
|
1 |
|
), |
216
|
|
|
1415187764 |
217
|
1 |
|
); |
218
|
|
|
} |
219
|
3 |
|
} |
220
|
|
|
|
221
|
3 |
|
return $repositoryInfos; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns the service URL of this session. |
226
|
|
|
* |
227
|
|
|
* @return string|null |
228
|
|
|
*/ |
229
|
2 |
|
protected function getServiceUrl() |
230
|
|
|
{ |
231
|
2 |
|
$browserUrl = $this->getSession()->get(SessionParameter::BROWSER_URL); |
232
|
2 |
|
if (is_string($browserUrl)) { |
233
|
1 |
|
return $browserUrl; |
234
|
|
|
} |
235
|
|
|
|
236
|
1 |
|
return null; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Do a get request for the given url |
241
|
|
|
* |
242
|
|
|
* @param Url $url |
243
|
|
|
* @return \GuzzleHttp\Message\Response |
244
|
|
|
* @throws CmisBaseException an more specific exception of this type could be thrown. For more details see |
245
|
|
|
* @see AbstractBrowserBindingService::convertStatusCode() |
246
|
|
|
*/ |
247
|
3 |
|
protected function read(Url $url) |
248
|
|
|
{ |
249
|
|
|
/** @var \GuzzleHttp\Message\Response $response */ |
250
|
|
|
try { |
251
|
3 |
|
$response = $this->getHttpInvoker()->get((string) $url); |
252
|
3 |
|
} catch (RequestException $exception) { |
253
|
2 |
|
$code = 0; |
254
|
2 |
|
$message = null; |
255
|
2 |
|
if ($exception->getResponse()) { |
256
|
1 |
|
$code = $exception->getResponse()->getStatusCode(); |
257
|
1 |
|
$message = $exception->getResponse()->getBody(); |
258
|
1 |
|
} |
259
|
2 |
|
throw $this->convertStatusCode( |
260
|
2 |
|
$code, |
261
|
2 |
|
(string) $message, |
262
|
|
|
$exception |
263
|
2 |
|
); |
264
|
|
|
} |
265
|
|
|
|
266
|
1 |
|
return $response; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get a HTTP Invoker instance |
271
|
|
|
* |
272
|
|
|
* @return \GuzzleHttp\Client |
273
|
|
|
*/ |
274
|
4 |
|
protected function getHttpInvoker() |
275
|
|
|
{ |
276
|
|
|
/** @var \GuzzleHttp\Client $invoker */ |
277
|
4 |
|
$invoker = $this->cmisBindingsHelper->getHttpInvoker($this->getSession()); |
278
|
|
|
|
279
|
4 |
|
return $invoker; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Converts an error message or a HTTP status code into an Exception. |
284
|
|
|
* |
285
|
|
|
* @see http://docs.oasis-open.org/cmis/CMIS/v1.1/os/CMIS-v1.1-os.html#x1-551021r549 |
286
|
|
|
* |
287
|
|
|
* @param integer $code |
288
|
|
|
* @param string $message |
289
|
|
|
* @param null|\Exception $exception |
290
|
|
|
* @return CmisBaseException |
291
|
|
|
*/ |
292
|
26 |
|
protected function convertStatusCode($code, $message, \Exception $exception = null) |
293
|
|
|
{ |
294
|
26 |
|
$messageData = json_decode($message, true); |
295
|
|
|
|
296
|
26 |
|
if (is_array($messageData) && !empty($messageData[JSONConstants::ERROR_EXCEPTION])) { |
297
|
13 |
|
$jsonError = $messageData[JSONConstants::ERROR_EXCEPTION]; |
298
|
|
|
|
299
|
13 |
|
if (!empty($messageData[JSONConstants::ERROR_MESSAGE]) |
300
|
13 |
|
&& is_string($messageData[JSONConstants::ERROR_MESSAGE]) |
301
|
13 |
|
) { |
302
|
13 |
|
$message = $messageData[JSONConstants::ERROR_MESSAGE]; |
303
|
13 |
|
} |
304
|
|
|
|
305
|
13 |
|
$exceptionName = '\\Dkd\\PhpCmis\\Exception\\Cmis' . ucfirst($jsonError) . 'Exception'; |
306
|
|
|
|
307
|
13 |
|
if (class_exists($exceptionName)) { |
308
|
12 |
|
return new $exceptionName($message, null, $exception); |
309
|
|
|
} |
310
|
1 |
|
} |
311
|
|
|
|
312
|
14 |
|
if (empty($message) && $exception !== null) { |
313
|
|
|
$message = $exception->getMessage(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
// fall back to status code |
317
|
|
|
switch ($code) { |
318
|
14 |
|
case 301: |
319
|
14 |
|
case 302: |
320
|
14 |
|
case 303: |
321
|
14 |
|
case 307: |
322
|
4 |
|
return new CmisConnectionException( |
323
|
4 |
|
'Redirects are not supported (HTTP status code ' . $code . '): ' . $message, |
324
|
4 |
|
null, |
325
|
|
|
$exception |
326
|
4 |
|
); |
327
|
10 |
|
case 400: |
328
|
1 |
|
return new CmisInvalidArgumentException($message, null, $exception); |
329
|
9 |
|
case 401: |
330
|
1 |
|
return new CmisUnauthorizedException($message, null, $exception); |
331
|
8 |
|
case 403: |
332
|
1 |
|
return new CmisPermissionDeniedException($message, null, $exception); |
333
|
7 |
|
case 404: |
334
|
1 |
|
return new CmisObjectNotFoundException($message, null, $exception); |
335
|
6 |
|
case 405: |
336
|
1 |
|
return new CmisNotSupportedException($message, null, $exception); |
337
|
5 |
|
case 407: |
338
|
1 |
|
return new CmisProxyAuthenticationException($message, null, $exception); |
339
|
4 |
|
case 409: |
340
|
1 |
|
return new CmisConstraintException($message, null, $exception); |
341
|
3 |
|
default: |
342
|
3 |
|
return new CmisRuntimeException($message, null, $exception); |
343
|
3 |
|
} |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
// ---- helpers ---- |
347
|
|
|
|
348
|
|
|
/** |
349
|
|
|
* Returns JSON Converter instance |
350
|
|
|
* |
351
|
|
|
* @return \Dkd\PhpCmis\Converter\JsonConverter |
352
|
|
|
*/ |
353
|
65 |
|
protected function getJsonConverter() |
354
|
|
|
{ |
355
|
65 |
|
return $this->cmisBindingsHelper->getJsonConverter($this->getSession()); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
/** |
359
|
|
|
* Generate url for a given path of a given repository. |
360
|
|
|
* |
361
|
|
|
* @param string $repositoryId |
362
|
|
|
* @param string $path |
363
|
|
|
* @param string|null $selector |
364
|
|
|
* @throws CmisConnectionException |
365
|
|
|
* @throws CmisObjectNotFoundException |
366
|
|
|
* @return Url |
367
|
|
|
*/ |
368
|
2 |
View Code Duplication |
protected function getPathUrl($repositoryId, $path, $selector = null) |
369
|
|
|
{ |
370
|
2 |
|
$result = $this->getRepositoryUrlCache()->getPathUrl($repositoryId, $path, $selector); |
371
|
|
|
|
372
|
2 |
|
if ($result === null) { |
373
|
1 |
|
$this->getRepositoriesInternal($repositoryId); |
374
|
1 |
|
$result = $this->getRepositoryUrlCache()->getPathUrl($repositoryId, $path, $selector); |
375
|
1 |
|
} |
376
|
|
|
|
377
|
2 |
|
if ($result === null) { |
378
|
1 |
|
throw new CmisObjectNotFoundException( |
379
|
1 |
|
sprintf( |
380
|
1 |
|
'Unknown path! Repository: "%s" | Path: "%s" | Selector: "%s"', |
381
|
1 |
|
$repositoryId, |
382
|
1 |
|
$path, |
383
|
|
|
$selector |
384
|
1 |
|
) |
385
|
1 |
|
); |
386
|
|
|
} |
387
|
|
|
|
388
|
1 |
|
return $result; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
// ---- URL ---- |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Get if succinct mode is used |
395
|
|
|
* |
396
|
|
|
* @return boolean |
397
|
|
|
*/ |
398
|
46 |
|
protected function getSuccinct() |
399
|
|
|
{ |
400
|
46 |
|
return $this->succinct; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* Performs a POST on an URL, checks the response code and returns the |
405
|
|
|
* result. |
406
|
|
|
* |
407
|
|
|
* @param Url $url Request url |
408
|
|
|
* @param resource|string|StreamInterface|array $content Entity body data or an array for POST fields and files |
409
|
|
|
* @param array $headers Additional header options |
410
|
|
|
* @return \GuzzleHttp\Message\Response |
411
|
|
|
* @throws CmisBaseException an more specific exception of this type could be thrown. For more details see |
412
|
|
|
* @see AbstractBrowserBindingService::convertStatusCode() |
413
|
|
|
*/ |
414
|
2 |
|
protected function post(Url $url, $content = array(), array $headers = array()) |
415
|
|
|
{ |
416
|
2 |
|
$headers['body'] = $content; |
417
|
|
|
|
418
|
|
|
try { |
419
|
|
|
/** @var \GuzzleHttp\Message\Response $response */ |
420
|
2 |
|
$response = $this->getHttpInvoker()->post((string) $url, $headers); |
421
|
2 |
|
} catch (RequestException $exception) { |
422
|
1 |
|
throw $this->convertStatusCode( |
423
|
1 |
|
$exception->getResponse()->getStatusCode(), |
424
|
1 |
|
(string) $exception->getResponse()->getBody(), |
425
|
|
|
$exception |
426
|
1 |
|
); |
427
|
|
|
} |
428
|
|
|
|
429
|
1 |
|
return $response; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
/** |
433
|
|
|
* Retrieves a type definition. |
434
|
|
|
* |
435
|
|
|
* @param string $repositoryId |
436
|
|
|
* @param string $typeId |
437
|
|
|
* @return TypeDefinitionInterface|null |
438
|
|
|
* @throws CmisInvalidArgumentException if repository id or type id is <code>null</code> |
439
|
|
|
*/ |
440
|
3 |
|
protected function getTypeDefinitionInternal($repositoryId, $typeId) |
441
|
|
|
{ |
442
|
3 |
|
if (empty($repositoryId)) { |
443
|
1 |
|
throw new CmisInvalidArgumentException('Repository id must not be empty!'); |
444
|
|
|
} |
445
|
|
|
|
446
|
2 |
|
if (empty($typeId)) { |
447
|
1 |
|
throw new CmisInvalidArgumentException('Type id must not be empty!'); |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
// build URL |
451
|
1 |
|
$url = $this->getRepositoryUrl($repositoryId, Constants::SELECTOR_TYPE_DEFINITION); |
452
|
1 |
|
$url->getQuery()->modify(array(Constants::PARAM_TYPE_ID => $typeId)); |
453
|
|
|
|
454
|
1 |
|
return $this->getJsonConverter()->convertTypeDefinition($this->read($url)->json()); |
455
|
|
|
} |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* Get url for a repository |
459
|
|
|
* |
460
|
|
|
* @param string $repositoryId |
461
|
|
|
* @param string|null $selector |
462
|
|
|
* @throws CmisConnectionException |
463
|
|
|
* @throws CmisObjectNotFoundException |
464
|
|
|
* @return Url |
465
|
|
|
*/ |
466
|
2 |
View Code Duplication |
protected function getRepositoryUrl($repositoryId, $selector = null) |
467
|
|
|
{ |
468
|
2 |
|
$result = $this->getRepositoryUrlCache()->getRepositoryUrl($repositoryId, $selector); |
469
|
|
|
|
470
|
2 |
|
if ($result === null) { |
471
|
1 |
|
$this->getRepositoriesInternal($repositoryId); |
472
|
1 |
|
$result = $this->getRepositoryUrlCache()->getRepositoryUrl($repositoryId, $selector); |
473
|
1 |
|
} |
474
|
|
|
|
475
|
2 |
|
if ($result === null) { |
476
|
1 |
|
throw new CmisObjectNotFoundException( |
477
|
1 |
|
sprintf( |
478
|
1 |
|
'Unknown repository! Repository: "%s" | Selector: "%s"', |
479
|
1 |
|
$repositoryId, |
480
|
|
|
$selector |
481
|
1 |
|
) |
482
|
1 |
|
); |
483
|
|
|
} |
484
|
|
|
|
485
|
1 |
|
return $result; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* Converts a Properties list into an array that can be used for the CMIS request. |
490
|
|
|
* |
491
|
|
|
* @param PropertiesInterface $properties |
492
|
|
|
* @return array Example <code> |
493
|
|
|
* array('propertyId' => array(0 => 'myId'), 'propertyValue' => array(0 => 'valueOfMyId')) |
494
|
|
|
* </code> |
495
|
|
|
*/ |
496
|
13 |
|
protected function convertPropertiesToQueryArray(PropertiesInterface $properties) |
497
|
|
|
{ |
498
|
13 |
|
$propertiesArray = array(); |
499
|
|
|
|
500
|
13 |
|
$propertyCounter = 0; |
501
|
13 |
|
$propertiesArray[Constants::CONTROL_PROP_ID] = array(); |
502
|
13 |
|
$propertiesArray[Constants::CONTROL_PROP_VALUE] = array(); |
503
|
13 |
|
foreach ($properties->getProperties() as $property) { |
504
|
13 |
|
$propertiesArray[Constants::CONTROL_PROP_ID][$propertyCounter] = $property->getId(); |
505
|
|
|
|
506
|
13 |
|
$propertyValues = $property->getValues(); |
507
|
|
|
|
508
|
13 |
|
if (count($propertyValues) === 1) { |
509
|
13 |
|
$propertiesArray[Constants::CONTROL_PROP_VALUE][$propertyCounter] = |
510
|
13 |
|
$this->convertPropertyValueToSimpleType( |
511
|
13 |
|
$property->getFirstValue() |
512
|
13 |
|
); |
513
|
13 |
|
} elseif (count($propertyValues) > 1) { |
514
|
1 |
|
$propertyValueCounter = 0; |
515
|
1 |
|
$propertiesArray[Constants::CONTROL_PROP_VALUE][$propertyCounter] = array(); |
516
|
1 |
|
foreach ($propertyValues as $propertyValue) { |
517
|
1 |
|
$propertiesArray[Constants::CONTROL_PROP_VALUE][$propertyCounter][$propertyValueCounter] = |
518
|
1 |
|
$this->convertPropertyValueToSimpleType( |
519
|
|
|
$propertyValue |
520
|
1 |
|
); |
521
|
1 |
|
$propertyValueCounter ++; |
522
|
1 |
|
} |
523
|
1 |
|
} |
524
|
|
|
|
525
|
13 |
|
$propertyCounter ++; |
526
|
13 |
|
} |
527
|
|
|
|
528
|
13 |
|
return $propertiesArray; |
529
|
|
|
} |
530
|
|
|
|
531
|
|
|
/** |
532
|
|
|
* Converts values to a format that can be used for the CMIS Browser binding request. |
533
|
|
|
* |
534
|
|
|
* @param mixed $value |
535
|
|
|
* @return mixed |
536
|
|
|
*/ |
537
|
13 |
|
protected function convertPropertyValueToSimpleType($value) |
538
|
|
|
{ |
539
|
13 |
|
if ($value instanceof \DateTime) { |
540
|
|
|
// CMIS expects a timestamp in milliseconds |
541
|
1 |
|
$value = $value->getTimestamp() * 1000; |
542
|
13 |
|
} elseif (is_bool($value)) { |
543
|
|
|
// Booleans must be represented in string form since request will fail if cast to integer |
544
|
1 |
|
$value = $value ? 'true' : 'false'; |
545
|
1 |
|
} |
546
|
|
|
|
547
|
13 |
|
return $value; |
548
|
|
|
} |
549
|
|
|
|
550
|
|
|
/** |
551
|
|
|
* Converts a Access Control list into an array that can be used for the CMIS request |
552
|
|
|
* |
553
|
|
|
* @param AclInterface $acl |
554
|
|
|
* @param string $principalControl one of principal ace constants |
555
|
|
|
* CONTROL_ADD_ACE_PRINCIPAL or CONTROL_REMOVE_ACE_PRINCIPAL |
556
|
|
|
* @param string $permissionControl one of permission ace constants |
557
|
|
|
* CONTROL_REMOVE_ACE_PRINCIPAL or CONTROL_REMOVE_ACE_PERMISSION |
558
|
|
|
* @return array Example <code> |
559
|
|
|
* array('addACEPrincipal' => array(0 => 'principalId'), |
560
|
|
|
* 'addACEPermission' => array(0 => array(0 => 'permissonValue'))) |
561
|
|
|
* </code> |
562
|
|
|
*/ |
563
|
7 |
|
protected function convertAclToQueryArray(AclInterface $acl, $principalControl, $permissionControl) |
564
|
|
|
{ |
565
|
7 |
|
$acesArray = array(); |
566
|
7 |
|
$principalCounter = 0; |
567
|
|
|
|
568
|
7 |
|
foreach ($acl->getAces() as $ace) { |
569
|
7 |
|
$permissions = $ace->getPermissions(); |
570
|
7 |
|
if ($ace->getPrincipal() !== null && $ace->getPrincipal()->getId() && !empty($permissions)) { |
571
|
7 |
|
$acesArray[$principalControl][$principalCounter] = $ace->getPrincipal()->getId(); |
572
|
7 |
|
$permissionCounter = 0; |
573
|
7 |
|
$acesArray[$permissionControl][$principalCounter] = array(); |
574
|
|
|
|
575
|
7 |
|
foreach ($permissions as $permission) { |
576
|
7 |
|
$acesArray[$permissionControl][$principalCounter][$permissionCounter] = $permission; |
577
|
7 |
|
$permissionCounter ++; |
578
|
7 |
|
} |
579
|
|
|
|
580
|
7 |
|
$principalCounter ++; |
581
|
7 |
|
} |
582
|
7 |
|
} |
583
|
|
|
|
584
|
7 |
|
return $acesArray; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
/** |
588
|
|
|
* Converts a policies array into an array that can be used for the CMIS request |
589
|
|
|
* |
590
|
|
|
* @param string[] $policies A list of policy string representations |
591
|
|
|
* @return array |
592
|
|
|
*/ |
593
|
8 |
|
protected function convertPolicyIdArrayToQueryArray(array $policies) |
594
|
|
|
{ |
595
|
8 |
|
$policiesArray = array(); |
596
|
8 |
|
$policyCounter = 0; |
597
|
|
|
|
598
|
8 |
|
foreach ($policies as $policy) { |
599
|
6 |
|
$policiesArray[Constants::CONTROL_POLICY][$policyCounter] = (string) $policy; |
600
|
6 |
|
$policyCounter ++; |
601
|
8 |
|
} |
602
|
|
|
|
603
|
8 |
|
return $policiesArray; |
604
|
|
|
} |
605
|
|
|
|
606
|
|
|
/** |
607
|
|
|
* Returns the date time format |
608
|
|
|
* |
609
|
|
|
* @return DateTimeFormat |
610
|
|
|
*/ |
611
|
32 |
|
public function getDateTimeFormat() |
612
|
|
|
{ |
613
|
32 |
|
return $this->dateTimeFormat; |
614
|
|
|
} |
615
|
|
|
|
616
|
|
|
/** |
617
|
|
|
* Sets the date time format |
618
|
|
|
* |
619
|
|
|
* @param DateTimeFormat $dateTimeFormat |
620
|
|
|
*/ |
621
|
1 |
|
public function setDateTimeFormat(DateTimeFormat $dateTimeFormat) |
622
|
|
|
{ |
623
|
1 |
|
$this->dateTimeFormat = $dateTimeFormat; |
624
|
1 |
|
} |
625
|
|
|
|
626
|
|
|
/** |
627
|
|
|
* Appends policies parameters to url |
628
|
|
|
* |
629
|
|
|
* @param Url $url |
630
|
|
|
* @param string[] $policies A list of policy IDs that must be applied to the newly created document object |
631
|
|
|
*/ |
632
|
4 |
|
protected function appendPoliciesToUrl(Url $url, array $policies) |
633
|
|
|
{ |
634
|
4 |
|
if (!empty($policies)) { |
635
|
2 |
|
$url->getQuery()->modify($this->convertPolicyIdArrayToQueryArray($policies)); |
636
|
2 |
|
} |
637
|
4 |
|
} |
638
|
|
|
|
639
|
|
|
/** |
640
|
|
|
* Appends addAces parameters to url |
641
|
|
|
* |
642
|
|
|
* @param Url $url |
643
|
|
|
* @param AclInterface|null $addAces A list of ACEs |
644
|
|
|
*/ |
645
|
4 |
View Code Duplication |
protected function appendAddAcesToUrl(Url $url, AclInterface $addAces = null) |
646
|
|
|
{ |
647
|
4 |
|
if ($addAces !== null) { |
648
|
2 |
|
$url->getQuery()->modify( |
649
|
2 |
|
$this->convertAclToQueryArray( |
650
|
2 |
|
$addAces, |
651
|
2 |
|
Constants::CONTROL_ADD_ACE_PRINCIPAL, |
652
|
|
|
Constants::CONTROL_ADD_ACE_PERMISSION |
653
|
2 |
|
) |
654
|
2 |
|
); |
655
|
2 |
|
} |
656
|
4 |
|
} |
657
|
|
|
|
658
|
|
|
/** |
659
|
|
|
* Appends removeAces parameters to url |
660
|
|
|
* |
661
|
|
|
* @param Url $url |
662
|
|
|
* @param AclInterface|null $removeAces A list of ACEs |
663
|
|
|
*/ |
664
|
4 |
View Code Duplication |
protected function appendRemoveAcesToUrl(Url $url, AclInterface $removeAces = null) |
665
|
|
|
{ |
666
|
4 |
|
if ($removeAces !== null) { |
667
|
2 |
|
$url->getQuery()->modify( |
668
|
2 |
|
$this->convertAclToQueryArray( |
669
|
2 |
|
$removeAces, |
670
|
2 |
|
Constants::CONTROL_REMOVE_ACE_PRINCIPAL, |
671
|
|
|
Constants::CONTROL_REMOVE_ACE_PERMISSION |
672
|
2 |
|
) |
673
|
2 |
|
); |
674
|
2 |
|
} |
675
|
4 |
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Gets the content link from the cache if it is there or loads it into the |
679
|
|
|
* cache if it is not there. |
680
|
|
|
* |
681
|
|
|
* @param string $repositoryId |
682
|
|
|
* @param string $documentId |
683
|
|
|
* @return string|null |
684
|
|
|
*/ |
685
|
|
|
public function loadContentLink($repositoryId, $documentId) |
686
|
|
|
{ |
687
|
|
|
$result = $this->getRepositoryUrlCache()->getObjectUrl($repositoryId, $documentId, Constants::SELECTOR_CONTENT); |
688
|
|
|
return $result === null ? null : (string) $result; |
689
|
|
|
} |
690
|
|
|
|
691
|
|
|
/** |
692
|
|
|
* Gets a rendition content link from the cache if it is there or loads it |
693
|
|
|
* into the cache if it is not there. |
694
|
|
|
* |
695
|
|
|
* @param string $repositoryId |
696
|
|
|
* @param string $documentId |
697
|
|
|
* @param string $streamId |
698
|
|
|
* @return string|null |
699
|
|
|
*/ |
700
|
|
|
public function loadRenditionContentLink($repositoryId, $documentId, $streamId) |
701
|
|
|
{ |
702
|
|
|
$result = $this->getRepositoryUrlCache()->getObjectUrl($repositoryId, $documentId, Constants::SELECTOR_CONTENT); |
703
|
|
|
if ($result !== null) { |
704
|
|
|
$result->getQuery()->modify(array(Constants::PARAM_STREAM_ID => $streamId)); |
705
|
|
|
$result = (string) $result; |
706
|
|
|
} |
707
|
|
|
return $result; |
708
|
|
|
} |
709
|
|
|
} |
710
|
|
|
|