2014-05-21 23:41:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2014-05-28 11:47:50 +00:00
|
|
|
* Copyright (c) 2014 Robin Appelman <icewind@owncloud.com>
|
2014-05-21 23:41:27 +00:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
2014-11-24 14:54:42 +00:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2014-05-21 23:41:27 +00:00
|
|
|
use OC\Files\Mount\MoveableMount;
|
|
|
|
|
|
|
|
/**
|
2014-05-28 11:47:50 +00:00
|
|
|
* Shared mount points can be moved by the user
|
2014-05-21 23:41:27 +00:00
|
|
|
*/
|
2014-11-24 14:54:42 +00:00
|
|
|
class SharedMount extends MountPoint implements MoveableMount {
|
2014-05-21 23:41:27 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Shared $storage
|
|
|
|
*/
|
|
|
|
protected $storage = null;
|
|
|
|
|
2014-05-27 18:48:41 +00:00
|
|
|
public function __construct($storage, $mountpoint, $arguments = null, $loader = null) {
|
2014-05-27 19:51:23 +00:00
|
|
|
// first update the mount point before creating the parent
|
2014-11-04 14:16:58 +00:00
|
|
|
$newMountPoint = $this->verifyMountPoint($arguments['share'], $arguments['user']);
|
|
|
|
$absMountPoint = '/' . $arguments['user'] . '/files' . $newMountPoint;
|
2014-05-27 20:43:20 +00:00
|
|
|
parent::__construct($storage, $absMountPoint, $arguments, $loader);
|
2014-05-27 18:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if the parent folder exists otherwise move the mount point up
|
|
|
|
*/
|
2014-11-04 14:16:58 +00:00
|
|
|
private function verifyMountPoint(&$share, $user) {
|
2014-05-27 18:48:41 +00:00
|
|
|
|
|
|
|
$mountPoint = basename($share['file_target']);
|
|
|
|
$parent = dirname($share['file_target']);
|
|
|
|
|
|
|
|
while (!\OC\Files\Filesystem::is_dir($parent)) {
|
|
|
|
$parent = dirname($parent);
|
|
|
|
}
|
|
|
|
|
|
|
|
$newMountPoint = \OCA\Files_Sharing\Helper::generateUniqueTarget(
|
|
|
|
\OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
|
|
|
|
array(),
|
2014-11-04 14:16:58 +00:00
|
|
|
new \OC\Files\View('/' . $user . '/files')
|
2014-05-27 18:48:41 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if($newMountPoint !== $share['file_target']) {
|
|
|
|
self::updateFileTarget($newMountPoint, $share);
|
|
|
|
$share['file_target'] = $newMountPoint;
|
|
|
|
$share['unique_name'] = true;
|
|
|
|
}
|
2014-05-27 20:43:20 +00:00
|
|
|
|
|
|
|
return $newMountPoint;
|
2014-05-27 18:48:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* update fileTarget in the database if the mount point changed
|
|
|
|
* @param string $newPath
|
|
|
|
* @param array $share reference to the share which should be modified
|
2014-09-25 09:29:57 +00:00
|
|
|
* @return bool
|
2014-05-27 18:48:41 +00:00
|
|
|
*/
|
|
|
|
private static function updateFileTarget($newPath, &$share) {
|
|
|
|
// 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 14:56:04 +00:00
|
|
|
if ($share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP && empty($share['unique_name'])) {
|
2014-05-27 18:48:41 +00:00
|
|
|
$query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (`item_type`, `item_source`, `item_target`,'
|
|
|
|
.' `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'],
|
|
|
|
2, \OCP\User::getUser(), $share['uid_owner'], $share['permissions'], $share['stime'], $share['file_source'],
|
|
|
|
$newPath, $share['token'], $share['id']);
|
|
|
|
} else {
|
|
|
|
// rename mount point
|
|
|
|
$query = \OC_DB::prepare(
|
|
|
|
'Update `*PREFIX*share`
|
|
|
|
SET `file_target` = ?
|
|
|
|
WHERE `id` = ?'
|
|
|
|
);
|
|
|
|
$arguments = array($newPath, $share['id']);
|
|
|
|
}
|
|
|
|
|
2014-05-27 19:51:23 +00:00
|
|
|
$result = $query->execute($arguments);
|
|
|
|
|
|
|
|
return $result === 1 ? true : false;
|
2014-05-27 18:48:41 +00:00
|
|
|
}
|
|
|
|
|
2014-05-21 23:41:27 +00: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 09:13:01 +00:00
|
|
|
protected function stripUserFilesPath($path) {
|
2014-05-21 23:41:27 +00: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 09:13:01 +00:00
|
|
|
\OCP\Util::ERROR);
|
|
|
|
throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10);
|
2014-05-21 23:41:27 +00: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 09:55:59 +00: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-21 23:41:27 +00: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 12:40:22 +00:00
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager();
|
2014-09-25 09:29:57 +00:00
|
|
|
/** @var \OC\Files\Storage\Shared */
|
2014-05-27 13:07:53 +00:00
|
|
|
$storage = $this->getStorage();
|
2014-07-31 09:55:59 +00:00
|
|
|
$result = $storage->unshareStorage();
|
2014-07-02 12:40:22 +00:00
|
|
|
$mountManager->removeMount($this->mountPoint);
|
2014-05-27 13:07:53 +00:00
|
|
|
|
|
|
|
return $result;
|
2014-05-21 23:41:27 +00:00
|
|
|
}
|
|
|
|
}
|