Welcome to mirror list, hosted at ThFree Co, Russian Federation.

CapabilitiesService.php « Service « lib - github.com/nextcloud/richdocuments.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3d18c957f9b022f8553e2141d6e9ec666a35f05e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
/**
 * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace OCA\Richdocuments\Service;

use OCP\App\IAppManager;
use OCP\Http\Client\IClientService;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;

class CapabilitiesService {

	/** @var IConfig */
	private $config;
	/** @var IClientService */
	private $clientService;
	/** @var ICache */
	private $cache;
	/** @var IAppManager */
	private $appManager;

	/** @var array */
	private $capabilities;


	public function __construct(IConfig $config, IClientService $clientService, ICacheFactory $cacheFactory, IAppManager $appManager) {
		$this->config = $config;
		$this->clientService = $clientService;
		$this->cache = $cacheFactory->createDistributed('richdocuments');
		$this->appManager = $appManager;
	}

	public function getCapabilities() {
		if (!$this->capabilities) {
			$this->capabilities = $this->cache->get('capabilities');
		}

		$isARM64 = php_uname('m') === 'aarch64';
		$CODEAppID = $isARM64 ? 'richdocumentscode_arm64' : 'richdocumentscode';
		$isCODEInstalled = $this->appManager->isEnabledForUser($CODEAppID);
		$isCODEEnabled = strpos($this->config->getAppValue('richdocuments', 'wopi_url'), 'proxy.php?req=') !== false;
		$shouldRecheckCODECapabilities = $isCODEInstalled && $isCODEEnabled && ($this->capabilities === null || count($this->capabilities) === 0);
		if($this->capabilities === null || $shouldRecheckCODECapabilities) {
			$this->refetch();
		}

		if (!is_array($this->capabilities)) {
			return [];
		}

		return $this->capabilities;
	}

	public function hasDrawSupport(): bool {
		$productVersion = $this->getCapabilities()['productVersion'] ?? '0.0.0.0';
		return version_compare($productVersion, '6.4.7', '>=');
	}

	public function hasTemplateSaveAs(): bool {
		return $this->getCapabilities()['hasTemplateSaveAs'] ?? false;
	}

	public function hasTemplateSource(): bool {
		return $this->getCapabilities()['hasTemplateSource'] ?? false;
	}

	public function clear(): void {
		$this->cache->remove('capabilities');
	}

	public function refetch(): void {
		$remoteHost = $this->config->getAppValue('richdocuments', 'wopi_url');
		if ($remoteHost === '') {
			return;
		}
		$capabilitiesEndpoint = rtrim($remoteHost, '/') . '/hosting/capabilities';

		$client = $this->clientService->newClient();
		$options = ['timeout' => 45, 'nextcloud' => ['allow_local_address' => true]];

		if ($this->config->getAppValue('richdocuments', 'disable_certificate_verification') === 'yes') {
			$options['verify'] = false;
		}

		try {
			$response = $client->get($capabilitiesEndpoint, $options);
			$responseBody = $response->getBody();
			$capabilities = \json_decode($responseBody, true);

			if (!is_array($capabilities)) {
				$capabilities = [];
			}


		} catch (\Exception $e) {
			$capabilities = [];
		}

		$this->capabilities = $capabilities;
		$ttl = 3600;
		if (count($capabilities) === 0)
			$ttl = 60;

		$this->cache->set('capabilities', $capabilities, $ttl);
	}
}