The app which enables the users to edit office documents from Nextcloud using ONLYOFFICE Document Server, allows multiple users to collaborate in real time and to save back those changes to Nextcloud
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
onlyoffice-nextcloud/lib/DirectEditor.php

224 lines
7.3 KiB

7 years ago
<?php
4 weeks ago
/*
* Copyright (C) Ascensio System SIA, 2009-2026
7 years ago
*
4 weeks ago
* This program is a free software product. You can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License (AGPL)
* version 3 as published by the Free Software Foundation, together with the
* additional terms provided in the LICENSE file.
7 years ago
*
4 weeks ago
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For
* details, see the GNU AGPL at: https://www.gnu.org/licenses/agpl-3.0.html
7 years ago
*
4 weeks ago
* You can contact Ascensio System SIA by email at info@onlyoffice.com
* or by postal mail at 20A-6 Ernesta Birznieka-Upisha Street, Riga,
* LV-1050, Latvia, European Union.
7 years ago
*
4 weeks ago
* The interactive user interfaces in modified versions of the Program
* are required to display Appropriate Legal Notices in accordance with
* Section 5 of the GNU AGPL version 3.
*
4 weeks ago
* No trademark rights are granted under this License.
*
4 weeks ago
* All non-code elements of the Product, including illustrations,
* icon sets, and technical writing content, are licensed under the
* Creative Commons Attribution-ShareAlike 4.0 International License:
* https://creativecommons.org/licenses/by-sa/4.0/legalcode
*
4 weeks ago
* This license applies only to such non-code elements and does not
* modify or replace the licensing terms applicable to the Program's
* source code, which remains licensed under the GNU Affero General
* Public License v3.
7 years ago
*
4 weeks ago
* SPDX-License-Identifier: AGPL-3.0-only
7 years ago
*/
namespace OCA\Onlyoffice;
7 years ago
use OCP\AppFramework\Http\ContentSecurityPolicy;
7 years ago
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\DirectEditing\IEditor;
use OCP\DirectEditing\IToken;
use OCP\IL10N;
use Psr\Log\LoggerInterface;
7 years ago
/**
* Direct Editor
*
* @package OCA\Onlyoffice
*/
class DirectEditor implements IEditor {
7 years ago
3 years ago
public function __construct(
private readonly string $appName,
private readonly IL10N $trans,
private readonly LoggerInterface $logger,
private readonly AppConfig $appConfig,
private readonly Crypt $crypt
) {}
7 years ago
/**
* Return a unique identifier for the editor
*/
public function getId(): string {
7 years ago
return $this->appName;
}
/**
* Return a readable name for the editor
*/
public function getName(): string {
return "ONLYOFFICE";
7 years ago
}
/**
* A list of mimetypes that should open the editor by default
*/
public function getMimetypes(): array {
$mimes = [];
if (!$this->appConfig->isUserAllowedToUse()) {
return $mimes;
}
$formats = $this->appConfig->formatsSetting();
foreach ($formats as $setting) {
if (array_key_exists("def", $setting) && $setting["def"]) {
$mimes[] = $setting["mime"][0];
7 years ago
}
}
return $mimes;
}
/**
* A list of mimetypes that can be opened in the editor optionally
*/
public function getMimetypesOptional(): array {
$mimes = [];
if (!$this->appConfig->isUserAllowedToUse()) {
return $mimes;
}
$formats = $this->appConfig->formatsSetting();
foreach ($formats as $setting) {
if (!array_key_exists("def", $setting) || !$setting["def"]) {
$mimes[] = $setting["mime"][0];
7 years ago
}
}
return $mimes;
}
/**
* Return a list of file creation options to be presented to the user
*
* @return array of ACreateFromTemplate|ACreateEmpty
*/
public function getCreators(): array {
if (!$this->appConfig->isUserAllowedToUse()) {
return [];
}
7 years ago
return [
new FileCreator($this->appName, $this->trans, $this->logger, "docx"),
new FileCreator($this->appName, $this->trans, $this->logger, "xlsx"),
new FileCreator($this->appName, $this->trans, $this->logger, "pptx")
7 years ago
];
7 years ago
}
/**
* Return if the view is able to securely view a file without downloading it to the browser
*/
public function isSecure(): bool {
7 years ago
return true;
}
/**
* Return a template response for displaying the editor
*
* open can only be called once when the client requests the editor with a one-time-use token
* For handling editing and later requests, editors need to implement their own token handling
7 years ago
* and take care of invalidation
*
7 years ago
* @param IToken $token - one time token
7 years ago
*/
public function open(IToken $token): Response {
7 years ago
try {
$token->useTokenScope();
$file = $token->getFile();
$fileId = $file->getId();
$userId = $token->getUser();
$this->logger->debug("DirectEditor open: $fileId");
7 years ago
if (!$this->appConfig->isUserAllowedToUse($userId)) {
return $this->renderError($this->trans->t("Not permitted"));
}
if ($this->appConfig->getRestrictExternalStorage()
&& $file->getMountPoint() instanceof \OCA\Files_External\Config\ExternalMountPoint) {
return $this->renderError($this->trans->t("Opening files with ONLYOFFICE from external storages is restricted. Please contact the admin."));
}
$documentServerUrl = $this->appConfig->getDocumentServerUrl();
7 years ago
if (empty($documentServerUrl)) {
$this->logger->error("documentServerUrl is empty");
7 years ago
return $this->renderError($this->trans->t("ONLYOFFICE app is not configured. Please contact admin"));
}
$directToken = $this->crypt->getHash([
"userId" => $userId,
"fileId" => $fileId,
"action" => "direct",
"iat" => time(),
"exp" => time() + 30
]);
7 years ago
$filePath = $file->getPath();
$filePath = preg_replace("/^\/" . $userId . "\/files/", "", (string) $filePath);
7 years ago
$params = [
7 years ago
"fileId" => null,
"filePath" => $filePath,
7 years ago
"shareToken" => null,
6 years ago
"directToken" => $directToken,
"isTemplate" => false,
"inframe" => false,
"inviewer" => false,
"anchor" => null
7 years ago
];
$response = new TemplateResponse($this->appName, "editor", $params, "base");
7 years ago
$csp = new ContentSecurityPolicy();
if (preg_match("/^https?:\/\//i", $documentServerUrl)) {
$csp->addAllowedScriptDomain($documentServerUrl);
$csp->addAllowedFrameDomain($documentServerUrl);
} else {
$csp->addAllowedFrameDomain("'self'");
7 years ago
}
$response->setContentSecurityPolicy($csp);
return $response;
7 years ago
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ["exception" => $e]);
7 years ago
return $this->renderError($e->getMessage());
}
}
/**
* Print error page
*
* @param string $error - error message
*/
private function renderError(string $error): TemplateResponse {
return new TemplateResponse($this->appName, "directeditorerror", [
"error" => $error
], TemplateResponse::RENDER_AS_ERROR);
7 years ago
}
}