1
0

share.php 15 KB

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