You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ShareAPIController.php 38KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Maxence Lange <maxence@nextcloud.com>
  10. * @author Michael Jobst <mjobst+github@tecratech.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 OCA\Files_Sharing\Controller;
  31. use OCA\Files\Helper;
  32. use OCP\App\IAppManager;
  33. use OCP\AppFramework\Http\DataResponse;
  34. use OCP\AppFramework\OCS\OCSBadRequestException;
  35. use OCP\AppFramework\OCS\OCSException;
  36. use OCP\AppFramework\OCS\OCSForbiddenException;
  37. use OCP\AppFramework\OCS\OCSNotFoundException;
  38. use OCP\AppFramework\OCSController;
  39. use OCP\AppFramework\QueryException;
  40. use OCP\Constants;
  41. use OCP\Files\Node;
  42. use OCP\Files\NotFoundException;
  43. use OCP\IConfig;
  44. use OCP\IGroupManager;
  45. use OCP\IL10N;
  46. use OCP\IUserManager;
  47. use OCP\IRequest;
  48. use OCP\IServerContainer;
  49. use OCP\IURLGenerator;
  50. use OCP\Files\IRootFolder;
  51. use OCP\Lock\LockedException;
  52. use OCP\Share;
  53. use OCP\Share\IManager;
  54. use OCP\Share\Exceptions\ShareNotFound;
  55. use OCP\Share\Exceptions\GenericShareException;
  56. use OCP\Lock\ILockingProvider;
  57. use OCP\Share\IShare;
  58. use OCA\Files_Sharing\External\Storage;
  59. /**
  60. * Class Share20OCS
  61. *
  62. * @package OCA\Files_Sharing\API
  63. */
  64. class ShareAPIController extends OCSController {
  65. /** @var IManager */
  66. private $shareManager;
  67. /** @var IGroupManager */
  68. private $groupManager;
  69. /** @var IUserManager */
  70. private $userManager;
  71. /** @var IRootFolder */
  72. private $rootFolder;
  73. /** @var IURLGenerator */
  74. private $urlGenerator;
  75. /** @var string */
  76. private $currentUser;
  77. /** @var IL10N */
  78. private $l;
  79. /** @var \OCP\Files\Node */
  80. private $lockedNode;
  81. /** @var IConfig */
  82. private $config;
  83. /** @var IAppManager */
  84. private $appManager;
  85. /** @var IServerContainer */
  86. private $serverContainer;
  87. /**
  88. * Share20OCS constructor.
  89. *
  90. * @param string $appName
  91. * @param IRequest $request
  92. * @param IManager $shareManager
  93. * @param IGroupManager $groupManager
  94. * @param IUserManager $userManager
  95. * @param IRootFolder $rootFolder
  96. * @param IURLGenerator $urlGenerator
  97. * @param string $userId
  98. * @param IL10N $l10n
  99. * @param IConfig $config
  100. * @param IAppManager $appManager
  101. * @param IServerContainer $serverContainer
  102. */
  103. public function __construct(
  104. string $appName,
  105. IRequest $request,
  106. IManager $shareManager,
  107. IGroupManager $groupManager,
  108. IUserManager $userManager,
  109. IRootFolder $rootFolder,
  110. IURLGenerator $urlGenerator,
  111. string $userId = null,
  112. IL10N $l10n,
  113. IConfig $config,
  114. IAppManager $appManager,
  115. IServerContainer $serverContainer
  116. ) {
  117. parent::__construct($appName, $request);
  118. $this->shareManager = $shareManager;
  119. $this->userManager = $userManager;
  120. $this->groupManager = $groupManager;
  121. $this->request = $request;
  122. $this->rootFolder = $rootFolder;
  123. $this->urlGenerator = $urlGenerator;
  124. $this->currentUser = $userId;
  125. $this->l = $l10n;
  126. $this->config = $config;
  127. $this->appManager = $appManager;
  128. $this->serverContainer = $serverContainer;
  129. }
  130. /**
  131. * Convert an IShare to an array for OCS output
  132. *
  133. * @param \OCP\Share\IShare $share
  134. * @param Node|null $recipientNode
  135. * @return array
  136. * @throws NotFoundException In case the node can't be resolved.
  137. *
  138. * @suppress PhanUndeclaredClassMethod
  139. */
  140. protected function formatShare(\OCP\Share\IShare $share, Node $recipientNode = null): array {
  141. $sharedBy = $this->userManager->get($share->getSharedBy());
  142. $shareOwner = $this->userManager->get($share->getShareOwner());
  143. $result = [
  144. 'id' => $share->getId(),
  145. 'share_type' => $share->getShareType(),
  146. 'uid_owner' => $share->getSharedBy(),
  147. 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(),
  148. 'permissions' => $share->getPermissions(),
  149. 'stime' => $share->getShareTime()->getTimestamp(),
  150. 'parent' => null,
  151. 'expiration' => null,
  152. 'token' => null,
  153. 'uid_file_owner' => $share->getShareOwner(),
  154. 'note' => $share->getNote(),
  155. 'label' => $share->getLabel(),
  156. 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(),
  157. ];
  158. $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
  159. if ($recipientNode) {
  160. $node = $recipientNode;
  161. } else {
  162. $nodes = $userFolder->getById($share->getNodeId());
  163. if (empty($nodes)) {
  164. // fallback to guessing the path
  165. $node = $userFolder->get($share->getTarget());
  166. if ($node === null || $share->getTarget() === '') {
  167. throw new NotFoundException();
  168. }
  169. } else {
  170. $node = $nodes[0];
  171. }
  172. }
  173. $result['path'] = $userFolder->getRelativePath($node->getPath());
  174. if ($node instanceOf \OCP\Files\Folder) {
  175. $result['item_type'] = 'folder';
  176. } else {
  177. $result['item_type'] = 'file';
  178. }
  179. $result['mimetype'] = $node->getMimetype();
  180. $result['storage_id'] = $node->getStorage()->getId();
  181. $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId();
  182. $result['item_source'] = $node->getId();
  183. $result['file_source'] = $node->getId();
  184. $result['file_parent'] = $node->getParent()->getId();
  185. $result['file_target'] = $share->getTarget();
  186. $expiration = $share->getExpirationDate();
  187. if ($expiration !== null) {
  188. $result['expiration'] = $expiration->format('Y-m-d 00:00:00');
  189. }
  190. if ($share->getShareType() === Share::SHARE_TYPE_USER) {
  191. $sharedWith = $this->userManager->get($share->getSharedWith());
  192. $result['share_with'] = $share->getSharedWith();
  193. $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
  194. } else if ($share->getShareType() === Share::SHARE_TYPE_GROUP) {
  195. $group = $this->groupManager->get($share->getSharedWith());
  196. $result['share_with'] = $share->getSharedWith();
  197. $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith();
  198. } else if ($share->getShareType() === Share::SHARE_TYPE_LINK) {
  199. // "share_with" and "share_with_displayname" for passwords of link
  200. // shares was deprecated in Nextcloud 15, use "password" instead.
  201. $result['share_with'] = $share->getPassword();
  202. $result['share_with_displayname'] = $share->getPassword();
  203. $result['password'] = $share->getPassword();
  204. $result['send_password_by_talk'] = $share->getSendPasswordByTalk();
  205. $result['token'] = $share->getToken();
  206. $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]);
  207. } else if ($share->getShareType() === Share::SHARE_TYPE_REMOTE || $share->getShareType() === Share::SHARE_TYPE_REMOTE_GROUP) {
  208. $result['share_with'] = $share->getSharedWith();
  209. $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD');
  210. $result['token'] = $share->getToken();
  211. } else if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) {
  212. $result['share_with'] = $share->getSharedWith();
  213. $result['password'] = $share->getPassword();
  214. $result['send_password_by_talk'] = $share->getSendPasswordByTalk();
  215. $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL');
  216. $result['token'] = $share->getToken();
  217. } else if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) {
  218. // getSharedWith() returns either "name (type, owner)" or
  219. // "name (type, owner) [id]", depending on the Circles app version.
  220. $hasCircleId = (substr($share->getSharedWith(), -1) === ']');
  221. $result['share_with_displayname'] = $share->getSharedWithDisplayName();
  222. if (empty($result['share_with_displayname'])) {
  223. $displayNameLength = ($hasCircleId? strrpos($share->getSharedWith(), ' '): strlen($share->getSharedWith()));
  224. $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength);
  225. }
  226. $result['share_with_avatar'] = $share->getSharedWithAvatar();
  227. $shareWithStart = ($hasCircleId? strrpos($share->getSharedWith(), '[') + 1: 0);
  228. $shareWithLength = ($hasCircleId? -1: strpos($share->getSharedWith(), ' '));
  229. if (is_bool($shareWithLength)) {
  230. $shareWithLength = -1;
  231. }
  232. $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
  233. } else if ($share->getShareType() === Share::SHARE_TYPE_ROOM) {
  234. $result['share_with'] = $share->getSharedWith();
  235. $result['share_with_displayname'] = '';
  236. try {
  237. $result = array_merge($result, $this->getRoomShareHelper()->formatShare($share));
  238. } catch (QueryException $e) {
  239. }
  240. }
  241. $result['mail_send'] = $share->getMailSend() ? 1 : 0;
  242. $result['hide_download'] = $share->getHideDownload() ? 1 : 0;
  243. return $result;
  244. }
  245. /**
  246. * Check if one of the users address books knows the exact property, if
  247. * yes we return the full name.
  248. *
  249. * @param string $query
  250. * @param string $property
  251. * @return string
  252. */
  253. private function getDisplayNameFromAddressBook(string $query, string $property): string {
  254. // FIXME: If we inject the contacts manager it gets initialized bofore any address books are registered
  255. $result = \OC::$server->getContactsManager()->search($query, [$property]);
  256. foreach ($result as $r) {
  257. foreach($r[$property] as $value) {
  258. if ($value === $query) {
  259. return $r['FN'];
  260. }
  261. }
  262. }
  263. return $query;
  264. }
  265. /**
  266. * Get a specific share by id
  267. *
  268. * @NoAdminRequired
  269. *
  270. * @param string $id
  271. * @return DataResponse
  272. * @throws OCSNotFoundException
  273. */
  274. public function getShare(string $id): DataResponse {
  275. try {
  276. $share = $this->getShareById($id);
  277. } catch (ShareNotFound $e) {
  278. throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
  279. }
  280. if ($this->canAccessShare($share)) {
  281. try {
  282. $share = $this->formatShare($share);
  283. return new DataResponse([$share]);
  284. } catch (NotFoundException $e) {
  285. //Fall trough
  286. }
  287. }
  288. throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
  289. }
  290. /**
  291. * Delete a share
  292. *
  293. * @NoAdminRequired
  294. *
  295. * @param string $id
  296. * @return DataResponse
  297. * @throws OCSNotFoundException
  298. */
  299. public function deleteShare(string $id): DataResponse {
  300. try {
  301. $share = $this->getShareById($id);
  302. } catch (ShareNotFound $e) {
  303. throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
  304. }
  305. try {
  306. $this->lock($share->getNode());
  307. } catch (LockedException $e) {
  308. throw new OCSNotFoundException($this->l->t('could not delete share'));
  309. }
  310. if (!$this->canAccessShare($share)) {
  311. throw new OCSNotFoundException($this->l->t('Could not delete share'));
  312. }
  313. if (($share->getShareType() === Share::SHARE_TYPE_GROUP ||
  314. $share->getShareType() === Share::SHARE_TYPE_ROOM) &&
  315. $share->getShareOwner() !== $this->currentUser &&
  316. $share->getSharedBy() !== $this->currentUser) {
  317. $this->shareManager->deleteFromSelf($share, $this->currentUser);
  318. } else {
  319. $this->shareManager->deleteShare($share);
  320. }
  321. return new DataResponse();
  322. }
  323. /**
  324. * @NoAdminRequired
  325. *
  326. * @param string $path
  327. * @param int $permissions
  328. * @param int $shareType
  329. * @param string $shareWith
  330. * @param string $publicUpload
  331. * @param string $password
  332. * @param string $sendPasswordByTalk
  333. * @param string $expireDate
  334. * @param string $label
  335. *
  336. * @return DataResponse
  337. * @throws NotFoundException
  338. * @throws OCSBadRequestException
  339. * @throws OCSException
  340. * @throws OCSForbiddenException
  341. * @throws OCSNotFoundException
  342. * @throws \OCP\Files\InvalidPathException
  343. * @suppress PhanUndeclaredClassMethod
  344. */
  345. public function createShare(
  346. string $path = null,
  347. int $permissions = null,
  348. int $shareType = -1,
  349. string $shareWith = null,
  350. string $publicUpload = 'false',
  351. string $password = '',
  352. string $sendPasswordByTalk = null,
  353. string $expireDate = '',
  354. string $label = ''
  355. ): DataResponse {
  356. $share = $this->shareManager->newShare();
  357. if ($permissions === null) {
  358. $permissions = $this->config->getAppValue('core', 'shareapi_default_permissions', Constants::PERMISSION_ALL);
  359. }
  360. // Verify path
  361. if ($path === null) {
  362. throw new OCSNotFoundException($this->l->t('Please specify a file or folder path'));
  363. }
  364. $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
  365. try {
  366. $path = $userFolder->get($path);
  367. } catch (NotFoundException $e) {
  368. throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
  369. }
  370. $share->setNode($path);
  371. try {
  372. $this->lock($share->getNode());
  373. } catch (LockedException $e) {
  374. throw new OCSNotFoundException($this->l->t('Could not create share'));
  375. }
  376. if ($permissions < 0 || $permissions > Constants::PERMISSION_ALL) {
  377. throw new OCSNotFoundException($this->l->t('invalid permissions'));
  378. }
  379. // Shares always require read permissions
  380. $permissions |= Constants::PERMISSION_READ;
  381. if ($path instanceof \OCP\Files\File) {
  382. // Single file shares should never have delete or create permissions
  383. $permissions &= ~Constants::PERMISSION_DELETE;
  384. $permissions &= ~Constants::PERMISSION_CREATE;
  385. }
  386. /*
  387. * Hack for https://github.com/owncloud/core/issues/22587
  388. * We check the permissions via webdav. But the permissions of the mount point
  389. * do not equal the share permissions. Here we fix that for federated mounts.
  390. */
  391. if ($path->getStorage()->instanceOfStorage(Storage::class)) {
  392. $permissions &= ~($permissions & ~$path->getPermissions());
  393. }
  394. if ($shareType === Share::SHARE_TYPE_USER) {
  395. // Valid user is required to share
  396. if ($shareWith === null || !$this->userManager->userExists($shareWith)) {
  397. throw new OCSNotFoundException($this->l->t('Please specify a valid user'));
  398. }
  399. $share->setSharedWith($shareWith);
  400. $share->setPermissions($permissions);
  401. } else if ($shareType === Share::SHARE_TYPE_GROUP) {
  402. if (!$this->shareManager->allowGroupSharing()) {
  403. throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator'));
  404. }
  405. // Valid group is required to share
  406. if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) {
  407. throw new OCSNotFoundException($this->l->t('Please specify a valid group'));
  408. }
  409. $share->setSharedWith($shareWith);
  410. $share->setPermissions($permissions);
  411. } else if ($shareType === Share::SHARE_TYPE_LINK) {
  412. //Can we even share links?
  413. if (!$this->shareManager->shareApiAllowLinks()) {
  414. throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator'));
  415. }
  416. if ($publicUpload === 'true') {
  417. // Check if public upload is allowed
  418. if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
  419. throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
  420. }
  421. // Public upload can only be set for folders
  422. if ($path instanceof \OCP\Files\File) {
  423. throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders'));
  424. }
  425. $share->setPermissions(
  426. Constants::PERMISSION_READ |
  427. Constants::PERMISSION_CREATE |
  428. Constants::PERMISSION_UPDATE |
  429. Constants::PERMISSION_DELETE
  430. );
  431. } else {
  432. $share->setPermissions(Constants::PERMISSION_READ);
  433. }
  434. // Set password
  435. if ($password !== '') {
  436. $share->setPassword($password);
  437. }
  438. if (!empty($label)) {
  439. $share->setLabel($label);
  440. }
  441. if ($sendPasswordByTalk === 'true') {
  442. if (!$this->appManager->isEnabledForUser('spreed')) {
  443. throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$path->getPath()]));
  444. }
  445. $share->setSendPasswordByTalk(true);
  446. }
  447. //Expire date
  448. if ($expireDate !== '') {
  449. try {
  450. $expireDate = $this->parseDate($expireDate);
  451. $share->setExpirationDate($expireDate);
  452. } catch (\Exception $e) {
  453. throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD'));
  454. }
  455. }
  456. } else if ($shareType === Share::SHARE_TYPE_REMOTE) {
  457. if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
  458. throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$path->getPath(), $shareType]));
  459. }
  460. $share->setSharedWith($shareWith);
  461. $share->setPermissions($permissions);
  462. } else if ($shareType === Share::SHARE_TYPE_REMOTE_GROUP) {
  463. if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) {
  464. throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$path->getPath(), $shareType]));
  465. }
  466. $share->setSharedWith($shareWith);
  467. $share->setPermissions($permissions);
  468. } else if ($shareType === Share::SHARE_TYPE_EMAIL) {
  469. if ($share->getNodeType() === 'file') {
  470. $share->setPermissions(Constants::PERMISSION_READ);
  471. } else {
  472. $share->setPermissions($permissions);
  473. }
  474. $share->setSharedWith($shareWith);
  475. if ($sendPasswordByTalk === 'true') {
  476. if (!$this->appManager->isEnabledForUser('spreed')) {
  477. throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$path->getPath()]));
  478. }
  479. $share->setSendPasswordByTalk(true);
  480. }
  481. } else if ($shareType === Share::SHARE_TYPE_CIRCLE) {
  482. if (!\OC::$server->getAppManager()->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) {
  483. throw new OCSNotFoundException($this->l->t('You cannot share to a Circle if the app is not enabled'));
  484. }
  485. $circle = \OCA\Circles\Api\v1\Circles::detailsCircle($shareWith);
  486. // Valid circle is required to share
  487. if ($circle === null) {
  488. throw new OCSNotFoundException($this->l->t('Please specify a valid circle'));
  489. }
  490. $share->setSharedWith($shareWith);
  491. $share->setPermissions($permissions);
  492. } else if ($shareType === Share::SHARE_TYPE_ROOM) {
  493. try {
  494. $this->getRoomShareHelper()->createShare($share, $shareWith, $permissions, $expireDate);
  495. } catch (QueryException $e) {
  496. throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$path->getPath()]));
  497. }
  498. } else {
  499. throw new OCSBadRequestException($this->l->t('Unknown share type'));
  500. }
  501. $share->setShareType($shareType);
  502. $share->setSharedBy($this->currentUser);
  503. try {
  504. $share = $this->shareManager->createShare($share);
  505. } catch (GenericShareException $e) {
  506. $code = $e->getCode() === 0 ? 403 : $e->getCode();
  507. throw new OCSException($e->getHint(), $code);
  508. } catch (\Exception $e) {
  509. throw new OCSForbiddenException($e->getMessage(), $e);
  510. }
  511. $output = $this->formatShare($share);
  512. return new DataResponse($output);
  513. }
  514. /**
  515. * @param \OCP\Files\File|\OCP\Files\Folder $node
  516. * @param boolean $includeTags
  517. * @return DataResponse
  518. */
  519. private function getSharedWithMe($node = null, bool $includeTags): DataResponse {
  520. $userShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_USER, $node, -1, 0);
  521. $groupShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_GROUP, $node, -1, 0);
  522. $circleShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_CIRCLE, $node, -1, 0);
  523. $roomShares = $this->shareManager->getSharedWith($this->currentUser, Share::SHARE_TYPE_ROOM, $node, -1, 0);
  524. $shares = array_merge($userShares, $groupShares, $circleShares, $roomShares);
  525. $shares = array_filter($shares, function (IShare $share) {
  526. return $share->getShareOwner() !== $this->currentUser;
  527. });
  528. $formatted = [];
  529. foreach ($shares as $share) {
  530. if ($this->canAccessShare($share)) {
  531. try {
  532. $formatted[] = $this->formatShare($share);
  533. } catch (NotFoundException $e) {
  534. // Ignore this share
  535. }
  536. }
  537. }
  538. if ($includeTags) {
  539. $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
  540. }
  541. return new DataResponse($formatted);
  542. }
  543. /**
  544. * @param \OCP\Files\Folder $folder
  545. * @return DataResponse
  546. * @throws OCSBadRequestException
  547. */
  548. private function getSharesInDir(Node $folder): DataResponse {
  549. if (!($folder instanceof \OCP\Files\Folder)) {
  550. throw new OCSBadRequestException($this->l->t('Not a directory'));
  551. }
  552. $nodes = $folder->getDirectoryListing();
  553. /** @var \OCP\Share\IShare[] $shares */
  554. $shares = [];
  555. foreach ($nodes as $node) {
  556. $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $node, true, -1, 0));
  557. $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $node, true, -1, 0));
  558. $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $node, true, -1, 0));
  559. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
  560. $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $node, true, -1, 0));
  561. }
  562. if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
  563. $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $node, true, -1, 0));
  564. }
  565. $shares = array_merge($shares, $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $node, true, -1, 0));
  566. }
  567. $formatted = $miniFormatted = [];
  568. $resharingRight = false;
  569. $known = [];
  570. foreach ($shares as $share) {
  571. if (in_array($share->getId(), $known) || $share->getSharedWith() === $this->currentUser) {
  572. continue;
  573. }
  574. try {
  575. $format = $this->formatShare($share);
  576. $known[] = $share->getId();
  577. $formatted[] = $format;
  578. if ($share->getSharedBy() === $this->currentUser) {
  579. $miniFormatted[] = $format;
  580. }
  581. if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $folder)) {
  582. $resharingRight = true;
  583. }
  584. } catch (\Exception $e) {
  585. //Ignore this share
  586. }
  587. }
  588. if (!$resharingRight) {
  589. $formatted = $miniFormatted;
  590. }
  591. return new DataResponse($formatted);
  592. }
  593. /**
  594. * The getShares function.
  595. *
  596. * @NoAdminRequired
  597. *
  598. * @param string $shared_with_me
  599. * @param string $reshares
  600. * @param string $subfiles
  601. * @param string $path
  602. *
  603. * - Get shares by the current user
  604. * - Get shares by the current user and reshares (?reshares=true)
  605. * - Get shares with the current user (?shared_with_me=true)
  606. * - Get shares for a specific path (?path=...)
  607. * - Get all shares in a folder (?subfiles=true&path=..)
  608. *
  609. * @return DataResponse
  610. * @throws OCSNotFoundException
  611. */
  612. public function getShares(
  613. string $shared_with_me = 'false',
  614. string $reshares = 'false',
  615. string $subfiles = 'false',
  616. string $path = null,
  617. string $include_tags = 'false'
  618. ): DataResponse {
  619. if ($path !== null) {
  620. $userFolder = $this->rootFolder->getUserFolder($this->currentUser);
  621. try {
  622. $path = $userFolder->get($path);
  623. $this->lock($path);
  624. } catch (\OCP\Files\NotFoundException $e) {
  625. throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist'));
  626. } catch (LockedException $e) {
  627. throw new OCSNotFoundException($this->l->t('Could not lock path'));
  628. }
  629. }
  630. $include_tags = $include_tags === 'true';
  631. if ($shared_with_me === 'true') {
  632. $result = $this->getSharedWithMe($path, $include_tags);
  633. return $result;
  634. }
  635. if ($subfiles === 'true') {
  636. $result = $this->getSharesInDir($path);
  637. return $result;
  638. }
  639. if ($reshares === 'true') {
  640. $reshares = true;
  641. } else {
  642. $reshares = false;
  643. }
  644. // Get all shares
  645. $userShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_USER, $path, $reshares, -1, 0);
  646. $groupShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_GROUP, $path, $reshares, -1, 0);
  647. $linkShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_LINK, $path, $reshares, -1, 0);
  648. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
  649. $mailShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_EMAIL, $path, $reshares, -1, 0);
  650. } else {
  651. $mailShares = [];
  652. }
  653. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) {
  654. $circleShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_CIRCLE, $path, $reshares, -1, 0);
  655. } else {
  656. $circleShares = [];
  657. }
  658. $roomShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_ROOM, $path, $reshares, -1, 0);
  659. $shares = array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares);
  660. if ($this->shareManager->outgoingServer2ServerSharesAllowed()) {
  661. $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE, $path, $reshares, -1, 0);
  662. $shares = array_merge($shares, $federatedShares);
  663. }
  664. if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) {
  665. $federatedShares = $this->shareManager->getSharesBy($this->currentUser, Share::SHARE_TYPE_REMOTE_GROUP, $path, $reshares, -1, 0);
  666. $shares = array_merge($shares, $federatedShares);
  667. }
  668. $formatted = $miniFormatted = [];
  669. $resharingRight = false;
  670. foreach ($shares as $share) {
  671. /** @var IShare $share */
  672. try {
  673. $format = $this->formatShare($share, $path);
  674. $formatted[] = $format;
  675. if ($share->getSharedBy() === $this->currentUser) {
  676. $miniFormatted[] = $format;
  677. }
  678. if (!$resharingRight && $this->shareProviderResharingRights($this->currentUser, $share, $path)) {
  679. $resharingRight = true;
  680. }
  681. } catch (\Exception $e) {
  682. //Ignore share
  683. }
  684. }
  685. if (!$resharingRight) {
  686. $formatted = $miniFormatted;
  687. }
  688. if ($include_tags) {
  689. $formatted = Helper::populateTags($formatted, 'file_source', \OC::$server->getTagManager());
  690. }
  691. return new DataResponse($formatted);
  692. }
  693. /**
  694. * @NoAdminRequired
  695. *
  696. * @param string $id
  697. * @param int $permissions
  698. * @param string $password
  699. * @param string $sendPasswordByTalk
  700. * @param string $publicUpload
  701. * @param string $expireDate
  702. * @param string $note
  703. * @param string $label
  704. * @param string $hideDownload
  705. * @return DataResponse
  706. * @throws LockedException
  707. * @throws NotFoundException
  708. * @throws OCSBadRequestException
  709. * @throws OCSForbiddenException
  710. * @throws OCSNotFoundException
  711. */
  712. public function updateShare(
  713. string $id,
  714. int $permissions = null,
  715. string $password = null,
  716. string $sendPasswordByTalk = null,
  717. string $publicUpload = null,
  718. string $expireDate = null,
  719. string $note = null,
  720. string $label = null,
  721. string $hideDownload = null
  722. ): DataResponse {
  723. try {
  724. $share = $this->getShareById($id);
  725. } catch (ShareNotFound $e) {
  726. throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
  727. }
  728. $this->lock($share->getNode());
  729. if (!$this->canAccessShare($share, false)) {
  730. throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist'));
  731. }
  732. if ($share->getShareOwner() !== $this->currentUser && $share->getSharedBy() !== $this->currentUser) {
  733. throw new OCSForbiddenException('You are not allowed to edit incoming shares');
  734. }
  735. if ($permissions === null &&
  736. $password === null &&
  737. $sendPasswordByTalk === null &&
  738. $publicUpload === null &&
  739. $expireDate === null &&
  740. $note === null &&
  741. $label === null &&
  742. $hideDownload === null
  743. ) {
  744. throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
  745. }
  746. if($note !== null) {
  747. $share->setNote($note);
  748. }
  749. /*
  750. * expirationdate, password and publicUpload only make sense for link shares
  751. */
  752. if ($share->getShareType() === Share::SHARE_TYPE_LINK) {
  753. // Update hide download state
  754. if ($hideDownload === 'true') {
  755. $share->setHideDownload(true);
  756. } else if ($hideDownload === 'false') {
  757. $share->setHideDownload(false);
  758. }
  759. $newPermissions = null;
  760. if ($publicUpload === 'true') {
  761. $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE;
  762. } else if ($publicUpload === 'false') {
  763. $newPermissions = Constants::PERMISSION_READ;
  764. }
  765. if ($permissions !== null) {
  766. $newPermissions = (int)$permissions;
  767. $newPermissions = $newPermissions & ~Constants::PERMISSION_SHARE;
  768. }
  769. if ($newPermissions !== null &&
  770. !in_array($newPermissions, [
  771. Constants::PERMISSION_READ,
  772. Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE, // legacy
  773. Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE, // correct
  774. Constants::PERMISSION_CREATE, // hidden file list
  775. Constants::PERMISSION_READ | Constants::PERMISSION_UPDATE, // allow to edit single files
  776. ], true)
  777. ) {
  778. throw new OCSBadRequestException($this->l->t('Can\'t change permissions for public share links'));
  779. }
  780. if (
  781. // legacy
  782. $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE) ||
  783. // correct
  784. $newPermissions === (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE)
  785. ) {
  786. if (!$this->shareManager->shareApiLinkAllowPublicUpload()) {
  787. throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator'));
  788. }
  789. if (!($share->getNode() instanceof \OCP\Files\Folder)) {
  790. throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders'));
  791. }
  792. // normalize to correct public upload permissions
  793. $newPermissions = Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE;
  794. }
  795. if ($newPermissions !== null) {
  796. $share->setPermissions($newPermissions);
  797. $permissions = $newPermissions;
  798. }
  799. if ($expireDate === '') {
  800. $share->setExpirationDate(null);
  801. } else if ($expireDate !== null) {
  802. try {
  803. $expireDate = $this->parseDate($expireDate);
  804. } catch (\Exception $e) {
  805. throw new OCSBadRequestException($e->getMessage(), $e);
  806. }
  807. $share->setExpirationDate($expireDate);
  808. }
  809. if ($password === '') {
  810. $share->setPassword(null);
  811. } else if ($password !== null) {
  812. $share->setPassword($password);
  813. }
  814. if ($label !== null) {
  815. $share->setLabel($label);
  816. }
  817. if ($sendPasswordByTalk === 'true') {
  818. if (!$this->appManager->isEnabledForUser('spreed')) {
  819. throw new OCSForbiddenException($this->l->t('Sharing sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled'));
  820. }
  821. $share->setSendPasswordByTalk(true);
  822. } else if ($sendPasswordByTalk !== null) {
  823. $share->setSendPasswordByTalk(false);
  824. }
  825. } else {
  826. if ($permissions !== null) {
  827. $permissions = (int)$permissions;
  828. $share->setPermissions($permissions);
  829. }
  830. if ($share->getShareType() === Share::SHARE_TYPE_EMAIL) {
  831. if ($password === '') {
  832. $share->setPassword(null);
  833. } else if ($password !== null) {
  834. $share->setPassword($password);
  835. }
  836. if ($sendPasswordByTalk === 'true') {
  837. if (!$this->appManager->isEnabledForUser('spreed')) {
  838. throw new OCSForbiddenException($this->l->t('Sharing sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled'));
  839. }
  840. $share->setSendPasswordByTalk(true);
  841. } else {
  842. $share->setSendPasswordByTalk(false);
  843. }
  844. }
  845. if ($expireDate === '') {
  846. $share->setExpirationDate(null);
  847. } else if ($expireDate !== null) {
  848. try {
  849. $expireDate = $this->parseDate($expireDate);
  850. } catch (\Exception $e) {
  851. throw new OCSBadRequestException($e->getMessage(), $e);
  852. }
  853. $share->setExpirationDate($expireDate);
  854. }
  855. }
  856. try {
  857. $share = $this->shareManager->updateShare($share);
  858. } catch (GenericShareException $e) {
  859. $code = $e->getCode() === 0 ? 403 : $e->getCode();
  860. throw new OCSException($e->getHint(), $code);
  861. } catch (\Exception $e) {
  862. throw new OCSBadRequestException($e->getMessage(), $e);
  863. }
  864. return new DataResponse($this->formatShare($share));
  865. }
  866. /**
  867. * @suppress PhanUndeclaredClassMethod
  868. */
  869. protected function canAccessShare(\OCP\Share\IShare $share, bool $checkGroups = true): bool {
  870. // A file with permissions 0 can't be accessed by us. So Don't show it
  871. if ($share->getPermissions() === 0) {
  872. return false;
  873. }
  874. // Owner of the file and the sharer of the file can always get share
  875. if ($share->getShareOwner() === $this->currentUser ||
  876. $share->getSharedBy() === $this->currentUser
  877. ) {
  878. return true;
  879. }
  880. // If the share is shared with you (or a group you are a member of)
  881. if ($share->getShareType() === Share::SHARE_TYPE_USER &&
  882. $share->getSharedWith() === $this->currentUser
  883. ) {
  884. return true;
  885. }
  886. if ($checkGroups && $share->getShareType() === Share::SHARE_TYPE_GROUP) {
  887. $sharedWith = $this->groupManager->get($share->getSharedWith());
  888. $user = $this->userManager->get($this->currentUser);
  889. if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) {
  890. return true;
  891. }
  892. }
  893. if ($share->getShareType() === Share::SHARE_TYPE_CIRCLE) {
  894. // TODO: have a sanity check like above?
  895. return true;
  896. }
  897. if ($share->getShareType() === Share::SHARE_TYPE_ROOM) {
  898. try {
  899. return $this->getRoomShareHelper()->canAccessShare($share, $this->currentUser);
  900. } catch (QueryException $e) {
  901. return false;
  902. }
  903. }
  904. return false;
  905. }
  906. /**
  907. * Make sure that the passed date is valid ISO 8601
  908. * So YYYY-MM-DD
  909. * If not throw an exception
  910. *
  911. * @param string $expireDate
  912. *
  913. * @throws \Exception
  914. * @return \DateTime
  915. */
  916. private function parseDate(string $expireDate): \DateTime {
  917. try {
  918. $date = new \DateTime($expireDate);
  919. } catch (\Exception $e) {
  920. throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
  921. }
  922. if ($date === false) {
  923. throw new \Exception('Invalid date. Format must be YYYY-MM-DD');
  924. }
  925. $date->setTime(0, 0, 0);
  926. return $date;
  927. }
  928. /**
  929. * Since we have multiple providers but the OCS Share API v1 does
  930. * not support this we need to check all backends.
  931. *
  932. * @param string $id
  933. * @return \OCP\Share\IShare
  934. * @throws ShareNotFound
  935. */
  936. private function getShareById(string $id): IShare {
  937. $share = null;
  938. // First check if it is an internal share.
  939. try {
  940. $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->currentUser);
  941. return $share;
  942. } catch (ShareNotFound $e) {
  943. // Do nothing, just try the other share type
  944. }
  945. try {
  946. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_CIRCLE)) {
  947. $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->currentUser);
  948. return $share;
  949. }
  950. } catch (ShareNotFound $e) {
  951. // Do nothing, just try the other share type
  952. }
  953. try {
  954. if ($this->shareManager->shareProviderExists(Share::SHARE_TYPE_EMAIL)) {
  955. $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->currentUser);
  956. return $share;
  957. }
  958. } catch (ShareNotFound $e) {
  959. // Do nothing, just try the other share type
  960. }
  961. try {
  962. $share = $this->shareManager->getShareById('ocRoomShare:' . $id, $this->currentUser);
  963. return $share;
  964. } catch (ShareNotFound $e) {
  965. // Do nothing, just try the other share type
  966. }
  967. if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) {
  968. throw new ShareNotFound();
  969. }
  970. $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->currentUser);
  971. return $share;
  972. }
  973. /**
  974. * Lock a Node
  975. *
  976. * @param \OCP\Files\Node $node
  977. * @throws LockedException
  978. */
  979. private function lock(\OCP\Files\Node $node) {
  980. $node->lock(ILockingProvider::LOCK_SHARED);
  981. $this->lockedNode = $node;
  982. }
  983. /**
  984. * Cleanup the remaining locks
  985. * @throws @LockedException
  986. */
  987. public function cleanup() {
  988. if ($this->lockedNode !== null) {
  989. $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED);
  990. }
  991. }
  992. /**
  993. * Returns the helper of ShareAPIController for room shares.
  994. *
  995. * If the Talk application is not enabled or the helper is not available
  996. * a QueryException is thrown instead.
  997. *
  998. * @return \OCA\Spreed\Share\Helper\ShareAPIController
  999. * @throws QueryException
  1000. */
  1001. private function getRoomShareHelper() {
  1002. if (!$this->appManager->isEnabledForUser('spreed')) {
  1003. throw new QueryException();
  1004. }
  1005. return $this->serverContainer->query('\OCA\Spreed\Share\Helper\ShareAPIController');
  1006. }
  1007. /**
  1008. * Returns if we can find resharing rights in an IShare object for a specific user.
  1009. *
  1010. * @suppress PhanUndeclaredClassMethod
  1011. *
  1012. * @param string $userId
  1013. * @param IShare $share
  1014. * @param Node $node
  1015. * @return bool
  1016. * @throws NotFoundException
  1017. * @throws \OCP\Files\InvalidPathException
  1018. */
  1019. private function shareProviderResharingRights(string $userId, IShare $share, $node): bool {
  1020. if ($share->getShareOwner() === $userId) {
  1021. return true;
  1022. }
  1023. // we check that current user have parent resharing rights on the current file
  1024. if ($node !== null && ($node->getPermissions() & \OCP\Constants::PERMISSION_SHARE) !== 0) {
  1025. return true;
  1026. }
  1027. if ((\OCP\Constants::PERMISSION_SHARE & $share->getPermissions()) === 0) {
  1028. return false;
  1029. }
  1030. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() === $userId) {
  1031. return true;
  1032. }
  1033. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP && $this->groupManager->isInGroup($userId, $share->getSharedWith())) {
  1034. return true;
  1035. }
  1036. if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE && \OC::$server->getAppManager()->isEnabledForUser('circles') &&
  1037. class_exists('\OCA\Circles\Api\v1\Circles')) {
  1038. $hasCircleId = (substr($share->getSharedWith(), -1) === ']');
  1039. $shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0);
  1040. $shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' '));
  1041. if (is_bool($shareWithLength)) {
  1042. $shareWithLength = -1;
  1043. }
  1044. $sharedWith = substr($share->getSharedWith(), $shareWithStart, $shareWithLength);
  1045. try {
  1046. $member = \OCA\Circles\Api\v1\Circles::getMember($sharedWith, $userId, 1);
  1047. if ($member->getLevel() >= 4) {
  1048. return true;
  1049. }
  1050. return false;
  1051. } catch (QueryException $e) {
  1052. return false;
  1053. }
  1054. }
  1055. return false;
  1056. }
  1057. }