share.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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 <rmccorkell@karoshi.org.uk>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Sam Tuke <mail@samtuke.com>
  14. * @author Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @copyright Copyright (c) 2015, 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. * @return array
  78. * @note $path needs to be relative to user data dir, e.g. 'file.txt'
  79. * not '/admin/data/file.txt'
  80. * @since 5.0.0
  81. */
  82. public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false) {
  83. return \OC\Share\Share::getUsersSharingFile($path, $ownerUser, $includeOwner, $returnUserPaths);
  84. }
  85. /**
  86. * Get the items of item type shared with the current user
  87. * @param string $itemType
  88. * @param int $format (optional) Format type must be defined by the backend
  89. * @param mixed $parameters (optional)
  90. * @param int $limit Number of items to return (optional) Returns all by default
  91. * @param bool $includeCollections (optional)
  92. * @return mixed Return depends on format
  93. * @since 5.0.0
  94. */
  95. public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
  96. $parameters = null, $limit = -1, $includeCollections = false) {
  97. return \OC\Share\Share::getItemsSharedWith($itemType, $format, $parameters, $limit, $includeCollections);
  98. }
  99. /**
  100. * Get the items of item type shared with a user
  101. * @param string $itemType
  102. * @param string $user for which user we want the shares
  103. * @param int $format (optional) Format type must be defined by the backend
  104. * @param mixed $parameters (optional)
  105. * @param int $limit Number of items to return (optional) Returns all by default
  106. * @param bool $includeCollections (optional)
  107. * @return mixed Return depends on format
  108. * @since 7.0.0
  109. */
  110. public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE,
  111. $parameters = null, $limit = -1, $includeCollections = false) {
  112. return \OC\Share\Share::getItemsSharedWithUser($itemType, $user, $format, $parameters, $limit, $includeCollections);
  113. }
  114. /**
  115. * Get the item of item type shared with the current user
  116. * @param string $itemType
  117. * @param string $itemTarget
  118. * @param int $format (optional) Format type must be defined by the backend
  119. * @param mixed $parameters (optional)
  120. * @param bool $includeCollections (optional)
  121. * @return mixed Return depends on format
  122. * @since 5.0.0
  123. */
  124. public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE,
  125. $parameters = null, $includeCollections = false) {
  126. return \OC\Share\Share::getItemSharedWith($itemType, $itemTarget, $format, $parameters, $includeCollections);
  127. }
  128. /**
  129. * Get the item of item type shared with a given user by source
  130. * @param string $itemType
  131. * @param string $itemSource
  132. * @param string $user User to whom the item was shared
  133. * @param string $owner Owner of the share
  134. * @return array Return list of items with file_target, permissions and expiration
  135. * @since 6.0.0 - parameter $owner was added in 8.0.0
  136. */
  137. public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null) {
  138. return \OC\Share\Share::getItemSharedWithUser($itemType, $itemSource, $user, $owner);
  139. }
  140. /**
  141. * Get the item of item type shared with the current user by source
  142. * @param string $itemType
  143. * @param string $itemSource
  144. * @param int $format (optional) Format type must be defined by the backend
  145. * @param mixed $parameters
  146. * @param bool $includeCollections
  147. * @return array
  148. * @since 5.0.0
  149. */
  150. public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
  151. $parameters = null, $includeCollections = false) {
  152. return \OC\Share\Share::getItemSharedWithBySource($itemType, $itemSource, $format, $parameters, $includeCollections);
  153. }
  154. /**
  155. * Get the item of item type shared by a link
  156. * @param string $itemType
  157. * @param string $itemSource
  158. * @param string $uidOwner Owner of link
  159. * @return array
  160. * @since 5.0.0
  161. */
  162. public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) {
  163. return \OC\Share\Share::getItemSharedWithByLink($itemType, $itemSource, $uidOwner);
  164. }
  165. /**
  166. * Based on the given token the share information will be returned - password protected shares will be verified
  167. * @param string $token
  168. * @param bool $checkPasswordProtection
  169. * @return array|bool false will be returned in case the token is unknown or unauthorized
  170. * @since 5.0.0 - parameter $checkPasswordProtection was added in 7.0.0
  171. */
  172. public static function getShareByToken($token, $checkPasswordProtection = true) {
  173. return \OC\Share\Share::getShareByToken($token, $checkPasswordProtection);
  174. }
  175. /**
  176. * resolves reshares down to the last real share
  177. * @param array $linkItem
  178. * @return array file owner
  179. * @since 6.0.0
  180. */
  181. public static function resolveReShare($linkItem) {
  182. return \OC\Share\Share::resolveReShare($linkItem);
  183. }
  184. /**
  185. * Get the shared items of item type owned by the current user
  186. * @param string $itemType
  187. * @param int $format (optional) Format type must be defined by the backend
  188. * @param mixed $parameters
  189. * @param int $limit Number of items to return (optional) Returns all by default
  190. * @param bool $includeCollections
  191. * @return mixed Return depends on format
  192. * @since 5.0.0
  193. */
  194. public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
  195. $limit = -1, $includeCollections = false) {
  196. return \OC\Share\Share::getItemsShared($itemType, $format, $parameters, $limit, $includeCollections);
  197. }
  198. /**
  199. * Get the shared item of item type owned by the current user
  200. * @param string $itemType
  201. * @param string $itemSource
  202. * @param int $format (optional) Format type must be defined by the backend
  203. * @param mixed $parameters
  204. * @param bool $includeCollections
  205. * @return mixed Return depends on format
  206. * @since 5.0.0
  207. */
  208. public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
  209. $parameters = null, $includeCollections = false) {
  210. return \OC\Share\Share::getItemShared($itemType, $itemSource, $format, $parameters, $includeCollections);
  211. }
  212. /**
  213. * Get all users an item is shared with
  214. * @param string $itemType
  215. * @param string $itemSource
  216. * @param string $uidOwner
  217. * @param bool $includeCollections
  218. * @param bool $checkExpireDate
  219. * @return array Return array of users
  220. * @since 5.0.0 - parameter $checkExpireDate was added in 7.0.0
  221. */
  222. public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) {
  223. return \OC\Share\Share::getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections, $checkExpireDate);
  224. }
  225. /**
  226. * Share an item with a user, group, or via private link
  227. * @param string $itemType
  228. * @param string $itemSource
  229. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  230. * @param string $shareWith User or group the item is being shared with
  231. * @param int $permissions CRUDS
  232. * @param string $itemSourceName
  233. * @param \DateTime $expirationDate
  234. * @return bool|string Returns true on success or false on failure, Returns token on success for links
  235. * @throws \Exception
  236. * @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0
  237. */
  238. public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null) {
  239. return \OC\Share\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName, $expirationDate);
  240. }
  241. /**
  242. * Unshare an item from a user, group, or delete a private link
  243. * @param string $itemType
  244. * @param string $itemSource
  245. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  246. * @param string $shareWith User or group the item is being shared with
  247. * @param string $owner owner of the share, if null the current user is used
  248. * @return boolean true on success or false on failure
  249. * @since 5.0.0 - parameter $owner was added in 8.0.0
  250. */
  251. public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) {
  252. return \OC\Share\Share::unshare($itemType, $itemSource, $shareType, $shareWith, $owner);
  253. }
  254. /**
  255. * Unshare an item from all users, groups, and remove all links
  256. * @param string $itemType
  257. * @param string $itemSource
  258. * @return boolean true on success or false on failure
  259. * @since 5.0.0
  260. */
  261. public static function unshareAll($itemType, $itemSource) {
  262. return \OC\Share\Share::unshareAll($itemType, $itemSource);
  263. }
  264. /**
  265. * Unshare an item shared with the current user
  266. * @param string $itemType
  267. * @param string $itemOrigin Item target or source
  268. * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional)
  269. * @return boolean true on success or false on failure
  270. *
  271. * Unsharing from self is not allowed for items inside collections
  272. * @since 5.0.0 - parameter $originIsSource was added in 8.0.0
  273. */
  274. public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) {
  275. return \OC\Share\Share::unshareFromSelf($itemType, $itemOrigin, $originIsSource);
  276. }
  277. /**
  278. * sent status if users got informed by mail about share
  279. * @param string $itemType
  280. * @param string $itemSource
  281. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  282. * @param string $recipient with whom was the item shared
  283. * @param bool $status
  284. * @since 6.0.0 - parameter $originIsSource was added in 8.0.0
  285. */
  286. public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) {
  287. return \OC\Share\Share::setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status);
  288. }
  289. /**
  290. * Set the permissions of an item for a specific user or group
  291. * @param string $itemType
  292. * @param string $itemSource
  293. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  294. * @param string $shareWith User or group the item is being shared with
  295. * @param int $permissions CRUDS permissions
  296. * @return boolean true on success or false on failure
  297. * @since 5.0.0
  298. */
  299. public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
  300. return \OC\Share\Share::setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions);
  301. }
  302. /**
  303. * Set expiration date for a share
  304. * @param string $itemType
  305. * @param string $itemSource
  306. * @param string $date expiration date
  307. * @param int $shareTime timestamp from when the file was shared
  308. * @return boolean
  309. * @since 5.0.0 - parameter $shareTime was added in 8.0.0
  310. */
  311. public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) {
  312. return \OC\Share\Share::setExpirationDate($itemType, $itemSource, $date, $shareTime);
  313. }
  314. /**
  315. * Set expiration date for a share
  316. * @param int $shareId
  317. * @param string $password
  318. * @return boolean
  319. * @since 8.1.0
  320. */
  321. public static function setPassword($shareId, $password) {
  322. $userSession = \OC::$server->getUserSession();
  323. $connection = \OC::$server->getDatabaseConnection();
  324. $config = \OC::$server->getConfig();
  325. return \OC\Share\Share::setPassword($userSession, $connection, $config, $shareId, $password);
  326. }
  327. /**
  328. * Get the backend class for the specified item type
  329. * @param string $itemType
  330. * @return Share_Backend
  331. * @since 5.0.0
  332. */
  333. public static function getBackend($itemType) {
  334. return \OC\Share\Share::getBackend($itemType);
  335. }
  336. /**
  337. * Delete all shares with type SHARE_TYPE_LINK
  338. * @since 6.0.0
  339. */
  340. public static function removeAllLinkShares() {
  341. return \OC\Share\Share::removeAllLinkShares();
  342. }
  343. /**
  344. * In case a password protected link is not yet authenticated this function will return false
  345. *
  346. * @param array $linkItem
  347. * @return bool
  348. * @since 7.0.0
  349. */
  350. public static function checkPasswordProtectedShare(array $linkItem) {
  351. return \OC\Share\Share::checkPasswordProtectedShare($linkItem);
  352. }
  353. /**
  354. * Check if resharing is allowed
  355. *
  356. * @return boolean true if allowed or false
  357. * @since 5.0.0
  358. */
  359. public static function isResharingAllowed() {
  360. return \OC\Share\Share::isResharingAllowed();
  361. }
  362. }