2014-05-22 03:41:27 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
2015-10-05 21:54:56 +03:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2015-10-26 15:54:55 +03:00
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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/>
|
|
|
|
*
|
2014-05-22 03:41:27 +04:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2014-05-22 03:41:27 +04:00
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
2014-11-24 17:54:42 +03:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2014-05-22 03:41:27 +04:00
|
|
|
use OC\Files\Mount\MoveableMount;
|
2015-04-15 19:18:28 +03:00
|
|
|
use OC\Files\View;
|
2014-05-22 03:41:27 +04:00
|
|
|
|
|
|
|
/**
|
2014-05-28 15:47:50 +04:00
|
|
|
* Shared mount points can be moved by the user
|
2014-05-22 03:41:27 +04:00
|
|
|
*/
|
2014-11-24 17:54:42 +03:00
|
|
|
class SharedMount extends MountPoint implements MoveableMount {
|
2014-05-22 03:41:27 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Shared $storage
|
|
|
|
*/
|
|
|
|
protected $storage = null;
|
|
|
|
|
2015-03-09 18:20:18 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Cache\ChangePropagator
|
|
|
|
*/
|
|
|
|
protected $ownerPropagator;
|
|
|
|
|
2015-06-29 20:13:39 +03:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\View
|
|
|
|
*/
|
|
|
|
private $recipientView;
|
|
|
|
|
2015-07-03 16:58:52 +03:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2014-05-27 22:48:41 +04:00
|
|
|
public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
|
2014-05-27 23:51:23 +04:00
|
|
|
// first update the mount point before creating the parent
|
2015-03-09 18:20:18 +03:00
|
|
|
$this->ownerPropagator = $arguments['propagator'];
|
2015-07-03 16:58:52 +03:00
|
|
|
$this->user = $arguments['user'];
|
|
|
|
$this->recipientView = new View('/' . $this->user . '/files');
|
2015-06-29 20:13:39 +03:00
|
|
|
$newMountPoint = $this->verifyMountPoint($arguments['share']);
|
2015-07-03 16:58:52 +03:00
|
|
|
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
|
2015-06-29 20:13:39 +03:00
|
|
|
$arguments['ownerView'] = new View('/' . $arguments['share']['uid_owner'] . '/files');
|
2014-05-28 00:43:20 +04:00
|
|
|
parent::__construct($storage, $absMountPoint, $arguments, $loader);
|
2014-05-27 22:48:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if the parent folder exists otherwise move the mount point up
|
|
|
|
*/
|
2015-06-29 20:13:39 +03:00
|
|
|
private function verifyMountPoint(&$share) {
|
2014-05-27 22:48:41 +04:00
|
|
|
|
|
|
|
$mountPoint = basename($share['file_target']);
|
|
|
|
$parent = dirname($share['file_target']);
|
|
|
|
|
2015-06-29 20:13:39 +03:00
|
|
|
if (!$this->recipientView->is_dir($parent)) {
|
2015-03-26 23:56:44 +03:00
|
|
|
$parent = Helper::getShareFolder();
|
2014-05-27 22:48:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
|
2015-06-29 20:13:39 +03:00
|
|
|
\OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
|
|
|
|
[],
|
|
|
|
$this->recipientView
|
|
|
|
);
|
2014-05-27 22:48:41 +04:00
|
|
|
|
2015-06-29 20:13:39 +03:00
|
|
|
if ($newMountPoint !== $share['file_target']) {
|
2015-09-07 15:28:20 +03:00
|
|
|
$this->updateFileTarget($newMountPoint, $share);
|
2014-05-27 22:48:41 +04:00
|
|
|
$share['file_target'] = $newMountPoint;
|
|
|
|
$share['unique_name'] = true;
|
|
|
|
}
|
2014-05-28 00:43:20 +04:00
|
|
|
|
|
|
|
return $newMountPoint;
|
2014-05-27 22:48:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update fileTarget in the database if the mount point changed
|
2015-06-29 20:13:39 +03:00
|
|
|
*
|
2014-05-27 22:48:41 +04:00
|
|
|
* @param string $newPath
|
|
|
|
* @param array $share reference to the share which should be modified
|
2014-09-25 13:29:57 +04:00
|
|
|
* @return bool
|
2014-05-27 22:48:41 +04:00
|
|
|
*/
|
2015-07-03 16:58:52 +03:00
|
|
|
private function updateFileTarget($newPath, &$share) {
|
2014-05-27 22:48:41 +04:00
|
|
|
// if the user renames a mount point from a group share we need to create a new db entry
|
|
|
|
// for the unique name
|
2014-06-03 18:56:04 +04:00
|
|
|
if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
|
2015-04-08 16:21:52 +03:00
|
|
|
$query = \OCP\DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
|
2014-05-27 22:48:41 +04:00
|
|
|
.' `share_type`, `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,'
|
|
|
|
.' `file_target`, `token`, `parent`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)');
|
|
|
|
$arguments = array($share['item_type'], $share['item_source'], $share['item_target'],
|
2015-07-03 16:58:52 +03:00
|
|
|
2, $this->user, $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
|
2014-05-27 22:48:41 +04:00
|
|
|
$newPath, $share['token'], $share['id']);
|
|
|
|
} else {
|
|
|
|
// rename mount point
|
2015-04-08 16:21:52 +03:00
|
|
|
$query = \OCP\DB::prepare(
|
2014-05-27 22:48:41 +04:00
|
|
|
'Update `*PREFIX*share`
|
|
|
|
SET `file_target` = ?
|
|
|
|
WHERE `id` = ?'
|
2015-06-29 20:13:39 +03:00
|
|
|
);
|
2014-05-27 22:48:41 +04:00
|
|
|
$arguments = array($newPath, $share['id']);
|
|
|
|
}
|
|
|
|
|
2014-05-27 23:51:23 +04:00
|
|
|
$result = $query->execute($arguments);
|
|
|
|
|
|
|
|
return $result === 1 ? true : false;
|
2014-05-27 22:48:41 +04:00
|
|
|
}
|
|
|
|
|
2014-05-22 03:41:27 +04:00
|
|
|
/**
|
|
|
|
* Format a path to be relative to the /user/files/ directory
|
|
|
|
*
|
|
|
|
* @param string $path the absolute path
|
|
|
|
* @return string e.g. turns '/admin/files/test.txt' into '/test.txt'
|
|
|
|
*/
|
2014-09-29 13:13:01 +04:00
|
|
|
protected function stripUserFilesPath($path) {
|
2014-05-22 03:41:27 +04:00
|
|
|
$trimmed = ltrim($path, '/');
|
|
|
|
$split = explode('/', $trimmed);
|
|
|
|
|
|
|
|
// it is not a file relative to data/user/files
|
|
|
|
if (count($split) < 3 || $split[1] !== 'files') {
|
|
|
|
\OCP\Util::writeLog('file sharing',
|
|
|
|
'Can not strip userid and "files/" from path: ' . $path,
|
2014-09-29 13:13:01 +04:00
|
|
|
\OCP\Util::ERROR);
|
|
|
|
throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
|
2014-05-22 03:41:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// skip 'user' and 'files'
|
|
|
|
$sliced = array_slice($split, 2);
|
|
|
|
$relPath = implode('/', $sliced);
|
|
|
|
|
|
|
|
return '/' . $relPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Move the mount point to $target
|
|
|
|
*
|
|
|
|
* @param string $target the target mount point
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function moveMount($target) {
|
|
|
|
|
|
|
|
$relTargetPath = $this->stripUserFilesPath($target);
|
|
|
|
$share = $this->storage->getShare();
|
|
|
|
|
2014-07-31 13:55:59 +04:00
|
|
|
$result = true;
|
|
|
|
|
|
|
|
if (!empty($share['grouped'])) {
|
|
|
|
foreach ($share['grouped'] as $s) {
|
|
|
|
$result = $this->updateFileTarget($relTargetPath, $s) && $result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result = $this->updateFileTarget($relTargetPath, $share) && $result;
|
|
|
|
}
|
2014-05-22 03:41:27 +04:00
|
|
|
|
|
|
|
if ($result) {
|
|
|
|
$this->setMountPoint($target);
|
|
|
|
$this->storage->setUniqueName();
|
|
|
|
$this->storage->setMountPoint($relTargetPath);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
\OCP\Util::writeLog('file sharing',
|
|
|
|
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"',
|
|
|
|
\OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the mount points
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function removeMount() {
|
2014-07-02 16:40:22 +04:00
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager();
|
2014-09-25 13:29:57 +04:00
|
|
|
/** @var \OC\Files\Storage\Shared */
|
2014-05-27 17:07:53 +04:00
|
|
|
$storage = $this->getStorage();
|
2014-07-31 13:55:59 +04:00
|
|
|
$result = $storage->unshareStorage();
|
2014-07-02 16:40:22 +04:00
|
|
|
$mountManager->removeMount($this->mountPoint);
|
2014-05-27 17:07:53 +04:00
|
|
|
|
|
|
|
return $result;
|
2014-05-22 03:41:27 +04:00
|
|
|
}
|
2015-03-09 18:20:18 +03:00
|
|
|
|
|
|
|
public function getShare() {
|
2015-04-20 14:32:09 +03:00
|
|
|
return $this->getStorage()->getShare();
|
2015-03-09 18:20:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OC\Files\Cache\ChangePropagator
|
|
|
|
*/
|
|
|
|
public function getOwnerPropagator() {
|
|
|
|
return $this->ownerPropagator;
|
|
|
|
}
|
2014-05-22 03:41:27 +04:00
|
|
|
}
|