@@ -53,1500 +53,1500 @@ |
||
| 53 | 53 | */ |
| 54 | 54 | class Share extends Constants { |
| 55 | 55 | |
| 56 | - /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask |
|
| 57 | - * Construct permissions for share() and setPermissions with Or (|) e.g. |
|
| 58 | - * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE |
|
| 59 | - * |
|
| 60 | - * Check if permission is granted with And (&) e.g. Check if delete is |
|
| 61 | - * granted: if ($permissions & PERMISSION_DELETE) |
|
| 62 | - * |
|
| 63 | - * Remove permissions with And (&) and Not (~) e.g. Remove the update |
|
| 64 | - * permission: $permissions &= ~PERMISSION_UPDATE |
|
| 65 | - * |
|
| 66 | - * Apps are required to handle permissions on their own, this class only |
|
| 67 | - * stores and manages the permissions of shares |
|
| 68 | - * @see lib/public/constants.php |
|
| 69 | - */ |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Register a sharing backend class that implements OCP\Share_Backend for an item type |
|
| 73 | - * @param string $itemType Item type |
|
| 74 | - * @param string $class Backend class |
|
| 75 | - * @param string $collectionOf (optional) Depends on item type |
|
| 76 | - * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files |
|
| 77 | - * @return boolean true if backend is registered or false if error |
|
| 78 | - */ |
|
| 79 | - public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { |
|
| 80 | - if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') == 'yes') { |
|
| 81 | - if (!isset(self::$backendTypes[$itemType])) { |
|
| 82 | - self::$backendTypes[$itemType] = array( |
|
| 83 | - 'class' => $class, |
|
| 84 | - 'collectionOf' => $collectionOf, |
|
| 85 | - 'supportedFileExtensions' => $supportedFileExtensions |
|
| 86 | - ); |
|
| 87 | - return true; |
|
| 88 | - } |
|
| 89 | - \OCP\Util::writeLog('OCP\Share', |
|
| 90 | - 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'] |
|
| 91 | - .' is already registered for '.$itemType, |
|
| 92 | - ILogger::WARN); |
|
| 93 | - } |
|
| 94 | - return false; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Get the items of item type shared with the current user |
|
| 99 | - * @param string $itemType |
|
| 100 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 101 | - * @param mixed $parameters (optional) |
|
| 102 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 103 | - * @param boolean $includeCollections (optional) |
|
| 104 | - * @return mixed Return depends on format |
|
| 105 | - * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 106 | - * called like this: |
|
| 107 | - * \OC\Share\Share::getItemsSharedWith('folder'); (apps/files_sharing/tests/UpdaterTest.php) |
|
| 108 | - */ |
|
| 109 | - public static function getItemsSharedWith() { |
|
| 110 | - return self::getItems('folder', null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, self::FORMAT_NONE, |
|
| 111 | - null, -1, false); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the items of item type shared with a user |
|
| 116 | - * @param string $itemType |
|
| 117 | - * @param string $user id for which user we want the shares |
|
| 118 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 119 | - * @param mixed $parameters (optional) |
|
| 120 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 121 | - * @param boolean $includeCollections (optional) |
|
| 122 | - * @return mixed Return depends on format |
|
| 123 | - */ |
|
| 124 | - public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE, |
|
| 125 | - $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 126 | - return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format, |
|
| 127 | - $parameters, $limit, $includeCollections); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Get the item of item type shared with a given user by source |
|
| 132 | - * @param string $itemType |
|
| 133 | - * @param string $itemSource |
|
| 134 | - * @param string $user User to whom the item was shared |
|
| 135 | - * @param string $owner Owner of the share |
|
| 136 | - * @param int $shareType only look for a specific share type |
|
| 137 | - * @return array Return list of items with file_target, permissions and expiration |
|
| 138 | - */ |
|
| 139 | - public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) { |
|
| 140 | - $shares = array(); |
|
| 141 | - $fileDependent = false; |
|
| 142 | - |
|
| 143 | - $where = 'WHERE'; |
|
| 144 | - $fileDependentWhere = ''; |
|
| 145 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 146 | - $fileDependent = true; |
|
| 147 | - $column = 'file_source'; |
|
| 148 | - $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 149 | - $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 150 | - } else { |
|
| 151 | - $column = 'item_source'; |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
|
| 155 | - |
|
| 156 | - $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 157 | - $arguments = array($itemSource, $itemType); |
|
| 158 | - // for link shares $user === null |
|
| 159 | - if ($user !== null) { |
|
| 160 | - $where .= ' AND `share_with` = ? '; |
|
| 161 | - $arguments[] = $user; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if ($shareType !== null) { |
|
| 165 | - $where .= ' AND `share_type` = ? '; |
|
| 166 | - $arguments[] = $shareType; |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - if ($owner !== null) { |
|
| 170 | - $where .= ' AND `uid_owner` = ? '; |
|
| 171 | - $arguments[] = $owner; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 175 | - |
|
| 176 | - $result = \OC_DB::executeAudited($query, $arguments); |
|
| 177 | - |
|
| 178 | - while ($row = $result->fetchRow()) { |
|
| 179 | - if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 180 | - continue; |
|
| 181 | - } |
|
| 182 | - if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 183 | - // if it is a mount point we need to get the path from the mount manager |
|
| 184 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 185 | - $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
|
| 186 | - if (!empty($mountPoint)) { |
|
| 187 | - $path = $mountPoint[0]->getMountPoint(); |
|
| 188 | - $path = trim($path, '/'); |
|
| 189 | - $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt` |
|
| 190 | - $row['path'] = $path; |
|
| 191 | - } else { |
|
| 192 | - \OC::$server->getLogger()->warning( |
|
| 193 | - 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 194 | - ['app' => 'OCP\Share'] |
|
| 195 | - ); |
|
| 196 | - } |
|
| 197 | - } |
|
| 198 | - $shares[] = $row; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - //if didn't found a result than let's look for a group share. |
|
| 202 | - if(empty($shares) && $user !== null) { |
|
| 203 | - $userObject = \OC::$server->getUserManager()->get($user); |
|
| 204 | - $groups = []; |
|
| 205 | - if ($userObject) { |
|
| 206 | - $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject); |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - if (!empty($groups)) { |
|
| 210 | - $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 211 | - $arguments = array($itemSource, $itemType, $groups); |
|
| 212 | - $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 213 | - |
|
| 214 | - if ($owner !== null) { |
|
| 215 | - $where .= ' AND `uid_owner` = ?'; |
|
| 216 | - $arguments[] = $owner; |
|
| 217 | - $types[] = null; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - // TODO: inject connection, hopefully one day in the future when this |
|
| 221 | - // class isn't static anymore... |
|
| 222 | - $conn = \OC::$server->getDatabaseConnection(); |
|
| 223 | - $result = $conn->executeQuery( |
|
| 224 | - 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 225 | - $arguments, |
|
| 226 | - $types |
|
| 227 | - ); |
|
| 228 | - |
|
| 229 | - while ($row = $result->fetch()) { |
|
| 230 | - $shares[] = $row; |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - |
|
| 235 | - return $shares; |
|
| 236 | - |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Get the item of item type shared with the current user by source |
|
| 241 | - * @param string $itemType |
|
| 242 | - * @param string $itemSource |
|
| 243 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 244 | - * @param mixed $parameters |
|
| 245 | - * @param boolean $includeCollections |
|
| 246 | - * @param string $shareWith (optional) define against which user should be checked, default: current user |
|
| 247 | - * @return array |
|
| 248 | - */ |
|
| 249 | - public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 250 | - $parameters = null, $includeCollections = false, $shareWith = null) { |
|
| 251 | - $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith; |
|
| 252 | - return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format, |
|
| 253 | - $parameters, 1, $includeCollections, true); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Get the shared item of item type owned by the current user |
|
| 258 | - * @param string $itemType |
|
| 259 | - * @param string $itemSource |
|
| 260 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 261 | - * @param mixed $parameters |
|
| 262 | - * @param boolean $includeCollections |
|
| 263 | - * @return mixed Return depends on format |
|
| 264 | - */ |
|
| 265 | - public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 266 | - $parameters = null, $includeCollections = false) { |
|
| 267 | - return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, |
|
| 268 | - $parameters, -1, $includeCollections); |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Share an item with a user, group, or via private link |
|
| 273 | - * @param string $itemType |
|
| 274 | - * @param string $itemSource |
|
| 275 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 276 | - * @param string $shareWith User or group the item is being shared with |
|
| 277 | - * @param int $permissions CRUDS |
|
| 278 | - * @param string $itemSourceName |
|
| 279 | - * @param \DateTime|null $expirationDate |
|
| 280 | - * @return boolean|string Returns true on success or false on failure, Returns token on success for links |
|
| 281 | - * @throws \OC\HintException when the share type is remote and the shareWith is invalid |
|
| 282 | - * @throws \Exception |
|
| 283 | - * @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0, parameter $passwordChanged added in 9.0.0 |
|
| 284 | - * @deprecated 14.0.0 TESTS ONLY - this methods is as of 2018-06 only used by tests |
|
| 285 | - * called like this: |
|
| 286 | - * \OC\Share\Share::shareItem('test', 1, \OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ); |
|
| 287 | - */ |
|
| 288 | - public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions) { |
|
| 289 | - $backend = self::getBackend($itemType); |
|
| 290 | - |
|
| 291 | - if ($backend->isShareTypeAllowed($shareType) === false) { |
|
| 292 | - $message = 'Sharing failed, because the backend does not allow shares from type %i'; |
|
| 293 | - throw new \Exception(sprintf($message, $shareType)); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - $uidOwner = \OC_User::getUser(); |
|
| 297 | - |
|
| 298 | - // Verify share type and sharing conditions are met |
|
| 299 | - if ($shareType === self::SHARE_TYPE_USER) { |
|
| 300 | - if ($shareWith == $uidOwner) { |
|
| 301 | - $message = 'Sharing failed, because you can not share with yourself'; |
|
| 302 | - throw new \Exception($message); |
|
| 303 | - } |
|
| 304 | - if (!\OC::$server->getUserManager()->userExists($shareWith)) { |
|
| 305 | - $message = 'Sharing failed, because the user %s does not exist'; |
|
| 306 | - throw new \Exception(sprintf($message, $shareWith)); |
|
| 307 | - } |
|
| 308 | - // Check if the item source is already shared with the user, either from the same owner or a different user |
|
| 309 | - if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, |
|
| 310 | - $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 311 | - // Only allow the same share to occur again if it is the same |
|
| 312 | - // owner and is not a user share, this use case is for increasing |
|
| 313 | - // permissions for a specific user |
|
| 314 | - if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 315 | - $message = 'Sharing failed, because this item is already shared with %s'; |
|
| 316 | - throw new \Exception(sprintf($message, $shareWith)); |
|
| 317 | - } |
|
| 318 | - } |
|
| 319 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, |
|
| 320 | - $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 321 | - // Only allow the same share to occur again if it is the same |
|
| 322 | - // owner and is not a user share, this use case is for increasing |
|
| 323 | - // permissions for a specific user |
|
| 324 | - if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 325 | - $message = 'Sharing failed, because this item is already shared with user %s'; |
|
| 326 | - throw new \Exception(sprintf($message, $shareWith)); |
|
| 327 | - } |
|
| 328 | - } |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - // Put the item into the database |
|
| 332 | - $result = self::put('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions); |
|
| 333 | - |
|
| 334 | - return $result ? true : false; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Unshare an item from a user, group, or delete a private link |
|
| 339 | - * @param string $itemType |
|
| 340 | - * @param string $itemSource |
|
| 341 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 342 | - * @param string $shareWith User or group the item is being shared with |
|
| 343 | - * @param string $owner owner of the share, if null the current user is used |
|
| 344 | - * @return boolean true on success or false on failure |
|
| 345 | - */ |
|
| 346 | - public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
| 347 | - |
|
| 348 | - // check if it is a valid itemType |
|
| 349 | - self::getBackend($itemType); |
|
| 350 | - |
|
| 351 | - $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
| 352 | - |
|
| 353 | - $toDelete = array(); |
|
| 354 | - $newParent = null; |
|
| 355 | - $currentUser = $owner ? $owner : \OC_User::getUser(); |
|
| 356 | - foreach ($items as $item) { |
|
| 357 | - // delete the item with the expected share_type and owner |
|
| 358 | - if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 359 | - $toDelete = $item; |
|
| 360 | - // if there is more then one result we don't have to delete the children |
|
| 361 | - // but update their parent. For group shares the new parent should always be |
|
| 362 | - // the original group share and not the db entry with the unique name |
|
| 363 | - } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 364 | - $newParent = $item['parent']; |
|
| 365 | - } else { |
|
| 366 | - $newParent = $item['id']; |
|
| 367 | - } |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - if (!empty($toDelete)) { |
|
| 371 | - self::unshareItem($toDelete, $newParent); |
|
| 372 | - return true; |
|
| 373 | - } |
|
| 374 | - return false; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - /** |
|
| 378 | - * Checks whether a share has expired, calls unshareItem() if yes. |
|
| 379 | - * @param array $item Share data (usually database row) |
|
| 380 | - * @return boolean True if item was expired, false otherwise. |
|
| 381 | - */ |
|
| 382 | - protected static function expireItem(array $item) { |
|
| 383 | - |
|
| 384 | - $result = false; |
|
| 385 | - |
|
| 386 | - // only use default expiration date for link shares |
|
| 387 | - if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 388 | - |
|
| 389 | - // calculate expiration date |
|
| 390 | - if (!empty($item['expiration'])) { |
|
| 391 | - $userDefinedExpire = new \DateTime($item['expiration']); |
|
| 392 | - $expires = $userDefinedExpire->getTimestamp(); |
|
| 393 | - } else { |
|
| 394 | - $expires = null; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - |
|
| 398 | - // get default expiration settings |
|
| 399 | - $defaultSettings = Helper::getDefaultExpireSetting(); |
|
| 400 | - $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); |
|
| 401 | - |
|
| 402 | - |
|
| 403 | - if (is_int($expires)) { |
|
| 404 | - $now = time(); |
|
| 405 | - if ($now > $expires) { |
|
| 406 | - self::unshareItem($item); |
|
| 407 | - $result = true; |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - } |
|
| 411 | - return $result; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - /** |
|
| 415 | - * Unshares a share given a share data array |
|
| 416 | - * @param array $item Share data (usually database row) |
|
| 417 | - * @param int $newParent parent ID |
|
| 418 | - * @return null |
|
| 419 | - */ |
|
| 420 | - protected static function unshareItem(array $item, $newParent = null) { |
|
| 421 | - |
|
| 422 | - $shareType = (int)$item['share_type']; |
|
| 423 | - $shareWith = null; |
|
| 424 | - if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
|
| 425 | - $shareWith = $item['share_with']; |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - // Pass all the vars we have for now, they may be useful |
|
| 429 | - $hookParams = array( |
|
| 430 | - 'id' => $item['id'], |
|
| 431 | - 'itemType' => $item['item_type'], |
|
| 432 | - 'itemSource' => $item['item_source'], |
|
| 433 | - 'shareType' => $shareType, |
|
| 434 | - 'shareWith' => $shareWith, |
|
| 435 | - 'itemParent' => $item['parent'], |
|
| 436 | - 'uidOwner' => $item['uid_owner'], |
|
| 437 | - ); |
|
| 438 | - if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 439 | - $hookParams['fileSource'] = $item['file_source']; |
|
| 440 | - $hookParams['fileTarget'] = $item['file_target']; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - \OC_Hook::emit(\OCP\Share::class, 'pre_unshare', $hookParams); |
|
| 444 | - $deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
| 445 | - $deletedShares[] = $hookParams; |
|
| 446 | - $hookParams['deletedShares'] = $deletedShares; |
|
| 447 | - \OC_Hook::emit(\OCP\Share::class, 'post_unshare', $hookParams); |
|
| 448 | - if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 449 | - list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
| 450 | - self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
| 451 | - } |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Get the backend class for the specified item type |
|
| 456 | - * @param string $itemType |
|
| 457 | - * @throws \Exception |
|
| 458 | - * @return \OCP\Share_Backend |
|
| 459 | - */ |
|
| 460 | - public static function getBackend($itemType) { |
|
| 461 | - $l = \OC::$server->getL10N('lib'); |
|
| 462 | - if (isset(self::$backends[$itemType])) { |
|
| 463 | - return self::$backends[$itemType]; |
|
| 464 | - } else if (isset(self::$backendTypes[$itemType]['class'])) { |
|
| 465 | - $class = self::$backendTypes[$itemType]['class']; |
|
| 466 | - if (class_exists($class)) { |
|
| 467 | - self::$backends[$itemType] = new $class; |
|
| 468 | - if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) { |
|
| 469 | - $message = 'Sharing backend %s must implement the interface OCP\Share_Backend'; |
|
| 470 | - $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class)); |
|
| 471 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), ILogger::ERROR); |
|
| 472 | - throw new \Exception($message_t); |
|
| 473 | - } |
|
| 474 | - return self::$backends[$itemType]; |
|
| 475 | - } else { |
|
| 476 | - $message = 'Sharing backend %s not found'; |
|
| 477 | - $message_t = $l->t('Sharing backend %s not found', array($class)); |
|
| 478 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), ILogger::ERROR); |
|
| 479 | - throw new \Exception($message_t); |
|
| 480 | - } |
|
| 481 | - } |
|
| 482 | - $message = 'Sharing backend for %s not found'; |
|
| 483 | - $message_t = $l->t('Sharing backend for %s not found', array($itemType)); |
|
| 484 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), ILogger::ERROR); |
|
| 485 | - throw new \Exception($message_t); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Check if resharing is allowed |
|
| 490 | - * @return boolean true if allowed or false |
|
| 491 | - * |
|
| 492 | - * Resharing is allowed by default if not configured |
|
| 493 | - */ |
|
| 494 | - public static function isResharingAllowed() { |
|
| 495 | - if (!isset(self::$isResharingAllowed)) { |
|
| 496 | - if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') { |
|
| 497 | - self::$isResharingAllowed = true; |
|
| 498 | - } else { |
|
| 499 | - self::$isResharingAllowed = false; |
|
| 500 | - } |
|
| 501 | - } |
|
| 502 | - return self::$isResharingAllowed; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Get a list of collection item types for the specified item type |
|
| 507 | - * @param string $itemType |
|
| 508 | - * @return array |
|
| 509 | - */ |
|
| 510 | - private static function getCollectionItemTypes($itemType) { |
|
| 511 | - $collectionTypes = array($itemType); |
|
| 512 | - foreach (self::$backendTypes as $type => $backend) { |
|
| 513 | - if (in_array($backend['collectionOf'], $collectionTypes)) { |
|
| 514 | - $collectionTypes[] = $type; |
|
| 515 | - } |
|
| 516 | - } |
|
| 517 | - // TODO Add option for collections to be collection of themselves, only 'folder' does it now... |
|
| 518 | - if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) { |
|
| 519 | - unset($collectionTypes[0]); |
|
| 520 | - } |
|
| 521 | - // Return array if collections were found or the item type is a |
|
| 522 | - // collection itself - collections can be inside collections |
|
| 523 | - if (count($collectionTypes) > 0) { |
|
| 524 | - return $collectionTypes; |
|
| 525 | - } |
|
| 526 | - return false; |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Get the owners of items shared with a user. |
|
| 531 | - * |
|
| 532 | - * @param string $user The user the items are shared with. |
|
| 533 | - * @param string $type The type of the items shared with the user. |
|
| 534 | - * @param boolean $includeCollections Include collection item types (optional) |
|
| 535 | - * @param boolean $includeOwner include owner in the list of users the item is shared with (optional) |
|
| 536 | - * @return array |
|
| 537 | - * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 538 | - * called like this: |
|
| 539 | - * \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true) |
|
| 540 | - */ |
|
| 541 | - public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) { |
|
| 542 | - // First, we find out if $type is part of a collection (and if that collection is part of |
|
| 543 | - // another one and so on). |
|
| 544 | - $collectionTypes = array(); |
|
| 545 | - if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) { |
|
| 546 | - $collectionTypes[] = $type; |
|
| 547 | - } |
|
| 548 | - |
|
| 549 | - // Of these collection types, along with our original $type, we make a |
|
| 550 | - // list of the ones for which a sharing backend has been registered. |
|
| 551 | - // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it |
|
| 552 | - // with its $includeCollections parameter set to true. Unfortunately, this fails currently. |
|
| 553 | - $allMaybeSharedItems = array(); |
|
| 554 | - foreach ($collectionTypes as $collectionType) { |
|
| 555 | - if (isset(self::$backends[$collectionType])) { |
|
| 556 | - $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser( |
|
| 557 | - $collectionType, |
|
| 558 | - $user, |
|
| 559 | - self::FORMAT_NONE |
|
| 560 | - ); |
|
| 561 | - } |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - $owners = array(); |
|
| 565 | - if ($includeOwner) { |
|
| 566 | - $owners[] = $user; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - // We take a look at all shared items of the given $type (or of the collections it is part of) |
|
| 570 | - // and find out their owners. Then, we gather the tags for the original $type from all owners, |
|
| 571 | - // and return them as elements of a list that look like "Tag (owner)". |
|
| 572 | - foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) { |
|
| 573 | - foreach ($maybeSharedItems as $sharedItem) { |
|
| 574 | - if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814 |
|
| 575 | - $owners[] = $sharedItem['uid_owner']; |
|
| 576 | - } |
|
| 577 | - } |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - return $owners; |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - /** |
|
| 584 | - * Get shared items from the database |
|
| 585 | - * @param string $itemType |
|
| 586 | - * @param string $item Item source or target (optional) |
|
| 587 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique |
|
| 588 | - * @param string $shareWith User or group the item is being shared with |
|
| 589 | - * @param string $uidOwner User that is the owner of shared items (optional) |
|
| 590 | - * @param int $format Format to convert items to with formatItems() (optional) |
|
| 591 | - * @param mixed $parameters to pass to formatItems() (optional) |
|
| 592 | - * @param int $limit Number of items to return, -1 to return all matches (optional) |
|
| 593 | - * @param boolean $includeCollections Include collection item types (optional) |
|
| 594 | - * @param boolean $itemShareWithBySource (optional) |
|
| 595 | - * @param boolean $checkExpireDate |
|
| 596 | - * @return array |
|
| 597 | - * |
|
| 598 | - * See public functions getItem(s)... for parameter usage |
|
| 599 | - * |
|
| 600 | - */ |
|
| 601 | - public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, |
|
| 602 | - $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, |
|
| 603 | - $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) { |
|
| 604 | - if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') != 'yes') { |
|
| 605 | - return array(); |
|
| 606 | - } |
|
| 607 | - $backend = self::getBackend($itemType); |
|
| 608 | - $collectionTypes = false; |
|
| 609 | - // Get filesystem root to add it to the file target and remove from the |
|
| 610 | - // file source, match file_source with the file cache |
|
| 611 | - if ($itemType == 'file' || $itemType == 'folder') { |
|
| 612 | - if(!is_null($uidOwner)) { |
|
| 613 | - $root = \OC\Files\Filesystem::getRoot(); |
|
| 614 | - } else { |
|
| 615 | - $root = ''; |
|
| 616 | - } |
|
| 617 | - $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 618 | - if (!isset($item)) { |
|
| 619 | - $where .= ' AND `file_target` IS NOT NULL '; |
|
| 620 | - } |
|
| 621 | - $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 622 | - $fileDependent = true; |
|
| 623 | - $queryArgs = array(); |
|
| 624 | - } else { |
|
| 625 | - $fileDependent = false; |
|
| 626 | - $root = ''; |
|
| 627 | - $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 628 | - if ($includeCollections && !isset($item) && $collectionTypes) { |
|
| 629 | - // If includeCollections is true, find collections of this item type, e.g. a music album contains songs |
|
| 630 | - if (!in_array($itemType, $collectionTypes)) { |
|
| 631 | - $itemTypes = array_merge(array($itemType), $collectionTypes); |
|
| 632 | - } else { |
|
| 633 | - $itemTypes = $collectionTypes; |
|
| 634 | - } |
|
| 635 | - $placeholders = implode(',', array_fill(0, count($itemTypes), '?')); |
|
| 636 | - $where = ' WHERE `item_type` IN ('.$placeholders.'))'; |
|
| 637 | - $queryArgs = $itemTypes; |
|
| 638 | - } else { |
|
| 639 | - $where = ' WHERE `item_type` = ?'; |
|
| 640 | - $queryArgs = array($itemType); |
|
| 641 | - } |
|
| 642 | - } |
|
| 643 | - if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 644 | - $where .= ' AND `share_type` != ?'; |
|
| 645 | - $queryArgs[] = self::SHARE_TYPE_LINK; |
|
| 646 | - } |
|
| 647 | - if (isset($shareType)) { |
|
| 648 | - // Include all user and group items |
|
| 649 | - if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) { |
|
| 650 | - $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) '; |
|
| 651 | - $queryArgs[] = self::SHARE_TYPE_USER; |
|
| 652 | - $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 653 | - $queryArgs[] = $shareWith; |
|
| 654 | - |
|
| 655 | - $user = \OC::$server->getUserManager()->get($shareWith); |
|
| 656 | - $groups = []; |
|
| 657 | - if ($user) { |
|
| 658 | - $groups = \OC::$server->getGroupManager()->getUserGroupIds($user); |
|
| 659 | - } |
|
| 660 | - if (!empty($groups)) { |
|
| 661 | - $placeholders = implode(',', array_fill(0, count($groups), '?')); |
|
| 662 | - $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; |
|
| 663 | - $queryArgs[] = self::SHARE_TYPE_GROUP; |
|
| 664 | - $queryArgs = array_merge($queryArgs, $groups); |
|
| 665 | - } |
|
| 666 | - $where .= ')'; |
|
| 667 | - // Don't include own group shares |
|
| 668 | - $where .= ' AND `uid_owner` != ?'; |
|
| 669 | - $queryArgs[] = $shareWith; |
|
| 670 | - } else { |
|
| 671 | - $where .= ' AND `share_type` = ?'; |
|
| 672 | - $queryArgs[] = $shareType; |
|
| 673 | - if (isset($shareWith)) { |
|
| 674 | - $where .= ' AND `share_with` = ?'; |
|
| 675 | - $queryArgs[] = $shareWith; |
|
| 676 | - } |
|
| 677 | - } |
|
| 678 | - } |
|
| 679 | - if (isset($uidOwner)) { |
|
| 680 | - $where .= ' AND `uid_owner` = ?'; |
|
| 681 | - $queryArgs[] = $uidOwner; |
|
| 682 | - if (!isset($shareType)) { |
|
| 683 | - // Prevent unique user targets for group shares from being selected |
|
| 684 | - $where .= ' AND `share_type` != ?'; |
|
| 685 | - $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 686 | - } |
|
| 687 | - if ($fileDependent) { |
|
| 688 | - $column = 'file_source'; |
|
| 689 | - } else { |
|
| 690 | - $column = 'item_source'; |
|
| 691 | - } |
|
| 692 | - } else { |
|
| 693 | - if ($fileDependent) { |
|
| 694 | - $column = 'file_target'; |
|
| 695 | - } else { |
|
| 696 | - $column = 'item_target'; |
|
| 697 | - } |
|
| 698 | - } |
|
| 699 | - if (isset($item)) { |
|
| 700 | - $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 701 | - if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 702 | - $where .= ' AND ('; |
|
| 703 | - } else { |
|
| 704 | - $where .= ' AND'; |
|
| 705 | - } |
|
| 706 | - // If looking for own shared items, check item_source else check item_target |
|
| 707 | - if (isset($uidOwner) || $itemShareWithBySource) { |
|
| 708 | - // If item type is a file, file source needs to be checked in case the item was converted |
|
| 709 | - if ($fileDependent) { |
|
| 710 | - $where .= ' `file_source` = ?'; |
|
| 711 | - $column = 'file_source'; |
|
| 712 | - } else { |
|
| 713 | - $where .= ' `item_source` = ?'; |
|
| 714 | - $column = 'item_source'; |
|
| 715 | - } |
|
| 716 | - } else { |
|
| 717 | - if ($fileDependent) { |
|
| 718 | - $where .= ' `file_target` = ?'; |
|
| 719 | - $item = \OC\Files\Filesystem::normalizePath($item); |
|
| 720 | - } else { |
|
| 721 | - $where .= ' `item_target` = ?'; |
|
| 722 | - } |
|
| 723 | - } |
|
| 724 | - $queryArgs[] = $item; |
|
| 725 | - if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 726 | - $placeholders = implode(',', array_fill(0, count($collectionTypes), '?')); |
|
| 727 | - $where .= ' OR `item_type` IN ('.$placeholders.'))'; |
|
| 728 | - $queryArgs = array_merge($queryArgs, $collectionTypes); |
|
| 729 | - } |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) { |
|
| 733 | - // Make sure the unique user target is returned if it exists, |
|
| 734 | - // unique targets should follow the group share in the database |
|
| 735 | - // If the limit is not 1, the filtering can be done later |
|
| 736 | - $where .= ' ORDER BY `*PREFIX*share`.`id` DESC'; |
|
| 737 | - } else { |
|
| 738 | - $where .= ' ORDER BY `*PREFIX*share`.`id` ASC'; |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - if ($limit != -1 && !$includeCollections) { |
|
| 742 | - // The limit must be at least 3, because filtering needs to be done |
|
| 743 | - if ($limit < 3) { |
|
| 744 | - $queryLimit = 3; |
|
| 745 | - } else { |
|
| 746 | - $queryLimit = $limit; |
|
| 747 | - } |
|
| 748 | - } else { |
|
| 749 | - $queryLimit = null; |
|
| 750 | - } |
|
| 751 | - $select = self::createSelectStatement($format, $fileDependent, $uidOwner); |
|
| 752 | - $root = strlen($root); |
|
| 753 | - $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit); |
|
| 754 | - $result = $query->execute($queryArgs); |
|
| 755 | - if ($result === false) { |
|
| 756 | - \OCP\Util::writeLog('OCP\Share', |
|
| 757 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 758 | - ILogger::ERROR); |
|
| 759 | - } |
|
| 760 | - $items = array(); |
|
| 761 | - $targets = array(); |
|
| 762 | - $switchedItems = array(); |
|
| 763 | - $mounts = array(); |
|
| 764 | - while ($row = $result->fetchRow()) { |
|
| 765 | - self::transformDBResults($row); |
|
| 766 | - // Filter out duplicate group shares for users with unique targets |
|
| 767 | - if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 768 | - continue; |
|
| 769 | - } |
|
| 770 | - if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { |
|
| 771 | - $row['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 772 | - $row['unique_name'] = true; // remember that we use a unique name for this user |
|
| 773 | - $row['share_with'] = $items[$row['parent']]['share_with']; |
|
| 774 | - // if the group share was unshared from the user we keep the permission, otherwise |
|
| 775 | - // we take the permission from the parent because this is always the up-to-date |
|
| 776 | - // permission for the group share |
|
| 777 | - if ($row['permissions'] > 0) { |
|
| 778 | - $row['permissions'] = $items[$row['parent']]['permissions']; |
|
| 779 | - } |
|
| 780 | - // Remove the parent group share |
|
| 781 | - unset($items[$row['parent']]); |
|
| 782 | - if ($row['permissions'] == 0) { |
|
| 783 | - continue; |
|
| 784 | - } |
|
| 785 | - } else if (!isset($uidOwner)) { |
|
| 786 | - // Check if the same target already exists |
|
| 787 | - if (isset($targets[$row['id']])) { |
|
| 788 | - // Check if the same owner shared with the user twice |
|
| 789 | - // through a group and user share - this is allowed |
|
| 790 | - $id = $targets[$row['id']]; |
|
| 791 | - if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) { |
|
| 792 | - // Switch to group share type to ensure resharing conditions aren't bypassed |
|
| 793 | - if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { |
|
| 794 | - $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 795 | - $items[$id]['share_with'] = $row['share_with']; |
|
| 796 | - } |
|
| 797 | - // Switch ids if sharing permission is granted on only |
|
| 798 | - // one share to ensure correct parent is used if resharing |
|
| 799 | - if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 800 | - && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 801 | - $items[$row['id']] = $items[$id]; |
|
| 802 | - $switchedItems[$id] = $row['id']; |
|
| 803 | - unset($items[$id]); |
|
| 804 | - $id = $row['id']; |
|
| 805 | - } |
|
| 806 | - $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 807 | - |
|
| 808 | - } |
|
| 809 | - continue; |
|
| 810 | - } elseif (!empty($row['parent'])) { |
|
| 811 | - $targets[$row['parent']] = $row['id']; |
|
| 812 | - } |
|
| 813 | - } |
|
| 814 | - // Remove root from file source paths if retrieving own shared items |
|
| 815 | - if (isset($uidOwner) && isset($row['path'])) { |
|
| 816 | - if (isset($row['parent'])) { |
|
| 817 | - $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
|
| 818 | - $parentResult = $query->execute(array($row['parent'])); |
|
| 819 | - if ($result === false) { |
|
| 820 | - \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 821 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 822 | - ILogger::ERROR); |
|
| 823 | - } else { |
|
| 824 | - $parentRow = $parentResult->fetchRow(); |
|
| 825 | - $tmpPath = $parentRow['file_target']; |
|
| 826 | - // find the right position where the row path continues from the target path |
|
| 827 | - $pos = strrpos($row['path'], $parentRow['file_target']); |
|
| 828 | - $subPath = substr($row['path'], $pos); |
|
| 829 | - $splitPath = explode('/', $subPath); |
|
| 830 | - foreach (array_slice($splitPath, 2) as $pathPart) { |
|
| 831 | - $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 832 | - } |
|
| 833 | - $row['path'] = $tmpPath; |
|
| 834 | - } |
|
| 835 | - } else { |
|
| 836 | - if (!isset($mounts[$row['storage']])) { |
|
| 837 | - $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']); |
|
| 838 | - if (is_array($mountPoints) && !empty($mountPoints)) { |
|
| 839 | - $mounts[$row['storage']] = current($mountPoints); |
|
| 840 | - } |
|
| 841 | - } |
|
| 842 | - if (!empty($mounts[$row['storage']])) { |
|
| 843 | - $path = $mounts[$row['storage']]->getMountPoint().$row['path']; |
|
| 844 | - $relPath = substr($path, $root); // path relative to data/user |
|
| 845 | - $row['path'] = rtrim($relPath, '/'); |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - } |
|
| 849 | - |
|
| 850 | - if($checkExpireDate) { |
|
| 851 | - if (self::expireItem($row)) { |
|
| 852 | - continue; |
|
| 853 | - } |
|
| 854 | - } |
|
| 855 | - // Check if resharing is allowed, if not remove share permission |
|
| 856 | - if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) { |
|
| 857 | - $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 858 | - } |
|
| 859 | - // Add display names to result |
|
| 860 | - $row['share_with_displayname'] = $row['share_with']; |
|
| 861 | - if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 862 | - $row['share_type'] === self::SHARE_TYPE_USER) { |
|
| 863 | - $shareWithUser = \OC::$server->getUserManager()->get($row['share_with']); |
|
| 864 | - $row['share_with_displayname'] = $shareWithUser === null ? $row['share_with'] : $shareWithUser->getDisplayName(); |
|
| 865 | - } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 866 | - $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
|
| 867 | - $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
|
| 868 | - foreach ($addressBookEntries as $entry) { |
|
| 869 | - foreach ($entry['CLOUD'] as $cloudID) { |
|
| 870 | - if ($cloudID === $row['share_with']) { |
|
| 871 | - $row['share_with_displayname'] = $entry['FN']; |
|
| 872 | - } |
|
| 873 | - } |
|
| 874 | - } |
|
| 875 | - } |
|
| 876 | - if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 877 | - $ownerUser = \OC::$server->getUserManager()->get($row['uid_owner']); |
|
| 878 | - $row['displayname_owner'] = $ownerUser === null ? $row['uid_owner'] : $ownerUser->getDisplayName(); |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - if ($row['permissions'] > 0) { |
|
| 882 | - $items[$row['id']] = $row; |
|
| 883 | - } |
|
| 884 | - |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - // group items if we are looking for items shared with the current user |
|
| 888 | - if (isset($shareWith) && $shareWith === \OCP\User::getUser()) { |
|
| 889 | - $items = self::groupItems($items, $itemType); |
|
| 890 | - } |
|
| 891 | - |
|
| 892 | - if (!empty($items)) { |
|
| 893 | - $collectionItems = array(); |
|
| 894 | - foreach ($items as &$row) { |
|
| 895 | - // Return only the item instead of a 2-dimensional array |
|
| 896 | - if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) { |
|
| 897 | - if ($format == self::FORMAT_NONE) { |
|
| 898 | - return $row; |
|
| 899 | - } else { |
|
| 900 | - break; |
|
| 901 | - } |
|
| 902 | - } |
|
| 903 | - // Check if this is a collection of the requested item type |
|
| 904 | - if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) { |
|
| 905 | - if (($collectionBackend = self::getBackend($row['item_type'])) |
|
| 906 | - && $collectionBackend instanceof \OCP\Share_Backend_Collection) { |
|
| 907 | - // Collections can be inside collections, check if the item is a collection |
|
| 908 | - if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) { |
|
| 909 | - $collectionItems[] = $row; |
|
| 910 | - } else { |
|
| 911 | - $collection = array(); |
|
| 912 | - $collection['item_type'] = $row['item_type']; |
|
| 913 | - if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 914 | - $collection['path'] = basename($row['path']); |
|
| 915 | - } |
|
| 916 | - $row['collection'] = $collection; |
|
| 917 | - // Fetch all of the children sources |
|
| 918 | - $children = $collectionBackend->getChildren($row[$column]); |
|
| 919 | - foreach ($children as $child) { |
|
| 920 | - $childItem = $row; |
|
| 921 | - $childItem['item_type'] = $itemType; |
|
| 922 | - if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') { |
|
| 923 | - $childItem['item_source'] = $child['source']; |
|
| 924 | - $childItem['item_target'] = $child['target']; |
|
| 925 | - } |
|
| 926 | - if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 927 | - if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 928 | - $childItem['file_source'] = $child['source']; |
|
| 929 | - } else { // TODO is this really needed if we already know that we use the file backend? |
|
| 930 | - $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']); |
|
| 931 | - $childItem['file_source'] = $meta['fileid']; |
|
| 932 | - } |
|
| 933 | - $childItem['file_target'] = |
|
| 934 | - \OC\Files\Filesystem::normalizePath($child['file_path']); |
|
| 935 | - } |
|
| 936 | - if (isset($item)) { |
|
| 937 | - if ($childItem[$column] == $item) { |
|
| 938 | - // Return only the item instead of a 2-dimensional array |
|
| 939 | - if ($limit == 1) { |
|
| 940 | - if ($format == self::FORMAT_NONE) { |
|
| 941 | - return $childItem; |
|
| 942 | - } else { |
|
| 943 | - // Unset the items array and break out of both loops |
|
| 944 | - $items = array(); |
|
| 945 | - $items[] = $childItem; |
|
| 946 | - break 2; |
|
| 947 | - } |
|
| 948 | - } else { |
|
| 949 | - $collectionItems[] = $childItem; |
|
| 950 | - } |
|
| 951 | - } |
|
| 952 | - } else { |
|
| 953 | - $collectionItems[] = $childItem; |
|
| 954 | - } |
|
| 955 | - } |
|
| 956 | - } |
|
| 957 | - } |
|
| 958 | - // Remove collection item |
|
| 959 | - $toRemove = $row['id']; |
|
| 960 | - if (array_key_exists($toRemove, $switchedItems)) { |
|
| 961 | - $toRemove = $switchedItems[$toRemove]; |
|
| 962 | - } |
|
| 963 | - unset($items[$toRemove]); |
|
| 964 | - } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) { |
|
| 965 | - // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 966 | - // see github issue #10588 for more details |
|
| 967 | - // Need to find a solution which works for all back-ends |
|
| 968 | - $collectionBackend = self::getBackend($row['item_type']); |
|
| 969 | - $sharedParents = $collectionBackend->getParents($row['item_source']); |
|
| 970 | - foreach ($sharedParents as $parent) { |
|
| 971 | - $collectionItems[] = $parent; |
|
| 972 | - } |
|
| 973 | - } |
|
| 974 | - } |
|
| 975 | - if (!empty($collectionItems)) { |
|
| 976 | - $collectionItems = array_unique($collectionItems, SORT_REGULAR); |
|
| 977 | - $items = array_merge($items, $collectionItems); |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - // filter out invalid items, these can appear when subshare entries exist |
|
| 981 | - // for a group in which the requested user isn't a member any more |
|
| 982 | - $items = array_filter($items, function($item) { |
|
| 983 | - return $item['share_type'] !== self::$shareTypeGroupUserUnique; |
|
| 984 | - }); |
|
| 985 | - |
|
| 986 | - return self::formatResult($items, $column, $backend, $format, $parameters); |
|
| 987 | - } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) { |
|
| 988 | - // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 989 | - // see github issue #10588 for more details |
|
| 990 | - // Need to find a solution which works for all back-ends |
|
| 991 | - $collectionItems = array(); |
|
| 992 | - $collectionBackend = self::getBackend('folder'); |
|
| 993 | - $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner); |
|
| 994 | - foreach ($sharedParents as $parent) { |
|
| 995 | - $collectionItems[] = $parent; |
|
| 996 | - } |
|
| 997 | - if ($limit === 1) { |
|
| 998 | - return reset($collectionItems); |
|
| 999 | - } |
|
| 1000 | - return self::formatResult($collectionItems, $column, $backend, $format, $parameters); |
|
| 1001 | - } |
|
| 1002 | - |
|
| 1003 | - return array(); |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - /** |
|
| 1007 | - * group items with link to the same source |
|
| 1008 | - * |
|
| 1009 | - * @param array $items |
|
| 1010 | - * @param string $itemType |
|
| 1011 | - * @return array of grouped items |
|
| 1012 | - */ |
|
| 1013 | - protected static function groupItems($items, $itemType) { |
|
| 1014 | - |
|
| 1015 | - $fileSharing = $itemType === 'file' || $itemType === 'folder'; |
|
| 1016 | - |
|
| 1017 | - $result = array(); |
|
| 1018 | - |
|
| 1019 | - foreach ($items as $item) { |
|
| 1020 | - $grouped = false; |
|
| 1021 | - foreach ($result as $key => $r) { |
|
| 1022 | - // for file/folder shares we need to compare file_source, otherwise we compare item_source |
|
| 1023 | - // only group shares if they already point to the same target, otherwise the file where shared |
|
| 1024 | - // before grouping of shares was added. In this case we don't group them toi avoid confusions |
|
| 1025 | - if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 1026 | - (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
|
| 1027 | - // add the first item to the list of grouped shares |
|
| 1028 | - if (!isset($result[$key]['grouped'])) { |
|
| 1029 | - $result[$key]['grouped'][] = $result[$key]; |
|
| 1030 | - } |
|
| 1031 | - $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions']; |
|
| 1032 | - $result[$key]['grouped'][] = $item; |
|
| 1033 | - $grouped = true; |
|
| 1034 | - break; |
|
| 1035 | - } |
|
| 1036 | - } |
|
| 1037 | - |
|
| 1038 | - if (!$grouped) { |
|
| 1039 | - $result[] = $item; |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - } |
|
| 1043 | - |
|
| 1044 | - return $result; |
|
| 1045 | - } |
|
| 1046 | - |
|
| 1047 | - /** |
|
| 1048 | - * Put shared item into the database |
|
| 1049 | - * @param string $itemType Item type |
|
| 1050 | - * @param string $itemSource Item source |
|
| 1051 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1052 | - * @param string $shareWith User or group the item is being shared with |
|
| 1053 | - * @param string $uidOwner User that is the owner of shared item |
|
| 1054 | - * @param int $permissions CRUDS permissions |
|
| 1055 | - * @throws \Exception |
|
| 1056 | - * @return mixed id of the new share or false |
|
| 1057 | - * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 1058 | - * called like this: |
|
| 1059 | - * self::put('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions); |
|
| 1060 | - */ |
|
| 1061 | - private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 1062 | - $permissions) { |
|
| 1063 | - |
|
| 1064 | - $queriesToExecute = array(); |
|
| 1065 | - $suggestedItemTarget = null; |
|
| 1066 | - $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = ''; |
|
| 1067 | - $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
|
| 1068 | - |
|
| 1069 | - $result = self::checkReshare('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions, null, null); |
|
| 1070 | - if(!empty($result)) { |
|
| 1071 | - $parent = $result['parent']; |
|
| 1072 | - $itemSource = $result['itemSource']; |
|
| 1073 | - $fileSource = $result['fileSource']; |
|
| 1074 | - $suggestedItemTarget = $result['suggestedItemTarget']; |
|
| 1075 | - $suggestedFileTarget = $result['suggestedFileTarget']; |
|
| 1076 | - $filePath = $result['filePath']; |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - $isGroupShare = false; |
|
| 1080 | - $users = array($shareWith); |
|
| 1081 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 1082 | - $suggestedItemTarget); |
|
| 1083 | - |
|
| 1084 | - $run = true; |
|
| 1085 | - $error = ''; |
|
| 1086 | - $preHookData = array( |
|
| 1087 | - 'itemType' => $itemType, |
|
| 1088 | - 'itemSource' => $itemSource, |
|
| 1089 | - 'shareType' => $shareType, |
|
| 1090 | - 'uidOwner' => $uidOwner, |
|
| 1091 | - 'permissions' => $permissions, |
|
| 1092 | - 'fileSource' => $fileSource, |
|
| 1093 | - 'expiration' => null, |
|
| 1094 | - 'token' => null, |
|
| 1095 | - 'run' => &$run, |
|
| 1096 | - 'error' => &$error |
|
| 1097 | - ); |
|
| 1098 | - |
|
| 1099 | - $preHookData['itemTarget'] = $itemTarget; |
|
| 1100 | - $preHookData['shareWith'] = $shareWith; |
|
| 1101 | - |
|
| 1102 | - \OC_Hook::emit(\OCP\Share::class, 'pre_shared', $preHookData); |
|
| 1103 | - |
|
| 1104 | - if ($run === false) { |
|
| 1105 | - throw new \Exception($error); |
|
| 1106 | - } |
|
| 1107 | - |
|
| 1108 | - foreach ($users as $user) { |
|
| 1109 | - $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource; |
|
| 1110 | - $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user); |
|
| 1111 | - |
|
| 1112 | - $userShareType = $shareType; |
|
| 1113 | - |
|
| 1114 | - if ($sourceExists && $sourceExists['item_source'] === $itemSource) { |
|
| 1115 | - $fileTarget = $sourceExists['file_target']; |
|
| 1116 | - $itemTarget = $sourceExists['item_target']; |
|
| 1117 | - |
|
| 1118 | - } elseif(!$sourceExists) { |
|
| 1119 | - |
|
| 1120 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
|
| 1121 | - $uidOwner, $suggestedItemTarget, $parent); |
|
| 1122 | - if (isset($fileSource)) { |
|
| 1123 | - $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, |
|
| 1124 | - $user, $uidOwner, $suggestedFileTarget, $parent); |
|
| 1125 | - } else { |
|
| 1126 | - $fileTarget = null; |
|
| 1127 | - } |
|
| 1128 | - |
|
| 1129 | - } else { |
|
| 1130 | - |
|
| 1131 | - // group share which doesn't exists until now, check if we need a unique target for this user |
|
| 1132 | - |
|
| 1133 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, |
|
| 1134 | - $uidOwner, $suggestedItemTarget, $parent); |
|
| 1135 | - |
|
| 1136 | - // do we also need a file target |
|
| 1137 | - if (isset($fileSource)) { |
|
| 1138 | - $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, |
|
| 1139 | - $uidOwner, $suggestedFileTarget, $parent); |
|
| 1140 | - } else { |
|
| 1141 | - $fileTarget = null; |
|
| 1142 | - } |
|
| 1143 | - |
|
| 1144 | - if (($itemTarget === $groupItemTarget) && |
|
| 1145 | - (!isset($fileSource) || $fileTarget === $groupFileTarget)) { |
|
| 1146 | - continue; |
|
| 1147 | - } |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - $queriesToExecute[] = array( |
|
| 1151 | - 'itemType' => $itemType, |
|
| 1152 | - 'itemSource' => $itemSource, |
|
| 1153 | - 'itemTarget' => $itemTarget, |
|
| 1154 | - 'shareType' => $userShareType, |
|
| 1155 | - 'shareWith' => $user, |
|
| 1156 | - 'uidOwner' => $uidOwner, |
|
| 1157 | - 'permissions' => $permissions, |
|
| 1158 | - 'shareTime' => time(), |
|
| 1159 | - 'fileSource' => $fileSource, |
|
| 1160 | - 'fileTarget' => $fileTarget, |
|
| 1161 | - 'token' => null, |
|
| 1162 | - 'parent' => $parent, |
|
| 1163 | - 'expiration' => null, |
|
| 1164 | - ); |
|
| 1165 | - |
|
| 1166 | - } |
|
| 1167 | - |
|
| 1168 | - $id = false; |
|
| 1169 | - |
|
| 1170 | - foreach ($queriesToExecute as $shareQuery) { |
|
| 1171 | - $shareQuery['parent'] = $parent; |
|
| 1172 | - $id = self::insertShare($shareQuery); |
|
| 1173 | - } |
|
| 1174 | - |
|
| 1175 | - $postHookData = array( |
|
| 1176 | - 'itemType' => $itemType, |
|
| 1177 | - 'itemSource' => $itemSource, |
|
| 1178 | - 'parent' => $parent, |
|
| 1179 | - 'shareType' => $shareType, |
|
| 1180 | - 'uidOwner' => $uidOwner, |
|
| 1181 | - 'permissions' => $permissions, |
|
| 1182 | - 'fileSource' => $fileSource, |
|
| 1183 | - 'id' => $parent, |
|
| 1184 | - 'token' => null, |
|
| 1185 | - 'expirationDate' => null, |
|
| 1186 | - ); |
|
| 1187 | - |
|
| 1188 | - $postHookData['shareWith'] = $isGroupShare ? $shareWith['group'] : $shareWith; |
|
| 1189 | - $postHookData['itemTarget'] = $isGroupShare ? $groupItemTarget : $itemTarget; |
|
| 1190 | - $postHookData['fileTarget'] = $isGroupShare ? $groupFileTarget : $fileTarget; |
|
| 1191 | - |
|
| 1192 | - \OC_Hook::emit(\OCP\Share::class, 'post_shared', $postHookData); |
|
| 1193 | - |
|
| 1194 | - |
|
| 1195 | - return $id ? $id : false; |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - /** |
|
| 1199 | - * @param string $itemType |
|
| 1200 | - * @param string $itemSource |
|
| 1201 | - * @param int $shareType |
|
| 1202 | - * @param string $shareWith |
|
| 1203 | - * @param string $uidOwner |
|
| 1204 | - * @param int $permissions |
|
| 1205 | - * @param string|null $itemSourceName |
|
| 1206 | - * @param null|\DateTime $expirationDate |
|
| 1207 | - * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 1208 | - * called like this: |
|
| 1209 | - * self::checkReshare('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions, null, null); |
|
| 1210 | - */ |
|
| 1211 | - private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) { |
|
| 1212 | - $backend = self::getBackend($itemType); |
|
| 1213 | - |
|
| 1214 | - $result = array(); |
|
| 1215 | - |
|
| 1216 | - $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source'; |
|
| 1217 | - |
|
| 1218 | - $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true); |
|
| 1219 | - if ($checkReshare) { |
|
| 1220 | - // Check if attempting to share back to owner |
|
| 1221 | - if ($checkReshare['uid_owner'] == $shareWith) { |
|
| 1222 | - $message = 'Sharing %1$s failed, because the user %2$s is the original sharer'; |
|
| 1223 | - throw new \Exception(sprintf($message, $itemSourceName, $shareWith)); |
|
| 1224 | - } |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
|
| 1228 | - // Check if share permissions is granted |
|
| 1229 | - if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1230 | - if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 1231 | - $message = 'Sharing %1$s failed, because the permissions exceed permissions granted to %2$s'; |
|
| 1232 | - throw new \Exception(sprintf($message, $itemSourceName, $uidOwner)); |
|
| 1233 | - } else { |
|
| 1234 | - // TODO Don't check if inside folder |
|
| 1235 | - $result['parent'] = $checkReshare['id']; |
|
| 1236 | - |
|
| 1237 | - $result['expirationDate'] = $expirationDate; |
|
| 1238 | - // $checkReshare['expiration'] could be null and then is always less than any value |
|
| 1239 | - if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 1240 | - $result['expirationDate'] = $checkReshare['expiration']; |
|
| 1241 | - } |
|
| 1242 | - |
|
| 1243 | - // only suggest the same name as new target if it is a reshare of the |
|
| 1244 | - // same file/folder and not the reshare of a child |
|
| 1245 | - if ($checkReshare[$column] === $itemSource) { |
|
| 1246 | - $result['filePath'] = $checkReshare['file_target']; |
|
| 1247 | - $result['itemSource'] = $checkReshare['item_source']; |
|
| 1248 | - $result['fileSource'] = $checkReshare['file_source']; |
|
| 1249 | - $result['suggestedItemTarget'] = $checkReshare['item_target']; |
|
| 1250 | - $result['suggestedFileTarget'] = $checkReshare['file_target']; |
|
| 1251 | - } else { |
|
| 1252 | - $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null; |
|
| 1253 | - $result['suggestedItemTarget'] = null; |
|
| 1254 | - $result['suggestedFileTarget'] = null; |
|
| 1255 | - $result['itemSource'] = $itemSource; |
|
| 1256 | - $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null; |
|
| 1257 | - } |
|
| 1258 | - } |
|
| 1259 | - } else { |
|
| 1260 | - $message = 'Sharing %s failed, because resharing is not allowed'; |
|
| 1261 | - throw new \Exception(sprintf($message, $itemSourceName)); |
|
| 1262 | - } |
|
| 1263 | - } else { |
|
| 1264 | - $result['parent'] = null; |
|
| 1265 | - $result['suggestedItemTarget'] = null; |
|
| 1266 | - $result['suggestedFileTarget'] = null; |
|
| 1267 | - $result['itemSource'] = $itemSource; |
|
| 1268 | - $result['expirationDate'] = $expirationDate; |
|
| 1269 | - if (!$backend->isValidSource($itemSource, $uidOwner)) { |
|
| 1270 | - $message = 'Sharing %1$s failed, because the sharing backend for ' |
|
| 1271 | - .'%2$s could not find its source'; |
|
| 1272 | - throw new \Exception(sprintf($message, $itemSource, $itemType)); |
|
| 1273 | - } |
|
| 1274 | - if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 1275 | - $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner); |
|
| 1276 | - $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']); |
|
| 1277 | - $result['fileSource'] = $meta['fileid']; |
|
| 1278 | - if ($result['fileSource'] == -1) { |
|
| 1279 | - $message = 'Sharing %s failed, because the file could not be found in the file cache'; |
|
| 1280 | - throw new \Exception(sprintf($message, $itemSource)); |
|
| 1281 | - } |
|
| 1282 | - } else { |
|
| 1283 | - $result['filePath'] = null; |
|
| 1284 | - $result['fileSource'] = null; |
|
| 1285 | - } |
|
| 1286 | - } |
|
| 1287 | - |
|
| 1288 | - return $result; |
|
| 1289 | - } |
|
| 1290 | - |
|
| 1291 | - /** |
|
| 1292 | - * |
|
| 1293 | - * @param array $shareData |
|
| 1294 | - * @return mixed false in case of a failure or the id of the new share |
|
| 1295 | - */ |
|
| 1296 | - private static function insertShare(array $shareData) { |
|
| 1297 | - |
|
| 1298 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' |
|
| 1299 | - .' `item_type`, `item_source`, `item_target`, `share_type`,' |
|
| 1300 | - .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' |
|
| 1301 | - .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 1302 | - $query->bindValue(1, $shareData['itemType']); |
|
| 1303 | - $query->bindValue(2, $shareData['itemSource']); |
|
| 1304 | - $query->bindValue(3, $shareData['itemTarget']); |
|
| 1305 | - $query->bindValue(4, $shareData['shareType']); |
|
| 1306 | - $query->bindValue(5, $shareData['shareWith']); |
|
| 1307 | - $query->bindValue(6, $shareData['uidOwner']); |
|
| 1308 | - $query->bindValue(7, $shareData['permissions']); |
|
| 1309 | - $query->bindValue(8, $shareData['shareTime']); |
|
| 1310 | - $query->bindValue(9, $shareData['fileSource']); |
|
| 1311 | - $query->bindValue(10, $shareData['fileTarget']); |
|
| 1312 | - $query->bindValue(11, $shareData['token']); |
|
| 1313 | - $query->bindValue(12, $shareData['parent']); |
|
| 1314 | - $query->bindValue(13, $shareData['expiration'], 'datetime'); |
|
| 1315 | - $result = $query->execute(); |
|
| 1316 | - |
|
| 1317 | - $id = false; |
|
| 1318 | - if ($result) { |
|
| 1319 | - $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 1320 | - } |
|
| 1321 | - |
|
| 1322 | - return $id; |
|
| 1323 | - |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - /** |
|
| 1327 | - * construct select statement |
|
| 1328 | - * @param int $format |
|
| 1329 | - * @param boolean $fileDependent ist it a file/folder share or a generla share |
|
| 1330 | - * @param string $uidOwner |
|
| 1331 | - * @return string select statement |
|
| 1332 | - */ |
|
| 1333 | - private static function createSelectStatement($format, $fileDependent, $uidOwner = null) { |
|
| 1334 | - $select = '*'; |
|
| 1335 | - if ($format == self::FORMAT_STATUSES) { |
|
| 1336 | - if ($fileDependent) { |
|
| 1337 | - $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, ' |
|
| 1338 | - . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, ' |
|
| 1339 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 1340 | - . '`uid_initiator`'; |
|
| 1341 | - } else { |
|
| 1342 | - $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`'; |
|
| 1343 | - } |
|
| 1344 | - } else { |
|
| 1345 | - if (isset($uidOwner)) { |
|
| 1346 | - if ($fileDependent) { |
|
| 1347 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,' |
|
| 1348 | - . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,' |
|
| 1349 | - . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, ' |
|
| 1350 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 1351 | - } else { |
|
| 1352 | - $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,' |
|
| 1353 | - . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`'; |
|
| 1354 | - } |
|
| 1355 | - } else { |
|
| 1356 | - if ($fileDependent) { |
|
| 1357 | - if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) { |
|
| 1358 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, ' |
|
| 1359 | - . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, ' |
|
| 1360 | - . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 1361 | - . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`'; |
|
| 1362 | - } else { |
|
| 1363 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,' |
|
| 1364 | - . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,' |
|
| 1365 | - . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,' |
|
| 1366 | - . '`stime`, `expiration`, `token`, `storage`, `mail_send`,' |
|
| 1367 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 1368 | - } |
|
| 1369 | - } |
|
| 1370 | - } |
|
| 1371 | - } |
|
| 1372 | - return $select; |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - |
|
| 1376 | - /** |
|
| 1377 | - * transform db results |
|
| 1378 | - * @param array $row result |
|
| 1379 | - */ |
|
| 1380 | - private static function transformDBResults(&$row) { |
|
| 1381 | - if (isset($row['id'])) { |
|
| 1382 | - $row['id'] = (int) $row['id']; |
|
| 1383 | - } |
|
| 1384 | - if (isset($row['share_type'])) { |
|
| 1385 | - $row['share_type'] = (int) $row['share_type']; |
|
| 1386 | - } |
|
| 1387 | - if (isset($row['parent'])) { |
|
| 1388 | - $row['parent'] = (int) $row['parent']; |
|
| 1389 | - } |
|
| 1390 | - if (isset($row['file_parent'])) { |
|
| 1391 | - $row['file_parent'] = (int) $row['file_parent']; |
|
| 1392 | - } |
|
| 1393 | - if (isset($row['file_source'])) { |
|
| 1394 | - $row['file_source'] = (int) $row['file_source']; |
|
| 1395 | - } |
|
| 1396 | - if (isset($row['permissions'])) { |
|
| 1397 | - $row['permissions'] = (int) $row['permissions']; |
|
| 1398 | - } |
|
| 1399 | - if (isset($row['storage'])) { |
|
| 1400 | - $row['storage'] = (int) $row['storage']; |
|
| 1401 | - } |
|
| 1402 | - if (isset($row['stime'])) { |
|
| 1403 | - $row['stime'] = (int) $row['stime']; |
|
| 1404 | - } |
|
| 1405 | - if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) { |
|
| 1406 | - // discard expiration date for non-link shares, which might have been |
|
| 1407 | - // set by ancient bugs |
|
| 1408 | - $row['expiration'] = null; |
|
| 1409 | - } |
|
| 1410 | - } |
|
| 1411 | - |
|
| 1412 | - /** |
|
| 1413 | - * format result |
|
| 1414 | - * @param array $items result |
|
| 1415 | - * @param string $column is it a file share or a general share ('file_target' or 'item_target') |
|
| 1416 | - * @param \OCP\Share_Backend $backend sharing backend |
|
| 1417 | - * @param int $format |
|
| 1418 | - * @param array $parameters additional format parameters |
|
| 1419 | - * @return array format result |
|
| 1420 | - */ |
|
| 1421 | - private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 1422 | - if ($format === self::FORMAT_NONE) { |
|
| 1423 | - return $items; |
|
| 1424 | - } else if ($format === self::FORMAT_STATUSES) { |
|
| 1425 | - $statuses = array(); |
|
| 1426 | - foreach ($items as $item) { |
|
| 1427 | - if ($item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 1428 | - if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) { |
|
| 1429 | - continue; |
|
| 1430 | - } |
|
| 1431 | - $statuses[$item[$column]]['link'] = true; |
|
| 1432 | - } else if (!isset($statuses[$item[$column]])) { |
|
| 1433 | - $statuses[$item[$column]]['link'] = false; |
|
| 1434 | - } |
|
| 1435 | - if (!empty($item['file_target'])) { |
|
| 1436 | - $statuses[$item[$column]]['path'] = $item['path']; |
|
| 1437 | - } |
|
| 1438 | - } |
|
| 1439 | - return $statuses; |
|
| 1440 | - } else { |
|
| 1441 | - return $backend->formatItems($items, $format, $parameters); |
|
| 1442 | - } |
|
| 1443 | - } |
|
| 1444 | - |
|
| 1445 | - /** |
|
| 1446 | - * remove protocol from URL |
|
| 1447 | - * |
|
| 1448 | - * @param string $url |
|
| 1449 | - * @return string |
|
| 1450 | - */ |
|
| 1451 | - public static function removeProtocolFromUrl($url) { |
|
| 1452 | - if (strpos($url, 'https://') === 0) { |
|
| 1453 | - return substr($url, strlen('https://')); |
|
| 1454 | - } else if (strpos($url, 'http://') === 0) { |
|
| 1455 | - return substr($url, strlen('http://')); |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - return $url; |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - /** |
|
| 1462 | - * try http post first with https and then with http as a fallback |
|
| 1463 | - * |
|
| 1464 | - * @param string $remoteDomain |
|
| 1465 | - * @param string $urlSuffix |
|
| 1466 | - * @param array $fields post parameters |
|
| 1467 | - * @return array |
|
| 1468 | - */ |
|
| 1469 | - private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
| 1470 | - $protocol = 'https://'; |
|
| 1471 | - $result = [ |
|
| 1472 | - 'success' => false, |
|
| 1473 | - 'result' => '', |
|
| 1474 | - ]; |
|
| 1475 | - $try = 0; |
|
| 1476 | - $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
| 1477 | - while ($result['success'] === false && $try < 2) { |
|
| 1478 | - $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 1479 | - $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
| 1480 | - $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 1481 | - |
|
| 1482 | - try { |
|
| 1483 | - $response = $client->post( |
|
| 1484 | - $protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, |
|
| 1485 | - [ |
|
| 1486 | - 'body' => $fields, |
|
| 1487 | - 'connect_timeout' => 10, |
|
| 1488 | - ] |
|
| 1489 | - ); |
|
| 1490 | - |
|
| 1491 | - $result = ['success' => true, 'result' => $response->getBody()]; |
|
| 1492 | - } catch (\Exception $e) { |
|
| 1493 | - $result = ['success' => false, 'result' => $e->getMessage()]; |
|
| 1494 | - } |
|
| 1495 | - |
|
| 1496 | - $try++; |
|
| 1497 | - $protocol = 'http://'; |
|
| 1498 | - } |
|
| 1499 | - |
|
| 1500 | - return $result; |
|
| 1501 | - } |
|
| 1502 | - |
|
| 1503 | - /** |
|
| 1504 | - * send server-to-server unshare to remote server |
|
| 1505 | - * |
|
| 1506 | - * @param string $remote url |
|
| 1507 | - * @param int $id share id |
|
| 1508 | - * @param string $token |
|
| 1509 | - * @return bool |
|
| 1510 | - */ |
|
| 1511 | - private static function sendRemoteUnshare($remote, $id, $token) { |
|
| 1512 | - $url = rtrim($remote, '/'); |
|
| 1513 | - $fields = array('token' => $token, 'format' => 'json'); |
|
| 1514 | - $url = self::removeProtocolFromUrl($url); |
|
| 1515 | - $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
| 1516 | - $status = json_decode($result['result'], true); |
|
| 1517 | - |
|
| 1518 | - return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
| 1519 | - } |
|
| 1520 | - |
|
| 1521 | - /** |
|
| 1522 | - * @return int |
|
| 1523 | - */ |
|
| 1524 | - public static function getExpireInterval() { |
|
| 1525 | - return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1526 | - } |
|
| 1527 | - |
|
| 1528 | - /** |
|
| 1529 | - * Checks whether the given path is reachable for the given owner |
|
| 1530 | - * |
|
| 1531 | - * @param string $path path relative to files |
|
| 1532 | - * @param string $ownerStorageId storage id of the owner |
|
| 1533 | - * |
|
| 1534 | - * @return boolean true if file is reachable, false otherwise |
|
| 1535 | - */ |
|
| 1536 | - private static function isFileReachable($path, $ownerStorageId) { |
|
| 1537 | - // if outside the home storage, file is always considered reachable |
|
| 1538 | - if (!(substr($ownerStorageId, 0, 6) === 'home::' || |
|
| 1539 | - substr($ownerStorageId, 0, 13) === 'object::user:' |
|
| 1540 | - )) { |
|
| 1541 | - return true; |
|
| 1542 | - } |
|
| 1543 | - |
|
| 1544 | - // if inside the home storage, the file has to be under "/files/" |
|
| 1545 | - $path = ltrim($path, '/'); |
|
| 1546 | - if (substr($path, 0, 6) === 'files/') { |
|
| 1547 | - return true; |
|
| 1548 | - } |
|
| 1549 | - |
|
| 1550 | - return false; |
|
| 1551 | - } |
|
| 56 | + /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask |
|
| 57 | + * Construct permissions for share() and setPermissions with Or (|) e.g. |
|
| 58 | + * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE |
|
| 59 | + * |
|
| 60 | + * Check if permission is granted with And (&) e.g. Check if delete is |
|
| 61 | + * granted: if ($permissions & PERMISSION_DELETE) |
|
| 62 | + * |
|
| 63 | + * Remove permissions with And (&) and Not (~) e.g. Remove the update |
|
| 64 | + * permission: $permissions &= ~PERMISSION_UPDATE |
|
| 65 | + * |
|
| 66 | + * Apps are required to handle permissions on their own, this class only |
|
| 67 | + * stores and manages the permissions of shares |
|
| 68 | + * @see lib/public/constants.php |
|
| 69 | + */ |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Register a sharing backend class that implements OCP\Share_Backend for an item type |
|
| 73 | + * @param string $itemType Item type |
|
| 74 | + * @param string $class Backend class |
|
| 75 | + * @param string $collectionOf (optional) Depends on item type |
|
| 76 | + * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files |
|
| 77 | + * @return boolean true if backend is registered or false if error |
|
| 78 | + */ |
|
| 79 | + public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { |
|
| 80 | + if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') == 'yes') { |
|
| 81 | + if (!isset(self::$backendTypes[$itemType])) { |
|
| 82 | + self::$backendTypes[$itemType] = array( |
|
| 83 | + 'class' => $class, |
|
| 84 | + 'collectionOf' => $collectionOf, |
|
| 85 | + 'supportedFileExtensions' => $supportedFileExtensions |
|
| 86 | + ); |
|
| 87 | + return true; |
|
| 88 | + } |
|
| 89 | + \OCP\Util::writeLog('OCP\Share', |
|
| 90 | + 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'] |
|
| 91 | + .' is already registered for '.$itemType, |
|
| 92 | + ILogger::WARN); |
|
| 93 | + } |
|
| 94 | + return false; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Get the items of item type shared with the current user |
|
| 99 | + * @param string $itemType |
|
| 100 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 101 | + * @param mixed $parameters (optional) |
|
| 102 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 103 | + * @param boolean $includeCollections (optional) |
|
| 104 | + * @return mixed Return depends on format |
|
| 105 | + * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 106 | + * called like this: |
|
| 107 | + * \OC\Share\Share::getItemsSharedWith('folder'); (apps/files_sharing/tests/UpdaterTest.php) |
|
| 108 | + */ |
|
| 109 | + public static function getItemsSharedWith() { |
|
| 110 | + return self::getItems('folder', null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, self::FORMAT_NONE, |
|
| 111 | + null, -1, false); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the items of item type shared with a user |
|
| 116 | + * @param string $itemType |
|
| 117 | + * @param string $user id for which user we want the shares |
|
| 118 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 119 | + * @param mixed $parameters (optional) |
|
| 120 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 121 | + * @param boolean $includeCollections (optional) |
|
| 122 | + * @return mixed Return depends on format |
|
| 123 | + */ |
|
| 124 | + public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE, |
|
| 125 | + $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 126 | + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format, |
|
| 127 | + $parameters, $limit, $includeCollections); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Get the item of item type shared with a given user by source |
|
| 132 | + * @param string $itemType |
|
| 133 | + * @param string $itemSource |
|
| 134 | + * @param string $user User to whom the item was shared |
|
| 135 | + * @param string $owner Owner of the share |
|
| 136 | + * @param int $shareType only look for a specific share type |
|
| 137 | + * @return array Return list of items with file_target, permissions and expiration |
|
| 138 | + */ |
|
| 139 | + public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) { |
|
| 140 | + $shares = array(); |
|
| 141 | + $fileDependent = false; |
|
| 142 | + |
|
| 143 | + $where = 'WHERE'; |
|
| 144 | + $fileDependentWhere = ''; |
|
| 145 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 146 | + $fileDependent = true; |
|
| 147 | + $column = 'file_source'; |
|
| 148 | + $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 149 | + $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 150 | + } else { |
|
| 151 | + $column = 'item_source'; |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
|
| 155 | + |
|
| 156 | + $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 157 | + $arguments = array($itemSource, $itemType); |
|
| 158 | + // for link shares $user === null |
|
| 159 | + if ($user !== null) { |
|
| 160 | + $where .= ' AND `share_with` = ? '; |
|
| 161 | + $arguments[] = $user; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if ($shareType !== null) { |
|
| 165 | + $where .= ' AND `share_type` = ? '; |
|
| 166 | + $arguments[] = $shareType; |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + if ($owner !== null) { |
|
| 170 | + $where .= ' AND `uid_owner` = ? '; |
|
| 171 | + $arguments[] = $owner; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 175 | + |
|
| 176 | + $result = \OC_DB::executeAudited($query, $arguments); |
|
| 177 | + |
|
| 178 | + while ($row = $result->fetchRow()) { |
|
| 179 | + if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 180 | + continue; |
|
| 181 | + } |
|
| 182 | + if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 183 | + // if it is a mount point we need to get the path from the mount manager |
|
| 184 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 185 | + $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
|
| 186 | + if (!empty($mountPoint)) { |
|
| 187 | + $path = $mountPoint[0]->getMountPoint(); |
|
| 188 | + $path = trim($path, '/'); |
|
| 189 | + $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt` |
|
| 190 | + $row['path'] = $path; |
|
| 191 | + } else { |
|
| 192 | + \OC::$server->getLogger()->warning( |
|
| 193 | + 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 194 | + ['app' => 'OCP\Share'] |
|
| 195 | + ); |
|
| 196 | + } |
|
| 197 | + } |
|
| 198 | + $shares[] = $row; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + //if didn't found a result than let's look for a group share. |
|
| 202 | + if(empty($shares) && $user !== null) { |
|
| 203 | + $userObject = \OC::$server->getUserManager()->get($user); |
|
| 204 | + $groups = []; |
|
| 205 | + if ($userObject) { |
|
| 206 | + $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject); |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + if (!empty($groups)) { |
|
| 210 | + $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 211 | + $arguments = array($itemSource, $itemType, $groups); |
|
| 212 | + $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 213 | + |
|
| 214 | + if ($owner !== null) { |
|
| 215 | + $where .= ' AND `uid_owner` = ?'; |
|
| 216 | + $arguments[] = $owner; |
|
| 217 | + $types[] = null; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + // TODO: inject connection, hopefully one day in the future when this |
|
| 221 | + // class isn't static anymore... |
|
| 222 | + $conn = \OC::$server->getDatabaseConnection(); |
|
| 223 | + $result = $conn->executeQuery( |
|
| 224 | + 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 225 | + $arguments, |
|
| 226 | + $types |
|
| 227 | + ); |
|
| 228 | + |
|
| 229 | + while ($row = $result->fetch()) { |
|
| 230 | + $shares[] = $row; |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + |
|
| 235 | + return $shares; |
|
| 236 | + |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Get the item of item type shared with the current user by source |
|
| 241 | + * @param string $itemType |
|
| 242 | + * @param string $itemSource |
|
| 243 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 244 | + * @param mixed $parameters |
|
| 245 | + * @param boolean $includeCollections |
|
| 246 | + * @param string $shareWith (optional) define against which user should be checked, default: current user |
|
| 247 | + * @return array |
|
| 248 | + */ |
|
| 249 | + public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 250 | + $parameters = null, $includeCollections = false, $shareWith = null) { |
|
| 251 | + $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith; |
|
| 252 | + return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format, |
|
| 253 | + $parameters, 1, $includeCollections, true); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Get the shared item of item type owned by the current user |
|
| 258 | + * @param string $itemType |
|
| 259 | + * @param string $itemSource |
|
| 260 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 261 | + * @param mixed $parameters |
|
| 262 | + * @param boolean $includeCollections |
|
| 263 | + * @return mixed Return depends on format |
|
| 264 | + */ |
|
| 265 | + public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 266 | + $parameters = null, $includeCollections = false) { |
|
| 267 | + return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, |
|
| 268 | + $parameters, -1, $includeCollections); |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Share an item with a user, group, or via private link |
|
| 273 | + * @param string $itemType |
|
| 274 | + * @param string $itemSource |
|
| 275 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 276 | + * @param string $shareWith User or group the item is being shared with |
|
| 277 | + * @param int $permissions CRUDS |
|
| 278 | + * @param string $itemSourceName |
|
| 279 | + * @param \DateTime|null $expirationDate |
|
| 280 | + * @return boolean|string Returns true on success or false on failure, Returns token on success for links |
|
| 281 | + * @throws \OC\HintException when the share type is remote and the shareWith is invalid |
|
| 282 | + * @throws \Exception |
|
| 283 | + * @since 5.0.0 - parameter $itemSourceName was added in 6.0.0, parameter $expirationDate was added in 7.0.0, parameter $passwordChanged added in 9.0.0 |
|
| 284 | + * @deprecated 14.0.0 TESTS ONLY - this methods is as of 2018-06 only used by tests |
|
| 285 | + * called like this: |
|
| 286 | + * \OC\Share\Share::shareItem('test', 1, \OCP\Share::SHARE_TYPE_USER, $otherUserId, \OCP\Constants::PERMISSION_READ); |
|
| 287 | + */ |
|
| 288 | + public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions) { |
|
| 289 | + $backend = self::getBackend($itemType); |
|
| 290 | + |
|
| 291 | + if ($backend->isShareTypeAllowed($shareType) === false) { |
|
| 292 | + $message = 'Sharing failed, because the backend does not allow shares from type %i'; |
|
| 293 | + throw new \Exception(sprintf($message, $shareType)); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $uidOwner = \OC_User::getUser(); |
|
| 297 | + |
|
| 298 | + // Verify share type and sharing conditions are met |
|
| 299 | + if ($shareType === self::SHARE_TYPE_USER) { |
|
| 300 | + if ($shareWith == $uidOwner) { |
|
| 301 | + $message = 'Sharing failed, because you can not share with yourself'; |
|
| 302 | + throw new \Exception($message); |
|
| 303 | + } |
|
| 304 | + if (!\OC::$server->getUserManager()->userExists($shareWith)) { |
|
| 305 | + $message = 'Sharing failed, because the user %s does not exist'; |
|
| 306 | + throw new \Exception(sprintf($message, $shareWith)); |
|
| 307 | + } |
|
| 308 | + // Check if the item source is already shared with the user, either from the same owner or a different user |
|
| 309 | + if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, |
|
| 310 | + $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 311 | + // Only allow the same share to occur again if it is the same |
|
| 312 | + // owner and is not a user share, this use case is for increasing |
|
| 313 | + // permissions for a specific user |
|
| 314 | + if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 315 | + $message = 'Sharing failed, because this item is already shared with %s'; |
|
| 316 | + throw new \Exception(sprintf($message, $shareWith)); |
|
| 317 | + } |
|
| 318 | + } |
|
| 319 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, |
|
| 320 | + $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 321 | + // Only allow the same share to occur again if it is the same |
|
| 322 | + // owner and is not a user share, this use case is for increasing |
|
| 323 | + // permissions for a specific user |
|
| 324 | + if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 325 | + $message = 'Sharing failed, because this item is already shared with user %s'; |
|
| 326 | + throw new \Exception(sprintf($message, $shareWith)); |
|
| 327 | + } |
|
| 328 | + } |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + // Put the item into the database |
|
| 332 | + $result = self::put('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions); |
|
| 333 | + |
|
| 334 | + return $result ? true : false; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Unshare an item from a user, group, or delete a private link |
|
| 339 | + * @param string $itemType |
|
| 340 | + * @param string $itemSource |
|
| 341 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 342 | + * @param string $shareWith User or group the item is being shared with |
|
| 343 | + * @param string $owner owner of the share, if null the current user is used |
|
| 344 | + * @return boolean true on success or false on failure |
|
| 345 | + */ |
|
| 346 | + public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
| 347 | + |
|
| 348 | + // check if it is a valid itemType |
|
| 349 | + self::getBackend($itemType); |
|
| 350 | + |
|
| 351 | + $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
| 352 | + |
|
| 353 | + $toDelete = array(); |
|
| 354 | + $newParent = null; |
|
| 355 | + $currentUser = $owner ? $owner : \OC_User::getUser(); |
|
| 356 | + foreach ($items as $item) { |
|
| 357 | + // delete the item with the expected share_type and owner |
|
| 358 | + if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 359 | + $toDelete = $item; |
|
| 360 | + // if there is more then one result we don't have to delete the children |
|
| 361 | + // but update their parent. For group shares the new parent should always be |
|
| 362 | + // the original group share and not the db entry with the unique name |
|
| 363 | + } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 364 | + $newParent = $item['parent']; |
|
| 365 | + } else { |
|
| 366 | + $newParent = $item['id']; |
|
| 367 | + } |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + if (!empty($toDelete)) { |
|
| 371 | + self::unshareItem($toDelete, $newParent); |
|
| 372 | + return true; |
|
| 373 | + } |
|
| 374 | + return false; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + /** |
|
| 378 | + * Checks whether a share has expired, calls unshareItem() if yes. |
|
| 379 | + * @param array $item Share data (usually database row) |
|
| 380 | + * @return boolean True if item was expired, false otherwise. |
|
| 381 | + */ |
|
| 382 | + protected static function expireItem(array $item) { |
|
| 383 | + |
|
| 384 | + $result = false; |
|
| 385 | + |
|
| 386 | + // only use default expiration date for link shares |
|
| 387 | + if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 388 | + |
|
| 389 | + // calculate expiration date |
|
| 390 | + if (!empty($item['expiration'])) { |
|
| 391 | + $userDefinedExpire = new \DateTime($item['expiration']); |
|
| 392 | + $expires = $userDefinedExpire->getTimestamp(); |
|
| 393 | + } else { |
|
| 394 | + $expires = null; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + |
|
| 398 | + // get default expiration settings |
|
| 399 | + $defaultSettings = Helper::getDefaultExpireSetting(); |
|
| 400 | + $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); |
|
| 401 | + |
|
| 402 | + |
|
| 403 | + if (is_int($expires)) { |
|
| 404 | + $now = time(); |
|
| 405 | + if ($now > $expires) { |
|
| 406 | + self::unshareItem($item); |
|
| 407 | + $result = true; |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + } |
|
| 411 | + return $result; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + /** |
|
| 415 | + * Unshares a share given a share data array |
|
| 416 | + * @param array $item Share data (usually database row) |
|
| 417 | + * @param int $newParent parent ID |
|
| 418 | + * @return null |
|
| 419 | + */ |
|
| 420 | + protected static function unshareItem(array $item, $newParent = null) { |
|
| 421 | + |
|
| 422 | + $shareType = (int)$item['share_type']; |
|
| 423 | + $shareWith = null; |
|
| 424 | + if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
|
| 425 | + $shareWith = $item['share_with']; |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + // Pass all the vars we have for now, they may be useful |
|
| 429 | + $hookParams = array( |
|
| 430 | + 'id' => $item['id'], |
|
| 431 | + 'itemType' => $item['item_type'], |
|
| 432 | + 'itemSource' => $item['item_source'], |
|
| 433 | + 'shareType' => $shareType, |
|
| 434 | + 'shareWith' => $shareWith, |
|
| 435 | + 'itemParent' => $item['parent'], |
|
| 436 | + 'uidOwner' => $item['uid_owner'], |
|
| 437 | + ); |
|
| 438 | + if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 439 | + $hookParams['fileSource'] = $item['file_source']; |
|
| 440 | + $hookParams['fileTarget'] = $item['file_target']; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + \OC_Hook::emit(\OCP\Share::class, 'pre_unshare', $hookParams); |
|
| 444 | + $deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
| 445 | + $deletedShares[] = $hookParams; |
|
| 446 | + $hookParams['deletedShares'] = $deletedShares; |
|
| 447 | + \OC_Hook::emit(\OCP\Share::class, 'post_unshare', $hookParams); |
|
| 448 | + if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 449 | + list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
| 450 | + self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
| 451 | + } |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Get the backend class for the specified item type |
|
| 456 | + * @param string $itemType |
|
| 457 | + * @throws \Exception |
|
| 458 | + * @return \OCP\Share_Backend |
|
| 459 | + */ |
|
| 460 | + public static function getBackend($itemType) { |
|
| 461 | + $l = \OC::$server->getL10N('lib'); |
|
| 462 | + if (isset(self::$backends[$itemType])) { |
|
| 463 | + return self::$backends[$itemType]; |
|
| 464 | + } else if (isset(self::$backendTypes[$itemType]['class'])) { |
|
| 465 | + $class = self::$backendTypes[$itemType]['class']; |
|
| 466 | + if (class_exists($class)) { |
|
| 467 | + self::$backends[$itemType] = new $class; |
|
| 468 | + if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) { |
|
| 469 | + $message = 'Sharing backend %s must implement the interface OCP\Share_Backend'; |
|
| 470 | + $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class)); |
|
| 471 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), ILogger::ERROR); |
|
| 472 | + throw new \Exception($message_t); |
|
| 473 | + } |
|
| 474 | + return self::$backends[$itemType]; |
|
| 475 | + } else { |
|
| 476 | + $message = 'Sharing backend %s not found'; |
|
| 477 | + $message_t = $l->t('Sharing backend %s not found', array($class)); |
|
| 478 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), ILogger::ERROR); |
|
| 479 | + throw new \Exception($message_t); |
|
| 480 | + } |
|
| 481 | + } |
|
| 482 | + $message = 'Sharing backend for %s not found'; |
|
| 483 | + $message_t = $l->t('Sharing backend for %s not found', array($itemType)); |
|
| 484 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), ILogger::ERROR); |
|
| 485 | + throw new \Exception($message_t); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Check if resharing is allowed |
|
| 490 | + * @return boolean true if allowed or false |
|
| 491 | + * |
|
| 492 | + * Resharing is allowed by default if not configured |
|
| 493 | + */ |
|
| 494 | + public static function isResharingAllowed() { |
|
| 495 | + if (!isset(self::$isResharingAllowed)) { |
|
| 496 | + if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') { |
|
| 497 | + self::$isResharingAllowed = true; |
|
| 498 | + } else { |
|
| 499 | + self::$isResharingAllowed = false; |
|
| 500 | + } |
|
| 501 | + } |
|
| 502 | + return self::$isResharingAllowed; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Get a list of collection item types for the specified item type |
|
| 507 | + * @param string $itemType |
|
| 508 | + * @return array |
|
| 509 | + */ |
|
| 510 | + private static function getCollectionItemTypes($itemType) { |
|
| 511 | + $collectionTypes = array($itemType); |
|
| 512 | + foreach (self::$backendTypes as $type => $backend) { |
|
| 513 | + if (in_array($backend['collectionOf'], $collectionTypes)) { |
|
| 514 | + $collectionTypes[] = $type; |
|
| 515 | + } |
|
| 516 | + } |
|
| 517 | + // TODO Add option for collections to be collection of themselves, only 'folder' does it now... |
|
| 518 | + if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) { |
|
| 519 | + unset($collectionTypes[0]); |
|
| 520 | + } |
|
| 521 | + // Return array if collections were found or the item type is a |
|
| 522 | + // collection itself - collections can be inside collections |
|
| 523 | + if (count($collectionTypes) > 0) { |
|
| 524 | + return $collectionTypes; |
|
| 525 | + } |
|
| 526 | + return false; |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Get the owners of items shared with a user. |
|
| 531 | + * |
|
| 532 | + * @param string $user The user the items are shared with. |
|
| 533 | + * @param string $type The type of the items shared with the user. |
|
| 534 | + * @param boolean $includeCollections Include collection item types (optional) |
|
| 535 | + * @param boolean $includeOwner include owner in the list of users the item is shared with (optional) |
|
| 536 | + * @return array |
|
| 537 | + * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 538 | + * called like this: |
|
| 539 | + * \OC\Share\Share::getSharedItemsOwners($this->user, $this->type, true) |
|
| 540 | + */ |
|
| 541 | + public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) { |
|
| 542 | + // First, we find out if $type is part of a collection (and if that collection is part of |
|
| 543 | + // another one and so on). |
|
| 544 | + $collectionTypes = array(); |
|
| 545 | + if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) { |
|
| 546 | + $collectionTypes[] = $type; |
|
| 547 | + } |
|
| 548 | + |
|
| 549 | + // Of these collection types, along with our original $type, we make a |
|
| 550 | + // list of the ones for which a sharing backend has been registered. |
|
| 551 | + // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it |
|
| 552 | + // with its $includeCollections parameter set to true. Unfortunately, this fails currently. |
|
| 553 | + $allMaybeSharedItems = array(); |
|
| 554 | + foreach ($collectionTypes as $collectionType) { |
|
| 555 | + if (isset(self::$backends[$collectionType])) { |
|
| 556 | + $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser( |
|
| 557 | + $collectionType, |
|
| 558 | + $user, |
|
| 559 | + self::FORMAT_NONE |
|
| 560 | + ); |
|
| 561 | + } |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + $owners = array(); |
|
| 565 | + if ($includeOwner) { |
|
| 566 | + $owners[] = $user; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + // We take a look at all shared items of the given $type (or of the collections it is part of) |
|
| 570 | + // and find out their owners. Then, we gather the tags for the original $type from all owners, |
|
| 571 | + // and return them as elements of a list that look like "Tag (owner)". |
|
| 572 | + foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) { |
|
| 573 | + foreach ($maybeSharedItems as $sharedItem) { |
|
| 574 | + if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814 |
|
| 575 | + $owners[] = $sharedItem['uid_owner']; |
|
| 576 | + } |
|
| 577 | + } |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + return $owners; |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + /** |
|
| 584 | + * Get shared items from the database |
|
| 585 | + * @param string $itemType |
|
| 586 | + * @param string $item Item source or target (optional) |
|
| 587 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique |
|
| 588 | + * @param string $shareWith User or group the item is being shared with |
|
| 589 | + * @param string $uidOwner User that is the owner of shared items (optional) |
|
| 590 | + * @param int $format Format to convert items to with formatItems() (optional) |
|
| 591 | + * @param mixed $parameters to pass to formatItems() (optional) |
|
| 592 | + * @param int $limit Number of items to return, -1 to return all matches (optional) |
|
| 593 | + * @param boolean $includeCollections Include collection item types (optional) |
|
| 594 | + * @param boolean $itemShareWithBySource (optional) |
|
| 595 | + * @param boolean $checkExpireDate |
|
| 596 | + * @return array |
|
| 597 | + * |
|
| 598 | + * See public functions getItem(s)... for parameter usage |
|
| 599 | + * |
|
| 600 | + */ |
|
| 601 | + public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, |
|
| 602 | + $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, |
|
| 603 | + $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) { |
|
| 604 | + if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_enabled', 'yes') != 'yes') { |
|
| 605 | + return array(); |
|
| 606 | + } |
|
| 607 | + $backend = self::getBackend($itemType); |
|
| 608 | + $collectionTypes = false; |
|
| 609 | + // Get filesystem root to add it to the file target and remove from the |
|
| 610 | + // file source, match file_source with the file cache |
|
| 611 | + if ($itemType == 'file' || $itemType == 'folder') { |
|
| 612 | + if(!is_null($uidOwner)) { |
|
| 613 | + $root = \OC\Files\Filesystem::getRoot(); |
|
| 614 | + } else { |
|
| 615 | + $root = ''; |
|
| 616 | + } |
|
| 617 | + $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 618 | + if (!isset($item)) { |
|
| 619 | + $where .= ' AND `file_target` IS NOT NULL '; |
|
| 620 | + } |
|
| 621 | + $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 622 | + $fileDependent = true; |
|
| 623 | + $queryArgs = array(); |
|
| 624 | + } else { |
|
| 625 | + $fileDependent = false; |
|
| 626 | + $root = ''; |
|
| 627 | + $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 628 | + if ($includeCollections && !isset($item) && $collectionTypes) { |
|
| 629 | + // If includeCollections is true, find collections of this item type, e.g. a music album contains songs |
|
| 630 | + if (!in_array($itemType, $collectionTypes)) { |
|
| 631 | + $itemTypes = array_merge(array($itemType), $collectionTypes); |
|
| 632 | + } else { |
|
| 633 | + $itemTypes = $collectionTypes; |
|
| 634 | + } |
|
| 635 | + $placeholders = implode(',', array_fill(0, count($itemTypes), '?')); |
|
| 636 | + $where = ' WHERE `item_type` IN ('.$placeholders.'))'; |
|
| 637 | + $queryArgs = $itemTypes; |
|
| 638 | + } else { |
|
| 639 | + $where = ' WHERE `item_type` = ?'; |
|
| 640 | + $queryArgs = array($itemType); |
|
| 641 | + } |
|
| 642 | + } |
|
| 643 | + if (\OC::$server->getConfig()->getAppValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 644 | + $where .= ' AND `share_type` != ?'; |
|
| 645 | + $queryArgs[] = self::SHARE_TYPE_LINK; |
|
| 646 | + } |
|
| 647 | + if (isset($shareType)) { |
|
| 648 | + // Include all user and group items |
|
| 649 | + if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) { |
|
| 650 | + $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) '; |
|
| 651 | + $queryArgs[] = self::SHARE_TYPE_USER; |
|
| 652 | + $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 653 | + $queryArgs[] = $shareWith; |
|
| 654 | + |
|
| 655 | + $user = \OC::$server->getUserManager()->get($shareWith); |
|
| 656 | + $groups = []; |
|
| 657 | + if ($user) { |
|
| 658 | + $groups = \OC::$server->getGroupManager()->getUserGroupIds($user); |
|
| 659 | + } |
|
| 660 | + if (!empty($groups)) { |
|
| 661 | + $placeholders = implode(',', array_fill(0, count($groups), '?')); |
|
| 662 | + $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; |
|
| 663 | + $queryArgs[] = self::SHARE_TYPE_GROUP; |
|
| 664 | + $queryArgs = array_merge($queryArgs, $groups); |
|
| 665 | + } |
|
| 666 | + $where .= ')'; |
|
| 667 | + // Don't include own group shares |
|
| 668 | + $where .= ' AND `uid_owner` != ?'; |
|
| 669 | + $queryArgs[] = $shareWith; |
|
| 670 | + } else { |
|
| 671 | + $where .= ' AND `share_type` = ?'; |
|
| 672 | + $queryArgs[] = $shareType; |
|
| 673 | + if (isset($shareWith)) { |
|
| 674 | + $where .= ' AND `share_with` = ?'; |
|
| 675 | + $queryArgs[] = $shareWith; |
|
| 676 | + } |
|
| 677 | + } |
|
| 678 | + } |
|
| 679 | + if (isset($uidOwner)) { |
|
| 680 | + $where .= ' AND `uid_owner` = ?'; |
|
| 681 | + $queryArgs[] = $uidOwner; |
|
| 682 | + if (!isset($shareType)) { |
|
| 683 | + // Prevent unique user targets for group shares from being selected |
|
| 684 | + $where .= ' AND `share_type` != ?'; |
|
| 685 | + $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 686 | + } |
|
| 687 | + if ($fileDependent) { |
|
| 688 | + $column = 'file_source'; |
|
| 689 | + } else { |
|
| 690 | + $column = 'item_source'; |
|
| 691 | + } |
|
| 692 | + } else { |
|
| 693 | + if ($fileDependent) { |
|
| 694 | + $column = 'file_target'; |
|
| 695 | + } else { |
|
| 696 | + $column = 'item_target'; |
|
| 697 | + } |
|
| 698 | + } |
|
| 699 | + if (isset($item)) { |
|
| 700 | + $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 701 | + if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 702 | + $where .= ' AND ('; |
|
| 703 | + } else { |
|
| 704 | + $where .= ' AND'; |
|
| 705 | + } |
|
| 706 | + // If looking for own shared items, check item_source else check item_target |
|
| 707 | + if (isset($uidOwner) || $itemShareWithBySource) { |
|
| 708 | + // If item type is a file, file source needs to be checked in case the item was converted |
|
| 709 | + if ($fileDependent) { |
|
| 710 | + $where .= ' `file_source` = ?'; |
|
| 711 | + $column = 'file_source'; |
|
| 712 | + } else { |
|
| 713 | + $where .= ' `item_source` = ?'; |
|
| 714 | + $column = 'item_source'; |
|
| 715 | + } |
|
| 716 | + } else { |
|
| 717 | + if ($fileDependent) { |
|
| 718 | + $where .= ' `file_target` = ?'; |
|
| 719 | + $item = \OC\Files\Filesystem::normalizePath($item); |
|
| 720 | + } else { |
|
| 721 | + $where .= ' `item_target` = ?'; |
|
| 722 | + } |
|
| 723 | + } |
|
| 724 | + $queryArgs[] = $item; |
|
| 725 | + if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 726 | + $placeholders = implode(',', array_fill(0, count($collectionTypes), '?')); |
|
| 727 | + $where .= ' OR `item_type` IN ('.$placeholders.'))'; |
|
| 728 | + $queryArgs = array_merge($queryArgs, $collectionTypes); |
|
| 729 | + } |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) { |
|
| 733 | + // Make sure the unique user target is returned if it exists, |
|
| 734 | + // unique targets should follow the group share in the database |
|
| 735 | + // If the limit is not 1, the filtering can be done later |
|
| 736 | + $where .= ' ORDER BY `*PREFIX*share`.`id` DESC'; |
|
| 737 | + } else { |
|
| 738 | + $where .= ' ORDER BY `*PREFIX*share`.`id` ASC'; |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + if ($limit != -1 && !$includeCollections) { |
|
| 742 | + // The limit must be at least 3, because filtering needs to be done |
|
| 743 | + if ($limit < 3) { |
|
| 744 | + $queryLimit = 3; |
|
| 745 | + } else { |
|
| 746 | + $queryLimit = $limit; |
|
| 747 | + } |
|
| 748 | + } else { |
|
| 749 | + $queryLimit = null; |
|
| 750 | + } |
|
| 751 | + $select = self::createSelectStatement($format, $fileDependent, $uidOwner); |
|
| 752 | + $root = strlen($root); |
|
| 753 | + $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit); |
|
| 754 | + $result = $query->execute($queryArgs); |
|
| 755 | + if ($result === false) { |
|
| 756 | + \OCP\Util::writeLog('OCP\Share', |
|
| 757 | + \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 758 | + ILogger::ERROR); |
|
| 759 | + } |
|
| 760 | + $items = array(); |
|
| 761 | + $targets = array(); |
|
| 762 | + $switchedItems = array(); |
|
| 763 | + $mounts = array(); |
|
| 764 | + while ($row = $result->fetchRow()) { |
|
| 765 | + self::transformDBResults($row); |
|
| 766 | + // Filter out duplicate group shares for users with unique targets |
|
| 767 | + if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 768 | + continue; |
|
| 769 | + } |
|
| 770 | + if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { |
|
| 771 | + $row['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 772 | + $row['unique_name'] = true; // remember that we use a unique name for this user |
|
| 773 | + $row['share_with'] = $items[$row['parent']]['share_with']; |
|
| 774 | + // if the group share was unshared from the user we keep the permission, otherwise |
|
| 775 | + // we take the permission from the parent because this is always the up-to-date |
|
| 776 | + // permission for the group share |
|
| 777 | + if ($row['permissions'] > 0) { |
|
| 778 | + $row['permissions'] = $items[$row['parent']]['permissions']; |
|
| 779 | + } |
|
| 780 | + // Remove the parent group share |
|
| 781 | + unset($items[$row['parent']]); |
|
| 782 | + if ($row['permissions'] == 0) { |
|
| 783 | + continue; |
|
| 784 | + } |
|
| 785 | + } else if (!isset($uidOwner)) { |
|
| 786 | + // Check if the same target already exists |
|
| 787 | + if (isset($targets[$row['id']])) { |
|
| 788 | + // Check if the same owner shared with the user twice |
|
| 789 | + // through a group and user share - this is allowed |
|
| 790 | + $id = $targets[$row['id']]; |
|
| 791 | + if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) { |
|
| 792 | + // Switch to group share type to ensure resharing conditions aren't bypassed |
|
| 793 | + if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { |
|
| 794 | + $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 795 | + $items[$id]['share_with'] = $row['share_with']; |
|
| 796 | + } |
|
| 797 | + // Switch ids if sharing permission is granted on only |
|
| 798 | + // one share to ensure correct parent is used if resharing |
|
| 799 | + if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 800 | + && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 801 | + $items[$row['id']] = $items[$id]; |
|
| 802 | + $switchedItems[$id] = $row['id']; |
|
| 803 | + unset($items[$id]); |
|
| 804 | + $id = $row['id']; |
|
| 805 | + } |
|
| 806 | + $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 807 | + |
|
| 808 | + } |
|
| 809 | + continue; |
|
| 810 | + } elseif (!empty($row['parent'])) { |
|
| 811 | + $targets[$row['parent']] = $row['id']; |
|
| 812 | + } |
|
| 813 | + } |
|
| 814 | + // Remove root from file source paths if retrieving own shared items |
|
| 815 | + if (isset($uidOwner) && isset($row['path'])) { |
|
| 816 | + if (isset($row['parent'])) { |
|
| 817 | + $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
|
| 818 | + $parentResult = $query->execute(array($row['parent'])); |
|
| 819 | + if ($result === false) { |
|
| 820 | + \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 821 | + \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 822 | + ILogger::ERROR); |
|
| 823 | + } else { |
|
| 824 | + $parentRow = $parentResult->fetchRow(); |
|
| 825 | + $tmpPath = $parentRow['file_target']; |
|
| 826 | + // find the right position where the row path continues from the target path |
|
| 827 | + $pos = strrpos($row['path'], $parentRow['file_target']); |
|
| 828 | + $subPath = substr($row['path'], $pos); |
|
| 829 | + $splitPath = explode('/', $subPath); |
|
| 830 | + foreach (array_slice($splitPath, 2) as $pathPart) { |
|
| 831 | + $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 832 | + } |
|
| 833 | + $row['path'] = $tmpPath; |
|
| 834 | + } |
|
| 835 | + } else { |
|
| 836 | + if (!isset($mounts[$row['storage']])) { |
|
| 837 | + $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']); |
|
| 838 | + if (is_array($mountPoints) && !empty($mountPoints)) { |
|
| 839 | + $mounts[$row['storage']] = current($mountPoints); |
|
| 840 | + } |
|
| 841 | + } |
|
| 842 | + if (!empty($mounts[$row['storage']])) { |
|
| 843 | + $path = $mounts[$row['storage']]->getMountPoint().$row['path']; |
|
| 844 | + $relPath = substr($path, $root); // path relative to data/user |
|
| 845 | + $row['path'] = rtrim($relPath, '/'); |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + } |
|
| 849 | + |
|
| 850 | + if($checkExpireDate) { |
|
| 851 | + if (self::expireItem($row)) { |
|
| 852 | + continue; |
|
| 853 | + } |
|
| 854 | + } |
|
| 855 | + // Check if resharing is allowed, if not remove share permission |
|
| 856 | + if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) { |
|
| 857 | + $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 858 | + } |
|
| 859 | + // Add display names to result |
|
| 860 | + $row['share_with_displayname'] = $row['share_with']; |
|
| 861 | + if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 862 | + $row['share_type'] === self::SHARE_TYPE_USER) { |
|
| 863 | + $shareWithUser = \OC::$server->getUserManager()->get($row['share_with']); |
|
| 864 | + $row['share_with_displayname'] = $shareWithUser === null ? $row['share_with'] : $shareWithUser->getDisplayName(); |
|
| 865 | + } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 866 | + $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
|
| 867 | + $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
|
| 868 | + foreach ($addressBookEntries as $entry) { |
|
| 869 | + foreach ($entry['CLOUD'] as $cloudID) { |
|
| 870 | + if ($cloudID === $row['share_with']) { |
|
| 871 | + $row['share_with_displayname'] = $entry['FN']; |
|
| 872 | + } |
|
| 873 | + } |
|
| 874 | + } |
|
| 875 | + } |
|
| 876 | + if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 877 | + $ownerUser = \OC::$server->getUserManager()->get($row['uid_owner']); |
|
| 878 | + $row['displayname_owner'] = $ownerUser === null ? $row['uid_owner'] : $ownerUser->getDisplayName(); |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + if ($row['permissions'] > 0) { |
|
| 882 | + $items[$row['id']] = $row; |
|
| 883 | + } |
|
| 884 | + |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + // group items if we are looking for items shared with the current user |
|
| 888 | + if (isset($shareWith) && $shareWith === \OCP\User::getUser()) { |
|
| 889 | + $items = self::groupItems($items, $itemType); |
|
| 890 | + } |
|
| 891 | + |
|
| 892 | + if (!empty($items)) { |
|
| 893 | + $collectionItems = array(); |
|
| 894 | + foreach ($items as &$row) { |
|
| 895 | + // Return only the item instead of a 2-dimensional array |
|
| 896 | + if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) { |
|
| 897 | + if ($format == self::FORMAT_NONE) { |
|
| 898 | + return $row; |
|
| 899 | + } else { |
|
| 900 | + break; |
|
| 901 | + } |
|
| 902 | + } |
|
| 903 | + // Check if this is a collection of the requested item type |
|
| 904 | + if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) { |
|
| 905 | + if (($collectionBackend = self::getBackend($row['item_type'])) |
|
| 906 | + && $collectionBackend instanceof \OCP\Share_Backend_Collection) { |
|
| 907 | + // Collections can be inside collections, check if the item is a collection |
|
| 908 | + if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) { |
|
| 909 | + $collectionItems[] = $row; |
|
| 910 | + } else { |
|
| 911 | + $collection = array(); |
|
| 912 | + $collection['item_type'] = $row['item_type']; |
|
| 913 | + if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 914 | + $collection['path'] = basename($row['path']); |
|
| 915 | + } |
|
| 916 | + $row['collection'] = $collection; |
|
| 917 | + // Fetch all of the children sources |
|
| 918 | + $children = $collectionBackend->getChildren($row[$column]); |
|
| 919 | + foreach ($children as $child) { |
|
| 920 | + $childItem = $row; |
|
| 921 | + $childItem['item_type'] = $itemType; |
|
| 922 | + if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') { |
|
| 923 | + $childItem['item_source'] = $child['source']; |
|
| 924 | + $childItem['item_target'] = $child['target']; |
|
| 925 | + } |
|
| 926 | + if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 927 | + if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 928 | + $childItem['file_source'] = $child['source']; |
|
| 929 | + } else { // TODO is this really needed if we already know that we use the file backend? |
|
| 930 | + $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']); |
|
| 931 | + $childItem['file_source'] = $meta['fileid']; |
|
| 932 | + } |
|
| 933 | + $childItem['file_target'] = |
|
| 934 | + \OC\Files\Filesystem::normalizePath($child['file_path']); |
|
| 935 | + } |
|
| 936 | + if (isset($item)) { |
|
| 937 | + if ($childItem[$column] == $item) { |
|
| 938 | + // Return only the item instead of a 2-dimensional array |
|
| 939 | + if ($limit == 1) { |
|
| 940 | + if ($format == self::FORMAT_NONE) { |
|
| 941 | + return $childItem; |
|
| 942 | + } else { |
|
| 943 | + // Unset the items array and break out of both loops |
|
| 944 | + $items = array(); |
|
| 945 | + $items[] = $childItem; |
|
| 946 | + break 2; |
|
| 947 | + } |
|
| 948 | + } else { |
|
| 949 | + $collectionItems[] = $childItem; |
|
| 950 | + } |
|
| 951 | + } |
|
| 952 | + } else { |
|
| 953 | + $collectionItems[] = $childItem; |
|
| 954 | + } |
|
| 955 | + } |
|
| 956 | + } |
|
| 957 | + } |
|
| 958 | + // Remove collection item |
|
| 959 | + $toRemove = $row['id']; |
|
| 960 | + if (array_key_exists($toRemove, $switchedItems)) { |
|
| 961 | + $toRemove = $switchedItems[$toRemove]; |
|
| 962 | + } |
|
| 963 | + unset($items[$toRemove]); |
|
| 964 | + } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) { |
|
| 965 | + // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 966 | + // see github issue #10588 for more details |
|
| 967 | + // Need to find a solution which works for all back-ends |
|
| 968 | + $collectionBackend = self::getBackend($row['item_type']); |
|
| 969 | + $sharedParents = $collectionBackend->getParents($row['item_source']); |
|
| 970 | + foreach ($sharedParents as $parent) { |
|
| 971 | + $collectionItems[] = $parent; |
|
| 972 | + } |
|
| 973 | + } |
|
| 974 | + } |
|
| 975 | + if (!empty($collectionItems)) { |
|
| 976 | + $collectionItems = array_unique($collectionItems, SORT_REGULAR); |
|
| 977 | + $items = array_merge($items, $collectionItems); |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + // filter out invalid items, these can appear when subshare entries exist |
|
| 981 | + // for a group in which the requested user isn't a member any more |
|
| 982 | + $items = array_filter($items, function($item) { |
|
| 983 | + return $item['share_type'] !== self::$shareTypeGroupUserUnique; |
|
| 984 | + }); |
|
| 985 | + |
|
| 986 | + return self::formatResult($items, $column, $backend, $format, $parameters); |
|
| 987 | + } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) { |
|
| 988 | + // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 989 | + // see github issue #10588 for more details |
|
| 990 | + // Need to find a solution which works for all back-ends |
|
| 991 | + $collectionItems = array(); |
|
| 992 | + $collectionBackend = self::getBackend('folder'); |
|
| 993 | + $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner); |
|
| 994 | + foreach ($sharedParents as $parent) { |
|
| 995 | + $collectionItems[] = $parent; |
|
| 996 | + } |
|
| 997 | + if ($limit === 1) { |
|
| 998 | + return reset($collectionItems); |
|
| 999 | + } |
|
| 1000 | + return self::formatResult($collectionItems, $column, $backend, $format, $parameters); |
|
| 1001 | + } |
|
| 1002 | + |
|
| 1003 | + return array(); |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + /** |
|
| 1007 | + * group items with link to the same source |
|
| 1008 | + * |
|
| 1009 | + * @param array $items |
|
| 1010 | + * @param string $itemType |
|
| 1011 | + * @return array of grouped items |
|
| 1012 | + */ |
|
| 1013 | + protected static function groupItems($items, $itemType) { |
|
| 1014 | + |
|
| 1015 | + $fileSharing = $itemType === 'file' || $itemType === 'folder'; |
|
| 1016 | + |
|
| 1017 | + $result = array(); |
|
| 1018 | + |
|
| 1019 | + foreach ($items as $item) { |
|
| 1020 | + $grouped = false; |
|
| 1021 | + foreach ($result as $key => $r) { |
|
| 1022 | + // for file/folder shares we need to compare file_source, otherwise we compare item_source |
|
| 1023 | + // only group shares if they already point to the same target, otherwise the file where shared |
|
| 1024 | + // before grouping of shares was added. In this case we don't group them toi avoid confusions |
|
| 1025 | + if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 1026 | + (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
|
| 1027 | + // add the first item to the list of grouped shares |
|
| 1028 | + if (!isset($result[$key]['grouped'])) { |
|
| 1029 | + $result[$key]['grouped'][] = $result[$key]; |
|
| 1030 | + } |
|
| 1031 | + $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions']; |
|
| 1032 | + $result[$key]['grouped'][] = $item; |
|
| 1033 | + $grouped = true; |
|
| 1034 | + break; |
|
| 1035 | + } |
|
| 1036 | + } |
|
| 1037 | + |
|
| 1038 | + if (!$grouped) { |
|
| 1039 | + $result[] = $item; |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + } |
|
| 1043 | + |
|
| 1044 | + return $result; |
|
| 1045 | + } |
|
| 1046 | + |
|
| 1047 | + /** |
|
| 1048 | + * Put shared item into the database |
|
| 1049 | + * @param string $itemType Item type |
|
| 1050 | + * @param string $itemSource Item source |
|
| 1051 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1052 | + * @param string $shareWith User or group the item is being shared with |
|
| 1053 | + * @param string $uidOwner User that is the owner of shared item |
|
| 1054 | + * @param int $permissions CRUDS permissions |
|
| 1055 | + * @throws \Exception |
|
| 1056 | + * @return mixed id of the new share or false |
|
| 1057 | + * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 1058 | + * called like this: |
|
| 1059 | + * self::put('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions); |
|
| 1060 | + */ |
|
| 1061 | + private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 1062 | + $permissions) { |
|
| 1063 | + |
|
| 1064 | + $queriesToExecute = array(); |
|
| 1065 | + $suggestedItemTarget = null; |
|
| 1066 | + $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = ''; |
|
| 1067 | + $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
|
| 1068 | + |
|
| 1069 | + $result = self::checkReshare('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions, null, null); |
|
| 1070 | + if(!empty($result)) { |
|
| 1071 | + $parent = $result['parent']; |
|
| 1072 | + $itemSource = $result['itemSource']; |
|
| 1073 | + $fileSource = $result['fileSource']; |
|
| 1074 | + $suggestedItemTarget = $result['suggestedItemTarget']; |
|
| 1075 | + $suggestedFileTarget = $result['suggestedFileTarget']; |
|
| 1076 | + $filePath = $result['filePath']; |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + $isGroupShare = false; |
|
| 1080 | + $users = array($shareWith); |
|
| 1081 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 1082 | + $suggestedItemTarget); |
|
| 1083 | + |
|
| 1084 | + $run = true; |
|
| 1085 | + $error = ''; |
|
| 1086 | + $preHookData = array( |
|
| 1087 | + 'itemType' => $itemType, |
|
| 1088 | + 'itemSource' => $itemSource, |
|
| 1089 | + 'shareType' => $shareType, |
|
| 1090 | + 'uidOwner' => $uidOwner, |
|
| 1091 | + 'permissions' => $permissions, |
|
| 1092 | + 'fileSource' => $fileSource, |
|
| 1093 | + 'expiration' => null, |
|
| 1094 | + 'token' => null, |
|
| 1095 | + 'run' => &$run, |
|
| 1096 | + 'error' => &$error |
|
| 1097 | + ); |
|
| 1098 | + |
|
| 1099 | + $preHookData['itemTarget'] = $itemTarget; |
|
| 1100 | + $preHookData['shareWith'] = $shareWith; |
|
| 1101 | + |
|
| 1102 | + \OC_Hook::emit(\OCP\Share::class, 'pre_shared', $preHookData); |
|
| 1103 | + |
|
| 1104 | + if ($run === false) { |
|
| 1105 | + throw new \Exception($error); |
|
| 1106 | + } |
|
| 1107 | + |
|
| 1108 | + foreach ($users as $user) { |
|
| 1109 | + $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource; |
|
| 1110 | + $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user); |
|
| 1111 | + |
|
| 1112 | + $userShareType = $shareType; |
|
| 1113 | + |
|
| 1114 | + if ($sourceExists && $sourceExists['item_source'] === $itemSource) { |
|
| 1115 | + $fileTarget = $sourceExists['file_target']; |
|
| 1116 | + $itemTarget = $sourceExists['item_target']; |
|
| 1117 | + |
|
| 1118 | + } elseif(!$sourceExists) { |
|
| 1119 | + |
|
| 1120 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
|
| 1121 | + $uidOwner, $suggestedItemTarget, $parent); |
|
| 1122 | + if (isset($fileSource)) { |
|
| 1123 | + $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, |
|
| 1124 | + $user, $uidOwner, $suggestedFileTarget, $parent); |
|
| 1125 | + } else { |
|
| 1126 | + $fileTarget = null; |
|
| 1127 | + } |
|
| 1128 | + |
|
| 1129 | + } else { |
|
| 1130 | + |
|
| 1131 | + // group share which doesn't exists until now, check if we need a unique target for this user |
|
| 1132 | + |
|
| 1133 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, |
|
| 1134 | + $uidOwner, $suggestedItemTarget, $parent); |
|
| 1135 | + |
|
| 1136 | + // do we also need a file target |
|
| 1137 | + if (isset($fileSource)) { |
|
| 1138 | + $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, |
|
| 1139 | + $uidOwner, $suggestedFileTarget, $parent); |
|
| 1140 | + } else { |
|
| 1141 | + $fileTarget = null; |
|
| 1142 | + } |
|
| 1143 | + |
|
| 1144 | + if (($itemTarget === $groupItemTarget) && |
|
| 1145 | + (!isset($fileSource) || $fileTarget === $groupFileTarget)) { |
|
| 1146 | + continue; |
|
| 1147 | + } |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + $queriesToExecute[] = array( |
|
| 1151 | + 'itemType' => $itemType, |
|
| 1152 | + 'itemSource' => $itemSource, |
|
| 1153 | + 'itemTarget' => $itemTarget, |
|
| 1154 | + 'shareType' => $userShareType, |
|
| 1155 | + 'shareWith' => $user, |
|
| 1156 | + 'uidOwner' => $uidOwner, |
|
| 1157 | + 'permissions' => $permissions, |
|
| 1158 | + 'shareTime' => time(), |
|
| 1159 | + 'fileSource' => $fileSource, |
|
| 1160 | + 'fileTarget' => $fileTarget, |
|
| 1161 | + 'token' => null, |
|
| 1162 | + 'parent' => $parent, |
|
| 1163 | + 'expiration' => null, |
|
| 1164 | + ); |
|
| 1165 | + |
|
| 1166 | + } |
|
| 1167 | + |
|
| 1168 | + $id = false; |
|
| 1169 | + |
|
| 1170 | + foreach ($queriesToExecute as $shareQuery) { |
|
| 1171 | + $shareQuery['parent'] = $parent; |
|
| 1172 | + $id = self::insertShare($shareQuery); |
|
| 1173 | + } |
|
| 1174 | + |
|
| 1175 | + $postHookData = array( |
|
| 1176 | + 'itemType' => $itemType, |
|
| 1177 | + 'itemSource' => $itemSource, |
|
| 1178 | + 'parent' => $parent, |
|
| 1179 | + 'shareType' => $shareType, |
|
| 1180 | + 'uidOwner' => $uidOwner, |
|
| 1181 | + 'permissions' => $permissions, |
|
| 1182 | + 'fileSource' => $fileSource, |
|
| 1183 | + 'id' => $parent, |
|
| 1184 | + 'token' => null, |
|
| 1185 | + 'expirationDate' => null, |
|
| 1186 | + ); |
|
| 1187 | + |
|
| 1188 | + $postHookData['shareWith'] = $isGroupShare ? $shareWith['group'] : $shareWith; |
|
| 1189 | + $postHookData['itemTarget'] = $isGroupShare ? $groupItemTarget : $itemTarget; |
|
| 1190 | + $postHookData['fileTarget'] = $isGroupShare ? $groupFileTarget : $fileTarget; |
|
| 1191 | + |
|
| 1192 | + \OC_Hook::emit(\OCP\Share::class, 'post_shared', $postHookData); |
|
| 1193 | + |
|
| 1194 | + |
|
| 1195 | + return $id ? $id : false; |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + /** |
|
| 1199 | + * @param string $itemType |
|
| 1200 | + * @param string $itemSource |
|
| 1201 | + * @param int $shareType |
|
| 1202 | + * @param string $shareWith |
|
| 1203 | + * @param string $uidOwner |
|
| 1204 | + * @param int $permissions |
|
| 1205 | + * @param string|null $itemSourceName |
|
| 1206 | + * @param null|\DateTime $expirationDate |
|
| 1207 | + * @deprecated TESTS ONLY - this methods is only used by tests |
|
| 1208 | + * called like this: |
|
| 1209 | + * self::checkReshare('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions, null, null); |
|
| 1210 | + */ |
|
| 1211 | + private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) { |
|
| 1212 | + $backend = self::getBackend($itemType); |
|
| 1213 | + |
|
| 1214 | + $result = array(); |
|
| 1215 | + |
|
| 1216 | + $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source'; |
|
| 1217 | + |
|
| 1218 | + $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true); |
|
| 1219 | + if ($checkReshare) { |
|
| 1220 | + // Check if attempting to share back to owner |
|
| 1221 | + if ($checkReshare['uid_owner'] == $shareWith) { |
|
| 1222 | + $message = 'Sharing %1$s failed, because the user %2$s is the original sharer'; |
|
| 1223 | + throw new \Exception(sprintf($message, $itemSourceName, $shareWith)); |
|
| 1224 | + } |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
|
| 1228 | + // Check if share permissions is granted |
|
| 1229 | + if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1230 | + if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 1231 | + $message = 'Sharing %1$s failed, because the permissions exceed permissions granted to %2$s'; |
|
| 1232 | + throw new \Exception(sprintf($message, $itemSourceName, $uidOwner)); |
|
| 1233 | + } else { |
|
| 1234 | + // TODO Don't check if inside folder |
|
| 1235 | + $result['parent'] = $checkReshare['id']; |
|
| 1236 | + |
|
| 1237 | + $result['expirationDate'] = $expirationDate; |
|
| 1238 | + // $checkReshare['expiration'] could be null and then is always less than any value |
|
| 1239 | + if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 1240 | + $result['expirationDate'] = $checkReshare['expiration']; |
|
| 1241 | + } |
|
| 1242 | + |
|
| 1243 | + // only suggest the same name as new target if it is a reshare of the |
|
| 1244 | + // same file/folder and not the reshare of a child |
|
| 1245 | + if ($checkReshare[$column] === $itemSource) { |
|
| 1246 | + $result['filePath'] = $checkReshare['file_target']; |
|
| 1247 | + $result['itemSource'] = $checkReshare['item_source']; |
|
| 1248 | + $result['fileSource'] = $checkReshare['file_source']; |
|
| 1249 | + $result['suggestedItemTarget'] = $checkReshare['item_target']; |
|
| 1250 | + $result['suggestedFileTarget'] = $checkReshare['file_target']; |
|
| 1251 | + } else { |
|
| 1252 | + $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null; |
|
| 1253 | + $result['suggestedItemTarget'] = null; |
|
| 1254 | + $result['suggestedFileTarget'] = null; |
|
| 1255 | + $result['itemSource'] = $itemSource; |
|
| 1256 | + $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null; |
|
| 1257 | + } |
|
| 1258 | + } |
|
| 1259 | + } else { |
|
| 1260 | + $message = 'Sharing %s failed, because resharing is not allowed'; |
|
| 1261 | + throw new \Exception(sprintf($message, $itemSourceName)); |
|
| 1262 | + } |
|
| 1263 | + } else { |
|
| 1264 | + $result['parent'] = null; |
|
| 1265 | + $result['suggestedItemTarget'] = null; |
|
| 1266 | + $result['suggestedFileTarget'] = null; |
|
| 1267 | + $result['itemSource'] = $itemSource; |
|
| 1268 | + $result['expirationDate'] = $expirationDate; |
|
| 1269 | + if (!$backend->isValidSource($itemSource, $uidOwner)) { |
|
| 1270 | + $message = 'Sharing %1$s failed, because the sharing backend for ' |
|
| 1271 | + .'%2$s could not find its source'; |
|
| 1272 | + throw new \Exception(sprintf($message, $itemSource, $itemType)); |
|
| 1273 | + } |
|
| 1274 | + if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 1275 | + $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner); |
|
| 1276 | + $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']); |
|
| 1277 | + $result['fileSource'] = $meta['fileid']; |
|
| 1278 | + if ($result['fileSource'] == -1) { |
|
| 1279 | + $message = 'Sharing %s failed, because the file could not be found in the file cache'; |
|
| 1280 | + throw new \Exception(sprintf($message, $itemSource)); |
|
| 1281 | + } |
|
| 1282 | + } else { |
|
| 1283 | + $result['filePath'] = null; |
|
| 1284 | + $result['fileSource'] = null; |
|
| 1285 | + } |
|
| 1286 | + } |
|
| 1287 | + |
|
| 1288 | + return $result; |
|
| 1289 | + } |
|
| 1290 | + |
|
| 1291 | + /** |
|
| 1292 | + * |
|
| 1293 | + * @param array $shareData |
|
| 1294 | + * @return mixed false in case of a failure or the id of the new share |
|
| 1295 | + */ |
|
| 1296 | + private static function insertShare(array $shareData) { |
|
| 1297 | + |
|
| 1298 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' |
|
| 1299 | + .' `item_type`, `item_source`, `item_target`, `share_type`,' |
|
| 1300 | + .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' |
|
| 1301 | + .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 1302 | + $query->bindValue(1, $shareData['itemType']); |
|
| 1303 | + $query->bindValue(2, $shareData['itemSource']); |
|
| 1304 | + $query->bindValue(3, $shareData['itemTarget']); |
|
| 1305 | + $query->bindValue(4, $shareData['shareType']); |
|
| 1306 | + $query->bindValue(5, $shareData['shareWith']); |
|
| 1307 | + $query->bindValue(6, $shareData['uidOwner']); |
|
| 1308 | + $query->bindValue(7, $shareData['permissions']); |
|
| 1309 | + $query->bindValue(8, $shareData['shareTime']); |
|
| 1310 | + $query->bindValue(9, $shareData['fileSource']); |
|
| 1311 | + $query->bindValue(10, $shareData['fileTarget']); |
|
| 1312 | + $query->bindValue(11, $shareData['token']); |
|
| 1313 | + $query->bindValue(12, $shareData['parent']); |
|
| 1314 | + $query->bindValue(13, $shareData['expiration'], 'datetime'); |
|
| 1315 | + $result = $query->execute(); |
|
| 1316 | + |
|
| 1317 | + $id = false; |
|
| 1318 | + if ($result) { |
|
| 1319 | + $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 1320 | + } |
|
| 1321 | + |
|
| 1322 | + return $id; |
|
| 1323 | + |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + /** |
|
| 1327 | + * construct select statement |
|
| 1328 | + * @param int $format |
|
| 1329 | + * @param boolean $fileDependent ist it a file/folder share or a generla share |
|
| 1330 | + * @param string $uidOwner |
|
| 1331 | + * @return string select statement |
|
| 1332 | + */ |
|
| 1333 | + private static function createSelectStatement($format, $fileDependent, $uidOwner = null) { |
|
| 1334 | + $select = '*'; |
|
| 1335 | + if ($format == self::FORMAT_STATUSES) { |
|
| 1336 | + if ($fileDependent) { |
|
| 1337 | + $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, ' |
|
| 1338 | + . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, ' |
|
| 1339 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 1340 | + . '`uid_initiator`'; |
|
| 1341 | + } else { |
|
| 1342 | + $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`'; |
|
| 1343 | + } |
|
| 1344 | + } else { |
|
| 1345 | + if (isset($uidOwner)) { |
|
| 1346 | + if ($fileDependent) { |
|
| 1347 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,' |
|
| 1348 | + . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,' |
|
| 1349 | + . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, ' |
|
| 1350 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 1351 | + } else { |
|
| 1352 | + $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,' |
|
| 1353 | + . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`'; |
|
| 1354 | + } |
|
| 1355 | + } else { |
|
| 1356 | + if ($fileDependent) { |
|
| 1357 | + if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) { |
|
| 1358 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, ' |
|
| 1359 | + . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, ' |
|
| 1360 | + . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 1361 | + . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`'; |
|
| 1362 | + } else { |
|
| 1363 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,' |
|
| 1364 | + . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,' |
|
| 1365 | + . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,' |
|
| 1366 | + . '`stime`, `expiration`, `token`, `storage`, `mail_send`,' |
|
| 1367 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 1368 | + } |
|
| 1369 | + } |
|
| 1370 | + } |
|
| 1371 | + } |
|
| 1372 | + return $select; |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + |
|
| 1376 | + /** |
|
| 1377 | + * transform db results |
|
| 1378 | + * @param array $row result |
|
| 1379 | + */ |
|
| 1380 | + private static function transformDBResults(&$row) { |
|
| 1381 | + if (isset($row['id'])) { |
|
| 1382 | + $row['id'] = (int) $row['id']; |
|
| 1383 | + } |
|
| 1384 | + if (isset($row['share_type'])) { |
|
| 1385 | + $row['share_type'] = (int) $row['share_type']; |
|
| 1386 | + } |
|
| 1387 | + if (isset($row['parent'])) { |
|
| 1388 | + $row['parent'] = (int) $row['parent']; |
|
| 1389 | + } |
|
| 1390 | + if (isset($row['file_parent'])) { |
|
| 1391 | + $row['file_parent'] = (int) $row['file_parent']; |
|
| 1392 | + } |
|
| 1393 | + if (isset($row['file_source'])) { |
|
| 1394 | + $row['file_source'] = (int) $row['file_source']; |
|
| 1395 | + } |
|
| 1396 | + if (isset($row['permissions'])) { |
|
| 1397 | + $row['permissions'] = (int) $row['permissions']; |
|
| 1398 | + } |
|
| 1399 | + if (isset($row['storage'])) { |
|
| 1400 | + $row['storage'] = (int) $row['storage']; |
|
| 1401 | + } |
|
| 1402 | + if (isset($row['stime'])) { |
|
| 1403 | + $row['stime'] = (int) $row['stime']; |
|
| 1404 | + } |
|
| 1405 | + if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) { |
|
| 1406 | + // discard expiration date for non-link shares, which might have been |
|
| 1407 | + // set by ancient bugs |
|
| 1408 | + $row['expiration'] = null; |
|
| 1409 | + } |
|
| 1410 | + } |
|
| 1411 | + |
|
| 1412 | + /** |
|
| 1413 | + * format result |
|
| 1414 | + * @param array $items result |
|
| 1415 | + * @param string $column is it a file share or a general share ('file_target' or 'item_target') |
|
| 1416 | + * @param \OCP\Share_Backend $backend sharing backend |
|
| 1417 | + * @param int $format |
|
| 1418 | + * @param array $parameters additional format parameters |
|
| 1419 | + * @return array format result |
|
| 1420 | + */ |
|
| 1421 | + private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 1422 | + if ($format === self::FORMAT_NONE) { |
|
| 1423 | + return $items; |
|
| 1424 | + } else if ($format === self::FORMAT_STATUSES) { |
|
| 1425 | + $statuses = array(); |
|
| 1426 | + foreach ($items as $item) { |
|
| 1427 | + if ($item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 1428 | + if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) { |
|
| 1429 | + continue; |
|
| 1430 | + } |
|
| 1431 | + $statuses[$item[$column]]['link'] = true; |
|
| 1432 | + } else if (!isset($statuses[$item[$column]])) { |
|
| 1433 | + $statuses[$item[$column]]['link'] = false; |
|
| 1434 | + } |
|
| 1435 | + if (!empty($item['file_target'])) { |
|
| 1436 | + $statuses[$item[$column]]['path'] = $item['path']; |
|
| 1437 | + } |
|
| 1438 | + } |
|
| 1439 | + return $statuses; |
|
| 1440 | + } else { |
|
| 1441 | + return $backend->formatItems($items, $format, $parameters); |
|
| 1442 | + } |
|
| 1443 | + } |
|
| 1444 | + |
|
| 1445 | + /** |
|
| 1446 | + * remove protocol from URL |
|
| 1447 | + * |
|
| 1448 | + * @param string $url |
|
| 1449 | + * @return string |
|
| 1450 | + */ |
|
| 1451 | + public static function removeProtocolFromUrl($url) { |
|
| 1452 | + if (strpos($url, 'https://') === 0) { |
|
| 1453 | + return substr($url, strlen('https://')); |
|
| 1454 | + } else if (strpos($url, 'http://') === 0) { |
|
| 1455 | + return substr($url, strlen('http://')); |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + return $url; |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + /** |
|
| 1462 | + * try http post first with https and then with http as a fallback |
|
| 1463 | + * |
|
| 1464 | + * @param string $remoteDomain |
|
| 1465 | + * @param string $urlSuffix |
|
| 1466 | + * @param array $fields post parameters |
|
| 1467 | + * @return array |
|
| 1468 | + */ |
|
| 1469 | + private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
| 1470 | + $protocol = 'https://'; |
|
| 1471 | + $result = [ |
|
| 1472 | + 'success' => false, |
|
| 1473 | + 'result' => '', |
|
| 1474 | + ]; |
|
| 1475 | + $try = 0; |
|
| 1476 | + $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
| 1477 | + while ($result['success'] === false && $try < 2) { |
|
| 1478 | + $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 1479 | + $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
| 1480 | + $client = \OC::$server->getHTTPClientService()->newClient(); |
|
| 1481 | + |
|
| 1482 | + try { |
|
| 1483 | + $response = $client->post( |
|
| 1484 | + $protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, |
|
| 1485 | + [ |
|
| 1486 | + 'body' => $fields, |
|
| 1487 | + 'connect_timeout' => 10, |
|
| 1488 | + ] |
|
| 1489 | + ); |
|
| 1490 | + |
|
| 1491 | + $result = ['success' => true, 'result' => $response->getBody()]; |
|
| 1492 | + } catch (\Exception $e) { |
|
| 1493 | + $result = ['success' => false, 'result' => $e->getMessage()]; |
|
| 1494 | + } |
|
| 1495 | + |
|
| 1496 | + $try++; |
|
| 1497 | + $protocol = 'http://'; |
|
| 1498 | + } |
|
| 1499 | + |
|
| 1500 | + return $result; |
|
| 1501 | + } |
|
| 1502 | + |
|
| 1503 | + /** |
|
| 1504 | + * send server-to-server unshare to remote server |
|
| 1505 | + * |
|
| 1506 | + * @param string $remote url |
|
| 1507 | + * @param int $id share id |
|
| 1508 | + * @param string $token |
|
| 1509 | + * @return bool |
|
| 1510 | + */ |
|
| 1511 | + private static function sendRemoteUnshare($remote, $id, $token) { |
|
| 1512 | + $url = rtrim($remote, '/'); |
|
| 1513 | + $fields = array('token' => $token, 'format' => 'json'); |
|
| 1514 | + $url = self::removeProtocolFromUrl($url); |
|
| 1515 | + $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
| 1516 | + $status = json_decode($result['result'], true); |
|
| 1517 | + |
|
| 1518 | + return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
| 1519 | + } |
|
| 1520 | + |
|
| 1521 | + /** |
|
| 1522 | + * @return int |
|
| 1523 | + */ |
|
| 1524 | + public static function getExpireInterval() { |
|
| 1525 | + return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1526 | + } |
|
| 1527 | + |
|
| 1528 | + /** |
|
| 1529 | + * Checks whether the given path is reachable for the given owner |
|
| 1530 | + * |
|
| 1531 | + * @param string $path path relative to files |
|
| 1532 | + * @param string $ownerStorageId storage id of the owner |
|
| 1533 | + * |
|
| 1534 | + * @return boolean true if file is reachable, false otherwise |
|
| 1535 | + */ |
|
| 1536 | + private static function isFileReachable($path, $ownerStorageId) { |
|
| 1537 | + // if outside the home storage, file is always considered reachable |
|
| 1538 | + if (!(substr($ownerStorageId, 0, 6) === 'home::' || |
|
| 1539 | + substr($ownerStorageId, 0, 13) === 'object::user:' |
|
| 1540 | + )) { |
|
| 1541 | + return true; |
|
| 1542 | + } |
|
| 1543 | + |
|
| 1544 | + // if inside the home storage, the file has to be under "/files/" |
|
| 1545 | + $path = ltrim($path, '/'); |
|
| 1546 | + if (substr($path, 0, 6) === 'files/') { |
|
| 1547 | + return true; |
|
| 1548 | + } |
|
| 1549 | + |
|
| 1550 | + return false; |
|
| 1551 | + } |
|
| 1552 | 1552 | } |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | |
| 154 | 154 | $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
| 155 | 155 | |
| 156 | - $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 156 | + $where .= ' `'.$column.'` = ? AND `item_type` = ? '; |
|
| 157 | 157 | $arguments = array($itemSource, $itemType); |
| 158 | 158 | // for link shares $user === null |
| 159 | 159 | if ($user !== null) { |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | $arguments[] = $owner; |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | - $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 174 | + $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$fileDependentWhere.$where); |
|
| 175 | 175 | |
| 176 | 176 | $result = \OC_DB::executeAudited($query, $arguments); |
| 177 | 177 | |
@@ -179,7 +179,7 @@ discard block |
||
| 179 | 179 | if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
| 180 | 180 | continue; |
| 181 | 181 | } |
| 182 | - if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 182 | + if ($fileDependent && (int) $row['file_parent'] === -1) { |
|
| 183 | 183 | // if it is a mount point we need to get the path from the mount manager |
| 184 | 184 | $mountManager = \OC\Files\Filesystem::getMountManager(); |
| 185 | 185 | $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
@@ -190,7 +190,7 @@ discard block |
||
| 190 | 190 | $row['path'] = $path; |
| 191 | 191 | } else { |
| 192 | 192 | \OC::$server->getLogger()->warning( |
| 193 | - 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 193 | + 'Could not resolve mount point for '.$row['storage_id'], |
|
| 194 | 194 | ['app' => 'OCP\Share'] |
| 195 | 195 | ); |
| 196 | 196 | } |
@@ -199,7 +199,7 @@ discard block |
||
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | //if didn't found a result than let's look for a group share. |
| 202 | - if(empty($shares) && $user !== null) { |
|
| 202 | + if (empty($shares) && $user !== null) { |
|
| 203 | 203 | $userObject = \OC::$server->getUserManager()->get($user); |
| 204 | 204 | $groups = []; |
| 205 | 205 | if ($userObject) { |
@@ -207,7 +207,7 @@ discard block |
||
| 207 | 207 | } |
| 208 | 208 | |
| 209 | 209 | if (!empty($groups)) { |
| 210 | - $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 210 | + $where = $fileDependentWhere.' WHERE `'.$column.'` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 211 | 211 | $arguments = array($itemSource, $itemType, $groups); |
| 212 | 212 | $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
| 213 | 213 | |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | // class isn't static anymore... |
| 222 | 222 | $conn = \OC::$server->getDatabaseConnection(); |
| 223 | 223 | $result = $conn->executeQuery( |
| 224 | - 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 224 | + 'SELECT '.$select.' FROM `*PREFIX*share` '.$where, |
|
| 225 | 225 | $arguments, |
| 226 | 226 | $types |
| 227 | 227 | ); |
@@ -355,12 +355,12 @@ discard block |
||
| 355 | 355 | $currentUser = $owner ? $owner : \OC_User::getUser(); |
| 356 | 356 | foreach ($items as $item) { |
| 357 | 357 | // delete the item with the expected share_type and owner |
| 358 | - if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 358 | + if ((int) $item['share_type'] === (int) $shareType && $item['uid_owner'] === $currentUser) { |
|
| 359 | 359 | $toDelete = $item; |
| 360 | 360 | // if there is more then one result we don't have to delete the children |
| 361 | 361 | // but update their parent. For group shares the new parent should always be |
| 362 | 362 | // the original group share and not the db entry with the unique name |
| 363 | - } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 363 | + } else if ((int) $item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 364 | 364 | $newParent = $item['parent']; |
| 365 | 365 | } else { |
| 366 | 366 | $newParent = $item['id']; |
@@ -419,7 +419,7 @@ discard block |
||
| 419 | 419 | */ |
| 420 | 420 | protected static function unshareItem(array $item, $newParent = null) { |
| 421 | 421 | |
| 422 | - $shareType = (int)$item['share_type']; |
|
| 422 | + $shareType = (int) $item['share_type']; |
|
| 423 | 423 | $shareWith = null; |
| 424 | 424 | if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
| 425 | 425 | $shareWith = $item['share_with']; |
@@ -435,7 +435,7 @@ discard block |
||
| 435 | 435 | 'itemParent' => $item['parent'], |
| 436 | 436 | 'uidOwner' => $item['uid_owner'], |
| 437 | 437 | ); |
| 438 | - if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 438 | + if ($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 439 | 439 | $hookParams['fileSource'] = $item['file_source']; |
| 440 | 440 | $hookParams['fileTarget'] = $item['file_target']; |
| 441 | 441 | } |
@@ -445,7 +445,7 @@ discard block |
||
| 445 | 445 | $deletedShares[] = $hookParams; |
| 446 | 446 | $hookParams['deletedShares'] = $deletedShares; |
| 447 | 447 | \OC_Hook::emit(\OCP\Share::class, 'post_unshare', $hookParams); |
| 448 | - if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 448 | + if ((int) $item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 449 | 449 | list(, $remote) = Helper::splitUserRemote($item['share_with']); |
| 450 | 450 | self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
| 451 | 451 | } |
@@ -609,7 +609,7 @@ discard block |
||
| 609 | 609 | // Get filesystem root to add it to the file target and remove from the |
| 610 | 610 | // file source, match file_source with the file cache |
| 611 | 611 | if ($itemType == 'file' || $itemType == 'folder') { |
| 612 | - if(!is_null($uidOwner)) { |
|
| 612 | + if (!is_null($uidOwner)) { |
|
| 613 | 613 | $root = \OC\Files\Filesystem::getRoot(); |
| 614 | 614 | } else { |
| 615 | 615 | $root = ''; |
@@ -754,7 +754,7 @@ discard block |
||
| 754 | 754 | $result = $query->execute($queryArgs); |
| 755 | 755 | if ($result === false) { |
| 756 | 756 | \OCP\Util::writeLog('OCP\Share', |
| 757 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 757 | + \OC_DB::getErrorMessage().', select='.$select.' where=', |
|
| 758 | 758 | ILogger::ERROR); |
| 759 | 759 | } |
| 760 | 760 | $items = array(); |
@@ -796,14 +796,14 @@ discard block |
||
| 796 | 796 | } |
| 797 | 797 | // Switch ids if sharing permission is granted on only |
| 798 | 798 | // one share to ensure correct parent is used if resharing |
| 799 | - if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 800 | - && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 799 | + if (~(int) $items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 800 | + && (int) $row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 801 | 801 | $items[$row['id']] = $items[$id]; |
| 802 | 802 | $switchedItems[$id] = $row['id']; |
| 803 | 803 | unset($items[$id]); |
| 804 | 804 | $id = $row['id']; |
| 805 | 805 | } |
| 806 | - $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 806 | + $items[$id]['permissions'] |= (int) $row['permissions']; |
|
| 807 | 807 | |
| 808 | 808 | } |
| 809 | 809 | continue; |
@@ -817,8 +817,8 @@ discard block |
||
| 817 | 817 | $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
| 818 | 818 | $parentResult = $query->execute(array($row['parent'])); |
| 819 | 819 | if ($result === false) { |
| 820 | - \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 821 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 820 | + \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: '. |
|
| 821 | + \OC_DB::getErrorMessage().', select='.$select.' where='.$where, |
|
| 822 | 822 | ILogger::ERROR); |
| 823 | 823 | } else { |
| 824 | 824 | $parentRow = $parentResult->fetchRow(); |
@@ -828,7 +828,7 @@ discard block |
||
| 828 | 828 | $subPath = substr($row['path'], $pos); |
| 829 | 829 | $splitPath = explode('/', $subPath); |
| 830 | 830 | foreach (array_slice($splitPath, 2) as $pathPart) { |
| 831 | - $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 831 | + $tmpPath = $tmpPath.'/'.$pathPart; |
|
| 832 | 832 | } |
| 833 | 833 | $row['path'] = $tmpPath; |
| 834 | 834 | } |
@@ -847,7 +847,7 @@ discard block |
||
| 847 | 847 | } |
| 848 | 848 | } |
| 849 | 849 | |
| 850 | - if($checkExpireDate) { |
|
| 850 | + if ($checkExpireDate) { |
|
| 851 | 851 | if (self::expireItem($row)) { |
| 852 | 852 | continue; |
| 853 | 853 | } |
@@ -858,11 +858,11 @@ discard block |
||
| 858 | 858 | } |
| 859 | 859 | // Add display names to result |
| 860 | 860 | $row['share_with_displayname'] = $row['share_with']; |
| 861 | - if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 861 | + if (isset($row['share_with']) && $row['share_with'] != '' && |
|
| 862 | 862 | $row['share_type'] === self::SHARE_TYPE_USER) { |
| 863 | 863 | $shareWithUser = \OC::$server->getUserManager()->get($row['share_with']); |
| 864 | 864 | $row['share_with_displayname'] = $shareWithUser === null ? $row['share_with'] : $shareWithUser->getDisplayName(); |
| 865 | - } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 865 | + } else if (isset($row['share_with']) && $row['share_with'] != '' && |
|
| 866 | 866 | $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
| 867 | 867 | $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
| 868 | 868 | foreach ($addressBookEntries as $entry) { |
@@ -873,7 +873,7 @@ discard block |
||
| 873 | 873 | } |
| 874 | 874 | } |
| 875 | 875 | } |
| 876 | - if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 876 | + if (isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 877 | 877 | $ownerUser = \OC::$server->getUserManager()->get($row['uid_owner']); |
| 878 | 878 | $row['displayname_owner'] = $ownerUser === null ? $row['uid_owner'] : $ownerUser->getDisplayName(); |
| 879 | 879 | } |
@@ -1022,7 +1022,7 @@ discard block |
||
| 1022 | 1022 | // for file/folder shares we need to compare file_source, otherwise we compare item_source |
| 1023 | 1023 | // only group shares if they already point to the same target, otherwise the file where shared |
| 1024 | 1024 | // before grouping of shares was added. In this case we don't group them toi avoid confusions |
| 1025 | - if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 1025 | + if (($fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 1026 | 1026 | (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
| 1027 | 1027 | // add the first item to the list of grouped shares |
| 1028 | 1028 | if (!isset($result[$key]['grouped'])) { |
@@ -1067,7 +1067,7 @@ discard block |
||
| 1067 | 1067 | $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
| 1068 | 1068 | |
| 1069 | 1069 | $result = self::checkReshare('test', $itemSource, self::SHARE_TYPE_USER, $shareWith, $uidOwner, $permissions, null, null); |
| 1070 | - if(!empty($result)) { |
|
| 1070 | + if (!empty($result)) { |
|
| 1071 | 1071 | $parent = $result['parent']; |
| 1072 | 1072 | $itemSource = $result['itemSource']; |
| 1073 | 1073 | $fileSource = $result['fileSource']; |
@@ -1115,7 +1115,7 @@ discard block |
||
| 1115 | 1115 | $fileTarget = $sourceExists['file_target']; |
| 1116 | 1116 | $itemTarget = $sourceExists['item_target']; |
| 1117 | 1117 | |
| 1118 | - } elseif(!$sourceExists) { |
|
| 1118 | + } elseif (!$sourceExists) { |
|
| 1119 | 1119 | |
| 1120 | 1120 | $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
| 1121 | 1121 | $uidOwner, $suggestedItemTarget, $parent); |
@@ -1226,8 +1226,8 @@ discard block |
||
| 1226 | 1226 | |
| 1227 | 1227 | if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
| 1228 | 1228 | // Check if share permissions is granted |
| 1229 | - if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1230 | - if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 1229 | + if (self::isResharingAllowed() && (int) $checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1230 | + if (~(int) $checkReshare['permissions'] & $permissions) { |
|
| 1231 | 1231 | $message = 'Sharing %1$s failed, because the permissions exceed permissions granted to %2$s'; |
| 1232 | 1232 | throw new \Exception(sprintf($message, $itemSourceName, $uidOwner)); |
| 1233 | 1233 | } else { |
@@ -1236,7 +1236,7 @@ discard block |
||
| 1236 | 1236 | |
| 1237 | 1237 | $result['expirationDate'] = $expirationDate; |
| 1238 | 1238 | // $checkReshare['expiration'] could be null and then is always less than any value |
| 1239 | - if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 1239 | + if (isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 1240 | 1240 | $result['expirationDate'] = $checkReshare['expiration']; |
| 1241 | 1241 | } |
| 1242 | 1242 | |
@@ -1316,7 +1316,7 @@ discard block |
||
| 1316 | 1316 | |
| 1317 | 1317 | $id = false; |
| 1318 | 1318 | if ($result) { |
| 1319 | - $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 1319 | + $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 1320 | 1320 | } |
| 1321 | 1321 | |
| 1322 | 1322 | return $id; |
@@ -1418,7 +1418,7 @@ discard block |
||
| 1418 | 1418 | * @param array $parameters additional format parameters |
| 1419 | 1419 | * @return array format result |
| 1420 | 1420 | */ |
| 1421 | - private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 1421 | + private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE, $parameters = null) { |
|
| 1422 | 1422 | if ($format === self::FORMAT_NONE) { |
| 1423 | 1423 | return $items; |
| 1424 | 1424 | } else if ($format === self::FORMAT_STATUSES) { |
@@ -1475,13 +1475,13 @@ discard block |
||
| 1475 | 1475 | $try = 0; |
| 1476 | 1476 | $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
| 1477 | 1477 | while ($result['success'] === false && $try < 2) { |
| 1478 | - $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 1478 | + $federationEndpoints = $discoveryService->discover($protocol.$remoteDomain, 'FEDERATED_SHARING'); |
|
| 1479 | 1479 | $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
| 1480 | 1480 | $client = \OC::$server->getHTTPClientService()->newClient(); |
| 1481 | 1481 | |
| 1482 | 1482 | try { |
| 1483 | 1483 | $response = $client->post( |
| 1484 | - $protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, |
|
| 1484 | + $protocol.$remoteDomain.$endpoint.$urlSuffix.'?format='.self::RESPONSE_FORMAT, |
|
| 1485 | 1485 | [ |
| 1486 | 1486 | 'body' => $fields, |
| 1487 | 1487 | 'connect_timeout' => 10, |
@@ -1522,7 +1522,7 @@ discard block |
||
| 1522 | 1522 | * @return int |
| 1523 | 1523 | */ |
| 1524 | 1524 | public static function getExpireInterval() { |
| 1525 | - return (int)\OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1525 | + return (int) \OC::$server->getConfig()->getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1526 | 1526 | } |
| 1527 | 1527 | |
| 1528 | 1528 | /** |