2016-10-27 16:32:23 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OC\Share20;
|
|
|
|
|
|
|
|
use OCP\Files\Node;
|
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
|
|
|
/**
|
|
|
|
* @param Node $node
|
|
|
|
* @param array[] $users
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-03-27 17:06:31 +03:00
|
|
|
protected function getPathsForUsers(Node $node, array $users) {
|
|
|
|
$byId = $results = [];
|
|
|
|
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-03-27 17:06:31 +03:00
|
|
|
if (isset($byId[$node->getId()])) {
|
|
|
|
foreach ($byId[$node->getId()] as $uid => $path) {
|
|
|
|
$results[$uid] = $path;
|
2016-10-27 16:32:23 +03:00
|
|
|
}
|
2017-03-27 17:06:31 +03:00
|
|
|
unset($byId[$node->getId()]);
|
|
|
|
}
|
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-10 17:10:34 +03:00
|
|
|
/** @var Node $item */
|
2017-03-27 17:06:31 +03:00
|
|
|
$item = $item->getParent();
|
|
|
|
|
|
|
|
if (!empty($byId[$item->getId()])) {
|
|
|
|
foreach ($byId[$item->getId()] as $uid => $path) {
|
|
|
|
$results[$uid] = $path . $appendix;
|
|
|
|
}
|
|
|
|
unset($byId[$item->getId()]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$appendix = '/' . $item->getName() . $appendix;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
$byId = $results = [];
|
|
|
|
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)) {
|
|
|
|
if (!empty($byId[$item->getId()])) {
|
2017-03-28 18:06:26 +03:00
|
|
|
$path = $this->getMountedPath($item);
|
2017-03-28 00:39:20 +03:00
|
|
|
foreach ($byId[$item->getId()] as $uid => $token) {
|
|
|
|
$results[$uid] = [
|
|
|
|
'node_path' => $path,
|
|
|
|
'token' => $token,
|
|
|
|
];
|
2017-03-27 17:06:31 +03:00
|
|
|
}
|
|
|
|
unset($byId[$item->getId()]);
|
|
|
|
}
|
2017-03-28 18:06:26 +03:00
|
|
|
$item = $item->getParent();
|
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
|
|
|
}
|