diff options
author | Björn Schießle <bjoern@schiessle.org> | 2015-10-29 17:27:14 +0100 |
---|---|---|
committer | Björn Schießle <bjoern@schiessle.org> | 2015-11-19 18:06:38 +0100 |
commit | ed039ee5ebdba6778b245f249fe206d2423a6a36 (patch) | |
tree | aa7a172b02e20eb878399c39624ee47496b4fee2 /apps/federation/lib | |
parent | 479cee66f40cbb6340b0c1f106101692edde821a (diff) | |
download | nextcloud-server-ed039ee5ebdba6778b245f249fe206d2423a6a36.tar.gz nextcloud-server-ed039ee5ebdba6778b245f249fe206d2423a6a36.zip |
added app "federation", allows you to connect ownClouds and exchange user lists
Diffstat (limited to 'apps/federation/lib')
-rw-r--r-- | apps/federation/lib/dbhandler.php | 147 | ||||
-rw-r--r-- | apps/federation/lib/trustedservers.php | 160 |
2 files changed, 307 insertions, 0 deletions
diff --git a/apps/federation/lib/dbhandler.php b/apps/federation/lib/dbhandler.php new file mode 100644 index 00000000000..1100875cc23 --- /dev/null +++ b/apps/federation/lib/dbhandler.php @@ -0,0 +1,147 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace OCA\Federation; + + +use OC\HintException; +use OCP\IDBConnection; +use OCP\IL10N; + +class DbHandler { + + /** @var IDBConnection */ + private $connection; + + /** @var IL10N */ + private $l; + + /** @var string */ + private $dbTable = 'trusted_servers'; + + /** + * @param IDBConnection $connection + * @param IL10N $il10n + */ + public function __construct( + IDBConnection $connection, + IL10N $il10n + ) { + $this->connection = $connection; + $this->IL10N = $il10n; + } + + /** + * add server to the list of trusted ownCloud servers + * + * @param $url + * @return int + * @throws HintException + */ + public function add($url) { + $hash = md5($url); + $query = $this->connection->getQueryBuilder(); + $query->insert($this->dbTable) + ->values( + [ + 'url' => $query->createParameter('url'), + 'url_hash' => $query->createParameter('url_hash'), + ] + ) + ->setParameter('url', $url) + ->setParameter('url_hash', $hash); + + $result = $query->execute(); + + if ($result) { + $id = $this->connection->lastInsertId(); + // Fallback, if lastInterId() doesn't work we need to perform a select + // to get the ID (seems to happen sometimes on Oracle) + if (!$id) { + $server = $this->get($url); + $id = $server['id']; + } + return $id; + } else { + $message = 'Internal failure, Could not add ownCloud as trusted server: ' . $url; + $message_t = $this->l->t('Could not add server'); + throw new HintException($message, $message_t); + } + } + + /** + * remove server from the list of trusted ownCloud servers + * + * @param int $id + */ + public function remove($id) { + $query = $this->connection->getQueryBuilder(); + $query->delete($this->dbTable) + ->where($query->expr()->eq('id', $query->createParameter('id'))) + ->setParameter('id', $id); + $query->execute(); + } + + /** + * get trusted server from database + * + * @param $url + * @return mixed + */ + public function get($url) { + $query = $this->connection->getQueryBuilder(); + $query->select('url', 'id')->from($this->dbTable) + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) + ->setParameter('url_hash', md5($url)); + + return $query->execute()->fetch(); + } + + /** + * get all trusted servers + * + * @return array + */ + public function getAll() { + $query = $this->connection->getQueryBuilder(); + $query->select('url', 'id')->from($this->dbTable); + $result = $query->execute()->fetchAll(); + return $result; + } + + /** + * check if server already exists in the database table + * + * @param string $url + * @return bool + */ + public function exists($url) { + $query = $this->connection->getQueryBuilder(); + $query->select('url')->from($this->dbTable) + ->where($query->expr()->eq('url_hash', $query->createParameter('url_hash'))) + ->setParameter('url_hash', md5($url)); + $result = $query->execute()->fetchAll(); + + return !empty($result); + } + +} diff --git a/apps/federation/lib/trustedservers.php b/apps/federation/lib/trustedservers.php new file mode 100644 index 00000000000..bf31277b2a8 --- /dev/null +++ b/apps/federation/lib/trustedservers.php @@ -0,0 +1,160 @@ +<?php +/** + * @author Björn Schießle <schiessle@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * 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, version 3, + * along with this program. If not, see <http://www.gnu.org/licenses/> + * + */ + + +namespace OCA\Federation; + + +use OC\Files\Filesystem; +use OCP\AppFramework\Http; +use OCP\Http\Client\IClientService; +use OCP\IDBConnection; +use OCP\ILogger; + +class TrustedServers { + + /** @var dbHandler */ + private $dbHandler; + + /** @var IClientService */ + private $httpClientService; + + /** @var ILogger */ + private $logger; + + private $dbTable = 'trusted_servers'; + + /** + * @param DbHandler $dbHandler + * @param IClientService $httpClientService + * @param ILogger $logger + */ + public function __construct( + DbHandler $dbHandler, + IClientService $httpClientService, + ILogger $logger + ) { + $this->dbHandler = $dbHandler; + $this->httpClientService = $httpClientService; + $this->logger = $logger; + } + + /** + * add server to the list of trusted ownCloud servers + * + * @param $url + * @return int server id + */ + public function addServer($url) { + return $this->dbHandler->add($this->normalizeUrl($url)); + } + + /** + * remove server from the list of trusted ownCloud servers + * + * @param int $id + */ + public function removeServer($id) { + $this->dbHandler->remove($id); + } + + /** + * get all trusted servers + * + * @return array + */ + public function getServers() { + return $this->dbHandler->getAll(); + } + + /** + * check if given server is a trusted ownCloud server + * + * @param string $url + * @return bool + */ + public function isTrustedServer($url) { + return $this->dbHandler->exists($this->normalizeUrl($url)); + } + + /** + * check if URL point to a ownCloud server + * + * @param string $url + * @return bool + */ + public function isOwnCloudServer($url) { + $isValidOwnCloud = false; + $client = $this->httpClientService->newClient(); + try { + $result = $client->get( + $url . '/status.php', + [ + 'timeout' => 3, + 'connect_timeout' => 3, + ] + ); + if ($result->getStatusCode() === Http::STATUS_OK) { + $isValidOwnCloud = $this->checkOwnCloudVersion($result->getBody()); + } + } catch (\Exception $e) { + $this->logger->error($e->getMessage(), ['app' => 'federation']); + return false; + } + return $isValidOwnCloud; + } + + /** + * check if ownCloud version is >= 9.0 + * + * @param $statusphp + * @return bool + */ + protected function checkOwnCloudVersion($statusphp) { + $decoded = json_decode($statusphp, true); + if (!empty($decoded) && isset($decoded['version'])) { + return version_compare($decoded['version'], '9.0.0', '>='); + } + return false; + } + + /** + * normalize URL + * + * @param string $url + * @return string + */ + protected function normalizeUrl($url) { + + $normalized = $url; + + if (strpos($url, 'https://') === 0) { + $normalized = substr($url, strlen('https://')); + } else if (strpos($url, 'http://') === 0) { + $normalized = substr($url, strlen('http://')); + } + + $normalized = Filesystem::normalizePath($normalized); + $normalized = trim($normalized, '/'); + + return $normalized; + } +} |