2015-01-14 22:39:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 18:07:57 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* @license AGPL-3.0
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* 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.
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-07 18:02:49 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 22:39:23 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-04-07 18:02:49 +03:00
|
|
|
* 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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Encryption;
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
use OC\Files\Filesystem;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OC\Files\Mount;
|
|
|
|
use OC\Files\View;
|
2015-01-14 22:39:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* update encrypted files, e.g. because a file was shared
|
|
|
|
*/
|
|
|
|
class Update {
|
|
|
|
|
|
|
|
/** @var \OC\Files\View */
|
|
|
|
protected $view;
|
|
|
|
|
|
|
|
/** @var \OC\Encryption\Util */
|
|
|
|
protected $util;
|
|
|
|
|
|
|
|
/** @var \OC\Files\Mount\Manager */
|
|
|
|
protected $mountManager;
|
|
|
|
|
|
|
|
/** @var \OC\Encryption\Manager */
|
|
|
|
protected $encryptionManager;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
protected $uid;
|
|
|
|
|
2015-03-31 17:23:31 +03:00
|
|
|
/** @var \OC\Encryption\File */
|
|
|
|
protected $file;
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param \OC\Files\View $view
|
|
|
|
* @param \OC\Encryption\Util $util
|
|
|
|
* @param \OC\Files\Mount\Manager $mountManager
|
|
|
|
* @param \OC\Encryption\Manager $encryptionManager
|
2015-03-31 17:23:31 +03:00
|
|
|
* @param \OC\Encryption\File $file
|
2015-01-14 22:39:23 +03:00
|
|
|
* @param string $uid
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
View $view,
|
|
|
|
Util $util,
|
|
|
|
Mount\Manager $mountManager,
|
|
|
|
Manager $encryptionManager,
|
2015-03-31 17:23:31 +03:00
|
|
|
File $file,
|
2015-01-14 22:39:23 +03:00
|
|
|
$uid
|
|
|
|
) {
|
|
|
|
|
|
|
|
$this->view = $view;
|
|
|
|
$this->util = $util;
|
|
|
|
$this->mountManager = $mountManager;
|
|
|
|
$this->encryptionManager = $encryptionManager;
|
2015-03-31 17:23:31 +03:00
|
|
|
$this->file = $file;
|
2015-01-14 22:39:23 +03:00
|
|
|
$this->uid = $uid;
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
|
|
|
* hook after file was shared
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
2015-01-14 22:39:23 +03:00
|
|
|
public function postShared($params) {
|
2015-04-27 12:40:10 +03:00
|
|
|
if ($this->encryptionManager->isEnabled()) {
|
|
|
|
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
|
|
|
|
$path = Filesystem::getPath($params['fileSource']);
|
|
|
|
list($owner, $ownerPath) = $this->getOwnerPath($path);
|
|
|
|
$absPath = '/' . $owner . '/files/' . $ownerPath;
|
|
|
|
$this->update($absPath);
|
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
|
|
|
* hook after file was unshared
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
2015-01-14 22:39:23 +03:00
|
|
|
public function postUnshared($params) {
|
2015-04-27 12:40:10 +03:00
|
|
|
if ($this->encryptionManager->isEnabled()) {
|
|
|
|
if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') {
|
|
|
|
$path = Filesystem::getPath($params['fileSource']);
|
|
|
|
list($owner, $ownerPath) = $this->getOwnerPath($path);
|
|
|
|
$absPath = '/' . $owner . '/files/' . $ownerPath;
|
|
|
|
$this->update($absPath);
|
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-11 11:35:42 +03:00
|
|
|
/**
|
|
|
|
* inform encryption module that a file was restored from the trash bin,
|
|
|
|
* e.g. to update the encryption keys
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function postRestore($params) {
|
|
|
|
if ($this->encryptionManager->isEnabled()) {
|
|
|
|
$path = Filesystem::normalizePath('/' . $this->uid . '/files/' . $params['filePath']);
|
|
|
|
$this->update($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* inform encryption module that a file was renamed,
|
|
|
|
* e.g. to update the encryption keys
|
|
|
|
*
|
|
|
|
* @param array $params
|
|
|
|
*/
|
|
|
|
public function postRename($params) {
|
|
|
|
$source = $params['oldpath'];
|
|
|
|
$target = $params['newpath'];
|
|
|
|
if(
|
|
|
|
$this->encryptionManager->isEnabled() &&
|
|
|
|
dirname($source) !== dirname($target)
|
|
|
|
) {
|
|
|
|
list($owner, $ownerPath) = $this->getOwnerPath($target);
|
|
|
|
$absPath = '/' . $owner . '/files/' . $ownerPath;
|
|
|
|
$this->update($absPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
/**
|
2015-04-23 17:48:11 +03:00
|
|
|
* get owner and path relative to data/<owner>/files
|
2015-01-14 22:39:23 +03:00
|
|
|
*
|
2015-04-23 17:48:11 +03:00
|
|
|
* @param string $path path to file for current user
|
|
|
|
* @return array ['owner' => $owner, 'path' => $path]
|
|
|
|
* @throw \InvalidArgumentException
|
2015-01-14 22:39:23 +03:00
|
|
|
*/
|
2015-05-11 11:35:42 +03:00
|
|
|
protected function getOwnerPath($path) {
|
2015-04-23 17:48:11 +03:00
|
|
|
$info = Filesystem::getFileInfo($path);
|
|
|
|
$owner = Filesystem::getOwner($path);
|
|
|
|
$view = new View('/' . $owner . '/files');
|
|
|
|
$path = $view->getPath($info->getId());
|
|
|
|
if ($path === null) {
|
|
|
|
throw new \InvalidArgumentException('No file found for ' . $info->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($owner, $path);
|
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
/**
|
|
|
|
* notify encryption module about added/removed users from a file/folder
|
|
|
|
*
|
|
|
|
* @param string $path relative to data/
|
|
|
|
* @throws Exceptions\ModuleDoesNotExistsException
|
|
|
|
*/
|
|
|
|
public function update($path) {
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2017-05-30 12:59:49 +03:00
|
|
|
$encryptionModule = $this->encryptionManager->getEncryptionModule();
|
|
|
|
|
|
|
|
// if the encryption module doesn't encrypt the files on a per-user basis
|
|
|
|
// we have nothing to do here.
|
|
|
|
if ($encryptionModule->needDetailedAccessList() === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-01 14:59:29 +03:00
|
|
|
// if a folder was shared, get a list of all (sub-)folders
|
2015-04-23 17:48:11 +03:00
|
|
|
if ($this->view->is_dir($path)) {
|
|
|
|
$allFiles = $this->util->getAllFiles($path);
|
2015-04-01 14:59:29 +03:00
|
|
|
} else {
|
2015-04-23 17:48:11 +03:00
|
|
|
$allFiles = array($path);
|
2015-04-01 14:59:29 +03:00
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2017-05-30 12:59:49 +03:00
|
|
|
|
2015-01-14 22:39:23 +03:00
|
|
|
|
2015-04-23 17:48:11 +03:00
|
|
|
foreach ($allFiles as $file) {
|
|
|
|
$usersSharing = $this->file->getAccessList($file);
|
|
|
|
$encryptionModule->update($file, $this->uid, $usersSharing);
|
2015-04-01 14:59:29 +03:00
|
|
|
}
|
2015-01-14 22:39:23 +03:00
|
|
|
}
|
|
|
|
|
2015-04-01 14:59:29 +03:00
|
|
|
}
|