nextcloud/lib/public/share.php

461 lines
16 KiB
PHP
Raw Normal View History

2012-10-14 23:04:08 +04:00
<?php
/**
2013-10-31 22:00:53 +04:00
* ownCloud
*
2014-02-18 15:37:32 +04:00
* @author Bjoern Schiessle, Michael Gapczynski
* @copyright 2012 Michael Gapczynski <mtgap@owncloud.com>
* 2014 Bjoern Schiessle <schiessle@owncloud.com>
2013-10-31 22:00:53 +04:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
2013-11-03 16:38:25 +04:00
/**
* Public interface of ownCloud for apps to use.
* Share Class
*
*/
// use OCP namespace for all classes that are considered public.
// This means that they should be used by apps instead of the internal ownCloud classes
2012-10-14 23:04:08 +04:00
namespace OCP;
/**
2013-10-31 22:00:53 +04:00
* This class provides the ability for apps to share their content between users.
* Apps must create a backend class that implements OCP\Share_Backend and register it with this class.
*
* It provides the following hooks:
* - post_shared
*/
2012-10-14 23:04:08 +04:00
class Share {
2014-02-18 15:37:32 +04:00
const FORMAT_NONE = -1;
const FORMAT_STATUSES = -2;
const FORMAT_SOURCES = -3;
2012-10-14 23:04:08 +04:00
const SHARE_TYPE_USER = 0;
const SHARE_TYPE_GROUP = 1;
const SHARE_TYPE_LINK = 3;
const SHARE_TYPE_EMAIL = 4;
const SHARE_TYPE_CONTACT = 5;
const SHARE_TYPE_REMOTE = 6;
/**
2013-10-31 22:00:53 +04:00
* Register a sharing backend class that implements OCP\Share_Backend for an item type
* @param string Item type
* @param string Backend class
* @param string (optional) Depends on item type
* @param array (optional) List of supported file extensions if this item type depends on files
2014-02-18 15:37:32 +04:00
* @return Returns true if backend is registered or false if error
2013-10-31 22:00:53 +04:00
*/
2012-10-14 23:04:08 +04:00
public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::registerBackend($itemType, $class, $collectionOf, $supportedFileExtensions);
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Check if the Share API is enabled
2014-02-18 15:37:32 +04:00
* @return Returns true if enabled or false
2013-10-31 22:00:53 +04:00
*
* The Share API is enabled by default if not configured
*/
2012-10-14 23:04:08 +04:00
public static function isEnabled() {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::isEnabled();
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Prepare a path to be passed to DB as file_target
* @param string $path path
* @return string Prepared path
*/
2014-02-18 15:37:32 +04:00
public static function prepFileTarget($path) {
return \OC\Share\Share::prepFileTarget($path);
}
2013-05-29 22:11:13 +04:00
/**
2013-10-31 22:00:53 +04:00
* Find which users can access a shared item
2014-02-18 15:37:32 +04:00
* @param $path to the file
2013-10-31 22:00:53 +04:00
* @param $user owner of the file
* @param include owner to the list of users with access to the file
* @return array
* @note $path needs to be relative to user data dir, e.g. 'file.txt'
* not '/admin/data/file.txt'
*/
public static function getUsersSharingFile($path, $user, $includeOwner = false) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getUsersSharingFile($path, $user, $includeOwner);
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Get the items of item type shared with the current user
* @param string Item type
* @param int Format (optional) Format type must be defined by the backend
* @param mixed Parameters (optional)
2013-10-31 22:00:53 +04:00
* @param int Number of items to return (optional) Returns all by default
* @param bool include collections (optional)
2013-10-31 22:00:53 +04:00
* @return Return depends on format
*/
2013-02-11 20:44:02 +04:00
public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE,
$parameters = null, $limit = -1, $includeCollections = false) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemsSharedWith($itemType, $format, $parameters, $limit, $includeCollections);
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Get the item of item type shared with the current user
* @param string $itemType
2013-11-25 19:42:28 +04:00
* @param string $itemTarget
2013-10-31 22:00:53 +04:00
* @param int $format (optional) Format type must be defined by the backend
* @param mixed Parameters (optional)
* @param bool include collections (optional)
2014-02-18 15:37:32 +04:00
* @return Return depends on format
2013-10-31 22:00:53 +04:00
*/
2013-02-11 20:44:02 +04:00
public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE,
$parameters = null, $includeCollections = false) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemSharedWith($itemType, $itemTarget, $format, $parameters, $includeCollections);
2012-10-14 23:04:08 +04:00
}
/**
* Get the item of item type shared with a given user by source
2013-11-25 19:42:28 +04:00
* @param string $itemType
* @param string $itemSource
2013-09-24 21:37:24 +04:00
* @param string $user User user to whom the item was shared
* @return array Return list of items with file_target, permissions and expiration
*/
public static function getItemSharedWithUser($itemType, $itemSource, $user) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemSharedWithUser($itemType, $itemSource, $user);
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Get the item of item type shared with the current user by source
* @param string Item type
* @param string Item source
* @param int Format (optional) Format type must be defined by the backend
* @param mixed Parameters
* @param bool include collections
* @return Return depends on format
*/
2013-02-11 20:44:02 +04:00
public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE,
$parameters = null, $includeCollections = false) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemSharedWithBySource($itemType, $itemSource, $format, $parameters, $includeCollections);
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Get the item of item type shared by a link
* @param string Item type
* @param string Item source
* @param string Owner of link
* @return Item
*/
2012-10-14 23:04:08 +04:00
public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemSharedWithByLink($itemType, $itemSource, $uidOwner);
2012-10-14 23:04:08 +04:00
}
2012-11-12 17:44:00 +04:00
/**
* Based on the given token the share information will be returned - password protected shares will be verified
* @param string $token
* @return array | bool false will be returned in case the token is unknown or unauthorized
2012-11-12 17:44:00 +04:00
*/
public static function getShareByToken($token, $checkPasswordProtection = true) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getShareByToken($token, $checkPasswordProtection);
2012-11-12 17:44:00 +04:00
}
/**
* resolves reshares down to the last real share
* @param $linkItem
* @return $fileOwner
*/
2014-02-18 15:37:32 +04:00
public static function resolveReShare($linkItem) {
return \OC\Share\Share::resolveReShare($linkItem);
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Get the shared items of item type owned by the current user
* @param string Item type
* @param int Format (optional) Format type must be defined by the backend
* @param mixed Parameters
* @param int Number of items to return (optional) Returns all by default
* @param bool include collections
* @return Return depends on format
*/
2013-02-11 20:44:02 +04:00
public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null,
$limit = -1, $includeCollections = false) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemsShared($itemType, $format, $parameters, $limit, $includeCollections);
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Get the shared item of item type owned by the current user
* @param string Item type
* @param string Item source
* @param int Format (optional) Format type must be defined by the backend
* @param mixed Parameters
* @param bool include collections
* @return Return depends on format
*/
2013-02-11 20:44:02 +04:00
public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE,
$parameters = null, $includeCollections = false) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getItemShared($itemType, $itemSource, $format, $parameters, $includeCollections);
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Get all users an item is shared with
* @param string Item type
* @param string Item source
* @param string Owner
* @param bool Include collections
* @praram bool check expire date
2013-10-31 22:00:53 +04:00
* @return Return array of users
*/
public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections, $checkExpireDate);
2012-10-14 23:04:08 +04:00
}
/**
* Share an item with a user, group, or via private link
* @param string $itemType
* @param string $itemSource
* @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @param string $shareWith User or group the item is being shared with
* @param int $permissions CRUDS
* @param null $itemSourceName
* @throws \Exception
* @internal param \OCP\Item $string type
* @internal param \OCP\Item $string source
* @internal param \OCP\SHARE_TYPE_USER $int , SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @internal param \OCP\User $string or group the item is being shared with
* @internal param \OCP\CRUDS $int permissions
* @return bool|string Returns true on success or false on failure, Returns token on success for links
*/
public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName);
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Unshare an item from a user, group, or delete a private link
* @param string Item type
* @param string Item source
* @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @param string User or group the item is being shared with
2014-02-18 15:37:32 +04:00
* @return Returns true on success or false on failure
2013-10-31 22:00:53 +04:00
*/
2012-10-14 23:04:08 +04:00
public static function unshare($itemType, $itemSource, $shareType, $shareWith) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::unshare($itemType, $itemSource, $shareType, $shareWith);
2012-10-14 23:04:08 +04:00
}
2012-10-16 23:38:52 +04:00
/**
2013-10-31 22:00:53 +04:00
* Unshare an item from all users, groups, and remove all links
* @param string Item type
* @param string Item source
2014-02-18 15:37:32 +04:00
* @return Returns true on success or false on failure
2013-10-31 22:00:53 +04:00
*/
2012-10-16 23:38:52 +04:00
public static function unshareAll($itemType, $itemSource) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::unshareAll($itemType, $itemSource);
2012-10-16 23:38:52 +04:00
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Unshare an item shared with the current user
* @param string Item type
* @param string Item target
2014-02-18 15:37:32 +04:00
* @return Returns true on success or false on failure
2013-10-31 22:00:53 +04:00
*
* Unsharing from self is not allowed for items inside collections
*/
2012-10-14 23:04:08 +04:00
public static function unshareFromSelf($itemType, $itemTarget) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::unshareFromSelf($itemType, $itemTarget);
2012-10-14 23:04:08 +04:00
}
/**
* sent status if users got informed by mail about share
* @param string $itemType
* @param string $itemSource
* @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @param bool $status
*/
public static function setSendMailStatus($itemType, $itemSource, $shareType, $status) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::setSendMailStatus($itemType, $itemSource, $shareType, $status);
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Set the permissions of an item for a specific user or group
2014-02-18 15:37:32 +04:00
* @param string Item type
* @param string Item source
* @param int SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK
* @param string User or group the item is being shared with
* @param int CRUDS permissions
* @return Returns true on success or false on failure
2013-10-31 22:00:53 +04:00
*/
2012-10-14 23:04:08 +04:00
public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions);
2012-10-14 23:04:08 +04:00
}
2013-10-31 22:00:53 +04:00
/**
* Set expiration date for a share
* @param string $itemType
* @param string $itemSource
* @param string $date expiration date
2014-02-18 15:37:32 +04:00
* @return Share_Backend
2013-10-31 22:00:53 +04:00
*/
2012-10-14 23:04:08 +04:00
public static function setExpirationDate($itemType, $itemSource, $date) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::setExpirationDate($itemType, $itemSource, $date);
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Get the backend class for the specified item type
* @param string $itemType
* @return Share_Backend
*/
2013-07-26 16:11:59 +04:00
public static function getBackend($itemType) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::getBackend($itemType);
2012-10-14 23:04:08 +04:00
}
/**
* Delete all shares with type SHARE_TYPE_LINK
*/
public static function removeAllLinkShares() {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::removeAllLinkShares();
}
2012-10-14 23:04:08 +04:00
/**
2013-10-31 22:00:53 +04:00
* Hook Listeners
*/
2012-10-14 23:04:08 +04:00
2013-10-31 22:00:53 +04:00
/**
* Function that is called after a user is deleted. Cleans up the shares of that user.
2013-10-31 22:00:53 +04:00
* @param array arguments
*/
2012-10-14 23:04:08 +04:00
public static function post_deleteUser($arguments) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::post_deleteUser($arguments);
2012-10-14 23:04:08 +04:00
}
2013-10-31 22:00:53 +04:00
/**
* Function that is called after a user is added to a group.
* TODO what does it do?
2013-10-31 22:00:53 +04:00
* @param array arguments
*/
2012-10-14 23:04:08 +04:00
public static function post_addToGroup($arguments) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::post_addToGroup($arguments);
2012-10-14 23:04:08 +04:00
}
2013-10-31 22:00:53 +04:00
/**
2013-11-27 13:32:43 +04:00
* Function that is called after a user is removed from a group. Shares are cleaned up.
2013-10-31 22:00:53 +04:00
* @param array arguments
*/
2012-10-14 23:04:08 +04:00
public static function post_removeFromGroup($arguments) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::post_removeFromGroup($arguments);
2012-10-14 23:04:08 +04:00
}
2013-10-31 22:00:53 +04:00
/**
* Function that is called after a group is removed. Cleans up the shares to that group.
2013-10-31 22:00:53 +04:00
* @param array arguments
*/
2012-10-14 23:04:08 +04:00
public static function post_deleteGroup($arguments) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::post_deleteGroup($arguments);
2012-10-14 23:04:08 +04:00
}
/**
* In case a password protected link is not yet authenticated this function will return false
*
* @param array $linkItem
* @return bool
*/
public static function checkPasswordProtectedShare(array $linkItem) {
2014-02-18 15:37:32 +04:00
return \OC\Share\Share::checkPasswordProtectedShare($linkItem);
}
2012-10-14 23:04:08 +04:00
}
/**
2013-10-31 22:00:53 +04:00
* Interface that apps must implement to share content.
*/
2012-10-14 23:04:08 +04:00
interface Share_Backend {
/**
2013-10-31 22:00:53 +04:00
* Get the source of the item to be stored in the database
* @param string Item source
* @param string Owner of the item
2014-02-18 15:37:32 +04:00
* @return mixed|array|false Source
2013-10-31 22:00:53 +04:00
*
* Return an array if the item is file dependent, the array needs two keys: 'item' and 'file'
* Return false if the item does not exist for the user
*
* The formatItems() function will translate the source returned back into the item
*/
2012-10-14 23:04:08 +04:00
public function isValidSource($itemSource, $uidOwner);
/**
2013-10-31 22:00:53 +04:00
* Get a unique name of the item for the specified user
* @param string Item source
* @param string|false User the item is being shared with
* @param array|null List of similar item names already existing as shared items
* @return string Target name
*
* This function needs to verify that the user does not already have an item with this name.
* If it does generate a new name e.g. name_#
*/
2012-10-14 23:04:08 +04:00
public function generateTarget($itemSource, $shareWith, $exclude = null);
/**
2013-10-31 22:00:53 +04:00
* Converts the shared item sources back into the item in the specified format
2014-02-18 15:37:32 +04:00
* @param array Shared items
* @param int Format
* @return TODO
2013-10-31 22:00:53 +04:00
*
* The items array is a 3-dimensional array with the item_source as the
* first key and the share id as the second key to an array with the share
* info.
*
* The key/value pairs included in the share info depend on the function originally called:
* If called by getItem(s)Shared: id, item_type, item, item_source,
* share_type, share_with, permissions, stime, file_source
*
* If called by getItem(s)SharedWith: id, item_type, item, item_source,
* item_target, share_type, share_with, permissions, stime, file_source,
* file_target
*
* This function allows the backend to control the output of shared items with custom formats.
* It is only called through calls to the public getItem(s)Shared(With) functions.
*/
2012-10-14 23:04:08 +04:00
public function formatItems($items, $format, $parameters = null);
}
/**
2013-10-31 22:00:53 +04:00
* Interface for share backends that share content that is dependent on files.
* Extends the Share_Backend interface.
*/
2012-10-14 23:04:08 +04:00
interface Share_Backend_File_Dependent extends Share_Backend {
/**
2013-10-31 22:00:53 +04:00
* Get the file path of the item
* @param string Item source
* @param string User that is the owner of shared item
2013-10-31 22:00:53 +04:00
*/
2012-10-14 23:04:08 +04:00
public function getFilePath($itemSource, $uidOwner);
}
/**
2013-10-31 22:00:53 +04:00
* Interface for collections of of items implemented by another share backend.
* Extends the Share_Backend interface.
*/
2012-10-14 23:04:08 +04:00
interface Share_Backend_Collection extends Share_Backend {
/**
2013-10-31 22:00:53 +04:00
* Get the sources of the children of the item
* @param string Item source
* @return array Returns an array of children each inside an array with the keys: source, target, and file_path if applicable
*/
2012-10-14 23:04:08 +04:00
public function getChildren($itemSource);
}