/** * restore files from trash bin * * @param string $file path to the deleted file * @param string $filename name of the file * @param int $timestamp time when the file was deleted * * @return bool */ public static function restore($file, $filename, $timestamp) { $user = \OCP\User::getUser(); $view = new \OC\Files\View('/' . $user); $location = ''; if ($timestamp) { $query = \OC_DB::prepare('SELECT `location` 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); } else { $location = $result[0]['location']; // if location no longer exists, restore file in the root directory if ($location !== '/' && (!$view->is_dir('files' . $location) || !$view->isUpdatable('files' . $location))) { $location = ''; } } } // we need a extension in case a file/dir with the same name already exists $uniqueFilename = self::getUniqueFilename($location, $filename, $view); $source = \OC\Files\Filesystem::normalizePath('files_trashbin/files/' . $file); $target = \OC\Files\Filesystem::normalizePath('files/' . $location . '/' . $uniqueFilename); $mtime = $view->filemtime($source); // disable proxy to prevent recursive calls $proxyStatus = \OC_FileProxy::$enabled; \OC_FileProxy::$enabled = false; // restore file $restoreResult = $view->rename($source, $target); // handle the restore result if ($restoreResult) { $fakeRoot = $view->getRoot(); $view->chroot('/' . $user . '/files'); $view->touch('/' . $location . '/' . $uniqueFilename, $mtime); $view->chroot($fakeRoot); \OCP\Util::emitHook('\\OCA\\Files_Trashbin\\Trashbin', 'post_restore', array('filePath' => \OC\Files\Filesystem::normalizePath('/' . $location . '/' . $uniqueFilename), 'trashPath' => \OC\Files\Filesystem::normalizePath($file))); self::restoreVersions($view, $file, $filename, $uniqueFilename, $location, $timestamp); self::restoreEncryptionKeys($view, $file, $filename, $uniqueFilename, $location, $timestamp); if ($timestamp) { $query = \OC_DB::prepare('DELETE FROM `*PREFIX*files_trash` WHERE `user`=? AND `id`=? AND `timestamp`=?'); $query->execute(array($user, $filename, $timestamp)); } // enable proxy \OC_FileProxy::$enabled = $proxyStatus; return true; } // enable proxy \OC_FileProxy::$enabled = $proxyStatus; return false; }