@@ -60,2865 +60,2865 @@ |
||
| 60 | 60 | */ |
| 61 | 61 | class Share extends Constants { |
| 62 | 62 | |
| 63 | - /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask |
|
| 64 | - * Construct permissions for share() and setPermissions with Or (|) e.g. |
|
| 65 | - * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE |
|
| 66 | - * |
|
| 67 | - * Check if permission is granted with And (&) e.g. Check if delete is |
|
| 68 | - * granted: if ($permissions & PERMISSION_DELETE) |
|
| 69 | - * |
|
| 70 | - * Remove permissions with And (&) and Not (~) e.g. Remove the update |
|
| 71 | - * permission: $permissions &= ~PERMISSION_UPDATE |
|
| 72 | - * |
|
| 73 | - * Apps are required to handle permissions on their own, this class only |
|
| 74 | - * stores and manages the permissions of shares |
|
| 75 | - * @see lib/public/constants.php |
|
| 76 | - */ |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * Register a sharing backend class that implements OCP\Share_Backend for an item type |
|
| 80 | - * @param string $itemType Item type |
|
| 81 | - * @param string $class Backend class |
|
| 82 | - * @param string $collectionOf (optional) Depends on item type |
|
| 83 | - * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files |
|
| 84 | - * @return boolean true if backend is registered or false if error |
|
| 85 | - */ |
|
| 86 | - public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { |
|
| 87 | - if (self::isEnabled()) { |
|
| 88 | - if (!isset(self::$backendTypes[$itemType])) { |
|
| 89 | - self::$backendTypes[$itemType] = array( |
|
| 90 | - 'class' => $class, |
|
| 91 | - 'collectionOf' => $collectionOf, |
|
| 92 | - 'supportedFileExtensions' => $supportedFileExtensions |
|
| 93 | - ); |
|
| 94 | - if(count(self::$backendTypes) === 1) { |
|
| 95 | - Util::addScript('core', 'merged-share-backend'); |
|
| 96 | - \OC_Util::addStyle('core', 'share'); |
|
| 97 | - } |
|
| 98 | - return true; |
|
| 99 | - } |
|
| 100 | - \OCP\Util::writeLog('OCP\Share', |
|
| 101 | - 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'] |
|
| 102 | - .' is already registered for '.$itemType, |
|
| 103 | - \OCP\Util::WARN); |
|
| 104 | - } |
|
| 105 | - return false; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Check if the Share API is enabled |
|
| 110 | - * @return boolean true if enabled or false |
|
| 111 | - * |
|
| 112 | - * The Share API is enabled by default if not configured |
|
| 113 | - */ |
|
| 114 | - public static function isEnabled() { |
|
| 115 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_enabled', 'yes') == 'yes') { |
|
| 116 | - return true; |
|
| 117 | - } |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Find which users can access a shared item |
|
| 123 | - * @param string $path to the file |
|
| 124 | - * @param string $ownerUser owner of the file |
|
| 125 | - * @param IUserManager $userManager |
|
| 126 | - * @param ILogger $logger |
|
| 127 | - * @param boolean $includeOwner include owner to the list of users with access to the file |
|
| 128 | - * @param boolean $returnUserPaths Return an array with the user => path map |
|
| 129 | - * @param boolean $recursive take all parent folders into account (default true) |
|
| 130 | - * @return array |
|
| 131 | - * @note $path needs to be relative to user data dir, e.g. 'file.txt' |
|
| 132 | - * not '/admin/data/file.txt' |
|
| 133 | - * @throws \OC\User\NoUserException |
|
| 134 | - */ |
|
| 135 | - public static function getUsersSharingFile($path, |
|
| 136 | - $ownerUser, |
|
| 137 | - IUserManager $userManager, |
|
| 138 | - ILogger $logger, |
|
| 139 | - $includeOwner = false, |
|
| 140 | - $returnUserPaths = false, |
|
| 141 | - $recursive = true) { |
|
| 142 | - $userObject = $userManager->get($ownerUser); |
|
| 143 | - |
|
| 144 | - if (is_null($userObject)) { |
|
| 145 | - $logger->error( |
|
| 146 | - sprintf( |
|
| 147 | - 'Backends provided no user object for %s', |
|
| 148 | - $ownerUser |
|
| 149 | - ), |
|
| 150 | - [ |
|
| 151 | - 'app' => 'files', |
|
| 152 | - ] |
|
| 153 | - ); |
|
| 154 | - throw new \OC\User\NoUserException('Backends provided no user object'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $ownerUser = $userObject->getUID(); |
|
| 158 | - |
|
| 159 | - Filesystem::initMountPoints($ownerUser); |
|
| 160 | - $shares = $sharePaths = $fileTargets = array(); |
|
| 161 | - $publicShare = false; |
|
| 162 | - $remoteShare = false; |
|
| 163 | - $source = -1; |
|
| 164 | - $cache = $mountPath = false; |
|
| 165 | - |
|
| 166 | - $view = new \OC\Files\View('/' . $ownerUser . '/files'); |
|
| 167 | - $meta = $view->getFileInfo($path); |
|
| 168 | - if ($meta) { |
|
| 169 | - $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); |
|
| 170 | - } else { |
|
| 171 | - // if the file doesn't exists yet we start with the parent folder |
|
| 172 | - $meta = $view->getFileInfo(dirname($path)); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - if($meta !== false) { |
|
| 176 | - $source = $meta['fileid']; |
|
| 177 | - $cache = new \OC\Files\Cache\Cache($meta['storage']); |
|
| 178 | - |
|
| 179 | - $mountPath = $meta->getMountPoint()->getMountPoint(); |
|
| 180 | - if ($mountPath !== false) { |
|
| 181 | - $mountPath = substr($mountPath, strlen('/' . $ownerUser . '/files')); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - $paths = []; |
|
| 186 | - while ($source !== -1) { |
|
| 187 | - // Fetch all shares with another user |
|
| 188 | - if (!$returnUserPaths) { |
|
| 189 | - $query = \OC_DB::prepare( |
|
| 190 | - 'SELECT `share_with`, `file_source`, `file_target` |
|
| 63 | + /** CRUDS permissions (Create, Read, Update, Delete, Share) using a bitmask |
|
| 64 | + * Construct permissions for share() and setPermissions with Or (|) e.g. |
|
| 65 | + * Give user read and update permissions: PERMISSION_READ | PERMISSION_UPDATE |
|
| 66 | + * |
|
| 67 | + * Check if permission is granted with And (&) e.g. Check if delete is |
|
| 68 | + * granted: if ($permissions & PERMISSION_DELETE) |
|
| 69 | + * |
|
| 70 | + * Remove permissions with And (&) and Not (~) e.g. Remove the update |
|
| 71 | + * permission: $permissions &= ~PERMISSION_UPDATE |
|
| 72 | + * |
|
| 73 | + * Apps are required to handle permissions on their own, this class only |
|
| 74 | + * stores and manages the permissions of shares |
|
| 75 | + * @see lib/public/constants.php |
|
| 76 | + */ |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * Register a sharing backend class that implements OCP\Share_Backend for an item type |
|
| 80 | + * @param string $itemType Item type |
|
| 81 | + * @param string $class Backend class |
|
| 82 | + * @param string $collectionOf (optional) Depends on item type |
|
| 83 | + * @param array $supportedFileExtensions (optional) List of supported file extensions if this item type depends on files |
|
| 84 | + * @return boolean true if backend is registered or false if error |
|
| 85 | + */ |
|
| 86 | + public static function registerBackend($itemType, $class, $collectionOf = null, $supportedFileExtensions = null) { |
|
| 87 | + if (self::isEnabled()) { |
|
| 88 | + if (!isset(self::$backendTypes[$itemType])) { |
|
| 89 | + self::$backendTypes[$itemType] = array( |
|
| 90 | + 'class' => $class, |
|
| 91 | + 'collectionOf' => $collectionOf, |
|
| 92 | + 'supportedFileExtensions' => $supportedFileExtensions |
|
| 93 | + ); |
|
| 94 | + if(count(self::$backendTypes) === 1) { |
|
| 95 | + Util::addScript('core', 'merged-share-backend'); |
|
| 96 | + \OC_Util::addStyle('core', 'share'); |
|
| 97 | + } |
|
| 98 | + return true; |
|
| 99 | + } |
|
| 100 | + \OCP\Util::writeLog('OCP\Share', |
|
| 101 | + 'Sharing backend '.$class.' not registered, '.self::$backendTypes[$itemType]['class'] |
|
| 102 | + .' is already registered for '.$itemType, |
|
| 103 | + \OCP\Util::WARN); |
|
| 104 | + } |
|
| 105 | + return false; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Check if the Share API is enabled |
|
| 110 | + * @return boolean true if enabled or false |
|
| 111 | + * |
|
| 112 | + * The Share API is enabled by default if not configured |
|
| 113 | + */ |
|
| 114 | + public static function isEnabled() { |
|
| 115 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_enabled', 'yes') == 'yes') { |
|
| 116 | + return true; |
|
| 117 | + } |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Find which users can access a shared item |
|
| 123 | + * @param string $path to the file |
|
| 124 | + * @param string $ownerUser owner of the file |
|
| 125 | + * @param IUserManager $userManager |
|
| 126 | + * @param ILogger $logger |
|
| 127 | + * @param boolean $includeOwner include owner to the list of users with access to the file |
|
| 128 | + * @param boolean $returnUserPaths Return an array with the user => path map |
|
| 129 | + * @param boolean $recursive take all parent folders into account (default true) |
|
| 130 | + * @return array |
|
| 131 | + * @note $path needs to be relative to user data dir, e.g. 'file.txt' |
|
| 132 | + * not '/admin/data/file.txt' |
|
| 133 | + * @throws \OC\User\NoUserException |
|
| 134 | + */ |
|
| 135 | + public static function getUsersSharingFile($path, |
|
| 136 | + $ownerUser, |
|
| 137 | + IUserManager $userManager, |
|
| 138 | + ILogger $logger, |
|
| 139 | + $includeOwner = false, |
|
| 140 | + $returnUserPaths = false, |
|
| 141 | + $recursive = true) { |
|
| 142 | + $userObject = $userManager->get($ownerUser); |
|
| 143 | + |
|
| 144 | + if (is_null($userObject)) { |
|
| 145 | + $logger->error( |
|
| 146 | + sprintf( |
|
| 147 | + 'Backends provided no user object for %s', |
|
| 148 | + $ownerUser |
|
| 149 | + ), |
|
| 150 | + [ |
|
| 151 | + 'app' => 'files', |
|
| 152 | + ] |
|
| 153 | + ); |
|
| 154 | + throw new \OC\User\NoUserException('Backends provided no user object'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $ownerUser = $userObject->getUID(); |
|
| 158 | + |
|
| 159 | + Filesystem::initMountPoints($ownerUser); |
|
| 160 | + $shares = $sharePaths = $fileTargets = array(); |
|
| 161 | + $publicShare = false; |
|
| 162 | + $remoteShare = false; |
|
| 163 | + $source = -1; |
|
| 164 | + $cache = $mountPath = false; |
|
| 165 | + |
|
| 166 | + $view = new \OC\Files\View('/' . $ownerUser . '/files'); |
|
| 167 | + $meta = $view->getFileInfo($path); |
|
| 168 | + if ($meta) { |
|
| 169 | + $path = substr($meta->getPath(), strlen('/' . $ownerUser . '/files')); |
|
| 170 | + } else { |
|
| 171 | + // if the file doesn't exists yet we start with the parent folder |
|
| 172 | + $meta = $view->getFileInfo(dirname($path)); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + if($meta !== false) { |
|
| 176 | + $source = $meta['fileid']; |
|
| 177 | + $cache = new \OC\Files\Cache\Cache($meta['storage']); |
|
| 178 | + |
|
| 179 | + $mountPath = $meta->getMountPoint()->getMountPoint(); |
|
| 180 | + if ($mountPath !== false) { |
|
| 181 | + $mountPath = substr($mountPath, strlen('/' . $ownerUser . '/files')); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + $paths = []; |
|
| 186 | + while ($source !== -1) { |
|
| 187 | + // Fetch all shares with another user |
|
| 188 | + if (!$returnUserPaths) { |
|
| 189 | + $query = \OC_DB::prepare( |
|
| 190 | + 'SELECT `share_with`, `file_source`, `file_target` |
|
| 191 | 191 | FROM |
| 192 | 192 | `*PREFIX*share` |
| 193 | 193 | WHERE |
| 194 | 194 | `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')' |
| 195 | - ); |
|
| 196 | - $result = $query->execute(array($source, self::SHARE_TYPE_USER)); |
|
| 197 | - } else { |
|
| 198 | - $query = \OC_DB::prepare( |
|
| 199 | - 'SELECT `share_with`, `file_source`, `file_target` |
|
| 195 | + ); |
|
| 196 | + $result = $query->execute(array($source, self::SHARE_TYPE_USER)); |
|
| 197 | + } else { |
|
| 198 | + $query = \OC_DB::prepare( |
|
| 199 | + 'SELECT `share_with`, `file_source`, `file_target` |
|
| 200 | 200 | FROM |
| 201 | 201 | `*PREFIX*share` |
| 202 | 202 | WHERE |
| 203 | 203 | `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')' |
| 204 | - ); |
|
| 205 | - $result = $query->execute(array($source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique)); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - if (\OCP\DB::isError($result)) { |
|
| 209 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 210 | - } else { |
|
| 211 | - while ($row = $result->fetchRow()) { |
|
| 212 | - $shares[] = $row['share_with']; |
|
| 213 | - if ($returnUserPaths) { |
|
| 214 | - $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; |
|
| 215 | - } |
|
| 216 | - } |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - // We also need to take group shares into account |
|
| 220 | - $query = \OC_DB::prepare( |
|
| 221 | - 'SELECT `share_with`, `file_source`, `file_target` |
|
| 204 | + ); |
|
| 205 | + $result = $query->execute(array($source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique)); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + if (\OCP\DB::isError($result)) { |
|
| 209 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 210 | + } else { |
|
| 211 | + while ($row = $result->fetchRow()) { |
|
| 212 | + $shares[] = $row['share_with']; |
|
| 213 | + if ($returnUserPaths) { |
|
| 214 | + $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; |
|
| 215 | + } |
|
| 216 | + } |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + // We also need to take group shares into account |
|
| 220 | + $query = \OC_DB::prepare( |
|
| 221 | + 'SELECT `share_with`, `file_source`, `file_target` |
|
| 222 | 222 | FROM |
| 223 | 223 | `*PREFIX*share` |
| 224 | 224 | WHERE |
| 225 | 225 | `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')' |
| 226 | - ); |
|
| 227 | - |
|
| 228 | - $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); |
|
| 229 | - |
|
| 230 | - if (\OCP\DB::isError($result)) { |
|
| 231 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 232 | - } else { |
|
| 233 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 234 | - while ($row = $result->fetchRow()) { |
|
| 235 | - |
|
| 236 | - $usersInGroup = []; |
|
| 237 | - $group = $groupManager->get($row['share_with']); |
|
| 238 | - if ($group) { |
|
| 239 | - $users = $group->searchUsers('', -1, 0); |
|
| 240 | - $userIds = array(); |
|
| 241 | - foreach ($users as $user) { |
|
| 242 | - $userIds[] = $user->getUID(); |
|
| 243 | - } |
|
| 244 | - $usersInGroup = $userIds; |
|
| 245 | - } |
|
| 246 | - $shares = array_merge($shares, $usersInGroup); |
|
| 247 | - if ($returnUserPaths) { |
|
| 248 | - foreach ($usersInGroup as $user) { |
|
| 249 | - if (!isset($fileTargets[(int) $row['file_source']][$user])) { |
|
| 250 | - // When the user already has an entry for this file source |
|
| 251 | - // the file is either shared directly with him as well, or |
|
| 252 | - // he has an exception entry (because of naming conflict). |
|
| 253 | - $fileTargets[(int) $row['file_source']][$user] = $row; |
|
| 254 | - } |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - //check for public link shares |
|
| 261 | - if (!$publicShare) { |
|
| 262 | - $query = \OC_DB::prepare(' |
|
| 226 | + ); |
|
| 227 | + |
|
| 228 | + $result = $query->execute(array($source, self::SHARE_TYPE_GROUP)); |
|
| 229 | + |
|
| 230 | + if (\OCP\DB::isError($result)) { |
|
| 231 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 232 | + } else { |
|
| 233 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 234 | + while ($row = $result->fetchRow()) { |
|
| 235 | + |
|
| 236 | + $usersInGroup = []; |
|
| 237 | + $group = $groupManager->get($row['share_with']); |
|
| 238 | + if ($group) { |
|
| 239 | + $users = $group->searchUsers('', -1, 0); |
|
| 240 | + $userIds = array(); |
|
| 241 | + foreach ($users as $user) { |
|
| 242 | + $userIds[] = $user->getUID(); |
|
| 243 | + } |
|
| 244 | + $usersInGroup = $userIds; |
|
| 245 | + } |
|
| 246 | + $shares = array_merge($shares, $usersInGroup); |
|
| 247 | + if ($returnUserPaths) { |
|
| 248 | + foreach ($usersInGroup as $user) { |
|
| 249 | + if (!isset($fileTargets[(int) $row['file_source']][$user])) { |
|
| 250 | + // When the user already has an entry for this file source |
|
| 251 | + // the file is either shared directly with him as well, or |
|
| 252 | + // he has an exception entry (because of naming conflict). |
|
| 253 | + $fileTargets[(int) $row['file_source']][$user] = $row; |
|
| 254 | + } |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + //check for public link shares |
|
| 261 | + if (!$publicShare) { |
|
| 262 | + $query = \OC_DB::prepare(' |
|
| 263 | 263 | SELECT `share_with` |
| 264 | 264 | FROM `*PREFIX*share` |
| 265 | 265 | WHERE `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')', 1 |
| 266 | - ); |
|
| 267 | - |
|
| 268 | - $result = $query->execute(array($source, self::SHARE_TYPE_LINK, self::SHARE_TYPE_EMAIL)); |
|
| 269 | - |
|
| 270 | - if (\OCP\DB::isError($result)) { |
|
| 271 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 272 | - } else { |
|
| 273 | - if ($result->fetchRow()) { |
|
| 274 | - $publicShare = true; |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - } |
|
| 278 | - |
|
| 279 | - //check for remote share |
|
| 280 | - if (!$remoteShare) { |
|
| 281 | - $query = \OC_DB::prepare(' |
|
| 266 | + ); |
|
| 267 | + |
|
| 268 | + $result = $query->execute(array($source, self::SHARE_TYPE_LINK, self::SHARE_TYPE_EMAIL)); |
|
| 269 | + |
|
| 270 | + if (\OCP\DB::isError($result)) { |
|
| 271 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 272 | + } else { |
|
| 273 | + if ($result->fetchRow()) { |
|
| 274 | + $publicShare = true; |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + } |
|
| 278 | + |
|
| 279 | + //check for remote share |
|
| 280 | + if (!$remoteShare) { |
|
| 281 | + $query = \OC_DB::prepare(' |
|
| 282 | 282 | SELECT `share_with` |
| 283 | 283 | FROM `*PREFIX*share` |
| 284 | 284 | WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1 |
| 285 | - ); |
|
| 286 | - |
|
| 287 | - $result = $query->execute(array($source, self::SHARE_TYPE_REMOTE)); |
|
| 288 | - |
|
| 289 | - if (\OCP\DB::isError($result)) { |
|
| 290 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 291 | - } else { |
|
| 292 | - if ($result->fetchRow()) { |
|
| 293 | - $remoteShare = true; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - // let's get the parent for the next round |
|
| 299 | - $meta = $cache->get((int)$source); |
|
| 300 | - if ($recursive === true && $meta !== false) { |
|
| 301 | - $paths[$source] = $meta['path']; |
|
| 302 | - $source = (int)$meta['parent']; |
|
| 303 | - } else { |
|
| 304 | - $source = -1; |
|
| 305 | - } |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - // Include owner in list of users, if requested |
|
| 309 | - if ($includeOwner) { |
|
| 310 | - $shares[] = $ownerUser; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - if ($returnUserPaths) { |
|
| 314 | - $fileTargetIDs = array_keys($fileTargets); |
|
| 315 | - $fileTargetIDs = array_unique($fileTargetIDs); |
|
| 316 | - |
|
| 317 | - if (!empty($fileTargetIDs)) { |
|
| 318 | - $query = \OC_DB::prepare( |
|
| 319 | - 'SELECT `fileid`, `path` |
|
| 285 | + ); |
|
| 286 | + |
|
| 287 | + $result = $query->execute(array($source, self::SHARE_TYPE_REMOTE)); |
|
| 288 | + |
|
| 289 | + if (\OCP\DB::isError($result)) { |
|
| 290 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 291 | + } else { |
|
| 292 | + if ($result->fetchRow()) { |
|
| 293 | + $remoteShare = true; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + // let's get the parent for the next round |
|
| 299 | + $meta = $cache->get((int)$source); |
|
| 300 | + if ($recursive === true && $meta !== false) { |
|
| 301 | + $paths[$source] = $meta['path']; |
|
| 302 | + $source = (int)$meta['parent']; |
|
| 303 | + } else { |
|
| 304 | + $source = -1; |
|
| 305 | + } |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + // Include owner in list of users, if requested |
|
| 309 | + if ($includeOwner) { |
|
| 310 | + $shares[] = $ownerUser; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + if ($returnUserPaths) { |
|
| 314 | + $fileTargetIDs = array_keys($fileTargets); |
|
| 315 | + $fileTargetIDs = array_unique($fileTargetIDs); |
|
| 316 | + |
|
| 317 | + if (!empty($fileTargetIDs)) { |
|
| 318 | + $query = \OC_DB::prepare( |
|
| 319 | + 'SELECT `fileid`, `path` |
|
| 320 | 320 | FROM `*PREFIX*filecache` |
| 321 | 321 | WHERE `fileid` IN (' . implode(',', $fileTargetIDs) . ')' |
| 322 | - ); |
|
| 323 | - $result = $query->execute(); |
|
| 324 | - |
|
| 325 | - if (\OCP\DB::isError($result)) { |
|
| 326 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 327 | - } else { |
|
| 328 | - while ($row = $result->fetchRow()) { |
|
| 329 | - foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { |
|
| 330 | - if ($mountPath !== false) { |
|
| 331 | - $sharedPath = $shareData['file_target']; |
|
| 332 | - $sharedPath .= substr($path, strlen($mountPath) + strlen($paths[$row['fileid']])); |
|
| 333 | - $sharePaths[$uid] = $sharedPath; |
|
| 334 | - } else { |
|
| 335 | - $sharedPath = $shareData['file_target']; |
|
| 336 | - $sharedPath .= substr($path, strlen($row['path']) -5); |
|
| 337 | - $sharePaths[$uid] = $sharedPath; |
|
| 338 | - } |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - } |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - if ($includeOwner) { |
|
| 345 | - $sharePaths[$ownerUser] = $path; |
|
| 346 | - } else { |
|
| 347 | - unset($sharePaths[$ownerUser]); |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - return $sharePaths; |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - return array('users' => array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Get the items of item type shared with the current user |
|
| 358 | - * @param string $itemType |
|
| 359 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 360 | - * @param mixed $parameters (optional) |
|
| 361 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 362 | - * @param boolean $includeCollections (optional) |
|
| 363 | - * @return mixed Return depends on format |
|
| 364 | - */ |
|
| 365 | - public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, |
|
| 366 | - $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 367 | - return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 368 | - $parameters, $limit, $includeCollections); |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - /** |
|
| 372 | - * Get the items of item type shared with a user |
|
| 373 | - * @param string $itemType |
|
| 374 | - * @param string $user id for which user we want the shares |
|
| 375 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 376 | - * @param mixed $parameters (optional) |
|
| 377 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 378 | - * @param boolean $includeCollections (optional) |
|
| 379 | - * @return mixed Return depends on format |
|
| 380 | - */ |
|
| 381 | - public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE, |
|
| 382 | - $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 383 | - return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format, |
|
| 384 | - $parameters, $limit, $includeCollections); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Get the item of item type shared with the current user |
|
| 389 | - * @param string $itemType |
|
| 390 | - * @param string $itemTarget |
|
| 391 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 392 | - * @param mixed $parameters (optional) |
|
| 393 | - * @param boolean $includeCollections (optional) |
|
| 394 | - * @return mixed Return depends on format |
|
| 395 | - */ |
|
| 396 | - public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE, |
|
| 397 | - $parameters = null, $includeCollections = false) { |
|
| 398 | - return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 399 | - $parameters, 1, $includeCollections); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Get the item of item type shared with a given user by source |
|
| 404 | - * @param string $itemType |
|
| 405 | - * @param string $itemSource |
|
| 406 | - * @param string $user User to whom the item was shared |
|
| 407 | - * @param string $owner Owner of the share |
|
| 408 | - * @param int $shareType only look for a specific share type |
|
| 409 | - * @return array Return list of items with file_target, permissions and expiration |
|
| 410 | - */ |
|
| 411 | - public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) { |
|
| 412 | - $shares = array(); |
|
| 413 | - $fileDependent = false; |
|
| 414 | - |
|
| 415 | - $where = 'WHERE'; |
|
| 416 | - $fileDependentWhere = ''; |
|
| 417 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 418 | - $fileDependent = true; |
|
| 419 | - $column = 'file_source'; |
|
| 420 | - $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 421 | - $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 422 | - } else { |
|
| 423 | - $column = 'item_source'; |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
|
| 427 | - |
|
| 428 | - $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 429 | - $arguments = array($itemSource, $itemType); |
|
| 430 | - // for link shares $user === null |
|
| 431 | - if ($user !== null) { |
|
| 432 | - $where .= ' AND `share_with` = ? '; |
|
| 433 | - $arguments[] = $user; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - if ($shareType !== null) { |
|
| 437 | - $where .= ' AND `share_type` = ? '; |
|
| 438 | - $arguments[] = $shareType; |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - if ($owner !== null) { |
|
| 442 | - $where .= ' AND `uid_owner` = ? '; |
|
| 443 | - $arguments[] = $owner; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 447 | - |
|
| 448 | - $result = \OC_DB::executeAudited($query, $arguments); |
|
| 449 | - |
|
| 450 | - while ($row = $result->fetchRow()) { |
|
| 451 | - if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 452 | - continue; |
|
| 453 | - } |
|
| 454 | - if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 455 | - // if it is a mount point we need to get the path from the mount manager |
|
| 456 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 457 | - $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
|
| 458 | - if (!empty($mountPoint)) { |
|
| 459 | - $path = $mountPoint[0]->getMountPoint(); |
|
| 460 | - $path = trim($path, '/'); |
|
| 461 | - $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt` |
|
| 462 | - $row['path'] = $path; |
|
| 463 | - } else { |
|
| 464 | - \OC::$server->getLogger()->warning( |
|
| 465 | - 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 466 | - ['app' => 'OCP\Share'] |
|
| 467 | - ); |
|
| 468 | - } |
|
| 469 | - } |
|
| 470 | - $shares[] = $row; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - //if didn't found a result than let's look for a group share. |
|
| 474 | - if(empty($shares) && $user !== null) { |
|
| 475 | - $userObject = \OC::$server->getUserManager()->get($user); |
|
| 476 | - $groups = []; |
|
| 477 | - if ($userObject) { |
|
| 478 | - $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if (!empty($groups)) { |
|
| 482 | - $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 483 | - $arguments = array($itemSource, $itemType, $groups); |
|
| 484 | - $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 485 | - |
|
| 486 | - if ($owner !== null) { |
|
| 487 | - $where .= ' AND `uid_owner` = ?'; |
|
| 488 | - $arguments[] = $owner; |
|
| 489 | - $types[] = null; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - // TODO: inject connection, hopefully one day in the future when this |
|
| 493 | - // class isn't static anymore... |
|
| 494 | - $conn = \OC::$server->getDatabaseConnection(); |
|
| 495 | - $result = $conn->executeQuery( |
|
| 496 | - 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 497 | - $arguments, |
|
| 498 | - $types |
|
| 499 | - ); |
|
| 500 | - |
|
| 501 | - while ($row = $result->fetch()) { |
|
| 502 | - $shares[] = $row; |
|
| 503 | - } |
|
| 504 | - } |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - return $shares; |
|
| 508 | - |
|
| 509 | - } |
|
| 510 | - |
|
| 511 | - /** |
|
| 512 | - * Get the item of item type shared with the current user by source |
|
| 513 | - * @param string $itemType |
|
| 514 | - * @param string $itemSource |
|
| 515 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 516 | - * @param mixed $parameters |
|
| 517 | - * @param boolean $includeCollections |
|
| 518 | - * @param string $shareWith (optional) define against which user should be checked, default: current user |
|
| 519 | - * @return array |
|
| 520 | - */ |
|
| 521 | - public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 522 | - $parameters = null, $includeCollections = false, $shareWith = null) { |
|
| 523 | - $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith; |
|
| 524 | - return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format, |
|
| 525 | - $parameters, 1, $includeCollections, true); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - /** |
|
| 529 | - * Get the item of item type shared by a link |
|
| 530 | - * @param string $itemType |
|
| 531 | - * @param string $itemSource |
|
| 532 | - * @param string $uidOwner Owner of link |
|
| 533 | - * @return array |
|
| 534 | - */ |
|
| 535 | - public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) { |
|
| 536 | - return self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, |
|
| 537 | - null, 1); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * Based on the given token the share information will be returned - password protected shares will be verified |
|
| 542 | - * @param string $token |
|
| 543 | - * @param bool $checkPasswordProtection |
|
| 544 | - * @return array|boolean false will be returned in case the token is unknown or unauthorized |
|
| 545 | - */ |
|
| 546 | - public static function getShareByToken($token, $checkPasswordProtection = true) { |
|
| 547 | - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1); |
|
| 548 | - $result = $query->execute(array($token)); |
|
| 549 | - if ($result === false) { |
|
| 550 | - \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR); |
|
| 551 | - } |
|
| 552 | - $row = $result->fetchRow(); |
|
| 553 | - if ($row === false) { |
|
| 554 | - return false; |
|
| 555 | - } |
|
| 556 | - if (is_array($row) and self::expireItem($row)) { |
|
| 557 | - return false; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - // password protected shares need to be authenticated |
|
| 561 | - if ($checkPasswordProtection && !\OCP\Share::checkPasswordProtectedShare($row)) { |
|
| 562 | - return false; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - return $row; |
|
| 566 | - } |
|
| 567 | - |
|
| 568 | - /** |
|
| 569 | - * resolves reshares down to the last real share |
|
| 570 | - * @param array $linkItem |
|
| 571 | - * @return array file owner |
|
| 572 | - */ |
|
| 573 | - public static function resolveReShare($linkItem) |
|
| 574 | - { |
|
| 575 | - if (isset($linkItem['parent'])) { |
|
| 576 | - $parent = $linkItem['parent']; |
|
| 577 | - while (isset($parent)) { |
|
| 578 | - $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1); |
|
| 579 | - $item = $query->execute(array($parent))->fetchRow(); |
|
| 580 | - if (isset($item['parent'])) { |
|
| 581 | - $parent = $item['parent']; |
|
| 582 | - } else { |
|
| 583 | - return $item; |
|
| 584 | - } |
|
| 585 | - } |
|
| 586 | - } |
|
| 587 | - return $linkItem; |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - |
|
| 591 | - /** |
|
| 592 | - * Get the shared items of item type owned by the current user |
|
| 593 | - * @param string $itemType |
|
| 594 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 595 | - * @param mixed $parameters |
|
| 596 | - * @param int $limit Number of items to return (optional) Returns all by default |
|
| 597 | - * @param boolean $includeCollections |
|
| 598 | - * @return mixed Return depends on format |
|
| 599 | - */ |
|
| 600 | - public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null, |
|
| 601 | - $limit = -1, $includeCollections = false) { |
|
| 602 | - return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, |
|
| 603 | - $parameters, $limit, $includeCollections); |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * Get the shared item of item type owned by the current user |
|
| 608 | - * @param string $itemType |
|
| 609 | - * @param string $itemSource |
|
| 610 | - * @param int $format (optional) Format type must be defined by the backend |
|
| 611 | - * @param mixed $parameters |
|
| 612 | - * @param boolean $includeCollections |
|
| 613 | - * @return mixed Return depends on format |
|
| 614 | - */ |
|
| 615 | - public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 616 | - $parameters = null, $includeCollections = false) { |
|
| 617 | - return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, |
|
| 618 | - $parameters, -1, $includeCollections); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - /** |
|
| 622 | - * Get all users an item is shared with |
|
| 623 | - * @param string $itemType |
|
| 624 | - * @param string $itemSource |
|
| 625 | - * @param string $uidOwner |
|
| 626 | - * @param boolean $includeCollections |
|
| 627 | - * @param boolean $checkExpireDate |
|
| 628 | - * @return array Return array of users |
|
| 629 | - */ |
|
| 630 | - public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) { |
|
| 631 | - |
|
| 632 | - $users = array(); |
|
| 633 | - $items = self::getItems($itemType, $itemSource, null, null, $uidOwner, self::FORMAT_NONE, null, -1, $includeCollections, false, $checkExpireDate); |
|
| 634 | - if ($items) { |
|
| 635 | - foreach ($items as $item) { |
|
| 636 | - if ((int)$item['share_type'] === self::SHARE_TYPE_USER) { |
|
| 637 | - $users[] = $item['share_with']; |
|
| 638 | - } else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { |
|
| 639 | - |
|
| 640 | - $group = \OC::$server->getGroupManager()->get($item['share_with']); |
|
| 641 | - $userIds = []; |
|
| 642 | - if ($group) { |
|
| 643 | - $users = $group->searchUsers('', -1, 0); |
|
| 644 | - foreach ($users as $user) { |
|
| 645 | - $userIds[] = $user->getUID(); |
|
| 646 | - } |
|
| 647 | - return $userIds; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - $users = array_merge($users, $userIds); |
|
| 651 | - } |
|
| 652 | - } |
|
| 653 | - } |
|
| 654 | - return $users; |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - /** |
|
| 658 | - * Share an item with a user, group, or via private link |
|
| 659 | - * @param string $itemType |
|
| 660 | - * @param string $itemSource |
|
| 661 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 662 | - * @param string $shareWith User or group the item is being shared with |
|
| 663 | - * @param int $permissions CRUDS |
|
| 664 | - * @param string $itemSourceName |
|
| 665 | - * @param \DateTime $expirationDate |
|
| 666 | - * @param bool $passwordChanged |
|
| 667 | - * @return boolean|string Returns true on success or false on failure, Returns token on success for links |
|
| 668 | - * @throws \OC\HintException when the share type is remote and the shareWith is invalid |
|
| 669 | - * @throws \Exception |
|
| 670 | - */ |
|
| 671 | - public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) { |
|
| 672 | - |
|
| 673 | - $backend = self::getBackend($itemType); |
|
| 674 | - $l = \OC::$server->getL10N('lib'); |
|
| 675 | - |
|
| 676 | - if ($backend->isShareTypeAllowed($shareType) === false) { |
|
| 677 | - $message = 'Sharing %s failed, because the backend does not allow shares from type %i'; |
|
| 678 | - $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType)); |
|
| 679 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG); |
|
| 680 | - throw new \Exception($message_t); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - $uidOwner = \OC_User::getUser(); |
|
| 684 | - $shareWithinGroupOnly = self::shareWithGroupMembersOnly(); |
|
| 685 | - |
|
| 686 | - if (is_null($itemSourceName)) { |
|
| 687 | - $itemSourceName = $itemSource; |
|
| 688 | - } |
|
| 689 | - $itemName = $itemSourceName; |
|
| 690 | - |
|
| 691 | - // check if file can be shared |
|
| 692 | - if ($itemType === 'file' or $itemType === 'folder') { |
|
| 693 | - $path = \OC\Files\Filesystem::getPath($itemSource); |
|
| 694 | - $itemName = $path; |
|
| 695 | - |
|
| 696 | - // verify that the file exists before we try to share it |
|
| 697 | - if (!$path) { |
|
| 698 | - $message = 'Sharing %s failed, because the file does not exist'; |
|
| 699 | - $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName)); |
|
| 700 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 701 | - throw new \Exception($message_t); |
|
| 702 | - } |
|
| 703 | - // verify that the user has share permission |
|
| 704 | - if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) { |
|
| 705 | - $message = 'You are not allowed to share %s'; |
|
| 706 | - $message_t = $l->t('You are not allowed to share %s', [$path]); |
|
| 707 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $path), \OCP\Util::DEBUG); |
|
| 708 | - throw new \Exception($message_t); |
|
| 709 | - } |
|
| 710 | - } |
|
| 711 | - |
|
| 712 | - //verify that we don't share a folder which already contains a share mount point |
|
| 713 | - if ($itemType === 'folder') { |
|
| 714 | - $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/'; |
|
| 715 | - $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 716 | - $mounts = $mountManager->findIn($path); |
|
| 717 | - foreach ($mounts as $mount) { |
|
| 718 | - if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 719 | - $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!'; |
|
| 720 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 721 | - throw new \Exception($message); |
|
| 722 | - } |
|
| 723 | - |
|
| 724 | - } |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - // single file shares should never have delete permissions |
|
| 728 | - if ($itemType === 'file') { |
|
| 729 | - $permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE; |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - //Validate expirationDate |
|
| 733 | - if ($expirationDate !== null) { |
|
| 734 | - try { |
|
| 735 | - /* |
|
| 322 | + ); |
|
| 323 | + $result = $query->execute(); |
|
| 324 | + |
|
| 325 | + if (\OCP\DB::isError($result)) { |
|
| 326 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
|
| 327 | + } else { |
|
| 328 | + while ($row = $result->fetchRow()) { |
|
| 329 | + foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { |
|
| 330 | + if ($mountPath !== false) { |
|
| 331 | + $sharedPath = $shareData['file_target']; |
|
| 332 | + $sharedPath .= substr($path, strlen($mountPath) + strlen($paths[$row['fileid']])); |
|
| 333 | + $sharePaths[$uid] = $sharedPath; |
|
| 334 | + } else { |
|
| 335 | + $sharedPath = $shareData['file_target']; |
|
| 336 | + $sharedPath .= substr($path, strlen($row['path']) -5); |
|
| 337 | + $sharePaths[$uid] = $sharedPath; |
|
| 338 | + } |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + } |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + if ($includeOwner) { |
|
| 345 | + $sharePaths[$ownerUser] = $path; |
|
| 346 | + } else { |
|
| 347 | + unset($sharePaths[$ownerUser]); |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + return $sharePaths; |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + return array('users' => array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Get the items of item type shared with the current user |
|
| 358 | + * @param string $itemType |
|
| 359 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 360 | + * @param mixed $parameters (optional) |
|
| 361 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 362 | + * @param boolean $includeCollections (optional) |
|
| 363 | + * @return mixed Return depends on format |
|
| 364 | + */ |
|
| 365 | + public static function getItemsSharedWith($itemType, $format = self::FORMAT_NONE, |
|
| 366 | + $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 367 | + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 368 | + $parameters, $limit, $includeCollections); |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + /** |
|
| 372 | + * Get the items of item type shared with a user |
|
| 373 | + * @param string $itemType |
|
| 374 | + * @param string $user id for which user we want the shares |
|
| 375 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 376 | + * @param mixed $parameters (optional) |
|
| 377 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 378 | + * @param boolean $includeCollections (optional) |
|
| 379 | + * @return mixed Return depends on format |
|
| 380 | + */ |
|
| 381 | + public static function getItemsSharedWithUser($itemType, $user, $format = self::FORMAT_NONE, |
|
| 382 | + $parameters = null, $limit = -1, $includeCollections = false) { |
|
| 383 | + return self::getItems($itemType, null, self::$shareTypeUserAndGroups, $user, null, $format, |
|
| 384 | + $parameters, $limit, $includeCollections); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Get the item of item type shared with the current user |
|
| 389 | + * @param string $itemType |
|
| 390 | + * @param string $itemTarget |
|
| 391 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 392 | + * @param mixed $parameters (optional) |
|
| 393 | + * @param boolean $includeCollections (optional) |
|
| 394 | + * @return mixed Return depends on format |
|
| 395 | + */ |
|
| 396 | + public static function getItemSharedWith($itemType, $itemTarget, $format = self::FORMAT_NONE, |
|
| 397 | + $parameters = null, $includeCollections = false) { |
|
| 398 | + return self::getItems($itemType, $itemTarget, self::$shareTypeUserAndGroups, \OC_User::getUser(), null, $format, |
|
| 399 | + $parameters, 1, $includeCollections); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Get the item of item type shared with a given user by source |
|
| 404 | + * @param string $itemType |
|
| 405 | + * @param string $itemSource |
|
| 406 | + * @param string $user User to whom the item was shared |
|
| 407 | + * @param string $owner Owner of the share |
|
| 408 | + * @param int $shareType only look for a specific share type |
|
| 409 | + * @return array Return list of items with file_target, permissions and expiration |
|
| 410 | + */ |
|
| 411 | + public static function getItemSharedWithUser($itemType, $itemSource, $user, $owner = null, $shareType = null) { |
|
| 412 | + $shares = array(); |
|
| 413 | + $fileDependent = false; |
|
| 414 | + |
|
| 415 | + $where = 'WHERE'; |
|
| 416 | + $fileDependentWhere = ''; |
|
| 417 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 418 | + $fileDependent = true; |
|
| 419 | + $column = 'file_source'; |
|
| 420 | + $fileDependentWhere = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 421 | + $fileDependentWhere .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 422 | + } else { |
|
| 423 | + $column = 'item_source'; |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + $select = self::createSelectStatement(self::FORMAT_NONE, $fileDependent); |
|
| 427 | + |
|
| 428 | + $where .= ' `' . $column . '` = ? AND `item_type` = ? '; |
|
| 429 | + $arguments = array($itemSource, $itemType); |
|
| 430 | + // for link shares $user === null |
|
| 431 | + if ($user !== null) { |
|
| 432 | + $where .= ' AND `share_with` = ? '; |
|
| 433 | + $arguments[] = $user; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + if ($shareType !== null) { |
|
| 437 | + $where .= ' AND `share_type` = ? '; |
|
| 438 | + $arguments[] = $shareType; |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + if ($owner !== null) { |
|
| 442 | + $where .= ' AND `uid_owner` = ? '; |
|
| 443 | + $arguments[] = $owner; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + $query = \OC_DB::prepare('SELECT ' . $select . ' FROM `*PREFIX*share` '. $fileDependentWhere . $where); |
|
| 447 | + |
|
| 448 | + $result = \OC_DB::executeAudited($query, $arguments); |
|
| 449 | + |
|
| 450 | + while ($row = $result->fetchRow()) { |
|
| 451 | + if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 452 | + continue; |
|
| 453 | + } |
|
| 454 | + if ($fileDependent && (int)$row['file_parent'] === -1) { |
|
| 455 | + // if it is a mount point we need to get the path from the mount manager |
|
| 456 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 457 | + $mountPoint = $mountManager->findByStorageId($row['storage_id']); |
|
| 458 | + if (!empty($mountPoint)) { |
|
| 459 | + $path = $mountPoint[0]->getMountPoint(); |
|
| 460 | + $path = trim($path, '/'); |
|
| 461 | + $path = substr($path, strlen($owner) + 1); //normalize path to 'files/foo.txt` |
|
| 462 | + $row['path'] = $path; |
|
| 463 | + } else { |
|
| 464 | + \OC::$server->getLogger()->warning( |
|
| 465 | + 'Could not resolve mount point for ' . $row['storage_id'], |
|
| 466 | + ['app' => 'OCP\Share'] |
|
| 467 | + ); |
|
| 468 | + } |
|
| 469 | + } |
|
| 470 | + $shares[] = $row; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + //if didn't found a result than let's look for a group share. |
|
| 474 | + if(empty($shares) && $user !== null) { |
|
| 475 | + $userObject = \OC::$server->getUserManager()->get($user); |
|
| 476 | + $groups = []; |
|
| 477 | + if ($userObject) { |
|
| 478 | + $groups = \OC::$server->getGroupManager()->getUserGroupIds($userObject); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if (!empty($groups)) { |
|
| 482 | + $where = $fileDependentWhere . ' WHERE `' . $column . '` = ? AND `item_type` = ? AND `share_with` in (?)'; |
|
| 483 | + $arguments = array($itemSource, $itemType, $groups); |
|
| 484 | + $types = array(null, null, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 485 | + |
|
| 486 | + if ($owner !== null) { |
|
| 487 | + $where .= ' AND `uid_owner` = ?'; |
|
| 488 | + $arguments[] = $owner; |
|
| 489 | + $types[] = null; |
|
| 490 | + } |
|
| 491 | + |
|
| 492 | + // TODO: inject connection, hopefully one day in the future when this |
|
| 493 | + // class isn't static anymore... |
|
| 494 | + $conn = \OC::$server->getDatabaseConnection(); |
|
| 495 | + $result = $conn->executeQuery( |
|
| 496 | + 'SELECT ' . $select . ' FROM `*PREFIX*share` ' . $where, |
|
| 497 | + $arguments, |
|
| 498 | + $types |
|
| 499 | + ); |
|
| 500 | + |
|
| 501 | + while ($row = $result->fetch()) { |
|
| 502 | + $shares[] = $row; |
|
| 503 | + } |
|
| 504 | + } |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + return $shares; |
|
| 508 | + |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + /** |
|
| 512 | + * Get the item of item type shared with the current user by source |
|
| 513 | + * @param string $itemType |
|
| 514 | + * @param string $itemSource |
|
| 515 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 516 | + * @param mixed $parameters |
|
| 517 | + * @param boolean $includeCollections |
|
| 518 | + * @param string $shareWith (optional) define against which user should be checked, default: current user |
|
| 519 | + * @return array |
|
| 520 | + */ |
|
| 521 | + public static function getItemSharedWithBySource($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 522 | + $parameters = null, $includeCollections = false, $shareWith = null) { |
|
| 523 | + $shareWith = ($shareWith === null) ? \OC_User::getUser() : $shareWith; |
|
| 524 | + return self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, $format, |
|
| 525 | + $parameters, 1, $includeCollections, true); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + /** |
|
| 529 | + * Get the item of item type shared by a link |
|
| 530 | + * @param string $itemType |
|
| 531 | + * @param string $itemSource |
|
| 532 | + * @param string $uidOwner Owner of link |
|
| 533 | + * @return array |
|
| 534 | + */ |
|
| 535 | + public static function getItemSharedWithByLink($itemType, $itemSource, $uidOwner) { |
|
| 536 | + return self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, |
|
| 537 | + null, 1); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * Based on the given token the share information will be returned - password protected shares will be verified |
|
| 542 | + * @param string $token |
|
| 543 | + * @param bool $checkPasswordProtection |
|
| 544 | + * @return array|boolean false will be returned in case the token is unknown or unauthorized |
|
| 545 | + */ |
|
| 546 | + public static function getShareByToken($token, $checkPasswordProtection = true) { |
|
| 547 | + $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `token` = ?', 1); |
|
| 548 | + $result = $query->execute(array($token)); |
|
| 549 | + if ($result === false) { |
|
| 550 | + \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage() . ', token=' . $token, \OCP\Util::ERROR); |
|
| 551 | + } |
|
| 552 | + $row = $result->fetchRow(); |
|
| 553 | + if ($row === false) { |
|
| 554 | + return false; |
|
| 555 | + } |
|
| 556 | + if (is_array($row) and self::expireItem($row)) { |
|
| 557 | + return false; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + // password protected shares need to be authenticated |
|
| 561 | + if ($checkPasswordProtection && !\OCP\Share::checkPasswordProtectedShare($row)) { |
|
| 562 | + return false; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + return $row; |
|
| 566 | + } |
|
| 567 | + |
|
| 568 | + /** |
|
| 569 | + * resolves reshares down to the last real share |
|
| 570 | + * @param array $linkItem |
|
| 571 | + * @return array file owner |
|
| 572 | + */ |
|
| 573 | + public static function resolveReShare($linkItem) |
|
| 574 | + { |
|
| 575 | + if (isset($linkItem['parent'])) { |
|
| 576 | + $parent = $linkItem['parent']; |
|
| 577 | + while (isset($parent)) { |
|
| 578 | + $query = \OC_DB::prepare('SELECT * FROM `*PREFIX*share` WHERE `id` = ?', 1); |
|
| 579 | + $item = $query->execute(array($parent))->fetchRow(); |
|
| 580 | + if (isset($item['parent'])) { |
|
| 581 | + $parent = $item['parent']; |
|
| 582 | + } else { |
|
| 583 | + return $item; |
|
| 584 | + } |
|
| 585 | + } |
|
| 586 | + } |
|
| 587 | + return $linkItem; |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + |
|
| 591 | + /** |
|
| 592 | + * Get the shared items of item type owned by the current user |
|
| 593 | + * @param string $itemType |
|
| 594 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 595 | + * @param mixed $parameters |
|
| 596 | + * @param int $limit Number of items to return (optional) Returns all by default |
|
| 597 | + * @param boolean $includeCollections |
|
| 598 | + * @return mixed Return depends on format |
|
| 599 | + */ |
|
| 600 | + public static function getItemsShared($itemType, $format = self::FORMAT_NONE, $parameters = null, |
|
| 601 | + $limit = -1, $includeCollections = false) { |
|
| 602 | + return self::getItems($itemType, null, null, null, \OC_User::getUser(), $format, |
|
| 603 | + $parameters, $limit, $includeCollections); |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * Get the shared item of item type owned by the current user |
|
| 608 | + * @param string $itemType |
|
| 609 | + * @param string $itemSource |
|
| 610 | + * @param int $format (optional) Format type must be defined by the backend |
|
| 611 | + * @param mixed $parameters |
|
| 612 | + * @param boolean $includeCollections |
|
| 613 | + * @return mixed Return depends on format |
|
| 614 | + */ |
|
| 615 | + public static function getItemShared($itemType, $itemSource, $format = self::FORMAT_NONE, |
|
| 616 | + $parameters = null, $includeCollections = false) { |
|
| 617 | + return self::getItems($itemType, $itemSource, null, null, \OC_User::getUser(), $format, |
|
| 618 | + $parameters, -1, $includeCollections); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + /** |
|
| 622 | + * Get all users an item is shared with |
|
| 623 | + * @param string $itemType |
|
| 624 | + * @param string $itemSource |
|
| 625 | + * @param string $uidOwner |
|
| 626 | + * @param boolean $includeCollections |
|
| 627 | + * @param boolean $checkExpireDate |
|
| 628 | + * @return array Return array of users |
|
| 629 | + */ |
|
| 630 | + public static function getUsersItemShared($itemType, $itemSource, $uidOwner, $includeCollections = false, $checkExpireDate = true) { |
|
| 631 | + |
|
| 632 | + $users = array(); |
|
| 633 | + $items = self::getItems($itemType, $itemSource, null, null, $uidOwner, self::FORMAT_NONE, null, -1, $includeCollections, false, $checkExpireDate); |
|
| 634 | + if ($items) { |
|
| 635 | + foreach ($items as $item) { |
|
| 636 | + if ((int)$item['share_type'] === self::SHARE_TYPE_USER) { |
|
| 637 | + $users[] = $item['share_with']; |
|
| 638 | + } else if ((int)$item['share_type'] === self::SHARE_TYPE_GROUP) { |
|
| 639 | + |
|
| 640 | + $group = \OC::$server->getGroupManager()->get($item['share_with']); |
|
| 641 | + $userIds = []; |
|
| 642 | + if ($group) { |
|
| 643 | + $users = $group->searchUsers('', -1, 0); |
|
| 644 | + foreach ($users as $user) { |
|
| 645 | + $userIds[] = $user->getUID(); |
|
| 646 | + } |
|
| 647 | + return $userIds; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + $users = array_merge($users, $userIds); |
|
| 651 | + } |
|
| 652 | + } |
|
| 653 | + } |
|
| 654 | + return $users; |
|
| 655 | + } |
|
| 656 | + |
|
| 657 | + /** |
|
| 658 | + * Share an item with a user, group, or via private link |
|
| 659 | + * @param string $itemType |
|
| 660 | + * @param string $itemSource |
|
| 661 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 662 | + * @param string $shareWith User or group the item is being shared with |
|
| 663 | + * @param int $permissions CRUDS |
|
| 664 | + * @param string $itemSourceName |
|
| 665 | + * @param \DateTime $expirationDate |
|
| 666 | + * @param bool $passwordChanged |
|
| 667 | + * @return boolean|string Returns true on success or false on failure, Returns token on success for links |
|
| 668 | + * @throws \OC\HintException when the share type is remote and the shareWith is invalid |
|
| 669 | + * @throws \Exception |
|
| 670 | + */ |
|
| 671 | + public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) { |
|
| 672 | + |
|
| 673 | + $backend = self::getBackend($itemType); |
|
| 674 | + $l = \OC::$server->getL10N('lib'); |
|
| 675 | + |
|
| 676 | + if ($backend->isShareTypeAllowed($shareType) === false) { |
|
| 677 | + $message = 'Sharing %s failed, because the backend does not allow shares from type %i'; |
|
| 678 | + $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType)); |
|
| 679 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG); |
|
| 680 | + throw new \Exception($message_t); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + $uidOwner = \OC_User::getUser(); |
|
| 684 | + $shareWithinGroupOnly = self::shareWithGroupMembersOnly(); |
|
| 685 | + |
|
| 686 | + if (is_null($itemSourceName)) { |
|
| 687 | + $itemSourceName = $itemSource; |
|
| 688 | + } |
|
| 689 | + $itemName = $itemSourceName; |
|
| 690 | + |
|
| 691 | + // check if file can be shared |
|
| 692 | + if ($itemType === 'file' or $itemType === 'folder') { |
|
| 693 | + $path = \OC\Files\Filesystem::getPath($itemSource); |
|
| 694 | + $itemName = $path; |
|
| 695 | + |
|
| 696 | + // verify that the file exists before we try to share it |
|
| 697 | + if (!$path) { |
|
| 698 | + $message = 'Sharing %s failed, because the file does not exist'; |
|
| 699 | + $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName)); |
|
| 700 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 701 | + throw new \Exception($message_t); |
|
| 702 | + } |
|
| 703 | + // verify that the user has share permission |
|
| 704 | + if (!\OC\Files\Filesystem::isSharable($path) || \OCP\Util::isSharingDisabledForUser()) { |
|
| 705 | + $message = 'You are not allowed to share %s'; |
|
| 706 | + $message_t = $l->t('You are not allowed to share %s', [$path]); |
|
| 707 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $path), \OCP\Util::DEBUG); |
|
| 708 | + throw new \Exception($message_t); |
|
| 709 | + } |
|
| 710 | + } |
|
| 711 | + |
|
| 712 | + //verify that we don't share a folder which already contains a share mount point |
|
| 713 | + if ($itemType === 'folder') { |
|
| 714 | + $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/'; |
|
| 715 | + $mountManager = \OC\Files\Filesystem::getMountManager(); |
|
| 716 | + $mounts = $mountManager->findIn($path); |
|
| 717 | + foreach ($mounts as $mount) { |
|
| 718 | + if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { |
|
| 719 | + $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!'; |
|
| 720 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 721 | + throw new \Exception($message); |
|
| 722 | + } |
|
| 723 | + |
|
| 724 | + } |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + // single file shares should never have delete permissions |
|
| 728 | + if ($itemType === 'file') { |
|
| 729 | + $permissions = (int)$permissions & ~\OCP\Constants::PERMISSION_DELETE; |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + //Validate expirationDate |
|
| 733 | + if ($expirationDate !== null) { |
|
| 734 | + try { |
|
| 735 | + /* |
|
| 736 | 736 | * Reuse the validateExpireDate. |
| 737 | 737 | * We have to pass time() since the second arg is the time |
| 738 | 738 | * the file was shared, since it is not shared yet we just use |
| 739 | 739 | * the current time. |
| 740 | 740 | */ |
| 741 | - $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource); |
|
| 742 | - } catch (\Exception $e) { |
|
| 743 | - throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404); |
|
| 744 | - } |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - // Verify share type and sharing conditions are met |
|
| 748 | - if ($shareType === self::SHARE_TYPE_USER) { |
|
| 749 | - if ($shareWith == $uidOwner) { |
|
| 750 | - $message = 'Sharing %s failed, because you can not share with yourself'; |
|
| 751 | - $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]); |
|
| 752 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 753 | - throw new \Exception($message_t); |
|
| 754 | - } |
|
| 755 | - if (!\OC_User::userExists($shareWith)) { |
|
| 756 | - $message = 'Sharing %s failed, because the user %s does not exist'; |
|
| 757 | - $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith)); |
|
| 758 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 759 | - throw new \Exception($message_t); |
|
| 760 | - } |
|
| 761 | - if ($shareWithinGroupOnly) { |
|
| 762 | - $userManager = \OC::$server->getUserManager(); |
|
| 763 | - $groupManager = \OC::$server->getGroupManager(); |
|
| 764 | - $userOwner = $userManager->get($uidOwner); |
|
| 765 | - $userShareWith = $userManager->get($shareWith); |
|
| 766 | - $groupsOwner = []; |
|
| 767 | - $groupsShareWith = []; |
|
| 768 | - if ($userOwner) { |
|
| 769 | - $groupsOwner = $groupManager->getUserGroupIds($userOwner); |
|
| 770 | - } |
|
| 771 | - if ($userShareWith) { |
|
| 772 | - $groupsShareWith = $groupManager->getUserGroupIds($userShareWith); |
|
| 773 | - } |
|
| 774 | - $inGroup = array_intersect($groupsOwner, $groupsShareWith); |
|
| 775 | - if (empty($inGroup)) { |
|
| 776 | - $message = 'Sharing %s failed, because the user ' |
|
| 777 | - .'%s is not a member of any groups that %s is a member of'; |
|
| 778 | - $message_t = $l->t('Sharing %s failed, because the user %s is not a member of any groups that %s is a member of', array($itemName, $shareWith, $uidOwner)); |
|
| 779 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG); |
|
| 780 | - throw new \Exception($message_t); |
|
| 781 | - } |
|
| 782 | - } |
|
| 783 | - // Check if the item source is already shared with the user, either from the same owner or a different user |
|
| 784 | - if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, |
|
| 785 | - $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 786 | - // Only allow the same share to occur again if it is the same |
|
| 787 | - // owner and is not a user share, this use case is for increasing |
|
| 788 | - // permissions for a specific user |
|
| 789 | - if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 790 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 791 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 792 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 793 | - throw new \Exception($message_t); |
|
| 794 | - } |
|
| 795 | - } |
|
| 796 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, |
|
| 797 | - $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 798 | - // Only allow the same share to occur again if it is the same |
|
| 799 | - // owner and is not a user share, this use case is for increasing |
|
| 800 | - // permissions for a specific user |
|
| 801 | - if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 802 | - $message = 'Sharing %s failed, because this item is already shared with user %s'; |
|
| 803 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith)); |
|
| 804 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR); |
|
| 805 | - throw new \Exception($message_t); |
|
| 806 | - } |
|
| 807 | - } |
|
| 808 | - } else if ($shareType === self::SHARE_TYPE_GROUP) { |
|
| 809 | - if (!\OC::$server->getGroupManager()->groupExists($shareWith)) { |
|
| 810 | - $message = 'Sharing %s failed, because the group %s does not exist'; |
|
| 811 | - $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); |
|
| 812 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 813 | - throw new \Exception($message_t); |
|
| 814 | - } |
|
| 815 | - if ($shareWithinGroupOnly) { |
|
| 816 | - $group = \OC::$server->getGroupManager()->get($shareWith); |
|
| 817 | - $user = \OC::$server->getUserManager()->get($uidOwner); |
|
| 818 | - if (!$group || !$user || !$group->inGroup($user)) { |
|
| 819 | - $message = 'Sharing %s failed, because ' |
|
| 820 | - . '%s is not a member of the group %s'; |
|
| 821 | - $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); |
|
| 822 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG); |
|
| 823 | - throw new \Exception($message_t); |
|
| 824 | - } |
|
| 825 | - } |
|
| 826 | - // Check if the item source is already shared with the group, either from the same owner or a different user |
|
| 827 | - // The check for each user in the group is done inside the put() function |
|
| 828 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, |
|
| 829 | - null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 830 | - |
|
| 831 | - if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 832 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 833 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 834 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 835 | - throw new \Exception($message_t); |
|
| 836 | - } |
|
| 837 | - } |
|
| 838 | - // Convert share with into an array with the keys group and users |
|
| 839 | - $group = $shareWith; |
|
| 840 | - $shareWith = array(); |
|
| 841 | - $shareWith['group'] = $group; |
|
| 842 | - |
|
| 843 | - |
|
| 844 | - $groupObject = \OC::$server->getGroupManager()->get($group); |
|
| 845 | - $userIds = []; |
|
| 846 | - if ($groupObject) { |
|
| 847 | - $users = $groupObject->searchUsers('', -1, 0); |
|
| 848 | - foreach ($users as $user) { |
|
| 849 | - $userIds[] = $user->getUID(); |
|
| 850 | - } |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - $shareWith['users'] = array_diff($userIds, array($uidOwner)); |
|
| 854 | - } else if ($shareType === self::SHARE_TYPE_LINK) { |
|
| 855 | - $updateExistingShare = false; |
|
| 856 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { |
|
| 857 | - |
|
| 858 | - // IF the password is changed via the old ajax endpoint verify it before deleting the old share |
|
| 859 | - if ($passwordChanged === true) { |
|
| 860 | - self::verifyPassword($shareWith); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - // when updating a link share |
|
| 864 | - // FIXME Don't delete link if we update it |
|
| 865 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, |
|
| 866 | - $uidOwner, self::FORMAT_NONE, null, 1)) { |
|
| 867 | - // remember old token |
|
| 868 | - $oldToken = $checkExists['token']; |
|
| 869 | - $oldPermissions = $checkExists['permissions']; |
|
| 870 | - //delete the old share |
|
| 871 | - Helper::delete($checkExists['id']); |
|
| 872 | - $updateExistingShare = true; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - if ($passwordChanged === null) { |
|
| 876 | - // Generate hash of password - same method as user passwords |
|
| 877 | - if (is_string($shareWith) && $shareWith !== '') { |
|
| 878 | - self::verifyPassword($shareWith); |
|
| 879 | - $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 880 | - } else { |
|
| 881 | - // reuse the already set password, but only if we change permissions |
|
| 882 | - // otherwise the user disabled the password protection |
|
| 883 | - if ($checkExists && (int)$permissions !== (int)$oldPermissions) { |
|
| 884 | - $shareWith = $checkExists['share_with']; |
|
| 885 | - } |
|
| 886 | - } |
|
| 887 | - } else { |
|
| 888 | - if ($passwordChanged === true) { |
|
| 889 | - if (is_string($shareWith) && $shareWith !== '') { |
|
| 890 | - self::verifyPassword($shareWith); |
|
| 891 | - $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 892 | - } |
|
| 893 | - } else if ($updateExistingShare) { |
|
| 894 | - $shareWith = $checkExists['share_with']; |
|
| 895 | - } |
|
| 896 | - } |
|
| 897 | - |
|
| 898 | - if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) { |
|
| 899 | - $message = 'You need to provide a password to create a public link, only protected links are allowed'; |
|
| 900 | - $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed'); |
|
| 901 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 902 | - throw new \Exception($message_t); |
|
| 903 | - } |
|
| 904 | - |
|
| 905 | - if ($updateExistingShare === false && |
|
| 906 | - self::isDefaultExpireDateEnabled() && |
|
| 907 | - empty($expirationDate)) { |
|
| 908 | - $expirationDate = Helper::calcExpireDate(); |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - // Generate token |
|
| 912 | - if (isset($oldToken)) { |
|
| 913 | - $token = $oldToken; |
|
| 914 | - } else { |
|
| 915 | - $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, |
|
| 916 | - \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_UPPER. |
|
| 917 | - \OCP\Security\ISecureRandom::CHAR_DIGITS |
|
| 918 | - ); |
|
| 919 | - } |
|
| 920 | - $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, |
|
| 921 | - null, $token, $itemSourceName, $expirationDate); |
|
| 922 | - if ($result) { |
|
| 923 | - return $token; |
|
| 924 | - } else { |
|
| 925 | - return false; |
|
| 926 | - } |
|
| 927 | - } |
|
| 928 | - $message = 'Sharing %s failed, because sharing with links is not allowed'; |
|
| 929 | - $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName)); |
|
| 930 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 931 | - throw new \Exception($message_t); |
|
| 932 | - } else if ($shareType === self::SHARE_TYPE_REMOTE) { |
|
| 933 | - |
|
| 934 | - /* |
|
| 741 | + $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource); |
|
| 742 | + } catch (\Exception $e) { |
|
| 743 | + throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404); |
|
| 744 | + } |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + // Verify share type and sharing conditions are met |
|
| 748 | + if ($shareType === self::SHARE_TYPE_USER) { |
|
| 749 | + if ($shareWith == $uidOwner) { |
|
| 750 | + $message = 'Sharing %s failed, because you can not share with yourself'; |
|
| 751 | + $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]); |
|
| 752 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 753 | + throw new \Exception($message_t); |
|
| 754 | + } |
|
| 755 | + if (!\OC_User::userExists($shareWith)) { |
|
| 756 | + $message = 'Sharing %s failed, because the user %s does not exist'; |
|
| 757 | + $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith)); |
|
| 758 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 759 | + throw new \Exception($message_t); |
|
| 760 | + } |
|
| 761 | + if ($shareWithinGroupOnly) { |
|
| 762 | + $userManager = \OC::$server->getUserManager(); |
|
| 763 | + $groupManager = \OC::$server->getGroupManager(); |
|
| 764 | + $userOwner = $userManager->get($uidOwner); |
|
| 765 | + $userShareWith = $userManager->get($shareWith); |
|
| 766 | + $groupsOwner = []; |
|
| 767 | + $groupsShareWith = []; |
|
| 768 | + if ($userOwner) { |
|
| 769 | + $groupsOwner = $groupManager->getUserGroupIds($userOwner); |
|
| 770 | + } |
|
| 771 | + if ($userShareWith) { |
|
| 772 | + $groupsShareWith = $groupManager->getUserGroupIds($userShareWith); |
|
| 773 | + } |
|
| 774 | + $inGroup = array_intersect($groupsOwner, $groupsShareWith); |
|
| 775 | + if (empty($inGroup)) { |
|
| 776 | + $message = 'Sharing %s failed, because the user ' |
|
| 777 | + .'%s is not a member of any groups that %s is a member of'; |
|
| 778 | + $message_t = $l->t('Sharing %s failed, because the user %s is not a member of any groups that %s is a member of', array($itemName, $shareWith, $uidOwner)); |
|
| 779 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG); |
|
| 780 | + throw new \Exception($message_t); |
|
| 781 | + } |
|
| 782 | + } |
|
| 783 | + // Check if the item source is already shared with the user, either from the same owner or a different user |
|
| 784 | + if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, |
|
| 785 | + $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 786 | + // Only allow the same share to occur again if it is the same |
|
| 787 | + // owner and is not a user share, this use case is for increasing |
|
| 788 | + // permissions for a specific user |
|
| 789 | + if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 790 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 791 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 792 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 793 | + throw new \Exception($message_t); |
|
| 794 | + } |
|
| 795 | + } |
|
| 796 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, |
|
| 797 | + $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 798 | + // Only allow the same share to occur again if it is the same |
|
| 799 | + // owner and is not a user share, this use case is for increasing |
|
| 800 | + // permissions for a specific user |
|
| 801 | + if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { |
|
| 802 | + $message = 'Sharing %s failed, because this item is already shared with user %s'; |
|
| 803 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith)); |
|
| 804 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR); |
|
| 805 | + throw new \Exception($message_t); |
|
| 806 | + } |
|
| 807 | + } |
|
| 808 | + } else if ($shareType === self::SHARE_TYPE_GROUP) { |
|
| 809 | + if (!\OC::$server->getGroupManager()->groupExists($shareWith)) { |
|
| 810 | + $message = 'Sharing %s failed, because the group %s does not exist'; |
|
| 811 | + $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); |
|
| 812 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 813 | + throw new \Exception($message_t); |
|
| 814 | + } |
|
| 815 | + if ($shareWithinGroupOnly) { |
|
| 816 | + $group = \OC::$server->getGroupManager()->get($shareWith); |
|
| 817 | + $user = \OC::$server->getUserManager()->get($uidOwner); |
|
| 818 | + if (!$group || !$user || !$group->inGroup($user)) { |
|
| 819 | + $message = 'Sharing %s failed, because ' |
|
| 820 | + . '%s is not a member of the group %s'; |
|
| 821 | + $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); |
|
| 822 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG); |
|
| 823 | + throw new \Exception($message_t); |
|
| 824 | + } |
|
| 825 | + } |
|
| 826 | + // Check if the item source is already shared with the group, either from the same owner or a different user |
|
| 827 | + // The check for each user in the group is done inside the put() function |
|
| 828 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, |
|
| 829 | + null, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 830 | + |
|
| 831 | + if ($checkExists['share_with'] === $shareWith && $checkExists['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 832 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 833 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 834 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 835 | + throw new \Exception($message_t); |
|
| 836 | + } |
|
| 837 | + } |
|
| 838 | + // Convert share with into an array with the keys group and users |
|
| 839 | + $group = $shareWith; |
|
| 840 | + $shareWith = array(); |
|
| 841 | + $shareWith['group'] = $group; |
|
| 842 | + |
|
| 843 | + |
|
| 844 | + $groupObject = \OC::$server->getGroupManager()->get($group); |
|
| 845 | + $userIds = []; |
|
| 846 | + if ($groupObject) { |
|
| 847 | + $users = $groupObject->searchUsers('', -1, 0); |
|
| 848 | + foreach ($users as $user) { |
|
| 849 | + $userIds[] = $user->getUID(); |
|
| 850 | + } |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + $shareWith['users'] = array_diff($userIds, array($uidOwner)); |
|
| 854 | + } else if ($shareType === self::SHARE_TYPE_LINK) { |
|
| 855 | + $updateExistingShare = false; |
|
| 856 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { |
|
| 857 | + |
|
| 858 | + // IF the password is changed via the old ajax endpoint verify it before deleting the old share |
|
| 859 | + if ($passwordChanged === true) { |
|
| 860 | + self::verifyPassword($shareWith); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + // when updating a link share |
|
| 864 | + // FIXME Don't delete link if we update it |
|
| 865 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, |
|
| 866 | + $uidOwner, self::FORMAT_NONE, null, 1)) { |
|
| 867 | + // remember old token |
|
| 868 | + $oldToken = $checkExists['token']; |
|
| 869 | + $oldPermissions = $checkExists['permissions']; |
|
| 870 | + //delete the old share |
|
| 871 | + Helper::delete($checkExists['id']); |
|
| 872 | + $updateExistingShare = true; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + if ($passwordChanged === null) { |
|
| 876 | + // Generate hash of password - same method as user passwords |
|
| 877 | + if (is_string($shareWith) && $shareWith !== '') { |
|
| 878 | + self::verifyPassword($shareWith); |
|
| 879 | + $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 880 | + } else { |
|
| 881 | + // reuse the already set password, but only if we change permissions |
|
| 882 | + // otherwise the user disabled the password protection |
|
| 883 | + if ($checkExists && (int)$permissions !== (int)$oldPermissions) { |
|
| 884 | + $shareWith = $checkExists['share_with']; |
|
| 885 | + } |
|
| 886 | + } |
|
| 887 | + } else { |
|
| 888 | + if ($passwordChanged === true) { |
|
| 889 | + if (is_string($shareWith) && $shareWith !== '') { |
|
| 890 | + self::verifyPassword($shareWith); |
|
| 891 | + $shareWith = \OC::$server->getHasher()->hash($shareWith); |
|
| 892 | + } |
|
| 893 | + } else if ($updateExistingShare) { |
|
| 894 | + $shareWith = $checkExists['share_with']; |
|
| 895 | + } |
|
| 896 | + } |
|
| 897 | + |
|
| 898 | + if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) { |
|
| 899 | + $message = 'You need to provide a password to create a public link, only protected links are allowed'; |
|
| 900 | + $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed'); |
|
| 901 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 902 | + throw new \Exception($message_t); |
|
| 903 | + } |
|
| 904 | + |
|
| 905 | + if ($updateExistingShare === false && |
|
| 906 | + self::isDefaultExpireDateEnabled() && |
|
| 907 | + empty($expirationDate)) { |
|
| 908 | + $expirationDate = Helper::calcExpireDate(); |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + // Generate token |
|
| 912 | + if (isset($oldToken)) { |
|
| 913 | + $token = $oldToken; |
|
| 914 | + } else { |
|
| 915 | + $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, |
|
| 916 | + \OCP\Security\ISecureRandom::CHAR_LOWER.\OCP\Security\ISecureRandom::CHAR_UPPER. |
|
| 917 | + \OCP\Security\ISecureRandom::CHAR_DIGITS |
|
| 918 | + ); |
|
| 919 | + } |
|
| 920 | + $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, |
|
| 921 | + null, $token, $itemSourceName, $expirationDate); |
|
| 922 | + if ($result) { |
|
| 923 | + return $token; |
|
| 924 | + } else { |
|
| 925 | + return false; |
|
| 926 | + } |
|
| 927 | + } |
|
| 928 | + $message = 'Sharing %s failed, because sharing with links is not allowed'; |
|
| 929 | + $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName)); |
|
| 930 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 931 | + throw new \Exception($message_t); |
|
| 932 | + } else if ($shareType === self::SHARE_TYPE_REMOTE) { |
|
| 933 | + |
|
| 934 | + /* |
|
| 935 | 935 | * Check if file is not already shared with the remote user |
| 936 | 936 | */ |
| 937 | - if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE, |
|
| 938 | - $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 939 | - $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 940 | - $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 941 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 942 | - throw new \Exception($message_t); |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - // don't allow federated shares if source and target server are the same |
|
| 946 | - list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 947 | - $currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/')); |
|
| 948 | - $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 949 | - if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) { |
|
| 950 | - $message = 'Not allowed to create a federated share with the same user.'; |
|
| 951 | - $message_t = $l->t('Not allowed to create a federated share with the same user'); |
|
| 952 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 953 | - throw new \Exception($message_t); |
|
| 954 | - } |
|
| 955 | - |
|
| 956 | - $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . |
|
| 957 | - \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 958 | - |
|
| 959 | - $shareWith = $user . '@' . $remote; |
|
| 960 | - $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName); |
|
| 961 | - |
|
| 962 | - $send = false; |
|
| 963 | - if ($shareId) { |
|
| 964 | - $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner); |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - if ($send === false) { |
|
| 968 | - $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 969 | - self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser); |
|
| 970 | - $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith)); |
|
| 971 | - throw new \Exception($message_t); |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - return $send; |
|
| 975 | - } else { |
|
| 976 | - // Future share types need to include their own conditions |
|
| 977 | - $message = 'Share type %s is not valid for %s'; |
|
| 978 | - $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource)); |
|
| 979 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG); |
|
| 980 | - throw new \Exception($message_t); |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - // Put the item into the database |
|
| 984 | - $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate); |
|
| 985 | - |
|
| 986 | - return $result ? true : false; |
|
| 987 | - } |
|
| 988 | - |
|
| 989 | - /** |
|
| 990 | - * Unshare an item from a user, group, or delete a private link |
|
| 991 | - * @param string $itemType |
|
| 992 | - * @param string $itemSource |
|
| 993 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 994 | - * @param string $shareWith User or group the item is being shared with |
|
| 995 | - * @param string $owner owner of the share, if null the current user is used |
|
| 996 | - * @return boolean true on success or false on failure |
|
| 997 | - */ |
|
| 998 | - public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
| 999 | - |
|
| 1000 | - // check if it is a valid itemType |
|
| 1001 | - self::getBackend($itemType); |
|
| 1002 | - |
|
| 1003 | - $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
| 1004 | - |
|
| 1005 | - $toDelete = array(); |
|
| 1006 | - $newParent = null; |
|
| 1007 | - $currentUser = $owner ? $owner : \OC_User::getUser(); |
|
| 1008 | - foreach ($items as $item) { |
|
| 1009 | - // delete the item with the expected share_type and owner |
|
| 1010 | - if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 1011 | - $toDelete = $item; |
|
| 1012 | - // if there is more then one result we don't have to delete the children |
|
| 1013 | - // but update their parent. For group shares the new parent should always be |
|
| 1014 | - // the original group share and not the db entry with the unique name |
|
| 1015 | - } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 1016 | - $newParent = $item['parent']; |
|
| 1017 | - } else { |
|
| 1018 | - $newParent = $item['id']; |
|
| 1019 | - } |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - if (!empty($toDelete)) { |
|
| 1023 | - self::unshareItem($toDelete, $newParent); |
|
| 1024 | - return true; |
|
| 1025 | - } |
|
| 1026 | - return false; |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - /** |
|
| 1030 | - * Unshare an item from all users, groups, and remove all links |
|
| 1031 | - * @param string $itemType |
|
| 1032 | - * @param string $itemSource |
|
| 1033 | - * @return boolean true on success or false on failure |
|
| 1034 | - */ |
|
| 1035 | - public static function unshareAll($itemType, $itemSource) { |
|
| 1036 | - // Get all of the owners of shares of this item. |
|
| 1037 | - $query = \OC_DB::prepare( 'SELECT `uid_owner` from `*PREFIX*share` WHERE `item_type`=? AND `item_source`=?' ); |
|
| 1038 | - $result = $query->execute(array($itemType, $itemSource)); |
|
| 1039 | - $shares = array(); |
|
| 1040 | - // Add each owner's shares to the array of all shares for this item. |
|
| 1041 | - while ($row = $result->fetchRow()) { |
|
| 1042 | - $shares = array_merge($shares, self::getItems($itemType, $itemSource, null, null, $row['uid_owner'])); |
|
| 1043 | - } |
|
| 1044 | - if (!empty($shares)) { |
|
| 1045 | - // Pass all the vars we have for now, they may be useful |
|
| 1046 | - $hookParams = array( |
|
| 1047 | - 'itemType' => $itemType, |
|
| 1048 | - 'itemSource' => $itemSource, |
|
| 1049 | - 'shares' => $shares, |
|
| 1050 | - ); |
|
| 1051 | - \OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams); |
|
| 1052 | - foreach ($shares as $share) { |
|
| 1053 | - self::unshareItem($share); |
|
| 1054 | - } |
|
| 1055 | - \OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams); |
|
| 1056 | - return true; |
|
| 1057 | - } |
|
| 1058 | - return false; |
|
| 1059 | - } |
|
| 1060 | - |
|
| 1061 | - /** |
|
| 1062 | - * Unshare an item shared with the current user |
|
| 1063 | - * @param string $itemType |
|
| 1064 | - * @param string $itemOrigin Item target or source |
|
| 1065 | - * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional) |
|
| 1066 | - * @return boolean true on success or false on failure |
|
| 1067 | - * |
|
| 1068 | - * Unsharing from self is not allowed for items inside collections |
|
| 1069 | - */ |
|
| 1070 | - public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) { |
|
| 1071 | - $originType = ($originIsSource) ? 'source' : 'target'; |
|
| 1072 | - $uid = \OCP\User::getUser(); |
|
| 1073 | - |
|
| 1074 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1075 | - $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?'; |
|
| 1076 | - } else { |
|
| 1077 | - $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?'; |
|
| 1078 | - } |
|
| 1079 | - |
|
| 1080 | - $query = \OCP\DB::prepare($statement); |
|
| 1081 | - $result = $query->execute(array($itemType, $itemOrigin)); |
|
| 1082 | - |
|
| 1083 | - $shares = $result->fetchAll(); |
|
| 1084 | - |
|
| 1085 | - $listOfUnsharedItems = array(); |
|
| 1086 | - |
|
| 1087 | - $itemUnshared = false; |
|
| 1088 | - foreach ($shares as $share) { |
|
| 1089 | - if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER && |
|
| 1090 | - $share['share_with'] === $uid) { |
|
| 1091 | - $deletedShares = Helper::delete($share['id']); |
|
| 1092 | - $shareTmp = array( |
|
| 1093 | - 'id' => $share['id'], |
|
| 1094 | - 'shareWith' => $share['share_with'], |
|
| 1095 | - 'itemTarget' => $share['item_target'], |
|
| 1096 | - 'itemType' => $share['item_type'], |
|
| 1097 | - 'shareType' => (int)$share['share_type'], |
|
| 1098 | - ); |
|
| 1099 | - if (isset($share['file_target'])) { |
|
| 1100 | - $shareTmp['fileTarget'] = $share['file_target']; |
|
| 1101 | - } |
|
| 1102 | - $listOfUnsharedItems = array_merge($listOfUnsharedItems, $deletedShares, array($shareTmp)); |
|
| 1103 | - $itemUnshared = true; |
|
| 1104 | - break; |
|
| 1105 | - } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1106 | - $group = \OC::$server->getGroupManager()->get($share['share_with']); |
|
| 1107 | - $user = \OC::$server->getUserManager()->get($uid); |
|
| 1108 | - if ($group && $user && $group->inGroup($user)) { |
|
| 1109 | - $groupShare = $share; |
|
| 1110 | - } |
|
| 1111 | - } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique && |
|
| 1112 | - $share['share_with'] === $uid) { |
|
| 1113 | - $uniqueGroupShare = $share; |
|
| 1114 | - } |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - if (!$itemUnshared && isset($groupShare) && !isset($uniqueGroupShare)) { |
|
| 1118 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' |
|
| 1119 | - .' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,' |
|
| 1120 | - .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)' |
|
| 1121 | - .' VALUES (?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 1122 | - $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'], |
|
| 1123 | - $groupShare['id'], self::$shareTypeGroupUserUnique, |
|
| 1124 | - \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'], |
|
| 1125 | - $groupShare['file_target'])); |
|
| 1126 | - $shareTmp = array( |
|
| 1127 | - 'id' => $groupShare['id'], |
|
| 1128 | - 'shareWith' => $groupShare['share_with'], |
|
| 1129 | - 'itemTarget' => $groupShare['item_target'], |
|
| 1130 | - 'itemType' => $groupShare['item_type'], |
|
| 1131 | - 'shareType' => (int)$groupShare['share_type'], |
|
| 1132 | - ); |
|
| 1133 | - if (isset($groupShare['file_target'])) { |
|
| 1134 | - $shareTmp['fileTarget'] = $groupShare['file_target']; |
|
| 1135 | - } |
|
| 1136 | - $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1137 | - $itemUnshared = true; |
|
| 1138 | - } elseif (!$itemUnshared && isset($uniqueGroupShare)) { |
|
| 1139 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); |
|
| 1140 | - $query->execute(array(0, $uniqueGroupShare['id'])); |
|
| 1141 | - $shareTmp = array( |
|
| 1142 | - 'id' => $uniqueGroupShare['id'], |
|
| 1143 | - 'shareWith' => $uniqueGroupShare['share_with'], |
|
| 1144 | - 'itemTarget' => $uniqueGroupShare['item_target'], |
|
| 1145 | - 'itemType' => $uniqueGroupShare['item_type'], |
|
| 1146 | - 'shareType' => (int)$uniqueGroupShare['share_type'], |
|
| 1147 | - ); |
|
| 1148 | - if (isset($uniqueGroupShare['file_target'])) { |
|
| 1149 | - $shareTmp['fileTarget'] = $uniqueGroupShare['file_target']; |
|
| 1150 | - } |
|
| 1151 | - $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1152 | - $itemUnshared = true; |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - if ($itemUnshared) { |
|
| 1156 | - \OC_Hook::emit('OCP\Share', 'post_unshareFromSelf', |
|
| 1157 | - array('unsharedItems' => $listOfUnsharedItems, 'itemType' => $itemType)); |
|
| 1158 | - } |
|
| 1159 | - |
|
| 1160 | - return $itemUnshared; |
|
| 1161 | - } |
|
| 1162 | - |
|
| 1163 | - /** |
|
| 1164 | - * sent status if users got informed by mail about share |
|
| 1165 | - * @param string $itemType |
|
| 1166 | - * @param string $itemSource |
|
| 1167 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1168 | - * @param string $recipient with whom was the file shared |
|
| 1169 | - * @param boolean $status |
|
| 1170 | - */ |
|
| 1171 | - public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) { |
|
| 1172 | - $status = $status ? 1 : 0; |
|
| 1173 | - |
|
| 1174 | - $query = \OC_DB::prepare( |
|
| 1175 | - 'UPDATE `*PREFIX*share` |
|
| 937 | + if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE, |
|
| 938 | + $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) { |
|
| 939 | + $message = 'Sharing %s failed, because this item is already shared with %s'; |
|
| 940 | + $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); |
|
| 941 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 942 | + throw new \Exception($message_t); |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + // don't allow federated shares if source and target server are the same |
|
| 946 | + list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 947 | + $currentServer = self::removeProtocolFromUrl(\OC::$server->getURLGenerator()->getAbsoluteURL('/')); |
|
| 948 | + $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 949 | + if (Helper::isSameUserOnSameServer($user, $remote, $currentUser, $currentServer)) { |
|
| 950 | + $message = 'Not allowed to create a federated share with the same user.'; |
|
| 951 | + $message_t = $l->t('Not allowed to create a federated share with the same user'); |
|
| 952 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::DEBUG); |
|
| 953 | + throw new \Exception($message_t); |
|
| 954 | + } |
|
| 955 | + |
|
| 956 | + $token = \OC::$server->getSecureRandom()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . |
|
| 957 | + \OCP\Security\ISecureRandom::CHAR_DIGITS); |
|
| 958 | + |
|
| 959 | + $shareWith = $user . '@' . $remote; |
|
| 960 | + $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName); |
|
| 961 | + |
|
| 962 | + $send = false; |
|
| 963 | + if ($shareId) { |
|
| 964 | + $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner); |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + if ($send === false) { |
|
| 968 | + $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); |
|
| 969 | + self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser); |
|
| 970 | + $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith)); |
|
| 971 | + throw new \Exception($message_t); |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + return $send; |
|
| 975 | + } else { |
|
| 976 | + // Future share types need to include their own conditions |
|
| 977 | + $message = 'Share type %s is not valid for %s'; |
|
| 978 | + $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource)); |
|
| 979 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG); |
|
| 980 | + throw new \Exception($message_t); |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + // Put the item into the database |
|
| 984 | + $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate); |
|
| 985 | + |
|
| 986 | + return $result ? true : false; |
|
| 987 | + } |
|
| 988 | + |
|
| 989 | + /** |
|
| 990 | + * Unshare an item from a user, group, or delete a private link |
|
| 991 | + * @param string $itemType |
|
| 992 | + * @param string $itemSource |
|
| 993 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 994 | + * @param string $shareWith User or group the item is being shared with |
|
| 995 | + * @param string $owner owner of the share, if null the current user is used |
|
| 996 | + * @return boolean true on success or false on failure |
|
| 997 | + */ |
|
| 998 | + public static function unshare($itemType, $itemSource, $shareType, $shareWith, $owner = null) { |
|
| 999 | + |
|
| 1000 | + // check if it is a valid itemType |
|
| 1001 | + self::getBackend($itemType); |
|
| 1002 | + |
|
| 1003 | + $items = self::getItemSharedWithUser($itemType, $itemSource, $shareWith, $owner, $shareType); |
|
| 1004 | + |
|
| 1005 | + $toDelete = array(); |
|
| 1006 | + $newParent = null; |
|
| 1007 | + $currentUser = $owner ? $owner : \OC_User::getUser(); |
|
| 1008 | + foreach ($items as $item) { |
|
| 1009 | + // delete the item with the expected share_type and owner |
|
| 1010 | + if ((int)$item['share_type'] === (int)$shareType && $item['uid_owner'] === $currentUser) { |
|
| 1011 | + $toDelete = $item; |
|
| 1012 | + // if there is more then one result we don't have to delete the children |
|
| 1013 | + // but update their parent. For group shares the new parent should always be |
|
| 1014 | + // the original group share and not the db entry with the unique name |
|
| 1015 | + } else if ((int)$item['share_type'] === self::$shareTypeGroupUserUnique) { |
|
| 1016 | + $newParent = $item['parent']; |
|
| 1017 | + } else { |
|
| 1018 | + $newParent = $item['id']; |
|
| 1019 | + } |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + if (!empty($toDelete)) { |
|
| 1023 | + self::unshareItem($toDelete, $newParent); |
|
| 1024 | + return true; |
|
| 1025 | + } |
|
| 1026 | + return false; |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + /** |
|
| 1030 | + * Unshare an item from all users, groups, and remove all links |
|
| 1031 | + * @param string $itemType |
|
| 1032 | + * @param string $itemSource |
|
| 1033 | + * @return boolean true on success or false on failure |
|
| 1034 | + */ |
|
| 1035 | + public static function unshareAll($itemType, $itemSource) { |
|
| 1036 | + // Get all of the owners of shares of this item. |
|
| 1037 | + $query = \OC_DB::prepare( 'SELECT `uid_owner` from `*PREFIX*share` WHERE `item_type`=? AND `item_source`=?' ); |
|
| 1038 | + $result = $query->execute(array($itemType, $itemSource)); |
|
| 1039 | + $shares = array(); |
|
| 1040 | + // Add each owner's shares to the array of all shares for this item. |
|
| 1041 | + while ($row = $result->fetchRow()) { |
|
| 1042 | + $shares = array_merge($shares, self::getItems($itemType, $itemSource, null, null, $row['uid_owner'])); |
|
| 1043 | + } |
|
| 1044 | + if (!empty($shares)) { |
|
| 1045 | + // Pass all the vars we have for now, they may be useful |
|
| 1046 | + $hookParams = array( |
|
| 1047 | + 'itemType' => $itemType, |
|
| 1048 | + 'itemSource' => $itemSource, |
|
| 1049 | + 'shares' => $shares, |
|
| 1050 | + ); |
|
| 1051 | + \OC_Hook::emit('OCP\Share', 'pre_unshareAll', $hookParams); |
|
| 1052 | + foreach ($shares as $share) { |
|
| 1053 | + self::unshareItem($share); |
|
| 1054 | + } |
|
| 1055 | + \OC_Hook::emit('OCP\Share', 'post_unshareAll', $hookParams); |
|
| 1056 | + return true; |
|
| 1057 | + } |
|
| 1058 | + return false; |
|
| 1059 | + } |
|
| 1060 | + |
|
| 1061 | + /** |
|
| 1062 | + * Unshare an item shared with the current user |
|
| 1063 | + * @param string $itemType |
|
| 1064 | + * @param string $itemOrigin Item target or source |
|
| 1065 | + * @param boolean $originIsSource true if $itemOrigin is the source, false if $itemOrigin is the target (optional) |
|
| 1066 | + * @return boolean true on success or false on failure |
|
| 1067 | + * |
|
| 1068 | + * Unsharing from self is not allowed for items inside collections |
|
| 1069 | + */ |
|
| 1070 | + public static function unshareFromSelf($itemType, $itemOrigin, $originIsSource = false) { |
|
| 1071 | + $originType = ($originIsSource) ? 'source' : 'target'; |
|
| 1072 | + $uid = \OCP\User::getUser(); |
|
| 1073 | + |
|
| 1074 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1075 | + $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `file_' . $originType . '` = ?'; |
|
| 1076 | + } else { |
|
| 1077 | + $statement = 'SELECT * FROM `*PREFIX*share` WHERE `item_type` = ? and `item_' . $originType . '` = ?'; |
|
| 1078 | + } |
|
| 1079 | + |
|
| 1080 | + $query = \OCP\DB::prepare($statement); |
|
| 1081 | + $result = $query->execute(array($itemType, $itemOrigin)); |
|
| 1082 | + |
|
| 1083 | + $shares = $result->fetchAll(); |
|
| 1084 | + |
|
| 1085 | + $listOfUnsharedItems = array(); |
|
| 1086 | + |
|
| 1087 | + $itemUnshared = false; |
|
| 1088 | + foreach ($shares as $share) { |
|
| 1089 | + if ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_USER && |
|
| 1090 | + $share['share_with'] === $uid) { |
|
| 1091 | + $deletedShares = Helper::delete($share['id']); |
|
| 1092 | + $shareTmp = array( |
|
| 1093 | + 'id' => $share['id'], |
|
| 1094 | + 'shareWith' => $share['share_with'], |
|
| 1095 | + 'itemTarget' => $share['item_target'], |
|
| 1096 | + 'itemType' => $share['item_type'], |
|
| 1097 | + 'shareType' => (int)$share['share_type'], |
|
| 1098 | + ); |
|
| 1099 | + if (isset($share['file_target'])) { |
|
| 1100 | + $shareTmp['fileTarget'] = $share['file_target']; |
|
| 1101 | + } |
|
| 1102 | + $listOfUnsharedItems = array_merge($listOfUnsharedItems, $deletedShares, array($shareTmp)); |
|
| 1103 | + $itemUnshared = true; |
|
| 1104 | + break; |
|
| 1105 | + } elseif ((int)$share['share_type'] === \OCP\Share::SHARE_TYPE_GROUP) { |
|
| 1106 | + $group = \OC::$server->getGroupManager()->get($share['share_with']); |
|
| 1107 | + $user = \OC::$server->getUserManager()->get($uid); |
|
| 1108 | + if ($group && $user && $group->inGroup($user)) { |
|
| 1109 | + $groupShare = $share; |
|
| 1110 | + } |
|
| 1111 | + } elseif ((int)$share['share_type'] === self::$shareTypeGroupUserUnique && |
|
| 1112 | + $share['share_with'] === $uid) { |
|
| 1113 | + $uniqueGroupShare = $share; |
|
| 1114 | + } |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + if (!$itemUnshared && isset($groupShare) && !isset($uniqueGroupShare)) { |
|
| 1118 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share`' |
|
| 1119 | + .' (`item_type`, `item_source`, `item_target`, `parent`, `share_type`,' |
|
| 1120 | + .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`, `file_target`)' |
|
| 1121 | + .' VALUES (?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 1122 | + $query->execute(array($groupShare['item_type'], $groupShare['item_source'], $groupShare['item_target'], |
|
| 1123 | + $groupShare['id'], self::$shareTypeGroupUserUnique, |
|
| 1124 | + \OC_User::getUser(), $groupShare['uid_owner'], 0, $groupShare['stime'], $groupShare['file_source'], |
|
| 1125 | + $groupShare['file_target'])); |
|
| 1126 | + $shareTmp = array( |
|
| 1127 | + 'id' => $groupShare['id'], |
|
| 1128 | + 'shareWith' => $groupShare['share_with'], |
|
| 1129 | + 'itemTarget' => $groupShare['item_target'], |
|
| 1130 | + 'itemType' => $groupShare['item_type'], |
|
| 1131 | + 'shareType' => (int)$groupShare['share_type'], |
|
| 1132 | + ); |
|
| 1133 | + if (isset($groupShare['file_target'])) { |
|
| 1134 | + $shareTmp['fileTarget'] = $groupShare['file_target']; |
|
| 1135 | + } |
|
| 1136 | + $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1137 | + $itemUnshared = true; |
|
| 1138 | + } elseif (!$itemUnshared && isset($uniqueGroupShare)) { |
|
| 1139 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = ? WHERE `id` = ?'); |
|
| 1140 | + $query->execute(array(0, $uniqueGroupShare['id'])); |
|
| 1141 | + $shareTmp = array( |
|
| 1142 | + 'id' => $uniqueGroupShare['id'], |
|
| 1143 | + 'shareWith' => $uniqueGroupShare['share_with'], |
|
| 1144 | + 'itemTarget' => $uniqueGroupShare['item_target'], |
|
| 1145 | + 'itemType' => $uniqueGroupShare['item_type'], |
|
| 1146 | + 'shareType' => (int)$uniqueGroupShare['share_type'], |
|
| 1147 | + ); |
|
| 1148 | + if (isset($uniqueGroupShare['file_target'])) { |
|
| 1149 | + $shareTmp['fileTarget'] = $uniqueGroupShare['file_target']; |
|
| 1150 | + } |
|
| 1151 | + $listOfUnsharedItems = array_merge($listOfUnsharedItems, [$shareTmp]); |
|
| 1152 | + $itemUnshared = true; |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + if ($itemUnshared) { |
|
| 1156 | + \OC_Hook::emit('OCP\Share', 'post_unshareFromSelf', |
|
| 1157 | + array('unsharedItems' => $listOfUnsharedItems, 'itemType' => $itemType)); |
|
| 1158 | + } |
|
| 1159 | + |
|
| 1160 | + return $itemUnshared; |
|
| 1161 | + } |
|
| 1162 | + |
|
| 1163 | + /** |
|
| 1164 | + * sent status if users got informed by mail about share |
|
| 1165 | + * @param string $itemType |
|
| 1166 | + * @param string $itemSource |
|
| 1167 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1168 | + * @param string $recipient with whom was the file shared |
|
| 1169 | + * @param boolean $status |
|
| 1170 | + */ |
|
| 1171 | + public static function setSendMailStatus($itemType, $itemSource, $shareType, $recipient, $status) { |
|
| 1172 | + $status = $status ? 1 : 0; |
|
| 1173 | + |
|
| 1174 | + $query = \OC_DB::prepare( |
|
| 1175 | + 'UPDATE `*PREFIX*share` |
|
| 1176 | 1176 | SET `mail_send` = ? |
| 1177 | 1177 | WHERE `item_type` = ? AND `item_source` = ? AND `share_type` = ? AND `share_with` = ?'); |
| 1178 | 1178 | |
| 1179 | - $result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient)); |
|
| 1180 | - |
|
| 1181 | - if($result === false) { |
|
| 1182 | - \OCP\Util::writeLog('OCP\Share', 'Couldn\'t set send mail status', \OCP\Util::ERROR); |
|
| 1183 | - } |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - /** |
|
| 1187 | - * Set the permissions of an item for a specific user or group |
|
| 1188 | - * @param string $itemType |
|
| 1189 | - * @param string $itemSource |
|
| 1190 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1191 | - * @param string $shareWith User or group the item is being shared with |
|
| 1192 | - * @param int $permissions CRUDS permissions |
|
| 1193 | - * @return boolean true on success or false on failure |
|
| 1194 | - * @throws \Exception when trying to grant more permissions then the user has himself |
|
| 1195 | - */ |
|
| 1196 | - public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) { |
|
| 1197 | - $l = \OC::$server->getL10N('lib'); |
|
| 1198 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 1199 | - |
|
| 1200 | - $intArrayToLiteralArray = function($intArray, $eb) { |
|
| 1201 | - return array_map(function($int) use ($eb) { |
|
| 1202 | - return $eb->literal((int)$int, 'integer'); |
|
| 1203 | - }, $intArray); |
|
| 1204 | - }; |
|
| 1205 | - $sanitizeItem = function($item) { |
|
| 1206 | - $item['id'] = (int)$item['id']; |
|
| 1207 | - $item['premissions'] = (int)$item['permissions']; |
|
| 1208 | - return $item; |
|
| 1209 | - }; |
|
| 1210 | - |
|
| 1211 | - if ($rootItem = self::getItems($itemType, $itemSource, $shareType, $shareWith, |
|
| 1212 | - \OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) { |
|
| 1213 | - // Check if this item is a reshare and verify that the permissions |
|
| 1214 | - // granted don't exceed the parent shared item |
|
| 1215 | - if (isset($rootItem['parent'])) { |
|
| 1216 | - $qb = $connection->getQueryBuilder(); |
|
| 1217 | - $qb->select('permissions') |
|
| 1218 | - ->from('share') |
|
| 1219 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1220 | - ->setParameter(':id', $rootItem['parent']); |
|
| 1221 | - $dbresult = $qb->execute(); |
|
| 1222 | - |
|
| 1223 | - $result = $dbresult->fetch(); |
|
| 1224 | - $dbresult->closeCursor(); |
|
| 1225 | - if (~(int)$result['permissions'] & $permissions) { |
|
| 1226 | - $message = 'Setting permissions for %s failed,' |
|
| 1227 | - .' because the permissions exceed permissions granted to %s'; |
|
| 1228 | - $message_t = $l->t('Setting permissions for %s failed, because the permissions exceed permissions granted to %s', array($itemSource, \OC_User::getUser())); |
|
| 1229 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, \OC_User::getUser()), \OCP\Util::DEBUG); |
|
| 1230 | - throw new \Exception($message_t); |
|
| 1231 | - } |
|
| 1232 | - } |
|
| 1233 | - $qb = $connection->getQueryBuilder(); |
|
| 1234 | - $qb->update('share') |
|
| 1235 | - ->set('permissions', $qb->createParameter('permissions')) |
|
| 1236 | - ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1237 | - ->setParameter(':id', $rootItem['id']) |
|
| 1238 | - ->setParameter(':permissions', $permissions); |
|
| 1239 | - $qb->execute(); |
|
| 1240 | - if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1241 | - \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 1242 | - 'itemType' => $itemType, |
|
| 1243 | - 'itemSource' => $itemSource, |
|
| 1244 | - 'shareType' => $shareType, |
|
| 1245 | - 'shareWith' => $shareWith, |
|
| 1246 | - 'uidOwner' => \OC_User::getUser(), |
|
| 1247 | - 'permissions' => $permissions, |
|
| 1248 | - 'path' => $rootItem['path'], |
|
| 1249 | - 'share' => $rootItem |
|
| 1250 | - )); |
|
| 1251 | - } |
|
| 1252 | - |
|
| 1253 | - // Share id's to update with the new permissions |
|
| 1254 | - $ids = []; |
|
| 1255 | - $items = []; |
|
| 1256 | - |
|
| 1257 | - // Check if permissions were removed |
|
| 1258 | - if ((int)$rootItem['permissions'] & ~$permissions) { |
|
| 1259 | - // If share permission is removed all reshares must be deleted |
|
| 1260 | - if (($rootItem['permissions'] & \OCP\Constants::PERMISSION_SHARE) && (~$permissions & \OCP\Constants::PERMISSION_SHARE)) { |
|
| 1261 | - // delete all shares, keep parent and group children |
|
| 1262 | - Helper::delete($rootItem['id'], true, null, null, true); |
|
| 1263 | - } |
|
| 1264 | - |
|
| 1265 | - // Remove permission from all children |
|
| 1266 | - $parents = [$rootItem['id']]; |
|
| 1267 | - while (!empty($parents)) { |
|
| 1268 | - $parents = $intArrayToLiteralArray($parents, $qb->expr()); |
|
| 1269 | - $qb = $connection->getQueryBuilder(); |
|
| 1270 | - $qb->select('id', 'permissions', 'item_type') |
|
| 1271 | - ->from('share') |
|
| 1272 | - ->where($qb->expr()->in('parent', $parents)); |
|
| 1273 | - $result = $qb->execute(); |
|
| 1274 | - // Reset parents array, only go through loop again if |
|
| 1275 | - // items are found that need permissions removed |
|
| 1276 | - $parents = []; |
|
| 1277 | - while ($item = $result->fetch()) { |
|
| 1278 | - $item = $sanitizeItem($item); |
|
| 1279 | - |
|
| 1280 | - $items[] = $item; |
|
| 1281 | - // Check if permissions need to be removed |
|
| 1282 | - if ($item['permissions'] & ~$permissions) { |
|
| 1283 | - // Add to list of items that need permissions removed |
|
| 1284 | - $ids[] = $item['id']; |
|
| 1285 | - $parents[] = $item['id']; |
|
| 1286 | - } |
|
| 1287 | - } |
|
| 1288 | - $result->closeCursor(); |
|
| 1289 | - } |
|
| 1290 | - |
|
| 1291 | - // Remove the permissions for all reshares of this item |
|
| 1292 | - if (!empty($ids)) { |
|
| 1293 | - $ids = "'".implode("','", $ids)."'"; |
|
| 1294 | - // TODO this should be done with Doctrine platform objects |
|
| 1295 | - if (\OC::$server->getConfig()->getSystemValue("dbtype") === 'oci') { |
|
| 1296 | - $andOp = 'BITAND(`permissions`, ?)'; |
|
| 1297 | - } else { |
|
| 1298 | - $andOp = '`permissions` & ?'; |
|
| 1299 | - } |
|
| 1300 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = '.$andOp |
|
| 1301 | - .' WHERE `id` IN ('.$ids.')'); |
|
| 1302 | - $query->execute(array($permissions)); |
|
| 1303 | - } |
|
| 1304 | - |
|
| 1305 | - } |
|
| 1306 | - |
|
| 1307 | - /* |
|
| 1179 | + $result = $query->execute(array($status, $itemType, $itemSource, $shareType, $recipient)); |
|
| 1180 | + |
|
| 1181 | + if($result === false) { |
|
| 1182 | + \OCP\Util::writeLog('OCP\Share', 'Couldn\'t set send mail status', \OCP\Util::ERROR); |
|
| 1183 | + } |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + /** |
|
| 1187 | + * Set the permissions of an item for a specific user or group |
|
| 1188 | + * @param string $itemType |
|
| 1189 | + * @param string $itemSource |
|
| 1190 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 1191 | + * @param string $shareWith User or group the item is being shared with |
|
| 1192 | + * @param int $permissions CRUDS permissions |
|
| 1193 | + * @return boolean true on success or false on failure |
|
| 1194 | + * @throws \Exception when trying to grant more permissions then the user has himself |
|
| 1195 | + */ |
|
| 1196 | + public static function setPermissions($itemType, $itemSource, $shareType, $shareWith, $permissions) { |
|
| 1197 | + $l = \OC::$server->getL10N('lib'); |
|
| 1198 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 1199 | + |
|
| 1200 | + $intArrayToLiteralArray = function($intArray, $eb) { |
|
| 1201 | + return array_map(function($int) use ($eb) { |
|
| 1202 | + return $eb->literal((int)$int, 'integer'); |
|
| 1203 | + }, $intArray); |
|
| 1204 | + }; |
|
| 1205 | + $sanitizeItem = function($item) { |
|
| 1206 | + $item['id'] = (int)$item['id']; |
|
| 1207 | + $item['premissions'] = (int)$item['permissions']; |
|
| 1208 | + return $item; |
|
| 1209 | + }; |
|
| 1210 | + |
|
| 1211 | + if ($rootItem = self::getItems($itemType, $itemSource, $shareType, $shareWith, |
|
| 1212 | + \OC_User::getUser(), self::FORMAT_NONE, null, 1, false)) { |
|
| 1213 | + // Check if this item is a reshare and verify that the permissions |
|
| 1214 | + // granted don't exceed the parent shared item |
|
| 1215 | + if (isset($rootItem['parent'])) { |
|
| 1216 | + $qb = $connection->getQueryBuilder(); |
|
| 1217 | + $qb->select('permissions') |
|
| 1218 | + ->from('share') |
|
| 1219 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1220 | + ->setParameter(':id', $rootItem['parent']); |
|
| 1221 | + $dbresult = $qb->execute(); |
|
| 1222 | + |
|
| 1223 | + $result = $dbresult->fetch(); |
|
| 1224 | + $dbresult->closeCursor(); |
|
| 1225 | + if (~(int)$result['permissions'] & $permissions) { |
|
| 1226 | + $message = 'Setting permissions for %s failed,' |
|
| 1227 | + .' because the permissions exceed permissions granted to %s'; |
|
| 1228 | + $message_t = $l->t('Setting permissions for %s failed, because the permissions exceed permissions granted to %s', array($itemSource, \OC_User::getUser())); |
|
| 1229 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, \OC_User::getUser()), \OCP\Util::DEBUG); |
|
| 1230 | + throw new \Exception($message_t); |
|
| 1231 | + } |
|
| 1232 | + } |
|
| 1233 | + $qb = $connection->getQueryBuilder(); |
|
| 1234 | + $qb->update('share') |
|
| 1235 | + ->set('permissions', $qb->createParameter('permissions')) |
|
| 1236 | + ->where($qb->expr()->eq('id', $qb->createParameter('id'))) |
|
| 1237 | + ->setParameter(':id', $rootItem['id']) |
|
| 1238 | + ->setParameter(':permissions', $permissions); |
|
| 1239 | + $qb->execute(); |
|
| 1240 | + if ($itemType === 'file' || $itemType === 'folder') { |
|
| 1241 | + \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( |
|
| 1242 | + 'itemType' => $itemType, |
|
| 1243 | + 'itemSource' => $itemSource, |
|
| 1244 | + 'shareType' => $shareType, |
|
| 1245 | + 'shareWith' => $shareWith, |
|
| 1246 | + 'uidOwner' => \OC_User::getUser(), |
|
| 1247 | + 'permissions' => $permissions, |
|
| 1248 | + 'path' => $rootItem['path'], |
|
| 1249 | + 'share' => $rootItem |
|
| 1250 | + )); |
|
| 1251 | + } |
|
| 1252 | + |
|
| 1253 | + // Share id's to update with the new permissions |
|
| 1254 | + $ids = []; |
|
| 1255 | + $items = []; |
|
| 1256 | + |
|
| 1257 | + // Check if permissions were removed |
|
| 1258 | + if ((int)$rootItem['permissions'] & ~$permissions) { |
|
| 1259 | + // If share permission is removed all reshares must be deleted |
|
| 1260 | + if (($rootItem['permissions'] & \OCP\Constants::PERMISSION_SHARE) && (~$permissions & \OCP\Constants::PERMISSION_SHARE)) { |
|
| 1261 | + // delete all shares, keep parent and group children |
|
| 1262 | + Helper::delete($rootItem['id'], true, null, null, true); |
|
| 1263 | + } |
|
| 1264 | + |
|
| 1265 | + // Remove permission from all children |
|
| 1266 | + $parents = [$rootItem['id']]; |
|
| 1267 | + while (!empty($parents)) { |
|
| 1268 | + $parents = $intArrayToLiteralArray($parents, $qb->expr()); |
|
| 1269 | + $qb = $connection->getQueryBuilder(); |
|
| 1270 | + $qb->select('id', 'permissions', 'item_type') |
|
| 1271 | + ->from('share') |
|
| 1272 | + ->where($qb->expr()->in('parent', $parents)); |
|
| 1273 | + $result = $qb->execute(); |
|
| 1274 | + // Reset parents array, only go through loop again if |
|
| 1275 | + // items are found that need permissions removed |
|
| 1276 | + $parents = []; |
|
| 1277 | + while ($item = $result->fetch()) { |
|
| 1278 | + $item = $sanitizeItem($item); |
|
| 1279 | + |
|
| 1280 | + $items[] = $item; |
|
| 1281 | + // Check if permissions need to be removed |
|
| 1282 | + if ($item['permissions'] & ~$permissions) { |
|
| 1283 | + // Add to list of items that need permissions removed |
|
| 1284 | + $ids[] = $item['id']; |
|
| 1285 | + $parents[] = $item['id']; |
|
| 1286 | + } |
|
| 1287 | + } |
|
| 1288 | + $result->closeCursor(); |
|
| 1289 | + } |
|
| 1290 | + |
|
| 1291 | + // Remove the permissions for all reshares of this item |
|
| 1292 | + if (!empty($ids)) { |
|
| 1293 | + $ids = "'".implode("','", $ids)."'"; |
|
| 1294 | + // TODO this should be done with Doctrine platform objects |
|
| 1295 | + if (\OC::$server->getConfig()->getSystemValue("dbtype") === 'oci') { |
|
| 1296 | + $andOp = 'BITAND(`permissions`, ?)'; |
|
| 1297 | + } else { |
|
| 1298 | + $andOp = '`permissions` & ?'; |
|
| 1299 | + } |
|
| 1300 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `permissions` = '.$andOp |
|
| 1301 | + .' WHERE `id` IN ('.$ids.')'); |
|
| 1302 | + $query->execute(array($permissions)); |
|
| 1303 | + } |
|
| 1304 | + |
|
| 1305 | + } |
|
| 1306 | + |
|
| 1307 | + /* |
|
| 1308 | 1308 | * Permissions were added |
| 1309 | 1309 | * Update all USERGROUP shares. (So group shares where the user moved their mountpoint). |
| 1310 | 1310 | */ |
| 1311 | - if ($permissions & ~(int)$rootItem['permissions']) { |
|
| 1312 | - $qb = $connection->getQueryBuilder(); |
|
| 1313 | - $qb->select('id', 'permissions', 'item_type') |
|
| 1314 | - ->from('share') |
|
| 1315 | - ->where($qb->expr()->eq('parent', $qb->createParameter('parent'))) |
|
| 1316 | - ->andWhere($qb->expr()->eq('share_type', $qb->createParameter('share_type'))) |
|
| 1317 | - ->andWhere($qb->expr()->neq('permissions', $qb->createParameter('shareDeleted'))) |
|
| 1318 | - ->setParameter(':parent', (int)$rootItem['id']) |
|
| 1319 | - ->setParameter(':share_type', 2) |
|
| 1320 | - ->setParameter(':shareDeleted', 0); |
|
| 1321 | - $result = $qb->execute(); |
|
| 1322 | - |
|
| 1323 | - $ids = []; |
|
| 1324 | - while ($item = $result->fetch()) { |
|
| 1325 | - $item = $sanitizeItem($item); |
|
| 1326 | - $items[] = $item; |
|
| 1327 | - $ids[] = $item['id']; |
|
| 1328 | - } |
|
| 1329 | - $result->closeCursor(); |
|
| 1330 | - |
|
| 1331 | - // Add permssions for all USERGROUP shares of this item |
|
| 1332 | - if (!empty($ids)) { |
|
| 1333 | - $ids = $intArrayToLiteralArray($ids, $qb->expr()); |
|
| 1334 | - |
|
| 1335 | - $qb = $connection->getQueryBuilder(); |
|
| 1336 | - $qb->update('share') |
|
| 1337 | - ->set('permissions', $qb->createParameter('permissions')) |
|
| 1338 | - ->where($qb->expr()->in('id', $ids)) |
|
| 1339 | - ->setParameter(':permissions', $permissions); |
|
| 1340 | - $qb->execute(); |
|
| 1341 | - } |
|
| 1342 | - } |
|
| 1343 | - |
|
| 1344 | - foreach ($items as $item) { |
|
| 1345 | - \OC_Hook::emit('OCP\Share', 'post_update_permissions', ['share' => $item]); |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - return true; |
|
| 1349 | - } |
|
| 1350 | - $message = 'Setting permissions for %s failed, because the item was not found'; |
|
| 1351 | - $message_t = $l->t('Setting permissions for %s failed, because the item was not found', array($itemSource)); |
|
| 1352 | - |
|
| 1353 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 1354 | - throw new \Exception($message_t); |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - /** |
|
| 1358 | - * validate expiration date if it meets all constraints |
|
| 1359 | - * |
|
| 1360 | - * @param string $expireDate well formatted date string, e.g. "DD-MM-YYYY" |
|
| 1361 | - * @param string $shareTime timestamp when the file was shared |
|
| 1362 | - * @param string $itemType |
|
| 1363 | - * @param string $itemSource |
|
| 1364 | - * @return \DateTime validated date |
|
| 1365 | - * @throws \Exception when the expire date is in the past or further in the future then the enforced date |
|
| 1366 | - */ |
|
| 1367 | - private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) { |
|
| 1368 | - $l = \OC::$server->getL10N('lib'); |
|
| 1369 | - $date = new \DateTime($expireDate); |
|
| 1370 | - $today = new \DateTime('now'); |
|
| 1371 | - |
|
| 1372 | - // if the user doesn't provide a share time we need to get it from the database |
|
| 1373 | - // fall-back mode to keep API stable, because the $shareTime parameter was added later |
|
| 1374 | - $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced(); |
|
| 1375 | - if ($defaultExpireDateEnforced && $shareTime === null) { |
|
| 1376 | - $items = self::getItemShared($itemType, $itemSource); |
|
| 1377 | - $firstItem = reset($items); |
|
| 1378 | - $shareTime = (int)$firstItem['stime']; |
|
| 1379 | - } |
|
| 1380 | - |
|
| 1381 | - if ($defaultExpireDateEnforced) { |
|
| 1382 | - // initialize max date with share time |
|
| 1383 | - $maxDate = new \DateTime(); |
|
| 1384 | - $maxDate->setTimestamp($shareTime); |
|
| 1385 | - $maxDays = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1386 | - $maxDate->add(new \DateInterval('P' . $maxDays . 'D')); |
|
| 1387 | - if ($date > $maxDate) { |
|
| 1388 | - $warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared'; |
|
| 1389 | - $warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays)); |
|
| 1390 | - \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1391 | - throw new \Exception($warning_t); |
|
| 1392 | - } |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - if ($date < $today) { |
|
| 1396 | - $message = 'Cannot set expiration date. Expiration date is in the past'; |
|
| 1397 | - $message_t = $l->t('Cannot set expiration date. Expiration date is in the past'); |
|
| 1398 | - \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::WARN); |
|
| 1399 | - throw new \Exception($message_t); |
|
| 1400 | - } |
|
| 1401 | - |
|
| 1402 | - return $date; |
|
| 1403 | - } |
|
| 1404 | - |
|
| 1405 | - /** |
|
| 1406 | - * Set expiration date for a share |
|
| 1407 | - * @param string $itemType |
|
| 1408 | - * @param string $itemSource |
|
| 1409 | - * @param string $date expiration date |
|
| 1410 | - * @param int $shareTime timestamp from when the file was shared |
|
| 1411 | - * @return boolean |
|
| 1412 | - * @throws \Exception when the expire date is not set, in the past or further in the future then the enforced date |
|
| 1413 | - */ |
|
| 1414 | - public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) { |
|
| 1415 | - $user = \OC_User::getUser(); |
|
| 1416 | - $l = \OC::$server->getL10N('lib'); |
|
| 1417 | - |
|
| 1418 | - if ($date == '') { |
|
| 1419 | - if (\OCP\Util::isDefaultExpireDateEnforced()) { |
|
| 1420 | - $warning = 'Cannot clear expiration date. Shares are required to have an expiration date.'; |
|
| 1421 | - $warning_t = $l->t('Cannot clear expiration date. Shares are required to have an expiration date.'); |
|
| 1422 | - \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1423 | - throw new \Exception($warning_t); |
|
| 1424 | - } else { |
|
| 1425 | - $date = null; |
|
| 1426 | - } |
|
| 1427 | - } else { |
|
| 1428 | - $date = self::validateExpireDate($date, $shareTime, $itemType, $itemSource); |
|
| 1429 | - } |
|
| 1430 | - $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?'); |
|
| 1431 | - $query->bindValue(1, $date, 'datetime'); |
|
| 1432 | - $query->bindValue(2, $itemType); |
|
| 1433 | - $query->bindValue(3, $itemSource); |
|
| 1434 | - $query->bindValue(4, $user); |
|
| 1435 | - $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK); |
|
| 1436 | - |
|
| 1437 | - $query->execute(); |
|
| 1438 | - |
|
| 1439 | - \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array( |
|
| 1440 | - 'itemType' => $itemType, |
|
| 1441 | - 'itemSource' => $itemSource, |
|
| 1442 | - 'date' => $date, |
|
| 1443 | - 'uidOwner' => $user |
|
| 1444 | - )); |
|
| 1445 | - |
|
| 1446 | - return true; |
|
| 1447 | - } |
|
| 1448 | - |
|
| 1449 | - /** |
|
| 1450 | - * Retrieve the owner of a connection |
|
| 1451 | - * |
|
| 1452 | - * @param IDBConnection $connection |
|
| 1453 | - * @param int $shareId |
|
| 1454 | - * @throws \Exception |
|
| 1455 | - * @return string uid of share owner |
|
| 1456 | - */ |
|
| 1457 | - private static function getShareOwner(IDBConnection $connection, $shareId) { |
|
| 1458 | - $qb = $connection->getQueryBuilder(); |
|
| 1459 | - |
|
| 1460 | - $qb->select('uid_owner') |
|
| 1461 | - ->from('share') |
|
| 1462 | - ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1463 | - ->setParameter(':shareId', $shareId); |
|
| 1464 | - $result = $qb->execute(); |
|
| 1465 | - $result = $result->fetch(); |
|
| 1466 | - |
|
| 1467 | - if (empty($result)) { |
|
| 1468 | - throw new \Exception('Share not found'); |
|
| 1469 | - } |
|
| 1470 | - |
|
| 1471 | - return $result['uid_owner']; |
|
| 1472 | - } |
|
| 1473 | - |
|
| 1474 | - /** |
|
| 1475 | - * Set password for a public link share |
|
| 1476 | - * |
|
| 1477 | - * @param IUserSession $userSession |
|
| 1478 | - * @param IDBConnection $connection |
|
| 1479 | - * @param IConfig $config |
|
| 1480 | - * @param int $shareId |
|
| 1481 | - * @param string $password |
|
| 1482 | - * @throws \Exception |
|
| 1483 | - * @return boolean |
|
| 1484 | - */ |
|
| 1485 | - public static function setPassword(IUserSession $userSession, |
|
| 1486 | - IDBConnection $connection, |
|
| 1487 | - IConfig $config, |
|
| 1488 | - $shareId, $password) { |
|
| 1489 | - $user = $userSession->getUser(); |
|
| 1490 | - if (is_null($user)) { |
|
| 1491 | - throw new \Exception("User not logged in"); |
|
| 1492 | - } |
|
| 1493 | - |
|
| 1494 | - $uid = self::getShareOwner($connection, $shareId); |
|
| 1495 | - |
|
| 1496 | - if ($uid !== $user->getUID()) { |
|
| 1497 | - throw new \Exception('Cannot update share of a different user'); |
|
| 1498 | - } |
|
| 1499 | - |
|
| 1500 | - if ($password === '') { |
|
| 1501 | - $password = null; |
|
| 1502 | - } |
|
| 1503 | - |
|
| 1504 | - //If passwords are enforced the password can't be null |
|
| 1505 | - if (self::enforcePassword($config) && is_null($password)) { |
|
| 1506 | - throw new \Exception('Cannot remove password'); |
|
| 1507 | - } |
|
| 1508 | - |
|
| 1509 | - self::verifyPassword($password); |
|
| 1510 | - |
|
| 1511 | - $qb = $connection->getQueryBuilder(); |
|
| 1512 | - $qb->update('share') |
|
| 1513 | - ->set('share_with', $qb->createParameter('pass')) |
|
| 1514 | - ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1515 | - ->setParameter(':pass', is_null($password) ? null : \OC::$server->getHasher()->hash($password)) |
|
| 1516 | - ->setParameter(':shareId', $shareId); |
|
| 1517 | - |
|
| 1518 | - $qb->execute(); |
|
| 1519 | - |
|
| 1520 | - return true; |
|
| 1521 | - } |
|
| 1522 | - |
|
| 1523 | - /** |
|
| 1524 | - * Checks whether a share has expired, calls unshareItem() if yes. |
|
| 1525 | - * @param array $item Share data (usually database row) |
|
| 1526 | - * @return boolean True if item was expired, false otherwise. |
|
| 1527 | - */ |
|
| 1528 | - protected static function expireItem(array $item) { |
|
| 1529 | - |
|
| 1530 | - $result = false; |
|
| 1531 | - |
|
| 1532 | - // only use default expiration date for link shares |
|
| 1533 | - if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 1534 | - |
|
| 1535 | - // calculate expiration date |
|
| 1536 | - if (!empty($item['expiration'])) { |
|
| 1537 | - $userDefinedExpire = new \DateTime($item['expiration']); |
|
| 1538 | - $expires = $userDefinedExpire->getTimestamp(); |
|
| 1539 | - } else { |
|
| 1540 | - $expires = null; |
|
| 1541 | - } |
|
| 1542 | - |
|
| 1543 | - |
|
| 1544 | - // get default expiration settings |
|
| 1545 | - $defaultSettings = Helper::getDefaultExpireSetting(); |
|
| 1546 | - $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); |
|
| 1547 | - |
|
| 1548 | - |
|
| 1549 | - if (is_int($expires)) { |
|
| 1550 | - $now = time(); |
|
| 1551 | - if ($now > $expires) { |
|
| 1552 | - self::unshareItem($item); |
|
| 1553 | - $result = true; |
|
| 1554 | - } |
|
| 1555 | - } |
|
| 1556 | - } |
|
| 1557 | - return $result; |
|
| 1558 | - } |
|
| 1559 | - |
|
| 1560 | - /** |
|
| 1561 | - * Unshares a share given a share data array |
|
| 1562 | - * @param array $item Share data (usually database row) |
|
| 1563 | - * @param int $newParent parent ID |
|
| 1564 | - * @return null |
|
| 1565 | - */ |
|
| 1566 | - protected static function unshareItem(array $item, $newParent = null) { |
|
| 1567 | - |
|
| 1568 | - $shareType = (int)$item['share_type']; |
|
| 1569 | - $shareWith = null; |
|
| 1570 | - if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1571 | - $shareWith = $item['share_with']; |
|
| 1572 | - } |
|
| 1573 | - |
|
| 1574 | - // Pass all the vars we have for now, they may be useful |
|
| 1575 | - $hookParams = array( |
|
| 1576 | - 'id' => $item['id'], |
|
| 1577 | - 'itemType' => $item['item_type'], |
|
| 1578 | - 'itemSource' => $item['item_source'], |
|
| 1579 | - 'shareType' => $shareType, |
|
| 1580 | - 'shareWith' => $shareWith, |
|
| 1581 | - 'itemParent' => $item['parent'], |
|
| 1582 | - 'uidOwner' => $item['uid_owner'], |
|
| 1583 | - ); |
|
| 1584 | - if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 1585 | - $hookParams['fileSource'] = $item['file_source']; |
|
| 1586 | - $hookParams['fileTarget'] = $item['file_target']; |
|
| 1587 | - } |
|
| 1588 | - |
|
| 1589 | - \OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams); |
|
| 1590 | - $deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
| 1591 | - $deletedShares[] = $hookParams; |
|
| 1592 | - $hookParams['deletedShares'] = $deletedShares; |
|
| 1593 | - \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams); |
|
| 1594 | - if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 1595 | - list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
| 1596 | - self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
| 1597 | - } |
|
| 1598 | - } |
|
| 1599 | - |
|
| 1600 | - /** |
|
| 1601 | - * Get the backend class for the specified item type |
|
| 1602 | - * @param string $itemType |
|
| 1603 | - * @throws \Exception |
|
| 1604 | - * @return \OCP\Share_Backend |
|
| 1605 | - */ |
|
| 1606 | - public static function getBackend($itemType) { |
|
| 1607 | - $l = \OC::$server->getL10N('lib'); |
|
| 1608 | - if (isset(self::$backends[$itemType])) { |
|
| 1609 | - return self::$backends[$itemType]; |
|
| 1610 | - } else if (isset(self::$backendTypes[$itemType]['class'])) { |
|
| 1611 | - $class = self::$backendTypes[$itemType]['class']; |
|
| 1612 | - if (class_exists($class)) { |
|
| 1613 | - self::$backends[$itemType] = new $class; |
|
| 1614 | - if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) { |
|
| 1615 | - $message = 'Sharing backend %s must implement the interface OCP\Share_Backend'; |
|
| 1616 | - $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class)); |
|
| 1617 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1618 | - throw new \Exception($message_t); |
|
| 1619 | - } |
|
| 1620 | - return self::$backends[$itemType]; |
|
| 1621 | - } else { |
|
| 1622 | - $message = 'Sharing backend %s not found'; |
|
| 1623 | - $message_t = $l->t('Sharing backend %s not found', array($class)); |
|
| 1624 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1625 | - throw new \Exception($message_t); |
|
| 1626 | - } |
|
| 1627 | - } |
|
| 1628 | - $message = 'Sharing backend for %s not found'; |
|
| 1629 | - $message_t = $l->t('Sharing backend for %s not found', array($itemType)); |
|
| 1630 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), \OCP\Util::ERROR); |
|
| 1631 | - throw new \Exception($message_t); |
|
| 1632 | - } |
|
| 1633 | - |
|
| 1634 | - /** |
|
| 1635 | - * Check if resharing is allowed |
|
| 1636 | - * @return boolean true if allowed or false |
|
| 1637 | - * |
|
| 1638 | - * Resharing is allowed by default if not configured |
|
| 1639 | - */ |
|
| 1640 | - public static function isResharingAllowed() { |
|
| 1641 | - if (!isset(self::$isResharingAllowed)) { |
|
| 1642 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') { |
|
| 1643 | - self::$isResharingAllowed = true; |
|
| 1644 | - } else { |
|
| 1645 | - self::$isResharingAllowed = false; |
|
| 1646 | - } |
|
| 1647 | - } |
|
| 1648 | - return self::$isResharingAllowed; |
|
| 1649 | - } |
|
| 1650 | - |
|
| 1651 | - /** |
|
| 1652 | - * Get a list of collection item types for the specified item type |
|
| 1653 | - * @param string $itemType |
|
| 1654 | - * @return array |
|
| 1655 | - */ |
|
| 1656 | - private static function getCollectionItemTypes($itemType) { |
|
| 1657 | - $collectionTypes = array($itemType); |
|
| 1658 | - foreach (self::$backendTypes as $type => $backend) { |
|
| 1659 | - if (in_array($backend['collectionOf'], $collectionTypes)) { |
|
| 1660 | - $collectionTypes[] = $type; |
|
| 1661 | - } |
|
| 1662 | - } |
|
| 1663 | - // TODO Add option for collections to be collection of themselves, only 'folder' does it now... |
|
| 1664 | - if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) { |
|
| 1665 | - unset($collectionTypes[0]); |
|
| 1666 | - } |
|
| 1667 | - // Return array if collections were found or the item type is a |
|
| 1668 | - // collection itself - collections can be inside collections |
|
| 1669 | - if (count($collectionTypes) > 0) { |
|
| 1670 | - return $collectionTypes; |
|
| 1671 | - } |
|
| 1672 | - return false; |
|
| 1673 | - } |
|
| 1674 | - |
|
| 1675 | - /** |
|
| 1676 | - * Get the owners of items shared with a user. |
|
| 1677 | - * |
|
| 1678 | - * @param string $user The user the items are shared with. |
|
| 1679 | - * @param string $type The type of the items shared with the user. |
|
| 1680 | - * @param boolean $includeCollections Include collection item types (optional) |
|
| 1681 | - * @param boolean $includeOwner include owner in the list of users the item is shared with (optional) |
|
| 1682 | - * @return array |
|
| 1683 | - */ |
|
| 1684 | - public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) { |
|
| 1685 | - // First, we find out if $type is part of a collection (and if that collection is part of |
|
| 1686 | - // another one and so on). |
|
| 1687 | - $collectionTypes = array(); |
|
| 1688 | - if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) { |
|
| 1689 | - $collectionTypes[] = $type; |
|
| 1690 | - } |
|
| 1691 | - |
|
| 1692 | - // Of these collection types, along with our original $type, we make a |
|
| 1693 | - // list of the ones for which a sharing backend has been registered. |
|
| 1694 | - // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it |
|
| 1695 | - // with its $includeCollections parameter set to true. Unfortunately, this fails currently. |
|
| 1696 | - $allMaybeSharedItems = array(); |
|
| 1697 | - foreach ($collectionTypes as $collectionType) { |
|
| 1698 | - if (isset(self::$backends[$collectionType])) { |
|
| 1699 | - $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser( |
|
| 1700 | - $collectionType, |
|
| 1701 | - $user, |
|
| 1702 | - self::FORMAT_NONE |
|
| 1703 | - ); |
|
| 1704 | - } |
|
| 1705 | - } |
|
| 1706 | - |
|
| 1707 | - $owners = array(); |
|
| 1708 | - if ($includeOwner) { |
|
| 1709 | - $owners[] = $user; |
|
| 1710 | - } |
|
| 1711 | - |
|
| 1712 | - // We take a look at all shared items of the given $type (or of the collections it is part of) |
|
| 1713 | - // and find out their owners. Then, we gather the tags for the original $type from all owners, |
|
| 1714 | - // and return them as elements of a list that look like "Tag (owner)". |
|
| 1715 | - foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) { |
|
| 1716 | - foreach ($maybeSharedItems as $sharedItem) { |
|
| 1717 | - if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814 |
|
| 1718 | - $owners[] = $sharedItem['uid_owner']; |
|
| 1719 | - } |
|
| 1720 | - } |
|
| 1721 | - } |
|
| 1722 | - |
|
| 1723 | - return $owners; |
|
| 1724 | - } |
|
| 1725 | - |
|
| 1726 | - /** |
|
| 1727 | - * Get shared items from the database |
|
| 1728 | - * @param string $itemType |
|
| 1729 | - * @param string $item Item source or target (optional) |
|
| 1730 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique |
|
| 1731 | - * @param string $shareWith User or group the item is being shared with |
|
| 1732 | - * @param string $uidOwner User that is the owner of shared items (optional) |
|
| 1733 | - * @param int $format Format to convert items to with formatItems() (optional) |
|
| 1734 | - * @param mixed $parameters to pass to formatItems() (optional) |
|
| 1735 | - * @param int $limit Number of items to return, -1 to return all matches (optional) |
|
| 1736 | - * @param boolean $includeCollections Include collection item types (optional) |
|
| 1737 | - * @param boolean $itemShareWithBySource (optional) |
|
| 1738 | - * @param boolean $checkExpireDate |
|
| 1739 | - * @return array |
|
| 1740 | - * |
|
| 1741 | - * See public functions getItem(s)... for parameter usage |
|
| 1742 | - * |
|
| 1743 | - */ |
|
| 1744 | - public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, |
|
| 1745 | - $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, |
|
| 1746 | - $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) { |
|
| 1747 | - if (!self::isEnabled()) { |
|
| 1748 | - return array(); |
|
| 1749 | - } |
|
| 1750 | - $backend = self::getBackend($itemType); |
|
| 1751 | - $collectionTypes = false; |
|
| 1752 | - // Get filesystem root to add it to the file target and remove from the |
|
| 1753 | - // file source, match file_source with the file cache |
|
| 1754 | - if ($itemType == 'file' || $itemType == 'folder') { |
|
| 1755 | - if(!is_null($uidOwner)) { |
|
| 1756 | - $root = \OC\Files\Filesystem::getRoot(); |
|
| 1757 | - } else { |
|
| 1758 | - $root = ''; |
|
| 1759 | - } |
|
| 1760 | - $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 1761 | - if (!isset($item)) { |
|
| 1762 | - $where .= ' AND `file_target` IS NOT NULL '; |
|
| 1763 | - } |
|
| 1764 | - $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 1765 | - $fileDependent = true; |
|
| 1766 | - $queryArgs = array(); |
|
| 1767 | - } else { |
|
| 1768 | - $fileDependent = false; |
|
| 1769 | - $root = ''; |
|
| 1770 | - $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1771 | - if ($includeCollections && !isset($item) && $collectionTypes) { |
|
| 1772 | - // If includeCollections is true, find collections of this item type, e.g. a music album contains songs |
|
| 1773 | - if (!in_array($itemType, $collectionTypes)) { |
|
| 1774 | - $itemTypes = array_merge(array($itemType), $collectionTypes); |
|
| 1775 | - } else { |
|
| 1776 | - $itemTypes = $collectionTypes; |
|
| 1777 | - } |
|
| 1778 | - $placeholders = join(',', array_fill(0, count($itemTypes), '?')); |
|
| 1779 | - $where = ' WHERE `item_type` IN ('.$placeholders.'))'; |
|
| 1780 | - $queryArgs = $itemTypes; |
|
| 1781 | - } else { |
|
| 1782 | - $where = ' WHERE `item_type` = ?'; |
|
| 1783 | - $queryArgs = array($itemType); |
|
| 1784 | - } |
|
| 1785 | - } |
|
| 1786 | - if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 1787 | - $where .= ' AND `share_type` != ?'; |
|
| 1788 | - $queryArgs[] = self::SHARE_TYPE_LINK; |
|
| 1789 | - } |
|
| 1790 | - if (isset($shareType)) { |
|
| 1791 | - // Include all user and group items |
|
| 1792 | - if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) { |
|
| 1793 | - $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) '; |
|
| 1794 | - $queryArgs[] = self::SHARE_TYPE_USER; |
|
| 1795 | - $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1796 | - $queryArgs[] = $shareWith; |
|
| 1797 | - |
|
| 1798 | - $user = \OC::$server->getUserManager()->get($shareWith); |
|
| 1799 | - $groups = []; |
|
| 1800 | - if ($user) { |
|
| 1801 | - $groups = \OC::$server->getGroupManager()->getUserGroupIds($user); |
|
| 1802 | - } |
|
| 1803 | - if (!empty($groups)) { |
|
| 1804 | - $placeholders = join(',', array_fill(0, count($groups), '?')); |
|
| 1805 | - $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; |
|
| 1806 | - $queryArgs[] = self::SHARE_TYPE_GROUP; |
|
| 1807 | - $queryArgs = array_merge($queryArgs, $groups); |
|
| 1808 | - } |
|
| 1809 | - $where .= ')'; |
|
| 1810 | - // Don't include own group shares |
|
| 1811 | - $where .= ' AND `uid_owner` != ?'; |
|
| 1812 | - $queryArgs[] = $shareWith; |
|
| 1813 | - } else { |
|
| 1814 | - $where .= ' AND `share_type` = ?'; |
|
| 1815 | - $queryArgs[] = $shareType; |
|
| 1816 | - if (isset($shareWith)) { |
|
| 1817 | - $where .= ' AND `share_with` = ?'; |
|
| 1818 | - $queryArgs[] = $shareWith; |
|
| 1819 | - } |
|
| 1820 | - } |
|
| 1821 | - } |
|
| 1822 | - if (isset($uidOwner)) { |
|
| 1823 | - $where .= ' AND `uid_owner` = ?'; |
|
| 1824 | - $queryArgs[] = $uidOwner; |
|
| 1825 | - if (!isset($shareType)) { |
|
| 1826 | - // Prevent unique user targets for group shares from being selected |
|
| 1827 | - $where .= ' AND `share_type` != ?'; |
|
| 1828 | - $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1829 | - } |
|
| 1830 | - if ($fileDependent) { |
|
| 1831 | - $column = 'file_source'; |
|
| 1832 | - } else { |
|
| 1833 | - $column = 'item_source'; |
|
| 1834 | - } |
|
| 1835 | - } else { |
|
| 1836 | - if ($fileDependent) { |
|
| 1837 | - $column = 'file_target'; |
|
| 1838 | - } else { |
|
| 1839 | - $column = 'item_target'; |
|
| 1840 | - } |
|
| 1841 | - } |
|
| 1842 | - if (isset($item)) { |
|
| 1843 | - $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1844 | - if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1845 | - $where .= ' AND ('; |
|
| 1846 | - } else { |
|
| 1847 | - $where .= ' AND'; |
|
| 1848 | - } |
|
| 1849 | - // If looking for own shared items, check item_source else check item_target |
|
| 1850 | - if (isset($uidOwner) || $itemShareWithBySource) { |
|
| 1851 | - // If item type is a file, file source needs to be checked in case the item was converted |
|
| 1852 | - if ($fileDependent) { |
|
| 1853 | - $where .= ' `file_source` = ?'; |
|
| 1854 | - $column = 'file_source'; |
|
| 1855 | - } else { |
|
| 1856 | - $where .= ' `item_source` = ?'; |
|
| 1857 | - $column = 'item_source'; |
|
| 1858 | - } |
|
| 1859 | - } else { |
|
| 1860 | - if ($fileDependent) { |
|
| 1861 | - $where .= ' `file_target` = ?'; |
|
| 1862 | - $item = \OC\Files\Filesystem::normalizePath($item); |
|
| 1863 | - } else { |
|
| 1864 | - $where .= ' `item_target` = ?'; |
|
| 1865 | - } |
|
| 1866 | - } |
|
| 1867 | - $queryArgs[] = $item; |
|
| 1868 | - if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1869 | - $placeholders = join(',', array_fill(0, count($collectionTypes), '?')); |
|
| 1870 | - $where .= ' OR `item_type` IN ('.$placeholders.'))'; |
|
| 1871 | - $queryArgs = array_merge($queryArgs, $collectionTypes); |
|
| 1872 | - } |
|
| 1873 | - } |
|
| 1874 | - |
|
| 1875 | - if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) { |
|
| 1876 | - // Make sure the unique user target is returned if it exists, |
|
| 1877 | - // unique targets should follow the group share in the database |
|
| 1878 | - // If the limit is not 1, the filtering can be done later |
|
| 1879 | - $where .= ' ORDER BY `*PREFIX*share`.`id` DESC'; |
|
| 1880 | - } else { |
|
| 1881 | - $where .= ' ORDER BY `*PREFIX*share`.`id` ASC'; |
|
| 1882 | - } |
|
| 1883 | - |
|
| 1884 | - if ($limit != -1 && !$includeCollections) { |
|
| 1885 | - // The limit must be at least 3, because filtering needs to be done |
|
| 1886 | - if ($limit < 3) { |
|
| 1887 | - $queryLimit = 3; |
|
| 1888 | - } else { |
|
| 1889 | - $queryLimit = $limit; |
|
| 1890 | - } |
|
| 1891 | - } else { |
|
| 1892 | - $queryLimit = null; |
|
| 1893 | - } |
|
| 1894 | - $select = self::createSelectStatement($format, $fileDependent, $uidOwner); |
|
| 1895 | - $root = strlen($root); |
|
| 1896 | - $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit); |
|
| 1897 | - $result = $query->execute($queryArgs); |
|
| 1898 | - if ($result === false) { |
|
| 1899 | - \OCP\Util::writeLog('OCP\Share', |
|
| 1900 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 1901 | - \OCP\Util::ERROR); |
|
| 1902 | - } |
|
| 1903 | - $items = array(); |
|
| 1904 | - $targets = array(); |
|
| 1905 | - $switchedItems = array(); |
|
| 1906 | - $mounts = array(); |
|
| 1907 | - while ($row = $result->fetchRow()) { |
|
| 1908 | - self::transformDBResults($row); |
|
| 1909 | - // Filter out duplicate group shares for users with unique targets |
|
| 1910 | - if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 1911 | - continue; |
|
| 1912 | - } |
|
| 1913 | - if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { |
|
| 1914 | - $row['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1915 | - $row['unique_name'] = true; // remember that we use a unique name for this user |
|
| 1916 | - $row['share_with'] = $items[$row['parent']]['share_with']; |
|
| 1917 | - // if the group share was unshared from the user we keep the permission, otherwise |
|
| 1918 | - // we take the permission from the parent because this is always the up-to-date |
|
| 1919 | - // permission for the group share |
|
| 1920 | - if ($row['permissions'] > 0) { |
|
| 1921 | - $row['permissions'] = $items[$row['parent']]['permissions']; |
|
| 1922 | - } |
|
| 1923 | - // Remove the parent group share |
|
| 1924 | - unset($items[$row['parent']]); |
|
| 1925 | - if ($row['permissions'] == 0) { |
|
| 1926 | - continue; |
|
| 1927 | - } |
|
| 1928 | - } else if (!isset($uidOwner)) { |
|
| 1929 | - // Check if the same target already exists |
|
| 1930 | - if (isset($targets[$row['id']])) { |
|
| 1931 | - // Check if the same owner shared with the user twice |
|
| 1932 | - // through a group and user share - this is allowed |
|
| 1933 | - $id = $targets[$row['id']]; |
|
| 1934 | - if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) { |
|
| 1935 | - // Switch to group share type to ensure resharing conditions aren't bypassed |
|
| 1936 | - if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { |
|
| 1937 | - $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1938 | - $items[$id]['share_with'] = $row['share_with']; |
|
| 1939 | - } |
|
| 1940 | - // Switch ids if sharing permission is granted on only |
|
| 1941 | - // one share to ensure correct parent is used if resharing |
|
| 1942 | - if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 1943 | - && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1944 | - $items[$row['id']] = $items[$id]; |
|
| 1945 | - $switchedItems[$id] = $row['id']; |
|
| 1946 | - unset($items[$id]); |
|
| 1947 | - $id = $row['id']; |
|
| 1948 | - } |
|
| 1949 | - $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 1950 | - |
|
| 1951 | - } |
|
| 1952 | - continue; |
|
| 1953 | - } elseif (!empty($row['parent'])) { |
|
| 1954 | - $targets[$row['parent']] = $row['id']; |
|
| 1955 | - } |
|
| 1956 | - } |
|
| 1957 | - // Remove root from file source paths if retrieving own shared items |
|
| 1958 | - if (isset($uidOwner) && isset($row['path'])) { |
|
| 1959 | - if (isset($row['parent'])) { |
|
| 1960 | - $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
|
| 1961 | - $parentResult = $query->execute(array($row['parent'])); |
|
| 1962 | - if ($result === false) { |
|
| 1963 | - \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 1964 | - \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 1965 | - \OCP\Util::ERROR); |
|
| 1966 | - } else { |
|
| 1967 | - $parentRow = $parentResult->fetchRow(); |
|
| 1968 | - $tmpPath = $parentRow['file_target']; |
|
| 1969 | - // find the right position where the row path continues from the target path |
|
| 1970 | - $pos = strrpos($row['path'], $parentRow['file_target']); |
|
| 1971 | - $subPath = substr($row['path'], $pos); |
|
| 1972 | - $splitPath = explode('/', $subPath); |
|
| 1973 | - foreach (array_slice($splitPath, 2) as $pathPart) { |
|
| 1974 | - $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 1975 | - } |
|
| 1976 | - $row['path'] = $tmpPath; |
|
| 1977 | - } |
|
| 1978 | - } else { |
|
| 1979 | - if (!isset($mounts[$row['storage']])) { |
|
| 1980 | - $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']); |
|
| 1981 | - if (is_array($mountPoints) && !empty($mountPoints)) { |
|
| 1982 | - $mounts[$row['storage']] = current($mountPoints); |
|
| 1983 | - } |
|
| 1984 | - } |
|
| 1985 | - if (!empty($mounts[$row['storage']])) { |
|
| 1986 | - $path = $mounts[$row['storage']]->getMountPoint().$row['path']; |
|
| 1987 | - $relPath = substr($path, $root); // path relative to data/user |
|
| 1988 | - $row['path'] = rtrim($relPath, '/'); |
|
| 1989 | - } |
|
| 1990 | - } |
|
| 1991 | - } |
|
| 1992 | - |
|
| 1993 | - if($checkExpireDate) { |
|
| 1994 | - if (self::expireItem($row)) { |
|
| 1995 | - continue; |
|
| 1996 | - } |
|
| 1997 | - } |
|
| 1998 | - // Check if resharing is allowed, if not remove share permission |
|
| 1999 | - if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) { |
|
| 2000 | - $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 2001 | - } |
|
| 2002 | - // Add display names to result |
|
| 2003 | - $row['share_with_displayname'] = $row['share_with']; |
|
| 2004 | - if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2005 | - $row['share_type'] === self::SHARE_TYPE_USER) { |
|
| 2006 | - $row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']); |
|
| 2007 | - } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2008 | - $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
|
| 2009 | - $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
|
| 2010 | - foreach ($addressBookEntries as $entry) { |
|
| 2011 | - foreach ($entry['CLOUD'] as $cloudID) { |
|
| 2012 | - if ($cloudID === $row['share_with']) { |
|
| 2013 | - $row['share_with_displayname'] = $entry['FN']; |
|
| 2014 | - } |
|
| 2015 | - } |
|
| 2016 | - } |
|
| 2017 | - } |
|
| 2018 | - if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 2019 | - $row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']); |
|
| 2020 | - } |
|
| 2021 | - |
|
| 2022 | - if ($row['permissions'] > 0) { |
|
| 2023 | - $items[$row['id']] = $row; |
|
| 2024 | - } |
|
| 2025 | - |
|
| 2026 | - } |
|
| 2027 | - |
|
| 2028 | - // group items if we are looking for items shared with the current user |
|
| 2029 | - if (isset($shareWith) && $shareWith === \OCP\User::getUser()) { |
|
| 2030 | - $items = self::groupItems($items, $itemType); |
|
| 2031 | - } |
|
| 2032 | - |
|
| 2033 | - if (!empty($items)) { |
|
| 2034 | - $collectionItems = array(); |
|
| 2035 | - foreach ($items as &$row) { |
|
| 2036 | - // Return only the item instead of a 2-dimensional array |
|
| 2037 | - if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) { |
|
| 2038 | - if ($format == self::FORMAT_NONE) { |
|
| 2039 | - return $row; |
|
| 2040 | - } else { |
|
| 2041 | - break; |
|
| 2042 | - } |
|
| 2043 | - } |
|
| 2044 | - // Check if this is a collection of the requested item type |
|
| 2045 | - if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) { |
|
| 2046 | - if (($collectionBackend = self::getBackend($row['item_type'])) |
|
| 2047 | - && $collectionBackend instanceof \OCP\Share_Backend_Collection) { |
|
| 2048 | - // Collections can be inside collections, check if the item is a collection |
|
| 2049 | - if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) { |
|
| 2050 | - $collectionItems[] = $row; |
|
| 2051 | - } else { |
|
| 2052 | - $collection = array(); |
|
| 2053 | - $collection['item_type'] = $row['item_type']; |
|
| 2054 | - if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2055 | - $collection['path'] = basename($row['path']); |
|
| 2056 | - } |
|
| 2057 | - $row['collection'] = $collection; |
|
| 2058 | - // Fetch all of the children sources |
|
| 2059 | - $children = $collectionBackend->getChildren($row[$column]); |
|
| 2060 | - foreach ($children as $child) { |
|
| 2061 | - $childItem = $row; |
|
| 2062 | - $childItem['item_type'] = $itemType; |
|
| 2063 | - if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') { |
|
| 2064 | - $childItem['item_source'] = $child['source']; |
|
| 2065 | - $childItem['item_target'] = $child['target']; |
|
| 2066 | - } |
|
| 2067 | - if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2068 | - if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2069 | - $childItem['file_source'] = $child['source']; |
|
| 2070 | - } else { // TODO is this really needed if we already know that we use the file backend? |
|
| 2071 | - $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']); |
|
| 2072 | - $childItem['file_source'] = $meta['fileid']; |
|
| 2073 | - } |
|
| 2074 | - $childItem['file_target'] = |
|
| 2075 | - \OC\Files\Filesystem::normalizePath($child['file_path']); |
|
| 2076 | - } |
|
| 2077 | - if (isset($item)) { |
|
| 2078 | - if ($childItem[$column] == $item) { |
|
| 2079 | - // Return only the item instead of a 2-dimensional array |
|
| 2080 | - if ($limit == 1) { |
|
| 2081 | - if ($format == self::FORMAT_NONE) { |
|
| 2082 | - return $childItem; |
|
| 2083 | - } else { |
|
| 2084 | - // Unset the items array and break out of both loops |
|
| 2085 | - $items = array(); |
|
| 2086 | - $items[] = $childItem; |
|
| 2087 | - break 2; |
|
| 2088 | - } |
|
| 2089 | - } else { |
|
| 2090 | - $collectionItems[] = $childItem; |
|
| 2091 | - } |
|
| 2092 | - } |
|
| 2093 | - } else { |
|
| 2094 | - $collectionItems[] = $childItem; |
|
| 2095 | - } |
|
| 2096 | - } |
|
| 2097 | - } |
|
| 2098 | - } |
|
| 2099 | - // Remove collection item |
|
| 2100 | - $toRemove = $row['id']; |
|
| 2101 | - if (array_key_exists($toRemove, $switchedItems)) { |
|
| 2102 | - $toRemove = $switchedItems[$toRemove]; |
|
| 2103 | - } |
|
| 2104 | - unset($items[$toRemove]); |
|
| 2105 | - } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) { |
|
| 2106 | - // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2107 | - // see github issue #10588 for more details |
|
| 2108 | - // Need to find a solution which works for all back-ends |
|
| 2109 | - $collectionBackend = self::getBackend($row['item_type']); |
|
| 2110 | - $sharedParents = $collectionBackend->getParents($row['item_source']); |
|
| 2111 | - foreach ($sharedParents as $parent) { |
|
| 2112 | - $collectionItems[] = $parent; |
|
| 2113 | - } |
|
| 2114 | - } |
|
| 2115 | - } |
|
| 2116 | - if (!empty($collectionItems)) { |
|
| 2117 | - $collectionItems = array_unique($collectionItems, SORT_REGULAR); |
|
| 2118 | - $items = array_merge($items, $collectionItems); |
|
| 2119 | - } |
|
| 2120 | - |
|
| 2121 | - // filter out invalid items, these can appear when subshare entries exist |
|
| 2122 | - // for a group in which the requested user isn't a member any more |
|
| 2123 | - $items = array_filter($items, function($item) { |
|
| 2124 | - return $item['share_type'] !== self::$shareTypeGroupUserUnique; |
|
| 2125 | - }); |
|
| 2126 | - |
|
| 2127 | - return self::formatResult($items, $column, $backend, $format, $parameters); |
|
| 2128 | - } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) { |
|
| 2129 | - // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2130 | - // see github issue #10588 for more details |
|
| 2131 | - // Need to find a solution which works for all back-ends |
|
| 2132 | - $collectionItems = array(); |
|
| 2133 | - $collectionBackend = self::getBackend('folder'); |
|
| 2134 | - $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner); |
|
| 2135 | - foreach ($sharedParents as $parent) { |
|
| 2136 | - $collectionItems[] = $parent; |
|
| 2137 | - } |
|
| 2138 | - if ($limit === 1) { |
|
| 2139 | - return reset($collectionItems); |
|
| 2140 | - } |
|
| 2141 | - return self::formatResult($collectionItems, $column, $backend, $format, $parameters); |
|
| 2142 | - } |
|
| 2143 | - |
|
| 2144 | - return array(); |
|
| 2145 | - } |
|
| 2146 | - |
|
| 2147 | - /** |
|
| 2148 | - * group items with link to the same source |
|
| 2149 | - * |
|
| 2150 | - * @param array $items |
|
| 2151 | - * @param string $itemType |
|
| 2152 | - * @return array of grouped items |
|
| 2153 | - */ |
|
| 2154 | - protected static function groupItems($items, $itemType) { |
|
| 2155 | - |
|
| 2156 | - $fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false; |
|
| 2157 | - |
|
| 2158 | - $result = array(); |
|
| 2159 | - |
|
| 2160 | - foreach ($items as $item) { |
|
| 2161 | - $grouped = false; |
|
| 2162 | - foreach ($result as $key => $r) { |
|
| 2163 | - // for file/folder shares we need to compare file_source, otherwise we compare item_source |
|
| 2164 | - // only group shares if they already point to the same target, otherwise the file where shared |
|
| 2165 | - // before grouping of shares was added. In this case we don't group them toi avoid confusions |
|
| 2166 | - if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 2167 | - (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
|
| 2168 | - // add the first item to the list of grouped shares |
|
| 2169 | - if (!isset($result[$key]['grouped'])) { |
|
| 2170 | - $result[$key]['grouped'][] = $result[$key]; |
|
| 2171 | - } |
|
| 2172 | - $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions']; |
|
| 2173 | - $result[$key]['grouped'][] = $item; |
|
| 2174 | - $grouped = true; |
|
| 2175 | - break; |
|
| 2176 | - } |
|
| 2177 | - } |
|
| 2178 | - |
|
| 2179 | - if (!$grouped) { |
|
| 2180 | - $result[] = $item; |
|
| 2181 | - } |
|
| 2182 | - |
|
| 2183 | - } |
|
| 2184 | - |
|
| 2185 | - return $result; |
|
| 2186 | - } |
|
| 2187 | - |
|
| 2188 | - /** |
|
| 2189 | - * Put shared item into the database |
|
| 2190 | - * @param string $itemType Item type |
|
| 2191 | - * @param string $itemSource Item source |
|
| 2192 | - * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 2193 | - * @param string $shareWith User or group the item is being shared with |
|
| 2194 | - * @param string $uidOwner User that is the owner of shared item |
|
| 2195 | - * @param int $permissions CRUDS permissions |
|
| 2196 | - * @param boolean|array $parentFolder Parent folder target (optional) |
|
| 2197 | - * @param string $token (optional) |
|
| 2198 | - * @param string $itemSourceName name of the source item (optional) |
|
| 2199 | - * @param \DateTime $expirationDate (optional) |
|
| 2200 | - * @throws \Exception |
|
| 2201 | - * @return mixed id of the new share or false |
|
| 2202 | - */ |
|
| 2203 | - private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2204 | - $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) { |
|
| 2205 | - |
|
| 2206 | - $queriesToExecute = array(); |
|
| 2207 | - $suggestedItemTarget = null; |
|
| 2208 | - $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = ''; |
|
| 2209 | - $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
|
| 2210 | - |
|
| 2211 | - $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate); |
|
| 2212 | - if(!empty($result)) { |
|
| 2213 | - $parent = $result['parent']; |
|
| 2214 | - $itemSource = $result['itemSource']; |
|
| 2215 | - $fileSource = $result['fileSource']; |
|
| 2216 | - $suggestedItemTarget = $result['suggestedItemTarget']; |
|
| 2217 | - $suggestedFileTarget = $result['suggestedFileTarget']; |
|
| 2218 | - $filePath = $result['filePath']; |
|
| 2219 | - } |
|
| 2220 | - |
|
| 2221 | - $isGroupShare = false; |
|
| 2222 | - if ($shareType == self::SHARE_TYPE_GROUP) { |
|
| 2223 | - $isGroupShare = true; |
|
| 2224 | - if (isset($shareWith['users'])) { |
|
| 2225 | - $users = $shareWith['users']; |
|
| 2226 | - } else { |
|
| 2227 | - $group = \OC::$server->getGroupManager()->get($shareWith['group']); |
|
| 2228 | - if ($group) { |
|
| 2229 | - $users = $group->searchUsers('', -1, 0); |
|
| 2230 | - $userIds = []; |
|
| 2231 | - foreach ($users as $user) { |
|
| 2232 | - $userIds[] = $user->getUID(); |
|
| 2233 | - } |
|
| 2234 | - $users = $userIds; |
|
| 2235 | - } else { |
|
| 2236 | - $users = []; |
|
| 2237 | - } |
|
| 2238 | - } |
|
| 2239 | - // remove current user from list |
|
| 2240 | - if (in_array(\OCP\User::getUser(), $users)) { |
|
| 2241 | - unset($users[array_search(\OCP\User::getUser(), $users)]); |
|
| 2242 | - } |
|
| 2243 | - $groupItemTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2244 | - $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget); |
|
| 2245 | - $groupFileTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2246 | - $shareType, $shareWith['group'], $uidOwner, $filePath); |
|
| 2247 | - |
|
| 2248 | - // add group share to table and remember the id as parent |
|
| 2249 | - $queriesToExecute['groupShare'] = array( |
|
| 2250 | - 'itemType' => $itemType, |
|
| 2251 | - 'itemSource' => $itemSource, |
|
| 2252 | - 'itemTarget' => $groupItemTarget, |
|
| 2253 | - 'shareType' => $shareType, |
|
| 2254 | - 'shareWith' => $shareWith['group'], |
|
| 2255 | - 'uidOwner' => $uidOwner, |
|
| 2256 | - 'permissions' => $permissions, |
|
| 2257 | - 'shareTime' => time(), |
|
| 2258 | - 'fileSource' => $fileSource, |
|
| 2259 | - 'fileTarget' => $groupFileTarget, |
|
| 2260 | - 'token' => $token, |
|
| 2261 | - 'parent' => $parent, |
|
| 2262 | - 'expiration' => $expirationDate, |
|
| 2263 | - ); |
|
| 2264 | - |
|
| 2265 | - } else { |
|
| 2266 | - $users = array($shareWith); |
|
| 2267 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2268 | - $suggestedItemTarget); |
|
| 2269 | - } |
|
| 2270 | - |
|
| 2271 | - $run = true; |
|
| 2272 | - $error = ''; |
|
| 2273 | - $preHookData = array( |
|
| 2274 | - 'itemType' => $itemType, |
|
| 2275 | - 'itemSource' => $itemSource, |
|
| 2276 | - 'shareType' => $shareType, |
|
| 2277 | - 'uidOwner' => $uidOwner, |
|
| 2278 | - 'permissions' => $permissions, |
|
| 2279 | - 'fileSource' => $fileSource, |
|
| 2280 | - 'expiration' => $expirationDate, |
|
| 2281 | - 'token' => $token, |
|
| 2282 | - 'run' => &$run, |
|
| 2283 | - 'error' => &$error |
|
| 2284 | - ); |
|
| 2285 | - |
|
| 2286 | - $preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2287 | - $preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2288 | - |
|
| 2289 | - \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); |
|
| 2290 | - |
|
| 2291 | - if ($run === false) { |
|
| 2292 | - throw new \Exception($error); |
|
| 2293 | - } |
|
| 2294 | - |
|
| 2295 | - foreach ($users as $user) { |
|
| 2296 | - $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource; |
|
| 2297 | - $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user); |
|
| 2298 | - |
|
| 2299 | - $userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType; |
|
| 2300 | - |
|
| 2301 | - if ($sourceExists && $sourceExists['item_source'] === $itemSource) { |
|
| 2302 | - $fileTarget = $sourceExists['file_target']; |
|
| 2303 | - $itemTarget = $sourceExists['item_target']; |
|
| 2304 | - |
|
| 2305 | - // for group shares we don't need a additional entry if the target is the same |
|
| 2306 | - if($isGroupShare && $groupItemTarget === $itemTarget) { |
|
| 2307 | - continue; |
|
| 2308 | - } |
|
| 2309 | - |
|
| 2310 | - } elseif(!$sourceExists && !$isGroupShare) { |
|
| 2311 | - |
|
| 2312 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
|
| 2313 | - $uidOwner, $suggestedItemTarget, $parent); |
|
| 2314 | - if (isset($fileSource)) { |
|
| 2315 | - if ($parentFolder) { |
|
| 2316 | - if ($parentFolder === true) { |
|
| 2317 | - $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user, |
|
| 2318 | - $uidOwner, $suggestedFileTarget, $parent); |
|
| 2319 | - if ($fileTarget != $groupFileTarget) { |
|
| 2320 | - $parentFolders[$user]['folder'] = $fileTarget; |
|
| 2321 | - } |
|
| 2322 | - } else if (isset($parentFolder[$user])) { |
|
| 2323 | - $fileTarget = $parentFolder[$user]['folder'].$itemSource; |
|
| 2324 | - $parent = $parentFolder[$user]['id']; |
|
| 2325 | - } |
|
| 2326 | - } else { |
|
| 2327 | - $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, |
|
| 2328 | - $user, $uidOwner, $suggestedFileTarget, $parent); |
|
| 2329 | - } |
|
| 2330 | - } else { |
|
| 2331 | - $fileTarget = null; |
|
| 2332 | - } |
|
| 2333 | - |
|
| 2334 | - } else { |
|
| 2335 | - |
|
| 2336 | - // group share which doesn't exists until now, check if we need a unique target for this user |
|
| 2337 | - |
|
| 2338 | - $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, |
|
| 2339 | - $uidOwner, $suggestedItemTarget, $parent); |
|
| 2340 | - |
|
| 2341 | - // do we also need a file target |
|
| 2342 | - if (isset($fileSource)) { |
|
| 2343 | - $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, |
|
| 2344 | - $uidOwner, $suggestedFileTarget, $parent); |
|
| 2345 | - } else { |
|
| 2346 | - $fileTarget = null; |
|
| 2347 | - } |
|
| 2348 | - |
|
| 2349 | - if (($itemTarget === $groupItemTarget) && |
|
| 2350 | - (!isset($fileSource) || $fileTarget === $groupFileTarget)) { |
|
| 2351 | - continue; |
|
| 2352 | - } |
|
| 2353 | - } |
|
| 2354 | - |
|
| 2355 | - $queriesToExecute[] = array( |
|
| 2356 | - 'itemType' => $itemType, |
|
| 2357 | - 'itemSource' => $itemSource, |
|
| 2358 | - 'itemTarget' => $itemTarget, |
|
| 2359 | - 'shareType' => $userShareType, |
|
| 2360 | - 'shareWith' => $user, |
|
| 2361 | - 'uidOwner' => $uidOwner, |
|
| 2362 | - 'permissions' => $permissions, |
|
| 2363 | - 'shareTime' => time(), |
|
| 2364 | - 'fileSource' => $fileSource, |
|
| 2365 | - 'fileTarget' => $fileTarget, |
|
| 2366 | - 'token' => $token, |
|
| 2367 | - 'parent' => $parent, |
|
| 2368 | - 'expiration' => $expirationDate, |
|
| 2369 | - ); |
|
| 2370 | - |
|
| 2371 | - } |
|
| 2372 | - |
|
| 2373 | - $id = false; |
|
| 2374 | - if ($isGroupShare) { |
|
| 2375 | - $id = self::insertShare($queriesToExecute['groupShare']); |
|
| 2376 | - // Save this id, any extra rows for this group share will need to reference it |
|
| 2377 | - $parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2378 | - unset($queriesToExecute['groupShare']); |
|
| 2379 | - } |
|
| 2380 | - |
|
| 2381 | - foreach ($queriesToExecute as $shareQuery) { |
|
| 2382 | - $shareQuery['parent'] = $parent; |
|
| 2383 | - $id = self::insertShare($shareQuery); |
|
| 2384 | - } |
|
| 2385 | - |
|
| 2386 | - $postHookData = array( |
|
| 2387 | - 'itemType' => $itemType, |
|
| 2388 | - 'itemSource' => $itemSource, |
|
| 2389 | - 'parent' => $parent, |
|
| 2390 | - 'shareType' => $shareType, |
|
| 2391 | - 'uidOwner' => $uidOwner, |
|
| 2392 | - 'permissions' => $permissions, |
|
| 2393 | - 'fileSource' => $fileSource, |
|
| 2394 | - 'id' => $parent, |
|
| 2395 | - 'token' => $token, |
|
| 2396 | - 'expirationDate' => $expirationDate, |
|
| 2397 | - ); |
|
| 2398 | - |
|
| 2399 | - $postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2400 | - $postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2401 | - $postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget; |
|
| 2402 | - |
|
| 2403 | - \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); |
|
| 2404 | - |
|
| 2405 | - |
|
| 2406 | - return $id ? $id : false; |
|
| 2407 | - } |
|
| 2408 | - |
|
| 2409 | - /** |
|
| 2410 | - * @param string $itemType |
|
| 2411 | - * @param string $itemSource |
|
| 2412 | - * @param int $shareType |
|
| 2413 | - * @param string $shareWith |
|
| 2414 | - * @param string $uidOwner |
|
| 2415 | - * @param int $permissions |
|
| 2416 | - * @param string|null $itemSourceName |
|
| 2417 | - * @param null|\DateTime $expirationDate |
|
| 2418 | - */ |
|
| 2419 | - private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) { |
|
| 2420 | - $backend = self::getBackend($itemType); |
|
| 2421 | - |
|
| 2422 | - $l = \OC::$server->getL10N('lib'); |
|
| 2423 | - $result = array(); |
|
| 2424 | - |
|
| 2425 | - $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source'; |
|
| 2426 | - |
|
| 2427 | - $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true); |
|
| 2428 | - if ($checkReshare) { |
|
| 2429 | - // Check if attempting to share back to owner |
|
| 2430 | - if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) { |
|
| 2431 | - $message = 'Sharing %s failed, because the user %s is the original sharer'; |
|
| 2432 | - $message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]); |
|
| 2433 | - |
|
| 2434 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 2435 | - throw new \Exception($message_t); |
|
| 2436 | - } |
|
| 2437 | - } |
|
| 2438 | - |
|
| 2439 | - if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
|
| 2440 | - // Check if share permissions is granted |
|
| 2441 | - if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 2442 | - if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 2443 | - $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s'; |
|
| 2444 | - $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner)); |
|
| 2445 | - |
|
| 2446 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG); |
|
| 2447 | - throw new \Exception($message_t); |
|
| 2448 | - } else { |
|
| 2449 | - // TODO Don't check if inside folder |
|
| 2450 | - $result['parent'] = $checkReshare['id']; |
|
| 2451 | - |
|
| 2452 | - $result['expirationDate'] = $expirationDate; |
|
| 2453 | - // $checkReshare['expiration'] could be null and then is always less than any value |
|
| 2454 | - if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 2455 | - $result['expirationDate'] = $checkReshare['expiration']; |
|
| 2456 | - } |
|
| 2457 | - |
|
| 2458 | - // only suggest the same name as new target if it is a reshare of the |
|
| 2459 | - // same file/folder and not the reshare of a child |
|
| 2460 | - if ($checkReshare[$column] === $itemSource) { |
|
| 2461 | - $result['filePath'] = $checkReshare['file_target']; |
|
| 2462 | - $result['itemSource'] = $checkReshare['item_source']; |
|
| 2463 | - $result['fileSource'] = $checkReshare['file_source']; |
|
| 2464 | - $result['suggestedItemTarget'] = $checkReshare['item_target']; |
|
| 2465 | - $result['suggestedFileTarget'] = $checkReshare['file_target']; |
|
| 2466 | - } else { |
|
| 2467 | - $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null; |
|
| 2468 | - $result['suggestedItemTarget'] = null; |
|
| 2469 | - $result['suggestedFileTarget'] = null; |
|
| 2470 | - $result['itemSource'] = $itemSource; |
|
| 2471 | - $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null; |
|
| 2472 | - } |
|
| 2473 | - } |
|
| 2474 | - } else { |
|
| 2475 | - $message = 'Sharing %s failed, because resharing is not allowed'; |
|
| 2476 | - $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName)); |
|
| 2477 | - |
|
| 2478 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 2479 | - throw new \Exception($message_t); |
|
| 2480 | - } |
|
| 2481 | - } else { |
|
| 2482 | - $result['parent'] = null; |
|
| 2483 | - $result['suggestedItemTarget'] = null; |
|
| 2484 | - $result['suggestedFileTarget'] = null; |
|
| 2485 | - $result['itemSource'] = $itemSource; |
|
| 2486 | - $result['expirationDate'] = $expirationDate; |
|
| 2487 | - if (!$backend->isValidSource($itemSource, $uidOwner)) { |
|
| 2488 | - $message = 'Sharing %s failed, because the sharing backend for ' |
|
| 2489 | - .'%s could not find its source'; |
|
| 2490 | - $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType)); |
|
| 2491 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG); |
|
| 2492 | - throw new \Exception($message_t); |
|
| 2493 | - } |
|
| 2494 | - if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2495 | - $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner); |
|
| 2496 | - if ($itemType == 'file' || $itemType == 'folder') { |
|
| 2497 | - $result['fileSource'] = $itemSource; |
|
| 2498 | - } else { |
|
| 2499 | - $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']); |
|
| 2500 | - $result['fileSource'] = $meta['fileid']; |
|
| 2501 | - } |
|
| 2502 | - if ($result['fileSource'] == -1) { |
|
| 2503 | - $message = 'Sharing %s failed, because the file could not be found in the file cache'; |
|
| 2504 | - $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource)); |
|
| 2505 | - |
|
| 2506 | - \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 2507 | - throw new \Exception($message_t); |
|
| 2508 | - } |
|
| 2509 | - } else { |
|
| 2510 | - $result['filePath'] = null; |
|
| 2511 | - $result['fileSource'] = null; |
|
| 2512 | - } |
|
| 2513 | - } |
|
| 2514 | - |
|
| 2515 | - return $result; |
|
| 2516 | - } |
|
| 2517 | - |
|
| 2518 | - /** |
|
| 2519 | - * |
|
| 2520 | - * @param array $shareData |
|
| 2521 | - * @return mixed false in case of a failure or the id of the new share |
|
| 2522 | - */ |
|
| 2523 | - private static function insertShare(array $shareData) { |
|
| 2524 | - |
|
| 2525 | - $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' |
|
| 2526 | - .' `item_type`, `item_source`, `item_target`, `share_type`,' |
|
| 2527 | - .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' |
|
| 2528 | - .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 2529 | - $query->bindValue(1, $shareData['itemType']); |
|
| 2530 | - $query->bindValue(2, $shareData['itemSource']); |
|
| 2531 | - $query->bindValue(3, $shareData['itemTarget']); |
|
| 2532 | - $query->bindValue(4, $shareData['shareType']); |
|
| 2533 | - $query->bindValue(5, $shareData['shareWith']); |
|
| 2534 | - $query->bindValue(6, $shareData['uidOwner']); |
|
| 2535 | - $query->bindValue(7, $shareData['permissions']); |
|
| 2536 | - $query->bindValue(8, $shareData['shareTime']); |
|
| 2537 | - $query->bindValue(9, $shareData['fileSource']); |
|
| 2538 | - $query->bindValue(10, $shareData['fileTarget']); |
|
| 2539 | - $query->bindValue(11, $shareData['token']); |
|
| 2540 | - $query->bindValue(12, $shareData['parent']); |
|
| 2541 | - $query->bindValue(13, $shareData['expiration'], 'datetime'); |
|
| 2542 | - $result = $query->execute(); |
|
| 2543 | - |
|
| 2544 | - $id = false; |
|
| 2545 | - if ($result) { |
|
| 2546 | - $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2547 | - } |
|
| 2548 | - |
|
| 2549 | - return $id; |
|
| 2550 | - |
|
| 2551 | - } |
|
| 2552 | - |
|
| 2553 | - /** |
|
| 2554 | - * Delete all shares with type SHARE_TYPE_LINK |
|
| 2555 | - */ |
|
| 2556 | - public static function removeAllLinkShares() { |
|
| 2557 | - // Delete any link shares |
|
| 2558 | - $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ?'); |
|
| 2559 | - $result = $query->execute(array(self::SHARE_TYPE_LINK)); |
|
| 2560 | - while ($item = $result->fetchRow()) { |
|
| 2561 | - Helper::delete($item['id']); |
|
| 2562 | - } |
|
| 2563 | - } |
|
| 2564 | - |
|
| 2565 | - /** |
|
| 2566 | - * In case a password protected link is not yet authenticated this function will return false |
|
| 2567 | - * |
|
| 2568 | - * @param array $linkItem |
|
| 2569 | - * @return boolean |
|
| 2570 | - */ |
|
| 2571 | - public static function checkPasswordProtectedShare(array $linkItem) { |
|
| 2572 | - if (!isset($linkItem['share_with'])) { |
|
| 2573 | - return true; |
|
| 2574 | - } |
|
| 2575 | - if (!isset($linkItem['share_type'])) { |
|
| 2576 | - return true; |
|
| 2577 | - } |
|
| 2578 | - if (!isset($linkItem['id'])) { |
|
| 2579 | - return true; |
|
| 2580 | - } |
|
| 2581 | - |
|
| 2582 | - if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) { |
|
| 2583 | - return true; |
|
| 2584 | - } |
|
| 2585 | - |
|
| 2586 | - if ( \OC::$server->getSession()->exists('public_link_authenticated') |
|
| 2587 | - && \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) { |
|
| 2588 | - return true; |
|
| 2589 | - } |
|
| 2590 | - |
|
| 2591 | - return false; |
|
| 2592 | - } |
|
| 2593 | - |
|
| 2594 | - /** |
|
| 2595 | - * construct select statement |
|
| 2596 | - * @param int $format |
|
| 2597 | - * @param boolean $fileDependent ist it a file/folder share or a generla share |
|
| 2598 | - * @param string $uidOwner |
|
| 2599 | - * @return string select statement |
|
| 2600 | - */ |
|
| 2601 | - private static function createSelectStatement($format, $fileDependent, $uidOwner = null) { |
|
| 2602 | - $select = '*'; |
|
| 2603 | - if ($format == self::FORMAT_STATUSES) { |
|
| 2604 | - if ($fileDependent) { |
|
| 2605 | - $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, ' |
|
| 2606 | - . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, ' |
|
| 2607 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2608 | - . '`uid_initiator`'; |
|
| 2609 | - } else { |
|
| 2610 | - $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`'; |
|
| 2611 | - } |
|
| 2612 | - } else { |
|
| 2613 | - if (isset($uidOwner)) { |
|
| 2614 | - if ($fileDependent) { |
|
| 2615 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,' |
|
| 2616 | - . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,' |
|
| 2617 | - . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, ' |
|
| 2618 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2619 | - } else { |
|
| 2620 | - $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,' |
|
| 2621 | - . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`'; |
|
| 2622 | - } |
|
| 2623 | - } else { |
|
| 2624 | - if ($fileDependent) { |
|
| 2625 | - if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) { |
|
| 2626 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, ' |
|
| 2627 | - . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, ' |
|
| 2628 | - . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2629 | - . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`'; |
|
| 2630 | - } else { |
|
| 2631 | - $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,' |
|
| 2632 | - . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,' |
|
| 2633 | - . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,' |
|
| 2634 | - . '`stime`, `expiration`, `token`, `storage`, `mail_send`,' |
|
| 2635 | - . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2636 | - } |
|
| 2637 | - } |
|
| 2638 | - } |
|
| 2639 | - } |
|
| 2640 | - return $select; |
|
| 2641 | - } |
|
| 2642 | - |
|
| 2643 | - |
|
| 2644 | - /** |
|
| 2645 | - * transform db results |
|
| 2646 | - * @param array $row result |
|
| 2647 | - */ |
|
| 2648 | - private static function transformDBResults(&$row) { |
|
| 2649 | - if (isset($row['id'])) { |
|
| 2650 | - $row['id'] = (int) $row['id']; |
|
| 2651 | - } |
|
| 2652 | - if (isset($row['share_type'])) { |
|
| 2653 | - $row['share_type'] = (int) $row['share_type']; |
|
| 2654 | - } |
|
| 2655 | - if (isset($row['parent'])) { |
|
| 2656 | - $row['parent'] = (int) $row['parent']; |
|
| 2657 | - } |
|
| 2658 | - if (isset($row['file_parent'])) { |
|
| 2659 | - $row['file_parent'] = (int) $row['file_parent']; |
|
| 2660 | - } |
|
| 2661 | - if (isset($row['file_source'])) { |
|
| 2662 | - $row['file_source'] = (int) $row['file_source']; |
|
| 2663 | - } |
|
| 2664 | - if (isset($row['permissions'])) { |
|
| 2665 | - $row['permissions'] = (int) $row['permissions']; |
|
| 2666 | - } |
|
| 2667 | - if (isset($row['storage'])) { |
|
| 2668 | - $row['storage'] = (int) $row['storage']; |
|
| 2669 | - } |
|
| 2670 | - if (isset($row['stime'])) { |
|
| 2671 | - $row['stime'] = (int) $row['stime']; |
|
| 2672 | - } |
|
| 2673 | - if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) { |
|
| 2674 | - // discard expiration date for non-link shares, which might have been |
|
| 2675 | - // set by ancient bugs |
|
| 2676 | - $row['expiration'] = null; |
|
| 2677 | - } |
|
| 2678 | - } |
|
| 2679 | - |
|
| 2680 | - /** |
|
| 2681 | - * format result |
|
| 2682 | - * @param array $items result |
|
| 2683 | - * @param string $column is it a file share or a general share ('file_target' or 'item_target') |
|
| 2684 | - * @param \OCP\Share_Backend $backend sharing backend |
|
| 2685 | - * @param int $format |
|
| 2686 | - * @param array $parameters additional format parameters |
|
| 2687 | - * @return array format result |
|
| 2688 | - */ |
|
| 2689 | - private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 2690 | - if ($format === self::FORMAT_NONE) { |
|
| 2691 | - return $items; |
|
| 2692 | - } else if ($format === self::FORMAT_STATUSES) { |
|
| 2693 | - $statuses = array(); |
|
| 2694 | - foreach ($items as $item) { |
|
| 2695 | - if ($item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 2696 | - if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) { |
|
| 2697 | - continue; |
|
| 2698 | - } |
|
| 2699 | - $statuses[$item[$column]]['link'] = true; |
|
| 2700 | - } else if (!isset($statuses[$item[$column]])) { |
|
| 2701 | - $statuses[$item[$column]]['link'] = false; |
|
| 2702 | - } |
|
| 2703 | - if (!empty($item['file_target'])) { |
|
| 2704 | - $statuses[$item[$column]]['path'] = $item['path']; |
|
| 2705 | - } |
|
| 2706 | - } |
|
| 2707 | - return $statuses; |
|
| 2708 | - } else { |
|
| 2709 | - return $backend->formatItems($items, $format, $parameters); |
|
| 2710 | - } |
|
| 2711 | - } |
|
| 2712 | - |
|
| 2713 | - /** |
|
| 2714 | - * remove protocol from URL |
|
| 2715 | - * |
|
| 2716 | - * @param string $url |
|
| 2717 | - * @return string |
|
| 2718 | - */ |
|
| 2719 | - public static function removeProtocolFromUrl($url) { |
|
| 2720 | - if (strpos($url, 'https://') === 0) { |
|
| 2721 | - return substr($url, strlen('https://')); |
|
| 2722 | - } else if (strpos($url, 'http://') === 0) { |
|
| 2723 | - return substr($url, strlen('http://')); |
|
| 2724 | - } |
|
| 2725 | - |
|
| 2726 | - return $url; |
|
| 2727 | - } |
|
| 2728 | - |
|
| 2729 | - /** |
|
| 2730 | - * try http post first with https and then with http as a fallback |
|
| 2731 | - * |
|
| 2732 | - * @param string $remoteDomain |
|
| 2733 | - * @param string $urlSuffix |
|
| 2734 | - * @param array $fields post parameters |
|
| 2735 | - * @return array |
|
| 2736 | - */ |
|
| 2737 | - private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
| 2738 | - $protocol = 'https://'; |
|
| 2739 | - $result = [ |
|
| 2740 | - 'success' => false, |
|
| 2741 | - 'result' => '', |
|
| 2742 | - ]; |
|
| 2743 | - $try = 0; |
|
| 2744 | - $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
| 2745 | - while ($result['success'] === false && $try < 2) { |
|
| 2746 | - $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 2747 | - $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
| 2748 | - $result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields); |
|
| 2749 | - $try++; |
|
| 2750 | - $protocol = 'http://'; |
|
| 2751 | - } |
|
| 2752 | - |
|
| 2753 | - return $result; |
|
| 2754 | - } |
|
| 2755 | - |
|
| 2756 | - /** |
|
| 2757 | - * send server-to-server share to remote server |
|
| 2758 | - * |
|
| 2759 | - * @param string $token |
|
| 2760 | - * @param string $shareWith |
|
| 2761 | - * @param string $name |
|
| 2762 | - * @param int $remote_id |
|
| 2763 | - * @param string $owner |
|
| 2764 | - * @return bool |
|
| 2765 | - */ |
|
| 2766 | - private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) { |
|
| 2767 | - |
|
| 2768 | - list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 2769 | - |
|
| 2770 | - if ($user && $remote) { |
|
| 2771 | - $url = $remote; |
|
| 2772 | - |
|
| 2773 | - $local = \OC::$server->getURLGenerator()->getAbsoluteURL('/'); |
|
| 2774 | - |
|
| 2775 | - $fields = array( |
|
| 2776 | - 'shareWith' => $user, |
|
| 2777 | - 'token' => $token, |
|
| 2778 | - 'name' => $name, |
|
| 2779 | - 'remoteId' => $remote_id, |
|
| 2780 | - 'owner' => $owner, |
|
| 2781 | - 'remote' => $local, |
|
| 2782 | - ); |
|
| 2783 | - |
|
| 2784 | - $url = self::removeProtocolFromUrl($url); |
|
| 2785 | - $result = self::tryHttpPostToShareEndpoint($url, '', $fields); |
|
| 2786 | - $status = json_decode($result['result'], true); |
|
| 2787 | - |
|
| 2788 | - if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) { |
|
| 2789 | - \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); |
|
| 2790 | - return true; |
|
| 2791 | - } |
|
| 2792 | - |
|
| 2793 | - } |
|
| 2794 | - |
|
| 2795 | - return false; |
|
| 2796 | - } |
|
| 2797 | - |
|
| 2798 | - /** |
|
| 2799 | - * send server-to-server unshare to remote server |
|
| 2800 | - * |
|
| 2801 | - * @param string $remote url |
|
| 2802 | - * @param int $id share id |
|
| 2803 | - * @param string $token |
|
| 2804 | - * @return bool |
|
| 2805 | - */ |
|
| 2806 | - private static function sendRemoteUnshare($remote, $id, $token) { |
|
| 2807 | - $url = rtrim($remote, '/'); |
|
| 2808 | - $fields = array('token' => $token, 'format' => 'json'); |
|
| 2809 | - $url = self::removeProtocolFromUrl($url); |
|
| 2810 | - $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
| 2811 | - $status = json_decode($result['result'], true); |
|
| 2812 | - |
|
| 2813 | - return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
| 2814 | - } |
|
| 2815 | - |
|
| 2816 | - /** |
|
| 2817 | - * check if user can only share with group members |
|
| 2818 | - * @return bool |
|
| 2819 | - */ |
|
| 2820 | - public static function shareWithGroupMembersOnly() { |
|
| 2821 | - $value = \OC::$server->getAppConfig()->getValue('core', 'shareapi_only_share_with_group_members', 'no'); |
|
| 2822 | - return ($value === 'yes') ? true : false; |
|
| 2823 | - } |
|
| 2824 | - |
|
| 2825 | - /** |
|
| 2826 | - * @return bool |
|
| 2827 | - */ |
|
| 2828 | - public static function isDefaultExpireDateEnabled() { |
|
| 2829 | - $defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no'); |
|
| 2830 | - return ($defaultExpireDateEnabled === "yes") ? true : false; |
|
| 2831 | - } |
|
| 2832 | - |
|
| 2833 | - /** |
|
| 2834 | - * @return bool |
|
| 2835 | - */ |
|
| 2836 | - public static function enforceDefaultExpireDate() { |
|
| 2837 | - $enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no'); |
|
| 2838 | - return ($enforceDefaultExpireDate === "yes") ? true : false; |
|
| 2839 | - } |
|
| 2840 | - |
|
| 2841 | - /** |
|
| 2842 | - * @return int |
|
| 2843 | - */ |
|
| 2844 | - public static function getExpireInterval() { |
|
| 2845 | - return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 2846 | - } |
|
| 2847 | - |
|
| 2848 | - /** |
|
| 2849 | - * Checks whether the given path is reachable for the given owner |
|
| 2850 | - * |
|
| 2851 | - * @param string $path path relative to files |
|
| 2852 | - * @param string $ownerStorageId storage id of the owner |
|
| 2853 | - * |
|
| 2854 | - * @return boolean true if file is reachable, false otherwise |
|
| 2855 | - */ |
|
| 2856 | - private static function isFileReachable($path, $ownerStorageId) { |
|
| 2857 | - // if outside the home storage, file is always considered reachable |
|
| 2858 | - if (!(substr($ownerStorageId, 0, 6) === 'home::' || |
|
| 2859 | - substr($ownerStorageId, 0, 13) === 'object::user:' |
|
| 2860 | - )) { |
|
| 2861 | - return true; |
|
| 2862 | - } |
|
| 2863 | - |
|
| 2864 | - // if inside the home storage, the file has to be under "/files/" |
|
| 2865 | - $path = ltrim($path, '/'); |
|
| 2866 | - if (substr($path, 0, 6) === 'files/') { |
|
| 2867 | - return true; |
|
| 2868 | - } |
|
| 2869 | - |
|
| 2870 | - return false; |
|
| 2871 | - } |
|
| 2872 | - |
|
| 2873 | - /** |
|
| 2874 | - * @param IConfig $config |
|
| 2875 | - * @return bool |
|
| 2876 | - */ |
|
| 2877 | - public static function enforcePassword(IConfig $config) { |
|
| 2878 | - $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no'); |
|
| 2879 | - return ($enforcePassword === "yes") ? true : false; |
|
| 2880 | - } |
|
| 2881 | - |
|
| 2882 | - /** |
|
| 2883 | - * Get all share entries, including non-unique group items |
|
| 2884 | - * |
|
| 2885 | - * @param string $owner |
|
| 2886 | - * @return array |
|
| 2887 | - */ |
|
| 2888 | - public static function getAllSharesForOwner($owner) { |
|
| 2889 | - $query = 'SELECT * FROM `*PREFIX*share` WHERE `uid_owner` = ?'; |
|
| 2890 | - $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$owner]); |
|
| 2891 | - return $result->fetchAll(); |
|
| 2892 | - } |
|
| 2893 | - |
|
| 2894 | - /** |
|
| 2895 | - * Get all share entries, including non-unique group items for a file |
|
| 2896 | - * |
|
| 2897 | - * @param int $id |
|
| 2898 | - * @return array |
|
| 2899 | - */ |
|
| 2900 | - public static function getAllSharesForFileId($id) { |
|
| 2901 | - $query = 'SELECT * FROM `*PREFIX*share` WHERE `file_source` = ?'; |
|
| 2902 | - $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$id]); |
|
| 2903 | - return $result->fetchAll(); |
|
| 2904 | - } |
|
| 2905 | - |
|
| 2906 | - /** |
|
| 2907 | - * @param string $password |
|
| 2908 | - * @throws \Exception |
|
| 2909 | - */ |
|
| 2910 | - private static function verifyPassword($password) { |
|
| 2911 | - |
|
| 2912 | - $accepted = true; |
|
| 2913 | - $message = ''; |
|
| 2914 | - \OCP\Util::emitHook('\OC\Share', 'verifyPassword', [ |
|
| 2915 | - 'password' => $password, |
|
| 2916 | - 'accepted' => &$accepted, |
|
| 2917 | - 'message' => &$message |
|
| 2918 | - ]); |
|
| 2919 | - |
|
| 2920 | - if (!$accepted) { |
|
| 2921 | - throw new \Exception($message); |
|
| 2922 | - } |
|
| 2923 | - } |
|
| 1311 | + if ($permissions & ~(int)$rootItem['permissions']) { |
|
| 1312 | + $qb = $connection->getQueryBuilder(); |
|
| 1313 | + $qb->select('id', 'permissions', 'item_type') |
|
| 1314 | + ->from('share') |
|
| 1315 | + ->where($qb->expr()->eq('parent', $qb->createParameter('parent'))) |
|
| 1316 | + ->andWhere($qb->expr()->eq('share_type', $qb->createParameter('share_type'))) |
|
| 1317 | + ->andWhere($qb->expr()->neq('permissions', $qb->createParameter('shareDeleted'))) |
|
| 1318 | + ->setParameter(':parent', (int)$rootItem['id']) |
|
| 1319 | + ->setParameter(':share_type', 2) |
|
| 1320 | + ->setParameter(':shareDeleted', 0); |
|
| 1321 | + $result = $qb->execute(); |
|
| 1322 | + |
|
| 1323 | + $ids = []; |
|
| 1324 | + while ($item = $result->fetch()) { |
|
| 1325 | + $item = $sanitizeItem($item); |
|
| 1326 | + $items[] = $item; |
|
| 1327 | + $ids[] = $item['id']; |
|
| 1328 | + } |
|
| 1329 | + $result->closeCursor(); |
|
| 1330 | + |
|
| 1331 | + // Add permssions for all USERGROUP shares of this item |
|
| 1332 | + if (!empty($ids)) { |
|
| 1333 | + $ids = $intArrayToLiteralArray($ids, $qb->expr()); |
|
| 1334 | + |
|
| 1335 | + $qb = $connection->getQueryBuilder(); |
|
| 1336 | + $qb->update('share') |
|
| 1337 | + ->set('permissions', $qb->createParameter('permissions')) |
|
| 1338 | + ->where($qb->expr()->in('id', $ids)) |
|
| 1339 | + ->setParameter(':permissions', $permissions); |
|
| 1340 | + $qb->execute(); |
|
| 1341 | + } |
|
| 1342 | + } |
|
| 1343 | + |
|
| 1344 | + foreach ($items as $item) { |
|
| 1345 | + \OC_Hook::emit('OCP\Share', 'post_update_permissions', ['share' => $item]); |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + return true; |
|
| 1349 | + } |
|
| 1350 | + $message = 'Setting permissions for %s failed, because the item was not found'; |
|
| 1351 | + $message_t = $l->t('Setting permissions for %s failed, because the item was not found', array($itemSource)); |
|
| 1352 | + |
|
| 1353 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 1354 | + throw new \Exception($message_t); |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + /** |
|
| 1358 | + * validate expiration date if it meets all constraints |
|
| 1359 | + * |
|
| 1360 | + * @param string $expireDate well formatted date string, e.g. "DD-MM-YYYY" |
|
| 1361 | + * @param string $shareTime timestamp when the file was shared |
|
| 1362 | + * @param string $itemType |
|
| 1363 | + * @param string $itemSource |
|
| 1364 | + * @return \DateTime validated date |
|
| 1365 | + * @throws \Exception when the expire date is in the past or further in the future then the enforced date |
|
| 1366 | + */ |
|
| 1367 | + private static function validateExpireDate($expireDate, $shareTime, $itemType, $itemSource) { |
|
| 1368 | + $l = \OC::$server->getL10N('lib'); |
|
| 1369 | + $date = new \DateTime($expireDate); |
|
| 1370 | + $today = new \DateTime('now'); |
|
| 1371 | + |
|
| 1372 | + // if the user doesn't provide a share time we need to get it from the database |
|
| 1373 | + // fall-back mode to keep API stable, because the $shareTime parameter was added later |
|
| 1374 | + $defaultExpireDateEnforced = \OCP\Util::isDefaultExpireDateEnforced(); |
|
| 1375 | + if ($defaultExpireDateEnforced && $shareTime === null) { |
|
| 1376 | + $items = self::getItemShared($itemType, $itemSource); |
|
| 1377 | + $firstItem = reset($items); |
|
| 1378 | + $shareTime = (int)$firstItem['stime']; |
|
| 1379 | + } |
|
| 1380 | + |
|
| 1381 | + if ($defaultExpireDateEnforced) { |
|
| 1382 | + // initialize max date with share time |
|
| 1383 | + $maxDate = new \DateTime(); |
|
| 1384 | + $maxDate->setTimestamp($shareTime); |
|
| 1385 | + $maxDays = \OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 1386 | + $maxDate->add(new \DateInterval('P' . $maxDays . 'D')); |
|
| 1387 | + if ($date > $maxDate) { |
|
| 1388 | + $warning = 'Cannot set expiration date. Shares cannot expire later than ' . $maxDays . ' after they have been shared'; |
|
| 1389 | + $warning_t = $l->t('Cannot set expiration date. Shares cannot expire later than %s after they have been shared', array($maxDays)); |
|
| 1390 | + \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1391 | + throw new \Exception($warning_t); |
|
| 1392 | + } |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + if ($date < $today) { |
|
| 1396 | + $message = 'Cannot set expiration date. Expiration date is in the past'; |
|
| 1397 | + $message_t = $l->t('Cannot set expiration date. Expiration date is in the past'); |
|
| 1398 | + \OCP\Util::writeLog('OCP\Share', $message, \OCP\Util::WARN); |
|
| 1399 | + throw new \Exception($message_t); |
|
| 1400 | + } |
|
| 1401 | + |
|
| 1402 | + return $date; |
|
| 1403 | + } |
|
| 1404 | + |
|
| 1405 | + /** |
|
| 1406 | + * Set expiration date for a share |
|
| 1407 | + * @param string $itemType |
|
| 1408 | + * @param string $itemSource |
|
| 1409 | + * @param string $date expiration date |
|
| 1410 | + * @param int $shareTime timestamp from when the file was shared |
|
| 1411 | + * @return boolean |
|
| 1412 | + * @throws \Exception when the expire date is not set, in the past or further in the future then the enforced date |
|
| 1413 | + */ |
|
| 1414 | + public static function setExpirationDate($itemType, $itemSource, $date, $shareTime = null) { |
|
| 1415 | + $user = \OC_User::getUser(); |
|
| 1416 | + $l = \OC::$server->getL10N('lib'); |
|
| 1417 | + |
|
| 1418 | + if ($date == '') { |
|
| 1419 | + if (\OCP\Util::isDefaultExpireDateEnforced()) { |
|
| 1420 | + $warning = 'Cannot clear expiration date. Shares are required to have an expiration date.'; |
|
| 1421 | + $warning_t = $l->t('Cannot clear expiration date. Shares are required to have an expiration date.'); |
|
| 1422 | + \OCP\Util::writeLog('OCP\Share', $warning, \OCP\Util::WARN); |
|
| 1423 | + throw new \Exception($warning_t); |
|
| 1424 | + } else { |
|
| 1425 | + $date = null; |
|
| 1426 | + } |
|
| 1427 | + } else { |
|
| 1428 | + $date = self::validateExpireDate($date, $shareTime, $itemType, $itemSource); |
|
| 1429 | + } |
|
| 1430 | + $query = \OC_DB::prepare('UPDATE `*PREFIX*share` SET `expiration` = ? WHERE `item_type` = ? AND `item_source` = ? AND `uid_owner` = ? AND `share_type` = ?'); |
|
| 1431 | + $query->bindValue(1, $date, 'datetime'); |
|
| 1432 | + $query->bindValue(2, $itemType); |
|
| 1433 | + $query->bindValue(3, $itemSource); |
|
| 1434 | + $query->bindValue(4, $user); |
|
| 1435 | + $query->bindValue(5, \OCP\Share::SHARE_TYPE_LINK); |
|
| 1436 | + |
|
| 1437 | + $query->execute(); |
|
| 1438 | + |
|
| 1439 | + \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', array( |
|
| 1440 | + 'itemType' => $itemType, |
|
| 1441 | + 'itemSource' => $itemSource, |
|
| 1442 | + 'date' => $date, |
|
| 1443 | + 'uidOwner' => $user |
|
| 1444 | + )); |
|
| 1445 | + |
|
| 1446 | + return true; |
|
| 1447 | + } |
|
| 1448 | + |
|
| 1449 | + /** |
|
| 1450 | + * Retrieve the owner of a connection |
|
| 1451 | + * |
|
| 1452 | + * @param IDBConnection $connection |
|
| 1453 | + * @param int $shareId |
|
| 1454 | + * @throws \Exception |
|
| 1455 | + * @return string uid of share owner |
|
| 1456 | + */ |
|
| 1457 | + private static function getShareOwner(IDBConnection $connection, $shareId) { |
|
| 1458 | + $qb = $connection->getQueryBuilder(); |
|
| 1459 | + |
|
| 1460 | + $qb->select('uid_owner') |
|
| 1461 | + ->from('share') |
|
| 1462 | + ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1463 | + ->setParameter(':shareId', $shareId); |
|
| 1464 | + $result = $qb->execute(); |
|
| 1465 | + $result = $result->fetch(); |
|
| 1466 | + |
|
| 1467 | + if (empty($result)) { |
|
| 1468 | + throw new \Exception('Share not found'); |
|
| 1469 | + } |
|
| 1470 | + |
|
| 1471 | + return $result['uid_owner']; |
|
| 1472 | + } |
|
| 1473 | + |
|
| 1474 | + /** |
|
| 1475 | + * Set password for a public link share |
|
| 1476 | + * |
|
| 1477 | + * @param IUserSession $userSession |
|
| 1478 | + * @param IDBConnection $connection |
|
| 1479 | + * @param IConfig $config |
|
| 1480 | + * @param int $shareId |
|
| 1481 | + * @param string $password |
|
| 1482 | + * @throws \Exception |
|
| 1483 | + * @return boolean |
|
| 1484 | + */ |
|
| 1485 | + public static function setPassword(IUserSession $userSession, |
|
| 1486 | + IDBConnection $connection, |
|
| 1487 | + IConfig $config, |
|
| 1488 | + $shareId, $password) { |
|
| 1489 | + $user = $userSession->getUser(); |
|
| 1490 | + if (is_null($user)) { |
|
| 1491 | + throw new \Exception("User not logged in"); |
|
| 1492 | + } |
|
| 1493 | + |
|
| 1494 | + $uid = self::getShareOwner($connection, $shareId); |
|
| 1495 | + |
|
| 1496 | + if ($uid !== $user->getUID()) { |
|
| 1497 | + throw new \Exception('Cannot update share of a different user'); |
|
| 1498 | + } |
|
| 1499 | + |
|
| 1500 | + if ($password === '') { |
|
| 1501 | + $password = null; |
|
| 1502 | + } |
|
| 1503 | + |
|
| 1504 | + //If passwords are enforced the password can't be null |
|
| 1505 | + if (self::enforcePassword($config) && is_null($password)) { |
|
| 1506 | + throw new \Exception('Cannot remove password'); |
|
| 1507 | + } |
|
| 1508 | + |
|
| 1509 | + self::verifyPassword($password); |
|
| 1510 | + |
|
| 1511 | + $qb = $connection->getQueryBuilder(); |
|
| 1512 | + $qb->update('share') |
|
| 1513 | + ->set('share_with', $qb->createParameter('pass')) |
|
| 1514 | + ->where($qb->expr()->eq('id', $qb->createParameter('shareId'))) |
|
| 1515 | + ->setParameter(':pass', is_null($password) ? null : \OC::$server->getHasher()->hash($password)) |
|
| 1516 | + ->setParameter(':shareId', $shareId); |
|
| 1517 | + |
|
| 1518 | + $qb->execute(); |
|
| 1519 | + |
|
| 1520 | + return true; |
|
| 1521 | + } |
|
| 1522 | + |
|
| 1523 | + /** |
|
| 1524 | + * Checks whether a share has expired, calls unshareItem() if yes. |
|
| 1525 | + * @param array $item Share data (usually database row) |
|
| 1526 | + * @return boolean True if item was expired, false otherwise. |
|
| 1527 | + */ |
|
| 1528 | + protected static function expireItem(array $item) { |
|
| 1529 | + |
|
| 1530 | + $result = false; |
|
| 1531 | + |
|
| 1532 | + // only use default expiration date for link shares |
|
| 1533 | + if ((int) $item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 1534 | + |
|
| 1535 | + // calculate expiration date |
|
| 1536 | + if (!empty($item['expiration'])) { |
|
| 1537 | + $userDefinedExpire = new \DateTime($item['expiration']); |
|
| 1538 | + $expires = $userDefinedExpire->getTimestamp(); |
|
| 1539 | + } else { |
|
| 1540 | + $expires = null; |
|
| 1541 | + } |
|
| 1542 | + |
|
| 1543 | + |
|
| 1544 | + // get default expiration settings |
|
| 1545 | + $defaultSettings = Helper::getDefaultExpireSetting(); |
|
| 1546 | + $expires = Helper::calculateExpireDate($defaultSettings, $item['stime'], $expires); |
|
| 1547 | + |
|
| 1548 | + |
|
| 1549 | + if (is_int($expires)) { |
|
| 1550 | + $now = time(); |
|
| 1551 | + if ($now > $expires) { |
|
| 1552 | + self::unshareItem($item); |
|
| 1553 | + $result = true; |
|
| 1554 | + } |
|
| 1555 | + } |
|
| 1556 | + } |
|
| 1557 | + return $result; |
|
| 1558 | + } |
|
| 1559 | + |
|
| 1560 | + /** |
|
| 1561 | + * Unshares a share given a share data array |
|
| 1562 | + * @param array $item Share data (usually database row) |
|
| 1563 | + * @param int $newParent parent ID |
|
| 1564 | + * @return null |
|
| 1565 | + */ |
|
| 1566 | + protected static function unshareItem(array $item, $newParent = null) { |
|
| 1567 | + |
|
| 1568 | + $shareType = (int)$item['share_type']; |
|
| 1569 | + $shareWith = null; |
|
| 1570 | + if ($shareType !== \OCP\Share::SHARE_TYPE_LINK) { |
|
| 1571 | + $shareWith = $item['share_with']; |
|
| 1572 | + } |
|
| 1573 | + |
|
| 1574 | + // Pass all the vars we have for now, they may be useful |
|
| 1575 | + $hookParams = array( |
|
| 1576 | + 'id' => $item['id'], |
|
| 1577 | + 'itemType' => $item['item_type'], |
|
| 1578 | + 'itemSource' => $item['item_source'], |
|
| 1579 | + 'shareType' => $shareType, |
|
| 1580 | + 'shareWith' => $shareWith, |
|
| 1581 | + 'itemParent' => $item['parent'], |
|
| 1582 | + 'uidOwner' => $item['uid_owner'], |
|
| 1583 | + ); |
|
| 1584 | + if($item['item_type'] === 'file' || $item['item_type'] === 'folder') { |
|
| 1585 | + $hookParams['fileSource'] = $item['file_source']; |
|
| 1586 | + $hookParams['fileTarget'] = $item['file_target']; |
|
| 1587 | + } |
|
| 1588 | + |
|
| 1589 | + \OC_Hook::emit('OCP\Share', 'pre_unshare', $hookParams); |
|
| 1590 | + $deletedShares = Helper::delete($item['id'], false, null, $newParent); |
|
| 1591 | + $deletedShares[] = $hookParams; |
|
| 1592 | + $hookParams['deletedShares'] = $deletedShares; |
|
| 1593 | + \OC_Hook::emit('OCP\Share', 'post_unshare', $hookParams); |
|
| 1594 | + if ((int)$item['share_type'] === \OCP\Share::SHARE_TYPE_REMOTE && \OC::$server->getUserSession()->getUser()) { |
|
| 1595 | + list(, $remote) = Helper::splitUserRemote($item['share_with']); |
|
| 1596 | + self::sendRemoteUnshare($remote, $item['id'], $item['token']); |
|
| 1597 | + } |
|
| 1598 | + } |
|
| 1599 | + |
|
| 1600 | + /** |
|
| 1601 | + * Get the backend class for the specified item type |
|
| 1602 | + * @param string $itemType |
|
| 1603 | + * @throws \Exception |
|
| 1604 | + * @return \OCP\Share_Backend |
|
| 1605 | + */ |
|
| 1606 | + public static function getBackend($itemType) { |
|
| 1607 | + $l = \OC::$server->getL10N('lib'); |
|
| 1608 | + if (isset(self::$backends[$itemType])) { |
|
| 1609 | + return self::$backends[$itemType]; |
|
| 1610 | + } else if (isset(self::$backendTypes[$itemType]['class'])) { |
|
| 1611 | + $class = self::$backendTypes[$itemType]['class']; |
|
| 1612 | + if (class_exists($class)) { |
|
| 1613 | + self::$backends[$itemType] = new $class; |
|
| 1614 | + if (!(self::$backends[$itemType] instanceof \OCP\Share_Backend)) { |
|
| 1615 | + $message = 'Sharing backend %s must implement the interface OCP\Share_Backend'; |
|
| 1616 | + $message_t = $l->t('Sharing backend %s must implement the interface OCP\Share_Backend', array($class)); |
|
| 1617 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1618 | + throw new \Exception($message_t); |
|
| 1619 | + } |
|
| 1620 | + return self::$backends[$itemType]; |
|
| 1621 | + } else { |
|
| 1622 | + $message = 'Sharing backend %s not found'; |
|
| 1623 | + $message_t = $l->t('Sharing backend %s not found', array($class)); |
|
| 1624 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $class), \OCP\Util::ERROR); |
|
| 1625 | + throw new \Exception($message_t); |
|
| 1626 | + } |
|
| 1627 | + } |
|
| 1628 | + $message = 'Sharing backend for %s not found'; |
|
| 1629 | + $message_t = $l->t('Sharing backend for %s not found', array($itemType)); |
|
| 1630 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemType), \OCP\Util::ERROR); |
|
| 1631 | + throw new \Exception($message_t); |
|
| 1632 | + } |
|
| 1633 | + |
|
| 1634 | + /** |
|
| 1635 | + * Check if resharing is allowed |
|
| 1636 | + * @return boolean true if allowed or false |
|
| 1637 | + * |
|
| 1638 | + * Resharing is allowed by default if not configured |
|
| 1639 | + */ |
|
| 1640 | + public static function isResharingAllowed() { |
|
| 1641 | + if (!isset(self::$isResharingAllowed)) { |
|
| 1642 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_resharing', 'yes') == 'yes') { |
|
| 1643 | + self::$isResharingAllowed = true; |
|
| 1644 | + } else { |
|
| 1645 | + self::$isResharingAllowed = false; |
|
| 1646 | + } |
|
| 1647 | + } |
|
| 1648 | + return self::$isResharingAllowed; |
|
| 1649 | + } |
|
| 1650 | + |
|
| 1651 | + /** |
|
| 1652 | + * Get a list of collection item types for the specified item type |
|
| 1653 | + * @param string $itemType |
|
| 1654 | + * @return array |
|
| 1655 | + */ |
|
| 1656 | + private static function getCollectionItemTypes($itemType) { |
|
| 1657 | + $collectionTypes = array($itemType); |
|
| 1658 | + foreach (self::$backendTypes as $type => $backend) { |
|
| 1659 | + if (in_array($backend['collectionOf'], $collectionTypes)) { |
|
| 1660 | + $collectionTypes[] = $type; |
|
| 1661 | + } |
|
| 1662 | + } |
|
| 1663 | + // TODO Add option for collections to be collection of themselves, only 'folder' does it now... |
|
| 1664 | + if (isset(self::$backendTypes[$itemType]) && (!self::getBackend($itemType) instanceof \OCP\Share_Backend_Collection || $itemType != 'folder')) { |
|
| 1665 | + unset($collectionTypes[0]); |
|
| 1666 | + } |
|
| 1667 | + // Return array if collections were found or the item type is a |
|
| 1668 | + // collection itself - collections can be inside collections |
|
| 1669 | + if (count($collectionTypes) > 0) { |
|
| 1670 | + return $collectionTypes; |
|
| 1671 | + } |
|
| 1672 | + return false; |
|
| 1673 | + } |
|
| 1674 | + |
|
| 1675 | + /** |
|
| 1676 | + * Get the owners of items shared with a user. |
|
| 1677 | + * |
|
| 1678 | + * @param string $user The user the items are shared with. |
|
| 1679 | + * @param string $type The type of the items shared with the user. |
|
| 1680 | + * @param boolean $includeCollections Include collection item types (optional) |
|
| 1681 | + * @param boolean $includeOwner include owner in the list of users the item is shared with (optional) |
|
| 1682 | + * @return array |
|
| 1683 | + */ |
|
| 1684 | + public static function getSharedItemsOwners($user, $type, $includeCollections = false, $includeOwner = false) { |
|
| 1685 | + // First, we find out if $type is part of a collection (and if that collection is part of |
|
| 1686 | + // another one and so on). |
|
| 1687 | + $collectionTypes = array(); |
|
| 1688 | + if (!$includeCollections || !$collectionTypes = self::getCollectionItemTypes($type)) { |
|
| 1689 | + $collectionTypes[] = $type; |
|
| 1690 | + } |
|
| 1691 | + |
|
| 1692 | + // Of these collection types, along with our original $type, we make a |
|
| 1693 | + // list of the ones for which a sharing backend has been registered. |
|
| 1694 | + // FIXME: Ideally, we wouldn't need to nest getItemsSharedWith in this loop but just call it |
|
| 1695 | + // with its $includeCollections parameter set to true. Unfortunately, this fails currently. |
|
| 1696 | + $allMaybeSharedItems = array(); |
|
| 1697 | + foreach ($collectionTypes as $collectionType) { |
|
| 1698 | + if (isset(self::$backends[$collectionType])) { |
|
| 1699 | + $allMaybeSharedItems[$collectionType] = self::getItemsSharedWithUser( |
|
| 1700 | + $collectionType, |
|
| 1701 | + $user, |
|
| 1702 | + self::FORMAT_NONE |
|
| 1703 | + ); |
|
| 1704 | + } |
|
| 1705 | + } |
|
| 1706 | + |
|
| 1707 | + $owners = array(); |
|
| 1708 | + if ($includeOwner) { |
|
| 1709 | + $owners[] = $user; |
|
| 1710 | + } |
|
| 1711 | + |
|
| 1712 | + // We take a look at all shared items of the given $type (or of the collections it is part of) |
|
| 1713 | + // and find out their owners. Then, we gather the tags for the original $type from all owners, |
|
| 1714 | + // and return them as elements of a list that look like "Tag (owner)". |
|
| 1715 | + foreach ($allMaybeSharedItems as $collectionType => $maybeSharedItems) { |
|
| 1716 | + foreach ($maybeSharedItems as $sharedItem) { |
|
| 1717 | + if (isset($sharedItem['id'])) { //workaround for https://github.com/owncloud/core/issues/2814 |
|
| 1718 | + $owners[] = $sharedItem['uid_owner']; |
|
| 1719 | + } |
|
| 1720 | + } |
|
| 1721 | + } |
|
| 1722 | + |
|
| 1723 | + return $owners; |
|
| 1724 | + } |
|
| 1725 | + |
|
| 1726 | + /** |
|
| 1727 | + * Get shared items from the database |
|
| 1728 | + * @param string $itemType |
|
| 1729 | + * @param string $item Item source or target (optional) |
|
| 1730 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, SHARE_TYPE_LINK, $shareTypeUserAndGroups, or $shareTypeGroupUserUnique |
|
| 1731 | + * @param string $shareWith User or group the item is being shared with |
|
| 1732 | + * @param string $uidOwner User that is the owner of shared items (optional) |
|
| 1733 | + * @param int $format Format to convert items to with formatItems() (optional) |
|
| 1734 | + * @param mixed $parameters to pass to formatItems() (optional) |
|
| 1735 | + * @param int $limit Number of items to return, -1 to return all matches (optional) |
|
| 1736 | + * @param boolean $includeCollections Include collection item types (optional) |
|
| 1737 | + * @param boolean $itemShareWithBySource (optional) |
|
| 1738 | + * @param boolean $checkExpireDate |
|
| 1739 | + * @return array |
|
| 1740 | + * |
|
| 1741 | + * See public functions getItem(s)... for parameter usage |
|
| 1742 | + * |
|
| 1743 | + */ |
|
| 1744 | + public static function getItems($itemType, $item = null, $shareType = null, $shareWith = null, |
|
| 1745 | + $uidOwner = null, $format = self::FORMAT_NONE, $parameters = null, $limit = -1, |
|
| 1746 | + $includeCollections = false, $itemShareWithBySource = false, $checkExpireDate = true) { |
|
| 1747 | + if (!self::isEnabled()) { |
|
| 1748 | + return array(); |
|
| 1749 | + } |
|
| 1750 | + $backend = self::getBackend($itemType); |
|
| 1751 | + $collectionTypes = false; |
|
| 1752 | + // Get filesystem root to add it to the file target and remove from the |
|
| 1753 | + // file source, match file_source with the file cache |
|
| 1754 | + if ($itemType == 'file' || $itemType == 'folder') { |
|
| 1755 | + if(!is_null($uidOwner)) { |
|
| 1756 | + $root = \OC\Files\Filesystem::getRoot(); |
|
| 1757 | + } else { |
|
| 1758 | + $root = ''; |
|
| 1759 | + } |
|
| 1760 | + $where = 'INNER JOIN `*PREFIX*filecache` ON `file_source` = `*PREFIX*filecache`.`fileid` '; |
|
| 1761 | + if (!isset($item)) { |
|
| 1762 | + $where .= ' AND `file_target` IS NOT NULL '; |
|
| 1763 | + } |
|
| 1764 | + $where .= 'INNER JOIN `*PREFIX*storages` ON `numeric_id` = `*PREFIX*filecache`.`storage` '; |
|
| 1765 | + $fileDependent = true; |
|
| 1766 | + $queryArgs = array(); |
|
| 1767 | + } else { |
|
| 1768 | + $fileDependent = false; |
|
| 1769 | + $root = ''; |
|
| 1770 | + $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1771 | + if ($includeCollections && !isset($item) && $collectionTypes) { |
|
| 1772 | + // If includeCollections is true, find collections of this item type, e.g. a music album contains songs |
|
| 1773 | + if (!in_array($itemType, $collectionTypes)) { |
|
| 1774 | + $itemTypes = array_merge(array($itemType), $collectionTypes); |
|
| 1775 | + } else { |
|
| 1776 | + $itemTypes = $collectionTypes; |
|
| 1777 | + } |
|
| 1778 | + $placeholders = join(',', array_fill(0, count($itemTypes), '?')); |
|
| 1779 | + $where = ' WHERE `item_type` IN ('.$placeholders.'))'; |
|
| 1780 | + $queryArgs = $itemTypes; |
|
| 1781 | + } else { |
|
| 1782 | + $where = ' WHERE `item_type` = ?'; |
|
| 1783 | + $queryArgs = array($itemType); |
|
| 1784 | + } |
|
| 1785 | + } |
|
| 1786 | + if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') !== 'yes') { |
|
| 1787 | + $where .= ' AND `share_type` != ?'; |
|
| 1788 | + $queryArgs[] = self::SHARE_TYPE_LINK; |
|
| 1789 | + } |
|
| 1790 | + if (isset($shareType)) { |
|
| 1791 | + // Include all user and group items |
|
| 1792 | + if ($shareType == self::$shareTypeUserAndGroups && isset($shareWith)) { |
|
| 1793 | + $where .= ' AND ((`share_type` in (?, ?) AND `share_with` = ?) '; |
|
| 1794 | + $queryArgs[] = self::SHARE_TYPE_USER; |
|
| 1795 | + $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1796 | + $queryArgs[] = $shareWith; |
|
| 1797 | + |
|
| 1798 | + $user = \OC::$server->getUserManager()->get($shareWith); |
|
| 1799 | + $groups = []; |
|
| 1800 | + if ($user) { |
|
| 1801 | + $groups = \OC::$server->getGroupManager()->getUserGroupIds($user); |
|
| 1802 | + } |
|
| 1803 | + if (!empty($groups)) { |
|
| 1804 | + $placeholders = join(',', array_fill(0, count($groups), '?')); |
|
| 1805 | + $where .= ' OR (`share_type` = ? AND `share_with` IN ('.$placeholders.')) '; |
|
| 1806 | + $queryArgs[] = self::SHARE_TYPE_GROUP; |
|
| 1807 | + $queryArgs = array_merge($queryArgs, $groups); |
|
| 1808 | + } |
|
| 1809 | + $where .= ')'; |
|
| 1810 | + // Don't include own group shares |
|
| 1811 | + $where .= ' AND `uid_owner` != ?'; |
|
| 1812 | + $queryArgs[] = $shareWith; |
|
| 1813 | + } else { |
|
| 1814 | + $where .= ' AND `share_type` = ?'; |
|
| 1815 | + $queryArgs[] = $shareType; |
|
| 1816 | + if (isset($shareWith)) { |
|
| 1817 | + $where .= ' AND `share_with` = ?'; |
|
| 1818 | + $queryArgs[] = $shareWith; |
|
| 1819 | + } |
|
| 1820 | + } |
|
| 1821 | + } |
|
| 1822 | + if (isset($uidOwner)) { |
|
| 1823 | + $where .= ' AND `uid_owner` = ?'; |
|
| 1824 | + $queryArgs[] = $uidOwner; |
|
| 1825 | + if (!isset($shareType)) { |
|
| 1826 | + // Prevent unique user targets for group shares from being selected |
|
| 1827 | + $where .= ' AND `share_type` != ?'; |
|
| 1828 | + $queryArgs[] = self::$shareTypeGroupUserUnique; |
|
| 1829 | + } |
|
| 1830 | + if ($fileDependent) { |
|
| 1831 | + $column = 'file_source'; |
|
| 1832 | + } else { |
|
| 1833 | + $column = 'item_source'; |
|
| 1834 | + } |
|
| 1835 | + } else { |
|
| 1836 | + if ($fileDependent) { |
|
| 1837 | + $column = 'file_target'; |
|
| 1838 | + } else { |
|
| 1839 | + $column = 'item_target'; |
|
| 1840 | + } |
|
| 1841 | + } |
|
| 1842 | + if (isset($item)) { |
|
| 1843 | + $collectionTypes = self::getCollectionItemTypes($itemType); |
|
| 1844 | + if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1845 | + $where .= ' AND ('; |
|
| 1846 | + } else { |
|
| 1847 | + $where .= ' AND'; |
|
| 1848 | + } |
|
| 1849 | + // If looking for own shared items, check item_source else check item_target |
|
| 1850 | + if (isset($uidOwner) || $itemShareWithBySource) { |
|
| 1851 | + // If item type is a file, file source needs to be checked in case the item was converted |
|
| 1852 | + if ($fileDependent) { |
|
| 1853 | + $where .= ' `file_source` = ?'; |
|
| 1854 | + $column = 'file_source'; |
|
| 1855 | + } else { |
|
| 1856 | + $where .= ' `item_source` = ?'; |
|
| 1857 | + $column = 'item_source'; |
|
| 1858 | + } |
|
| 1859 | + } else { |
|
| 1860 | + if ($fileDependent) { |
|
| 1861 | + $where .= ' `file_target` = ?'; |
|
| 1862 | + $item = \OC\Files\Filesystem::normalizePath($item); |
|
| 1863 | + } else { |
|
| 1864 | + $where .= ' `item_target` = ?'; |
|
| 1865 | + } |
|
| 1866 | + } |
|
| 1867 | + $queryArgs[] = $item; |
|
| 1868 | + if ($includeCollections && $collectionTypes && !in_array('folder', $collectionTypes)) { |
|
| 1869 | + $placeholders = join(',', array_fill(0, count($collectionTypes), '?')); |
|
| 1870 | + $where .= ' OR `item_type` IN ('.$placeholders.'))'; |
|
| 1871 | + $queryArgs = array_merge($queryArgs, $collectionTypes); |
|
| 1872 | + } |
|
| 1873 | + } |
|
| 1874 | + |
|
| 1875 | + if ($shareType == self::$shareTypeUserAndGroups && $limit === 1) { |
|
| 1876 | + // Make sure the unique user target is returned if it exists, |
|
| 1877 | + // unique targets should follow the group share in the database |
|
| 1878 | + // If the limit is not 1, the filtering can be done later |
|
| 1879 | + $where .= ' ORDER BY `*PREFIX*share`.`id` DESC'; |
|
| 1880 | + } else { |
|
| 1881 | + $where .= ' ORDER BY `*PREFIX*share`.`id` ASC'; |
|
| 1882 | + } |
|
| 1883 | + |
|
| 1884 | + if ($limit != -1 && !$includeCollections) { |
|
| 1885 | + // The limit must be at least 3, because filtering needs to be done |
|
| 1886 | + if ($limit < 3) { |
|
| 1887 | + $queryLimit = 3; |
|
| 1888 | + } else { |
|
| 1889 | + $queryLimit = $limit; |
|
| 1890 | + } |
|
| 1891 | + } else { |
|
| 1892 | + $queryLimit = null; |
|
| 1893 | + } |
|
| 1894 | + $select = self::createSelectStatement($format, $fileDependent, $uidOwner); |
|
| 1895 | + $root = strlen($root); |
|
| 1896 | + $query = \OC_DB::prepare('SELECT '.$select.' FROM `*PREFIX*share` '.$where, $queryLimit); |
|
| 1897 | + $result = $query->execute($queryArgs); |
|
| 1898 | + if ($result === false) { |
|
| 1899 | + \OCP\Util::writeLog('OCP\Share', |
|
| 1900 | + \OC_DB::getErrorMessage() . ', select=' . $select . ' where=', |
|
| 1901 | + \OCP\Util::ERROR); |
|
| 1902 | + } |
|
| 1903 | + $items = array(); |
|
| 1904 | + $targets = array(); |
|
| 1905 | + $switchedItems = array(); |
|
| 1906 | + $mounts = array(); |
|
| 1907 | + while ($row = $result->fetchRow()) { |
|
| 1908 | + self::transformDBResults($row); |
|
| 1909 | + // Filter out duplicate group shares for users with unique targets |
|
| 1910 | + if ($fileDependent && !self::isFileReachable($row['path'], $row['storage_id'])) { |
|
| 1911 | + continue; |
|
| 1912 | + } |
|
| 1913 | + if ($row['share_type'] == self::$shareTypeGroupUserUnique && isset($items[$row['parent']])) { |
|
| 1914 | + $row['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1915 | + $row['unique_name'] = true; // remember that we use a unique name for this user |
|
| 1916 | + $row['share_with'] = $items[$row['parent']]['share_with']; |
|
| 1917 | + // if the group share was unshared from the user we keep the permission, otherwise |
|
| 1918 | + // we take the permission from the parent because this is always the up-to-date |
|
| 1919 | + // permission for the group share |
|
| 1920 | + if ($row['permissions'] > 0) { |
|
| 1921 | + $row['permissions'] = $items[$row['parent']]['permissions']; |
|
| 1922 | + } |
|
| 1923 | + // Remove the parent group share |
|
| 1924 | + unset($items[$row['parent']]); |
|
| 1925 | + if ($row['permissions'] == 0) { |
|
| 1926 | + continue; |
|
| 1927 | + } |
|
| 1928 | + } else if (!isset($uidOwner)) { |
|
| 1929 | + // Check if the same target already exists |
|
| 1930 | + if (isset($targets[$row['id']])) { |
|
| 1931 | + // Check if the same owner shared with the user twice |
|
| 1932 | + // through a group and user share - this is allowed |
|
| 1933 | + $id = $targets[$row['id']]; |
|
| 1934 | + if (isset($items[$id]) && $items[$id]['uid_owner'] == $row['uid_owner']) { |
|
| 1935 | + // Switch to group share type to ensure resharing conditions aren't bypassed |
|
| 1936 | + if ($items[$id]['share_type'] != self::SHARE_TYPE_GROUP) { |
|
| 1937 | + $items[$id]['share_type'] = self::SHARE_TYPE_GROUP; |
|
| 1938 | + $items[$id]['share_with'] = $row['share_with']; |
|
| 1939 | + } |
|
| 1940 | + // Switch ids if sharing permission is granted on only |
|
| 1941 | + // one share to ensure correct parent is used if resharing |
|
| 1942 | + if (~(int)$items[$id]['permissions'] & \OCP\Constants::PERMISSION_SHARE |
|
| 1943 | + && (int)$row['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 1944 | + $items[$row['id']] = $items[$id]; |
|
| 1945 | + $switchedItems[$id] = $row['id']; |
|
| 1946 | + unset($items[$id]); |
|
| 1947 | + $id = $row['id']; |
|
| 1948 | + } |
|
| 1949 | + $items[$id]['permissions'] |= (int)$row['permissions']; |
|
| 1950 | + |
|
| 1951 | + } |
|
| 1952 | + continue; |
|
| 1953 | + } elseif (!empty($row['parent'])) { |
|
| 1954 | + $targets[$row['parent']] = $row['id']; |
|
| 1955 | + } |
|
| 1956 | + } |
|
| 1957 | + // Remove root from file source paths if retrieving own shared items |
|
| 1958 | + if (isset($uidOwner) && isset($row['path'])) { |
|
| 1959 | + if (isset($row['parent'])) { |
|
| 1960 | + $query = \OC_DB::prepare('SELECT `file_target` FROM `*PREFIX*share` WHERE `id` = ?'); |
|
| 1961 | + $parentResult = $query->execute(array($row['parent'])); |
|
| 1962 | + if ($result === false) { |
|
| 1963 | + \OCP\Util::writeLog('OCP\Share', 'Can\'t select parent: ' . |
|
| 1964 | + \OC_DB::getErrorMessage() . ', select=' . $select . ' where=' . $where, |
|
| 1965 | + \OCP\Util::ERROR); |
|
| 1966 | + } else { |
|
| 1967 | + $parentRow = $parentResult->fetchRow(); |
|
| 1968 | + $tmpPath = $parentRow['file_target']; |
|
| 1969 | + // find the right position where the row path continues from the target path |
|
| 1970 | + $pos = strrpos($row['path'], $parentRow['file_target']); |
|
| 1971 | + $subPath = substr($row['path'], $pos); |
|
| 1972 | + $splitPath = explode('/', $subPath); |
|
| 1973 | + foreach (array_slice($splitPath, 2) as $pathPart) { |
|
| 1974 | + $tmpPath = $tmpPath . '/' . $pathPart; |
|
| 1975 | + } |
|
| 1976 | + $row['path'] = $tmpPath; |
|
| 1977 | + } |
|
| 1978 | + } else { |
|
| 1979 | + if (!isset($mounts[$row['storage']])) { |
|
| 1980 | + $mountPoints = \OC\Files\Filesystem::getMountByNumericId($row['storage']); |
|
| 1981 | + if (is_array($mountPoints) && !empty($mountPoints)) { |
|
| 1982 | + $mounts[$row['storage']] = current($mountPoints); |
|
| 1983 | + } |
|
| 1984 | + } |
|
| 1985 | + if (!empty($mounts[$row['storage']])) { |
|
| 1986 | + $path = $mounts[$row['storage']]->getMountPoint().$row['path']; |
|
| 1987 | + $relPath = substr($path, $root); // path relative to data/user |
|
| 1988 | + $row['path'] = rtrim($relPath, '/'); |
|
| 1989 | + } |
|
| 1990 | + } |
|
| 1991 | + } |
|
| 1992 | + |
|
| 1993 | + if($checkExpireDate) { |
|
| 1994 | + if (self::expireItem($row)) { |
|
| 1995 | + continue; |
|
| 1996 | + } |
|
| 1997 | + } |
|
| 1998 | + // Check if resharing is allowed, if not remove share permission |
|
| 1999 | + if (isset($row['permissions']) && (!self::isResharingAllowed() | \OCP\Util::isSharingDisabledForUser())) { |
|
| 2000 | + $row['permissions'] &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 2001 | + } |
|
| 2002 | + // Add display names to result |
|
| 2003 | + $row['share_with_displayname'] = $row['share_with']; |
|
| 2004 | + if ( isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2005 | + $row['share_type'] === self::SHARE_TYPE_USER) { |
|
| 2006 | + $row['share_with_displayname'] = \OCP\User::getDisplayName($row['share_with']); |
|
| 2007 | + } else if(isset($row['share_with']) && $row['share_with'] != '' && |
|
| 2008 | + $row['share_type'] === self::SHARE_TYPE_REMOTE) { |
|
| 2009 | + $addressBookEntries = \OC::$server->getContactsManager()->search($row['share_with'], ['CLOUD']); |
|
| 2010 | + foreach ($addressBookEntries as $entry) { |
|
| 2011 | + foreach ($entry['CLOUD'] as $cloudID) { |
|
| 2012 | + if ($cloudID === $row['share_with']) { |
|
| 2013 | + $row['share_with_displayname'] = $entry['FN']; |
|
| 2014 | + } |
|
| 2015 | + } |
|
| 2016 | + } |
|
| 2017 | + } |
|
| 2018 | + if ( isset($row['uid_owner']) && $row['uid_owner'] != '') { |
|
| 2019 | + $row['displayname_owner'] = \OCP\User::getDisplayName($row['uid_owner']); |
|
| 2020 | + } |
|
| 2021 | + |
|
| 2022 | + if ($row['permissions'] > 0) { |
|
| 2023 | + $items[$row['id']] = $row; |
|
| 2024 | + } |
|
| 2025 | + |
|
| 2026 | + } |
|
| 2027 | + |
|
| 2028 | + // group items if we are looking for items shared with the current user |
|
| 2029 | + if (isset($shareWith) && $shareWith === \OCP\User::getUser()) { |
|
| 2030 | + $items = self::groupItems($items, $itemType); |
|
| 2031 | + } |
|
| 2032 | + |
|
| 2033 | + if (!empty($items)) { |
|
| 2034 | + $collectionItems = array(); |
|
| 2035 | + foreach ($items as &$row) { |
|
| 2036 | + // Return only the item instead of a 2-dimensional array |
|
| 2037 | + if ($limit == 1 && $row[$column] == $item && ($row['item_type'] == $itemType || $itemType == 'file')) { |
|
| 2038 | + if ($format == self::FORMAT_NONE) { |
|
| 2039 | + return $row; |
|
| 2040 | + } else { |
|
| 2041 | + break; |
|
| 2042 | + } |
|
| 2043 | + } |
|
| 2044 | + // Check if this is a collection of the requested item type |
|
| 2045 | + if ($includeCollections && $collectionTypes && $row['item_type'] !== 'folder' && in_array($row['item_type'], $collectionTypes)) { |
|
| 2046 | + if (($collectionBackend = self::getBackend($row['item_type'])) |
|
| 2047 | + && $collectionBackend instanceof \OCP\Share_Backend_Collection) { |
|
| 2048 | + // Collections can be inside collections, check if the item is a collection |
|
| 2049 | + if (isset($item) && $row['item_type'] == $itemType && $row[$column] == $item) { |
|
| 2050 | + $collectionItems[] = $row; |
|
| 2051 | + } else { |
|
| 2052 | + $collection = array(); |
|
| 2053 | + $collection['item_type'] = $row['item_type']; |
|
| 2054 | + if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2055 | + $collection['path'] = basename($row['path']); |
|
| 2056 | + } |
|
| 2057 | + $row['collection'] = $collection; |
|
| 2058 | + // Fetch all of the children sources |
|
| 2059 | + $children = $collectionBackend->getChildren($row[$column]); |
|
| 2060 | + foreach ($children as $child) { |
|
| 2061 | + $childItem = $row; |
|
| 2062 | + $childItem['item_type'] = $itemType; |
|
| 2063 | + if ($row['item_type'] != 'file' && $row['item_type'] != 'folder') { |
|
| 2064 | + $childItem['item_source'] = $child['source']; |
|
| 2065 | + $childItem['item_target'] = $child['target']; |
|
| 2066 | + } |
|
| 2067 | + if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2068 | + if ($row['item_type'] == 'file' || $row['item_type'] == 'folder') { |
|
| 2069 | + $childItem['file_source'] = $child['source']; |
|
| 2070 | + } else { // TODO is this really needed if we already know that we use the file backend? |
|
| 2071 | + $meta = \OC\Files\Filesystem::getFileInfo($child['file_path']); |
|
| 2072 | + $childItem['file_source'] = $meta['fileid']; |
|
| 2073 | + } |
|
| 2074 | + $childItem['file_target'] = |
|
| 2075 | + \OC\Files\Filesystem::normalizePath($child['file_path']); |
|
| 2076 | + } |
|
| 2077 | + if (isset($item)) { |
|
| 2078 | + if ($childItem[$column] == $item) { |
|
| 2079 | + // Return only the item instead of a 2-dimensional array |
|
| 2080 | + if ($limit == 1) { |
|
| 2081 | + if ($format == self::FORMAT_NONE) { |
|
| 2082 | + return $childItem; |
|
| 2083 | + } else { |
|
| 2084 | + // Unset the items array and break out of both loops |
|
| 2085 | + $items = array(); |
|
| 2086 | + $items[] = $childItem; |
|
| 2087 | + break 2; |
|
| 2088 | + } |
|
| 2089 | + } else { |
|
| 2090 | + $collectionItems[] = $childItem; |
|
| 2091 | + } |
|
| 2092 | + } |
|
| 2093 | + } else { |
|
| 2094 | + $collectionItems[] = $childItem; |
|
| 2095 | + } |
|
| 2096 | + } |
|
| 2097 | + } |
|
| 2098 | + } |
|
| 2099 | + // Remove collection item |
|
| 2100 | + $toRemove = $row['id']; |
|
| 2101 | + if (array_key_exists($toRemove, $switchedItems)) { |
|
| 2102 | + $toRemove = $switchedItems[$toRemove]; |
|
| 2103 | + } |
|
| 2104 | + unset($items[$toRemove]); |
|
| 2105 | + } elseif ($includeCollections && $collectionTypes && in_array($row['item_type'], $collectionTypes)) { |
|
| 2106 | + // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2107 | + // see github issue #10588 for more details |
|
| 2108 | + // Need to find a solution which works for all back-ends |
|
| 2109 | + $collectionBackend = self::getBackend($row['item_type']); |
|
| 2110 | + $sharedParents = $collectionBackend->getParents($row['item_source']); |
|
| 2111 | + foreach ($sharedParents as $parent) { |
|
| 2112 | + $collectionItems[] = $parent; |
|
| 2113 | + } |
|
| 2114 | + } |
|
| 2115 | + } |
|
| 2116 | + if (!empty($collectionItems)) { |
|
| 2117 | + $collectionItems = array_unique($collectionItems, SORT_REGULAR); |
|
| 2118 | + $items = array_merge($items, $collectionItems); |
|
| 2119 | + } |
|
| 2120 | + |
|
| 2121 | + // filter out invalid items, these can appear when subshare entries exist |
|
| 2122 | + // for a group in which the requested user isn't a member any more |
|
| 2123 | + $items = array_filter($items, function($item) { |
|
| 2124 | + return $item['share_type'] !== self::$shareTypeGroupUserUnique; |
|
| 2125 | + }); |
|
| 2126 | + |
|
| 2127 | + return self::formatResult($items, $column, $backend, $format, $parameters); |
|
| 2128 | + } elseif ($includeCollections && $collectionTypes && in_array('folder', $collectionTypes)) { |
|
| 2129 | + // FIXME: Thats a dirty hack to improve file sharing performance, |
|
| 2130 | + // see github issue #10588 for more details |
|
| 2131 | + // Need to find a solution which works for all back-ends |
|
| 2132 | + $collectionItems = array(); |
|
| 2133 | + $collectionBackend = self::getBackend('folder'); |
|
| 2134 | + $sharedParents = $collectionBackend->getParents($item, $shareWith, $uidOwner); |
|
| 2135 | + foreach ($sharedParents as $parent) { |
|
| 2136 | + $collectionItems[] = $parent; |
|
| 2137 | + } |
|
| 2138 | + if ($limit === 1) { |
|
| 2139 | + return reset($collectionItems); |
|
| 2140 | + } |
|
| 2141 | + return self::formatResult($collectionItems, $column, $backend, $format, $parameters); |
|
| 2142 | + } |
|
| 2143 | + |
|
| 2144 | + return array(); |
|
| 2145 | + } |
|
| 2146 | + |
|
| 2147 | + /** |
|
| 2148 | + * group items with link to the same source |
|
| 2149 | + * |
|
| 2150 | + * @param array $items |
|
| 2151 | + * @param string $itemType |
|
| 2152 | + * @return array of grouped items |
|
| 2153 | + */ |
|
| 2154 | + protected static function groupItems($items, $itemType) { |
|
| 2155 | + |
|
| 2156 | + $fileSharing = ($itemType === 'file' || $itemType === 'folder') ? true : false; |
|
| 2157 | + |
|
| 2158 | + $result = array(); |
|
| 2159 | + |
|
| 2160 | + foreach ($items as $item) { |
|
| 2161 | + $grouped = false; |
|
| 2162 | + foreach ($result as $key => $r) { |
|
| 2163 | + // for file/folder shares we need to compare file_source, otherwise we compare item_source |
|
| 2164 | + // only group shares if they already point to the same target, otherwise the file where shared |
|
| 2165 | + // before grouping of shares was added. In this case we don't group them toi avoid confusions |
|
| 2166 | + if (( $fileSharing && $item['file_source'] === $r['file_source'] && $item['file_target'] === $r['file_target']) || |
|
| 2167 | + (!$fileSharing && $item['item_source'] === $r['item_source'] && $item['item_target'] === $r['item_target'])) { |
|
| 2168 | + // add the first item to the list of grouped shares |
|
| 2169 | + if (!isset($result[$key]['grouped'])) { |
|
| 2170 | + $result[$key]['grouped'][] = $result[$key]; |
|
| 2171 | + } |
|
| 2172 | + $result[$key]['permissions'] = (int) $item['permissions'] | (int) $r['permissions']; |
|
| 2173 | + $result[$key]['grouped'][] = $item; |
|
| 2174 | + $grouped = true; |
|
| 2175 | + break; |
|
| 2176 | + } |
|
| 2177 | + } |
|
| 2178 | + |
|
| 2179 | + if (!$grouped) { |
|
| 2180 | + $result[] = $item; |
|
| 2181 | + } |
|
| 2182 | + |
|
| 2183 | + } |
|
| 2184 | + |
|
| 2185 | + return $result; |
|
| 2186 | + } |
|
| 2187 | + |
|
| 2188 | + /** |
|
| 2189 | + * Put shared item into the database |
|
| 2190 | + * @param string $itemType Item type |
|
| 2191 | + * @param string $itemSource Item source |
|
| 2192 | + * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK |
|
| 2193 | + * @param string $shareWith User or group the item is being shared with |
|
| 2194 | + * @param string $uidOwner User that is the owner of shared item |
|
| 2195 | + * @param int $permissions CRUDS permissions |
|
| 2196 | + * @param boolean|array $parentFolder Parent folder target (optional) |
|
| 2197 | + * @param string $token (optional) |
|
| 2198 | + * @param string $itemSourceName name of the source item (optional) |
|
| 2199 | + * @param \DateTime $expirationDate (optional) |
|
| 2200 | + * @throws \Exception |
|
| 2201 | + * @return mixed id of the new share or false |
|
| 2202 | + */ |
|
| 2203 | + private static function put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2204 | + $permissions, $parentFolder = null, $token = null, $itemSourceName = null, \DateTime $expirationDate = null) { |
|
| 2205 | + |
|
| 2206 | + $queriesToExecute = array(); |
|
| 2207 | + $suggestedItemTarget = null; |
|
| 2208 | + $groupFileTarget = $fileTarget = $suggestedFileTarget = $filePath = ''; |
|
| 2209 | + $groupItemTarget = $itemTarget = $fileSource = $parent = 0; |
|
| 2210 | + |
|
| 2211 | + $result = self::checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate); |
|
| 2212 | + if(!empty($result)) { |
|
| 2213 | + $parent = $result['parent']; |
|
| 2214 | + $itemSource = $result['itemSource']; |
|
| 2215 | + $fileSource = $result['fileSource']; |
|
| 2216 | + $suggestedItemTarget = $result['suggestedItemTarget']; |
|
| 2217 | + $suggestedFileTarget = $result['suggestedFileTarget']; |
|
| 2218 | + $filePath = $result['filePath']; |
|
| 2219 | + } |
|
| 2220 | + |
|
| 2221 | + $isGroupShare = false; |
|
| 2222 | + if ($shareType == self::SHARE_TYPE_GROUP) { |
|
| 2223 | + $isGroupShare = true; |
|
| 2224 | + if (isset($shareWith['users'])) { |
|
| 2225 | + $users = $shareWith['users']; |
|
| 2226 | + } else { |
|
| 2227 | + $group = \OC::$server->getGroupManager()->get($shareWith['group']); |
|
| 2228 | + if ($group) { |
|
| 2229 | + $users = $group->searchUsers('', -1, 0); |
|
| 2230 | + $userIds = []; |
|
| 2231 | + foreach ($users as $user) { |
|
| 2232 | + $userIds[] = $user->getUID(); |
|
| 2233 | + } |
|
| 2234 | + $users = $userIds; |
|
| 2235 | + } else { |
|
| 2236 | + $users = []; |
|
| 2237 | + } |
|
| 2238 | + } |
|
| 2239 | + // remove current user from list |
|
| 2240 | + if (in_array(\OCP\User::getUser(), $users)) { |
|
| 2241 | + unset($users[array_search(\OCP\User::getUser(), $users)]); |
|
| 2242 | + } |
|
| 2243 | + $groupItemTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2244 | + $shareType, $shareWith['group'], $uidOwner, $suggestedItemTarget); |
|
| 2245 | + $groupFileTarget = Helper::generateTarget($itemType, $itemSource, |
|
| 2246 | + $shareType, $shareWith['group'], $uidOwner, $filePath); |
|
| 2247 | + |
|
| 2248 | + // add group share to table and remember the id as parent |
|
| 2249 | + $queriesToExecute['groupShare'] = array( |
|
| 2250 | + 'itemType' => $itemType, |
|
| 2251 | + 'itemSource' => $itemSource, |
|
| 2252 | + 'itemTarget' => $groupItemTarget, |
|
| 2253 | + 'shareType' => $shareType, |
|
| 2254 | + 'shareWith' => $shareWith['group'], |
|
| 2255 | + 'uidOwner' => $uidOwner, |
|
| 2256 | + 'permissions' => $permissions, |
|
| 2257 | + 'shareTime' => time(), |
|
| 2258 | + 'fileSource' => $fileSource, |
|
| 2259 | + 'fileTarget' => $groupFileTarget, |
|
| 2260 | + 'token' => $token, |
|
| 2261 | + 'parent' => $parent, |
|
| 2262 | + 'expiration' => $expirationDate, |
|
| 2263 | + ); |
|
| 2264 | + |
|
| 2265 | + } else { |
|
| 2266 | + $users = array($shareWith); |
|
| 2267 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, $shareType, $shareWith, $uidOwner, |
|
| 2268 | + $suggestedItemTarget); |
|
| 2269 | + } |
|
| 2270 | + |
|
| 2271 | + $run = true; |
|
| 2272 | + $error = ''; |
|
| 2273 | + $preHookData = array( |
|
| 2274 | + 'itemType' => $itemType, |
|
| 2275 | + 'itemSource' => $itemSource, |
|
| 2276 | + 'shareType' => $shareType, |
|
| 2277 | + 'uidOwner' => $uidOwner, |
|
| 2278 | + 'permissions' => $permissions, |
|
| 2279 | + 'fileSource' => $fileSource, |
|
| 2280 | + 'expiration' => $expirationDate, |
|
| 2281 | + 'token' => $token, |
|
| 2282 | + 'run' => &$run, |
|
| 2283 | + 'error' => &$error |
|
| 2284 | + ); |
|
| 2285 | + |
|
| 2286 | + $preHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2287 | + $preHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2288 | + |
|
| 2289 | + \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); |
|
| 2290 | + |
|
| 2291 | + if ($run === false) { |
|
| 2292 | + throw new \Exception($error); |
|
| 2293 | + } |
|
| 2294 | + |
|
| 2295 | + foreach ($users as $user) { |
|
| 2296 | + $sourceId = ($itemType === 'file' || $itemType === 'folder') ? $fileSource : $itemSource; |
|
| 2297 | + $sourceExists = self::getItemSharedWithBySource($itemType, $sourceId, self::FORMAT_NONE, null, true, $user); |
|
| 2298 | + |
|
| 2299 | + $userShareType = ($isGroupShare) ? self::$shareTypeGroupUserUnique : $shareType; |
|
| 2300 | + |
|
| 2301 | + if ($sourceExists && $sourceExists['item_source'] === $itemSource) { |
|
| 2302 | + $fileTarget = $sourceExists['file_target']; |
|
| 2303 | + $itemTarget = $sourceExists['item_target']; |
|
| 2304 | + |
|
| 2305 | + // for group shares we don't need a additional entry if the target is the same |
|
| 2306 | + if($isGroupShare && $groupItemTarget === $itemTarget) { |
|
| 2307 | + continue; |
|
| 2308 | + } |
|
| 2309 | + |
|
| 2310 | + } elseif(!$sourceExists && !$isGroupShare) { |
|
| 2311 | + |
|
| 2312 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, $userShareType, $user, |
|
| 2313 | + $uidOwner, $suggestedItemTarget, $parent); |
|
| 2314 | + if (isset($fileSource)) { |
|
| 2315 | + if ($parentFolder) { |
|
| 2316 | + if ($parentFolder === true) { |
|
| 2317 | + $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, $user, |
|
| 2318 | + $uidOwner, $suggestedFileTarget, $parent); |
|
| 2319 | + if ($fileTarget != $groupFileTarget) { |
|
| 2320 | + $parentFolders[$user]['folder'] = $fileTarget; |
|
| 2321 | + } |
|
| 2322 | + } else if (isset($parentFolder[$user])) { |
|
| 2323 | + $fileTarget = $parentFolder[$user]['folder'].$itemSource; |
|
| 2324 | + $parent = $parentFolder[$user]['id']; |
|
| 2325 | + } |
|
| 2326 | + } else { |
|
| 2327 | + $fileTarget = Helper::generateTarget('file', $filePath, $userShareType, |
|
| 2328 | + $user, $uidOwner, $suggestedFileTarget, $parent); |
|
| 2329 | + } |
|
| 2330 | + } else { |
|
| 2331 | + $fileTarget = null; |
|
| 2332 | + } |
|
| 2333 | + |
|
| 2334 | + } else { |
|
| 2335 | + |
|
| 2336 | + // group share which doesn't exists until now, check if we need a unique target for this user |
|
| 2337 | + |
|
| 2338 | + $itemTarget = Helper::generateTarget($itemType, $itemSource, self::SHARE_TYPE_USER, $user, |
|
| 2339 | + $uidOwner, $suggestedItemTarget, $parent); |
|
| 2340 | + |
|
| 2341 | + // do we also need a file target |
|
| 2342 | + if (isset($fileSource)) { |
|
| 2343 | + $fileTarget = Helper::generateTarget('file', $filePath, self::SHARE_TYPE_USER, $user, |
|
| 2344 | + $uidOwner, $suggestedFileTarget, $parent); |
|
| 2345 | + } else { |
|
| 2346 | + $fileTarget = null; |
|
| 2347 | + } |
|
| 2348 | + |
|
| 2349 | + if (($itemTarget === $groupItemTarget) && |
|
| 2350 | + (!isset($fileSource) || $fileTarget === $groupFileTarget)) { |
|
| 2351 | + continue; |
|
| 2352 | + } |
|
| 2353 | + } |
|
| 2354 | + |
|
| 2355 | + $queriesToExecute[] = array( |
|
| 2356 | + 'itemType' => $itemType, |
|
| 2357 | + 'itemSource' => $itemSource, |
|
| 2358 | + 'itemTarget' => $itemTarget, |
|
| 2359 | + 'shareType' => $userShareType, |
|
| 2360 | + 'shareWith' => $user, |
|
| 2361 | + 'uidOwner' => $uidOwner, |
|
| 2362 | + 'permissions' => $permissions, |
|
| 2363 | + 'shareTime' => time(), |
|
| 2364 | + 'fileSource' => $fileSource, |
|
| 2365 | + 'fileTarget' => $fileTarget, |
|
| 2366 | + 'token' => $token, |
|
| 2367 | + 'parent' => $parent, |
|
| 2368 | + 'expiration' => $expirationDate, |
|
| 2369 | + ); |
|
| 2370 | + |
|
| 2371 | + } |
|
| 2372 | + |
|
| 2373 | + $id = false; |
|
| 2374 | + if ($isGroupShare) { |
|
| 2375 | + $id = self::insertShare($queriesToExecute['groupShare']); |
|
| 2376 | + // Save this id, any extra rows for this group share will need to reference it |
|
| 2377 | + $parent = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2378 | + unset($queriesToExecute['groupShare']); |
|
| 2379 | + } |
|
| 2380 | + |
|
| 2381 | + foreach ($queriesToExecute as $shareQuery) { |
|
| 2382 | + $shareQuery['parent'] = $parent; |
|
| 2383 | + $id = self::insertShare($shareQuery); |
|
| 2384 | + } |
|
| 2385 | + |
|
| 2386 | + $postHookData = array( |
|
| 2387 | + 'itemType' => $itemType, |
|
| 2388 | + 'itemSource' => $itemSource, |
|
| 2389 | + 'parent' => $parent, |
|
| 2390 | + 'shareType' => $shareType, |
|
| 2391 | + 'uidOwner' => $uidOwner, |
|
| 2392 | + 'permissions' => $permissions, |
|
| 2393 | + 'fileSource' => $fileSource, |
|
| 2394 | + 'id' => $parent, |
|
| 2395 | + 'token' => $token, |
|
| 2396 | + 'expirationDate' => $expirationDate, |
|
| 2397 | + ); |
|
| 2398 | + |
|
| 2399 | + $postHookData['shareWith'] = ($isGroupShare) ? $shareWith['group'] : $shareWith; |
|
| 2400 | + $postHookData['itemTarget'] = ($isGroupShare) ? $groupItemTarget : $itemTarget; |
|
| 2401 | + $postHookData['fileTarget'] = ($isGroupShare) ? $groupFileTarget : $fileTarget; |
|
| 2402 | + |
|
| 2403 | + \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); |
|
| 2404 | + |
|
| 2405 | + |
|
| 2406 | + return $id ? $id : false; |
|
| 2407 | + } |
|
| 2408 | + |
|
| 2409 | + /** |
|
| 2410 | + * @param string $itemType |
|
| 2411 | + * @param string $itemSource |
|
| 2412 | + * @param int $shareType |
|
| 2413 | + * @param string $shareWith |
|
| 2414 | + * @param string $uidOwner |
|
| 2415 | + * @param int $permissions |
|
| 2416 | + * @param string|null $itemSourceName |
|
| 2417 | + * @param null|\DateTime $expirationDate |
|
| 2418 | + */ |
|
| 2419 | + private static function checkReshare($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, $itemSourceName, $expirationDate) { |
|
| 2420 | + $backend = self::getBackend($itemType); |
|
| 2421 | + |
|
| 2422 | + $l = \OC::$server->getL10N('lib'); |
|
| 2423 | + $result = array(); |
|
| 2424 | + |
|
| 2425 | + $column = ($itemType === 'file' || $itemType === 'folder') ? 'file_source' : 'item_source'; |
|
| 2426 | + |
|
| 2427 | + $checkReshare = self::getItemSharedWithBySource($itemType, $itemSource, self::FORMAT_NONE, null, true); |
|
| 2428 | + if ($checkReshare) { |
|
| 2429 | + // Check if attempting to share back to owner |
|
| 2430 | + if ($checkReshare['uid_owner'] == $shareWith && $shareType == self::SHARE_TYPE_USER) { |
|
| 2431 | + $message = 'Sharing %s failed, because the user %s is the original sharer'; |
|
| 2432 | + $message_t = $l->t('Sharing failed, because the user %s is the original sharer', [$shareWith]); |
|
| 2433 | + |
|
| 2434 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); |
|
| 2435 | + throw new \Exception($message_t); |
|
| 2436 | + } |
|
| 2437 | + } |
|
| 2438 | + |
|
| 2439 | + if ($checkReshare && $checkReshare['uid_owner'] !== \OC_User::getUser()) { |
|
| 2440 | + // Check if share permissions is granted |
|
| 2441 | + if (self::isResharingAllowed() && (int)$checkReshare['permissions'] & \OCP\Constants::PERMISSION_SHARE) { |
|
| 2442 | + if (~(int)$checkReshare['permissions'] & $permissions) { |
|
| 2443 | + $message = 'Sharing %s failed, because the permissions exceed permissions granted to %s'; |
|
| 2444 | + $message_t = $l->t('Sharing %s failed, because the permissions exceed permissions granted to %s', array($itemSourceName, $uidOwner)); |
|
| 2445 | + |
|
| 2446 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName, $uidOwner), \OCP\Util::DEBUG); |
|
| 2447 | + throw new \Exception($message_t); |
|
| 2448 | + } else { |
|
| 2449 | + // TODO Don't check if inside folder |
|
| 2450 | + $result['parent'] = $checkReshare['id']; |
|
| 2451 | + |
|
| 2452 | + $result['expirationDate'] = $expirationDate; |
|
| 2453 | + // $checkReshare['expiration'] could be null and then is always less than any value |
|
| 2454 | + if(isset($checkReshare['expiration']) && $checkReshare['expiration'] < $expirationDate) { |
|
| 2455 | + $result['expirationDate'] = $checkReshare['expiration']; |
|
| 2456 | + } |
|
| 2457 | + |
|
| 2458 | + // only suggest the same name as new target if it is a reshare of the |
|
| 2459 | + // same file/folder and not the reshare of a child |
|
| 2460 | + if ($checkReshare[$column] === $itemSource) { |
|
| 2461 | + $result['filePath'] = $checkReshare['file_target']; |
|
| 2462 | + $result['itemSource'] = $checkReshare['item_source']; |
|
| 2463 | + $result['fileSource'] = $checkReshare['file_source']; |
|
| 2464 | + $result['suggestedItemTarget'] = $checkReshare['item_target']; |
|
| 2465 | + $result['suggestedFileTarget'] = $checkReshare['file_target']; |
|
| 2466 | + } else { |
|
| 2467 | + $result['filePath'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $backend->getFilePath($itemSource, $uidOwner) : null; |
|
| 2468 | + $result['suggestedItemTarget'] = null; |
|
| 2469 | + $result['suggestedFileTarget'] = null; |
|
| 2470 | + $result['itemSource'] = $itemSource; |
|
| 2471 | + $result['fileSource'] = ($backend instanceof \OCP\Share_Backend_File_Dependent) ? $itemSource : null; |
|
| 2472 | + } |
|
| 2473 | + } |
|
| 2474 | + } else { |
|
| 2475 | + $message = 'Sharing %s failed, because resharing is not allowed'; |
|
| 2476 | + $message_t = $l->t('Sharing %s failed, because resharing is not allowed', array($itemSourceName)); |
|
| 2477 | + |
|
| 2478 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); |
|
| 2479 | + throw new \Exception($message_t); |
|
| 2480 | + } |
|
| 2481 | + } else { |
|
| 2482 | + $result['parent'] = null; |
|
| 2483 | + $result['suggestedItemTarget'] = null; |
|
| 2484 | + $result['suggestedFileTarget'] = null; |
|
| 2485 | + $result['itemSource'] = $itemSource; |
|
| 2486 | + $result['expirationDate'] = $expirationDate; |
|
| 2487 | + if (!$backend->isValidSource($itemSource, $uidOwner)) { |
|
| 2488 | + $message = 'Sharing %s failed, because the sharing backend for ' |
|
| 2489 | + .'%s could not find its source'; |
|
| 2490 | + $message_t = $l->t('Sharing %s failed, because the sharing backend for %s could not find its source', array($itemSource, $itemType)); |
|
| 2491 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource, $itemType), \OCP\Util::DEBUG); |
|
| 2492 | + throw new \Exception($message_t); |
|
| 2493 | + } |
|
| 2494 | + if ($backend instanceof \OCP\Share_Backend_File_Dependent) { |
|
| 2495 | + $result['filePath'] = $backend->getFilePath($itemSource, $uidOwner); |
|
| 2496 | + if ($itemType == 'file' || $itemType == 'folder') { |
|
| 2497 | + $result['fileSource'] = $itemSource; |
|
| 2498 | + } else { |
|
| 2499 | + $meta = \OC\Files\Filesystem::getFileInfo($result['filePath']); |
|
| 2500 | + $result['fileSource'] = $meta['fileid']; |
|
| 2501 | + } |
|
| 2502 | + if ($result['fileSource'] == -1) { |
|
| 2503 | + $message = 'Sharing %s failed, because the file could not be found in the file cache'; |
|
| 2504 | + $message_t = $l->t('Sharing %s failed, because the file could not be found in the file cache', array($itemSource)); |
|
| 2505 | + |
|
| 2506 | + \OCP\Util::writeLog('OCP\Share', sprintf($message, $itemSource), \OCP\Util::DEBUG); |
|
| 2507 | + throw new \Exception($message_t); |
|
| 2508 | + } |
|
| 2509 | + } else { |
|
| 2510 | + $result['filePath'] = null; |
|
| 2511 | + $result['fileSource'] = null; |
|
| 2512 | + } |
|
| 2513 | + } |
|
| 2514 | + |
|
| 2515 | + return $result; |
|
| 2516 | + } |
|
| 2517 | + |
|
| 2518 | + /** |
|
| 2519 | + * |
|
| 2520 | + * @param array $shareData |
|
| 2521 | + * @return mixed false in case of a failure or the id of the new share |
|
| 2522 | + */ |
|
| 2523 | + private static function insertShare(array $shareData) { |
|
| 2524 | + |
|
| 2525 | + $query = \OC_DB::prepare('INSERT INTO `*PREFIX*share` (' |
|
| 2526 | + .' `item_type`, `item_source`, `item_target`, `share_type`,' |
|
| 2527 | + .' `share_with`, `uid_owner`, `permissions`, `stime`, `file_source`,' |
|
| 2528 | + .' `file_target`, `token`, `parent`, `expiration`) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)'); |
|
| 2529 | + $query->bindValue(1, $shareData['itemType']); |
|
| 2530 | + $query->bindValue(2, $shareData['itemSource']); |
|
| 2531 | + $query->bindValue(3, $shareData['itemTarget']); |
|
| 2532 | + $query->bindValue(4, $shareData['shareType']); |
|
| 2533 | + $query->bindValue(5, $shareData['shareWith']); |
|
| 2534 | + $query->bindValue(6, $shareData['uidOwner']); |
|
| 2535 | + $query->bindValue(7, $shareData['permissions']); |
|
| 2536 | + $query->bindValue(8, $shareData['shareTime']); |
|
| 2537 | + $query->bindValue(9, $shareData['fileSource']); |
|
| 2538 | + $query->bindValue(10, $shareData['fileTarget']); |
|
| 2539 | + $query->bindValue(11, $shareData['token']); |
|
| 2540 | + $query->bindValue(12, $shareData['parent']); |
|
| 2541 | + $query->bindValue(13, $shareData['expiration'], 'datetime'); |
|
| 2542 | + $result = $query->execute(); |
|
| 2543 | + |
|
| 2544 | + $id = false; |
|
| 2545 | + if ($result) { |
|
| 2546 | + $id = \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*share'); |
|
| 2547 | + } |
|
| 2548 | + |
|
| 2549 | + return $id; |
|
| 2550 | + |
|
| 2551 | + } |
|
| 2552 | + |
|
| 2553 | + /** |
|
| 2554 | + * Delete all shares with type SHARE_TYPE_LINK |
|
| 2555 | + */ |
|
| 2556 | + public static function removeAllLinkShares() { |
|
| 2557 | + // Delete any link shares |
|
| 2558 | + $query = \OC_DB::prepare('SELECT `id` FROM `*PREFIX*share` WHERE `share_type` = ?'); |
|
| 2559 | + $result = $query->execute(array(self::SHARE_TYPE_LINK)); |
|
| 2560 | + while ($item = $result->fetchRow()) { |
|
| 2561 | + Helper::delete($item['id']); |
|
| 2562 | + } |
|
| 2563 | + } |
|
| 2564 | + |
|
| 2565 | + /** |
|
| 2566 | + * In case a password protected link is not yet authenticated this function will return false |
|
| 2567 | + * |
|
| 2568 | + * @param array $linkItem |
|
| 2569 | + * @return boolean |
|
| 2570 | + */ |
|
| 2571 | + public static function checkPasswordProtectedShare(array $linkItem) { |
|
| 2572 | + if (!isset($linkItem['share_with'])) { |
|
| 2573 | + return true; |
|
| 2574 | + } |
|
| 2575 | + if (!isset($linkItem['share_type'])) { |
|
| 2576 | + return true; |
|
| 2577 | + } |
|
| 2578 | + if (!isset($linkItem['id'])) { |
|
| 2579 | + return true; |
|
| 2580 | + } |
|
| 2581 | + |
|
| 2582 | + if ($linkItem['share_type'] != \OCP\Share::SHARE_TYPE_LINK) { |
|
| 2583 | + return true; |
|
| 2584 | + } |
|
| 2585 | + |
|
| 2586 | + if ( \OC::$server->getSession()->exists('public_link_authenticated') |
|
| 2587 | + && \OC::$server->getSession()->get('public_link_authenticated') === (string)$linkItem['id'] ) { |
|
| 2588 | + return true; |
|
| 2589 | + } |
|
| 2590 | + |
|
| 2591 | + return false; |
|
| 2592 | + } |
|
| 2593 | + |
|
| 2594 | + /** |
|
| 2595 | + * construct select statement |
|
| 2596 | + * @param int $format |
|
| 2597 | + * @param boolean $fileDependent ist it a file/folder share or a generla share |
|
| 2598 | + * @param string $uidOwner |
|
| 2599 | + * @return string select statement |
|
| 2600 | + */ |
|
| 2601 | + private static function createSelectStatement($format, $fileDependent, $uidOwner = null) { |
|
| 2602 | + $select = '*'; |
|
| 2603 | + if ($format == self::FORMAT_STATUSES) { |
|
| 2604 | + if ($fileDependent) { |
|
| 2605 | + $select = '`*PREFIX*share`.`id`, `*PREFIX*share`.`parent`, `share_type`, `path`, `storage`, ' |
|
| 2606 | + . '`share_with`, `uid_owner` , `file_source`, `stime`, `*PREFIX*share`.`permissions`, ' |
|
| 2607 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2608 | + . '`uid_initiator`'; |
|
| 2609 | + } else { |
|
| 2610 | + $select = '`id`, `parent`, `share_type`, `share_with`, `uid_owner`, `item_source`, `stime`, `*PREFIX*share`.`permissions`'; |
|
| 2611 | + } |
|
| 2612 | + } else { |
|
| 2613 | + if (isset($uidOwner)) { |
|
| 2614 | + if ($fileDependent) { |
|
| 2615 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`,' |
|
| 2616 | + . ' `share_type`, `share_with`, `file_source`, `file_target`, `path`, `*PREFIX*share`.`permissions`, `stime`,' |
|
| 2617 | + . ' `expiration`, `token`, `storage`, `mail_send`, `uid_owner`, ' |
|
| 2618 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2619 | + } else { |
|
| 2620 | + $select = '`id`, `item_type`, `item_source`, `parent`, `share_type`, `share_with`, `*PREFIX*share`.`permissions`,' |
|
| 2621 | + . ' `stime`, `file_source`, `expiration`, `token`, `mail_send`, `uid_owner`'; |
|
| 2622 | + } |
|
| 2623 | + } else { |
|
| 2624 | + if ($fileDependent) { |
|
| 2625 | + if ($format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_GET_FOLDER_CONTENTS || $format == \OCA\Files_Sharing\ShareBackend\File::FORMAT_FILE_APP_ROOT) { |
|
| 2626 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `*PREFIX*share`.`parent`, `uid_owner`, ' |
|
| 2627 | + . '`share_type`, `share_with`, `file_source`, `path`, `file_target`, `stime`, ' |
|
| 2628 | + . '`*PREFIX*share`.`permissions`, `expiration`, `storage`, `*PREFIX*filecache`.`parent` as `file_parent`, ' |
|
| 2629 | + . '`name`, `mtime`, `mimetype`, `mimepart`, `size`, `encrypted`, `etag`, `mail_send`'; |
|
| 2630 | + } else { |
|
| 2631 | + $select = '`*PREFIX*share`.`id`, `item_type`, `item_source`, `item_target`,' |
|
| 2632 | + . '`*PREFIX*share`.`parent`, `share_type`, `share_with`, `uid_owner`,' |
|
| 2633 | + . '`file_source`, `path`, `file_target`, `*PREFIX*share`.`permissions`,' |
|
| 2634 | + . '`stime`, `expiration`, `token`, `storage`, `mail_send`,' |
|
| 2635 | + . '`*PREFIX*storages`.`id` AS `storage_id`, `*PREFIX*filecache`.`parent` as `file_parent`'; |
|
| 2636 | + } |
|
| 2637 | + } |
|
| 2638 | + } |
|
| 2639 | + } |
|
| 2640 | + return $select; |
|
| 2641 | + } |
|
| 2642 | + |
|
| 2643 | + |
|
| 2644 | + /** |
|
| 2645 | + * transform db results |
|
| 2646 | + * @param array $row result |
|
| 2647 | + */ |
|
| 2648 | + private static function transformDBResults(&$row) { |
|
| 2649 | + if (isset($row['id'])) { |
|
| 2650 | + $row['id'] = (int) $row['id']; |
|
| 2651 | + } |
|
| 2652 | + if (isset($row['share_type'])) { |
|
| 2653 | + $row['share_type'] = (int) $row['share_type']; |
|
| 2654 | + } |
|
| 2655 | + if (isset($row['parent'])) { |
|
| 2656 | + $row['parent'] = (int) $row['parent']; |
|
| 2657 | + } |
|
| 2658 | + if (isset($row['file_parent'])) { |
|
| 2659 | + $row['file_parent'] = (int) $row['file_parent']; |
|
| 2660 | + } |
|
| 2661 | + if (isset($row['file_source'])) { |
|
| 2662 | + $row['file_source'] = (int) $row['file_source']; |
|
| 2663 | + } |
|
| 2664 | + if (isset($row['permissions'])) { |
|
| 2665 | + $row['permissions'] = (int) $row['permissions']; |
|
| 2666 | + } |
|
| 2667 | + if (isset($row['storage'])) { |
|
| 2668 | + $row['storage'] = (int) $row['storage']; |
|
| 2669 | + } |
|
| 2670 | + if (isset($row['stime'])) { |
|
| 2671 | + $row['stime'] = (int) $row['stime']; |
|
| 2672 | + } |
|
| 2673 | + if (isset($row['expiration']) && $row['share_type'] !== self::SHARE_TYPE_LINK) { |
|
| 2674 | + // discard expiration date for non-link shares, which might have been |
|
| 2675 | + // set by ancient bugs |
|
| 2676 | + $row['expiration'] = null; |
|
| 2677 | + } |
|
| 2678 | + } |
|
| 2679 | + |
|
| 2680 | + /** |
|
| 2681 | + * format result |
|
| 2682 | + * @param array $items result |
|
| 2683 | + * @param string $column is it a file share or a general share ('file_target' or 'item_target') |
|
| 2684 | + * @param \OCP\Share_Backend $backend sharing backend |
|
| 2685 | + * @param int $format |
|
| 2686 | + * @param array $parameters additional format parameters |
|
| 2687 | + * @return array format result |
|
| 2688 | + */ |
|
| 2689 | + private static function formatResult($items, $column, $backend, $format = self::FORMAT_NONE , $parameters = null) { |
|
| 2690 | + if ($format === self::FORMAT_NONE) { |
|
| 2691 | + return $items; |
|
| 2692 | + } else if ($format === self::FORMAT_STATUSES) { |
|
| 2693 | + $statuses = array(); |
|
| 2694 | + foreach ($items as $item) { |
|
| 2695 | + if ($item['share_type'] === self::SHARE_TYPE_LINK) { |
|
| 2696 | + if ($item['uid_initiator'] !== \OC::$server->getUserSession()->getUser()->getUID()) { |
|
| 2697 | + continue; |
|
| 2698 | + } |
|
| 2699 | + $statuses[$item[$column]]['link'] = true; |
|
| 2700 | + } else if (!isset($statuses[$item[$column]])) { |
|
| 2701 | + $statuses[$item[$column]]['link'] = false; |
|
| 2702 | + } |
|
| 2703 | + if (!empty($item['file_target'])) { |
|
| 2704 | + $statuses[$item[$column]]['path'] = $item['path']; |
|
| 2705 | + } |
|
| 2706 | + } |
|
| 2707 | + return $statuses; |
|
| 2708 | + } else { |
|
| 2709 | + return $backend->formatItems($items, $format, $parameters); |
|
| 2710 | + } |
|
| 2711 | + } |
|
| 2712 | + |
|
| 2713 | + /** |
|
| 2714 | + * remove protocol from URL |
|
| 2715 | + * |
|
| 2716 | + * @param string $url |
|
| 2717 | + * @return string |
|
| 2718 | + */ |
|
| 2719 | + public static function removeProtocolFromUrl($url) { |
|
| 2720 | + if (strpos($url, 'https://') === 0) { |
|
| 2721 | + return substr($url, strlen('https://')); |
|
| 2722 | + } else if (strpos($url, 'http://') === 0) { |
|
| 2723 | + return substr($url, strlen('http://')); |
|
| 2724 | + } |
|
| 2725 | + |
|
| 2726 | + return $url; |
|
| 2727 | + } |
|
| 2728 | + |
|
| 2729 | + /** |
|
| 2730 | + * try http post first with https and then with http as a fallback |
|
| 2731 | + * |
|
| 2732 | + * @param string $remoteDomain |
|
| 2733 | + * @param string $urlSuffix |
|
| 2734 | + * @param array $fields post parameters |
|
| 2735 | + * @return array |
|
| 2736 | + */ |
|
| 2737 | + private static function tryHttpPostToShareEndpoint($remoteDomain, $urlSuffix, array $fields) { |
|
| 2738 | + $protocol = 'https://'; |
|
| 2739 | + $result = [ |
|
| 2740 | + 'success' => false, |
|
| 2741 | + 'result' => '', |
|
| 2742 | + ]; |
|
| 2743 | + $try = 0; |
|
| 2744 | + $discoveryService = \OC::$server->query(\OCP\OCS\IDiscoveryService::class); |
|
| 2745 | + while ($result['success'] === false && $try < 2) { |
|
| 2746 | + $federationEndpoints = $discoveryService->discover($protocol . $remoteDomain, 'FEDERATED_SHARING'); |
|
| 2747 | + $endpoint = isset($federationEndpoints['share']) ? $federationEndpoints['share'] : '/ocs/v2.php/cloud/shares'; |
|
| 2748 | + $result = \OC::$server->getHTTPHelper()->post($protocol . $remoteDomain . $endpoint . $urlSuffix . '?format=' . self::RESPONSE_FORMAT, $fields); |
|
| 2749 | + $try++; |
|
| 2750 | + $protocol = 'http://'; |
|
| 2751 | + } |
|
| 2752 | + |
|
| 2753 | + return $result; |
|
| 2754 | + } |
|
| 2755 | + |
|
| 2756 | + /** |
|
| 2757 | + * send server-to-server share to remote server |
|
| 2758 | + * |
|
| 2759 | + * @param string $token |
|
| 2760 | + * @param string $shareWith |
|
| 2761 | + * @param string $name |
|
| 2762 | + * @param int $remote_id |
|
| 2763 | + * @param string $owner |
|
| 2764 | + * @return bool |
|
| 2765 | + */ |
|
| 2766 | + private static function sendRemoteShare($token, $shareWith, $name, $remote_id, $owner) { |
|
| 2767 | + |
|
| 2768 | + list($user, $remote) = Helper::splitUserRemote($shareWith); |
|
| 2769 | + |
|
| 2770 | + if ($user && $remote) { |
|
| 2771 | + $url = $remote; |
|
| 2772 | + |
|
| 2773 | + $local = \OC::$server->getURLGenerator()->getAbsoluteURL('/'); |
|
| 2774 | + |
|
| 2775 | + $fields = array( |
|
| 2776 | + 'shareWith' => $user, |
|
| 2777 | + 'token' => $token, |
|
| 2778 | + 'name' => $name, |
|
| 2779 | + 'remoteId' => $remote_id, |
|
| 2780 | + 'owner' => $owner, |
|
| 2781 | + 'remote' => $local, |
|
| 2782 | + ); |
|
| 2783 | + |
|
| 2784 | + $url = self::removeProtocolFromUrl($url); |
|
| 2785 | + $result = self::tryHttpPostToShareEndpoint($url, '', $fields); |
|
| 2786 | + $status = json_decode($result['result'], true); |
|
| 2787 | + |
|
| 2788 | + if ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)) { |
|
| 2789 | + \OC_Hook::emit('OCP\Share', 'federated_share_added', ['server' => $remote]); |
|
| 2790 | + return true; |
|
| 2791 | + } |
|
| 2792 | + |
|
| 2793 | + } |
|
| 2794 | + |
|
| 2795 | + return false; |
|
| 2796 | + } |
|
| 2797 | + |
|
| 2798 | + /** |
|
| 2799 | + * send server-to-server unshare to remote server |
|
| 2800 | + * |
|
| 2801 | + * @param string $remote url |
|
| 2802 | + * @param int $id share id |
|
| 2803 | + * @param string $token |
|
| 2804 | + * @return bool |
|
| 2805 | + */ |
|
| 2806 | + private static function sendRemoteUnshare($remote, $id, $token) { |
|
| 2807 | + $url = rtrim($remote, '/'); |
|
| 2808 | + $fields = array('token' => $token, 'format' => 'json'); |
|
| 2809 | + $url = self::removeProtocolFromUrl($url); |
|
| 2810 | + $result = self::tryHttpPostToShareEndpoint($url, '/'.$id.'/unshare', $fields); |
|
| 2811 | + $status = json_decode($result['result'], true); |
|
| 2812 | + |
|
| 2813 | + return ($result['success'] && ($status['ocs']['meta']['statuscode'] === 100 || $status['ocs']['meta']['statuscode'] === 200)); |
|
| 2814 | + } |
|
| 2815 | + |
|
| 2816 | + /** |
|
| 2817 | + * check if user can only share with group members |
|
| 2818 | + * @return bool |
|
| 2819 | + */ |
|
| 2820 | + public static function shareWithGroupMembersOnly() { |
|
| 2821 | + $value = \OC::$server->getAppConfig()->getValue('core', 'shareapi_only_share_with_group_members', 'no'); |
|
| 2822 | + return ($value === 'yes') ? true : false; |
|
| 2823 | + } |
|
| 2824 | + |
|
| 2825 | + /** |
|
| 2826 | + * @return bool |
|
| 2827 | + */ |
|
| 2828 | + public static function isDefaultExpireDateEnabled() { |
|
| 2829 | + $defaultExpireDateEnabled = \OCP\Config::getAppValue('core', 'shareapi_default_expire_date', 'no'); |
|
| 2830 | + return ($defaultExpireDateEnabled === "yes") ? true : false; |
|
| 2831 | + } |
|
| 2832 | + |
|
| 2833 | + /** |
|
| 2834 | + * @return bool |
|
| 2835 | + */ |
|
| 2836 | + public static function enforceDefaultExpireDate() { |
|
| 2837 | + $enforceDefaultExpireDate = \OCP\Config::getAppValue('core', 'shareapi_enforce_expire_date', 'no'); |
|
| 2838 | + return ($enforceDefaultExpireDate === "yes") ? true : false; |
|
| 2839 | + } |
|
| 2840 | + |
|
| 2841 | + /** |
|
| 2842 | + * @return int |
|
| 2843 | + */ |
|
| 2844 | + public static function getExpireInterval() { |
|
| 2845 | + return (int)\OCP\Config::getAppValue('core', 'shareapi_expire_after_n_days', '7'); |
|
| 2846 | + } |
|
| 2847 | + |
|
| 2848 | + /** |
|
| 2849 | + * Checks whether the given path is reachable for the given owner |
|
| 2850 | + * |
|
| 2851 | + * @param string $path path relative to files |
|
| 2852 | + * @param string $ownerStorageId storage id of the owner |
|
| 2853 | + * |
|
| 2854 | + * @return boolean true if file is reachable, false otherwise |
|
| 2855 | + */ |
|
| 2856 | + private static function isFileReachable($path, $ownerStorageId) { |
|
| 2857 | + // if outside the home storage, file is always considered reachable |
|
| 2858 | + if (!(substr($ownerStorageId, 0, 6) === 'home::' || |
|
| 2859 | + substr($ownerStorageId, 0, 13) === 'object::user:' |
|
| 2860 | + )) { |
|
| 2861 | + return true; |
|
| 2862 | + } |
|
| 2863 | + |
|
| 2864 | + // if inside the home storage, the file has to be under "/files/" |
|
| 2865 | + $path = ltrim($path, '/'); |
|
| 2866 | + if (substr($path, 0, 6) === 'files/') { |
|
| 2867 | + return true; |
|
| 2868 | + } |
|
| 2869 | + |
|
| 2870 | + return false; |
|
| 2871 | + } |
|
| 2872 | + |
|
| 2873 | + /** |
|
| 2874 | + * @param IConfig $config |
|
| 2875 | + * @return bool |
|
| 2876 | + */ |
|
| 2877 | + public static function enforcePassword(IConfig $config) { |
|
| 2878 | + $enforcePassword = $config->getAppValue('core', 'shareapi_enforce_links_password', 'no'); |
|
| 2879 | + return ($enforcePassword === "yes") ? true : false; |
|
| 2880 | + } |
|
| 2881 | + |
|
| 2882 | + /** |
|
| 2883 | + * Get all share entries, including non-unique group items |
|
| 2884 | + * |
|
| 2885 | + * @param string $owner |
|
| 2886 | + * @return array |
|
| 2887 | + */ |
|
| 2888 | + public static function getAllSharesForOwner($owner) { |
|
| 2889 | + $query = 'SELECT * FROM `*PREFIX*share` WHERE `uid_owner` = ?'; |
|
| 2890 | + $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$owner]); |
|
| 2891 | + return $result->fetchAll(); |
|
| 2892 | + } |
|
| 2893 | + |
|
| 2894 | + /** |
|
| 2895 | + * Get all share entries, including non-unique group items for a file |
|
| 2896 | + * |
|
| 2897 | + * @param int $id |
|
| 2898 | + * @return array |
|
| 2899 | + */ |
|
| 2900 | + public static function getAllSharesForFileId($id) { |
|
| 2901 | + $query = 'SELECT * FROM `*PREFIX*share` WHERE `file_source` = ?'; |
|
| 2902 | + $result = \OC::$server->getDatabaseConnection()->executeQuery($query, [$id]); |
|
| 2903 | + return $result->fetchAll(); |
|
| 2904 | + } |
|
| 2905 | + |
|
| 2906 | + /** |
|
| 2907 | + * @param string $password |
|
| 2908 | + * @throws \Exception |
|
| 2909 | + */ |
|
| 2910 | + private static function verifyPassword($password) { |
|
| 2911 | + |
|
| 2912 | + $accepted = true; |
|
| 2913 | + $message = ''; |
|
| 2914 | + \OCP\Util::emitHook('\OC\Share', 'verifyPassword', [ |
|
| 2915 | + 'password' => $password, |
|
| 2916 | + 'accepted' => &$accepted, |
|
| 2917 | + 'message' => &$message |
|
| 2918 | + ]); |
|
| 2919 | + |
|
| 2920 | + if (!$accepted) { |
|
| 2921 | + throw new \Exception($message); |
|
| 2922 | + } |
|
| 2923 | + } |
|
| 2924 | 2924 | } |