ShareByMailProvider.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\ShareByMail;
  22. use OC\HintException;
  23. use OC\Share20\Exception\InvalidShare;
  24. use OCP\Activity\IManager;
  25. use OCP\Files\Folder;
  26. use OCP\Files\IRootFolder;
  27. use OCP\Files\Node;
  28. use OCP\IDBConnection;
  29. use OCP\IL10N;
  30. use OCP\ILogger;
  31. use OCP\IURLGenerator;
  32. use OCP\IUser;
  33. use OCP\IUserManager;
  34. use OCP\Mail\IMailer;
  35. use OCP\Security\ISecureRandom;
  36. use OC\Share20\Share;
  37. use OCP\Share\Exceptions\ShareNotFound;
  38. use OCP\Share\IShare;
  39. use OCP\Share\IShareProvider;
  40. use OCP\Template;
  41. /**
  42. * Class ShareByMail
  43. *
  44. * @package OCA\ShareByMail
  45. */
  46. class ShareByMailProvider implements IShareProvider {
  47. /** @var IDBConnection */
  48. private $dbConnection;
  49. /** @var ILogger */
  50. private $logger;
  51. /** @var ISecureRandom */
  52. private $secureRandom;
  53. /** @var IUserManager */
  54. private $userManager;
  55. /** @var IRootFolder */
  56. private $rootFolder;
  57. /** @var IL10N */
  58. private $l;
  59. /** @var IMailer */
  60. private $mailer;
  61. /** @var IURLGenerator */
  62. private $urlGenerator;
  63. /** @var IManager */
  64. private $activityManager;
  65. /**
  66. * Return the identifier of this provider.
  67. *
  68. * @return string Containing only [a-zA-Z0-9]
  69. */
  70. public function identifier() {
  71. return 'ocShareByMail';
  72. }
  73. /**
  74. * DefaultShareProvider constructor.
  75. *
  76. * @param IDBConnection $connection
  77. * @param ISecureRandom $secureRandom
  78. * @param IUserManager $userManager
  79. * @param IRootFolder $rootFolder
  80. * @param IL10N $l
  81. * @param ILogger $logger
  82. * @param IMailer $mailer
  83. * @param IURLGenerator $urlGenerator
  84. * @param IManager $activityManager
  85. */
  86. public function __construct(
  87. IDBConnection $connection,
  88. ISecureRandom $secureRandom,
  89. IUserManager $userManager,
  90. IRootFolder $rootFolder,
  91. IL10N $l,
  92. ILogger $logger,
  93. IMailer $mailer,
  94. IURLGenerator $urlGenerator,
  95. IManager $activityManager
  96. ) {
  97. $this->dbConnection = $connection;
  98. $this->secureRandom = $secureRandom;
  99. $this->userManager = $userManager;
  100. $this->rootFolder = $rootFolder;
  101. $this->l = $l;
  102. $this->logger = $logger;
  103. $this->mailer = $mailer;
  104. $this->urlGenerator = $urlGenerator;
  105. $this->activityManager = $activityManager;
  106. }
  107. /**
  108. * Share a path
  109. *
  110. * @param IShare $share
  111. * @return IShare The share object
  112. * @throws ShareNotFound
  113. * @throws \Exception
  114. */
  115. public function create(IShare $share) {
  116. $shareWith = $share->getSharedWith();
  117. /*
  118. * Check if file is not already shared with the remote user
  119. */
  120. $alreadyShared = $this->getSharedWith($shareWith, \OCP\Share::SHARE_TYPE_EMAIL, $share->getNode(), 1, 0);
  121. if (!empty($alreadyShared)) {
  122. $message = 'Sharing %s failed, this item is already shared with %s';
  123. $message_t = $this->l->t('Sharing %s failed, this item is already shared with %s', array($share->getNode()->getName(), $shareWith));
  124. $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
  125. throw new \Exception($message_t);
  126. }
  127. $shareId = $this->createMailShare($share);
  128. $this->createActivity($share);
  129. $data = $this->getRawShare($shareId);
  130. return $this->createShareObject($data);
  131. }
  132. /**
  133. * create activity if a file/folder was shared by mail
  134. *
  135. * @param IShare $share
  136. */
  137. protected function createActivity(IShare $share) {
  138. $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
  139. $this->publishActivity(
  140. Activity::SUBJECT_SHARED_EMAIL_SELF,
  141. [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
  142. $share->getSharedBy(),
  143. $share->getNode()->getId(),
  144. $userFolder->getRelativePath($share->getNode()->getPath())
  145. );
  146. if ($share->getShareOwner() !== $share->getSharedBy()) {
  147. $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
  148. $fileId = $share->getNode()->getId();
  149. $nodes = $ownerFolder->getById($fileId);
  150. $ownerPath = $nodes[0]->getPath();
  151. $this->publishActivity(
  152. Activity::SUBJECT_SHARED_EMAIL_BY,
  153. [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
  154. $share->getShareOwner(),
  155. $fileId,
  156. $ownerFolder->getRelativePath($ownerPath)
  157. );
  158. }
  159. }
  160. /**
  161. * publish activity if a file/folder was shared by mail
  162. *
  163. * @param $subject
  164. * @param $parameters
  165. * @param $affectedUser
  166. * @param $fileId
  167. * @param $filePath
  168. */
  169. protected function publishActivity($subject, $parameters, $affectedUser, $fileId, $filePath) {
  170. $event = $this->activityManager->generateEvent();
  171. $event->setApp('sharebymail')
  172. ->setType('shared')
  173. ->setSubject($subject, $parameters)
  174. ->setAffectedUser($affectedUser)
  175. ->setObject('files', $fileId, $filePath);
  176. $this->activityManager->publish($event);
  177. }
  178. /**
  179. * @param IShare $share
  180. * @return int
  181. * @throws \Exception
  182. */
  183. protected function createMailShare(IShare $share) {
  184. $share->setToken($this->generateToken());
  185. $shareId = $this->addShareToDB(
  186. $share->getNodeId(),
  187. $share->getNodeType(),
  188. $share->getSharedWith(),
  189. $share->getSharedBy(),
  190. $share->getShareOwner(),
  191. $share->getPermissions(),
  192. $share->getToken()
  193. );
  194. try {
  195. $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
  196. ['token' => $share->getToken()]);
  197. $this->sendMailNotification($share->getNode()->getName(),
  198. $link,
  199. $share->getShareOwner(),
  200. $share->getSharedBy(), $share->getSharedWith());
  201. } catch (HintException $hintException) {
  202. $this->logger->error('Failed to send share by mail: ' . $hintException->getMessage());
  203. $this->removeShareFromTable($shareId);
  204. throw $hintException;
  205. } catch (\Exception $e) {
  206. $this->logger->error('Failed to send share by mail: ' . $e->getMessage());
  207. $this->removeShareFromTable($shareId);
  208. throw new HintException('Failed to send share by mail',
  209. $this->l->t('Failed to send share by E-mail'));
  210. }
  211. return $shareId;
  212. }
  213. protected function sendMailNotification($filename, $link, $owner, $initiator, $shareWith) {
  214. $ownerUser = $this->userManager->get($owner);
  215. $initiatorUser = $this->userManager->get($initiator);
  216. $ownerDisplayName = ($ownerUser instanceof IUser) ? $ownerUser->getDisplayName() : $owner;
  217. $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
  218. if ($owner === $initiator) {
  219. $subject = (string)$this->l->t('%s shared »%s« with you', array($ownerDisplayName, $filename));
  220. } else {
  221. $subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($ownerDisplayName, $filename, $initiatorDisplayName));
  222. }
  223. $message = $this->mailer->createMessage();
  224. $htmlBody = $this->createMailBody('mail', $filename, $link, $ownerDisplayName, $initiatorDisplayName);
  225. $textBody = $this->createMailBody('altmail', $filename, $link, $ownerDisplayName, $initiatorDisplayName);
  226. $message->setTo([$shareWith]);
  227. $message->setSubject($subject);
  228. $message->setBody($textBody, 'text/plain');
  229. $message->setHtmlBody($htmlBody);
  230. $this->mailer->send($message);
  231. }
  232. /**
  233. * create mail body
  234. *
  235. * @param $filename
  236. * @param $link
  237. * @param $owner
  238. * @param $initiator
  239. * @return string plain text mail
  240. * @throws HintException
  241. */
  242. protected function createMailBody($template, $filename, $link, $owner, $initiator) {
  243. $mailBodyTemplate = new Template('sharebymail', $template, '');
  244. $mailBodyTemplate->assign ('filename', $filename);
  245. $mailBodyTemplate->assign ('link', $link);
  246. $mailBodyTemplate->assign ('owner', $owner);
  247. $mailBodyTemplate->assign ('initiator', $initiator);
  248. $mailBodyTemplate->assign ('onBehalfOf', $initiator !== $owner);
  249. $mailBody = $mailBodyTemplate->fetchPage();
  250. if (is_string($mailBody)) {
  251. return $mailBody;
  252. }
  253. throw new HintException('Failed to create the E-mail',
  254. $this->l->t('Failed to create the E-mail'));
  255. }
  256. /**
  257. * generate share token
  258. *
  259. * @return string
  260. */
  261. protected function generateToken() {
  262. $token = $this->secureRandom->generate(
  263. 15, ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_DIGITS);
  264. return $token;
  265. }
  266. /**
  267. * Get all children of this share
  268. *
  269. * @param IShare $parent
  270. * @return IShare[]
  271. */
  272. public function getChildren(IShare $parent) {
  273. $children = [];
  274. $qb = $this->dbConnection->getQueryBuilder();
  275. $qb->select('*')
  276. ->from('share')
  277. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  278. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
  279. ->orderBy('id');
  280. $cursor = $qb->execute();
  281. while($data = $cursor->fetch()) {
  282. $children[] = $this->createShareObject($data);
  283. }
  284. $cursor->closeCursor();
  285. return $children;
  286. }
  287. /**
  288. * add share to the database and return the ID
  289. *
  290. * @param int $itemSource
  291. * @param string $itemType
  292. * @param string $shareWith
  293. * @param string $sharedBy
  294. * @param string $uidOwner
  295. * @param int $permissions
  296. * @param string $token
  297. * @return int
  298. */
  299. protected function addShareToDB($itemSource, $itemType, $shareWith, $sharedBy, $uidOwner, $permissions, $token) {
  300. $qb = $this->dbConnection->getQueryBuilder();
  301. $qb->insert('share')
  302. ->setValue('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
  303. ->setValue('item_type', $qb->createNamedParameter($itemType))
  304. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  305. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  306. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  307. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  308. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  309. ->setValue('permissions', $qb->createNamedParameter($permissions))
  310. ->setValue('token', $qb->createNamedParameter($token))
  311. ->setValue('stime', $qb->createNamedParameter(time()));
  312. /*
  313. * Added to fix https://github.com/owncloud/core/issues/22215
  314. * Can be removed once we get rid of ajax/share.php
  315. */
  316. $qb->setValue('file_target', $qb->createNamedParameter(''));
  317. $qb->execute();
  318. $id = $qb->getLastInsertId();
  319. return (int)$id;
  320. }
  321. /**
  322. * Update a share
  323. *
  324. * @param IShare $share
  325. * @return IShare The share object
  326. */
  327. public function update(IShare $share) {
  328. /*
  329. * We allow updating the permissions of mail shares
  330. */
  331. $qb = $this->dbConnection->getQueryBuilder();
  332. $qb->update('share')
  333. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  334. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  335. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  336. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  337. ->execute();
  338. return $share;
  339. }
  340. /**
  341. * @inheritdoc
  342. */
  343. public function move(IShare $share, $recipient) {
  344. /**
  345. * nothing to do here, mail shares are only outgoing shares
  346. */
  347. return $share;
  348. }
  349. /**
  350. * Delete a share (owner unShares the file)
  351. *
  352. * @param IShare $share
  353. */
  354. public function delete(IShare $share) {
  355. $this->removeShareFromTable($share->getId());
  356. }
  357. /**
  358. * @inheritdoc
  359. */
  360. public function deleteFromSelf(IShare $share, $recipient) {
  361. // nothing to do here, mail shares are only outgoing shares
  362. return;
  363. }
  364. /**
  365. * @inheritdoc
  366. */
  367. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset) {
  368. $qb = $this->dbConnection->getQueryBuilder();
  369. $qb->select('*')
  370. ->from('share');
  371. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
  372. /**
  373. * Reshares for this user are shares where they are the owner.
  374. */
  375. if ($reshares === false) {
  376. //Special case for old shares created via the web UI
  377. $or1 = $qb->expr()->andX(
  378. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  379. $qb->expr()->isNull('uid_initiator')
  380. );
  381. $qb->andWhere(
  382. $qb->expr()->orX(
  383. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
  384. $or1
  385. )
  386. );
  387. } else {
  388. $qb->andWhere(
  389. $qb->expr()->orX(
  390. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  391. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  392. )
  393. );
  394. }
  395. if ($node !== null) {
  396. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  397. }
  398. if ($limit !== -1) {
  399. $qb->setMaxResults($limit);
  400. }
  401. $qb->setFirstResult($offset);
  402. $qb->orderBy('id');
  403. $cursor = $qb->execute();
  404. $shares = [];
  405. while($data = $cursor->fetch()) {
  406. $shares[] = $this->createShareObject($data);
  407. }
  408. $cursor->closeCursor();
  409. return $shares;
  410. }
  411. /**
  412. * @inheritdoc
  413. */
  414. public function getShareById($id, $recipientId = null) {
  415. $qb = $this->dbConnection->getQueryBuilder();
  416. $qb->select('*')
  417. ->from('share')
  418. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  419. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
  420. $cursor = $qb->execute();
  421. $data = $cursor->fetch();
  422. $cursor->closeCursor();
  423. if ($data === false) {
  424. throw new ShareNotFound();
  425. }
  426. try {
  427. $share = $this->createShareObject($data);
  428. } catch (InvalidShare $e) {
  429. throw new ShareNotFound();
  430. }
  431. return $share;
  432. }
  433. /**
  434. * Get shares for a given path
  435. *
  436. * @param \OCP\Files\Node $path
  437. * @return IShare[]
  438. */
  439. public function getSharesByPath(Node $path) {
  440. $qb = $this->dbConnection->getQueryBuilder();
  441. $cursor = $qb->select('*')
  442. ->from('share')
  443. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  444. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
  445. ->execute();
  446. $shares = [];
  447. while($data = $cursor->fetch()) {
  448. $shares[] = $this->createShareObject($data);
  449. }
  450. $cursor->closeCursor();
  451. return $shares;
  452. }
  453. /**
  454. * @inheritdoc
  455. */
  456. public function getSharedWith($userId, $shareType, $node, $limit, $offset) {
  457. /** @var IShare[] $shares */
  458. $shares = [];
  459. //Get shares directly with this user
  460. $qb = $this->dbConnection->getQueryBuilder();
  461. $qb->select('*')
  462. ->from('share');
  463. // Order by id
  464. $qb->orderBy('id');
  465. // Set limit and offset
  466. if ($limit !== -1) {
  467. $qb->setMaxResults($limit);
  468. }
  469. $qb->setFirstResult($offset);
  470. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)));
  471. $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
  472. // Filter by node if provided
  473. if ($node !== null) {
  474. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  475. }
  476. $cursor = $qb->execute();
  477. while($data = $cursor->fetch()) {
  478. $shares[] = $this->createShareObject($data);
  479. }
  480. $cursor->closeCursor();
  481. return $shares;
  482. }
  483. /**
  484. * Get a share by token
  485. *
  486. * @param string $token
  487. * @return IShare
  488. * @throws ShareNotFound
  489. */
  490. public function getShareByToken($token) {
  491. $qb = $this->dbConnection->getQueryBuilder();
  492. $cursor = $qb->select('*')
  493. ->from('share')
  494. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
  495. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  496. ->execute();
  497. $data = $cursor->fetch();
  498. if ($data === false) {
  499. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  500. }
  501. try {
  502. $share = $this->createShareObject($data);
  503. } catch (InvalidShare $e) {
  504. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  505. }
  506. return $share;
  507. }
  508. /**
  509. * remove share from table
  510. *
  511. * @param string $shareId
  512. */
  513. protected function removeShareFromTable($shareId) {
  514. $qb = $this->dbConnection->getQueryBuilder();
  515. $qb->delete('share')
  516. ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
  517. $qb->execute();
  518. }
  519. /**
  520. * Create a share object from an database row
  521. *
  522. * @param array $data
  523. * @return IShare
  524. * @throws InvalidShare
  525. * @throws ShareNotFound
  526. */
  527. protected function createShareObject($data) {
  528. $share = new Share($this->rootFolder, $this->userManager);
  529. $share->setId((int)$data['id'])
  530. ->setShareType((int)$data['share_type'])
  531. ->setPermissions((int)$data['permissions'])
  532. ->setTarget($data['file_target'])
  533. ->setMailSend((bool)$data['mail_send'])
  534. ->setToken($data['token']);
  535. $shareTime = new \DateTime();
  536. $shareTime->setTimestamp((int)$data['stime']);
  537. $share->setShareTime($shareTime);
  538. $share->setSharedWith($data['share_with']);
  539. if ($data['uid_initiator'] !== null) {
  540. $share->setShareOwner($data['uid_owner']);
  541. $share->setSharedBy($data['uid_initiator']);
  542. } else {
  543. //OLD SHARE
  544. $share->setSharedBy($data['uid_owner']);
  545. $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
  546. $owner = $path->getOwner();
  547. $share->setShareOwner($owner->getUID());
  548. }
  549. $share->setNodeId((int)$data['file_source']);
  550. $share->setNodeType($data['item_type']);
  551. $share->setProviderId($this->identifier());
  552. return $share;
  553. }
  554. /**
  555. * Get the node with file $id for $user
  556. *
  557. * @param string $userId
  558. * @param int $id
  559. * @return \OCP\Files\File|\OCP\Files\Folder
  560. * @throws InvalidShare
  561. */
  562. private function getNode($userId, $id) {
  563. try {
  564. $userFolder = $this->rootFolder->getUserFolder($userId);
  565. } catch (NotFoundException $e) {
  566. throw new InvalidShare();
  567. }
  568. $nodes = $userFolder->getById($id);
  569. if (empty($nodes)) {
  570. throw new InvalidShare();
  571. }
  572. return $nodes[0];
  573. }
  574. /**
  575. * A user is deleted from the system
  576. * So clean up the relevant shares.
  577. *
  578. * @param string $uid
  579. * @param int $shareType
  580. */
  581. public function userDeleted($uid, $shareType) {
  582. $qb = $this->dbConnection->getQueryBuilder();
  583. $qb->delete('share')
  584. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL)))
  585. ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
  586. ->execute();
  587. }
  588. /**
  589. * This provider does not support group shares
  590. *
  591. * @param string $gid
  592. */
  593. public function groupDeleted($gid) {
  594. return;
  595. }
  596. /**
  597. * This provider does not support group shares
  598. *
  599. * @param string $uid
  600. * @param string $gid
  601. */
  602. public function userDeletedFromGroup($uid, $gid) {
  603. return;
  604. }
  605. /**
  606. * get database row of a give share
  607. *
  608. * @param $id
  609. * @return array
  610. * @throws ShareNotFound
  611. */
  612. protected function getRawShare($id) {
  613. // Now fetch the inserted share and create a complete share object
  614. $qb = $this->dbConnection->getQueryBuilder();
  615. $qb->select('*')
  616. ->from('share')
  617. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  618. $cursor = $qb->execute();
  619. $data = $cursor->fetch();
  620. $cursor->closeCursor();
  621. if ($data === false) {
  622. throw new ShareNotFound;
  623. }
  624. return $data;
  625. }
  626. public function getSharesInFolder($userId, Folder $node, $reshares) {
  627. $qb = $this->dbConnection->getQueryBuilder();
  628. $qb->select('*')
  629. ->from('share', 's')
  630. ->andWhere($qb->expr()->orX(
  631. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  632. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  633. ))
  634. ->andWhere(
  635. $qb->expr()->eq('share_type', $qb->createNamedParameter(\OCP\Share::SHARE_TYPE_EMAIL))
  636. );
  637. /**
  638. * Reshares for this user are shares where they are the owner.
  639. */
  640. if ($reshares === false) {
  641. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  642. } else {
  643. $qb->andWhere(
  644. $qb->expr()->orX(
  645. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  646. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  647. )
  648. );
  649. }
  650. $qb->innerJoin('s', 'filecache' ,'f', 's.file_source = f.fileid');
  651. $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
  652. $qb->orderBy('id');
  653. $cursor = $qb->execute();
  654. $shares = [];
  655. while ($data = $cursor->fetch()) {
  656. $shares[$data['fileid']][] = $this->createShareObject($data);
  657. }
  658. $cursor->closeCursor();
  659. return $shares;
  660. }
  661. }