diff options
author | Björn Schießle <bjoern@schiessle.org> | 2015-12-14 18:28:38 +0100 |
---|---|---|
committer | Björn Schießle <bjoern@schiessle.org> | 2015-12-15 11:43:18 +0100 |
commit | 91d17790d66c26d387a8b72581558989931cdede (patch) | |
tree | cbc3234bed9187bb8a43f23b7b6b0d0167044408 | |
parent | f6be4f8ec1873359680938000f45ac9c55c43fd1 (diff) | |
download | nextcloud-server-91d17790d66c26d387a8b72581558989931cdede.tar.gz nextcloud-server-91d17790d66c26d387a8b72581558989931cdede.zip |
manually backport some helper methods
-rw-r--r-- | lib/private/share/helper.php | 74 | ||||
-rw-r--r-- | tests/lib/share/helper.php | 72 |
2 files changed, 146 insertions, 0 deletions
diff --git a/lib/private/share/helper.php b/lib/private/share/helper.php index 362577955d2..054ba6fb5f1 100644 --- a/lib/private/share/helper.php +++ b/lib/private/share/helper.php @@ -21,6 +21,8 @@ namespace OC\Share; +use OC\HintException; + class Helper extends \OC\Share\Constants { /** @@ -274,4 +276,76 @@ class Helper extends \OC\Share\Constants { return false; } + + /** + * split user and remote from federated cloud id + * + * @param string $id + * @return array + * @throws HintException + */ + public static function splitUserRemote($id) { + if (strpos($id, '@') === false) { + $l = \OC::$server->getL10N('core'); + $hint = $l->t('Invalid Federated Cloud ID'); + throw new HintException('Invalid Federated Cloud ID', $hint); + } + + // Find the first character that is not allowed in user names + $id = str_replace('\\', '/', $id); + $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 = self::fixRemoteURL($remote); + if (!empty($user) && !empty($remote)) { + return array($user, $remote); + } + } + + $l = \OC::$server->getL10N('core'); + $hint = $l->t('Invalid Federated Cloud ID'); + throw new HintException('Invalid Fededrated Cloud ID', $hint); + } + + /** + * 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 static function fixRemoteURL($remote) { + $remote = str_replace('\\', '/', $remote); + if ($fileNamePosition = strpos($remote, '/index.php')) { + $remote = substr($remote, 0, $fileNamePosition); + } + $remote = rtrim($remote, '/'); + + return $remote; + } } diff --git a/tests/lib/share/helper.php b/tests/lib/share/helper.php index 49476e4bc43..254e85ada5a 100644 --- a/tests/lib/share/helper.php +++ b/tests/lib/share/helper.php @@ -137,4 +137,76 @@ class Test_Share_Helper extends \Test\TestCase { ['user1', 'server1', 'user2', 'http://server1', false], ]; } + + 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; + } + + /** + * @dataProvider dataTestSplitUserRemote + * + * @param string $remote + * @param string $expectedUser + * @param string $expectedUrl + */ + public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) { + list($remoteUser, $remoteUrl) = \OC\Share\Helper::splitUserRemote($remote); + $this->assertSame($expectedUser, $remoteUser); + $this->assertSame($expectedUrl, $remoteUrl); + } + + 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 \OC\HintException + */ + public function testSplitUserRemoteError($id) { + \OC\Share\Helper::splitUserRemote($id); + } } |