2017-09-26 18:11:58 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.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 OC\Remote;
|
|
|
|
|
2017-09-27 18:46:24 +03:00
|
|
|
use OC\Remote\Api\NotFoundException;
|
2017-09-26 18:11:58 +03:00
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\ICache;
|
2017-10-04 17:21:50 +03:00
|
|
|
use OCP\Remote\IInstance;
|
2017-09-26 18:11:58 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides some basic info about a remote Nextcloud instance
|
|
|
|
*/
|
2017-10-04 17:21:50 +03:00
|
|
|
class Instance implements IInstance {
|
2017-09-26 18:11:58 +03:00
|
|
|
/** @var string */
|
|
|
|
private $url;
|
|
|
|
|
|
|
|
/** @var ICache */
|
|
|
|
private $cache;
|
|
|
|
|
|
|
|
/** @var IClientService */
|
|
|
|
private $clientService;
|
|
|
|
|
2017-10-19 14:50:27 +03:00
|
|
|
/** @var array|null */
|
2017-09-26 18:11:58 +03:00
|
|
|
private $status;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param ICache $cache
|
|
|
|
* @param IClientService $clientService
|
|
|
|
*/
|
|
|
|
public function __construct($url, ICache $cache, IClientService $clientService) {
|
|
|
|
$url = str_replace('https://', '', $url);
|
|
|
|
$this->url = str_replace('http://', '', $url);
|
|
|
|
$this->cache = $cache;
|
|
|
|
$this->clientService = $clientService;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string The url of the remote server without protocol
|
|
|
|
*/
|
|
|
|
public function getUrl() {
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string The of of the remote server with protocol
|
|
|
|
*/
|
|
|
|
public function getFullUrl() {
|
|
|
|
return $this->getProtocol() . '://' . $this->getUrl();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string The full version string in '13.1.2.3' format
|
|
|
|
*/
|
|
|
|
public function getVersion() {
|
|
|
|
$status = $this->getStatus();
|
|
|
|
return $status['version'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string 'http' or 'https'
|
|
|
|
*/
|
|
|
|
public function getProtocol() {
|
|
|
|
$status = $this->getStatus();
|
|
|
|
return $status['protocol'];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check that the remote server is installed and not in maintenance mode
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isActive() {
|
|
|
|
$status = $this->getStatus();
|
|
|
|
return $status['installed'] && !$status['maintenance'];
|
|
|
|
}
|
|
|
|
|
2017-10-19 14:50:27 +03:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws NotFoundException
|
2017-10-19 16:36:39 +03:00
|
|
|
* @throws \Exception
|
2017-10-19 14:50:27 +03:00
|
|
|
*/
|
2017-09-26 18:11:58 +03:00
|
|
|
private function getStatus() {
|
|
|
|
if ($this->status) {
|
|
|
|
return $this->status;
|
|
|
|
}
|
|
|
|
$key = 'remote/' . $this->url . '/status';
|
2017-10-19 16:36:39 +03:00
|
|
|
$httpsKey = 'remote/' . $this->url . '/https';
|
2017-09-26 18:11:58 +03:00
|
|
|
$status = $this->cache->get($key);
|
|
|
|
if (!$status) {
|
|
|
|
$response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php');
|
|
|
|
$protocol = 'https';
|
|
|
|
if (!$response) {
|
2017-10-19 16:36:39 +03: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 18:11:58 +03:00
|
|
|
$response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php');
|
|
|
|
$protocol = 'http';
|
2017-10-19 16:36:39 +03:00
|
|
|
} else {
|
|
|
|
$this->cache->set($httpsKey, true, 60 * 60 * 24 * 365);
|
2017-09-26 18:11:58 +03: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 18:46:24 +03:00
|
|
|
} else {
|
|
|
|
throw new NotFoundException('Remote server not found at address ' . $this->url);
|
2017-09-26 18:11:58 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $status;
|
|
|
|
}
|
|
|
|
|
2017-10-19 14:50:27 +03:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
2017-09-26 18:11:58 +03:00
|
|
|
private function downloadStatus($url) {
|
|
|
|
try {
|
|
|
|
$request = $this->clientService->newClient()->get($url);
|
|
|
|
return $request->getBody();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|