]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix reusing keys
authorFlorin Peter <github@florin-peter.de>
Sat, 27 Apr 2013 18:18:57 +0000 (20:18 +0200)
committerFlorin Peter <github@florin-peter.de>
Sat, 27 Apr 2013 18:18:57 +0000 (20:18 +0200)
fix webdav part files

apps/files_encryption/lib/keymanager.php

index cfc13ee132d65cdf7ab9bcd479542f13d03e10cc..9f3cb8120ca33cd3d4bdafb08af7912d2034e17e 100755 (executable)
@@ -129,16 +129,51 @@ class Keymanager {
                                mkdir($keyfileFolderName, 0750, true);
                        }
                }
-               
-               $result = $view->file_put_contents( $basePath . '/' . $targetPath . '.key', $catfile );
-               
+
+        // try reusing key file if part file
+        if(self::isPartialFilePath($targetPath)) {
+            $result = $view->file_put_contents( $basePath . '/' . self::fixPartialFilePath($targetPath) . '.key', $catfile );
+        } else {
+            $result = $view->file_put_contents( $basePath . '/' . $targetPath . '.key', $catfile );
+        }
                
                \OC_FileProxy::$enabled = $proxyStatus;
                
                return $result;
                
        }
-       
+
+    /**
+     * @brief Remove .path extension from a file path
+     * @param string $path Path that may identify a .part file
+     * @return string File path without .part extension
+     * @note this is needed for reusing keys
+     */
+    public static function fixPartialFilePath($path)
+    {
+        if (preg_match('/\.part$/', $path)) {
+
+            $newLength = strlen($path) - 5;
+            $fPath = substr($path, 0, $newLength);
+
+            return $fPath;
+        } else {
+
+            return $path;
+
+        }
+
+    }
+
+    public static function isPartialFilePath($path)
+    {
+        if (preg_match('/\.part$/', $path)) {
+             return true;
+        } else {
+            return false;
+        }
+
+    }
        /**
         * @brief retrieve keyfile for an encrypted file
         * @param \OC_FilesystemView $view
@@ -150,12 +185,20 @@ class Keymanager {
         * of the keyfile must be performed by client code
         */
        public static function getFileKey( \OC_FilesystemView $view, $userId, $filePath ) {
-               
+
+        // try reusing key file if part file
+        if(self::isPartialFilePath($filePath)) {
+            $result = self::getFileKey($view, $userId, self::fixPartialFilePath($filePath));
+            if($result) {
+                return $result;
+            }
+        }
+
                $util = new Util($view, \OCP\User::getUser());
                list($owner, $filename) = $util->getUidAndFilename($filePath);
                $filePath_f = ltrim( $filename, '/' );
-               
-               $keyfilePath = '/' . $owner . '/files_encryption/keyfiles/' . $filePath_f . '.key';
+
+        $keyfilePath = '/' . $owner . '/files_encryption/keyfiles/' . $filePath_f . '.key';
 
         $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
@@ -287,7 +330,7 @@ class Keymanager {
         */
        public static function setShareKey( \OC_FilesystemView $view, $path, $userId, $shareKey ) {
 
-               //here we need the currently logged in user, while userId can be a different user
+        //here we need the currently logged in user, while userId can be a different user
                $util = new Util( $view, \OCP\User::getUser() );
 
                list($owner, $filename) = $util->getUidAndFilename($path);
@@ -295,8 +338,13 @@ class Keymanager {
                $basePath = '/' . $owner . '/files_encryption/share-keys';
                
                $shareKeyPath = self::keySetPreparation( $view, $filename, $basePath, $owner );
-               
-               $writePath = $basePath . '/' . $shareKeyPath . '.' . $userId . '.shareKey';
+
+        // try reusing key file if part file
+        if(self::isPartialFilePath($shareKeyPath)) {
+            $writePath = $basePath . '/' . self::fixPartialFilePath($shareKeyPath) . '.' . $userId . '.shareKey';
+        } else {
+            $writePath = $basePath . '/' . $shareKeyPath . '.' . $userId . '.shareKey';
+        }
 
         $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;
@@ -359,6 +407,14 @@ class Keymanager {
         */
        public static function getShareKey( \OC_FilesystemView $view, $userId, $filePath ) {
 
+        // try reusing key file if part file
+        if(self::isPartialFilePath($filePath)) {
+            $result = self::getShareKey($view, $userId, self::fixPartialFilePath($filePath));
+            if($result) {
+                return $result;
+            }
+        }
+
         $proxyStatus = \OC_FileProxy::$enabled;
                \OC_FileProxy::$enabled = false;