diff options
author | Bjoern Schiessle <schiessle@owncloud.com> | 2014-06-23 19:13:56 +0400 |
---|---|---|
committer | Bjoern Schiessle <schiessle@owncloud.com> | 2014-06-24 00:06:50 +0400 |
commit | 46cc6f57473e32bc414b30ba472ba218fd47ac12 (patch) | |
tree | 0151d87ae9751f9fdc072eba91bf90cc9f72c542 /apps/files_encryption | |
parent | b66d3632ea049695a56732ca84dd35abf57ac988 (diff) |
always use a \OC\Files\View
Diffstat (limited to 'apps/files_encryption')
-rwxr-xr-x | apps/files_encryption/lib/helper.php | 9 | ||||
-rwxr-xr-x | apps/files_encryption/lib/keymanager.php | 46 | ||||
-rw-r--r-- | apps/files_encryption/tests/hooks.php | 2 | ||||
-rw-r--r-- | apps/files_encryption/tests/keymanager.php | 65 |
4 files changed, 58 insertions, 64 deletions
diff --git a/apps/files_encryption/lib/helper.php b/apps/files_encryption/lib/helper.php index c21f78820fd..440fcbf91db 100755 --- a/apps/files_encryption/lib/helper.php +++ b/apps/files_encryption/lib/helper.php @@ -421,15 +421,6 @@ class Helper { } /** - * @brief glob uses different pattern than regular expressions, escape glob pattern only - * @param unescaped path - * @return escaped path - */ - public static function escapeGlobPattern($path) { - return preg_replace('/(\*|\?|\[)/', '[$1]', $path); - } - - /** * find all share keys for a given file * @param string $path to the file * @param \OC\Files\View $view view, relative to data/ diff --git a/apps/files_encryption/lib/keymanager.php b/apps/files_encryption/lib/keymanager.php index d09f4d82516..090b345110c 100755 --- a/apps/files_encryption/lib/keymanager.php +++ b/apps/files_encryption/lib/keymanager.php @@ -133,7 +133,7 @@ class Keymanager { $basePath = '/' . $owner . '/files_encryption/keyfiles'; } - $targetPath = self::keySetPreparation($view, $filename, $basePath, $owner); + $targetPath = self::keySetPreparation($view, $filename, $basePath); if (!$view->is_dir($basePath . '/' . $targetPath)) { @@ -281,8 +281,9 @@ class Keymanager { $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; - if (!$view->file_exists('')) + if (!$view->file_exists('')) { $view->mkdir(''); + } $result = $view->file_put_contents($user . '.private.key', $key); @@ -340,7 +341,7 @@ class Keymanager { $basePath = '/' . $owner . '/files_encryption/share-keys'; } - $shareKeyPath = self::keySetPreparation($view, $filename, $basePath, $owner); + $shareKeyPath = self::keySetPreparation($view, $filename, $basePath); $result = true; @@ -466,8 +467,7 @@ class Keymanager { if ($view->is_dir($shareKeyPath)) { - $localPath = \OC\Files\Filesystem::normalizePath($view->getLocalFolder($shareKeyPath)); - self::recursiveDelShareKeys($localPath, $userIds); + self::recursiveDelShareKeys($shareKeyPath, $userIds, $view); } else { @@ -491,30 +491,32 @@ class Keymanager { * @param string $dir directory * @param array $userIds user ids for which the share keys should be deleted */ - private static function recursiveDelShareKeys($dir, $userIds) { - foreach ($userIds as $userId) { - $extension = '.' . $userId . '.shareKey'; - $escapedDir = Helper::escapeGlobPattern($dir); - $escapedExtension = Helper::escapeGlobPattern($extension); - $matches = glob($escapedDir . '/*' . $escapedExtension); - } - /** @var $matches array */ - foreach ($matches as $ma) { - if (!unlink($ma)) { - \OCP\Util::writeLog('Encryption library', - 'Could not delete shareKey; does not exist: "' . $ma . '"', \OCP\Util::ERROR); + private static function recursiveDelShareKeys($dir, $userIds, $view) { + + $dirContent = $view->opendir($dir); + + if (is_resource($dirContent)) { + while (($file = readdir($dirContent)) !== false) { + if (!\OC\Files\Filesystem::isIgnoredDir($file)) { + if ($view->is_dir($dir . '/' . $file)) { + self::recursiveDelShareKeys($dir . '/' . $file, $userIds, $view); + } else { + foreach ($userIds as $userId) { + if (preg_match("/(.*)." . $userId . ".shareKey/", $file)) { + $view->unlink($dir . '/' . $file); + } + } + } + } } - } - $subdirs = $directories = glob($escapedDir . '/*', GLOB_ONLYDIR); - foreach ($subdirs as $subdir) { - self::recursiveDelShareKeys($subdir, $userIds); + closedir($dirContent); } } /** * @brief Make preparations to vars and filesystem for saving a keyfile */ - public static function keySetPreparation(\OC_FilesystemView $view, $path, $basePath, $userId) { + public static function keySetPreparation(\OC_FilesystemView $view, $path, $basePath) { $targetPath = ltrim($path, '/'); diff --git a/apps/files_encryption/tests/hooks.php b/apps/files_encryption/tests/hooks.php index 36f1c755667..ba8edd24297 100644 --- a/apps/files_encryption/tests/hooks.php +++ b/apps/files_encryption/tests/hooks.php @@ -296,7 +296,7 @@ class Test_Encryption_Hooks extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->rootView->is_dir('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/' . $this->folder . '/' . $this->folder)); - // move the file out of the shared folder + // move the file to the sub-subfolder $root = $this->rootView->getRoot(); $this->rootView->chroot('/' . self::TEST_ENCRYPTION_HOOKS_USER1 . '/files/'); $this->rootView->rename($this->filename, '/' . $this->folder . '/' . $this->folder . '/' . $this->filename); diff --git a/apps/files_encryption/tests/keymanager.php b/apps/files_encryption/tests/keymanager.php index 6f32c50743c..3989ebbab76 100644 --- a/apps/files_encryption/tests/keymanager.php +++ b/apps/files_encryption/tests/keymanager.php @@ -206,43 +206,44 @@ class Test_Encryption_Keymanager extends \PHPUnit_Framework_TestCase { */ function testRecursiveDelShareKeys() { - // generate filename - $filename = '/tmp-' . uniqid() . '.txt'; - // create folder structure - $this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1'); - $this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1/subfolder'); - $this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1/subfolder/subsubfolder'); - - // enable encryption proxy - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = true; - - // save file with content - $cryptedFile = file_put_contents('crypt:///'.Test_Encryption_Keymanager::TEST_USER.'/files/folder1/subfolder/subsubfolder' . $filename, $this->dataShort); - - // test that data was successfully written - $this->assertTrue(is_int($cryptedFile)); - - // change encryption proxy to previous state - \OC_FileProxy::$enabled = $proxyStatus; - - // recursive delete keys - Encryption\Keymanager::delShareKey($this->view, array('admin'), '/folder1/'); - - // check if share key not exists + $this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1'); + $this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder'); + $this->view->mkdir('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder'); + + // create some dummy share keys + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/file1.user1.shareKey', 'data'); + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/file2.user2.shareKey', 'data'); + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/file2.user3.shareKey', 'data'); + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/file2.user3.shareKey', 'data'); + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder/file1.user1.shareKey', 'data'); + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder/file2.user2.shareKey', 'data'); + $this->view->file_put_contents('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder/file2.user3.shareKey', 'data'); + + // recursive delete share keys from user1 and user2 + Encryption\Keymanager::delShareKey($this->view, array('user1', 'user2'), '/folder1/'); + + // check if share keys from user1 and user2 are deleted + $this->assertFalse($this->view->file_exists( + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/file1.user1.shareKey')); + $this->assertFalse($this->view->file_exists( + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/file2.user2.shareKey')); $this->assertFalse($this->view->file_exists( - '/admin/files_encryption/share-keys/folder1/subfolder/subsubfolder/' . $filename . '.admin.shareKey')); + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder/file1.user1.shareKey')); + $this->assertFalse($this->view->file_exists( + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder/file2.user2.shareKey')); - // enable encryption proxy - $proxyStatus = \OC_FileProxy::$enabled; - \OC_FileProxy::$enabled = true; + // check if share keys from user3 still exists + $this->assertTrue($this->view->file_exists( + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/file2.user3.shareKey')); + $this->assertTrue($this->view->file_exists( + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/subsubfolder/file2.user3.shareKey')); + $this->assertTrue($this->view->file_exists( + '/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys/folder1/subfolder/file2.user3.shareKey')); // cleanup - $this->view->unlink('/admin/files/folder1'); + $this->view->deleteAll('/'.Test_Encryption_Keymanager::TEST_USER.'/files_encryption/share-keys'); - // change encryption proxy to previous state - \OC_FileProxy::$enabled = $proxyStatus; } } @@ -253,4 +254,4 @@ class TestProtectedKeymanagerMethods extends \OCA\Encryption\Keymanager { public static function testGetFilenameFromShareKey($sharekey) { return self::getFilenameFromShareKey($sharekey); } -}
\ No newline at end of file +} |