share.php 15 KB

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