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

285 lines
8.2 KiB

6 years ago
<?php
/**
*
* (c) Copyright Ascensio System SIA 2025
6 years 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.
* In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended to the effect
* that Ascensio System SIA expressly excludes the warranty of non-infringement of any third-party rights.
6 years 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: http://www.gnu.org/licenses/agpl-3.0.html
6 years ago
*
* You can contact Ascensio System SIA at 20A-12 Ernesta Birznieka-Upisha street, Riga, Latvia, EU, LV-1050.
*
* The interactive user interfaces in modified source and object code versions of the Program
* must display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
*
* Pursuant to Section 7(b) of the License you must retain the original Product logo when distributing the program.
* Pursuant to Section 7(e) we decline to grant you any rights under trademark law for use of our trademarks.
*
* All the Product's GUI elements, including illustrations and icon sets, as well as technical
* writing content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0 International.
* See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
6 years ago
*
*/
namespace OCA\Onlyoffice;
6 years ago
use OCP\AppFramework\Http\ContentSecurityPolicy;
6 years ago
use OCP\AppFramework\Http\Response;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\DirectEditing\IEditor;
use OCP\DirectEditing\IToken;
use OCP\IL10N;
6 years ago
use OCP\IURLGenerator;
use Psr\Log\LoggerInterface;
6 years ago
/**
* Direct Editor
*
* @package OCA\Onlyoffice
*/
class DirectEditor implements IEditor {
6 years ago
/**
* Application name
*
* @var string
*/
private $appName;
6 years ago
/**
* Url generator service
*
* @var IURLGenerator
*/
private $urlGenerator;
6 years ago
/**
* l10n service
*
* @var IL10N
*/
private $trans;
/**
* Logger
*
* @var LoggerInterface
6 years ago
*/
private $logger;
/**
* Application configuration
*
6 years ago
* @var AppConfig
6 years ago
*/
private $config;
6 years ago
/**
* Hash generator
*
6 years ago
* @var Crypt
6 years ago
*/
private $crypt;
6 years ago
/**
* @param string $AppName - application name
6 years ago
* @param IURLGenerator $urlGenerator - url generator service
6 years ago
* @param IL10N $trans - l10n service
* @param LoggerInterface $logger - logger
6 years ago
* @param AppConfig $config - application configuration
* @param Crypt $crypt - hash generator
6 years ago
*/
2 years ago
public function __construct(
$AppName,
IURLGenerator $urlGenerator,
IL10N $trans,
LoggerInterface $logger,
2 years ago
AppConfig $config,
Crypt $crypt
) {
6 years ago
$this->appName = $AppName;
6 years ago
$this->urlGenerator = $urlGenerator;
6 years ago
$this->trans = $trans;
$this->logger = $logger;
$this->config = $config;
6 years ago
$this->crypt = $crypt;
6 years ago
}
/**
* Return a unique identifier for the editor
*
* @return string
*/
public function getId(): string {
6 years ago
return $this->appName;
}
/**
* Return a readable name for the editor
*
* @return string
*/
public function getName(): string {
return "ONLYOFFICE";
6 years ago
}
/**
* A list of mimetypes that should open the editor by default
*
* @return array
*/
public function getMimetypes(): array {
6 years ago
$mimes = array();
if (!$this->config->isUserAllowedToUse()) {
return $mimes;
}
$formats = $this->config->formatsSetting();
6 years ago
foreach ($formats as $format => $setting) {
if (array_key_exists("def", $setting) && $setting["def"]) {
array_push($mimes, $setting["mime"][0]);
6 years ago
}
}
return $mimes;
}
/**
* A list of mimetypes that can be opened in the editor optionally
*
* @return array
*/
public function getMimetypesOptional(): array {
6 years ago
$mimes = array();
if (!$this->config->isUserAllowedToUse()) {
return $mimes;
}
$formats = $this->config->formatsSetting();
6 years ago
foreach ($formats as $format => $setting) {
if (!array_key_exists("def", $setting) || !$setting["def"]) {
array_push($mimes, $setting["mime"][0]);
6 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->config->isUserAllowedToUse()) {
return array();
}
6 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")
6 years ago
];
6 years ago
}
/**
* Return if the view is able to securely view a file without downloading it to the browser
*
* @return bool
*/
public function isSecure(): bool {
6 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
6 years ago
* and take care of invalidation
*
6 years ago
* @param IToken $token - one time token
*
6 years ago
* @return Response
*/
public function open(IToken $token): Response {
6 years ago
try {
$token->useTokenScope();
$file = $token->getFile();
$fileId = $file->getId();
$userId = $token->getUser();
$this->logger->debug("DirectEditor open: $fileId");
6 years ago
if (!$this->config->isUserAllowedToUse($userId)) {
return $this->renderError($this->trans->t("Not permitted"));
}
$documentServerUrl = $this->config->getDocumentServerUrl();
6 years ago
if (empty($documentServerUrl)) {
$this->logger->error("documentServerUrl is empty");
6 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
]);
6 years ago
$filePath = $file->getPath();
$filePath = preg_replace("/^\/" . $userId . "\/files/", "", $filePath);
6 years ago
$params = [
6 years ago
"fileId" => null,
"filePath" => $filePath,
6 years ago
"shareToken" => null,
6 years ago
"directToken" => $directToken,
"isTemplate" => false,
"inframe" => false,
"inviewer" => false,
"anchor" => null
6 years ago
];
$response = new TemplateResponse($this->appName, "editor", $params, "base");
6 years ago
$csp = new ContentSecurityPolicy();
if (preg_match("/^https?:\/\//i", $documentServerUrl)) {
$csp->addAllowedScriptDomain($documentServerUrl);
$csp->addAllowedFrameDomain($documentServerUrl);
} else {
$csp->addAllowedFrameDomain("'self'");
6 years ago
}
$response->setContentSecurityPolicy($csp);
return $response;
6 years ago
} catch (\Exception $e) {
$this->logger->error($e->getMessage(), ["exception" => $e]);
6 years ago
return $this->renderError($e->getMessage());
}
}
/**
* Print error page
*
* @param string $error - error message
*
* @return TemplateResponse
*/
private function renderError($error) {
return new TemplateResponse($this->appName, "directeditorerror", [
"error" => $error
], TemplateResponse::RENDER_AS_ERROR);
6 years ago
}
}