2016-10-27 16:32:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-10-27 16:32:23 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-27 16:32:23 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-10-27 16:32:23 +03:00
|
|
|
namespace OC\Share20;
|
|
|
|
|
2017-04-11 15:16:17 +03:00
|
|
|
use OCP\Files\InvalidPathException;
|
2016-10-27 16:32:23 +03:00
|
|
|
use OCP\Files\Node;
|
2017-04-11 15:16:17 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
2017-03-27 17:06:31 +03:00
|
|
|
use OCP\Share\IManager;
|
|
|
|
use OCP\Share\IShareHelper;
|
2016-10-27 16:32:23 +03:00
|
|
|
|
2017-03-27 17:06:31 +03:00
|
|
|
class ShareHelper implements IShareHelper {
|
2016-10-27 16:32:23 +03:00
|
|
|
|
2017-03-27 17:06:31 +03:00
|
|
|
/** @var IManager */
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
public function __construct(IManager $shareManager) {
|
|
|
|
$this->shareManager = $shareManager;
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Node $node
|
2017-03-28 18:06:26 +03:00
|
|
|
* @return array [ users => [Mapping $uid => $pathForUser], remotes => [Mapping $cloudId => $pathToMountRoot]]
|
2016-10-27 16:32:23 +03:00
|
|
|
*/
|
2017-03-27 17:06:31 +03:00
|
|
|
public function getPathsForAccessList(Node $node) {
|
|
|
|
$result = [
|
|
|
|
'users' => [],
|
|
|
|
'remotes' => [],
|
|
|
|
];
|
|
|
|
|
|
|
|
$accessList = $this->shareManager->getAccessList($node, true, true);
|
|
|
|
if (!empty($accessList['users'])) {
|
|
|
|
$result['users'] = $this->getPathsForUsers($node, $accessList['users']);
|
|
|
|
}
|
|
|
|
if (!empty($accessList['remote'])) {
|
|
|
|
$result['remotes'] = $this->getPathsForRemotes($node, $accessList['remote']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
/**
|
2017-04-11 15:45:18 +03:00
|
|
|
* Sample:
|
|
|
|
* $users = [
|
|
|
|
* 'test1' => ['node_id' => 16, 'node_path' => '/foo'],
|
|
|
|
* 'test2' => ['node_id' => 23, 'node_path' => '/bar'],
|
|
|
|
* 'test3' => ['node_id' => 42, 'node_path' => '/cat'],
|
|
|
|
* 'test4' => ['node_id' => 48, 'node_path' => '/dog'],
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* Node tree:
|
|
|
|
* - SixTeen is the parent of TwentyThree
|
|
|
|
* - TwentyThree is the parent of FortyTwo
|
|
|
|
* - FortyEight does not exist
|
|
|
|
*
|
|
|
|
* $return = [
|
|
|
|
* 'test1' => '/foo/TwentyThree/FortyTwo',
|
|
|
|
* 'test2' => '/bar/FortyTwo',
|
|
|
|
* 'test3' => '/cat',
|
|
|
|
* ],
|
|
|
|
*
|
2017-04-10 17:10:34 +03:00
|
|
|
* @param Node $node
|
|
|
|
* @param array[] $users
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-03-27 17:06:31 +03:00
|
|
|
protected function getPathsForUsers(Node $node, array $users) {
|
2017-04-11 15:16:17 +03:00
|
|
|
/** @var array[] $byId */
|
|
|
|
$byId = [];
|
|
|
|
/** @var array[] $results */
|
|
|
|
$results = [];
|
|
|
|
|
2017-03-27 17:06:31 +03:00
|
|
|
foreach ($users as $uid => $info) {
|
|
|
|
if (!isset($byId[$info['node_id']])) {
|
|
|
|
$byId[$info['node_id']] = [];
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|
2017-03-27 17:06:31 +03:00
|
|
|
$byId[$info['node_id']][$uid] = $info['node_path'];
|
|
|
|
}
|
2016-10-27 16:32:23 +03:00
|
|
|
|
2017-04-11 15:16:17 +03:00
|
|
|
try {
|
|
|
|
if (isset($byId[$node->getId()])) {
|
|
|
|
foreach ($byId[$node->getId()] as $uid => $path) {
|
|
|
|
$results[$uid] = $path;
|
|
|
|
}
|
|
|
|
unset($byId[$node->getId()]);
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|
2017-04-11 15:16:17 +03:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return $results;
|
|
|
|
} catch (InvalidPathException $e) {
|
|
|
|
return $results;
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
2016-10-27 16:32:23 +03:00
|
|
|
|
2017-03-27 17:06:31 +03:00
|
|
|
if (empty($byId)) {
|
|
|
|
return $results;
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|
|
|
|
|
2017-03-27 17:06:31 +03:00
|
|
|
$item = $node;
|
|
|
|
$appendix = '/' . $node->getName();
|
|
|
|
while (!empty($byId)) {
|
2017-04-11 15:16:17 +03:00
|
|
|
try {
|
|
|
|
/** @var Node $item */
|
|
|
|
$item = $item->getParent();
|
|
|
|
|
|
|
|
if (!empty($byId[$item->getId()])) {
|
|
|
|
foreach ($byId[$item->getId()] as $uid => $path) {
|
|
|
|
$results[$uid] = $path . $appendix;
|
|
|
|
}
|
|
|
|
unset($byId[$item->getId()]);
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
|
|
|
|
2017-04-11 15:16:17 +03:00
|
|
|
$appendix = '/' . $item->getName() . $appendix;
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return $results;
|
|
|
|
} catch (InvalidPathException $e) {
|
|
|
|
return $results;
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
return $results;
|
|
|
|
}
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
/**
|
2017-04-11 15:45:18 +03:00
|
|
|
* Sample:
|
|
|
|
* $remotes = [
|
|
|
|
* 'test1' => ['node_id' => 16, 'token' => 't1'],
|
|
|
|
* 'test2' => ['node_id' => 23, 'token' => 't2'],
|
|
|
|
* 'test3' => ['node_id' => 42, 'token' => 't3'],
|
|
|
|
* 'test4' => ['node_id' => 48, 'token' => 't4'],
|
|
|
|
* ];
|
|
|
|
*
|
|
|
|
* Node tree:
|
|
|
|
* - SixTeen is the parent of TwentyThree
|
|
|
|
* - TwentyThree is the parent of FortyTwo
|
|
|
|
* - FortyEight does not exist
|
|
|
|
*
|
|
|
|
* $return = [
|
|
|
|
* 'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
|
|
|
|
* 'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
|
|
|
|
* 'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
|
|
|
|
* ],
|
|
|
|
*
|
2017-04-10 17:10:34 +03:00
|
|
|
* @param Node $node
|
|
|
|
* @param array[] $remotes
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-03-27 17:06:31 +03:00
|
|
|
protected function getPathsForRemotes(Node $node, array $remotes) {
|
2017-04-11 15:16:17 +03:00
|
|
|
/** @var array[] $byId */
|
|
|
|
$byId = [];
|
|
|
|
/** @var array[] $results */
|
|
|
|
$results = [];
|
|
|
|
|
2017-03-27 17:06:31 +03:00
|
|
|
foreach ($remotes as $cloudId => $info) {
|
|
|
|
if (!isset($byId[$info['node_id']])) {
|
|
|
|
$byId[$info['node_id']] = [];
|
|
|
|
}
|
2017-03-28 00:39:20 +03:00
|
|
|
$byId[$info['node_id']][$cloudId] = $info['token'];
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$item = $node;
|
|
|
|
while (!empty($byId)) {
|
2017-04-11 15:16:17 +03:00
|
|
|
try {
|
|
|
|
if (!empty($byId[$item->getId()])) {
|
|
|
|
$path = $this->getMountedPath($item);
|
|
|
|
foreach ($byId[$item->getId()] as $uid => $token) {
|
|
|
|
$results[$uid] = [
|
|
|
|
'node_path' => $path,
|
|
|
|
'token' => $token,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
unset($byId[$item->getId()]);
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
2017-04-11 15:16:17 +03:00
|
|
|
|
|
|
|
/** @var Node $item */
|
|
|
|
$item = $item->getParent();
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return $results;
|
|
|
|
} catch (InvalidPathException $e) {
|
|
|
|
return $results;
|
|
|
|
} catch (NotPermittedException $e) {
|
|
|
|
return $results;
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|
2017-03-28 18:06:26 +03:00
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
/**
|
|
|
|
* @param Node $node
|
|
|
|
* @return string
|
|
|
|
*/
|
2017-03-28 18:06:26 +03:00
|
|
|
protected function getMountedPath(Node $node) {
|
|
|
|
$path = $node->getPath();
|
|
|
|
$sections = explode('/', $path, 4);
|
|
|
|
return '/' . $sections[3];
|
|
|
|
}
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|