public static function getByShareToken($token) { $linkItem = \OCP\Share::getShareByToken($token); if (is_array($linkItem) && isset($linkItem['uid_owner'])) { // seems to be a valid share $rootLinkItem = \OCP\Share::resolveReShare($linkItem); $fileOwner = $rootLinkItem['uid_owner']; } else { throw new \Exception('This file was probably unshared'); } if (!isset($rootLinkItem['path']) && isset($rootLinkItem['file_target'])) { $rootLinkItem['path'] = 'files/' . $rootLinkItem['file_target']; } $file = new File($rootLinkItem['file_source'], array($rootLinkItem)); if (isset($rootLinkItem['uid_owner'])) { \OC_Util::tearDownFS(); \OC_Util::setupFS($rootLinkItem['uid_owner']); $file->setOwner($rootLinkItem['uid_owner']); $file->setPath('/files' . \OC\Files\Filesystem::getPath($linkItem['file_source'])); } if (isset($linkItem['share_with']) && !empty($linkItem['share_with'])) { $file->setPasswordProtected(true); } return $file; }
/** * @brief list bookshelf contents * * @return array of FileInfo[], sorted by time added */ public static function get() { $files = array(); if ($bookshelf = json_decode(Config::get('bookshelf', ''), true)) { arsort($bookshelf); while (list($id, $time) = each($bookshelf)) { array_push($files, \OC\Files\Filesystem::getFileInfo(\OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($id)))); } } return $files; }
/** * hook after file was unshared * * @param array $params */ public function postUnshared($params) { if ($this->encryptionManager->isEnabled()) { if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { $path = Filesystem::getPath($params['fileSource']); list($owner, $ownerPath) = $this->getOwnerPath($path); $absPath = '/' . $owner . '/files/' . $ownerPath; $this->update($absPath); } } }
public function isValidSource($itemSource, $uidOwner) { $path = \OC\Files\Filesystem::getPath($itemSource); if ($path) { // FIXME: attributes should not be set here, // keeping this pattern for now to avoid unexpected // regressions $this->path = basename($path); return true; } return false; }
public function getFilePath($itemSource, $uidOwner) { if (isset($this->path)) { $path = $this->path; $this->path = null; return $path; } else { $path = \OC\Files\Filesystem::getPath($itemSource); if ($path) { return $path; } } return false; }
public function getFilePath($itemSource, $uidOwner) { if (isset($this->path)) { $path = $this->path; $this->path = null; return $path; } else { try { $path = \OC\Files\Filesystem::getPath($itemSource); return $path; } catch (\OCP\Files\NotFoundException $e) { return false; } } }
/** * Sets up the filesystem and user for public sharing * @param string $token string share token * @param string $relativePath optional path relative to the share * @param string $password optional password * @return array */ public static function setupFromToken($token, $relativePath = null, $password = null) { \OC_User::setIncognitoMode(true); $linkItem = \OCP\Share::getShareByToken($token, !$password); if ($linkItem === false || $linkItem['item_type'] !== 'file' && $linkItem['item_type'] !== 'folder') { \OC_Response::setStatus(404); \OCP\Util::writeLog('core-preview', 'Passed token parameter is not valid', \OCP\Util::DEBUG); exit; } if (!isset($linkItem['uid_owner']) || !isset($linkItem['file_source'])) { \OC_Response::setStatus(500); \OCP\Util::writeLog('core-preview', 'Passed token seems to be valid, but it does not contain all necessary information . ("' . $token . '")', \OCP\Util::WARN); exit; } $rootLinkItem = \OCP\Share::resolveReShare($linkItem); $path = null; if (isset($rootLinkItem['uid_owner'])) { \OCP\JSON::checkUserExists($rootLinkItem['uid_owner']); \OC_Util::tearDownFS(); \OC_Util::setupFS($rootLinkItem['uid_owner']); } try { $path = Filesystem::getPath($linkItem['file_source']); } catch (NotFoundException $e) { \OCP\Util::writeLog('share', 'could not resolve linkItem', \OCP\Util::DEBUG); \OC_Response::setStatus(404); \OCP\JSON::error(array('success' => false)); exit; } if (!isset($linkItem['item_type'])) { \OCP\Util::writeLog('share', 'No item type set for share id: ' . $linkItem['id'], \OCP\Util::ERROR); \OC_Response::setStatus(404); \OCP\JSON::error(array('success' => false)); exit; } if (isset($linkItem['share_with']) && (int) $linkItem['share_type'] === \OCP\Share::SHARE_TYPE_LINK) { if (!self::authenticate($linkItem, $password)) { \OC_Response::setStatus(403); \OCP\JSON::error(array('success' => false)); exit; } } $basePath = $path; if ($relativePath !== null && Filesystem::isReadable($basePath . $relativePath)) { $path .= Filesystem::normalizePath($relativePath); } return array('linkItem' => $linkItem, 'basePath' => $basePath, 'realPath' => $path); }
/** * update keyfiles and share keys recursively * * @param int $fileSource file source id */ private function update($fileSource) { $path = \OC\Files\Filesystem::getPath($fileSource); $info = \OC\Files\Filesystem::getFileInfo($path); $owner = \OC\Files\Filesystem::getOwner($path); $view = new \OC\Files\View('/' . $owner . '/files'); $ownerPath = $view->getPath($info->getId()); $absPath = '/' . $owner . '/files' . $ownerPath; $mount = $this->mountManager->find($path); $mountPoint = $mount->getMountPoint(); // if a folder was shared, get a list of all (sub-)folders if ($this->view->is_dir($absPath)) { $allFiles = $this->util->getAllFiles($absPath, $mountPoint); } else { $allFiles = array($absPath); } $encryptionModule = $this->encryptionManager->getDefaultEncryptionModule(); foreach ($allFiles as $path) { $usersSharing = $this->file->getAccessList($path); $encryptionModule->update($path, $this->uid, $usersSharing); } }
/** * Share an item with a user, group, or via private link * @param string $itemType * @param string $itemSource * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK * @param string $shareWith User or group the item is being shared with * @param int $permissions CRUDS * @param string $itemSourceName * @param \DateTime $expirationDate * @param bool $passwordChanged * @return boolean|string Returns true on success or false on failure, Returns token on success for links * @throws \OC\HintException when the share type is remote and the shareWith is invalid * @throws \Exception */ public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null, $passwordChanged = null) { $backend = self::getBackend($itemType); $l = \OC::$server->getL10N('lib'); if ($backend->isShareTypeAllowed($shareType) === false) { $message = 'Sharing %s failed, because the backend does not allow shares from type %i'; $message_t = $l->t('Sharing %s failed, because the backend does not allow shares from type %i', array($itemSourceName, $shareType)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareType), \OCP\Util::DEBUG); throw new \Exception($message_t); } $uidOwner = \OC_User::getUser(); $shareWithinGroupOnly = self::shareWithGroupMembersOnly(); if (is_null($itemSourceName)) { $itemSourceName = $itemSource; } $itemName = $itemSourceName; // check if file can be shared if ($itemType === 'file' or $itemType === 'folder') { $path = \OC\Files\Filesystem::getPath($itemSource); $itemName = $path; // verify that the file exists before we try to share it if (!$path) { $message = 'Sharing %s failed, because the file does not exist'; $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); throw new \Exception($message_t); } // verify that the user has share permission if (!\OC\Files\Filesystem::isSharable($path)) { $message = 'You are not allowed to share %s'; $message_t = $l->t('You are not allowed to share %s', [$path]); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $path), \OCP\Util::DEBUG); throw new \Exception($message_t); } } //verify that we don't share a folder which already contains a share mount point if ($itemType === 'folder') { $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/'; $mountManager = \OC\Files\Filesystem::getMountManager(); $mounts = $mountManager->findIn($path); foreach ($mounts as $mount) { if ($mount->getStorage()->instanceOfStorage('\\OCA\\Files_Sharing\\ISharedStorage')) { $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!'; \OCP\Util::writeLog('OCP\\Share', $message, \OCP\Util::DEBUG); throw new \Exception($message); } } } // single file shares should never have delete permissions if ($itemType === 'file') { $permissions = (int) $permissions & ~\OCP\Constants::PERMISSION_DELETE; } //Validate expirationDate if ($expirationDate !== null) { try { /* * Reuse the validateExpireDate. * We have to pass time() since the second arg is the time * the file was shared, since it is not shared yet we just use * the current time. */ $expirationDate = self::validateExpireDate($expirationDate->format('Y-m-d'), time(), $itemType, $itemSource); } catch (\Exception $e) { throw new \OC\HintException($e->getMessage(), $e->getMessage(), 404); } } // Verify share type and sharing conditions are met if ($shareType === self::SHARE_TYPE_USER) { if ($shareWith == $uidOwner) { $message = 'Sharing %s failed, because you can not share with yourself'; $message_t = $l->t('Sharing %s failed, because you can not share with yourself', [$itemName]); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); throw new \Exception($message_t); } if (!\OC_User::userExists($shareWith)) { $message = 'Sharing %s failed, because the user %s does not exist'; $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); throw new \Exception($message_t); } if ($shareWithinGroupOnly) { $inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith)); if (empty($inGroup)) { $message = 'Sharing %s failed, because the user ' . '%s is not a member of any groups that %s is a member of'; $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)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemName, $shareWith, $uidOwner), \OCP\Util::DEBUG); throw new \Exception($message_t); } } // Check if the item source is already shared with the user, either from the same owner or a different user if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a user share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing %s failed, because this item is already shared with %s'; $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); throw new \Exception($message_t); } } if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_USER, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a user share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing %s failed, because this item is already shared with user %s'; $message_t = $l->t('Sharing %s failed, because this item is already shared with user %s', array($itemSourceName, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::ERROR); throw new \Exception($message_t); } } } else { if ($shareType === self::SHARE_TYPE_GROUP) { if (!\OC_Group::groupExists($shareWith)) { $message = 'Sharing %s failed, because the group %s does not exist'; $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); throw new \Exception($message_t); } if ($shareWithinGroupOnly && !\OC_Group::inGroup($uidOwner, $shareWith)) { $message = 'Sharing %s failed, because ' . '%s is not a member of the group %s'; $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OCP\Util::DEBUG); throw new \Exception($message_t); } // Check if the item source is already shared with the group, either from the same owner or a different user // The check for each user in the group is done inside the put() function if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a group share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing %s failed, because this item is already shared with %s'; $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); throw new \Exception($message_t); } } // Convert share with into an array with the keys group and users $group = $shareWith; $shareWith = array(); $shareWith['group'] = $group; $shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner)); } else { if ($shareType === self::SHARE_TYPE_LINK) { $updateExistingShare = false; if (\OC::$server->getAppConfig()->getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { // when updating a link share // FIXME Don't delete link if we update it if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, null, 1)) { // remember old token $oldToken = $checkExists['token']; $oldPermissions = $checkExists['permissions']; //delete the old share Helper::delete($checkExists['id']); $updateExistingShare = true; } if ($passwordChanged === null) { // Generate hash of password - same method as user passwords if (is_string($shareWith) && $shareWith !== '') { self::verifyPassword($shareWith); $shareWith = \OC::$server->getHasher()->hash($shareWith); } else { // reuse the already set password, but only if we change permissions // otherwise the user disabled the password protection if ($checkExists && (int) $permissions !== (int) $oldPermissions) { $shareWith = $checkExists['share_with']; } } } else { if ($passwordChanged === true) { if (is_string($shareWith) && $shareWith !== '') { self::verifyPassword($shareWith); $shareWith = \OC::$server->getHasher()->hash($shareWith); } } else { if ($updateExistingShare) { $shareWith = $checkExists['share_with']; } } } if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) { $message = 'You need to provide a password to create a public link, only protected links are allowed'; $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed'); \OCP\Util::writeLog('OCP\\Share', $message, \OCP\Util::DEBUG); throw new \Exception($message_t); } if ($updateExistingShare === false && self::isDefaultExpireDateEnabled() && empty($expirationDate)) { $expirationDate = Helper::calcExpireDate(); } // Generate token if (isset($oldToken)) { $token = $oldToken; } else { $token = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . \OCP\Security\ISecureRandom::CHAR_DIGITS); } $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName, $expirationDate); if ($result) { return $token; } else { return false; } } $message = 'Sharing %s failed, because sharing with links is not allowed'; $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName), \OCP\Util::DEBUG); throw new \Exception($message_t); } else { if ($shareType === self::SHARE_TYPE_REMOTE) { /* * Check if file is not already shared with the remote user */ if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_REMOTE, $shareWith, $uidOwner, self::FORMAT_NONE, null, 1, true, true)) { $message = 'Sharing %s failed, because this item is already shared with %s'; $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OCP\Util::DEBUG); throw new \Exception($message_t); } $token = \OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(self::TOKEN_LENGTH, \OCP\Security\ISecureRandom::CHAR_LOWER . \OCP\Security\ISecureRandom::CHAR_UPPER . \OCP\Security\ISecureRandom::CHAR_DIGITS); list($user, $remote) = Helper::splitUserRemote($shareWith); $shareWith = $user . '@' . $remote; $shareId = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName); $send = false; if ($shareId) { $send = self::sendRemoteShare($token, $shareWith, $itemSourceName, $shareId, $uidOwner); } if ($send === false) { $currentUser = \OC::$server->getUserSession()->getUser()->getUID(); self::unshare($itemType, $itemSource, $shareType, $shareWith, $currentUser); $message_t = $l->t('Sharing %s failed, could not find %s, maybe the server is currently unreachable.', array($itemSourceName, $shareWith)); throw new \Exception($message_t); } return $send; } else { // Future share types need to include their own conditions $message = 'Share type %s is not valid for %s'; $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource)); \OCP\Util::writeLog('OCP\\Share', sprintf($message, $shareType, $itemSource), \OCP\Util::DEBUG); throw new \Exception($message_t); } } } } // Put the item into the database $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate); return $result ? true : false; }
$square = isset($_GET['square']) ? (bool) $_GET['square'] : false; $scale = isset($_GET['scale']) ? $_GET['scale'] : 1; $images = explode(';', $_GET['image']); if (!empty($_GET['token'])) { $linkItem = \OCP\Share::getShareByToken($_GET['token']); if (!(is_array($linkItem) && isset($linkItem['uid_owner']))) { exit; } // seems to be a valid share $rootLinkItem = \OCP\Share::resolveReShare($linkItem); $user = $rootLinkItem['uid_owner']; // Setup filesystem OCP\JSON::checkUserExists($user); OC_Util::tearDownFS(); OC_Util::setupFS($user); $root = \OC\Files\Filesystem::getPath($linkItem['file_source']) . '/'; $images = array_map(function ($image) use($root) { return $root . $image; }, $images); } else { $root = ''; OCP\JSON::checkLoggedIn(); $user = OCP\User::getUser(); } session_write_close(); $eventSource = new OC_EventSource(); foreach ($images as $image) { $height = 200 * $scale; if ($square) { $width = 200 * $scale; } else {
/** * Sharing a file or folder via link/public * @param array $params The hook params */ protected function shareFileOrFolder($params) { $path = Filesystem::getPath($params['fileSource']); $this->addNotificationsForUser($this->currentUser, 'shared_link_self', array($path), $path, $params['itemType'] === 'file', $this->userSettings->getUserSetting($this->currentUser, 'stream', Files_Sharing::TYPE_SHARED), $this->userSettings->getUserSetting($this->currentUser, 'email', Files_Sharing::TYPE_SHARED) ? $this->userSettings->getUserSetting($this->currentUser, 'setting', 'batchtime') : 0); }
/** * Share an item with a user, group, or via private link * @param string $itemType * @param string $itemSource * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK * @param string $shareWith User or group the item is being shared with * @param int $permissions CRUDS * @param string $itemSourceName * @param \DateTime $expirationDate * @return boolean|string Returns true on success or false on failure, Returns token on success for links * @throws \Exception */ public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null, \DateTime $expirationDate = null) { $uidOwner = \OC_User::getUser(); $shareWithinGroupOnly = self::shareWithGroupMembersOnly(); $l = \OC_L10N::get('lib'); if (is_null($itemSourceName)) { $itemSourceName = $itemSource; } // check if file can be shared if ($itemType === 'file' or $itemType === 'folder') { $path = \OC\Files\Filesystem::getPath($itemSource); // verify that the file exists before we try to share it if (!$path) { $message = 'Sharing %s failed, because the file does not exist'; $message_t = $l->t('Sharing %s failed, because the file does not exist', array($itemSourceName)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName), \OC_Log::ERROR); throw new \Exception($message_t); } // verify that the user has share permission if (!\OC\Files\Filesystem::isSharable($path)) { $message = 'You are not allowed to share %s'; $message_t = $l->t('You are not allowed to share %s', array($itemSourceName)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName), \OC_Log::ERROR); throw new \Exception($message_t); } } //verify that we don't share a folder which already contains a share mount point if ($itemType === 'folder') { $path = '/' . $uidOwner . '/files' . \OC\Files\Filesystem::getPath($itemSource) . '/'; $mountManager = \OC\Files\Filesystem::getMountManager(); $mounts = $mountManager->findIn($path); foreach ($mounts as $mount) { if ($mount->getStorage()->instanceOfStorage('\\OCA\\Files_Sharing\\ISharedStorage')) { $message = 'Sharing "' . $itemSourceName . '" failed, because it contains files shared with you!'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } } } // single file shares should never have delete permissions if ($itemType === 'file') { $permissions = (int) $permissions & ~\OCP\PERMISSION_DELETE; } // Verify share type and sharing conditions are met if ($shareType === self::SHARE_TYPE_USER) { if ($shareWith == $uidOwner) { $message = 'Sharing %s failed, because the user %s is the item owner'; $message_t = $l->t('Sharing %s failed, because the user %s is the item owner', array($itemSourceName, $shareWith)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR); throw new \Exception($message_t); } if (!\OC_User::userExists($shareWith)) { $message = 'Sharing %s failed, because the user %s does not exist'; $message_t = $l->t('Sharing %s failed, because the user %s does not exist', array($itemSourceName, $shareWith)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR); throw new \Exception($message_t); } if ($shareWithinGroupOnly) { $inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith)); if (empty($inGroup)) { $message = 'Sharing %s failed, because the user ' . '%s is not a member of any groups that %s is a member of'; $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($itemSourceName, $shareWith, $uidOwner)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $shareWith, $uidOwner), \OC_Log::ERROR); throw new \Exception($message_t); } } // Check if the item source is already shared with the user, either from the same owner or a different user if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a user share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing %s failed, because this item is already shared with %s'; $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR); throw new \Exception($message_t); } } } else { if ($shareType === self::SHARE_TYPE_GROUP) { if (!\OC_Group::groupExists($shareWith)) { $message = 'Sharing %s failed, because the group %s does not exist'; $message_t = $l->t('Sharing %s failed, because the group %s does not exist', array($itemSourceName, $shareWith)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR); throw new \Exception($message_t); } if ($shareWithinGroupOnly && !\OC_Group::inGroup($uidOwner, $shareWith)) { $message = 'Sharing %s failed, because ' . '%s is not a member of the group %s'; $message_t = $l->t('Sharing %s failed, because %s is not a member of the group %s', array($itemSourceName, $uidOwner, $shareWith)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $uidOwner, $shareWith), \OC_Log::ERROR); throw new \Exception($message_t); } // Check if the item source is already shared with the group, either from the same owner or a different user // The check for each user in the group is done inside the put() function if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a group share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing %s failed, because this item is already shared with %s'; $message_t = $l->t('Sharing %s failed, because this item is already shared with %s', array($itemSourceName, $shareWith)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName, $shareWith), \OC_Log::ERROR); throw new \Exception($message_t); } } // Convert share with into an array with the keys group and users $group = $shareWith; $shareWith = array(); $shareWith['group'] = $group; $shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner)); } else { if ($shareType === self::SHARE_TYPE_LINK) { $updateExistingShare = false; if (\OC_Appconfig::getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { // when updating a link share // FIXME Don't delete link if we update it if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, null, 1)) { // remember old token $oldToken = $checkExists['token']; $oldPermissions = $checkExists['permissions']; //delete the old share Helper::delete($checkExists['id']); $updateExistingShare = true; } // Generate hash of password - same method as user passwords if (!empty($shareWith)) { $forcePortable = CRYPT_BLOWFISH != 1; $hasher = new \PasswordHash(8, $forcePortable); $shareWith = $hasher->HashPassword($shareWith . \OC_Config::getValue('passwordsalt', '')); } else { // reuse the already set password, but only if we change permissions // otherwise the user disabled the password protection if ($checkExists && (int) $permissions !== (int) $oldPermissions) { $shareWith = $checkExists['share_with']; } } if (\OCP\Util::isPublicLinkPasswordRequired() && empty($shareWith)) { $message = 'You need to provide a password to create a public link, only protected links are allowed'; $message_t = $l->t('You need to provide a password to create a public link, only protected links are allowed'); \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message_t); } if ($updateExistingShare === false && self::isDefaultExpireDateEnabled() && empty($expirationDate)) { $expirationDate = Helper::calcExpireDate(); } // Generate token if (isset($oldToken)) { $token = $oldToken; } else { $token = \OC_Util::generateRandomBytes(self::TOKEN_LENGTH); } $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName, $expirationDate); if ($result) { return $token; } else { return false; } } $message = 'Sharing %s failed, because sharing with links is not allowed'; $message_t = $l->t('Sharing %s failed, because sharing with links is not allowed', array($itemSourceName)); \OC_Log::write('OCP\\Share', sprintf($message, $itemSourceName), \OC_Log::ERROR); throw new \Exception($message_t); return false; } else { // Future share types need to include their own conditions $message = 'Share type %s is not valid for %s'; $message_t = $l->t('Share type %s is not valid for %s', array($shareType, $itemSource)); \OC_Log::write('OCP\\Share', sprintf($message, $shareType, $itemSource), \OC_Log::ERROR); throw new \Exception($message_t); } } } // Put the item into the database return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName, $expirationDate); }
/** * get file * * @param string $user * @param int $fileSource * @return array with internal path of the file and a absolute link to it */ private function getFile($user, $fileSource) { \OC_Util::setupFS($user); $file = \OC\Files\Filesystem::getPath($fileSource); $args = \OC\Files\Filesystem::is_dir($file) ? array('dir' => $file) : array('dir' => dirname($file), 'scrollto' => $file); $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args); return array($file, $link); }
$linkItem = OCP\Share::getShareByToken($_POST['dirToken']); if ($linkItem === false) { OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Invalid Token'))))); die; } if (!($linkItem['permissions'] & OCP\PERMISSION_CREATE)) { OCP\JSON::checkLoggedIn(); } else { // resolve reshares $rootLinkItem = OCP\Share::resolveReShare($linkItem); OCP\JSON::checkUserExists($rootLinkItem['uid_owner']); // Setup FS with owner OC_Util::tearDownFS(); OC_Util::setupFS($rootLinkItem['uid_owner']); // The token defines the target directory (security reasons) $path = \OC\Files\Filesystem::getPath($linkItem['file_source']); $dir = sprintf("/%s/%s", $path, $publicDirectory); if (!$dir || empty($dir) || $dir === false) { OCP\JSON::error(array('data' => array_merge(array('message' => $l->t('Unable to set upload directory.'))))); die; } $dir = rtrim($dir, '/'); } } OCP\JSON::callCheck(); if (!\OCP\App::isEnabled('files_encryption')) { // encryption app need to create keys later, so can't close too early \OC::$session->close(); } // get array with current storage stats (e.g. max file size) $storageStats = \OCA\Files\Helper::buildFileStorageStatistics($dir);
if (Config::get('enable', 'false') === 'false') { Util::changeHttpStatus(403); exit; } /* id defaults to 'root' (meaning 'serve root feed') */ $id = isset($_GET['id']) ? $_GET['id'] : 'root'; /* if either pid or tid is set, serve preview image for id */ if (isset($_GET['pid'])) { $id = (int) $_GET['pid']; $type = 'cover'; } if (isset($_GET['tid'])) { $id = (int) $_GET['tid']; $type = 'thumbnail'; } $dir = \OC\Files\Filesystem::normalizePath(\OC\Files\Filesystem::getPath($id)); $root = Config::get('root_path', '/Library'); /* Only feed files descending from designated root directory */ if (!Files::isChild($root, $dir)) { $dir = $root; } $dirInfo = \OC\Files\Filesystem::getFileInfo($dir); /* If requested resource is a file, serve it, otherwise produce opds feed */ switch ($dirInfo->getType()) { case 'file': if ($type) { Feed::servePreview($dir, $type); } else { Feed::serveFile($dir, $id); } break;
/** * update share keys if a file was shared */ public static function postShared($params) { if (\OCP\App::isEnabled('files_encryption') === false) { return true; } if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { $path = \OC\Files\Filesystem::getPath($params['fileSource']); self::updateKeyfiles($path); } }
/** * @NoAdminRequired * @NoCSRFRequired * @SSOCORS */ public function unshare($id, $type) { $shareWith = null; $path = \OC\Files\Filesystem::getPath($id); $response = array('id' => $id); if ($path === null) { if ($type == 'file') { $error_msg = self::msg_idNotExist; } else { $replacement = '${1}folder${3}'; $error_msg = preg_replace($this->fileTypePattern, $replacement, self::msg_idNotExist); } return new DataResponse(array('data' => $response, 'status' => 'error', 'message' => $error_msg)); } if (\OC\Files\Filesystem::filetype($path) !== $type) { $replacement = '${1}\'' . $type . '\'${3}' . 'id: ' . $id; $error_msg = preg_replace($this->errorTypePattern, $replacement, self::msg_errorType); return new DataResponse(array('data' => $response, 'status' => 'error', 'message' => $error_msg)); } if ($type == 'dir') { $type = 'folder'; } $unshare = \OCP\Share::unshare((string) $type, (string) $id, (int) $this->shareType, $shareWith); if ($unshare) { return new DataResponse(array('data' => $response, 'status' => 'success')); } else { $replacement = '${1}folder${3}'; $error_msg = preg_replace($this->fileTypePattern, $replacement, self::msg_noRequireUnshareBeforeShare); return new DataResponse(array('data' => $response, 'status' => 'error', 'message' => $error_msg)); } }
/** * @param $token * @return null|string */ private function getPath($token) { $linkItem = Share::getShareByToken($token, false); $path = null; if (is_array($linkItem) && isset($linkItem['uid_owner'])) { // seems to be a valid share $rootLinkItem = Share::resolveReShare($linkItem); if (isset($rootLinkItem['uid_owner'])) { JSON::checkUserExists($rootLinkItem['uid_owner']); OC_Util::tearDownFS(); OC_Util::setupFS($rootLinkItem['uid_owner']); $path = Filesystem::getPath($linkItem['file_source']); } } return $path; }
/** * Get the path of a file by id * * Note that the resulting path is not guaranteed to be unique for the id, multiple paths can point to the same file * * @param int $fileId * @return string */ public function getPath($fileId) { return \OC\Files\Filesystem::getPath($fileId); }
/** * unshare file/folder from a user with whom you shared the file before */ public static function postUnshare($params) { if (\OCP\App::isEnabled('files_encryption') === false) { return true; } if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { $view = new \OC\Files\View('/'); $userId = \OCP\User::getUser(); $util = new Util($view, $userId); $path = \OC\Files\Filesystem::getPath($params['fileSource']); // for group shares get a list of the group members if ($params['shareType'] === \OCP\Share::SHARE_TYPE_GROUP) { $userIds = \OC_Group::usersInGroup($params['shareWith']); } else { if ($params['shareType'] === \OCP\Share::SHARE_TYPE_LINK) { $userIds = array($util->getPublicShareKeyId()); } else { $userIds = array($params['shareWith']); } } $mountManager = \OC\Files\Filesystem::getMountManager(); $mount = $mountManager->find('/' . $userId . '/files' . $path); $mountPoint = $mount->getMountPoint(); // if we unshare a folder we need a list of all (sub-)files if ($params['itemType'] === 'folder') { $allFiles = $util->getAllFiles($path, $mountPoint); } else { $allFiles = array($path); } foreach ($allFiles as $path) { // check if the user still has access to the file, otherwise delete share key $sharingUsers = $util->getSharingUsersArray(true, $path); // Unshare every user who no longer has access to the file $delUsers = array_diff($userIds, $sharingUsers); list($owner, $ownerPath) = $util->getUidAndFilename($path); // delete share key Keymanager::delShareKey($view, $delUsers, $ownerPath, $owner); } } }
/** * @brief Sharing a file or folder via link/public * @param array $params The hook params */ public static function shareFileOrFolder($params) { if (UserSettings::getUserSetting(\OCP\User::getUser(), 'setting', 'self') && UserSettings::getUserSetting(\OCP\User::getUser(), 'stream', Data::TYPE_SHARED)) { $path = \OC\Files\Filesystem::getPath($params['fileSource']); $link = \OCP\Util::linkToAbsolute('files', 'index.php', array('dir' => $params['itemType'] === 'file' ? dirname($path) : $path)); Data::send('files', 'shared_link_self', array($path), '', array(), $path, $link, \OCP\User::getUser(), Data::TYPE_SHARED, Data::PRIORITY_MEDIUM); } }
/** * Share an item with a user, group, or via private link * @param string $itemType * @param string $itemSource * @param int $shareType SHARE_TYPE_USER, SHARE_TYPE_GROUP, or SHARE_TYPE_LINK * @param string $shareWith User or group the item is being shared with * @param int $permissions CRUDS * @param null $itemSourceName * @throws \Exception * @internal param \OCP\Item $string type * @internal param \OCP\Item $string source * @internal param \OCP\SHARE_TYPE_USER $int , SHARE_TYPE_GROUP, or SHARE_TYPE_LINK * @internal param \OCP\User $string or group the item is being shared with * @internal param \OCP\CRUDS $int permissions * @return bool|string Returns true on success or false on failure, Returns token on success for links */ public static function shareItem($itemType, $itemSource, $shareType, $shareWith, $permissions, $itemSourceName = null) { $uidOwner = \OC_User::getUser(); $sharingPolicy = \OC_Appconfig::getValue('core', 'shareapi_share_policy', 'global'); if (is_null($itemSourceName)) { $itemSourceName = $itemSource; } // verify that the file exists before we try to share it if ($itemType === 'file' or $itemType === 'folder') { $path = \OC\Files\Filesystem::getPath($itemSource); if (!$path) { $message = 'Sharing ' . $itemSourceName . ' failed, because the file does not exist'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } } // Verify share type and sharing conditions are met if ($shareType === self::SHARE_TYPE_USER) { if ($shareWith == $uidOwner) { $message = 'Sharing ' . $itemSourceName . ' failed, because the user ' . $shareWith . ' is the item owner'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } if (!\OC_User::userExists($shareWith)) { $message = 'Sharing ' . $itemSourceName . ' failed, because the user ' . $shareWith . ' does not exist'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } if ($sharingPolicy == 'groups_only') { $inGroup = array_intersect(\OC_Group::getUserGroups($uidOwner), \OC_Group::getUserGroups($shareWith)); if (empty($inGroup)) { $message = 'Sharing ' . $itemSourceName . ' failed, because the user ' . $shareWith . ' is not a member of any groups that ' . $uidOwner . ' is a member of'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } } // Check if the item source is already shared with the user, either from the same owner or a different user if ($checkExists = self::getItems($itemType, $itemSource, self::$shareTypeUserAndGroups, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a user share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing ' . $itemSourceName . ' failed, because this item is already shared with ' . $shareWith; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } } } else { if ($shareType === self::SHARE_TYPE_GROUP) { if (!\OC_Group::groupExists($shareWith)) { $message = 'Sharing ' . $itemSourceName . ' failed, because the group ' . $shareWith . ' does not exist'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } if ($sharingPolicy == 'groups_only' && !\OC_Group::inGroup($uidOwner, $shareWith)) { $message = 'Sharing ' . $itemSourceName . ' failed, because ' . $uidOwner . ' is not a member of the group ' . $shareWith; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } // Check if the item source is already shared with the group, either from the same owner or a different user // The check for each user in the group is done inside the put() function if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_GROUP, $shareWith, null, self::FORMAT_NONE, null, 1, true, true)) { // Only allow the same share to occur again if it is the same // owner and is not a group share, this use case is for increasing // permissions for a specific user if ($checkExists['uid_owner'] != $uidOwner || $checkExists['share_type'] == $shareType) { $message = 'Sharing ' . $itemSourceName . ' failed, because this item is already shared with ' . $shareWith; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } } // Convert share with into an array with the keys group and users $group = $shareWith; $shareWith = array(); $shareWith['group'] = $group; $shareWith['users'] = array_diff(\OC_Group::usersInGroup($group), array($uidOwner)); } else { if ($shareType === self::SHARE_TYPE_LINK) { if (\OC_Appconfig::getValue('core', 'shareapi_allow_links', 'yes') == 'yes') { // when updating a link share if ($checkExists = self::getItems($itemType, $itemSource, self::SHARE_TYPE_LINK, null, $uidOwner, self::FORMAT_NONE, null, 1)) { // remember old token $oldToken = $checkExists['token']; $oldPermissions = $checkExists['permissions']; //delete the old share self::delete($checkExists['id']); } // Generate hash of password - same method as user passwords if (isset($shareWith)) { $forcePortable = CRYPT_BLOWFISH != 1; $hasher = new \PasswordHash(8, $forcePortable); $shareWith = $hasher->HashPassword($shareWith . \OC_Config::getValue('passwordsalt', '')); } else { // reuse the already set password, but only if we change permissions // otherwise the user disabled the password protection if ($checkExists && (int) $permissions !== (int) $oldPermissions) { $shareWith = $checkExists['share_with']; } } // Generate token if (isset($oldToken)) { $token = $oldToken; } else { $token = \OC_Util::generateRandomBytes(self::TOKEN_LENGTH); } $result = self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, $token, $itemSourceName); if ($result) { return $token; } else { return false; } } $message = 'Sharing ' . $itemSourceName . ' failed, because sharing with links is not allowed'; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); return false; // } else if ($shareType === self::SHARE_TYPE_CONTACT) { // if (!\OC_App::isEnabled('contacts')) { // $message = 'Sharing '.$itemSource.' failed, because the contacts app is not enabled'; // \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); // return false; // } // $vcard = \OC_Contacts_App::getContactVCard($shareWith); // if (!isset($vcard)) { // $message = 'Sharing '.$itemSource.' failed, because the contact does not exist'; // \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); // throw new \Exception($message); // } // $details = \OC_Contacts_VCard::structureContact($vcard); // // TODO Add ownCloud user to contacts vcard // if (!isset($details['EMAIL'])) { // $message = 'Sharing '.$itemSource.' failed, because no email address is associated with the contact'; // \OC_Log::write('OCP\Share', $message, \OC_Log::ERROR); // throw new \Exception($message); // } // return self::shareItem($itemType, $itemSource, self::SHARE_TYPE_EMAIL, $details['EMAIL'], $permissions); } else { // Future share types need to include their own conditions $message = 'Share type ' . $shareType . ' is not valid for ' . $itemSource; \OC_Log::write('OCP\\Share', $message, \OC_Log::ERROR); throw new \Exception($message); } } } // If the item is a folder, scan through the folder looking for equivalent item types // if ($itemType == 'folder') { // $parentFolder = self::put('folder', $itemSource, $shareType, $shareWith, $uidOwner, $permissions, true); // if ($parentFolder && $files = \OC\Files\Filesystem::getDirectoryContent($itemSource)) { // for ($i = 0; $i < count($files); $i++) { // $name = substr($files[$i]['name'], strpos($files[$i]['name'], $itemSource) - strlen($itemSource)); // if ($files[$i]['mimetype'] == 'httpd/unix-directory' // && $children = \OC\Files\Filesystem::getDirectoryContent($name, '/') // ) { // // Continue scanning into child folders // array_push($files, $children); // } else { // // Check file extension for an equivalent item type to convert to // $extension = strtolower(substr($itemSource, strrpos($itemSource, '.') + 1)); // foreach (self::$backends as $type => $backend) { // if (isset($backend->dependsOn) && $backend->dependsOn == 'file' && isset($backend->supportedFileExtensions) && in_array($extension, $backend->supportedFileExtensions)) { // $itemType = $type; // break; // } // } // // Pass on to put() to check if this item should be converted, the item won't be inserted into the database unless it can be converted // self::put($itemType, $name, $shareType, $shareWith, $uidOwner, $permissions, $parentFolder); // } // } // return true; // } // return false; // } else { // Put the item into the database return self::put($itemType, $itemSource, $shareType, $shareWith, $uidOwner, $permissions, null, null, $itemSourceName); // } }
private static function getInternalFileAttachment($attachment) { $path = urldecode($attachment['path']); $path = substr($path, strpos($path, "/")+1 ); //remove root folder "files/" $userId = OC_User::getUser(); if ( $attachment['owner'] == $userId ) { // file-owner can use own path $path = \OC\Files\Filesystem::getPath($attachment['fileid']); } else { $item_shared = OCP\Share::getItemSharedWithBySource('file', $attachment['fileid']); if ( $item_shared != false ) { // if item is direct shared use shared-file target $path = $item_shared['file_target']; } else { // else search shared parent folder $path = "/" . self::getInheritedSharedPath( urldecode($attachment['path']), $userId, $attachment['owner'] ); } $ocVersion = OCP\Util::getVersion(); if ( $ocVersion[0] < 7 ) { $path = "/Shared" . $path; } } $view = new \OC\Files\View('/' . $userId . '/files'); $fileinfo = $view->getFileInfo($path); $download_url = OCP\Util::linkToRoute('download', array('file' => $path)); // File not found if ( \OC\Files\Filesystem::is_file( $path ) == false ) { $fileinfo['name'] = "File not found."; $download_url = "#"; } $result = array( "type" => $attachment['type'], "mimetype" => $fileinfo['mimetype'], "name" => $fileinfo['name'], "path" => $path, "download_url" => $download_url ); return $result; }
/** * @brief scan files for metadata * * @param int $id fileid * @return array $meta metadata */ public static function scan($id) { $meta = self::create($id); $path = \OC\Files\Filesystem::getLocalFile(\OC\Files\Filesystem::getPath($id)); /* scan for Calibre-generated metadata.opf first. If found, use it as metadata source, * if not found continue file/isbn/etc scan */ if (!Calibre::get($path, $meta)) { /* try to call function named 'type' with signature type($path,$meta) * eg, pdf(), epub(), etc */ $type = strtolower(substr(strrchr($path, "."), 1)); if (is_callable(array(__CLASS__, $type))) { try { self::$type($path, $meta); } catch (Exception $e) { Util::logWarn("no metadata scanner for format " . $type); } } } /* if title is not set, assume metadata was invalid or not present * use filename as title */ if (!$meta['title']) { $info = pathinfo($path); $meta['title'] = basename($path, '.' . $info['extension']); } self::save($meta); return $meta; }
/** * @param string $token * @return string Resolved file path of the token * @throws \Exception In case share could not get properly resolved */ private function getPath($token) { $linkItem = Share::getShareByToken($token, false); if (is_array($linkItem) && isset($linkItem['uid_owner'])) { // seems to be a valid share $rootLinkItem = Share::resolveReShare($linkItem); if (isset($rootLinkItem['uid_owner'])) { if (!$this->userManager->userExists($rootLinkItem['uid_owner'])) { throw new \Exception('Owner of the share does not exist anymore'); } OC_Util::tearDownFS(); OC_Util::setupFS($rootLinkItem['uid_owner']); $path = Filesystem::getPath($linkItem['file_source']); if (!empty($path) && Filesystem::isReadable($path)) { return $path; } } } throw new \Exception('No file found belonging to file.'); }