]> source.dussan.org Git - nextcloud-server.git/commitdiff
Merge branch 'master' into trashbin_encryption
authorBjörn Schießle <schiessle@owncloud.com>
Mon, 25 Feb 2013 11:14:51 +0000 (12:14 +0100)
committerBjörn Schießle <schiessle@owncloud.com>
Mon, 25 Feb 2013 11:14:51 +0000 (12:14 +0100)
Conflicts:
apps/files_trashbin/index.php
apps/files_trashbin/lib/trash.php

1  2 
apps/files_trashbin/index.php
apps/files_trashbin/lib/trash.php

index 2276fee2f8112e31535c3127ddf46ddde1cc59bd,4615df3586fb3195d32fc4f3fa4accd835570c95..2f1fb32f8cee38cfec340440c39141f99ccdaff4
@@@ -18,7 -18,8 +18,7 @@@ $dir = isset($_GET['dir']) ? stripslash
  
  $result = array();
  if ($dir) {
-       $dirlisting = true;\r
+       $dirlisting = true;
 -      $view = new \OC_FilesystemView('/'.\OCP\User::getUser().'/files_trashbin');
        $fullpath = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath($dir);
        $dirContent = opendir($fullpath);
        $i = 0;
index f11e5c4cbfdf816f07a4feec0d41aa8b1d8944ce,8355c7252b0945c2cecf0db288ffb928fc8acc9b..83fc424834c6190b399ae825c68ba601be057c29
@@@ -34,66 -36,55 +36,66 @@@ class Trashbin 
         */
        public static function move2trash($file_path) {
                $user = \OCP\User::getUser();
 -              $view = new \OC_FilesystemView('/'. $user);
 -              if (!$view->is_dir('files_trashbin')) {
 -                      $view->mkdir('files_trashbin');
 -                      $view->mkdir("versions_trashbin");
 +              $view = new \OC\Files\View('/'. $user);\r
 +              if (!$view->is_dir('files_trashbin')) {\r
 +                      $view->mkdir('files_trashbin');\r
 +                      $view->mkdir("files_trashbin/files");
 +                      $view->mkdir("files_trashbin/versions");
 +                      $view->mkdir("files_trashbin/keyfiles");\r
 +              }\r
 +\r
 +              $path_parts = pathinfo($file_path);\r
 +\r
 +              $deleted = $path_parts['basename'];\r
 +              $location = $path_parts['dirname'];\r
 +              $timestamp = time();\r
 +              $mime = $view->getMimeType('files'.$file_path);\r
 +\r
 +              if ( $view->is_dir('files'.$file_path) ) {\r
 +                      $type = 'dir';\r
 +              } else {\r
 +                      $type = 'file';\r
                }
 -
 -              $path_parts = pathinfo($file_path);
 -
 -              $deleted = $path_parts['basename'];
 -              $location = $path_parts['dirname'];
 -              $timestamp = time();
 -              $mime = $view->getMimeType('files'.$file_path);
 -
 -              if ( $view->is_dir('files'.$file_path) ) {
 -                      $type = 'dir';
 -              } else {
 -                      $type = 'file';
 -              }
 -
 -              if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {
 +              \r
 +              if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {\r
-                       $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));\r
+                       $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
 -                      $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin'));
                }
 -              $trashbinSize += self::copy_recursive($file_path, 'files_trashbin/'.$deleted.'.d'.$timestamp, $view);
 +              $trashbinSize += self::copy_recursive($file_path, 'files_trashbin/files/'.$deleted.'.d'.$timestamp, $view);
  
 -              if ( $view->file_exists('files_trashbin/'.$deleted.'.d'.$timestamp) ) {
 -                      $query = \OC_DB::prepare("INSERT INTO *PREFIX*files_trash (id,timestamp,location,type,mime,user)"
 -                              ." VALUES (?,?,?,?,?,?)");
 +              if ( $view->file_exists('files_trashbin/files/'.$deleted.'.d'.$timestamp) ) {\r
-                       $query = \OC_DB::prepare("INSERT INTO *PREFIX*files_trash (id,timestamp,location,type,mime,user) VALUES (?,?,?,?,?,?)");\r
++                      $query = \OC_DB::prepare("INSERT INTO *PREFIX*files_trash (id,timestamp,location,type,mime,user) VALUES (?,?,?,?,?,?)");
                        $result = $query->execute(array($deleted, $timestamp, $location, $type, $mime, $user));
                        if ( !$result ) { // if file couldn't be added to the database than also don't store it in the trash bin.
 -                              $view->deleteAll('files_trashbin/'.$deleted.'.d'.$timestamp);
 +                              $view->deleteAll('files_trashbin/files/'.$deleted.'.d'.$timestamp);
                                \OC_Log::write('files_trashbin', 'trash bin database couldn\'t be updated', \OC_log::ERROR);
                                return;
 -                      }
 -
 -                      if ( \OCP\App::isEnabled('files_versions') ) {
 +                      }\r
 +                      
 +                      // Take care of file versions\r
 +                      if ( \OCP\App::isEnabled('files_versions') ) {\r
                                if ( $view->is_dir('files_versions'.$file_path) ) {
 -                                      $trashbinSize += self::calculateSize(
 -                                              new \OC_FilesystemView('/'. $user.'/files_versions/'.$file_path)
 -                                      );
 -                                      $view->rename('files_versions'.$file_path, 'versions_trashbin/'. $deleted.'.d'.$timestamp);
 -                              } else if ( $versions = \OCA\Files_Versions\Storage::getVersions($file_path) ) {
 +                                      $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_versions/'.$file_path));\r
 +                                      $view->rename('files_versions'.$file_path, 'files_trashbin/versions'. $deleted.'.d'.$timestamp);\r
 +                              } else if ( $versions = \OCA\Files_Versions\Storage::getVersions($file_path) ) {\r
                                        foreach ($versions as $v) {
 -                                              $trashbinSize += $view->filesize('files_versions'.$v['path'].'.v'.$v['version']);
 -                                              $view->rename('files_versions'.$v['path'].'.v'.$v['version'],
 -                                                      'versions_trashbin/'. $deleted.'.v'.$v['version'].'.d'.$timestamp);
 -                                      }
 +                                              $trashbinSize += $view->filesize('files_versions'.$v['path'].'.v'.$v['version']);\r
 +                                              $view->rename('files_versions'.$v['path'].'.v'.$v['version'], 'files_trashbin/versions'. $deleted.'.v'.$v['version'].'.d'.$timestamp);\r
 +                                      }\r
-                               }\r
+                               }
                        }
 +                      
 +                      // Take care of encryption keys
 +                      $keyfile = \OC_Filesystem::normalizePath('files_encryption/keyfiles/'.$file_path);\r
 +                      if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile.'.key') ) {
 +                              if ( $view->is_dir('files'.$file_path) ) {
 +                                      $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));
 +                                      $view->rename($keyfile, 'files_trashbin/keyfiles/'. $deleted.'.d'.$timestamp);
 +                              } else {\r
 +                                      $trashbinSize += $view->filesize($keyfile.'.key');
 +                                      $view->rename($keyfile.'.key', 'files_trashbin/keyfiles/'. $deleted.'.key.d'.$timestamp);
 +                              }\r
 +                      }
 +
                } else {
                        \OC_Log::write('files_trashbin', 'Couldn\'t move '.$file_path.' to the trash bin', \OC_log::ERROR);
                }
        public static function restore($file, $filename, $timestamp) {
                $user = \OCP\User::getUser();
                $view = new \OC_FilesystemView('/'.$user);
 -
 -              if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {
 +              
 +              if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {\r
-                       $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));\r
+                       $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
 -                      $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin'));
                }
                if ( $timestamp ) {
-                       $query = \OC_DB::prepare('SELECT location,type FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');\r
+                       $query = \OC_DB::prepare('SELECT location,type FROM *PREFIX*files_trash'
+                               .' WHERE user=? AND id=? AND timestamp=?');
                        $result = $query->execute(array($user,$filename,$timestamp))->fetchAll();
                        if ( count($result) != 1 ) {
                                \OC_Log::write('files_trashbin', 'trash bin database inconsistent!', \OC_Log::ERROR);
                                return false;
                        }
-                       
-                       // if location no longer exists, restore file in the root directory\r
-                       $location = $result[0]['location'];\r
-                       if ( $result[0]['location'] != '/' && 
-                                (!$view->is_dir('files'.$result[0]['location']) ||
-                                !$view->isUpdatable('files'.$result[0]['location'])) ) {\r
-                               $location = '';\r
+                       // if location no longer exists, restore file in the root directory
+                       $location = $result[0]['location'];
+                       if ( $result[0]['location'] != '/' &&
+                               (!$view->is_dir('files'.$result[0]['location']) ||
+                                !$view->isUpdatable('files'.$result[0]['location'])) ) {
+                               $location = '';
                        }
                } else {
 -                      $path_parts = pathinfo($filename);
 +                      $path_parts = pathinfo($file);
                        $result[] = array(
                                        'location' => $path_parts['dirname'],
 -                                      'type' => $view->is_dir('/files_trashbin/'.$file) ? 'dir' : 'files',
 +                                      'type' => $view->is_dir('/files_trashbin/files/'.$file) ? 'dir' : 'files',
                                        );
                        $location = '';
                }
 -
 -              $source = \OC_Filesystem::normalizePath('files_trashbin/'.$file);
 +              
 +              $source = \OC_Filesystem::normalizePath('files_trashbin/files/'.$file);
                $target = \OC_Filesystem::normalizePath('files/'.$location.'/'.$filename);
-               
                // we need a  extension in case a file/dir with the same name already exists
                $ext = self::getUniqueExtension($location, $filename, $view);
                $mtime = $view->filemtime($source);
                                        $versionedFile = $filename;
                                } else {
                                        $versionedFile = $file;
-                               }\r
+                               }
                                if ( $result[0]['type'] == 'dir' ) {
 -                                      $trashbinSize -= self::calculateSize(
 -                                              new \OC_FilesystemView('/'.$user.'/'.'versions_trashbin/'. $file)
 -                                      );
 -                                      $view->rename(\OC_Filesystem::normalizePath('versions_trashbin/'. $file),
 -                                              \OC_Filesystem::normalizePath('files_versions/'.$location.'/'.$filename.$ext));
 -                              } else if ( $versions = self::getVersionsFromTrash($versionedFile, $timestamp) ) {
 +                                      $trashbinSize -= self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.'files_trashbin/versions/'. $file));\r
 +                                      $view->rename(\OC_Filesystem::normalizePath('files_trashbin/versions/'. $file), \OC_Filesystem::normalizePath('files_versions/'.$location.'/'.$filename.$ext));\r
 +                              } else if ( $versions = self::getVersionsFromTrash($versionedFile, $timestamp) ) {\r
                                        foreach ($versions as $v) {
                                                if ($timestamp ) {
 -                                                      $trashbinSize -= $view->filesize('versions_trashbin/'.$versionedFile.'.v'.$v.'.d'.$timestamp);
 -                                                      $view->rename('versions_trashbin/'.$versionedFile.'.v'.$v.'.d'.$timestamp,
 -                                                              'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
 +                                                      $trashbinSize -= $view->filesize('files_trashbin/versions/'.$versionedFile.'.v'.$v.'.d'.$timestamp);
 +                                                      $view->rename('files_trashbin/versions/'.$versionedFile.'.v'.$v.'.d'.$timestamp, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
                                                } else {
 -                                                      $trashbinSize -= $view->filesize('versions_trashbin/'.$versionedFile.'.v'.$v);
 -                                                      $view->rename('versions_trashbin/'.$versionedFile.'.v'.$v,
 -                                                              'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
 -                                              }
 -                                      }
 +                                                      $trashbinSize -= $view->filesize('files_trashbin/versions/'.$versionedFile.'.v'.$v);
 +                                                      $view->rename('files_trashbin/versions/'.$versionedFile.'.v'.$v, 'files_versions/'.$location.'/'.$filename.$ext.'.v'.$v);
 +                                              }\r
 +                                      }\r
-                               }\r
+                               }
                        }
 -
 +                      
 +                      // Take care of encryption keys TODO! Get '.key' in file between file name and delete date (also for permanent delete!)
 +                      $parts = pathinfo($file);
 +                      if ( $result[0]['type'] == 'dir' ) {
 +                              $keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$parts['dirname'].'/'.$filename);
 +                      } else {
 +                              $keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$parts['dirname'].'/'.$filename.'.key');
 +                      }
 +                      if ($timestamp) {
 +                              $keyfile .= '.d'.$timestamp;
 +                      }\r
 +                      if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile) ) {
 +                              if ( $result[0]['type'] == 'dir' ) {\r
 +                                      $trashbinSize -= self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));
 +                                      $view->rename($keyfile, 'files_encryption/keyfiles/'. $location.'/'.$filename);
 +                              } else {
 +                                      $trashbinSize -= $view->filesize($keyfile);
 +                                      $view->rename($keyfile, 'files_encryption/keyfiles/'. $location.'/'.$filename.'.key');
 +                              }\r
 +                      }
 +                      
                        if ( $timestamp ) {
-                               $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');\r
+                               $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');
                                $query->execute(array($user,$filename,$timestamp));
                        }
  
  
                return false;
        }
