1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @license GNU AGPL version 3 or any later version |
6
|
|
|
* |
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
10
|
|
|
* License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\Richdocuments\WOPI; |
23
|
|
|
|
24
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
25
|
|
|
use OCP\Files\IAppData; |
26
|
|
|
use OCP\Files\NotFoundException; |
27
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
28
|
|
|
use OCP\Http\Client\IClientService; |
29
|
|
|
use OCP\IConfig; |
30
|
|
|
use OCP\IL10N; |
31
|
|
|
use OCP\Notification\IApp; |
32
|
|
|
|
33
|
|
|
class DiscoveryManager { |
34
|
|
|
/** @var IClientService */ |
35
|
|
|
private $clientService; |
36
|
|
|
/** @var ISimpleFolder */ |
37
|
|
|
private $appData; |
38
|
|
|
/** @var IConfig */ |
39
|
|
|
private $config; |
40
|
|
|
/** @var IL10N */ |
41
|
|
|
private $l10n; |
42
|
|
|
/** @var ITimeFactory */ |
43
|
|
|
private $timeFactory; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param IClientService $clientService |
47
|
|
|
* @param IAppData $appData |
48
|
|
|
* @param IConfig $config |
49
|
|
|
* @param IL10N $l10n |
50
|
|
|
* @param ITimeFactory $timeFactory |
51
|
|
|
*/ |
52
|
|
|
public function __construct(IClientService $clientService, |
53
|
|
|
IAppData $appData, |
54
|
|
|
IConfig $config, |
55
|
|
|
IL10N $l10n, |
|
|
|
|
56
|
|
|
ITimeFactory $timeFactory) { |
57
|
|
|
$this->clientService = $clientService; |
58
|
|
|
try { |
59
|
|
|
$this->appData = $appData->getFolder('richdocuments'); |
60
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
61
|
|
|
$this->appData = $appData->newFolder('richdocuments'); |
62
|
|
|
} |
63
|
|
|
$this->config = $config; |
64
|
|
|
$this->timeFactory = $timeFactory; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function get() { |
68
|
|
|
// First check if there is a local valid discovery file |
69
|
|
|
try { |
70
|
|
|
$file = $this->appData->getFile('discovery.xml'); |
71
|
|
|
$decodedFile = json_decode($file->getContent(), true); |
72
|
|
|
if($decodedFile['timestamp'] + 3600 > $this->timeFactory->getTime()) { |
73
|
|
|
return $decodedFile['data']; |
74
|
|
|
} |
75
|
|
|
} catch (NotFoundException $e) { |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); |
79
|
|
|
$wopiDiscovery = $remoteHost . '/hosting/discovery'; |
80
|
|
|
|
81
|
|
|
$client = $this->clientService->newClient(); |
82
|
|
|
try { |
83
|
|
|
$response = $client->get($wopiDiscovery); |
84
|
|
|
} catch (\Exception $e) { |
85
|
|
|
throw $e; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$responseBody = $response->getBody(); |
89
|
|
|
$file->putContent( |
90
|
|
|
json_encode([ |
91
|
|
|
'data' => $responseBody, |
92
|
|
|
'timestamp' => $this->timeFactory->getTime(), |
93
|
|
|
]) |
94
|
|
|
); |
95
|
|
|
return $responseBody; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function refretch() { |
99
|
|
|
$this->appData->getFile('discovery.xml')->delete(); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.