|
|
|
|
@ -251,85 +251,6 @@ class Share extends Constants { |
|
|
|
|
null, -1, $includeCollections); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unshare an item from a user, group, or delete a 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 string $owner owner of the share, if null the current user is used |
|
|
|
|
* @return boolean true on success or false on failure |
|
|
|
|
*/ |
|
|
|
|
public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
|
|
|
|
|
|
|
|
// check if it is a valid itemType |
|
|
|
|
self::getBackend($itemType); |
|
|
|
|
|
|
|
|
|
$items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
|
|
|
|
|
|
|
|
$toDelete = []; |
|
|
|
|
$newParent = null; |
|
|
|
|
$currentUser = $owner ? $owner : \OC_User::getUser(); |
|
|
|
|
foreach ($items as $item) { |
|
|
|
|
// delete the item with the expected share_type and owner |
|
|
|
|
if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
|
|
|
$toDelete = $item; |
|
|
|
|
// if there is more then one result we don't have to delete the children |
|
|
|
|
// but update their parent. For group shares the new parent should always be |
|
|
|
|
// the original group share and not the db entry with the unique name |
|
|
|
|
} elseif ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
|
|
|
$newParent = $item['parent']; |
|
|
|
|
} else { |
|
|
|
|
$newParent = $item['id']; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!empty($toDelete)) { |
|
|
|
|
self::unshareItem($toDelete, $newParent); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Unshares a share given a share data array |
|
|
|
|
* @param array $item Share data (usually database row) |
|
|
|
|
* @param int $newParent parent ID |
|
|
|
|
* @return null |
|
|
|
|
*/ |
|
|
|
|
protected static function unshareItem(array $item, $newParent = null) { |
|
|
|
|
$shareType = (int)$item['share_type']; |
|
|
|
|
$shareWith = null; |
|
|
|
|
if ($shareType !== IShare::TYPE_LINK) { |
|
|
|
|
$shareWith = $item['share_with']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Pass all the vars we have for now, they may be useful |
|
|
|
|
$hookParams = [ |
|
|
|
|
'id' => $item['id'], |
|
|
|
|
'itemType' => $item['item_type'], |
|
|
|
|
'itemSource' => $item['item_source'], |
|
|
|
|
'shareType' => $shareType, |
|
|
|
|
'shareWith' => $shareWith, |
|
|
|
|
'itemParent' => $item['parent'], |
|
|
|
|
'uidOwner' => $item['uid_owner'], |
|
|
|
|
]; |
|
|
|
|
if ($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
|
|
|
$hookParams['fileSource'] = $item['file_source']; |
|
|
|
|
$hookParams['fileTarget'] = $item['file_target']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
\OC_Hook::emit(\OCP\Share::class, 'pre_unshare', $hookParams); |
|
|
|
|
$deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
|
|
|
$deletedShares[] = $hookParams; |
|
|
|
|
$hookParams['deletedShares'] = $deletedShares; |
|
|
|
|
\OC_Hook::emit(\OCP\Share::class, 'post_unshare', $hookParams); |
|
|
|
|
if ((int)$item['share_type'] === IShare::TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
|
|
|
list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
|
|
|
self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Get the backend class for the specified item type |
|
|
|
|
* @param string $itemType |
|
|
|
|
@ -964,65 +885,6 @@ class Share extends Constants { |
|
|
|
|
return $url; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* try http post first with https and then with http as a fallback |
|
|
|
|
* |
|
|
|
|
* @param string $remoteDomain |
|
|
|
|
* @param string $urlSuffix |
|
|
|
|
* @param array $fields post parameters |
|
|
|
|
* @return array |
|
|
|
|
*/ |
|
|
|
|
private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
|
|
|
$protocol = 'https://'; |
|
|
|
|
$result = [ |
|
|
|
|
'success' => false, |
|
|
|
|
'result' => '', |
|
|
|
|
]; |
|
|
|
|
$try = 0; |
|
|
|
|
$discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
|
|
|
while ($result['success'] === false && $try < 2) { |
|
|
|
|
$federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
|
|
|
$endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
|
|
|
$client = \OC::$server->getHTTPClientService()->newClient(); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
$response = $client->post( |
|
|
|
|
$protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, |
|
|
|
|
[ |
|
|
|
|
'body' => $fields, |
|
|
|
|
'connect_timeout' => 10, |
|
|
|
|
] |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
$result = ['success' => true, 'result' => $response->getBody()]; |
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
$result = ['success' => false, 'result' => $e->getMessage()]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$try++; |
|
|
|
|
$protocol = 'http://'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $result; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* send server-to-server unshare to remote server |
|
|
|
|
* |
|
|
|
|
* @param string $remote url |
|
|
|
|
* @param int $id share id |
|
|
|
|
* @param string $token |
|
|
|
|
* @return bool |
|
|
|
|
*/ |
|
|
|
|
private static function sendRemoteUnshare($remote, $id, $token) { |
|
|
|
|
$url = rtrim($remote, '/'); |
|
|
|
|
$fields = ['token' => $token, 'format' => 'json']; |
|
|
|
|
$url = self::removeProtocolFromUrl($url); |
|
|
|
|
$result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
|
|
|
$status = json_decode($result['result'], true); |
|
|
|
|
|
|
|
|
|
return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @return int |
|
|
|
|
|