Share.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Share;
  8. use OCA\Files_Sharing\ShareBackend\File;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * This class provides the ability for apps to share their content between users.
  12. * Apps must create a backend class that implements OCP\Share_Backend and register it with this class.
  13. *
  14. * It provides the following hooks:
  15. * - post_shared
  16. */
  17. class Share extends Constants {
  18. /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask
  19. * Construct permissions for share() and setPermissions with Or (|) e.g.
  20. * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE
  21. *
  22. * Check if permission is granted with And (&) e.g. Check if delete is
  23. * granted: if ($permissions & PERMISSION_DELETE)
  24. *
  25. * Remove permissions with And (&) and Not (~) e.g. Remove the update
  26. * permission: $permissions &= ~PERMISSION_UPDATE
  27. *
  28. * Apps are required to handle permissions on their own, this class only
  29. * stores and manages the permissions of shares
  30. *
  31. * @see lib/public/Constants.php
  32. */
  33. /**
  34. * Register a sharing backend class that implements OCP\Share_Backend for an item type
  35. *
  36. * @param string $itemType Item type
  37. * @param string $class Backend class
  38. * @param string $collectionOf (optional) Depends on item type
  39. * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files
  40. * @return boolean true if backend is registered or false if error
  41. */
  42. public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
  43. if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') == 'yes') {
  44. if (!isset(self::$backendTypes[$itemType])) {
  45. self::$backendTypes[$itemType] = [
  46. 'class' => $class,
  47. 'collectionOf' => $collectionOf,
  48. 'supportedFileExtensions' => $supportedFileExtensions
  49. ];
  50. return true;
  51. }
  52. \OC::$server->get(LoggerInterface::class)->warning(
  53. 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class']
  54. .' is already registered for '.$itemType,
  55. ['app' => 'files_sharing']);
  56. }
  57. return false;
  58. }
  59. /**
  60. * Get the backend class for the specified item type
  61. *
  62. * @param string $itemType
  63. * @return \OCP\Share_Backend
  64. * @throws \Exception
  65. */
  66. public static function getBackend($itemType) {
  67. $l = \OCP\Util::getL10N('lib');
  68. $logger = \OCP\Server::get(LoggerInterface::class);
  69. if (isset(self::$backends[$itemType])) {
  70. return self::$backends[$itemType];
  71. } elseif (isset(self::$backendTypes[$itemType]['class'])) {
  72. $class = self::$backendTypes[$itemType]['class'];
  73. if (class_exists($class)) {
  74. self::$backends[$itemType] = new $class;
  75. if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) {
  76. $message = 'Sharing backend %s must implement the interface OCP\Share_Backend';
  77. $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', [$class]);
  78. $logger->error(sprintf($message, $class), ['app' => 'OCP\Share']);
  79. throw new \Exception($message_t);
  80. }
  81. return self::$backends[$itemType];
  82. } else {
  83. $message = 'Sharing backend %s not found';
  84. $message_t = $l->t('Sharing backend %s not found', [$class]);
  85. $logger->error(sprintf($message, $class), ['app' => 'OCP\Share']);
  86. throw new \Exception($message_t);
  87. }
  88. }
  89. $message = 'Sharing backend for %s not found';
  90. $message_t = $l->t('Sharing backend for %s not found', [$itemType]);
  91. $logger->error(sprintf($message, $itemType), ['app' => 'OCP\Share']);
  92. throw new \Exception($message_t);
  93. }
  94. /**
  95. * Check if resharing is allowed
  96. *
  97. * @return boolean true if allowed or false
  98. *
  99. * Resharing is allowed by default if not configured
  100. */
  101. public static function isResharingAllowed() {
  102. if (!isset(self::$isResharingAllowed)) {
  103. if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') {
  104. self::$isResharingAllowed = true;
  105. } else {
  106. self::$isResharingAllowed = false;
  107. }
  108. }
  109. return self::$isResharingAllowed;
  110. }
  111. /**
  112. * group items with link to the same source
  113. *
  114. * @param array $items
  115. * @param string $itemType
  116. * @return array of grouped items
  117. */
  118. protected static function groupItems($items, $itemType) {
  119. $fileSharing = $itemType === 'file' || $itemType === 'folder';
  120. $result = [];
  121. foreach ($items as $item) {
  122. $grouped = false;
  123. foreach ($result as $key => $r) {
  124. // for file/folder shares we need to compare file_source, otherwise we compare item_source
  125. // only group shares if they already point to the same target, otherwise the file where shared
  126. // before grouping of shares was added. In this case we don't group them to avoid confusions
  127. if (($fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
  128. (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) {
  129. // add the first item to the list of grouped shares
  130. if (!isset($result[$key]['grouped'])) {
  131. $result[$key]['grouped'][] = $result[$key];
  132. }
  133. $result[$key]['permissions'] = (int)$item['permissions'] | (int)$r['permissions'];
  134. $result[$key]['grouped'][] = $item;
  135. $grouped = true;
  136. break;
  137. }
  138. }
  139. if (!$grouped) {
  140. $result[] = $item;
  141. }
  142. }
  143. return $result;
  144. }
  145. /**
  146. * remove protocol from URL
  147. *
  148. * @param string $url
  149. * @return string
  150. */
  151. public static function removeProtocolFromUrl($url) {
  152. if (str_starts_with($url, 'https://')) {
  153. return substr($url, strlen('https://'));
  154. } elseif (str_starts_with($url, 'http://')) {
  155. return substr($url, strlen('http://'));
  156. }
  157. return $url;
  158. }
  159. /**
  160. * @return int
  161. */
  162. public static function getExpireInterval() {
  163. return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  164. }
  165. }