diff options
Diffstat (limited to 'lib/private/Remote')
-rw-r--r-- | lib/private/Remote/Api/ApiBase.php | 82 | ||||
-rw-r--r-- | lib/private/Remote/Api/ApiCollection.php | 35 | ||||
-rw-r--r-- | lib/private/Remote/Api/ApiFactory.php | 25 | ||||
-rw-r--r-- | lib/private/Remote/Api/NotFoundException.php | 10 | ||||
-rw-r--r-- | lib/private/Remote/Api/OCS.php | 83 | ||||
-rw-r--r-- | lib/private/Remote/Credentials.php | 39 | ||||
-rw-r--r-- | lib/private/Remote/Instance.php | 138 | ||||
-rw-r--r-- | lib/private/Remote/InstanceFactory.php | 27 | ||||
-rw-r--r-- | lib/private/Remote/User.php | 122 |
9 files changed, 561 insertions, 0 deletions
diff --git a/lib/private/Remote/Api/ApiBase.php b/lib/private/Remote/Api/ApiBase.php new file mode 100644 index 00000000000..b2f96fb3c24 --- /dev/null +++ b/lib/private/Remote/Api/ApiBase.php @@ -0,0 +1,82 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote\Api; + +use OCP\Http\Client\IClientService; +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +class ApiBase { + /** @var IInstance */ + private $instance; + /** @var ICredentials */ + private $credentials; + /** @var IClientService */ + private $clientService; + + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { + $this->instance = $instance; + $this->credentials = $credentials; + $this->clientService = $clientService; + } + + protected function getHttpClient() { + return $this->clientService->newClient(); + } + + protected function addDefaultHeaders(array $headers) { + return array_merge([ + 'OCS-APIREQUEST' => 'true', + 'Accept' => 'application/json' + ], $headers); + } + + /** + * @param string $method + * @param string $url + * @param array $body + * @param array $query + * @param array $headers + * @return resource|string + * @throws \InvalidArgumentException + */ + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { + $fullUrl = trim($this->instance->getFullUrl(), '/') . '/' . $url; + $options = [ + 'query' => $query, + 'headers' => $this->addDefaultHeaders($headers), + 'auth' => [$this->credentials->getUsername(), $this->credentials->getPassword()] + ]; + if ($body) { + $options['body'] = $body; + } + + $client = $this->getHttpClient(); + + switch ($method) { + case 'get': + $response = $client->get($fullUrl, $options); + break; + case 'post': + $response = $client->post($fullUrl, $options); + break; + case 'put': + $response = $client->put($fullUrl, $options); + break; + case 'delete': + $response = $client->delete($fullUrl, $options); + break; + case 'options': + $response = $client->options($fullUrl, $options); + break; + default: + throw new \InvalidArgumentException('Invalid method ' . $method); + } + + return $response->getBody(); + } +} diff --git a/lib/private/Remote/Api/ApiCollection.php b/lib/private/Remote/Api/ApiCollection.php new file mode 100644 index 00000000000..f154cd21926 --- /dev/null +++ b/lib/private/Remote/Api/ApiCollection.php @@ -0,0 +1,35 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote\Api; + +use OCP\Http\Client\IClientService; +use OCP\Remote\Api\IApiCollection; +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +class ApiCollection implements IApiCollection { + /** @var IInstance */ + private $instance; + /** @var ICredentials */ + private $credentials; + /** @var IClientService */ + private $clientService; + + public function __construct(IInstance $instance, ICredentials $credentials, IClientService $clientService) { + $this->instance = $instance; + $this->credentials = $credentials; + $this->clientService = $clientService; + } + + public function getCapabilitiesApi() { + return new OCS($this->instance, $this->credentials, $this->clientService); + } + + public function getUserApi() { + return new OCS($this->instance, $this->credentials, $this->clientService); + } +} diff --git a/lib/private/Remote/Api/ApiFactory.php b/lib/private/Remote/Api/ApiFactory.php new file mode 100644 index 00000000000..795584e566a --- /dev/null +++ b/lib/private/Remote/Api/ApiFactory.php @@ -0,0 +1,25 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote\Api; + +use OCP\Http\Client\IClientService; +use OCP\Remote\Api\IApiFactory; +use OCP\Remote\ICredentials; +use OCP\Remote\IInstance; + +class ApiFactory implements IApiFactory { + /** @var IClientService */ + private $clientService; + + public function __construct(IClientService $clientService) { + $this->clientService = $clientService; + } + + public function getApiCollection(IInstance $instance, ICredentials $credentials) { + return new ApiCollection($instance, $credentials, $this->clientService); + } +} diff --git a/lib/private/Remote/Api/NotFoundException.php b/lib/private/Remote/Api/NotFoundException.php new file mode 100644 index 00000000000..361d03d24ed --- /dev/null +++ b/lib/private/Remote/Api/NotFoundException.php @@ -0,0 +1,10 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote\Api; + +class NotFoundException extends \Exception { +} diff --git a/lib/private/Remote/Api/OCS.php b/lib/private/Remote/Api/OCS.php new file mode 100644 index 00000000000..36bc22751a5 --- /dev/null +++ b/lib/private/Remote/Api/OCS.php @@ -0,0 +1,83 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote\Api; + +use GuzzleHttp\Exception\ClientException; +use OC\ForbiddenException; +use OC\Remote\User; +use OCP\AppFramework\OCSController; +use OCP\Remote\Api\ICapabilitiesApi; +use OCP\Remote\Api\IUserApi; + +class OCS extends ApiBase implements ICapabilitiesApi, IUserApi { + /** + * @param string $method + * @param string $url + * @param array $body + * @param array $query + * @param array $headers + * @return array + * @throws ForbiddenException + * @throws NotFoundException + * @throws \Exception + */ + protected function request($method, $url, array $body = [], array $query = [], array $headers = []) { + try { + $response = json_decode(parent::request($method, 'ocs/v2.php/' . $url, $body, $query, $headers), true); + } catch (ClientException $e) { + if ($e->getResponse()->getStatusCode() === 404) { + throw new NotFoundException(); + } elseif ($e->getResponse()->getStatusCode() === 403 || $e->getResponse()->getStatusCode() === 401) { + throw new ForbiddenException(); + } else { + throw $e; + } + } + if (!isset($response['ocs']) || !isset($response['ocs']['meta'])) { + throw new \Exception('Invalid ocs response'); + } + if ($response['ocs']['meta']['statuscode'] === OCSController::RESPOND_UNAUTHORISED) { + throw new ForbiddenException(); + } + if ($response['ocs']['meta']['statuscode'] === OCSController::RESPOND_NOT_FOUND) { + throw new NotFoundException(); + } + if ($response['ocs']['meta']['status'] !== 'ok') { + throw new \Exception('Unknown ocs error ' . $response['ocs']['meta']['message']); + } + + return $response['ocs']['data']; + } + + /** + * @param array $data + * @param string $type + * @param string[] $keys + * @throws \Exception + */ + private function checkResponseArray(array $data, $type, array $keys) { + foreach ($keys as $key) { + if (!array_key_exists($key, $data)) { + throw new \Exception('Invalid ' . $type . ' response, expected field ' . $key . ' not found'); + } + } + } + + public function getUser($userId) { + $result = $this->request('get', 'cloud/users/' . $userId); + $this->checkResponseArray($result, 'user', User::EXPECTED_KEYS); + return new User($result); + } + + /** + * @return array The capabilities in the form of [$appId => [$capability => $value]] + */ + public function getCapabilities() { + $result = $this->request('get', 'cloud/capabilities'); + return $result['capabilities']; + } +} diff --git a/lib/private/Remote/Credentials.php b/lib/private/Remote/Credentials.php new file mode 100644 index 00000000000..7f1ffaa727c --- /dev/null +++ b/lib/private/Remote/Credentials.php @@ -0,0 +1,39 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote; + +use OCP\Remote\ICredentials; + +class Credentials implements ICredentials { + /** @var string */ + private $user; + /** @var string */ + private $password; + + /** + * @param string $user + * @param string $password + */ + public function __construct($user, $password) { + $this->user = $user; + $this->password = $password; + } + + /** + * @return string + */ + public function getUsername() { + return $this->user; + } + + /** + * @return string + */ + public function getPassword() { + return $this->password; + } +} diff --git a/lib/private/Remote/Instance.php b/lib/private/Remote/Instance.php new file mode 100644 index 00000000000..10403af4ec7 --- /dev/null +++ b/lib/private/Remote/Instance.php @@ -0,0 +1,138 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote; + +use OC\Remote\Api\NotFoundException; +use OCP\Http\Client\IClientService; +use OCP\ICache; +use OCP\Remote\IInstance; + +/** + * Provides some basic info about a remote Nextcloud instance + */ +class Instance implements IInstance { + /** @var string */ + private $url; + + /** @var ICache */ + private $cache; + + /** @var IClientService */ + private $clientService; + + /** @var array|null */ + 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 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']; + } + + /** + * @return array + * @throws NotFoundException + * @throws \Exception + */ + private function getStatus() { + if ($this->status) { + return $this->status; + } + $key = 'remote/' . $this->url . '/status'; + $httpsKey = 'remote/' . $this->url . '/https'; + $status = $this->cache->get($key); + if (!$status) { + $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php'); + $protocol = 'https'; + if (!$response) { + 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'); + } + $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php'); + $protocol = 'http'; + } else { + $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365); + } + $status = json_decode($response, true); + if ($status) { + $status['protocol'] = $protocol; + } + if ($status) { + $this->cache->set($key, $status, 5 * 60); + $this->status = $status; + } else { + throw new NotFoundException('Remote server not found at address ' . $this->url); + } + } + return $status; + } + + /** + * @param string $url + * @return bool|string + */ + private function downloadStatus($url) { + try { + $request = $this->clientService->newClient()->get($url); + $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; + } catch (\Exception $e) { + return false; + } + } +} diff --git a/lib/private/Remote/InstanceFactory.php b/lib/private/Remote/InstanceFactory.php new file mode 100644 index 00000000000..f1b7a1de4ba --- /dev/null +++ b/lib/private/Remote/InstanceFactory.php @@ -0,0 +1,27 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote; + +use OCP\Http\Client\IClientService; +use OCP\ICache; +use OCP\Remote\IInstanceFactory; + +class InstanceFactory implements IInstanceFactory { + /** @var ICache */ + private $cache; + /** @var IClientService */ + private $clientService; + + public function __construct(ICache $cache, IClientService $clientService) { + $this->cache = $cache; + $this->clientService = $clientService; + } + + public function getInstance($url) { + return new Instance($url, $this->cache, $this->clientService); + } +} diff --git a/lib/private/Remote/User.php b/lib/private/Remote/User.php new file mode 100644 index 00000000000..ae1032cdc1c --- /dev/null +++ b/lib/private/Remote/User.php @@ -0,0 +1,122 @@ +<?php + +/** + * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +namespace OC\Remote; + +use OCP\Remote\IUser; + +class User implements IUser { + public const EXPECTED_KEYS = [ + 'id', + 'email', + 'displayname', + 'phone', + 'address', + 'website', + 'groups', + 'language', + 'quota' + ]; + + /** @var array */ + private $data; + + public function __construct(array $data) { + $this->data = $data; + } + + + /** + * @return string + */ + public function getUserId() { + return $this->data['id']; + } + + /** + * @return string + */ + public function getEmail() { + return $this->data['email']; + } + + /** + * @return string + */ + public function getDisplayName() { + return $this->data['displayname']; + } + + /** + * @return string + */ + public function getPhone() { + return $this->data['phone']; + } + + /** + * @return string + */ + public function getAddress() { + return $this->data['address']; + } + + /** + * @return string + */ + public function getWebsite() { + return $this->data['website']; + } + + /** + * @return string + */ + public function getTwitter() { + return $this->data['twitter'] ?? ''; + } + + /** + * @return string[] + */ + public function getGroups() { + return $this->data['groups']; + } + + /** + * @return string + */ + public function getLanguage() { + return $this->data['language']; + } + + /** + * @return int + */ + public function getUsedSpace() { + return $this->data['quota']['used']; + } + + /** + * @return int + */ + public function getFreeSpace() { + return $this->data['quota']['free']; + } + + /** + * @return int + */ + public function getTotalSpace() { + return $this->data['quota']['total']; + } + + /** + * @return int + */ + public function getQuota() { + return $this->data['quota']['quota']; + } +} |