Share.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  11. * @author Michael Kuhn <suraia@ikkoku.de>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Sam Tuke <mail@samtuke.com>
  16. * @author Stefan Weil <sw@weilnetz.de>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. /**
  35. * Public interface of ownCloud for apps to use.
  36. * Share Class
  37. *
  38. */
  39. // use OCP namespace for all classes that are considered public.
  40. // This means that they should be used by apps instead of the internal ownCloud classes
  41. namespace OCP;
  42. /**
  43. * This class provides the ability for apps to share their content between users.
  44. * Apps must create a backend class that implements OCP\Share_Backend and register it with this class.
  45. *
  46. * It provides the following hooks:
  47. * - post_shared
  48. * @since 5.0.0
  49. */
  50. class Share extends \OC\Share\Constants {
  51. /**
  52. * Register a sharing backend class that implements OCP\Share_Backend for an item type
  53. * @param string $itemType Item type
  54. * @param string $class Backend class
  55. * @param string $collectionOf (optional) Depends on item type
  56. * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files
  57. * @return boolean true if backend is registered or false if error
  58. * @since 5.0.0
  59. */
  60. public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
  61. return \OC\Share\Share::registerBackend($itemType, $class, $collectionOf, $supportedFileExtensions);
  62. }
  63. /**
  64. * Check if the Share API is enabled
  65. * @return boolean true if enabled or false
  66. *
  67. * The Share API is enabled by default if not configured
  68. * @since 5.0.0
  69. */
  70. public static function isEnabled() {
  71. return \OC\Share\Share::isEnabled();
  72. }
  73. /**
  74. * Find which users can access a shared item
  75. * @param string $path to the file
  76. * @param string $ownerUser owner of the file
  77. * @param bool $includeOwner include owner to the list of users with access to the file
  78. * @param bool $returnUserPaths Return an array with the user => path map
  79. * @param bool $recursive take parent folders into account
  80. * @return array
  81. * @note $path needs to be relative to user data dir, e.g. 'file.txt'
  82. * not '/admin/files/file.txt'
  83. * @since 5.0.0 - $recursive was added in 9.0.0
  84. */
  85. public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) {
  86. return \OC\Share\Share::getUsersSharingFile(
  87. $path,
  88. $ownerUser,
  89. \OC::$server->getUserManager(),
  90. \OC::$server->getLogger(),
  91. $includeOwner,
  92. $returnUserPaths,
  93. $recursive
  94. );
  95. }
  96. /**
  97. * Get the items of item type shared with the current user
  98. * @param string $itemType
  99. * @param int $format (optional) Format type must be defined by the backend
  100. * @param mixed $parameters (optional)
  101. * @param int $limit Number of items to return (optional) Returns all by default
  102. * @param bool $includeCollections (optional)
  103. * @return mixed Return depends on format
  104. * @since 5.0.0
  105. */
  106. public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
  107. $parameters = null, $limit = -1, $includeCollections = false) {
  108. return \OC\Share\Share::getItemsSharedWith($itemType, $format, $parameters, $limit, $includeCollections);
  109. }
  110. /**
  111. * Get the items of item type shared with a user
  112. * @param string $itemType
  113. * @param string $user for which user we want the shares
  114. * @param int $format (optional) Format type must be defined by the backend
  115. * @param mixed $parameters (optional)
  116. * @param int $limit Number of items to return (optional) Returns all by default
  117. * @param bool $includeCollections (optional)
  118. * @return mixed Return depends on format
  119. * @since 7.0.0
  120. */
  121. public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE,
  122. $parameters = null, $limit = -1, $includeCollections = false) {
  123. return \OC\Share\Share::getItemsSharedWithUser($itemType, $user, $format, $parameters, $limit, $includeCollections);
  124. }
  125. /**
  126. * Get the item of item type shared with the current user
  127. * @param string $itemType
  128. * @param string $itemTarget
  129. * @param int $format (optional) Format type must be defined by the backend
  130. * @param mixed $parameters (optional)
  131. * @param bool $includeCollections (optional)
  132. * @return mixed Return depends on format
  133. * @since 5.0.0
  134. */
  135. public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE,
  136. $parameters = null, $includeCollections = false) {
  137. return \OC\Share\Share::getItemSharedWith($itemType, $itemTarget, $format, $parameters, $includeCollections);
  138. }
  139. /**
  140. * Get the item of item type shared with a given user by source
  141. * @param string $itemType
  142. * @param string $itemSource
  143. * @param string $user User to whom the item was shared
  144. * @param string $owner Owner of the share
  145. * @return array Return list of items with file_target, permissions and expiration
  146. * @since 6.0.0 - parameter $owner was added in 8.0.0
  147. */
  148. public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null) {
  149. return \OC\Share\Share::getItemSharedWithUser($itemType, $itemSource, $user, $owner);
  150. }
  151. /**
  152. * Get the item of item type shared with the current user by source
  153. * @param string $itemType
  154. * @param string $itemSource
  155. * @param int $format (optional) Format type must be defined by the backend
  156. * @param mixed $parameters
  157. * @param bool $includeCollections
  158. * @return array
  159. * @since 5.0.0
  160. */
  161. public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
  162. $parameters = null, $includeCollections = false) {
  163. return \OC\Share\Share::getItemSharedWithBySource($itemType, $itemSource, $format, $parameters, $includeCollections);
  164. }
  165. /**
  166. * Get the item of item type shared by a link
  167. * @param string $itemType
  168. * @param string $itemSource
  169. * @param string $uidOwner Owner of link
  170. * @return array
  171. * @since 5.0.0
  172. */
  173. public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) {
  174. return \OC\Share\Share::getItemSharedWithByLink($itemType, $itemSource, $uidOwner);
  175. }
  176. /**
  177. * Based on the given token the share information will be returned - password protected shares will be verified
  178. * @param string $token
  179. * @param bool $checkPasswordProtection
  180. * @return array|bool false will be returned in case the token is unknown or unauthorized
  181. * @since 5.0.0 - parameter $checkPasswordProtection was added in 7.0.0
  182. */
  183. public static function getShareByToken($token, $checkPasswordProtection = true) {
  184. return \OC\Share\Share::getShareByToken($token, $checkPasswordProtection);
  185. }
  186. /**
  187. * resolves reshares down to the last real share
  188. * @param array $linkItem
  189. * @return array file owner
  190. * @since 6.0.0
  191. */
  192. public static function resolveReShare($linkItem) {
  193. return \OC\Share\Share::resolveReShare($linkItem);
  194. }
  195. /**
  196. * Get the shared items of item type owned by the current user
  197. * @param string $itemType
  198. * @param int $format (optional) Format type must be defined by the backend
  199. * @param mixed $parameters
  200. * @param int $limit Number of items to return (optional) Returns all by default
  201. * @param bool $includeCollections
  202. * @return mixed Return depends on format
  203. * @since 5.0.0
  204. */
  205. public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
  206. $limit = -1, $includeCollections = false) {
  207. return \OC\Share\Share::getItemsShared($itemType, $format, $parameters, $limit, $includeCollections);
  208. }
  209. /**
  210. * Get the shared item of item type owned by the current user
  211. * @param string $itemType
  212. * @param string $itemSource
  213. * @param int $format (optional) Format type must be defined by the backend
  214. * @param mixed $parameters
  215. * @param bool $includeCollections
  216. * @return mixed Return depends on format
  217. * @since 5.0.0
  218. */
  219. public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
  220. $parameters = null, $includeCollections = false) {
  221. return \OC\Share\Share::getItemShared($itemType, $itemSource, $format, $parameters, $includeCollections);
  222. }
  223. /**
  224. * Get all users an item is shared with
  225. * @param string $itemType
  226. * @param string $itemSource
  227. * @param string $uidOwner
  228. * @param bool $includeCollections
  229. * @param bool $checkExpireDate
  230. * @return array Return array of users
  231. * @since 5.0.0 - parameter $checkExpireDate was added in 7.0.0
  232. */
  233. public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) {
  234. return \OC\Share\Share::getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections, $checkExpireDate);
  235. }
  236. /**
  237. * Share an item with a user, group, or via private link
  238. * @param string $itemType
  239. * @param string $itemSource
  240. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  241. * @param string $shareWith User or group the item is being shared with
  242. * @param int $permissions CRUDS
  243. * @param string $itemSourceName
  244. * @param \DateTime $expirationDate
  245. * @param bool $passwordChanged
  246. * @return bool|string Returns true on success or false on failure, Returns token on success for links
  247. * @throws \OC\HintException when the share type is remote and the shareWith is invalid
  248. * @throws \Exception
  249. * @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0, parameter $passwordChanged added in 9.0.0
  250. */
  251. public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) {
  252. return \OC\Share\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName, $expirationDate, $passwordChanged);
  253. }
  254. /**
  255. * Unshare an item from a user, group, or delete a private link
  256. * @param string $itemType
  257. * @param string $itemSource
  258. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  259. * @param string $shareWith User or group the item is being shared with
  260. * @param string $owner owner of the share, if null the current user is used
  261. * @return boolean true on success or false on failure
  262. * @since 5.0.0 - parameter $owner was added in 8.0.0
  263. */
  264. public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) {
  265. return \OC\Share\Share::unshare($itemType, $itemSource, $shareType, $shareWith, $owner);
  266. }
  267. /**
  268. * Unshare an item from all users, groups, and remove all links
  269. * @param string $itemType
  270. * @param string $itemSource
  271. * @return boolean true on success or false on failure
  272. * @since 5.0.0
  273. */
  274. public static function unshareAll($itemType, $itemSource) {
  275. return \OC\Share\Share::unshareAll($itemType, $itemSource);
  276. }
  277. /**
  278. * Unshare an item shared with the current user
  279. * @param string $itemType
  280. * @param string $itemOrigin Item target or source
  281. * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional)
  282. * @return boolean true on success or false on failure
  283. *
  284. * Unsharing from self is not allowed for items inside collections
  285. * @since 5.0.0 - parameter $originIsSource was added in 8.0.0
  286. */
  287. public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) {
  288. return \OC\Share\Share::unshareFromSelf($itemType, $itemOrigin, $originIsSource);
  289. }
  290. /**
  291. * sent status if users got informed by mail about share
  292. * @param string $itemType
  293. * @param string $itemSource
  294. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  295. * @param string $recipient with whom was the item shared
  296. * @param bool $status
  297. * @since 6.0.0 - parameter $originIsSource was added in 8.0.0
  298. */
  299. public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) {
  300. return \OC\Share\Share::setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status);
  301. }
  302. /**
  303. * Set the permissions of an item for a specific user or group
  304. * @param string $itemType
  305. * @param string $itemSource
  306. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  307. * @param string $shareWith User or group the item is being shared with
  308. * @param int $permissions CRUDS permissions
  309. * @return boolean true on success or false on failure
  310. * @since 5.0.0
  311. */
  312. public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
  313. return \OC\Share\Share::setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions);
  314. }
  315. /**
  316. * Set expiration date for a share
  317. * @param string $itemType
  318. * @param string $itemSource
  319. * @param string $date expiration date
  320. * @param int $shareTime timestamp from when the file was shared
  321. * @return boolean
  322. * @since 5.0.0 - parameter $shareTime was added in 8.0.0
  323. */
  324. public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) {
  325. return \OC\Share\Share::setExpirationDate($itemType, $itemSource, $date, $shareTime);
  326. }
  327. /**
  328. * Set password for a public link share
  329. * @param int $shareId
  330. * @param string $password
  331. * @return boolean
  332. * @since 8.1.0
  333. */
  334. public static function setPassword($shareId, $password) {
  335. $userSession = \OC::$server->getUserSession();
  336. $connection = \OC::$server->getDatabaseConnection();
  337. $config = \OC::$server->getConfig();
  338. return \OC\Share\Share::setPassword($userSession, $connection, $config, $shareId, $password);
  339. }
  340. /**
  341. * Get the backend class for the specified item type
  342. * @param string $itemType
  343. * @return Share_Backend
  344. * @since 5.0.0
  345. */
  346. public static function getBackend($itemType) {
  347. return \OC\Share\Share::getBackend($itemType);
  348. }
  349. /**
  350. * Delete all shares with type SHARE_TYPE_LINK
  351. * @since 6.0.0
  352. */
  353. public static function removeAllLinkShares() {
  354. return \OC\Share\Share::removeAllLinkShares();
  355. }
  356. /**
  357. * In case a password protected link is not yet authenticated this function will return false
  358. *
  359. * @param array $linkItem
  360. * @return bool
  361. * @since 7.0.0
  362. */
  363. public static function checkPasswordProtectedShare(array $linkItem) {
  364. return \OC\Share\Share::checkPasswordProtectedShare($linkItem);
  365. }
  366. /**
  367. * Check if resharing is allowed
  368. *
  369. * @return boolean true if allowed or false
  370. * @since 5.0.0
  371. */
  372. public static function isResharingAllowed() {
  373. return \OC\Share\Share::isResharingAllowed();
  374. }
  375. }