Merge branch 'files_encryption' of https://github.com/owncloud/core into files_encryption

This commit is contained in:
Florin Peter 2013-05-07 22:19:58 +02:00
commit f82fb0e160

View file

@ -183,7 +183,7 @@ class Hooks {
/** /**
* @brief * @brief
*/ */
public static function postShared($params) { public static function postShared( $params ) {
// NOTE: $params has keys: // NOTE: $params has keys:
// [itemType] => file // [itemType] => file
@ -202,19 +202,20 @@ class Hooks {
// [id] => 10 // [id] => 10
// [token] => // [token] =>
// TODO: Should other kinds of item be encrypted too? // TODO: Should other kinds of item be encrypted too?
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
$view = new \OC_FilesystemView('/'); if ( $params['itemType'] === 'file' || $params['itemType'] === 'folder' ) {
$view = new \OC_FilesystemView( '/' );
$session = new Session($view); $session = new Session($view);
$userId = \OCP\User::getUser(); $userId = \OCP\User::getUser();
$util = new Util($view, $userId); $util = new Util($view, $userId);
$path = $util->fileIdToPath($params['itemSource']); $path = $util->fileIdToPath( $params['itemSource'] );
//if parent is set, then this is a re-share action //if parent is set, then this is a re-share action
if($params['parent']) { if( $params['parent'] ) {
// get the parent from current share // get the parent from current share
$parent = $util->getShareParent($params['parent']); $parent = $util->getShareParent( $params['parent'] );
// if parent is file the it is an 1:1 share // if parent is file the it is an 1:1 share
if($parent['item_type'] === 'file') { if($parent['item_type'] === 'file') {
@ -223,29 +224,41 @@ class Hooks {
$path = '/Shared'.$parent['file_target']; $path = '/Shared'.$parent['file_target'];
} else { } else {
// parent is folder but shared was a file!
// NOTE: parent is folder but shared was a file!
// we try to rebuild the missing path // we try to rebuild the missing path
// some examples we face here // some examples we face here
// user1 share folder1 with user2 folder1 has the following structure /folder1/subfolder1/subsubfolder1/somefile.txt // user1 share folder1 with user2 folder1 has
// the following structure
// /folder1/subfolder1/subsubfolder1/somefile.txt
// user2 re-share subfolder2 with user3 // user2 re-share subfolder2 with user3
// user3 re-share somefile.txt user4 // user3 re-share somefile.txt user4
// so our path should be /Shared/subfolder1/subsubfolder1/somefile.txt while user3 is sharing // so our path should be
if($params['itemType'] === 'file') { // /Shared/subfolder1/subsubfolder1/somefile.txt
// while user3 is sharing
if ( $params['itemType'] === 'file' ) {
// get target path // get target path
$targetPath = $util->fileIdToPath($params['fileSource']); $targetPath = $util->fileIdToPath( $params['fileSource'] );
$targetPathSplit = array_reverse(explode('/', $targetPath)); $targetPathSplit = array_reverse( explode( '/', $targetPath ) );
// init values // init values
$path = ''; $path = '';
$sharedPart = ltrim( $parent['file_target'], '/' ); $sharedPart = ltrim( $parent['file_target'], '/' );
// rebuild path // rebuild path
foreach ($targetPathSplit as $pathPart) { foreach ( $targetPathSplit as $pathPart ) {
if($pathPart !== $sharedPart) {
$path = '/'.$pathPart.$path; if( $pathPart !== $sharedPart ) {
$path = '/' . $pathPart . $path;
} else { } else {
break; break;
} }
} }
// prefix path with Shared // prefix path with Shared
@ -262,29 +275,36 @@ class Hooks {
$sharingEnabled = \OCP\Share::isEnabled(); $sharingEnabled = \OCP\Share::isEnabled();
// if a folder was shared, get a list if all (sub-)folders // if a folder was shared, get a list if all (sub-)folders
if ($params['itemType'] === 'folder') { if ( $params['itemType'] === 'folder' ) {
$allFiles = $util->getAllFiles($path); $allFiles = $util->getAllFiles($path);
} else { } else {
$allFiles = array($path);
$allFiles = array( $path );
} }
foreach ($allFiles as $path) { foreach ( $allFiles as $path ) {
$usersSharing = $util->getSharingUsersArray($sharingEnabled, $path);
$usersSharing = $util->getSharingUsersArray( $sharingEnabled, $path );
$failed = array(); $failed = array();
// Attempt to set shareKey // Attempt to set shareKey
if (!$util->setSharedFileKeyfiles($session, $usersSharing, $path)) { if ( !$util->setSharedFileKeyfiles( $session, $usersSharing, $path ) ) {
$failed[] = $path; $failed[] = $path;
} }
} }
// If no attempts to set keyfiles failed // If no attempts to set keyfiles failed
if (empty($failed)) { if ( empty( $failed ) ) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
} }
@ -292,8 +312,7 @@ class Hooks {
/** /**
* @brief * @brief
*/ */
public static function postUnshare($params) public static function postUnshare( $params ) {
{
// NOTE: $params has keys: // NOTE: $params has keys:
// [itemType] => file // [itemType] => file
@ -302,34 +321,40 @@ class Hooks {
// [shareWith] => test1 // [shareWith] => test1
// [itemParent] => // [itemParent] =>
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { if ( $params['itemType'] === 'file' || $params['itemType'] === 'folder' ) {
$view = new \OC_FilesystemView('/'); $view = new \OC_FilesystemView( '/' );
$userId = \OCP\User::getUser(); $userId = \OCP\User::getUser();
$util = new Util($view, $userId); $util = new Util( $view, $userId);
$path = $util->fileIdToPath($params['itemSource']); $path = $util->fileIdToPath( $params['itemSource'] );
// check if this is a re-share // check if this is a re-share
if ($params['itemParent']) { if ( $params['itemParent'] ) {
// get the parent from current share // get the parent from current share
$parent = $util->getShareParent($params['itemParent']); $parent = $util->getShareParent( $params['itemParent'] );
// get target path // get target path
$targetPath = $util->fileIdToPath($params['itemSource']); $targetPath = $util->fileIdToPath( $params['itemSource'] );
$targetPathSplit = array_reverse(explode('/', $targetPath)); $targetPathSplit = array_reverse( explode( '/', $targetPath ) );
// init values // init values
$path = ''; $path = '';
$sharedPart = ltrim($parent['file_target'], '/'); $sharedPart = ltrim( $parent['file_target'], '/' );
// rebuild path // rebuild path
foreach ($targetPathSplit as $pathPart) { foreach ( $targetPathSplit as $pathPart ) {
if ($pathPart !== $sharedPart) {
if ( $pathPart !== $sharedPart ) {
$path = '/' . $pathPart . $path; $path = '/' . $pathPart . $path;
} else { } else {
break; break;
} }
} }
// prefix path with Shared // prefix path with Shared
@ -337,37 +362,51 @@ class Hooks {
} }
// for group shares get a list of the group members // for group shares get a list of the group members
if ($params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP) { if ( $params['shareType'] == \OCP\Share::SHARE_TYPE_GROUP ) {
$userIds = \OC_Group::usersInGroup($params['shareWith']); $userIds = \OC_Group::usersInGroup($params['shareWith']);
} else { } else {
$userIds = array($params['shareWith']);
$userIds = array( $params['shareWith'] );
} }
// if we unshare a folder we need a list of all (sub-)files // if we unshare a folder we need a list of all (sub-)files
if ($params['itemType'] === 'folder') { if ( $params['itemType'] === 'folder' ) {
$allFiles = $util->getAllFiles($path);
$allFiles = $util->getAllFiles( $path );
} else { } else {
$allFiles = array($path);
$allFiles = array( $path );
} }
foreach ($allFiles as $path) { foreach ( $allFiles as $path ) {
// check if the user still has access to the file, otherwise delete share key // check if the user still has access to the file, otherwise delete share key
$sharingUsers = $util->getSharingUsersArray(true, $path); $sharingUsers = $util->getSharingUsersArray( true, $path );
// Unshare every user who no longer has access to the file // Unshare every user who no longer has access to the file
$delUsers = array_diff($userIds, $sharingUsers); $delUsers = array_diff( $userIds, $sharingUsers);
if (!Keymanager::delShareKey($view, $delUsers, $path)) {
if ( !Keymanager::delShareKey( $view, $delUsers, $path ) ) {
$failed[] = $path; $failed[] = $path;
} }
} }
// If no attempts to set keyfiles failed // If no attempts to set keyfiles failed
if (empty($failed)) { if ( empty( $failed ) ) {
return true; return true;
} else { } else {
return false; return false;
} }
} }
} }