From a2ba3c8a43780eab5ad8a4d96db33c584e7ae2ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Schie=C3=9Fle?= Date: Mon, 22 Apr 2013 11:58:39 +0200 Subject: [PATCH] fix sharing of folders. First we need to collect all files. Than we need to find all users with access to the file because this can vary from file to file and than we can encrypt it for all recipients --- apps/files_encryption/hooks/hooks.php | 72 +++++++++++++-------------- apps/files_encryption/lib/util.php | 20 ++++++++ 2 files changed, 54 insertions(+), 38 deletions(-) diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 2731ee1112..13cf352b4e 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -166,8 +166,8 @@ class Hooks { /** * @brief */ - public static function postShared( $params ) { - + public static function postShared($params) { + // NOTE: $params has keys: // [itemType] => file // itemSource -> int, filecache file ID @@ -183,50 +183,46 @@ class Hooks { // [fileSource] => 13 // [fileTarget] => /test8 // [id] => 10 - // [token] => - + // [token] => // TODO: Should other kinds of item be encrypted too? - if ( $params['itemType'] === 'file' ) { - - $view = new \OC_FilesystemView( '/' ); + if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { + + $view = new \OC_FilesystemView('/'); $session = new Session($view); $userId = \OCP\User::getUser(); - $util = new Util( $view, $userId ); - $path = $util->fileIdToPath( $params['itemSource'] ); + $util = new Util($view, $userId); + $path = $util->fileIdToPath($params['itemSource']); $sharingEnabled = \OCP\Share::isEnabled(); - $usersSharing = $util->getSharingUsersArray( $sharingEnabled, $path); - - // Recursively expand path to include subfiles - $allPaths = $util->getPaths( $path ); - - $failed = array(); - - // Loop through all subfiles - foreach ( $allPaths as $path ) { - - // Attempt to set shareKey - if ( ! $util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) { - - $failed[] = $path; - - } - - } - - // If no attempts to set keyfiles failed - if ( empty( $failed ) ) { - - return true; - + + if ($params['itemType'] === 'folder') { + //list($owner, $ownerPath) = $util->getUidAndFilename($filePath); + $allFiles = $util->getAllFiles($path); } else { - - return false; - + $allFiles = array($path); + } + + foreach ($allFiles as $path) { + $usersSharing = $util->getSharingUsersArray($sharingEnabled, $path); + + $failed = array(); + + // Attempt to set shareKey + if (!$util->setSharedFileKeyfiles($session, $usersSharing, $path)) { + + $failed[] = $path; + } + } + + // If no attempts to set keyfiles failed + if (empty($failed)) { + + return true; + } else { + + return false; } - } - } /** diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 8807de7c2a..b3df7f0db0 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -939,4 +939,24 @@ class Util { } + /** + *@ brief geo recursively through a dir and collect all files and sub files. + * @param type $dir relative to the users files folder + * @return array with list of files relative to the users files folder + */ + public function getAllFiles($dir) { + $result = array(); + $path = $this->view->getLocalFile(); + $content = $this->view->getDirectoryContent("/".$this->userFilesDir.'/'.$this->filesFolderName.$dir); + + foreach ($content as $c) { + if ($c['type'] === "dir" ) { + $result = array_merge($result, $this->getAllFiles(substr($c['path'],5))); + } else { + $result[] = substr($c['path'], 5); + } + } + return $result; + } + }