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\Http\Client\IClientService; |
25
|
|
|
use OCP\ICache; |
26
|
|
|
use OCP\ICacheFactory; |
27
|
|
|
use OCP\IConfig; |
28
|
|
|
|
29
|
|
|
class DiscoveryManager { |
30
|
|
|
|
31
|
|
|
/** @var IClientService */ |
32
|
|
|
private $clientService; |
33
|
|
|
/** @var ICache */ |
34
|
|
|
private $cache; |
35
|
|
|
/** @var IConfig */ |
36
|
|
|
private $config; |
37
|
|
|
|
38
|
|
|
/** @var string */ |
39
|
|
|
private $discovery; |
40
|
|
|
|
41
|
|
View Code Duplication |
public function __construct(IClientService $clientService, |
|
|
|
|
42
|
|
|
ICacheFactory $cacheFactory, |
43
|
|
|
IConfig $config) { |
44
|
|
|
$this->clientService = $clientService; |
45
|
|
|
$this->cache = $cacheFactory->createDistributed('richdocuments'); |
46
|
|
|
$this->config = $config; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function get() { |
50
|
|
|
if ($this->discovery) { |
51
|
|
|
return $this->discovery; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->discovery = $this->cache->get('discovery'); |
55
|
|
|
if (!$this->discovery) { |
56
|
|
|
$response = $this->fetchFromRemote(); |
57
|
|
|
$responseBody = $response->getBody(); |
58
|
|
|
$this->discovery = $responseBody; |
59
|
|
|
$this->cache->set('discovery', $this->discovery, 3600); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->discovery; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return \OCP\Http\Client\IResponse |
67
|
|
|
* @throws \Exception |
68
|
|
|
*/ |
69
|
|
|
public function fetchFromRemote() { |
70
|
|
|
$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); |
71
|
|
|
$wopiDiscovery = $remoteHost . '/hosting/discovery'; |
72
|
|
|
|
73
|
|
|
$client = $this->clientService->newClient(); |
74
|
|
|
$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]]; |
75
|
|
|
|
76
|
|
|
if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') { |
77
|
|
|
$options['verify'] = false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($this->isProxyStarting($wopiDiscovery)) |
81
|
|
|
$options['timeout'] = 180; |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
return $client->get($wopiDiscovery, $options); |
85
|
|
|
} catch (\Exception $e) { |
86
|
|
|
throw $e; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function refetch() { |
91
|
|
|
$this->cache->remove('discovery'); |
92
|
|
|
$this->discovery = null; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return boolean indicating if proxy.php is in initialize or false otherwise |
97
|
|
|
*/ |
98
|
|
|
private function isProxyStarting($url) { |
99
|
|
|
$usesProxy = false; |
100
|
|
|
$proxyPos = strrpos($url, 'proxy.php'); |
101
|
|
|
if ($proxyPos === false) |
102
|
|
|
$usesProxy = false; |
103
|
|
|
else |
104
|
|
|
$usesProxy = true; |
105
|
|
|
|
106
|
|
|
if ($usesProxy === true) { |
107
|
|
|
$statusUrl = substr($url, 0, $proxyPos); |
108
|
|
|
$statusUrl = $statusUrl . 'proxy.php?status'; |
109
|
|
|
|
110
|
|
|
$client = $this->clientService->newClient(); |
111
|
|
|
$options = ['timeout' => 5, 'nextcloud' => ['allow_local_address' => true]]; |
112
|
|
|
|
113
|
|
|
if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') { |
114
|
|
|
$options['verify'] = false; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
try { |
118
|
|
|
$response = $client->get($statusUrl, $options); |
119
|
|
|
|
120
|
|
|
if ($response->getStatusCode() === 200) { |
121
|
|
|
$body = json_decode($response->getBody(), true); |
122
|
|
|
|
123
|
|
|
if ($body['status'] === 'starting' |
124
|
|
|
|| $body['status'] === 'stopped' |
125
|
|
|
|| $body['status'] === 'restarting') { |
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} catch (\Exception $e) { |
130
|
|
|
// ignore |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return false; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.