From fb88aba8f4927b3175df34a2a499978a3b4c1b6b Mon Sep 17 00:00:00 2001
From: Bjoern Schiessle <schiessle@owncloud.com>
Date: Tue, 15 Apr 2014 20:18:04 +0200
Subject: [PATCH] some fixes to make the gallery work, this made it necessary
 to adjust some tests and the encryption code

---
 apps/files_encryption/hooks/hooks.php | 127 +++-----------------------
 apps/files_encryption/lib/util.php    | 101 ++------------------
 apps/files_sharing/lib/cache.php      |   5 +-
 apps/files_sharing/tests/cache.php    |  18 ++--
 lib/private/share/share.php           |   1 +
 5 files changed, 33 insertions(+), 219 deletions(-)

diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php
index 7a8b54e2d7..5f0494e62c 100644
--- a/apps/files_encryption/hooks/hooks.php
+++ b/apps/files_encryption/hooks/hooks.php
@@ -302,25 +302,6 @@ class Hooks {
 	 */
 	public static function postShared($params) {
 
-		// NOTE: $params has keys:
-		// [itemType] => file
-		// itemSource -> int, filecache file ID
-		// [parent] =>
-		// [itemTarget] => /13
-		// shareWith -> string, uid of user being shared to
-		// fileTarget -> path of file being shared
-		// uidOwner -> owner of the original file being shared
-		// [shareType] => 0
-		// [shareWith] => test1
-		// [uidOwner] => admin
-		// [permissions] => 17
-		// [fileSource] => 13
-		// [fileTarget] => /test8
-		// [id] => 10
-		// [token] =>
-		// [run] => whether emitting script should continue to run
-		// TODO: Should other kinds of item be encrypted too?
-
 		if (\OCP\App::isEnabled('files_encryption') === false) {
 			return true;
 		}
@@ -331,68 +312,22 @@ class Hooks {
 			$session = new \OCA\Encryption\Session($view);
 			$userId = \OCP\User::getUser();
 			$util = new Util($view, $userId);
-			$path = $util->fileIdToPath($params['itemSource']);
-
-			$share = $util->getParentFromShare($params['id']);
-			//if parent is set, then this is a re-share action
-			if ($share['parent'] !== null) {
-
-				// get the parent from current share
-				$parent = $util->getShareParent($params['parent']);
-
-				// if parent has the same type than the child it is a 1:1 share
-				if ($parent['item_type'] === $params['itemType']) {
-					$path = $parent['file_target'];
-				} else {
-
-					// NOTE: parent is folder but shared was a file!
-					// we try to rebuild the missing path
-					// some examples we face here
-					// user1 share folder1 with user2 folder1 has
-					// the following structure
-					// /folder1/subfolder1/subsubfolder1/somefile.txt
-					// user2 re-share subfolder2 with user3
-					// user3 re-share somefile.txt user4
-					// so our path should be
-					// /Shared/subfolder1/subsubfolder1/somefile.txt
-					// while user3 is sharing
-
-					if ($params['itemType'] === 'file') {
-						// get target path
-						$targetPath = $util->fileIdToPath($params['fileSource']);
-						$targetPathSplit = array_reverse(explode('/', $targetPath));
-
-						// init values
-						$path = '';
-						$sharedPart = ltrim($parent['file_target'], '/');
-
-						// rebuild path
-						foreach ($targetPathSplit as $pathPart) {
-							if ($pathPart !== $sharedPart) {
-								$path = '/' . $pathPart . $path;
-							} else {
-								break;
-							}
-						}
-						$path = $parent['file_target'] . $path;
-					} else {
-						$path = $parent['file_target'] . $params['fileTarget'];
-					}
-				}
-			}
+			$path = \OC\Files\Filesystem::getPath($params['fileSource']);
 
 			$sharingEnabled = \OCP\Share::isEnabled();
 
 			// get the path including mount point only if not a shared folder
 			list($storage, ) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/files' . $path);
-			if (!($storage instanceof \OC\Files\Storage\Shared)) {
-				// get path including the the storage mount point
-				$path = $util->getPathWithMountPoint($params['itemSource']);
+
+			if (!($storage instanceof \OC\Files\Storage\Local)) {
+				$mountPoint = 'files' . $storage->getMountPoint();
+			} else {
+				$mountPoint = '';
 			}
 
 			// if a folder was shared, get a list of all (sub-)folders
 			if ($params['itemType'] === 'folder') {
-				$allFiles = $util->getAllFiles($path);
+				$allFiles = $util->getAllFiles($path, $mountPoint);
 			} else {
 				$allFiles = array($path);
 			}
@@ -409,13 +344,6 @@ class Hooks {
 	 */
 	public static function postUnshare($params) {
 
-		// NOTE: $params has keys:
-		// [itemType] => file
-		// [itemSource] => 13
-		// [shareType] => 0
-		// [shareWith] => test1
-		// [itemParent] =>
-
 		if (\OCP\App::isEnabled('files_encryption') === false) {
 			return true;
 		}
@@ -425,34 +353,7 @@ class Hooks {
 			$view = new \OC_FilesystemView('/');
 			$userId = \OCP\User::getUser();
 			$util = new Util($view, $userId);
-			$path = $util->fileIdToPath($params['itemSource']);
-
-			// check if this is a re-share
-			if ($params['itemParent']) {
-
-				// get the parent from current share
-				$parent = $util->getShareParent($params['itemParent']);
-
-				// get target path
-				$targetPath = $util->fileIdToPath($params['itemSource']);
-				$targetPathSplit = array_reverse(explode('/', $targetPath));
-
-				// init values
-				$path = '';
-				$sharedPart = ltrim($parent['file_target'], '/');
-
-				// rebuild path
-				foreach ($targetPathSplit as $pathPart) {
-					if ($pathPart !== $sharedPart) {
-						$path = '/' . $pathPart . $path;
-					} else {
-						break;
-					}
-				}
-
-				// prefix path with Shared
-				$path = $parent['file_target'] . $path;
-			}
+			$path = \OC\Files\Filesystem::getPath($params['fileSource']);
 
 			// for group shares get a list of the group members
 			if ($params['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) {
@@ -466,15 +367,17 @@ class Hooks {
 			}
 
 			// get the path including mount point only if not a shared folder
-			list($storage, ) = \OC\Files\Filesystem::resolvePath($path);
-			if (!($storage instanceof \OC\Files\Storage\Shared)) {
-				// get path including the the storage mount point
-				//$path = $util->getPathWithMountPoint($params['itemSource']);
+			list($storage, ) = \OC\Files\Filesystem::resolvePath('/' . $userId . '/files' . $path);
+
+			if (!($storage instanceof \OC\Files\Storage\Local)) {
+				$mountPoint = 'files' . $storage->getMountPoint();
+			} else {
+				$mountPoint = '';
 			}
 
 			// if we unshare a folder we need a list of all (sub-)files
 			if ($params['itemType'] === 'folder') {
-				$allFiles = $util->getAllFiles($path);
+				$allFiles = $util->getAllFiles($path, $mountPoint);
 			} else {
 				$allFiles = array($path);
 			}
diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php
index 4be4ab9565..6372ab31b6 100644
--- a/apps/files_encryption/lib/util.php
+++ b/apps/files_encryption/lib/util.php
@@ -969,33 +969,6 @@ class Util {
 
 	}
 
-	/**
-	 * @brief get path of a file.
-	 * @param int $fileId id of the file
-	 * @return string path of the file
-	 */
-	public static function fileIdToPath($fileId) {
-
-		$sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?';
-
-		$query = \OCP\DB::prepare($sql);
-
-		$result = $query->execute(array($fileId));
-
-		$path = false;
-		if (\OCP\DB::isError($result)) {
-			\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
-		} else {
-			$row = $result->fetchRow();
-			if ($row) {
-				$path = substr($row['path'], strlen('files'));
-			}
-		}
-
-		return $path;
-
-	}
-
 	/**
 	 * @brief Filter an array of UIDs to return only ones ready for sharing
 	 * @param array $unfilteredUsers users to be checked for sharing readiness
@@ -1398,7 +1371,7 @@ class Util {
 	 * @param string $dir relative to the users files folder
 	 * @return array with list of files relative to the users files folder
 	 */
-	public function getAllFiles($dir) {
+	public function getAllFiles($dir, $mountPoint = '') {
 		$result = array();
 		$dirList = array($dir);
 
@@ -1408,10 +1381,13 @@ class Util {
 					$this->userFilesDir . '/' . $dir));
 
 			foreach ($content as $c) {
+				// getDirectoryContent() returns the paths relative to the mount points, so we need
+				// to re-construct the complete path
+				$path = ($mountPoint !== '') ? $mountPoint . '/' .  $c['path'] : $c['path'];
 				if ($c['type'] === 'dir') {
-					$dirList[] = substr($c['path'], strlen("files"));
+					$dirList[] = substr($path, strlen("files"));
 				} else {
-					$result[] = substr($c['path'], strlen("files"));
+					$result[] = substr($path, strlen("files"));
 				}
 			}
 
@@ -1420,54 +1396,6 @@ class Util {
 		return $result;
 	}
 
-	/**
-	 * @brief get shares parent.
-	 * @param int $id of the current share
-	 * @return array of the parent
-	 */
-	public static function getShareParent($id) {
-
-		$sql = 'SELECT `file_target`, `item_type` FROM `*PREFIX*share` WHERE `id` = ?';
-
-		$query = \OCP\DB::prepare($sql);
-
-		$result = $query->execute(array($id));
-
-		$row = array();
-		if (\OCP\DB::isError($result)) {
-			\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
-		} else {
-			$row = $result->fetchRow();
-		}
-
-		return $row;
-
-	}
-
-	/**
-	 * @brief get shares parent.
-	 * @param int $id of the current share
-	 * @return array of the parent
-	 */
-	public static function getParentFromShare($id) {
-
-		$sql = 'SELECT `parent` FROM `*PREFIX*share` WHERE `id` = ?';
-
-		$query = \OCP\DB::prepare($sql);
-
-		$result = $query->execute(array($id));
-
-		$row = array();
-		if (\OCP\DB::isError($result)) {
-			\OCP\Util::writeLog('Encryption library', \OC_DB::getErrorMessage($result), \OCP\Util::ERROR);
-		} else {
-			$row = $result->fetchRow();
-		}
-
-		return $row;
-
-	}
-
 	/**
 	 * @brief get owner of the shared files.
 	 * @param $id
@@ -1709,23 +1637,6 @@ class Util {
 		$this->recoverAllFiles('/', $privateKey);
 	}
 
-	/**
-	 * Get the path including the storage mount point
-	 * @param int $id
-	 * @return string the path including the mount point like AmazonS3/folder/file.txt
-	 */
-	public function getPathWithMountPoint($id) {
-		list($storage, $internalPath) = \OC\Files\Cache\Cache::getById($id);
-		$mount = \OC\Files\Filesystem::getMountByStorageId($storage);
-		$mountPoint = $mount[0]->getMountPoint();
-		$path = \OC\Files\Filesystem::normalizePath($mountPoint . '/' . $internalPath);
-
-		// reformat the path to be relative e.g. /user/files/folder becomes /folder/
-		$relativePath = \OCA\Encryption\Helper::stripUserFilesPath($path);
-
-		return $relativePath;
-	}
-
 	/**
 	 * @brief check if the file is stored on a system wide mount point
 	 * @param $path relative to /data/user with leading '/'
diff --git a/apps/files_sharing/lib/cache.php b/apps/files_sharing/lib/cache.php
index 3d9fbcf4de..4b473c6057 100644
--- a/apps/files_sharing/lib/cache.php
+++ b/apps/files_sharing/lib/cache.php
@@ -141,15 +141,14 @@ class Shared_Cache extends Cache {
 			$folder = '';
 		}
 
-		$dir = 'files' . $this->storage->getMountPoint();
-		$dir .= ($folder !== '') ? '/' . $folder : '';
+		$dir = ($folder !== '') ? $folder . '/' : '';
 
 		$cache = $this->getSourceCache($folder);
 		if ($cache) {
 			$parent = $this->storage->getFile($folder);
 			$sourceFolderContent = $cache->getFolderContents($this->files[$folder]);
 			foreach ($sourceFolderContent as $key => $c) {
-				$sourceFolderContent[$key]['path'] = $dir . '/' . $c['name'];
+				$sourceFolderContent[$key]['path'] = $dir . $c['name'];
 				$sourceFolderContent[$key]['uid_owner'] = $parent['uid_owner'];
 				$sourceFolderContent[$key]['displayname_owner'] = $parent['uid_owner'];
 			}
diff --git a/apps/files_sharing/tests/cache.php b/apps/files_sharing/tests/cache.php
index b8ebeab3c3..1af73c558d 100644
--- a/apps/files_sharing/tests/cache.php
+++ b/apps/files_sharing/tests/cache.php
@@ -100,15 +100,15 @@ class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base {
 		$check = array(
 				array(
 					'name' => 'bar.txt',
-					'path' => 'files/shareddir/bar.txt'
+					'path' => 'bar.txt'
 				),
 				array(
 					'name' => 'another too.txt',
-					'path' => 'files/shareddir/subdir/another too.txt'
+					'path' => 'subdir/another too.txt'
 				),
 				array(
 					'name' => 'another.txt',
-					'path' => 'files/shareddir/subdir/another.txt'
+					'path' => 'subdir/another.txt'
 				),
 			);
 		$this->verifyFiles($check, $results);
@@ -151,17 +151,17 @@ class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base {
 			array(
 				array(
 					'name' => 'bar.txt',
-					'path' => 'files/shareddir/bar.txt',
+					'path' => 'bar.txt',
 					'mimetype' => 'text/plain',
 				),
 				array(
 					'name' => 'emptydir',
-					'path' => 'files/shareddir/emptydir',
+					'path' => 'emptydir',
 					'mimetype' => 'httpd/unix-directory',
 				),
 				array(
 					'name' => 'subdir',
-					'path' => 'files/shareddir/subdir',
+					'path' => 'subdir',
 					'mimetype' => 'httpd/unix-directory',
 				),
 			),
@@ -185,17 +185,17 @@ class Test_Files_Sharing_Cache extends Test_Files_Sharing_Base {
 			array(
 				array(
 					'name' => 'another too.txt',
-					'path' => 'files/subdir/another too.txt',
+					'path' => 'another too.txt',
 					'mimetype' => 'text/plain',
 				),
 				array(
 					'name' => 'another.txt',
-					'path' => 'files/subdir/another.txt',
+					'path' => 'another.txt',
 					'mimetype' => 'text/plain',
 				),
 				array(
 					'name' => 'not a text file.xml',
-					'path' => 'files/subdir/not a text file.xml',
+					'path' => 'not a text file.xml',
 					'mimetype' => 'application/xml',
 				),
 			),
diff --git a/lib/private/share/share.php b/lib/private/share/share.php
index c07dd04b29..4e3e261baf 100644
--- a/lib/private/share/share.php
+++ b/lib/private/share/share.php
@@ -896,6 +896,7 @@ class Share extends \OC\Share\Constants {
 		$hookParams = array(
 			'itemType'      => $item['item_type'],
 			'itemSource'    => $item['item_source'],
+			'fileSource'    => $item['file_source'],
 			'shareType'     => $item['share_type'],
 			'shareWith'     => $item['share_with'],
 			'itemParent'    => $item['parent'],
-- 
GitLab