1
0

DefaultShareProvider.php 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andrius <andrius.kulbis@ktu.lt>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Jan-Philipp Litza <jplitza@users.noreply.github.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author phisch <git@philippschaffrath.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Share20;
  31. use OC\Files\Cache\Cache;
  32. use OCP\Files\Folder;
  33. use OCP\Share\IShareProvider;
  34. use OC\Share20\Exception\InvalidShare;
  35. use OC\Share20\Exception\ProviderException;
  36. use OCP\Share\Exceptions\ShareNotFound;
  37. use OC\Share20\Exception\BackendError;
  38. use OCP\DB\QueryBuilder\IQueryBuilder;
  39. use OCP\IGroup;
  40. use OCP\IGroupManager;
  41. use OCP\IUserManager;
  42. use OCP\Files\IRootFolder;
  43. use OCP\IDBConnection;
  44. use OCP\Files\Node;
  45. /**
  46. * Class DefaultShareProvider
  47. *
  48. * @package OC\Share20
  49. */
  50. class DefaultShareProvider implements IShareProvider {
  51. // Special share type for user modified group shares
  52. const SHARE_TYPE_USERGROUP = 2;
  53. /** @var IDBConnection */
  54. private $dbConn;
  55. /** @var IUserManager */
  56. private $userManager;
  57. /** @var IGroupManager */
  58. private $groupManager;
  59. /** @var IRootFolder */
  60. private $rootFolder;
  61. /**
  62. * DefaultShareProvider constructor.
  63. *
  64. * @param IDBConnection $connection
  65. * @param IUserManager $userManager
  66. * @param IGroupManager $groupManager
  67. * @param IRootFolder $rootFolder
  68. */
  69. public function __construct(
  70. IDBConnection $connection,
  71. IUserManager $userManager,
  72. IGroupManager $groupManager,
  73. IRootFolder $rootFolder) {
  74. $this->dbConn = $connection;
  75. $this->userManager = $userManager;
  76. $this->groupManager = $groupManager;
  77. $this->rootFolder = $rootFolder;
  78. }
  79. /**
  80. * Return the identifier of this provider.
  81. *
  82. * @return string Containing only [a-zA-Z0-9]
  83. */
  84. public function identifier() {
  85. return 'ocinternal';
  86. }
  87. /**
  88. * Share a path
  89. *
  90. * @param \OCP\Share\IShare $share
  91. * @return \OCP\Share\IShare The share object
  92. * @throws ShareNotFound
  93. * @throws \Exception
  94. */
  95. public function create(\OCP\Share\IShare $share) {
  96. $qb = $this->dbConn->getQueryBuilder();
  97. $qb->insert('share');
  98. $qb->setValue('share_type', $qb->createNamedParameter($share->getShareType()));
  99. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  100. //Set the UID of the user we share with
  101. $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
  102. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  103. //Set the GID of the group we share with
  104. $qb->setValue('share_with', $qb->createNamedParameter($share->getSharedWith()));
  105. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  106. //Set the token of the share
  107. $qb->setValue('token', $qb->createNamedParameter($share->getToken()));
  108. //If a password is set store it
  109. if ($share->getPassword() !== null) {
  110. $qb->setValue('password', $qb->createNamedParameter($share->getPassword()));
  111. }
  112. //If an expiration date is set store it
  113. if ($share->getExpirationDate() !== null) {
  114. $qb->setValue('expiration', $qb->createNamedParameter($share->getExpirationDate(), 'datetime'));
  115. }
  116. if (method_exists($share, 'getParent')) {
  117. $qb->setValue('parent', $qb->createNamedParameter($share->getParent()));
  118. }
  119. } else {
  120. throw new \Exception('invalid share type!');
  121. }
  122. // Set what is shares
  123. $qb->setValue('item_type', $qb->createParameter('itemType'));
  124. if ($share->getNode() instanceof \OCP\Files\File) {
  125. $qb->setParameter('itemType', 'file');
  126. } else {
  127. $qb->setParameter('itemType', 'folder');
  128. }
  129. // Set the file id
  130. $qb->setValue('item_source', $qb->createNamedParameter($share->getNode()->getId()));
  131. $qb->setValue('file_source', $qb->createNamedParameter($share->getNode()->getId()));
  132. // set the permissions
  133. $qb->setValue('permissions', $qb->createNamedParameter($share->getPermissions()));
  134. // Set who created this share
  135. $qb->setValue('uid_initiator', $qb->createNamedParameter($share->getSharedBy()));
  136. // Set who is the owner of this file/folder (and this the owner of the share)
  137. $qb->setValue('uid_owner', $qb->createNamedParameter($share->getShareOwner()));
  138. // Set the file target
  139. $qb->setValue('file_target', $qb->createNamedParameter($share->getTarget()));
  140. // Set the time this share was created
  141. $qb->setValue('stime', $qb->createNamedParameter(time()));
  142. // insert the data and fetch the id of the share
  143. $this->dbConn->beginTransaction();
  144. $qb->execute();
  145. $id = $this->dbConn->lastInsertId('*PREFIX*share');
  146. // Now fetch the inserted share and create a complete share object
  147. $qb = $this->dbConn->getQueryBuilder();
  148. $qb->select('*')
  149. ->from('share')
  150. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  151. $cursor = $qb->execute();
  152. $data = $cursor->fetch();
  153. $this->dbConn->commit();
  154. $cursor->closeCursor();
  155. if ($data === false) {
  156. throw new ShareNotFound();
  157. }
  158. $mailSendValue = $share->getMailSend();
  159. $data['mail_send'] = ($mailSendValue === null) ? true : $mailSendValue;
  160. $share = $this->createShare($data);
  161. return $share;
  162. }
  163. /**
  164. * Update a share
  165. *
  166. * @param \OCP\Share\IShare $share
  167. * @return \OCP\Share\IShare The share object
  168. */
  169. public function update(\OCP\Share\IShare $share) {
  170. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  171. /*
  172. * We allow updating the recipient on user shares.
  173. */
  174. $qb = $this->dbConn->getQueryBuilder();
  175. $qb->update('share')
  176. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  177. ->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
  178. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  179. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  180. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  181. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  182. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  183. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  184. ->execute();
  185. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  186. $qb = $this->dbConn->getQueryBuilder();
  187. $qb->update('share')
  188. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  189. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  190. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  191. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  192. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  193. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  194. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  195. ->execute();
  196. /*
  197. * Update all user defined group shares
  198. */
  199. $qb = $this->dbConn->getQueryBuilder();
  200. $qb->update('share')
  201. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  202. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  203. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  204. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  205. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  206. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  207. ->execute();
  208. /*
  209. * Now update the permissions for all children that have not set it to 0
  210. */
  211. $qb = $this->dbConn->getQueryBuilder();
  212. $qb->update('share')
  213. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  214. ->andWhere($qb->expr()->neq('permissions', $qb->createNamedParameter(0)))
  215. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  216. ->execute();
  217. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  218. $qb = $this->dbConn->getQueryBuilder();
  219. $qb->update('share')
  220. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  221. ->set('password', $qb->createNamedParameter($share->getPassword()))
  222. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  223. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  224. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  225. ->set('item_source', $qb->createNamedParameter($share->getNode()->getId()))
  226. ->set('file_source', $qb->createNamedParameter($share->getNode()->getId()))
  227. ->set('token', $qb->createNamedParameter($share->getToken()))
  228. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATE))
  229. ->execute();
  230. }
  231. return $share;
  232. }
  233. /**
  234. * Get all children of this share
  235. * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in
  236. *
  237. * @param \OCP\Share\IShare $parent
  238. * @return \OCP\Share\IShare[]
  239. */
  240. public function getChildren(\OCP\Share\IShare $parent) {
  241. $children = [];
  242. $qb = $this->dbConn->getQueryBuilder();
  243. $qb->select('*')
  244. ->from('share')
  245. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  246. ->andWhere(
  247. $qb->expr()->in(
  248. 'share_type',
  249. $qb->createNamedParameter([
  250. \OCP\Share::SHARE_TYPE_USER,
  251. \OCP\Share::SHARE_TYPE_GROUP,
  252. \OCP\Share::SHARE_TYPE_LINK,
  253. ], IQueryBuilder::PARAM_INT_ARRAY)
  254. )
  255. )
  256. ->andWhere($qb->expr()->orX(
  257. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  258. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  259. ))
  260. ->orderBy('id');
  261. $cursor = $qb->execute();
  262. while($data = $cursor->fetch()) {
  263. $children[] = $this->createShare($data);
  264. }
  265. $cursor->closeCursor();
  266. return $children;
  267. }
  268. /**
  269. * Delete a share
  270. *
  271. * @param \OCP\Share\IShare $share
  272. */
  273. public function delete(\OCP\Share\IShare $share) {
  274. $qb = $this->dbConn->getQueryBuilder();
  275. $qb->delete('share')
  276. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())));
  277. /*
  278. * If the share is a group share delete all possible
  279. * user defined groups shares.
  280. */
  281. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  282. $qb->orWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())));
  283. }
  284. $qb->execute();
  285. }
  286. /**
  287. * Unshare a share from the recipient. If this is a group share
  288. * this means we need a special entry in the share db.
  289. *
  290. * @param \OCP\Share\IShare $share
  291. * @param string $recipient UserId of recipient
  292. * @throws BackendError
  293. * @throws ProviderException
  294. */
  295. public function deleteFromSelf(\OCP\Share\IShare $share, $recipient) {
  296. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  297. $group = $this->groupManager->get($share->getSharedWith());
  298. $user = $this->userManager->get($recipient);
  299. if (is_null($group)) {
  300. throw new ProviderException('Group "' . $share->getSharedWith() . '" does not exist');
  301. }
  302. if (!$group->inGroup($user)) {
  303. throw new ProviderException('Recipient not in receiving group');
  304. }
  305. // Try to fetch user specific share
  306. $qb = $this->dbConn->getQueryBuilder();
  307. $stmt = $qb->select('*')
  308. ->from('share')
  309. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  310. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
  311. ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  312. ->andWhere($qb->expr()->orX(
  313. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  314. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  315. ))
  316. ->execute();
  317. $data = $stmt->fetch();
  318. /*
  319. * Check if there already is a user specific group share.
  320. * If there is update it (if required).
  321. */
  322. if ($data === false) {
  323. $qb = $this->dbConn->getQueryBuilder();
  324. $type = $share->getNodeType();
  325. //Insert new share
  326. $qb->insert('share')
  327. ->values([
  328. 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
  329. 'share_with' => $qb->createNamedParameter($recipient),
  330. 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
  331. 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
  332. 'parent' => $qb->createNamedParameter($share->getId()),
  333. 'item_type' => $qb->createNamedParameter($type),
  334. 'item_source' => $qb->createNamedParameter($share->getNodeId()),
  335. 'file_source' => $qb->createNamedParameter($share->getNodeId()),
  336. 'file_target' => $qb->createNamedParameter($share->getTarget()),
  337. 'permissions' => $qb->createNamedParameter(0),
  338. 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
  339. ])->execute();
  340. } else if ($data['permissions'] !== 0) {
  341. // Update existing usergroup share
  342. $qb = $this->dbConn->getQueryBuilder();
  343. $qb->update('share')
  344. ->set('permissions', $qb->createNamedParameter(0))
  345. ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
  346. ->execute();
  347. }
  348. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  349. if ($share->getSharedWith() !== $recipient) {
  350. throw new ProviderException('Recipient does not match');
  351. }
  352. // We can just delete user and link shares
  353. $this->delete($share);
  354. } else {
  355. throw new ProviderException('Invalid shareType');
  356. }
  357. }
  358. /**
  359. * @inheritdoc
  360. */
  361. public function move(\OCP\Share\IShare $share, $recipient) {
  362. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  363. // Just update the target
  364. $qb = $this->dbConn->getQueryBuilder();
  365. $qb->update('share')
  366. ->set('file_target', $qb->createNamedParameter($share->getTarget()))
  367. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  368. ->execute();
  369. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  370. // Check if there is a usergroup share
  371. $qb = $this->dbConn->getQueryBuilder();
  372. $stmt = $qb->select('id')
  373. ->from('share')
  374. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  375. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($recipient)))
  376. ->andWhere($qb->expr()->eq('parent', $qb->createNamedParameter($share->getId())))
  377. ->andWhere($qb->expr()->orX(
  378. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  379. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  380. ))
  381. ->setMaxResults(1)
  382. ->execute();
  383. $data = $stmt->fetch();
  384. $stmt->closeCursor();
  385. if ($data === false) {
  386. // No usergroup share yet. Create one.
  387. $qb = $this->dbConn->getQueryBuilder();
  388. $qb->insert('share')
  389. ->values([
  390. 'share_type' => $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP),
  391. 'share_with' => $qb->createNamedParameter($recipient),
  392. 'uid_owner' => $qb->createNamedParameter($share->getShareOwner()),
  393. 'uid_initiator' => $qb->createNamedParameter($share->getSharedBy()),
  394. 'parent' => $qb->createNamedParameter($share->getId()),
  395. 'item_type' => $qb->createNamedParameter($share->getNodeType()),
  396. 'item_source' => $qb->createNamedParameter($share->getNodeId()),
  397. 'file_source' => $qb->createNamedParameter($share->getNodeId()),
  398. 'file_target' => $qb->createNamedParameter($share->getTarget()),
  399. 'permissions' => $qb->createNamedParameter($share->getPermissions()),
  400. 'stime' => $qb->createNamedParameter($share->getShareTime()->getTimestamp()),
  401. ])->execute();
  402. } else {
  403. // Already a usergroup share. Update it.
  404. $qb = $this->dbConn->getQueryBuilder();
  405. $qb->update('share')
  406. ->set('file_target', $qb->createNamedParameter($share->getTarget()))
  407. ->where($qb->expr()->eq('id', $qb->createNamedParameter($data['id'])))
  408. ->execute();
  409. }
  410. }
  411. return $share;
  412. }
  413. public function getSharesInFolder($userId, Folder $node, $reshares) {
  414. $qb = $this->dbConn->getQueryBuilder();
  415. $qb->select('*')
  416. ->from('share', 's')
  417. ->andWhere($qb->expr()->orX(
  418. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  419. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  420. ));
  421. $qb->andWhere($qb->expr()->orX(
  422. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
  423. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
  424. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
  425. ));
  426. /**
  427. * Reshares for this user are shares where they are the owner.
  428. */
  429. if ($reshares === false) {
  430. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  431. } else {
  432. $qb->andWhere(
  433. $qb->expr()->orX(
  434. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  435. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  436. )
  437. );
  438. }
  439. $qb->innerJoin('s', 'filecache' ,'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
  440. $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
  441. $qb->orderBy('id');
  442. $cursor = $qb->execute();
  443. $shares = [];
  444. while ($data = $cursor->fetch()) {
  445. $shares[$data['fileid']][] = $this->createShare($data);
  446. }
  447. $cursor->closeCursor();
  448. return $shares;
  449. }
  450. /**
  451. * @inheritdoc
  452. */
  453. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
  454. $qb = $this->dbConn->getQueryBuilder();
  455. $qb->select('*')
  456. ->from('share')
  457. ->andWhere($qb->expr()->orX(
  458. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  459. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  460. ));
  461. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter($shareType)));
  462. /**
  463. * Reshares for this user are shares where they are the owner.
  464. */
  465. if ($reshares === false) {
  466. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  467. } else {
  468. $qb->andWhere(
  469. $qb->expr()->orX(
  470. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  471. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  472. )
  473. );
  474. }
  475. if ($node !== null) {
  476. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  477. }
  478. if ($limit !== -1) {
  479. $qb->setMaxResults($limit);
  480. }
  481. $qb->setFirstResult($offset);
  482. $qb->orderBy('id');
  483. $cursor = $qb->execute();
  484. $shares = [];
  485. while($data = $cursor->fetch()) {
  486. $shares[] = $this->createShare($data);
  487. }
  488. $cursor->closeCursor();
  489. return $shares;
  490. }
  491. /**
  492. * @inheritdoc
  493. */
  494. public function getShareById($id, $recipientId = null) {
  495. $qb = $this->dbConn->getQueryBuilder();
  496. $qb->select('*')
  497. ->from('share')
  498. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  499. ->andWhere(
  500. $qb->expr()->in(
  501. 'share_type',
  502. $qb->createNamedParameter([
  503. \OCP\Share::SHARE_TYPE_USER,
  504. \OCP\Share::SHARE_TYPE_GROUP,
  505. \OCP\Share::SHARE_TYPE_LINK,
  506. ], IQueryBuilder::PARAM_INT_ARRAY)
  507. )
  508. )
  509. ->andWhere($qb->expr()->orX(
  510. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  511. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  512. ));
  513. $cursor = $qb->execute();
  514. $data = $cursor->fetch();
  515. $cursor->closeCursor();
  516. if ($data === false) {
  517. throw new ShareNotFound();
  518. }
  519. try {
  520. $share = $this->createShare($data);
  521. } catch (InvalidShare $e) {
  522. throw new ShareNotFound();
  523. }
  524. // If the recipient is set for a group share resolve to that user
  525. if ($recipientId !== null && $share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  526. $share = $this->resolveGroupShares([$share], $recipientId)[0];
  527. }
  528. return $share;
  529. }
  530. /**
  531. * Get shares for a given path
  532. *
  533. * @param \OCP\Files\Node $path
  534. * @return \OCP\Share\IShare[]
  535. */
  536. public function getSharesByPath(Node $path) {
  537. $qb = $this->dbConn->getQueryBuilder();
  538. $cursor = $qb->select('*')
  539. ->from('share')
  540. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  541. ->andWhere(
  542. $qb->expr()->orX(
  543. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
  544. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP))
  545. )
  546. )
  547. ->andWhere($qb->expr()->orX(
  548. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  549. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  550. ))
  551. ->execute();
  552. $shares = [];
  553. while($data = $cursor->fetch()) {
  554. $shares[] = $this->createShare($data);
  555. }
  556. $cursor->closeCursor();
  557. return $shares;
  558. }
  559. /**
  560. * Returns whether the given database result can be interpreted as
  561. * a share with accessible file (not trashed, not deleted)
  562. */
  563. private function isAccessibleResult($data) {
  564. // exclude shares leading to deleted file entries
  565. if ($data['fileid'] === null) {
  566. return false;
  567. }
  568. // exclude shares leading to trashbin on home storages
  569. $pathSections = explode('/', $data['path'], 2);
  570. // FIXME: would not detect rare md5'd home storage case properly
  571. if ($pathSections[0] !== 'files'
  572. && in_array(explode(':', $data['storage_string_id'], 2)[0], array('home', 'object'))) {
  573. return false;
  574. }
  575. return true;
  576. }
  577. /**
  578. * @inheritdoc
  579. */
  580. public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
  581. /** @var Share[] $shares */
  582. $shares = [];
  583. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  584. //Get shares directly with this user
  585. $qb = $this->dbConn->getQueryBuilder();
  586. $qb->select('s.*',
  587. 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
  588. 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
  589. 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
  590. )
  591. ->selectAlias('st.id', 'storage_string_id')
  592. ->from('share', 's')
  593. ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
  594. ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'));
  595. // Order by id
  596. $qb->orderBy('s.id');
  597. // Set limit and offset
  598. if ($limit !== -1) {
  599. $qb->setMaxResults($limit);
  600. }
  601. $qb->setFirstResult($offset);
  602. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)))
  603. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
  604. ->andWhere($qb->expr()->orX(
  605. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  606. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  607. ));
  608. // Filter by node if provided
  609. if ($node !== null) {
  610. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  611. }
  612. $cursor = $qb->execute();
  613. while($data = $cursor->fetch()) {
  614. if ($this->isAccessibleResult($data)) {
  615. $shares[] = $this->createShare($data);
  616. }
  617. }
  618. $cursor->closeCursor();
  619. } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  620. $user = $this->userManager->get($userId);
  621. $allGroups = $this->groupManager->getUserGroups($user);
  622. /** @var Share[] $shares2 */
  623. $shares2 = [];
  624. $start = 0;
  625. while(true) {
  626. $groups = array_slice($allGroups, $start, 100);
  627. $start += 100;
  628. if ($groups === []) {
  629. break;
  630. }
  631. $qb = $this->dbConn->getQueryBuilder();
  632. $qb->select('s.*',
  633. 'f.fileid', 'f.path', 'f.permissions AS f_permissions', 'f.storage', 'f.path_hash',
  634. 'f.parent AS f_parent', 'f.name', 'f.mimetype', 'f.mimepart', 'f.size', 'f.mtime', 'f.storage_mtime',
  635. 'f.encrypted', 'f.unencrypted_size', 'f.etag', 'f.checksum'
  636. )
  637. ->selectAlias('st.id', 'storage_string_id')
  638. ->from('share', 's')
  639. ->leftJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'))
  640. ->leftJoin('f', 'storages', 'st', $qb->expr()->eq('f.storage', 'st.numeric_id'))
  641. ->orderBy('s.id')
  642. ->setFirstResult(0);
  643. if ($limit !== -1) {
  644. $qb->setMaxResults($limit - count($shares));
  645. }
  646. // Filter by node if provided
  647. if ($node !== null) {
  648. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  649. }
  650. $groups = array_filter($groups, function($group) { return $group instanceof IGroup; });
  651. $groups = array_map(function(IGroup $group) { return $group->getGID(); }, $groups);
  652. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  653. ->andWhere($qb->expr()->in('share_with', $qb->createNamedParameter(
  654. $groups,
  655. IQueryBuilder::PARAM_STR_ARRAY
  656. )))
  657. ->andWhere($qb->expr()->orX(
  658. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  659. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  660. ));
  661. $cursor = $qb->execute();
  662. while($data = $cursor->fetch()) {
  663. if ($offset > 0) {
  664. $offset--;
  665. continue;
  666. }
  667. if ($this->isAccessibleResult($data)) {
  668. $shares2[] = $this->createShare($data);
  669. }
  670. }
  671. $cursor->closeCursor();
  672. }
  673. /*
  674. * Resolve all group shares to user specific shares
  675. */
  676. $shares = $this->resolveGroupShares($shares2, $userId);
  677. } else {
  678. throw new BackendError('Invalid backend');
  679. }
  680. return $shares;
  681. }
  682. /**
  683. * Get a share by token
  684. *
  685. * @param string $token
  686. * @return \OCP\Share\IShare
  687. * @throws ShareNotFound
  688. */
  689. public function getShareByToken($token) {
  690. $qb = $this->dbConn->getQueryBuilder();
  691. $cursor = $qb->select('*')
  692. ->from('share')
  693. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)))
  694. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  695. ->andWhere($qb->expr()->orX(
  696. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  697. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  698. ))
  699. ->execute();
  700. $data = $cursor->fetch();
  701. if ($data === false) {
  702. throw new ShareNotFound();
  703. }
  704. try {
  705. $share = $this->createShare($data);
  706. } catch (InvalidShare $e) {
  707. throw new ShareNotFound();
  708. }
  709. return $share;
  710. }
  711. /**
  712. * Create a share object from an database row
  713. *
  714. * @param mixed[] $data
  715. * @return \OCP\Share\IShare
  716. * @throws InvalidShare
  717. */
  718. private function createShare($data) {
  719. $share = new Share($this->rootFolder, $this->userManager);
  720. $share->setId((int)$data['id'])
  721. ->setShareType((int)$data['share_type'])
  722. ->setPermissions((int)$data['permissions'])
  723. ->setTarget($data['file_target'])
  724. ->setMailSend((bool)$data['mail_send']);
  725. $shareTime = new \DateTime();
  726. $shareTime->setTimestamp((int)$data['stime']);
  727. $share->setShareTime($shareTime);
  728. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) {
  729. $share->setSharedWith($data['share_with']);
  730. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) {
  731. $share->setSharedWith($data['share_with']);
  732. } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) {
  733. $share->setPassword($data['password']);
  734. $share->setToken($data['token']);
  735. }
  736. $share->setSharedBy($data['uid_initiator']);
  737. $share->setShareOwner($data['uid_owner']);
  738. $share->setNodeId((int)$data['file_source']);
  739. $share->setNodeType($data['item_type']);
  740. if ($data['expiration'] !== null) {
  741. $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
  742. $share->setExpirationDate($expiration);
  743. }
  744. if (isset($data['f_permissions'])) {
  745. $entryData = $data;
  746. $entryData['permissions'] = $entryData['f_permissions'];
  747. $entryData['parent'] = $entryData['f_parent'];
  748. $share->setNodeCacheEntry(Cache::cacheEntryFromData($entryData,
  749. \OC::$server->getMimeTypeLoader()));
  750. }
  751. $share->setProviderId($this->identifier());
  752. return $share;
  753. }
  754. /**
  755. * @param Share[] $shares
  756. * @param $userId
  757. * @return Share[] The updates shares if no update is found for a share return the original
  758. */
  759. private function resolveGroupShares($shares, $userId) {
  760. $result = [];
  761. $start = 0;
  762. while(true) {
  763. /** @var Share[] $shareSlice */
  764. $shareSlice = array_slice($shares, $start, 100);
  765. $start += 100;
  766. if ($shareSlice === []) {
  767. break;
  768. }
  769. /** @var int[] $ids */
  770. $ids = [];
  771. /** @var Share[] $shareMap */
  772. $shareMap = [];
  773. foreach ($shareSlice as $share) {
  774. $ids[] = (int)$share->getId();
  775. $shareMap[$share->getId()] = $share;
  776. }
  777. $qb = $this->dbConn->getQueryBuilder();
  778. $query = $qb->select('*')
  779. ->from('share')
  780. ->where($qb->expr()->in('parent', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  781. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)))
  782. ->andWhere($qb->expr()->orX(
  783. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  784. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  785. ));
  786. $stmt = $query->execute();
  787. while($data = $stmt->fetch()) {
  788. $shareMap[$data['parent']]->setPermissions((int)$data['permissions']);
  789. $shareMap[$data['parent']]->setTarget($data['file_target']);
  790. }
  791. $stmt->closeCursor();
  792. foreach ($shareMap as $share) {
  793. $result[] = $share;
  794. }
  795. }
  796. return $result;
  797. }
  798. /**
  799. * A user is deleted from the system
  800. * So clean up the relevant shares.
  801. *
  802. * @param string $uid
  803. * @param int $shareType
  804. */
  805. public function userDeleted($uid, $shareType) {
  806. $qb = $this->dbConn->getQueryBuilder();
  807. $qb->delete('share');
  808. if ($shareType === \OCP\Share::SHARE_TYPE_USER) {
  809. /*
  810. * Delete all user shares that are owned by this user
  811. * or that are received by this user
  812. */
  813. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)));
  814. $qb->andWhere(
  815. $qb->expr()->orX(
  816. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
  817. $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
  818. )
  819. );
  820. } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) {
  821. /*
  822. * Delete all group shares that are owned by this user
  823. * Or special user group shares that are received by this user
  824. */
  825. $qb->where(
  826. $qb->expr()->andX(
  827. $qb->expr()->orX(
  828. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
  829. $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP))
  830. ),
  831. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid))
  832. )
  833. );
  834. $qb->orWhere(
  835. $qb->expr()->andX(
  836. $qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)),
  837. $qb->expr()->eq('share_with', $qb->createNamedParameter($uid))
  838. )
  839. );
  840. } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) {
  841. /*
  842. * Delete all link shares owned by this user.
  843. * And all link shares initiated by this user (until #22327 is in)
  844. */
  845. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK)));
  846. $qb->andWhere(
  847. $qb->expr()->orX(
  848. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)),
  849. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($uid))
  850. )
  851. );
  852. }
  853. $qb->execute();
  854. }
  855. /**
  856. * Delete all shares received by this group. As well as any custom group
  857. * shares for group members.
  858. *
  859. * @param string $gid
  860. */
  861. public function groupDeleted($gid) {
  862. /*
  863. * First delete all custom group shares for group members
  864. */
  865. $qb = $this->dbConn->getQueryBuilder();
  866. $qb->select('id')
  867. ->from('share')
  868. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  869. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
  870. $cursor = $qb->execute();
  871. $ids = [];
  872. while($row = $cursor->fetch()) {
  873. $ids[] = (int)$row['id'];
  874. }
  875. $cursor->closeCursor();
  876. if (!empty($ids)) {
  877. $chunks = array_chunk($ids, 100);
  878. foreach ($chunks as $chunk) {
  879. $qb->delete('share')
  880. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  881. ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
  882. $qb->execute();
  883. }
  884. }
  885. /*
  886. * Now delete all the group shares
  887. */
  888. $qb = $this->dbConn->getQueryBuilder();
  889. $qb->delete('share')
  890. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  891. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
  892. $qb->execute();
  893. }
  894. /**
  895. * Delete custom group shares to this group for this user
  896. *
  897. * @param string $uid
  898. * @param string $gid
  899. */
  900. public function userDeletedFromGroup($uid, $gid) {
  901. /*
  902. * Get all group shares
  903. */
  904. $qb = $this->dbConn->getQueryBuilder();
  905. $qb->select('id')
  906. ->from('share')
  907. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)))
  908. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($gid)));
  909. $cursor = $qb->execute();
  910. $ids = [];
  911. while($row = $cursor->fetch()) {
  912. $ids[] = (int)$row['id'];
  913. }
  914. $cursor->closeCursor();
  915. if (!empty($ids)) {
  916. $chunks = array_chunk($ids, 100);
  917. foreach ($chunks as $chunk) {
  918. /*
  919. * Delete all special shares wit this users for the found group shares
  920. */
  921. $qb->delete('share')
  922. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)))
  923. ->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($uid)))
  924. ->andWhere($qb->expr()->in('parent', $qb->createNamedParameter($chunk, IQueryBuilder::PARAM_INT_ARRAY)));
  925. $qb->execute();
  926. }
  927. }
  928. }
  929. /**
  930. * @inheritdoc
  931. */
  932. public function getAccessList($nodes, $currentAccess) {
  933. $ids = [];
  934. foreach ($nodes as $node) {
  935. $ids[] = $node->getId();
  936. }
  937. $qb = $this->dbConn->getQueryBuilder();
  938. $or = $qb->expr()->orX(
  939. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_USER)),
  940. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_GROUP)),
  941. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_LINK))
  942. );
  943. if ($currentAccess) {
  944. $or->add($qb->expr()->eq('share_type', $qb->createNamedParameter(self::SHARE_TYPE_USERGROUP)));
  945. }
  946. $qb->select('id', 'parent', 'share_type', 'share_with', 'file_source', 'file_target', 'permissions')
  947. ->from('share')
  948. ->where(
  949. $or
  950. )
  951. ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  952. ->andWhere($qb->expr()->orX(
  953. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  954. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  955. ));
  956. $cursor = $qb->execute();
  957. $users = [];
  958. $link = false;
  959. while($row = $cursor->fetch()) {
  960. $type = (int)$row['share_type'];
  961. if ($type === \OCP\Share::SHARE_TYPE_USER) {
  962. $uid = $row['share_with'];
  963. $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
  964. $users[$uid][$row['id']] = $row;
  965. } else if ($type === \OCP\Share::SHARE_TYPE_GROUP) {
  966. $gid = $row['share_with'];
  967. $group = $this->groupManager->get($gid);
  968. if ($group === null) {
  969. continue;
  970. }
  971. $userList = $group->getUsers();
  972. foreach ($userList as $user) {
  973. $uid = $user->getUID();
  974. $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
  975. $users[$uid][$row['id']] = $row;
  976. }
  977. } else if ($type === \OCP\Share::SHARE_TYPE_LINK) {
  978. $link = true;
  979. } else if ($type === self::SHARE_TYPE_USERGROUP && $currentAccess === true) {
  980. $uid = $row['share_with'];
  981. $users[$uid] = isset($users[$uid]) ? $users[$uid] : [];
  982. $users[$uid][$row['id']] = $row;
  983. }
  984. }
  985. $cursor->closeCursor();
  986. if ($currentAccess === true) {
  987. $users = array_map([$this, 'filterSharesOfUser'], $users);
  988. $users = array_filter($users);
  989. } else {
  990. $users = array_keys($users);
  991. }
  992. return ['users' => $users, 'public' => $link];
  993. }
  994. /**
  995. * For each user the path with the fewest slashes is returned
  996. * @param array $shares
  997. * @return array
  998. */
  999. protected function filterSharesOfUser(array $shares) {
  1000. // Group shares when the user has a share exception
  1001. foreach ($shares as $id => $share) {
  1002. $type = (int) $share['share_type'];
  1003. $permissions = (int) $share['permissions'];
  1004. if ($type === self::SHARE_TYPE_USERGROUP) {
  1005. unset($shares[$share['parent']]);
  1006. if ($permissions === 0) {
  1007. unset($shares[$id]);
  1008. }
  1009. }
  1010. }
  1011. $best = [];
  1012. $bestDepth = 0;
  1013. foreach ($shares as $id => $share) {
  1014. $depth = substr_count($share['file_target'], '/');
  1015. if (empty($best) || $depth < $bestDepth) {
  1016. $bestDepth = $depth;
  1017. $best = [
  1018. 'node_id' => $share['file_source'],
  1019. 'node_path' => $share['file_target'],
  1020. ];
  1021. }
  1022. }
  1023. return $best;
  1024. }
  1025. }