-       
-       /**\r
-        * delete file from trash bin permanently\r
+       /**
+        * delete file from trash bin permanently
         * @param $filename path to the file
-        * @param $timestamp of deletion time\r
-        * @return size of deleted files\r
-        */\r
-       public static function delete($filename, $timestamp=null) {\r
-               $user = \OCP\User::getUser();\r
+        * @param $timestamp of deletion time
+        * @return size of deleted files
+        */
+       public static function delete($filename, $timestamp=null) {
+               $user = \OCP\User::getUser();
                $view = new \OC_FilesystemView('/'.$user);
 -              $size = 0;
 -
 -              if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {
 +              $size = 0;\r
 +      
 +              if (  ($trashbinSize = \OCP\Config::getAppValue('files_trashbin', 'size')) === null ) {\r
-                       $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));\r
+                       $trashbinSize = self::calculateSize(new \OC_FilesystemView('/'. $user.'/files_trashbin'));
 -                      $trashbinSize += self::calculateSize(new \OC_FilesystemView('/'. $user.'/versions_trashbin'));
                }
\r
-               if ( $timestamp ) {\r
-                       $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');\r
-                       $query->execute(array($user,$filename,$timestamp));\r
+               if ( $timestamp ) {
+                       $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND id=? AND timestamp=?');
+                       $query->execute(array($user,$filename,$timestamp));
                        $file = $filename.'.d'.$timestamp;
                } else {
                        $file = $filename;
                }
-               
                if ( \OCP\App::isEnabled('files_versions') ) {
 -                      if ($view->is_dir('versions_trashbin/'.$file)) {
 -                              $size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/versions_trashbin/'.$file));
 -                              $view->unlink('versions_trashbin/'.$file);
 +                      if ($view->is_dir('files_trashbin/versions/'.$file)) {
 +                              $size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/versions/'.$file));
 +                              $view->unlink('files_trashbin/versions/'.$file);
                        } else if ( $versions = self::getVersionsFromTrash($filename, $timestamp) ) {
                                foreach ($versions as $v) {
                                        if ($timestamp ) {
                                }
                        }
                }
 -
 -              if ($view->is_dir('/files_trashbin/'.$file)) {
 -                      $size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/'.$file));
 +              
 +              // Take care of encryption keys
 +              $parts = pathinfo($file);\r
 +              if ( $view->is_dir('/files_trashbin/files/'.$file) ) {\r
 +                      $keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$filename);\r
 +              } else {\r
 +                      $keyfile = \OC_Filesystem::normalizePath('files_trashbin/keyfiles/'.$filename.'.key');\r
 +              }\r
 +              if ($timestamp) {\r
 +                      $keyfile .= '.d'.$timestamp;\r
 +              }\r
 +              if ( \OCP\App::isEnabled('files_encryption') && $view->file_exists($keyfile) ) {\r
 +                      if ( $view->is_dir($keyfile) ) {\r
 +                              $size += self::calculateSize(new \OC_FilesystemView('/'.$user.'/'.$keyfile));\r
 +                      } else {\r
 +                              $size += $view->filesize($keyfile);\r
 +                      }\r
 +                      $view->unlink($keyfile);\r
 +              }\r
 +      
 +              if ($view->is_dir('/files_trashbin/files/'.$file)) {
 +                      $size += self::calculateSize(new \OC_Filesystemview('/'.$user.'/files_trashbin/files/'.$file));
                } else {
 -                      $size += $view->filesize('/files_trashbin/'.$file);
 +                      $size += $view->filesize('/files_trashbin/files/'.$file);
                }
 -              $view->unlink('/files_trashbin/'.$file);
 +              $view->unlink('/files_trashbin/files/'.$file);
                $trashbinSize -= $size;
                \OCP\Config::setAppValue('files_trashbin', 'size', $trashbinSize);
-               \r
-               return $size;\r
+               return $size;
        }
  
        /**
                        $timestamp = $r['timestamp'];
                        $filename = $r['id'];
                        if ( $r['timestamp'] < $limit ) {
 -                              if ($view->is_dir('files_trashbin/'.$filename.'.d'.$timestamp)) {
 -                                      $size += self::calculateSize(
 -                                              new \OC_FilesystemView('/'.$user.'/files_trashbin/'.$filename.'.d'.$timestamp)
 -                                      );
 -                              } else {
 -                                      $size += $view->filesize('files_trashbin/'.$filename.'.d'.$timestamp);
 -                              }
 -                              $view->unlink('files_trashbin/'.$filename.'.d'.$timestamp);
 -                              if ($r['type'] == 'dir') {
 -                                      $size += self::calculateSize(
 -                                              new \OC_FilesystemView('/'.$user.'/versions_trashbin/'.$filename.'.d'.$timestamp)
 -                                      );
 -                                      $view->unlink('versions_trashbin/'.$filename.'.d'.$timestamp);
 -                              } else if ( $versions = self::getVersionsFromTrash($filename, $timestamp) ) {
 -                                      foreach ($versions as $v) {
 -                                              $size += $view->filesize('versions_trashbin/'.$filename.'.v'.$v.'.d'.$timestamp);
 -                                              $view->unlink('versions_trashbin/'.$filename.'.v'.$v.'.d'.$timestamp);
 -                                      }
 -                              }
 +                              $size += self::delete($filename, $timestamp);
                        }
                }
-                               
 -
 -              $query = \OC_DB::prepare('DELETE FROM *PREFIX*files_trash WHERE user=? AND timestamp<?');
 -              $query->execute(array($user,$limit));
 -
                $availableSpace = $availableSpace + $size;
                // if size limit for trash bin reached, delete oldest files in trash bin
                if ($availableSpace < 0) {
                                $availableSpace += $tmp;
                                $size += $tmp;
                                $i++;
-                       }       
+                       }
++
                }
-               
-               return $size;\r
+               return $size;
        }
-       
        /**
         * recursive copy to copy a whole directory
-        * 
+        *
         * @param $source source path, relative to the users files directory
         * @param $destination destination path relative to the users root directoy
         * @param $view file view for the users root directory
         * @param $timestamp timestamp when the file was deleted
         */
        private static function getVersionsFromTrash($filename, $timestamp) {
 -              $view = new \OC_FilesystemView('/'.\OCP\User::getUser().'/versions_trashbin');
 +              $view = new \OC_FilesystemView('/'.\OCP\User::getUser().'/files_trashbin/versions');
-               $versionsName = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath($filename);\r
+               $versionsName = \OCP\Config::getSystemValue('datadirectory').$view->getAbsolutePath($filename);
                $versions = array();
                if ($timestamp ) {
                        // fetch for old versions