2017-09-26 11:11:58 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2017-09-26 11:11:58 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-09-26 11:11:58 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Remote;
|
|
|
|
|
|
2017-09-27 11:46:24 -04:00
|
|
|
use OC\Remote\Api\NotFoundException;
|
2017-09-26 11:11:58 -04:00
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
|
use OCP\ICache;
|
2017-10-04 10:21:50 -04:00
|
|
|
use OCP\Remote\IInstance;
|
2025-12-02 05:39:50 -05:00
|
|
|
use Override;
|
2017-09-26 11:11:58 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Provides some basic info about a remote Nextcloud instance
|
|
|
|
|
*/
|
2017-10-04 10:21:50 -04:00
|
|
|
class Instance implements IInstance {
|
2025-12-02 05:39:50 -05:00
|
|
|
private string $url;
|
|
|
|
|
private ?array $status = null;
|
2017-09-26 11:11:58 -04:00
|
|
|
|
2025-11-17 09:32:54 -05:00
|
|
|
public function __construct(
|
2025-12-02 05:39:50 -05:00
|
|
|
string $url,
|
2025-11-17 09:32:54 -05:00
|
|
|
private ICache $cache,
|
|
|
|
|
private IClientService $clientService,
|
|
|
|
|
) {
|
2017-09-26 11:11:58 -04:00
|
|
|
$url = str_replace('https://', '', $url);
|
|
|
|
|
$this->url = str_replace('http://', '', $url);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
#[Override]
|
|
|
|
|
public function getUrl(): string {
|
2017-09-26 11:11:58 -04:00
|
|
|
return $this->url;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
#[Override]
|
|
|
|
|
public function getFullUrl(): string {
|
2017-09-26 11:11:58 -04:00
|
|
|
return $this->getProtocol() . '://' . $this->getUrl();
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
#[Override]
|
|
|
|
|
public function getVersion(): string {
|
2017-09-26 11:11:58 -04:00
|
|
|
$status = $this->getStatus();
|
|
|
|
|
return $status['version'];
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
#[Override]
|
|
|
|
|
public function getProtocol(): string {
|
2017-09-26 11:11:58 -04:00
|
|
|
$status = $this->getStatus();
|
|
|
|
|
return $status['protocol'];
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
#[Override]
|
|
|
|
|
public function isActive(): bool {
|
2017-09-26 11:11:58 -04:00
|
|
|
$status = $this->getStatus();
|
|
|
|
|
return $status['installed'] && !$status['maintenance'];
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
private function getStatus(): array {
|
2017-09-26 11:11:58 -04:00
|
|
|
if ($this->status) {
|
|
|
|
|
return $this->status;
|
|
|
|
|
}
|
|
|
|
|
$key = 'remote/' . $this->url . '/status';
|
2017-10-19 09:36:39 -04:00
|
|
|
$httpsKey = 'remote/' . $this->url . '/https';
|
2017-09-26 11:11:58 -04:00
|
|
|
$status = $this->cache->get($key);
|
|
|
|
|
if (!$status) {
|
|
|
|
|
$response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php');
|
|
|
|
|
$protocol = 'https';
|
|
|
|
|
if (!$response) {
|
2017-10-19 09:36:39 -04:00
|
|
|
if ($status = $this->cache->get($httpsKey)) {
|
|
|
|
|
throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https');
|
|
|
|
|
}
|
2017-09-26 11:11:58 -04:00
|
|
|
$response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php');
|
|
|
|
|
$protocol = 'http';
|
2017-10-19 09:36:39 -04:00
|
|
|
} else {
|
|
|
|
|
$this->cache->set($httpsKey, true, 60 * 60 * 24 * 365);
|
2017-09-26 11:11:58 -04:00
|
|
|
}
|
|
|
|
|
$status = json_decode($response, true);
|
|
|
|
|
if ($status) {
|
|
|
|
|
$status['protocol'] = $protocol;
|
|
|
|
|
}
|
|
|
|
|
if ($status) {
|
|
|
|
|
$this->cache->set($key, $status, 5 * 60);
|
|
|
|
|
$this->status = $status;
|
2017-09-27 11:46:24 -04:00
|
|
|
} else {
|
|
|
|
|
throw new NotFoundException('Remote server not found at address ' . $this->url);
|
2017-09-26 11:11:58 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $status;
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-02 05:39:50 -05:00
|
|
|
private function downloadStatus(string $url): false|string {
|
2017-09-26 11:11:58 -04:00
|
|
|
try {
|
|
|
|
|
$request = $this->clientService->newClient()->get($url);
|
2025-06-26 14:41:49 -04:00
|
|
|
$content = $request->getBody();
|
|
|
|
|
|
|
|
|
|
// IResponse.getBody responds with null|resource if returning a stream response was requested.
|
|
|
|
|
// As that's not the case here, we can just ignore the psalm warning by adding an assertion.
|
|
|
|
|
assert(is_string($content));
|
|
|
|
|
|
|
|
|
|
return $content;
|
2025-12-02 05:39:50 -05:00
|
|
|
} catch (\Exception) {
|
2017-09-26 11:11:58 -04:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|