diff options
author | Björn Schießle <bjoern@schiessle.org> | 2015-12-14 20:28:38 +0300 |
---|---|---|
committer | Björn Schießle <bjoern@schiessle.org> | 2015-12-15 13:43:18 +0300 |
commit | 91d17790d66c26d387a8b72581558989931cdede (patch) | |
tree | cbc3234bed9187bb8a43f23b7b6b0d0167044408 /lib | |
parent | f6be4f8ec1873359680938000f45ac9c55c43fd1 (diff) |
manually backport some helper methods
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/share/helper.php | 74 |
1 files changed, 74 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; + } } |