2019-10-14 17:55:39 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
2020-01-13 16:23:49 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-10-14 17:55:39 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Tobias Kaminsky <tobias@kaminsky.me>
|
2019-10-14 17:55:39 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2019-12-03 21:57:53 +03:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-14 17:55:39 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\DirectEditing;
|
|
|
|
|
|
|
|
use Doctrine\DBAL\FetchMode;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
|
|
|
use OCP\DirectEditing\ACreateFromTemplate;
|
|
|
|
use OCP\DirectEditing\IEditor;
|
|
|
|
use \OCP\DirectEditing\IManager;
|
|
|
|
use OCP\DirectEditing\IToken;
|
2020-08-25 18:12:27 +03:00
|
|
|
use OCP\Encryption\IManager as EncryptionManager;
|
2019-10-14 17:55:39 +03:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\IRootFolder;
|
2019-12-04 14:09:33 +03:00
|
|
|
use OCP\Files\Node;
|
2019-10-14 17:55:39 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\IDBConnection;
|
2019-12-16 16:58:56 +03:00
|
|
|
use OCP\IL10N;
|
2019-10-14 17:55:39 +03:00
|
|
|
use OCP\IUserSession;
|
2019-12-16 16:58:56 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2019-10-14 17:55:39 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
|
|
|
use OCP\Share\IShare;
|
2020-08-25 18:12:27 +03:00
|
|
|
use Throwable;
|
2019-12-19 11:38:22 +03:00
|
|
|
use function array_key_exists;
|
|
|
|
use function in_array;
|
2019-10-14 17:55:39 +03:00
|
|
|
|
|
|
|
class Manager implements IManager {
|
|
|
|
private const TOKEN_CLEANUP_TIME = 12 * 60 * 60 ;
|
|
|
|
|
|
|
|
public const TABLE_TOKENS = 'direct_edit';
|
|
|
|
|
|
|
|
/** @var IEditor[] */
|
2019-11-25 15:56:22 +03:00
|
|
|
private $editors = [];
|
2019-10-14 17:55:39 +03:00
|
|
|
/** @var IDBConnection */
|
|
|
|
private $connection;
|
2020-08-25 18:12:27 +03:00
|
|
|
/** @var ISecureRandom */
|
2019-10-14 17:55:39 +03:00
|
|
|
private $random;
|
2020-08-25 18:12:27 +03:00
|
|
|
/** @var string|null */
|
2019-10-14 17:55:39 +03:00
|
|
|
private $userId;
|
2020-08-25 18:12:27 +03:00
|
|
|
/** @var IRootFolder */
|
2019-10-14 17:55:39 +03:00
|
|
|
private $rootFolder;
|
2019-12-16 16:58:56 +03:00
|
|
|
/** @var IL10N */
|
|
|
|
private $l10n;
|
2020-08-25 18:12:27 +03:00
|
|
|
/** @var EncryptionManager */
|
|
|
|
private $encryptionManager;
|
2019-10-14 17:55:39 +03:00
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
ISecureRandom $random,
|
|
|
|
IDBConnection $connection,
|
|
|
|
IUserSession $userSession,
|
2019-12-16 16:58:56 +03:00
|
|
|
IRootFolder $rootFolder,
|
2020-08-25 18:12:27 +03:00
|
|
|
IFactory $l10nFactory,
|
|
|
|
EncryptionManager $encryptionManager
|
2019-10-14 17:55:39 +03:00
|
|
|
) {
|
|
|
|
$this->random = $random;
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->userId = $userSession->getUser() ? $userSession->getUser()->getUID() : null;
|
|
|
|
$this->rootFolder = $rootFolder;
|
2019-12-16 16:58:56 +03:00
|
|
|
$this->l10n = $l10nFactory->get('core');
|
2020-08-25 18:12:27 +03:00
|
|
|
$this->encryptionManager = $encryptionManager;
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function registerDirectEditor(IEditor $directEditor): void {
|
|
|
|
$this->editors[$directEditor->getId()] = $directEditor;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getEditors(): array {
|
|
|
|
return $this->editors;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTemplates(string $editor, string $type): array {
|
|
|
|
if (!array_key_exists($editor, $this->editors)) {
|
|
|
|
throw new \RuntimeException('No matching editor found');
|
|
|
|
}
|
|
|
|
$templates = [];
|
|
|
|
foreach ($this->editors[$editor]->getCreators() as $creator) {
|
2019-12-16 16:58:56 +03:00
|
|
|
if ($creator->getId() === $type) {
|
|
|
|
$templates = [
|
|
|
|
'empty' => [
|
|
|
|
'id' => 'empty',
|
|
|
|
'title' => $this->l10n->t('Empty file'),
|
|
|
|
'preview' => null
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
|
|
|
if ($creator instanceof ACreateFromTemplate) {
|
|
|
|
$templates = $creator->getTemplates();
|
|
|
|
}
|
|
|
|
|
|
|
|
$templates = array_map(function ($template) use ($creator) {
|
|
|
|
$template['extension'] = $creator->getExtension();
|
|
|
|
$template['mimetype'] = $creator->getMimetype();
|
|
|
|
return $template;
|
|
|
|
}, $templates);
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
}
|
2019-10-31 11:18:05 +03:00
|
|
|
$return = [];
|
2020-10-05 16:12:57 +03:00
|
|
|
$return['templates'] = $templates;
|
2019-10-31 11:18:05 +03:00
|
|
|
return $return;
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function create(string $path, string $editorId, string $creatorId, $templateId = null): string {
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($this->userId);
|
2020-01-14 12:57:54 +03:00
|
|
|
if ($userFolder->nodeExists($path)) {
|
2020-01-10 15:06:27 +03:00
|
|
|
throw new \RuntimeException('File already exists');
|
2020-01-14 12:57:54 +03:00
|
|
|
} else {
|
2020-01-10 15:06:27 +03:00
|
|
|
$file = $userFolder->newFile($path);
|
|
|
|
$editor = $this->getEditor($editorId);
|
|
|
|
$creators = $editor->getCreators();
|
|
|
|
foreach ($creators as $creator) {
|
|
|
|
if ($creator->getId() === $creatorId) {
|
|
|
|
$creator->create($file, $creatorId, $templateId);
|
|
|
|
return $this->createToken($editorId, $file, $path);
|
|
|
|
}
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 15:06:27 +03:00
|
|
|
|
2019-10-14 17:55:39 +03:00
|
|
|
throw new \RuntimeException('No creator found');
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:09:33 +03:00
|
|
|
public function open(string $filePath, string $editorId = null): string {
|
2019-10-14 17:55:39 +03:00
|
|
|
/** @var File $file */
|
2019-12-04 14:09:33 +03:00
|
|
|
$file = $this->rootFolder->getUserFolder($this->userId)->get($filePath);
|
2019-10-14 17:55:39 +03:00
|
|
|
|
|
|
|
if ($editorId === null) {
|
|
|
|
$editorId = $this->findEditorForFile($file);
|
|
|
|
}
|
2019-12-19 11:38:22 +03:00
|
|
|
if (!array_key_exists($editorId, $this->editors)) {
|
|
|
|
throw new \RuntimeException("Editor $editorId is unknown");
|
|
|
|
}
|
2019-10-14 17:55:39 +03:00
|
|
|
|
2019-12-04 14:09:33 +03:00
|
|
|
return $this->createToken($editorId, $file, $filePath);
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private function findEditorForFile(File $file) {
|
|
|
|
foreach ($this->editors as $editor) {
|
|
|
|
if (in_array($file->getMimeType(), $editor->getMimetypes())) {
|
|
|
|
return $editor->getId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new \RuntimeException('No default editor found for files mimetype');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function edit(string $token): Response {
|
|
|
|
try {
|
|
|
|
/** @var IEditor $editor */
|
|
|
|
$tokenObject = $this->getToken($token);
|
|
|
|
if ($tokenObject->hasBeenAccessed()) {
|
|
|
|
throw new \RuntimeException('Token has already been used and can only be used for followup requests');
|
|
|
|
}
|
|
|
|
$editor = $this->getEditor($tokenObject->getEditor());
|
|
|
|
$this->accessToken($token);
|
2020-08-25 18:12:27 +03:00
|
|
|
} catch (Throwable $throwable) {
|
2019-10-14 17:55:39 +03:00
|
|
|
$this->invalidateToken($token);
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
return $editor->open($tokenObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function editSecure(File $file, string $editorId): TemplateResponse {
|
|
|
|
// TODO: Implementation in follow up
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getEditor($editorId): IEditor {
|
|
|
|
if (!array_key_exists($editorId, $this->editors)) {
|
|
|
|
throw new \RuntimeException('No editor found');
|
|
|
|
}
|
|
|
|
return $this->editors[$editorId];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getToken(string $token): IToken {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')->from(self::TABLE_TOKENS)
|
|
|
|
->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR)));
|
|
|
|
$result = $query->execute();
|
|
|
|
if ($tokenRow = $result->fetch(FetchMode::ASSOCIATIVE)) {
|
|
|
|
return new Token($this, $tokenRow);
|
|
|
|
}
|
|
|
|
throw new \RuntimeException('Failed to validate the token');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function cleanup(): int {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete(self::TABLE_TOKENS)
|
|
|
|
->where($query->expr()->lt('timestamp', $query->createNamedParameter(time() - self::TOKEN_CLEANUP_TIME)));
|
|
|
|
return $query->execute();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refreshToken(string $token): bool {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update(self::TABLE_TOKENS)
|
|
|
|
->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
|
|
|
|
->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR)));
|
|
|
|
$result = $query->execute();
|
|
|
|
return $result !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function invalidateToken(string $token): bool {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->delete(self::TABLE_TOKENS)
|
|
|
|
->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR)));
|
|
|
|
$result = $query->execute();
|
|
|
|
return $result !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function accessToken(string $token): bool {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->update(self::TABLE_TOKENS)
|
|
|
|
->set('accessed', $query->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
|
|
|
|
->set('timestamp', $query->createNamedParameter(time(), IQueryBuilder::PARAM_INT))
|
|
|
|
->where($query->expr()->eq('token', $query->createNamedParameter($token, IQueryBuilder::PARAM_STR)));
|
|
|
|
$result = $query->execute();
|
|
|
|
return $result !== 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invokeTokenScope($userId): void {
|
|
|
|
\OC_User::setIncognitoMode(true);
|
|
|
|
\OC_User::setUserId($userId);
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:09:33 +03:00
|
|
|
public function createToken($editorId, File $file, string $filePath, IShare $share = null): string {
|
2019-10-14 17:55:39 +03:00
|
|
|
$token = $this->random->generate(64, ISecureRandom::CHAR_HUMAN_READABLE);
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->insert(self::TABLE_TOKENS)
|
|
|
|
->values([
|
|
|
|
'token' => $query->createNamedParameter($token),
|
|
|
|
'editor_id' => $query->createNamedParameter($editorId),
|
|
|
|
'file_id' => $query->createNamedParameter($file->getId()),
|
2019-12-04 14:09:33 +03:00
|
|
|
'file_path' => $query->createNamedParameter($filePath),
|
2019-10-14 17:55:39 +03:00
|
|
|
'user_id' => $query->createNamedParameter($this->userId),
|
|
|
|
'share_id' => $query->createNamedParameter($share !== null ? $share->getId(): null),
|
|
|
|
'timestamp' => $query->createNamedParameter(time())
|
|
|
|
]);
|
|
|
|
$query->execute();
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:09:33 +03:00
|
|
|
/**
|
|
|
|
* @param $userId
|
|
|
|
* @param $fileId
|
|
|
|
* @param null $filePath
|
|
|
|
* @return Node
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
|
|
|
public function getFileForToken($userId, $fileId, $filePath = null): Node {
|
2019-10-14 17:55:39 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($userId);
|
2019-12-04 14:09:33 +03:00
|
|
|
if ($filePath !== null) {
|
|
|
|
return $userFolder->get($filePath);
|
|
|
|
}
|
|
|
|
$files = $userFolder->getById($fileId);
|
|
|
|
if (count($files) === 0) {
|
|
|
|
throw new NotFoundException('File nound found by id ' . $fileId);
|
|
|
|
}
|
|
|
|
return $files[0];
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|
2020-08-25 18:12:27 +03:00
|
|
|
|
|
|
|
public function isEnabled(): bool {
|
|
|
|
if (!$this->encryptionManager->isEnabled()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$moduleId = $this->encryptionManager->getDefaultEncryptionModuleId();
|
|
|
|
$module = $this->encryptionManager->getEncryptionModule($moduleId);
|
|
|
|
/** @var \OCA\Encryption\Util $util */
|
|
|
|
$util = \OC::$server->get(\OCA\Encryption\Util::class);
|
|
|
|
if ($module->isReadyForUser($this->userId) && $util->isMasterKeyEnabled()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-14 17:55:39 +03:00
|
|
|
}
|