You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle, Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski <mtgap@owncloud.com>
  7. * 2014 Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * Public interface of ownCloud for apps to use.
  24. * Share Class
  25. *
  26. */
  27. // use OCP namespace for all classes that are considered public.
  28. // This means that they should be used by apps instead of the internal ownCloud classes
  29. namespace OCP;
  30. /**
  31. * This class provides the ability for apps to share their content between users.
  32. * Apps must create a backend class that implements OCP\Share_Backend and register it with this class.
  33. *
  34. * It provides the following hooks:
  35. * - post_shared
  36. */
  37. class Share extends \OC\Share\Constants {
  38. /**
  39. * Register a sharing backend class that implements OCP\Share_Backend for an item type
  40. * @param string Item type
  41. * @param string Backend class
  42. * @param string (optional) Depends on item type
  43. * @param array (optional) List of supported file extensions if this item type depends on files
  44. * @return Returns true if backend is registered or false if error
  45. */
  46. public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
  47. return \OC\Share\Share::registerBackend($itemType, $class, $collectionOf, $supportedFileExtensions);
  48. }
  49. /**
  50. * Check if the Share API is enabled
  51. * @return Returns true if enabled or false
  52. *
  53. * The Share API is enabled by default if not configured
  54. */
  55. public static function isEnabled() {
  56. return \OC\Share\Share::isEnabled();
  57. }
  58. /**
  59. * Find which users can access a shared item
  60. * @param string $path to the file
  61. * @param string $user owner of the file
  62. * @param bool $includeOwner include owner to the list of users with access to the file
  63. * @return array
  64. * @note $path needs to be relative to user data dir, e.g. 'file.txt'
  65. * not '/admin/data/file.txt'
  66. */
  67. public static function getUsersSharingFile($path, $user, $includeOwner = false) {
  68. return \OC\Share\Share::getUsersSharingFile($path, $user, $includeOwner);
  69. }
  70. /**
  71. * Get the items of item type shared with the current user
  72. * @param string Item type
  73. * @param int Format (optional) Format type must be defined by the backend
  74. * @param mixed Parameters (optional)
  75. * @param int Number of items to return (optional) Returns all by default
  76. * @param bool include collections (optional)
  77. * @return Return depends on format
  78. */
  79. public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
  80. $parameters = null, $limit = -1, $includeCollections = false) {
  81. return \OC\Share\Share::getItemsSharedWith($itemType, $format, $parameters, $limit, $includeCollections);
  82. }
  83. /**
  84. * Get the item of item type shared with the current user
  85. * @param string $itemType
  86. * @param string $itemTarget
  87. * @param int $format (optional) Format type must be defined by the backend
  88. * @param mixed Parameters (optional)
  89. * @param bool include collections (optional)
  90. * @return Return depends on format
  91. */
  92. public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE,
  93. $parameters = null, $includeCollections = false) {
  94. return \OC\Share\Share::getItemSharedWith($itemType, $itemTarget, $format, $parameters, $includeCollections);
  95. }
  96. /**
  97. * Get the item of item type shared with a given user by source
  98. * @param string $itemType
  99. * @param string $itemSource
  100. * @param string $user User user to whom the item was shared
  101. * @return array Return list of items with file_target, permissions and expiration
  102. */
  103. public static function getItemSharedWithUser($itemType, $itemSource, $user) {
  104. return \OC\Share\Share::getItemSharedWithUser($itemType, $itemSource, $user);
  105. }
  106. /**
  107. * Get the item of item type shared with the current user by source
  108. * @param string Item type
  109. * @param string Item source
  110. * @param int Format (optional) Format type must be defined by the backend
  111. * @param mixed Parameters
  112. * @param bool include collections
  113. * @return Return depends on format
  114. */
  115. public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
  116. $parameters = null, $includeCollections = false) {
  117. return \OC\Share\Share::getItemSharedWithBySource($itemType, $itemSource, $format, $parameters, $includeCollections);
  118. }
  119. /**
  120. * Get the item of item type shared by a link
  121. * @param string Item type
  122. * @param string Item source
  123. * @param string Owner of link
  124. * @return Item
  125. */
  126. public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) {
  127. return \OC\Share\Share::getItemSharedWithByLink($itemType, $itemSource, $uidOwner);
  128. }
  129. /**
  130. * Based on the given token the share information will be returned - password protected shares will be verified
  131. * @param string $token
  132. * @return array | bool false will be returned in case the token is unknown or unauthorized
  133. */
  134. public static function getShareByToken($token, $checkPasswordProtection = true) {
  135. return \OC\Share\Share::getShareByToken($token, $checkPasswordProtection);
  136. }
  137. /**
  138. * resolves reshares down to the last real share
  139. * @param $linkItem
  140. * @return $fileOwner
  141. */
  142. public static function resolveReShare($linkItem) {
  143. return \OC\Share\Share::resolveReShare($linkItem);
  144. }
  145. /**
  146. * Get the shared items of item type owned by the current user
  147. * @param string Item type
  148. * @param int Format (optional) Format type must be defined by the backend
  149. * @param mixed Parameters
  150. * @param int Number of items to return (optional) Returns all by default
  151. * @param bool include collections
  152. * @return Return depends on format
  153. */
  154. public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
  155. $limit = -1, $includeCollections = false) {
  156. return \OC\Share\Share::getItemsShared($itemType, $format, $parameters, $limit, $includeCollections);
  157. }
  158. /**
  159. * Get the shared item of item type owned by the current user
  160. * @param string Item type
  161. * @param string Item source
  162. * @param int Format (optional) Format type must be defined by the backend
  163. * @param mixed Parameters
  164. * @param bool include collections
  165. * @return Return depends on format
  166. */
  167. public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
  168. $parameters = null, $includeCollections = false) {
  169. return \OC\Share\Share::getItemShared($itemType, $itemSource, $format, $parameters, $includeCollections);
  170. }
  171. /**
  172. * Get all users an item is shared with
  173. * @param string Item type
  174. * @param string Item source
  175. * @param string Owner
  176. * @param bool Include collections
  177. * @praram bool check expire date
  178. * @return Return array of users
  179. */
  180. public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) {
  181. return \OC\Share\Share::getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections, $checkExpireDate);
  182. }
  183. /**
  184. * Share an item with a user, group, or via private link
  185. * @param string $itemType
  186. * @param string $itemSource
  187. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  188. * @param string $shareWith User or group the item is being shared with
  189. * @param int $permissions CRUDS
  190. * @param null $itemSourceName
  191. * @throws \Exception
  192. * @internal param \OCP\Item $string type
  193. * @internal param \OCP\Item $string source
  194. * @internal param \OCP\SHARE_TYPE_USER $int , SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  195. * @internal param \OCP\User $string or group the item is being shared with
  196. * @internal param \OCP\CRUDS $int permissions
  197. * @return bool|string Returns true on success or false on failure, Returns token on success for links
  198. */
  199. public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null) {
  200. return \OC\Share\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName);
  201. }
  202. /**
  203. * Unshare an item from a user, group, or delete a private link
  204. * @param string Item type
  205. * @param string Item source
  206. * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  207. * @param string User or group the item is being shared with
  208. * @return Returns true on success or false on failure
  209. */
  210. public static function unshare($itemType, $itemSource, $shareType, $shareWith) {
  211. return \OC\Share\Share::unshare($itemType, $itemSource, $shareType, $shareWith);
  212. }
  213. /**
  214. * Unshare an item from all users, groups, and remove all links
  215. * @param string Item type
  216. * @param string Item source
  217. * @return Returns true on success or false on failure
  218. */
  219. public static function unshareAll($itemType, $itemSource) {
  220. return \OC\Share\Share::unshareAll($itemType, $itemSource);
  221. }
  222. /**
  223. * Unshare an item shared with the current user
  224. * @param string Item type
  225. * @param string Item target
  226. * @return Returns true on success or false on failure
  227. *
  228. * Unsharing from self is not allowed for items inside collections
  229. */
  230. public static function unshareFromSelf($itemType, $itemTarget) {
  231. return \OC\Share\Share::unshareFromSelf($itemType, $itemTarget);
  232. }
  233. /**
  234. * sent status if users got informed by mail about share
  235. * @param string $itemType
  236. * @param string $itemSource
  237. * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  238. * @param bool $status
  239. */
  240. public static function setSendMailStatus($itemType, $itemSource, $shareType, $status) {
  241. return \OC\Share\Share::setSendMailStatus($itemType, $itemSource, $shareType, $status);
  242. }
  243. /**
  244. * Set the permissions of an item for a specific user or group
  245. * @param string Item type
  246. * @param string Item source
  247. * @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
  248. * @param string User or group the item is being shared with
  249. * @param int CRUDS permissions
  250. * @return Returns true on success or false on failure
  251. */
  252. public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
  253. return \OC\Share\Share::setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions);
  254. }
  255. /**
  256. * Set expiration date for a share
  257. * @param string $itemType
  258. * @param string $itemSource
  259. * @param string $date expiration date
  260. * @return Share_Backend
  261. */
  262. public static function setExpirationDate($itemType, $itemSource, $date) {
  263. return \OC\Share\Share::setExpirationDate($itemType, $itemSource, $date);
  264. }
  265. /**
  266. * Get the backend class for the specified item type
  267. * @param string $itemType
  268. * @return Share_Backend
  269. */
  270. public static function getBackend($itemType) {
  271. return \OC\Share\Share::getBackend($itemType);
  272. }
  273. /**
  274. * Delete all shares with type SHARE_TYPE_LINK
  275. */
  276. public static function removeAllLinkShares() {
  277. return \OC\Share\Share::removeAllLinkShares();
  278. }
  279. /**
  280. * In case a password protected link is not yet authenticated this function will return false
  281. *
  282. * @param array $linkItem
  283. * @return bool
  284. */
  285. public static function checkPasswordProtectedShare(array $linkItem) {
  286. return \OC\Share\Share::checkPasswordProtectedShare($linkItem);
  287. }
  288. }
  289. /**
  290. * Interface that apps must implement to share content.
  291. */
  292. interface Share_Backend {
  293. /**
  294. * Get the source of the item to be stored in the database
  295. * @param string Item source
  296. * @param string Owner of the item
  297. * @return mixed|array|false Source
  298. *
  299. * Return an array if the item is file dependent, the array needs two keys: 'item' and 'file'
  300. * Return false if the item does not exist for the user
  301. *
  302. * The formatItems() function will translate the source returned back into the item
  303. */
  304. public function isValidSource($itemSource, $uidOwner);
  305. /**
  306. * Get a unique name of the item for the specified user
  307. * @param string Item source
  308. * @param string|false User the item is being shared with
  309. * @param array|null List of similar item names already existing as shared items
  310. * @return string Target name
  311. *
  312. * This function needs to verify that the user does not already have an item with this name.
  313. * If it does generate a new name e.g. name_#
  314. */
  315. public function generateTarget($itemSource, $shareWith, $exclude = null);
  316. /**
  317. * Converts the shared item sources back into the item in the specified format
  318. * @param array Shared items
  319. * @param int Format
  320. * @return TODO
  321. *
  322. * The items array is a 3-dimensional array with the item_source as the
  323. * first key and the share id as the second key to an array with the share
  324. * info.
  325. *
  326. * The key/value pairs included in the share info depend on the function originally called:
  327. * If called by getItem(s)Shared: id, item_type, item, item_source,
  328. * share_type, share_with, permissions, stime, file_source
  329. *
  330. * If called by getItem(s)SharedWith: id, item_type, item, item_source,
  331. * item_target, share_type, share_with, permissions, stime, file_source,
  332. * file_target
  333. *
  334. * This function allows the backend to control the output of shared items with custom formats.
  335. * It is only called through calls to the public getItem(s)Shared(With) functions.
  336. */
  337. public function formatItems($items, $format, $parameters = null);
  338. }
  339. /**
  340. * Interface for share backends that share content that is dependent on files.
  341. * Extends the Share_Backend interface.
  342. */
  343. interface Share_Backend_File_Dependent extends Share_Backend {
  344. /**
  345. * Get the file path of the item
  346. * @param string Item source
  347. * @param string User that is the owner of shared item
  348. */
  349. public function getFilePath($itemSource, $uidOwner);
  350. }
  351. /**
  352. * Interface for collections of of items implemented by another share backend.
  353. * Extends the Share_Backend interface.
  354. */
  355. interface Share_Backend_Collection extends Share_Backend {
  356. /**
  357. * Get the sources of the children of the item
  358. * @param string Item source
  359. * @return array Returns an array of children each inside an array with the keys: source, target, and file_path if applicable
  360. */
  361. public function getChildren($itemSource);
  362. }