1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2019, Roeland Jago Douma <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace OCA\Richdocuments\Service; |
25
|
|
|
|
26
|
|
|
use OCP\Files\IAppData; |
27
|
|
|
use OCP\Files\NotFoundException; |
28
|
|
|
use OCP\Files\SimpleFS\ISimpleFile; |
29
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder; |
30
|
|
|
use OCP\Http\Client\IClientService; |
31
|
|
|
use OCP\IConfig; |
32
|
|
|
|
33
|
|
|
class CapabilitiesService { |
34
|
|
|
|
35
|
|
|
/** @var IConfig */ |
36
|
|
|
private $config; |
37
|
|
|
/** @var IClientService */ |
38
|
|
|
private $clientService; |
39
|
|
|
/** @var ISimpleFolder */ |
40
|
|
|
private $appData; |
41
|
|
|
/** @var array */ |
42
|
|
|
private $capabilities; |
43
|
|
|
|
44
|
|
View Code Duplication |
public function __construct(IConfig $config, IClientService $clientService, IAppData $appData) { |
|
|
|
|
45
|
|
|
$this->config = $config; |
46
|
|
|
$this->clientService = $clientService; |
47
|
|
|
try { |
48
|
|
|
$this->appData = $appData->getFolder('richdocuments'); |
49
|
|
|
} catch (NotFoundException $e) { |
50
|
|
|
$this->appData = $appData->newFolder('richdocuments'); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
|
55
|
|
|
public function getCapabilities() { |
56
|
|
|
if ($this->capabilities) { |
|
|
|
|
57
|
|
|
$isCODEInstalled = $this->getContainer()->getServer()->getAppManager()->isEnabledForUser('richdocumentscode'); |
|
|
|
|
58
|
|
|
$isCODEEnabled = strpos($this->config->getAppValue('richdocuments', 'wopi_url'), 'proxy.php?req=') !== false; |
59
|
|
|
if($isCODEInstalled && $isCODEEnabled && count($this->capabilities) === 0) { |
60
|
|
|
$this->refretch(); |
61
|
|
|
} |
62
|
|
|
return $this->capabilities; |
63
|
|
|
} |
64
|
|
|
try { |
65
|
|
|
$file = $this->appData->getFile('capabilities.json'); |
66
|
|
|
$decodedFile = \json_decode($file->getContent(), true); |
67
|
|
|
} catch (NotFoundException $e) { |
68
|
|
|
return []; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!is_array($decodedFile)) { |
72
|
|
|
return []; |
73
|
|
|
} |
74
|
|
|
$this->capabilities = $decodedFile; |
75
|
|
|
|
76
|
|
|
return $this->capabilities; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function hasTemplateSaveAs() { |
80
|
|
|
return $this->getCapabilities()['hasTemplateSaveAs'] ?? false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function hasTemplateSource() { |
84
|
|
|
return $this->getCapabilities()['hasTemplateSource'] ?? false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
private function getFile() { |
88
|
|
|
try { |
89
|
|
|
$file = $this->appData->getFile('capabilities.json'); |
90
|
|
|
} catch (NotFoundException $e) { |
91
|
|
|
$file = $this->appData->newFile('capabilities.json'); |
92
|
|
|
$file->putContent(json_encode([])); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $file; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function clear() { |
99
|
|
|
$file = $this->getFile(); |
100
|
|
|
$file->putContent(json_encode([])); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function refretch() { |
104
|
|
|
$capabilties = $this->renewCapabilities(); |
105
|
|
|
|
106
|
|
|
if ($capabilties !== []) { |
107
|
|
|
$file = $this->getFile(); |
108
|
|
|
$file->putContent(json_encode($capabilties)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
private function renewCapabilities() { |
113
|
|
|
$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); |
114
|
|
|
if ($remoteHost === '') { |
115
|
|
|
return []; |
116
|
|
|
} |
117
|
|
|
$capabilitiesEndpoint = $remoteHost . '/hosting/capabilities'; |
118
|
|
|
|
119
|
|
|
$client = $this->clientService->newClient(); |
120
|
|
|
$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]]; |
121
|
|
|
|
122
|
|
|
$options['verify'] = $this->config->getAppValue('richdocuments', 'disable_certificate_verification', '') === ''; |
123
|
|
|
|
124
|
|
|
try { |
125
|
|
|
$response = $client->get($capabilitiesEndpoint, $options); |
126
|
|
|
} catch (\Exception $e) { |
127
|
|
|
return []; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
$responseBody = $response->getBody(); |
131
|
|
|
$ret = \json_decode($responseBody, true); |
132
|
|
|
|
133
|
|
|
if (!is_array($ret)) { |
134
|
|
|
return []; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $ret; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
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.