Share.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Bernhard Reiter <ockham@raz.or.at>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Sebastian Döll <sebastian.doell@libasys.de>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. * @author Volkan Gezer <volkangezer@gmail.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OC\Share;
  36. use OCA\Files_Sharing\ShareBackend\File;
  37. use Psr\Log\LoggerInterface;
  38. /**
  39. * This class provides the ability for apps to share their content between users.
  40. * Apps must create a backend class that implements OCP\Share_Backend and register it with this class.
  41. *
  42. * It provides the following hooks:
  43. * - post_shared
  44. */
  45. class Share extends Constants {
  46. /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask
  47. * Construct permissions for share() and setPermissions with Or (|) e.g.
  48. * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE
  49. *
  50. * Check if permission is granted with And (&) e.g. Check if delete is
  51. * granted: if ($permissions & PERMISSION_DELETE)
  52. *
  53. * Remove permissions with And (&) and Not (~) e.g. Remove the update
  54. * permission: $permissions &= ~PERMISSION_UPDATE
  55. *
  56. * Apps are required to handle permissions on their own, this class only
  57. * stores and manages the permissions of shares
  58. *
  59. * @see lib/public/Constants.php
  60. */
  61. /**
  62. * Register a sharing backend class that implements OCP\Share_Backend for an item type
  63. *
  64. * @param string $itemType Item type
  65. * @param string $class Backend class
  66. * @param string $collectionOf (optional) Depends on item type
  67. * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files
  68. * @return boolean true if backend is registered or false if error
  69. */
  70. public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
  71. if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') == 'yes') {
  72. if (!isset(self::$backendTypes[$itemType])) {
  73. self::$backendTypes[$itemType] = [
  74. 'class' => $class,
  75. 'collectionOf' => $collectionOf,
  76. 'supportedFileExtensions' => $supportedFileExtensions
  77. ];
  78. return true;
  79. }
  80. \OC::$server->get(LoggerInterface::class)->warning(
  81. 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class']
  82. .' is already registered for '.$itemType,
  83. ['app' => 'files_sharing']);
  84. }
  85. return false;
  86. }
  87. /**
  88. * Get the backend class for the specified item type
  89. *
  90. * @param string $itemType
  91. * @return \OCP\Share_Backend
  92. * @throws \Exception
  93. */
  94. public static function getBackend($itemType) {
  95. $l = \OCP\Util::getL10N('lib');
  96. $logger = \OCP\Server::get(LoggerInterface::class);
  97. if (isset(self::$backends[$itemType])) {
  98. return self::$backends[$itemType];
  99. } elseif (isset(self::$backendTypes[$itemType]['class'])) {
  100. $class = self::$backendTypes[$itemType]['class'];
  101. if (class_exists($class)) {
  102. self::$backends[$itemType] = new $class;
  103. if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) {
  104. $message = 'Sharing backend %s must implement the interface OCP\Share_Backend';
  105. $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', [$class]);
  106. $logger->error(sprintf($message, $class), ['app' => 'OCP\Share']);
  107. throw new \Exception($message_t);
  108. }
  109. return self::$backends[$itemType];
  110. } else {
  111. $message = 'Sharing backend %s not found';
  112. $message_t = $l->t('Sharing backend %s not found', [$class]);
  113. $logger->error(sprintf($message, $class), ['app' => 'OCP\Share']);
  114. throw new \Exception($message_t);
  115. }
  116. }
  117. $message = 'Sharing backend for %s not found';
  118. $message_t = $l->t('Sharing backend for %s not found', [$itemType]);
  119. $logger->error(sprintf($message, $itemType), ['app' => 'OCP\Share']);
  120. throw new \Exception($message_t);
  121. }
  122. /**
  123. * Check if resharing is allowed
  124. *
  125. * @return boolean true if allowed or false
  126. *
  127. * Resharing is allowed by default if not configured
  128. */
  129. public static function isResharingAllowed() {
  130. if (!isset(self::$isResharingAllowed)) {
  131. if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') {
  132. self::$isResharingAllowed = true;
  133. } else {
  134. self::$isResharingAllowed = false;
  135. }
  136. }
  137. return self::$isResharingAllowed;
  138. }
  139. /**
  140. * group items with link to the same source
  141. *
  142. * @param array $items
  143. * @param string $itemType
  144. * @return array of grouped items
  145. */
  146. protected static function groupItems($items, $itemType) {
  147. $fileSharing = $itemType === 'file' || $itemType === 'folder';
  148. $result = [];
  149. foreach ($items as $item) {
  150. $grouped = false;
  151. foreach ($result as $key => $r) {
  152. // for file/folder shares we need to compare file_source, otherwise we compare item_source
  153. // only group shares if they already point to the same target, otherwise the file where shared
  154. // before grouping of shares was added. In this case we don't group them to avoid confusions
  155. if (($fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) ||
  156. (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) {
  157. // add the first item to the list of grouped shares
  158. if (!isset($result[$key]['grouped'])) {
  159. $result[$key]['grouped'][] = $result[$key];
  160. }
  161. $result[$key]['permissions'] = (int)$item['permissions'] | (int)$r['permissions'];
  162. $result[$key]['grouped'][] = $item;
  163. $grouped = true;
  164. break;
  165. }
  166. }
  167. if (!$grouped) {
  168. $result[] = $item;
  169. }
  170. }
  171. return $result;
  172. }
  173. /**
  174. * remove protocol from URL
  175. *
  176. * @param string $url
  177. * @return string
  178. */
  179. public static function removeProtocolFromUrl($url) {
  180. if (str_starts_with($url, 'https://')) {
  181. return substr($url, strlen('https://'));
  182. } elseif (str_starts_with($url, 'http://')) {
  183. return substr($url, strlen('http://'));
  184. }
  185. return $url;
  186. }
  187. /**
  188. * @return int
  189. */
  190. public static function getExpireInterval() {
  191. return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7');
  192. }
  193. }