[Pkg-owncloud-commits] [owncloud] 32/63: manually backport some helper methods

David Prévot taffit at moszumanska.debian.org
Tue Dec 22 16:50:57 UTC 2015


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch stable8.0
in repository owncloud.

commit 91d17790d66c26d387a8b72581558989931cdede
Author: Björn Schießle <bjoern at schiessle.org>
Date:   Mon Dec 14 18:28:38 2015 +0100

    manually backport some helper methods
---
 lib/private/share/helper.php | 74 ++++++++++++++++++++++++++++++++++++++++++++
 tests/lib/share/helper.php   | 72 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 146 insertions(+)

diff --git a/lib/private/share/helper.php b/lib/private/share/helper.php
index 3625779..054ba6f 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 49476e4..254e85a 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 at name', 'username'];
+		$protocols = ['', 'http://', 'https://'];
+		$remotes = [
+				'localhost',
+				'local.host',
+				'dev.local.host',
+				'dev.local.host/path',
+				'dev.local.host/at at inpath',
+				'127.0.0.1',
+				'::1',
+				'::192.0.2.128',
+				'::192.0.2.128/at 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 at server'),
+			array('us:er at 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);
+	}
 }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list