add remote server to the result

This commit is contained in:
Bjoern Schiessle 2016-02-12 15:27:39 +01:00
parent 159a0eb597
commit 11abf2487c
2 changed files with 168 additions and 4 deletions

View File

@ -279,6 +279,7 @@ class Sharees {
$cloudIds = [$cloudIds];
}
foreach ($cloudIds as $cloudId) {
list(, $serverUrl) = $this->splitUserRemote($cloudId);
if (strtolower($contact['FN']) === $search || strtolower($cloudId) === $search) {
if (strtolower($cloudId) === $search) {
$foundRemoteById = true;
@ -288,6 +289,7 @@ class Sharees {
'value' => [
'shareType' => Share::SHARE_TYPE_REMOTE,
'shareWith' => $cloudId,
'server' => $serverUrl,
],
];
} else {
@ -296,6 +298,7 @@ class Sharees {
'value' => [
'shareType' => Share::SHARE_TYPE_REMOTE,
'shareWith' => $cloudId,
'server' => $serverUrl,
],
];
}
@ -320,6 +323,74 @@ class Sharees {
$this->reachedEndFor[] = 'remotes';
}
/**
* split user and remote from federated cloud id
*
* @param string $address federated share address
* @return array [user, remoteURL]
* @throws \Exception
*/
public function splitUserRemote($address) {
if (strpos($address, '@') === false) {
throw new \Exception('Invalid Federated Cloud ID');
}
// Find the first character that is not allowed in user names
$id = str_replace('\\', '/', $address);
$posSlash = strpos($id, '/');
$posColon = strpos($id, ':');
if ($posSlash === false && $posColon === false) {
$invalidPos = strlen($id);
} else if ($posSlash === false) {
$invalidPos = $posColon;
} else if ($posColon === false) {
$invalidPos = $posSlash;
} else {
$invalidPos = min($posSlash, $posColon);
}
// Find the last @ before $invalidPos
$pos = $lastAtPos = 0;
while ($lastAtPos !== false && $lastAtPos <= $invalidPos) {
$pos = $lastAtPos;
$lastAtPos = strpos($id, '@', $pos + 1);
}
if ($pos !== false) {
$user = substr($id, 0, $pos);
$remote = substr($id, $pos + 1);
$remote = $this->fixRemoteURL($remote);
if (!empty($user) && !empty($remote)) {
return array($user, $remote);
}
}
throw new \Exception('Invalid Federated Cloud ID');
}
/**
* 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
*/
protected function fixRemoteURL($remote) {
$remote = str_replace('\\', '/', $remote);
if ($fileNamePosition = strpos($remote, '/index.php')) {
$remote = substr($remote, 0, $fileNamePosition);
}
$remote = rtrim($remote, '/');
return $remote;
}
/**
* @return \OC_OCS_Result
*/

View File

@ -805,7 +805,7 @@ class ShareesTest extends TestCase {
true,
[],
[
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost']],
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']],
],
true,
],
@ -855,7 +855,7 @@ class ShareesTest extends TestCase {
['label' => 'test@remote', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'test@remote']],
],
[
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost']],
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']],
],
true,
],
@ -904,7 +904,7 @@ class ShareesTest extends TestCase {
],
true,
[
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost']],
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']],
],
[],
true,
@ -929,7 +929,7 @@ class ShareesTest extends TestCase {
],
false,
[
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost']],
['label' => 'User @ Localhost', 'value' => ['shareType' => Share::SHARE_TYPE_REMOTE, 'shareWith' => 'username@localhost', 'server' => 'localhost']],
],
[],
true,
@ -1435,4 +1435,97 @@ class ShareesTest extends TestCase {
$this->assertArrayHasKey('message', $meta);
$this->assertSame($message, $meta['message']);
}
/**
* @dataProvider dataTestSplitUserRemote
*
* @param string $remote
* @param string $expectedUser
* @param string $expectedUrl
*/
public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
list($remoteUser, $remoteUrl) = $this->sharees->splitUserRemote($remote);
$this->assertSame($expectedUser, $remoteUser);
$this->assertSame($expectedUrl, $remoteUrl);
}
public function dataTestSplitUserRemote() {
$userPrefix = ['user@name', 'username'];
$protocols = ['', 'http://', 'https://'];
$remotes = [
'localhost',
'local.host',
'dev.local.host',
'dev.local.host/path',
'dev.local.host/at@inpath',
'127.0.0.1',
'::1',
'::192.0.2.128',
'::192.0.2.128/at@inpath',
];
$testCases = [];
foreach ($userPrefix as $user) {
foreach ($remotes as $remote) {
foreach ($protocols as $protocol) {
$baseUrl = $user . '@' . $protocol . $remote;
$testCases[] = [$baseUrl, $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
$testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
}
}
}
return $testCases;
}
public function dataTestSplitUserRemoteError() {
return array(
// Invalid path
array('user@'),
// Invalid user
array('@server'),
array('us/er@server'),
array('us:er@server'),
// Invalid splitting
array('user'),
array(''),
array('us/erserver'),
array('us:erserver'),
);
}
/**
* @dataProvider dataTestSplitUserRemoteError
*
* @param string $id
* @expectedException \Exception
*/
public function testSplitUserRemoteError($id) {
$this->sharees->splitUserRemote($id);
}
/**
* @dataProvider dataTestFixRemoteUrl
*
* @param string $url
* @param string $expected
*/
public function testFixRemoteUrl($url, $expected) {
$this->assertSame($expected,
$this->invokePrivate($this->sharees, 'fixRemoteURL', [$url])
);
}
public function dataTestFixRemoteUrl() {
return [
['http://localhost', 'http://localhost'],
['http://localhost/', 'http://localhost'],
['http://localhost/index.php', 'http://localhost'],
['http://localhost/index.php/s/AShareToken', 'http://localhost'],
];
}
}