2012-07-03 16:42:51 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Sam Tuke <mail@samtuke.com>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2012-07-03 16:42:51 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class contains all hooks.
|
|
|
|
*/
|
|
|
|
|
2012-09-19 18:59:57 +00:00
|
|
|
namespace OCA\Files_Versions;
|
2012-07-03 16:42:51 +00:00
|
|
|
|
2019-11-06 09:11:23 +00:00
|
|
|
use OC\Files\Filesystem;
|
|
|
|
use OC\Files\Mount\MoveableMount;
|
|
|
|
use OC\Files\View;
|
|
|
|
use OCP\Util;
|
|
|
|
|
2012-07-03 16:42:51 +00:00
|
|
|
class Hooks {
|
|
|
|
|
2014-07-24 14:49:38 +00:00
|
|
|
public static function connectHooks() {
|
|
|
|
// Listen to write signals
|
2019-11-06 09:11:23 +00:00
|
|
|
Util::connectHook('OC_Filesystem', 'write', Hooks::class, 'write_hook');
|
2014-07-24 14:49:38 +00:00
|
|
|
// Listen to delete and rename signals
|
2019-11-06 09:11:23 +00:00
|
|
|
Util::connectHook('OC_Filesystem', 'post_delete', Hooks::class, 'remove_hook');
|
|
|
|
Util::connectHook('OC_Filesystem', 'delete', Hooks::class, 'pre_remove_hook');
|
|
|
|
Util::connectHook('OC_Filesystem', 'post_rename', Hooks::class, 'rename_hook');
|
|
|
|
Util::connectHook('OC_Filesystem', 'post_copy', Hooks::class, 'copy_hook');
|
|
|
|
Util::connectHook('OC_Filesystem', 'rename', Hooks::class, 'pre_renameOrCopy_hook');
|
|
|
|
Util::connectHook('OC_Filesystem', 'copy', Hooks::class, 'pre_renameOrCopy_hook');
|
2014-07-24 14:49:38 +00:00
|
|
|
}
|
|
|
|
|
2012-07-03 16:42:51 +00:00
|
|
|
/**
|
|
|
|
* listen to write event.
|
|
|
|
*/
|
|
|
|
public static function write_hook( $params ) {
|
2019-11-06 09:11:23 +00:00
|
|
|
$path = $params[Filesystem::signal_param_path];
|
2017-10-23 21:47:06 +00:00
|
|
|
if($path !== '') {
|
|
|
|
Storage::store($path);
|
2012-07-03 16:42:51 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-09 14:07:26 +00:00
|
|
|
|
2012-09-07 12:10:43 +00:00
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* Erase versions of deleted file
|
2014-05-15 12:19:32 +00:00
|
|
|
* @param array $params
|
2012-09-07 12:10:43 +00:00
|
|
|
*
|
|
|
|
* This function is connected to the delete signal of OC_Filesystem
|
|
|
|
* cleanup the versions directory if the actual file gets deleted
|
|
|
|
*/
|
2012-07-09 14:51:16 +00:00
|
|
|
public static function remove_hook($params) {
|
2019-11-06 09:11:23 +00:00
|
|
|
$path = $params[Filesystem::signal_param_path];
|
2017-10-23 21:47:06 +00:00
|
|
|
if($path !== '') {
|
|
|
|
Storage::delete($path);
|
2013-01-28 19:04:10 +00:00
|
|
|
}
|
2012-09-07 12:10:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-28 12:17:19 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* mark file as "deleted" so that we can clean up the versions if the file is gone
|
2013-11-28 12:17:19 +00:00
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public static function pre_remove_hook($params) {
|
2019-11-06 09:11:23 +00:00
|
|
|
$path = $params[Filesystem::signal_param_path];
|
2017-05-10 12:05:14 +00:00
|
|
|
if($path !== '') {
|
2013-11-28 12:17:19 +00:00
|
|
|
Storage::markDeletedFile($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 12:10:43 +00:00
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* rename/move versions of renamed/moved files
|
2014-05-15 12:19:32 +00:00
|
|
|
* @param array $params array with oldpath and newpath
|
2012-09-07 12:10:43 +00:00
|
|
|
*
|
|
|
|
* This function is connected to the rename signal of OC_Filesystem and adjust the name and location
|
|
|
|
* of the stored versions along the actual file
|
|
|
|
*/
|
2012-07-09 14:51:16 +00:00
|
|
|
public static function rename_hook($params) {
|
2017-10-23 21:47:06 +00:00
|
|
|
$oldpath = $params['oldpath'];
|
|
|
|
$newpath = $params['newpath'];
|
|
|
|
if($oldpath !== '' && $newpath !== '') {
|
|
|
|
Storage::renameOrCopy($oldpath, $newpath, 'rename');
|
2014-07-24 13:30:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* copy versions of copied files
|
|
|
|
* @param array $params array with oldpath and newpath
|
|
|
|
*
|
|
|
|
* This function is connected to the copy signal of OC_Filesystem and copies the
|
|
|
|
* the stored versions to the new location
|
|
|
|
*/
|
|
|
|
public static function copy_hook($params) {
|
2017-10-23 21:47:06 +00:00
|
|
|
$oldpath = $params['oldpath'];
|
|
|
|
$newpath = $params['newpath'];
|
|
|
|
if($oldpath !== '' && $newpath !== '') {
|
|
|
|
Storage::renameOrCopy($oldpath, $newpath, 'copy');
|
2012-09-07 12:10:43 +00:00
|
|
|
}
|
2012-07-05 09:35:08 +00:00
|
|
|
}
|
2013-02-22 16:21:57 +00:00
|
|
|
|
2014-10-28 14:57:08 +00:00
|
|
|
/**
|
|
|
|
* Remember owner and the owner path of the source file.
|
|
|
|
* If the file already exists, then it was a upload of a existing file
|
|
|
|
* over the web interface and we call Storage::store() directly
|
|
|
|
*
|
|
|
|
* @param array $params array with oldpath and newpath
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function pre_renameOrCopy_hook($params) {
|
2017-10-23 21:47:06 +00:00
|
|
|
// if we rename a movable mount point, then the versions don't have
|
|
|
|
// to be renamed
|
2019-11-06 09:11:23 +00:00
|
|
|
$absOldPath = Filesystem::normalizePath('/' . \OCP\User::getUser() . '/files' . $params['oldpath']);
|
|
|
|
$manager = Filesystem::getMountManager();
|
2017-10-23 21:47:06 +00:00
|
|
|
$mount = $manager->find($absOldPath);
|
|
|
|
$internalPath = $mount->getInternalPath($absOldPath);
|
2019-11-06 09:11:23 +00:00
|
|
|
if ($internalPath === '' and $mount instanceof MoveableMount) {
|
2017-10-23 21:47:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2014-10-28 14:57:08 +00:00
|
|
|
|
2019-11-06 09:11:23 +00:00
|
|
|
$view = new View(\OCP\User::getUser() . '/files');
|
2017-10-23 21:47:06 +00:00
|
|
|
if ($view->file_exists($params['newpath'])) {
|
|
|
|
Storage::store($params['newpath']);
|
|
|
|
} else {
|
|
|
|
Storage::setSourcePathAndUser($params['oldpath']);
|
2014-10-28 14:57:08 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-03 16:42:51 +00:00
|
|
|
}
|