2014-05-21 23:41:27 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
2015-10-05 18:54:56 +00:00
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin Appelman <icewind@owncloud.com>
|
2015-10-26 12:54:55 +00:00
|
|
|
* @author Roeland Jago Douma <rullzer@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
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/>
|
|
|
|
*
|
2014-05-21 23:41:27 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-05-21 23:41:27 +00:00
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
2016-04-14 09:50:27 +00:00
|
|
|
use OC\Files\Filesystem;
|
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;
|
2015-04-15 16:18:28 +00:00
|
|
|
use OC\Files\View;
|
2014-05-21 23:41:27 +00:00
|
|
|
|
|
|
|
/**
|
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;
|
|
|
|
|
2015-06-29 17:13:39 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\View
|
|
|
|
*/
|
|
|
|
private $recipientView;
|
|
|
|
|
2015-07-03 13:58:52 +00:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
|
2016-04-15 12:03:48 +00:00
|
|
|
/** @var \OCP\Share\IShare */
|
|
|
|
private $share;
|
|
|
|
|
2015-12-07 20:08:20 +00:00
|
|
|
/**
|
|
|
|
* @param string $storage
|
2016-04-14 09:50:27 +00:00
|
|
|
* @param SharedMount[] $mountpoints
|
2015-12-08 08:11:50 +00:00
|
|
|
* @param array|null $arguments
|
2015-12-07 20:08:20 +00:00
|
|
|
* @param \OCP\Files\Storage\IStorageFactory $loader
|
|
|
|
*/
|
2016-04-14 09:50:27 +00:00
|
|
|
public function __construct($storage, array $mountpoints, $arguments = null, $loader = null) {
|
2015-07-03 13:58:52 +00:00
|
|
|
$this->user = $arguments['user'];
|
|
|
|
$this->recipientView = new View('/' . $this->user . '/files');
|
2016-04-15 12:03:48 +00:00
|
|
|
$this->share = $arguments['newShare'];
|
|
|
|
$newMountPoint = $this->verifyMountPoint($this->share, $mountpoints);
|
2015-07-03 13:58:52 +00:00
|
|
|
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint;
|
2016-04-15 12:03:48 +00:00
|
|
|
$arguments['ownerView'] = new View('/' . $this->share->getShareOwner() . '/files');
|
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
|
2015-12-08 08:11:50 +00:00
|
|
|
*
|
2016-04-15 07:01:10 +00:00
|
|
|
* @param \OCP\Share\IShare $share
|
2016-04-14 09:50:27 +00:00
|
|
|
* @param SharedMount[] $mountpoints
|
2015-12-08 08:11:50 +00:00
|
|
|
* @return string
|
2014-05-27 18:48:41 +00:00
|
|
|
*/
|
2016-04-15 07:01:10 +00:00
|
|
|
private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints) {
|
2014-05-27 18:48:41 +00:00
|
|
|
|
2016-04-15 07:01:10 +00:00
|
|
|
$mountPoint = basename($share->getTarget());
|
|
|
|
$parent = dirname($share->getTarget());
|
2014-05-27 18:48:41 +00:00
|
|
|
|
2015-06-29 17:13:39 +00:00
|
|
|
if (!$this->recipientView->is_dir($parent)) {
|
2015-03-26 20:56:44 +00:00
|
|
|
$parent = Helper::getShareFolder();
|
2014-05-27 18:48:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-14 09:50:27 +00:00
|
|
|
$newMountPoint = $this->generateUniqueTarget(
|
2015-06-29 17:13:39 +00:00
|
|
|
\OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint),
|
2016-04-14 09:50:27 +00:00
|
|
|
$this->recipientView,
|
|
|
|
$mountpoints
|
2015-06-29 17:13:39 +00:00
|
|
|
);
|
2014-05-27 18:48:41 +00:00
|
|
|
|
2016-04-15 07:01:10 +00:00
|
|
|
if ($newMountPoint !== $share->getTarget()) {
|
2015-09-07 12:28:20 +00:00
|
|
|
$this->updateFileTarget($newMountPoint, $share);
|
2014-05-27 18:48:41 +00:00
|
|
|
}
|
2014-05-27 20:43:20 +00:00
|
|
|
|
|
|
|
return $newMountPoint;
|
2014-05-27 18:48:41 +00:00
|
|
|
}
|
|
|
|
|
2016-04-15 07:01:10 +00:00
|
|
|
/**
|
|
|
|
* update fileTarget in the database if the mount point changed
|
|
|
|
*
|
|
|
|
* @param string $newPath
|
|
|
|
* @param \OCP\Share\IShare $share
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function updateFileTarget($newPath, &$share) {
|
|
|
|
$share->setTarget($newPath);
|
|
|
|
\OC::$server->getShareManager()->moveShare($share, $this->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-04-14 09:50:27 +00:00
|
|
|
/**
|
|
|
|
* @param string $path
|
|
|
|
* @param View $view
|
|
|
|
* @param SharedMount[] $mountpoints
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
private function generateUniqueTarget($path, $view, array $mountpoints) {
|
|
|
|
$pathinfo = pathinfo($path);
|
|
|
|
$ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : '';
|
|
|
|
$name = $pathinfo['filename'];
|
|
|
|
$dir = $pathinfo['dirname'];
|
|
|
|
|
|
|
|
// Helper function to find existing mount points
|
|
|
|
$mountpointExists = function($path) use ($mountpoints) {
|
|
|
|
foreach ($mountpoints as $mountpoint) {
|
2016-04-15 07:01:10 +00:00
|
|
|
if ($mountpoint->getShare()->getTarget() === $path) {
|
2016-04-14 09:50:27 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$i = 2;
|
|
|
|
while ($view->file_exists($path) || $mountpointExists($path)) {
|
|
|
|
$path = Filesystem::normalizePath($dir . '/' . $name . ' ('.$i.')' . $ext);
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
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'
|
2015-12-08 08:11:50 +00:00
|
|
|
* @throws \OCA\Files_Sharing\Exceptions\BrokenPath
|
2014-05-21 23:41:27 +00:00
|
|
|
*/
|
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;
|
|
|
|
|
2016-04-15 07:01:10 +00:00
|
|
|
try {
|
|
|
|
$this->updateFileTarget($relTargetPath, $share);
|
2014-05-21 23:41:27 +00:00
|
|
|
$this->setMountPoint($target);
|
|
|
|
$this->storage->setMountPoint($relTargetPath);
|
2016-04-15 07:01:10 +00:00
|
|
|
} catch (\Exception $e) {
|
2014-05-21 23:41:27 +00:00
|
|
|
\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();
|
2015-12-08 08:11:50 +00:00
|
|
|
/** @var $storage \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
|
|
|
}
|
2015-03-09 15:20:18 +00:00
|
|
|
|
2015-12-08 08:11:50 +00:00
|
|
|
/**
|
2016-04-15 07:01:10 +00:00
|
|
|
* @return \OCP\Share\IShare
|
2015-12-08 08:11:50 +00:00
|
|
|
*/
|
2015-03-09 15:20:18 +00:00
|
|
|
public function getShare() {
|
2016-04-15 12:03:48 +00:00
|
|
|
return $this->share;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the file id of the root of the storage
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getStorageRootId() {
|
|
|
|
return $this->share->getNodeId();
|
2015-03-09 15:20:18 +00:00
|
|
|
}
|
2014-05-21 23:41:27 +00:00
|
|
|
}
|