appName = $AppName; $this->trans = $trans; $this->logger = $logger; $this->config = $config; $this->shareManager = $shareManager; $this->session = $session; } /** * Getting file by token * * @param integer $fileId - file identifier * @param string $shareToken - access token * @param string $path - file path * * @return array */ public function getFileByToken($fileId, $shareToken, $path = null) { list($node, $error, $share) = $this->getNodeByToken($shareToken); if (isset($error)) { return [null, $error, null]; } if ($node instanceof Folder) { if ($fileId !== null && $fileId !== 0) { try { $files = $node->getById($fileId); } catch (\Exception $e) { $this->logger->error("getFileByToken: $fileId", ["exception" => $e]); return [null, $this->trans->t("Invalid request"), null]; } if (empty($files)) { $this->logger->info("Files not found: $fileId"); return [null, $this->trans->t("File not found"), null]; } $file = $files[0]; } else { try { $file = $node->get($path); } catch (\Exception $e) { $this->logger->error("getFileByToken for path: $path", ["exception" => $e]); return [null, $this->trans->t("Invalid request"), null]; } } } else { $file = $node; } return [$file, null, $share]; } /** * Getting file by token * * @param string $shareToken - access token * * @return array */ public function getNodeByToken($shareToken) { list($share, $error) = $this->getShare($shareToken); if (isset($error)) { return [null, $error, null]; } if (($share->getPermissions() & Constants::PERMISSION_READ) === 0) { return [null, $this->trans->t("You do not have enough permissions to view the file"), null]; } try { $node = $share->getNode(); } catch (NotFoundException $e) { $this->logger->error("getNodeByToken error", ["exception" => $e]); return [null, $this->trans->t("File not found"), null]; } return [$node, null, $share]; } /** * Getting share by token * * @param string $shareToken - access token * * @return array */ public function getShare($shareToken) { if (empty($shareToken)) { return [null, $this->trans->t("FileId is empty")]; } try { $share = $this->shareManager->getShareByToken($shareToken); } catch (ShareNotFound $e) { $this->logger->error("getShare error", ["exception" => $e]); $share = null; } if ($share === null || $share === false) { return [null, $this->trans->t("You do not have enough permissions to view the file")]; } $authenticatedLinks = $this->session->get('public_link_authenticated'); $isAuthenticated = is_array($authenticatedLinks) && in_array($share->getId(), $authenticatedLinks); $isAuthenticated = $isAuthenticated || $authenticatedLinks === (string) $share->getId(); if ($share->getPassword() && !$isAuthenticated) { return [null, $this->trans->t("You do not have enough permissions to view the file")]; } return [$share, null]; } /** * Generate unique document identifier * * @param File $file - file * @param bool $origin - request from federated store * * @return string */ public function getKey($file, $origin = false) { $fileId = $file->getId(); if ($origin && RemoteInstance::isRemoteFile($file)) { $key = RemoteInstance::getRemoteKey($file); if (!empty($key)) { return $key; } } $key = KeyManager::get($fileId); if (empty($key)) { $instanceId = $this->config->getSystemValue("instanceid", true); $key = $instanceId . "_" . $this->GUID(); KeyManager::set($fileId, $key); } return $key; } /** * Generate unique identifier * * @return string */ private function GUID() { if (function_exists("com_create_guid") === true) { return trim(com_create_guid(), "{}"); } return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535)); } /** * Generate unique file version key * * @param OCA\Files_Versions\Versions\IVersion $version - file version * * @return string */ public function getVersionKey($version) { $instanceId = $this->config->getSystemValue("instanceid", true); $key = $instanceId . "_" . $version->getSourceFile()->getEtag() . "_" . $version->getRevisionId(); return $key; } /** * The method checks download permission * * @param IShare $share - share object * * @return bool */ public static function canShareDownload($share) { $can = true; $downloadAttribute = self::getShareAttrubute($share, "download"); if (isset($downloadAttribute)) { $can = $downloadAttribute; } return $can; } /** * The method extracts share attribute * * @param IShare $share - share object * @param string $attribute - attribute name * * @return bool|null */ private static function getShareAttrubute($share, $attribute) { $attributes = null; if (method_exists(IShare::class, "getAttributes")) { $attributes = $share->getAttributes(); } $attribute = isset($attributes) ? $attributes->getAttribute("permissions", $attribute) : null; return $attribute; } }