2017-01-27 14:52:17 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-01-16 18:11:51 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2017, Robin Appelman <robin@icewind.nl>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
2017-02-09 15:32:36 +03:00
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* 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.
|
2017-01-27 14:52:17 +03:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2017-11-06 17:56:42 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-01-27 14:52:17 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2017-01-27 14:52:17 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Federation;
|
|
|
|
|
2020-11-16 19:56:44 +03:00
|
|
|
use OCP\Contacts\IManager;
|
2017-01-27 14:52:17 +03:00
|
|
|
use OCP\Federation\ICloudId;
|
|
|
|
use OCP\Federation\ICloudIdManager;
|
|
|
|
|
|
|
|
class CloudIdManager implements ICloudIdManager {
|
2020-11-16 19:56:44 +03:00
|
|
|
/** @var IManager */
|
|
|
|
private $contactsManager;
|
|
|
|
|
|
|
|
public function __construct(IManager $contactsManager) {
|
|
|
|
$this->contactsManager = $contactsManager;
|
|
|
|
}
|
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
/**
|
|
|
|
* @param string $cloudId
|
|
|
|
* @return ICloudId
|
2017-02-14 14:47:46 +03:00
|
|
|
* @throws \InvalidArgumentException
|
2017-01-27 14:52:17 +03:00
|
|
|
*/
|
2018-01-16 18:11:51 +03:00
|
|
|
public function resolveCloudId(string $cloudId): ICloudId {
|
2017-01-27 14:52:17 +03:00
|
|
|
// TODO magic here to get the url and user instead of just splitting on @
|
|
|
|
|
|
|
|
if (!$this->isValidCloudId($cloudId)) {
|
|
|
|
throw new \InvalidArgumentException('Invalid cloud id');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the first character that is not allowed in user names
|
2017-02-09 15:31:06 +03:00
|
|
|
$id = $this->fixRemoteURL($cloudId);
|
2017-01-27 14:52:17 +03:00
|
|
|
$posSlash = strpos($id, '/');
|
|
|
|
$posColon = strpos($id, ':');
|
|
|
|
|
|
|
|
if ($posSlash === false && $posColon === false) {
|
2018-01-16 18:11:51 +03:00
|
|
|
$invalidPos = \strlen($id);
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($posSlash === false) {
|
2017-01-27 14:52:17 +03:00
|
|
|
$invalidPos = $posColon;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($posColon === false) {
|
2017-01-27 14:52:17 +03:00
|
|
|
$invalidPos = $posSlash;
|
|
|
|
} else {
|
|
|
|
$invalidPos = min($posSlash, $posColon);
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:56:44 +03:00
|
|
|
$lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id));
|
2017-01-27 14:52:17 +03:00
|
|
|
|
2020-11-16 19:56:44 +03:00
|
|
|
if ($lastValidAtPos !== false) {
|
|
|
|
$user = substr($id, 0, $lastValidAtPos);
|
|
|
|
$remote = substr($id, $lastValidAtPos + 1);
|
2017-01-27 14:52:17 +03:00
|
|
|
if (!empty($user) && !empty($remote)) {
|
2020-11-16 19:56:44 +03:00
|
|
|
return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id));
|
2017-01-27 14:52:17 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new \InvalidArgumentException('Invalid cloud id');
|
|
|
|
}
|
|
|
|
|
2020-11-16 19:56:44 +03:00
|
|
|
protected function getDisplayNameFromContact(string $cloudId): ?string {
|
|
|
|
$addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD']);
|
|
|
|
foreach ($addressBookEntries as $entry) {
|
|
|
|
if (isset($entry['CLOUD'])) {
|
|
|
|
foreach ($entry['CLOUD'] as $cloudID) {
|
|
|
|
if ($cloudID === $cloudId) {
|
|
|
|
return $entry['FN'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-01-27 14:52:17 +03:00
|
|
|
/**
|
|
|
|
* @param string $user
|
|
|
|
* @param string $remote
|
|
|
|
* @return CloudId
|
|
|
|
*/
|
2018-01-16 18:11:51 +03:00
|
|
|
public function getCloudId(string $user, string $remote): ICloudId {
|
2017-01-27 14:52:17 +03:00
|
|
|
// TODO check what the correct url is for remote (asking the remote)
|
2020-11-16 19:56:44 +03:00
|
|
|
$fixedRemote = $this->fixRemoteURL($remote);
|
|
|
|
if (strpos($fixedRemote, 'http://') === 0) {
|
|
|
|
$host = substr($fixedRemote, strlen('http://'));
|
|
|
|
} elseif (strpos($fixedRemote, 'https://') === 0) {
|
|
|
|
$host = substr($fixedRemote, strlen('https://'));
|
|
|
|
} else {
|
|
|
|
$host = $fixedRemote;
|
|
|
|
}
|
|
|
|
$id = $user . '@' . $remote;
|
2020-11-25 01:14:43 +03:00
|
|
|
$displayName = $this->getDisplayNameFromContact($user . '@' . $host);
|
|
|
|
return new CloudId($id, $user, $fixedRemote, $displayName);
|
2017-01-27 14:52:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Strips away a potential file names and trailing slashes:
|
|
|
|
* - http://localhost
|
|
|
|
* - http://localhost/
|
|
|
|
* - http://localhost/index.php
|
|
|
|
* - http://localhost/index.php/s/{shareToken}
|
|
|
|
*
|
|
|
|
* all return: http://localhost
|
|
|
|
*
|
|
|
|
* @param string $remote
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-16 18:11:51 +03:00
|
|
|
protected function fixRemoteURL(string $remote): string {
|
2017-01-27 14:52:17 +03:00
|
|
|
$remote = str_replace('\\', '/', $remote);
|
|
|
|
if ($fileNamePosition = strpos($remote, '/index.php')) {
|
|
|
|
$remote = substr($remote, 0, $fileNamePosition);
|
|
|
|
}
|
|
|
|
$remote = rtrim($remote, '/');
|
|
|
|
|
|
|
|
return $remote;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $cloudId
|
|
|
|
* @return bool
|
|
|
|
*/
|
2018-01-16 18:11:51 +03:00
|
|
|
public function isValidCloudId(string $cloudId): bool {
|
2017-01-27 14:52:17 +03:00
|
|
|
return strpos($cloudId, '@') !== false;
|
|
|
|
}
|
2017-02-08 17:06:38 +03:00
|
|
|
}
|