These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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 | 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) { |
||
0 ignored issues
–
show
|
|||
57 | return $this->capabilities; |
||
58 | } |
||
59 | try { |
||
60 | $file = $this->appData->getFile('capabilities.json'); |
||
61 | $decodedFile = \json_decode($file->getContent(), true); |
||
62 | } catch (NotFoundException $e) { |
||
63 | return []; |
||
64 | } |
||
65 | |||
66 | if (!is_array($decodedFile)) { |
||
67 | return []; |
||
68 | } |
||
69 | $this->capabilities = $decodedFile; |
||
70 | |||
71 | return $this->capabilities; |
||
72 | } |
||
73 | |||
74 | public function hasTemplateSaveAs() { |
||
75 | return $this->getCapabilities()['hasTemplateSaveAs'] ?? false; |
||
76 | } |
||
77 | |||
78 | public function hasTemplateSource() { |
||
79 | return $this->getCapabilities()['hasTemplateSource'] ?? false; |
||
80 | } |
||
81 | |||
82 | private function getFile() { |
||
83 | try { |
||
84 | $file = $this->appData->getFile('capabilities.json'); |
||
85 | } catch (NotFoundException $e) { |
||
86 | $file = $this->appData->newFile('capabilities.json'); |
||
87 | $file->putContent(json_encode([])); |
||
88 | } |
||
89 | |||
90 | return $file; |
||
91 | } |
||
92 | |||
93 | public function clear() { |
||
94 | $file = $this->getFile(); |
||
95 | $file->putContent(json_encode([])); |
||
96 | } |
||
97 | |||
98 | public function refretch() { |
||
99 | $capabilties = $this->renewCapabilities(); |
||
100 | |||
101 | if ($capabilties !== []) { |
||
102 | $file = $this->getFile(); |
||
103 | $file->putContent(json_encode($capabilties)); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | private function renewCapabilities() { |
||
108 | $remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url'); |
||
109 | if ($remoteHost === '') { |
||
110 | return []; |
||
111 | } |
||
112 | $capabilitiesEndpoint = $remoteHost . '/hosting/capabilities'; |
||
113 | |||
114 | $client = $this->clientService->newClient(); |
||
115 | $options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]]; |
||
116 | |||
117 | $options['verify'] = $this->config->getAppValue('richdocuments', 'disable_certificate_verification', '') === ''; |
||
118 | |||
119 | try { |
||
120 | $response = $client->get($capabilitiesEndpoint, $options); |
||
121 | } catch (\Exception $e) { |
||
122 | return []; |
||
123 | } |
||
124 | |||
125 | $responseBody = $response->getBody(); |
||
126 | $ret = \json_decode($responseBody, true); |
||
127 | |||
128 | if (!is_array($ret)) { |
||
129 | return []; |
||
130 | } |
||
131 | |||
132 | return $ret; |
||
133 | } |
||
134 | |||
135 | } |
||
136 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.