|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @author Vincent Petry <pvince81@owncloud.com> |
| 4 | + * |
| 5 | + * @copyright Copyright (c) 2016, ownCloud, Inc. |
| 6 | + * @license AGPL-3.0 |
| 7 | + * |
| 8 | + * This code is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 10 | + * as published by the Free Software Foundation. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Affero General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 18 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 19 | + * |
| 20 | + */ |
| 21 | +namespace OCA\DAV\Connector\Sabre; |
| 22 | + |
| 23 | +/** |
| 24 | + * ownCloud |
| 25 | + * |
| 26 | + * @author Vincent Petry |
| 27 | + * @copyright 2016 Vincent Petry <pvince81@owncloud.com> |
| 28 | + * |
| 29 | + * This library is free software; you can redistribute it and/or |
| 30 | + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
| 31 | + * License as published by the Free Software Foundation; either |
| 32 | + * version 3 of the License, or any later version. |
| 33 | + * |
| 34 | + * This library is distributed in the hope that it will be useful, |
| 35 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 36 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 37 | + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
| 38 | + * |
| 39 | + * You should have received a copy of the GNU Affero General Public |
| 40 | + * License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 41 | + * |
| 42 | + */ |
| 43 | + |
| 44 | +use \Sabre\DAV\PropFind; |
| 45 | +use \Sabre\DAV\PropPatch; |
| 46 | +use OCP\IUserSession; |
| 47 | +use OCP\Share\IShare; |
| 48 | + |
| 49 | +class SharesPlugin extends \Sabre\DAV\ServerPlugin |
| 50 | +{ |
| 51 | + |
| 52 | + // namespace |
| 53 | + const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
| 54 | + const SHARES_PROPERTYNAME = '{http://owncloud.org/ns}shares'; |
| 55 | + |
| 56 | + /** |
| 57 | + * Reference to main server object |
| 58 | + * |
| 59 | + * @var \Sabre\DAV\Server |
| 60 | + */ |
| 61 | + private $server; |
| 62 | + |
| 63 | + /** |
| 64 | + * @var \OCP\Share\IManager |
| 65 | + */ |
| 66 | + private $shareManager; |
| 67 | + |
| 68 | + /** |
| 69 | + * @var \Sabre\DAV\Tree |
| 70 | + */ |
| 71 | + private $tree; |
| 72 | + |
| 73 | + /** |
| 74 | + * @var string |
| 75 | + */ |
| 76 | + private $userId; |
| 77 | + |
| 78 | + /** |
| 79 | + * @var \OCP\Files\Folder |
| 80 | + */ |
| 81 | + private $userFolder; |
| 82 | + |
| 83 | + /** |
| 84 | + * @var IShare[] |
| 85 | + */ |
| 86 | + private $cachedShares; |
| 87 | + |
| 88 | + /** |
| 89 | + * @param \Sabre\DAV\Tree $tree tree |
| 90 | + * @param \OCP\ITagManager $tagManager tag manager |
| 91 | + */ |
| 92 | + public function __construct( |
| 93 | + \Sabre\DAV\Tree $tree, |
| 94 | + IUserSession $userSession, |
| 95 | + \OCP\Files\Folder $userFolder, |
| 96 | + \OCP\Share\IManager $shareManager |
| 97 | + ) { |
| 98 | + $this->tree = $tree; |
| 99 | + $this->shareManager = $shareManager; |
| 100 | + $this->userFolder = $userFolder; |
| 101 | + $this->userId = $userSession->getUser()->getUID(); |
| 102 | + $this->cachedShares = []; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * This initializes the plugin. |
| 107 | + * |
| 108 | + * This function is called by \Sabre\DAV\Server, after |
| 109 | + * addPlugin is called. |
| 110 | + * |
| 111 | + * This method should set up the required event subscriptions. |
| 112 | + * |
| 113 | + * @param \Sabre\DAV\Server $server |
| 114 | + * @return void |
| 115 | + */ |
| 116 | + public function initialize(\Sabre\DAV\Server $server) { |
| 117 | + |
| 118 | + $server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc'; |
| 119 | + // FIXME |
| 120 | + //$server->xml->elementMap[self::SHARES_PROPERTYNAME] = 'OCA\\DAV\\Connector\\Sabre\\SharesList'; |
| 121 | + |
| 122 | + $this->server = $server; |
| 123 | + $this->server->on('propFind', array($this, 'handleGetProperties')); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * Returns shares for the given file id |
| 128 | + * |
| 129 | + * @param \OCP\Files\Node $node node for which to retrieve shares |
| 130 | + * |
| 131 | + * @return IShare list of shares |
| 132 | + */ |
| 133 | + private function getShares($node) { |
| 134 | + $shares = []; |
| 135 | + $shareTypes = [0, 1, 3]; // FIXME: use constants |
| 136 | + foreach ($shareTypes as $shareType) { |
| 137 | + // one of each type is enough to find out about the types |
| 138 | + $shares = array_merge($shares, $this->shareManager->getSharesBy($this->userId, $shareType, $node, false, 1)); |
| 139 | + } |
| 140 | + return $shares; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Adds shares to propfind response |
| 145 | + * |
| 146 | + * @param PropFind $propFind |
| 147 | + * @param \Sabre\DAV\INode $node |
| 148 | + * @return void |
| 149 | + */ |
| 150 | + public function handleGetProperties( |
| 151 | + PropFind $propFind, |
| 152 | + \Sabre\DAV\INode $sabreNode |
| 153 | + ) { |
| 154 | + if (!($sabreNode instanceof \OCA\DAV\Connector\Sabre\Node)) { |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + // need prefetch ? |
| 159 | + if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory |
| 160 | + && $propFind->getDepth() !== 0 |
| 161 | + && !is_null($propFind->getStatus(self::SHARES_PROPERTYNAME)) |
| 162 | + ) { |
| 163 | + $folderNode = $this->userFolder->get($propFind->getPath()); |
| 164 | + $children = $folderNode->getDirectoryListing(); |
| 165 | + |
| 166 | + foreach ($children as $childNode) { |
| 167 | + $this->cachedShares[$childNode->getId()] = $this->getShares($childNode); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + $propFind->handle(self::SHARES_PROPERTYNAME, function() use ($sabreNode) { |
| 172 | + if (isset($this->cachedShares[$sabreNode->getId()])) { |
| 173 | + $shares = $this->cachedShares[$sabreNode->getId()]; |
| 174 | + } else { |
| 175 | + $node = $this->userFolder->get($sabreNode->getPath()); |
| 176 | + $shares = $this->getShares($node); |
| 177 | + } |
| 178 | + |
| 179 | + $shareTypes = []; |
| 180 | + foreach ($shares as $share) { |
| 181 | + $shareTypes[] = $share->getShareType(); |
| 182 | + } |
| 183 | + |
| 184 | + // FIXME: decide on the response format |
| 185 | + if (!empty($shareTypes)) { |
| 186 | + return json_encode($shareTypes); |
| 187 | + } |
| 188 | + return null; |
| 189 | + }); |
| 190 | + } |
| 191 | +} |
0 commit comments