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.

178 lines
5.3 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Pauli Järvinen <pauli.jarvinen@gmail.com>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Share20;
  28. use OCP\Files\File;
  29. use OCP\Share;
  30. use OCP\Share\IShare;
  31. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32. use Symfony\Component\EventDispatcher\GenericEvent;
  33. class LegacyHooks {
  34. /** @var EventDispatcherInterface */
  35. private $eventDispatcher;
  36. /**
  37. * LegacyHooks constructor.
  38. *
  39. * @param EventDispatcherInterface $eventDispatcher
  40. */
  41. public function __construct(EventDispatcherInterface $eventDispatcher) {
  42. $this->eventDispatcher = $eventDispatcher;
  43. $this->eventDispatcher->addListener('OCP\Share::preUnshare', [$this, 'preUnshare']);
  44. $this->eventDispatcher->addListener('OCP\Share::postUnshare', [$this, 'postUnshare']);
  45. $this->eventDispatcher->addListener('OCP\Share::postUnshareFromSelf', [$this, 'postUnshareFromSelf']);
  46. $this->eventDispatcher->addListener('OCP\Share::preShare', [$this, 'preShare']);
  47. $this->eventDispatcher->addListener('OCP\Share::postShare', [$this, 'postShare']);
  48. }
  49. /**
  50. * @param GenericEvent $e
  51. */
  52. public function preUnshare(GenericEvent $e) {
  53. /** @var IShare $share */
  54. $share = $e->getSubject();
  55. $formatted = $this->formatHookParams($share);
  56. \OC_Hook::emit(Share::class, 'pre_unshare', $formatted);
  57. }
  58. /**
  59. * @param GenericEvent $e
  60. */
  61. public function postUnshare(GenericEvent $e) {
  62. /** @var IShare $share */
  63. $share = $e->getSubject();
  64. $formatted = $this->formatHookParams($share);
  65. /** @var IShare[] $deletedShares */
  66. $deletedShares = $e->getArgument('deletedShares');
  67. $formattedDeletedShares = array_map(function ($share) {
  68. return $this->formatHookParams($share);
  69. }, $deletedShares);
  70. $formatted['deletedShares'] = $formattedDeletedShares;
  71. \OC_Hook::emit(Share::class, 'post_unshare', $formatted);
  72. }
  73. /**
  74. * @param GenericEvent $e
  75. */
  76. public function postUnshareFromSelf(GenericEvent $e) {
  77. /** @var IShare $share */
  78. $share = $e->getSubject();
  79. $formatted = $this->formatHookParams($share);
  80. $formatted['itemTarget'] = $formatted['fileTarget'];
  81. $formatted['unsharedItems'] = [$formatted];
  82. \OC_Hook::emit(Share::class, 'post_unshareFromSelf', $formatted);
  83. }
  84. private function formatHookParams(IShare $share) {
  85. // Prepare hook
  86. $shareType = $share->getShareType();
  87. $sharedWith = '';
  88. if ($shareType === IShare::TYPE_USER ||
  89. $shareType === IShare::TYPE_GROUP ||
  90. $shareType === IShare::TYPE_REMOTE) {
  91. $sharedWith = $share->getSharedWith();
  92. }
  93. $hookParams = [
  94. 'id' => $share->getId(),
  95. 'itemType' => $share->getNodeType(),
  96. 'itemSource' => $share->getNodeId(),
  97. 'shareType' => $shareType,
  98. 'shareWith' => $sharedWith,
  99. 'itemparent' => method_exists($share, 'getParent') ? $share->getParent() : '',
  100. 'uidOwner' => $share->getSharedBy(),
  101. 'fileSource' => $share->getNodeId(),
  102. 'fileTarget' => $share->getTarget()
  103. ];
  104. return $hookParams;
  105. }
  106. public function preShare(GenericEvent $e) {
  107. /** @var IShare $share */
  108. $share = $e->getSubject();
  109. // Pre share hook
  110. $run = true;
  111. $error = '';
  112. $preHookData = [
  113. 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
  114. 'itemSource' => $share->getNode()->getId(),
  115. 'shareType' => $share->getShareType(),
  116. 'uidOwner' => $share->getSharedBy(),
  117. 'permissions' => $share->getPermissions(),
  118. 'fileSource' => $share->getNode()->getId(),
  119. 'expiration' => $share->getExpirationDate(),
  120. 'token' => $share->getToken(),
  121. 'itemTarget' => $share->getTarget(),
  122. 'shareWith' => $share->getSharedWith(),
  123. 'run' => &$run,
  124. 'error' => &$error,
  125. ];
  126. \OC_Hook::emit(Share::class, 'pre_shared', $preHookData);
  127. if ($run === false) {
  128. $e->setArgument('error', $error);
  129. $e->stopPropagation();
  130. }
  131. return $e;
  132. }
  133. public function postShare(GenericEvent $e) {
  134. /** @var IShare $share */
  135. $share = $e->getSubject();
  136. $postHookData = [
  137. 'itemType' => $share->getNode() instanceof File ? 'file' : 'folder',
  138. 'itemSource' => $share->getNode()->getId(),
  139. 'shareType' => $share->getShareType(),
  140. 'uidOwner' => $share->getSharedBy(),
  141. 'permissions' => $share->getPermissions(),
  142. 'fileSource' => $share->getNode()->getId(),
  143. 'expiration' => $share->getExpirationDate(),
  144. 'token' => $share->getToken(),
  145. 'id' => $share->getId(),
  146. 'shareWith' => $share->getSharedWith(),
  147. 'itemTarget' => $share->getTarget(),
  148. 'fileTarget' => $share->getTarget(),
  149. ];
  150. \OC_Hook::emit(Share::class, 'post_shared', $postHookData);
  151. }
  152. }