@ -60,9 +60,23 @@ class Keymanager {
* @param string user name of the file owner
* @return string file key or false
*/
public static function getFileKey($user, $file) {
public static function getFileKey($userId, $path) {
$keypath = ltrim($path, '/');
$user = $userId;
// update $keypath and $user if path point to a file shared by someone else
$query = \OC_DB::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );
$result = $query->execute( array ('/'.$userId.'/files/'.$keypath, $userId));
if ($row = $result->fetchRow()){
$keypath = $row['source'];
$keypath_parts=explode('/',$keypath);
$user = $keypath_parts[1];
$keypath = str_replace('/'.$user.'/files/', '', $keypath);
}
$view = new \OC_FilesystemView('/'.$user.'/files_encryption/keyfiles/');
return $view->file_get_contents($file.'.key');
return $view->file_get_contents($keypath .'.key');
}
/**
@ -115,13 +129,27 @@ class Keymanager {
* @return bool true/false
*/
public static function setFileKey( $userId, $path, $key ) {
\OC_FileProxy::$enabled = false;
$targetpath = ltrim($path, '/');
$user = $userId;
// update $keytarget and $user if key belongs to a file shared by someone else
$query = \OC_DB::prepare( "SELECT uid_owner, source, target FROM `*PREFIX*sharing` WHERE target = ? AND uid_shared_with = ?" );
$result = $query->execute( array ('/'.$userId.'/files/'.$targetpath, $userId));
if ($row = $result->fetchRow()){
$targetpath = $row['source'];
$targetpath_parts=explode('/',$targetpath);
$user = $targetpath_parts[1];
$targetpath = str_replace('/'.$user.'/files/', '', $targetpath);
}
$view = new \OC_FilesystemView( '/' . $user . '/files_encryption/keyfiles' );
$path_parts = pathinfo($targetpath);
$view = new \OC_FilesystemView( '/' . $userId . '/' . 'files_encryption/keyfiles' );
$path_parts = pathinfo($path);
if (!$view->file_exists($path_parts['dirname'])) $view->mkdir($path_parts['dirname']);
$result = $view->file_put_contents( '/' . $path . '.key', $key );
$result = $view->file_put_contents( '/' . $targetpath . '.key', $key );
\OC_FileProxy::$enabled = true;