/** * Unshare an item shared with the current user * @param string $itemType * @param string $itemOrigin Item target or source * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional) * @return boolean true on success or false on failure * * Unsharing from self is not allowed for items inside collections */ public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) { $originType = $originIsSource ? 'source' : 'target'; $uid = \OCP\User::getUser(); if ($itemType === 'file' || $itemType === 'folder') { $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?'; } else { $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?'; } $query = \OCP\DB::prepare($statement); $result = $query->execute(array($itemType, $itemOrigin)); $shares = $result->fetchAll(); $listOfUnsharedItems = array(); $itemUnshared = false; foreach ($shares as $share) { if ((int) $share['share_type'] === \OCP\Share::SHARE_TYPE_USER && $share['share_with'] === $uid) { $deletedShares = Helper::delete($share['id']); $shareTmp = array('id' => $share['id'], 'shareWith' => $share['share_with'], 'itemTarget' => $share['item_target'], 'itemType' => $share['item_type'], 'shareType' => (int) $share['share_type']); if (isset($share['file_target'])) { $shareTmp['fileTarget'] = $share['file_target']; } $listOfUnsharedItems = array_merge($listOfUnsharedItems, $deletedShares, array($shareTmp)); $itemUnshared = true; break; } elseif ((int) $share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { if (\OC_Group::inGroup($uid, $share['share_with'])) { $groupShare = $share; } } elseif ((int) $share['share_type'] === \OCP\Share::SHARE_TYPE_SHARING_GROUP) { if (\OCA\Sharing_Group\Data::inGroup($uid, $share['share_with'])) { $groupShare = $share; } } elseif ((int) $share['share_type'] === self::$shareTypeGroupUserUnique && $share['share_with'] === $uid) { $uniqueGroupShare = $share; } } if (!$itemUnshared && isset($groupShare) && !isset($uniqueGroupShare)) { $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' . ' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,' . ' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)' . ' VALUES (?,?,?,?,?,?,?,?,?,?,?)'); $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'], $groupShare['id'], self::$shareTypeGroupUserUnique, \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'], $groupShare['file_target'])); $shareTmp = array('id' => $groupShare['id'], 'shareWith' => $groupShare['share_with'], 'itemTarget' => $groupShare['item_target'], 'itemType' => $groupShare['item_type'], 'shareType' => (int) $groupShare['share_type']); if (isset($groupShare['file_target'])) { $shareTmp['fileTarget'] = $groupShare['file_target']; } $listOfUnsharedItems = array_merge($listOfUnsharedItems, array($groupShare)); $itemUnshared = true; } elseif (!$itemUnshared && isset($uniqueGroupShare)) { $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); $query->execute(array(0, $uniqueGroupShare['id'])); $shareTmp = array('id' => $uniqueGroupShare['id'], 'shareWith' => $uniqueGroupShare['share_with'], 'itemTarget' => $uniqueGroupShare['item_target'], 'itemType' => $uniqueGroupShare['item_type'], 'shareType' => (int) $uniqueGroupShare['share_type']); if (isset($uniqueGroupShare['file_target'])) { $shareTmp['fileTarget'] = $uniqueGroupShare['file_target']; } $listOfUnsharedItems = array_merge($listOfUnsharedItems, array($uniqueGroupShare)); $itemUnshared = true; } if ($itemUnshared) { \OC_Hook::emit('OCP\\Share', 'post_unshareFromSelf', array('unsharedItems' => $listOfUnsharedItems, 'itemType' => $itemType)); } return $itemUnshared; }