2016-12-06 23:09:40 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-10-19 16:36:10 +03:00
|
|
|
* @copyright Copyright (c) 2016-2017 Lukas Reschke <lukas@statuscode.ch>
|
2016-12-06 23:09:40 +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
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* 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 OCA\Richdocuments\Controller;
|
|
|
|
|
2017-05-11 18:31:40 +03:00
|
|
|
use OC\Files\View;
|
2018-03-27 14:33:33 +03:00
|
|
|
use OCA\Richdocuments\Db\WopiMapper;
|
2017-11-22 18:23:55 +03:00
|
|
|
use OCA\Richdocuments\TokenManager;
|
2017-05-11 18:31:40 +03:00
|
|
|
use OCA\Richdocuments\Helper;
|
2016-12-06 23:09:40 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
2018-03-27 14:33:33 +03:00
|
|
|
use OCP\AppFramework\Db\DoesNotExistException;
|
2016-12-06 23:09:40 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\IRootFolder;
|
2017-10-19 16:36:10 +03:00
|
|
|
use OCP\IConfig;
|
2018-07-09 11:37:08 +03:00
|
|
|
use OCP\ILogger;
|
2016-12-06 23:09:40 +03:00
|
|
|
use OCP\IRequest;
|
2017-10-19 16:36:10 +03:00
|
|
|
use OCP\IURLGenerator;
|
2016-12-06 23:09:40 +03:00
|
|
|
use OCP\AppFramework\Http\StreamResponse;
|
2017-10-19 16:36:10 +03:00
|
|
|
use OCP\IUserManager;
|
2016-12-06 23:09:40 +03:00
|
|
|
|
|
|
|
class WopiController extends Controller {
|
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
2017-10-19 16:36:10 +03:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
2018-03-27 14:33:33 +03:00
|
|
|
/** @var TokenManager */
|
2017-11-22 18:23:55 +03:00
|
|
|
private $tokenManager;
|
2017-10-19 16:36:10 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
2018-03-27 14:33:33 +03:00
|
|
|
/** @var WopiMapper */
|
|
|
|
private $wopiMapper;
|
2018-07-09 11:37:08 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2016-12-06 23:09:40 +03:00
|
|
|
|
2017-06-06 19:01:47 +03:00
|
|
|
// Signifies LOOL that document has been changed externally in this storage
|
|
|
|
const LOOL_STATUS_DOC_CHANGED = 1010;
|
|
|
|
|
2016-12-06 23:09:40 +03:00
|
|
|
/**
|
|
|
|
* @param string $appName
|
2017-10-19 16:36:10 +03:00
|
|
|
* @param string $UserId
|
2016-12-06 23:09:40 +03:00
|
|
|
* @param IRequest $request
|
|
|
|
* @param IRootFolder $rootFolder
|
2017-10-19 16:36:10 +03:00
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param IConfig $config
|
2018-03-27 14:33:33 +03:00
|
|
|
* @param TokenManager $tokenManager
|
2017-10-19 16:36:10 +03:00
|
|
|
* @param IUserManager $userManager
|
2018-03-27 14:33:33 +03:00
|
|
|
* @param WopiMapper $wopiMapper
|
2018-07-09 11:37:08 +03:00
|
|
|
* @param ILogger $logger
|
2016-12-06 23:09:40 +03:00
|
|
|
*/
|
|
|
|
public function __construct($appName,
|
|
|
|
$UserId,
|
|
|
|
IRequest $request,
|
2017-10-19 16:36:10 +03:00
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IConfig $config,
|
2017-11-22 18:23:55 +03:00
|
|
|
TokenManager $tokenManager,
|
2018-03-27 14:33:33 +03:00
|
|
|
IUserManager $userManager,
|
2018-07-09 11:37:08 +03:00
|
|
|
WopiMapper $wopiMapper,
|
|
|
|
ILogger $logger) {
|
2016-12-06 23:09:40 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->rootFolder = $rootFolder;
|
2017-10-19 16:36:10 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->config = $config;
|
2017-11-22 18:23:55 +03:00
|
|
|
$this->tokenManager = $tokenManager;
|
2017-10-19 16:36:10 +03:00
|
|
|
$this->userManager = $userManager;
|
2018-03-27 14:33:33 +03:00
|
|
|
$this->wopiMapper = $wopiMapper;
|
2018-07-09 11:37:08 +03:00
|
|
|
$this->logger = $logger;
|
2017-01-03 21:50:07 +03:00
|
|
|
}
|
|
|
|
|
2016-12-06 23:09:40 +03:00
|
|
|
/**
|
|
|
|
* Returns general info about a file.
|
|
|
|
*
|
|
|
|
* @NoAdminRequired
|
|
|
|
* @NoCSRFRequired
|
|
|
|
* @PublicPage
|
|
|
|
*
|
|
|
|
* @param string $fileId
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function checkFileInfo($fileId) {
|
|
|
|
$token = $this->request->getParam('access_token');
|
|
|
|
|
2017-05-11 18:31:40 +03:00
|
|
|
list($fileId, , $version) = Helper::parseFileId($fileId);
|
2018-03-27 14:33:33 +03:00
|
|
|
|
|
|
|
try {
|
|
|
|
$wopi = $this->wopiMapper->getPathForToken($token);
|
|
|
|
} catch (DoesNotExistException $e) {
|
2016-12-07 02:57:01 +03:00
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
2016-12-06 23:09:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Login the user to see his mount locations
|
|
|
|
try {
|
|
|
|
/** @var File $file */
|
2018-03-27 14:33:33 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid());
|
2016-12-06 23:09:40 +03:00
|
|
|
$file = $userFolder->getById($fileId)[0];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
2016-12-07 02:57:01 +03:00
|
|
|
if(!($file instanceof File)) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
2017-10-19 16:36:10 +03:00
|
|
|
$response = [
|
|
|
|
'BaseFileName' => $file->getName(),
|
|
|
|
'Size' => $file->getSize(),
|
|
|
|
'Version' => $version,
|
2018-03-27 14:33:33 +03:00
|
|
|
'UserId' => !is_null($wopi->getEditorUid()) ? $wopi->getEditorUid() : 'guest',
|
|
|
|
'OwnerId' => $wopi->getOwnerUid(),
|
2018-06-13 16:31:18 +03:00
|
|
|
'UserFriendlyName' => !is_null($wopi->getEditorUid()) ? \OC_User::getDisplayName($wopi->getEditorUid()) : $wopi->getGuestDisplayname(),
|
2017-10-19 16:36:10 +03:00
|
|
|
'UserExtraInfo' => [
|
|
|
|
],
|
2018-03-27 14:33:33 +03:00
|
|
|
'UserCanWrite' => $wopi->getCanwrite(),
|
|
|
|
'UserCanNotWriteRelative' => \OC::$server->getEncryptionManager()->isEnabled() ? true : is_null($wopi->getEditorUid()),
|
|
|
|
'PostMessageOrigin' => $wopi->getServerHost(),
|
2018-07-09 22:25:35 +03:00
|
|
|
'LastModifiedTime' => Helper::toISO8601($file->getMTime())
|
2017-10-19 16:36:10 +03:00
|
|
|
];
|
|
|
|
|
|
|
|
$serverVersion = $this->config->getSystemValue('version');
|
|
|
|
if (version_compare($serverVersion, '13', '>=')) {
|
2018-03-27 14:33:33 +03:00
|
|
|
$user = $this->userManager->get($wopi->getEditorUid());
|
2018-07-09 22:25:35 +03:00
|
|
|
if($user !== null && $user->getAvatarImage(32) !== null) {
|
|
|
|
$response['UserExtraInfo']['avatar'] = $this->urlGenerator->linkToRouteAbsolute('core.avatar.getAvatar', ['userId' => $wopi->getEditorUid(), 'size' => 32]);
|
2017-10-19 16:36:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JSONResponse($response);
|
2016-12-06 23:09:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an access token and a fileId, returns the contents of the file.
|
|
|
|
* Expects a valid token in access_token parameter.
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $fileId
|
|
|
|
* @param string $access_token
|
|
|
|
* @return Http\Response
|
|
|
|
*/
|
|
|
|
public function getFile($fileId,
|
|
|
|
$access_token) {
|
2017-05-11 18:31:40 +03:00
|
|
|
list($fileId, , $version) = Helper::parseFileId($fileId);
|
2018-03-27 14:33:33 +03:00
|
|
|
|
|
|
|
$wopi = $this->wopiMapper->getPathForToken($access_token);
|
2018-04-05 14:01:55 +03:00
|
|
|
|
|
|
|
if ((int)$fileId !== $wopi->getFileid()) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
2016-12-06 23:09:40 +03:00
|
|
|
try {
|
|
|
|
/** @var File $file */
|
2018-03-27 14:33:33 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid());
|
2016-12-06 23:09:40 +03:00
|
|
|
$file = $userFolder->getById($fileId)[0];
|
2017-05-17 13:27:15 +03:00
|
|
|
\OC_User::setIncognitoMode(true);
|
2018-03-27 14:33:33 +03:00
|
|
|
if ($version !== '0') {
|
|
|
|
$view = new View('/' . $wopi->getOwnerUid() . '/files');
|
2017-05-11 18:31:40 +03:00
|
|
|
$relPath = $view->getRelativePath($file->getPath());
|
|
|
|
$versionPath = '/files_versions/' . $relPath . '.v' . $version;
|
2018-03-27 14:33:33 +03:00
|
|
|
$view = new View('/' . $wopi->getOwnerUid());
|
2017-05-11 18:31:40 +03:00
|
|
|
if ($view->file_exists($versionPath)){
|
|
|
|
$response = new StreamResponse($view->fopen($versionPath, 'rb'));
|
|
|
|
}
|
|
|
|
else {
|
2018-07-09 22:25:35 +03:00
|
|
|
return new JSONResponse([], Http::STATUS_NOT_FOUND);
|
2017-05-11 18:31:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$response = new StreamResponse($file->fopen('rb'));
|
|
|
|
}
|
2016-12-06 23:09:40 +03:00
|
|
|
$response->addHeader('Content-Disposition', 'attachment');
|
|
|
|
$response->addHeader('Content-Type', 'application/octet-stream');
|
|
|
|
return $response;
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an access token and a fileId, replaces the files with the request body.
|
|
|
|
* Expects a valid token in access_token parameter.
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $fileId
|
|
|
|
* @param string $access_token
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
2017-01-03 21:50:07 +03:00
|
|
|
public function putFile($fileId,
|
|
|
|
$access_token) {
|
2018-07-09 22:25:35 +03:00
|
|
|
list($fileId, ,) = Helper::parseFileId($fileId);
|
2017-11-22 18:23:55 +03:00
|
|
|
$isPutRelative = ($this->request->getHeader('X-WOPI-Override') === 'PUT_RELATIVE');
|
2016-12-06 23:09:40 +03:00
|
|
|
|
2018-03-27 14:33:33 +03:00
|
|
|
$wopi = $this->wopiMapper->getPathForToken($access_token);
|
|
|
|
if (!$wopi->getCanwrite()) {
|
2016-12-06 23:09:40 +03:00
|
|
|
return new JSONResponse([], Http::STATUS_FORBIDDEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
/** @var File $file */
|
2018-03-27 14:33:33 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($wopi->getOwnerUid());
|
2016-12-06 23:09:40 +03:00
|
|
|
$file = $userFolder->getById($fileId)[0];
|
2017-06-06 19:01:47 +03:00
|
|
|
|
2017-11-22 18:23:55 +03:00
|
|
|
if ($isPutRelative) {
|
2018-04-09 13:55:36 +03:00
|
|
|
// the new file needs to be installed in the current user dir
|
|
|
|
$userFolder = $this->rootFolder->getUserFolder($wopi->getEditorUid());
|
|
|
|
$file = $userFolder->getById($fileId)[0];
|
|
|
|
|
2017-11-22 18:23:55 +03:00
|
|
|
$suggested = $this->request->getHeader('X-WOPI-SuggestedTarget');
|
|
|
|
$suggested = iconv('utf-7', 'utf-8', $suggested);
|
|
|
|
|
|
|
|
if ($suggested[0] === '.') {
|
|
|
|
$path = dirname($file->getPath()) . '/New File' . $suggested;
|
|
|
|
}
|
|
|
|
else if ($suggested[0] !== '/') {
|
|
|
|
$path = dirname($file->getPath()) . '/' . $suggested;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$path = $userFolder->getPath() . $suggested;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($path === '') {
|
2018-07-09 22:25:35 +03:00
|
|
|
return new JSONResponse([
|
2017-11-22 18:23:55 +03:00
|
|
|
'status' => 'error',
|
|
|
|
'message' => 'Cannot create the file'
|
2018-07-09 22:25:35 +03:00
|
|
|
]);
|
2017-11-22 18:23:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$root = \OC::$server->getRootFolder();
|
|
|
|
|
|
|
|
// create the folder first
|
|
|
|
if (!$root->nodeExists(dirname($path))) {
|
|
|
|
$root->newFolder(dirname($path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a unique new file
|
|
|
|
$path = $root->getNonExistingName($path);
|
|
|
|
$root->newFile($path);
|
|
|
|
$file = $root->get($path);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$wopiHeaderTime = $this->request->getHeader('X-LOOL-WOPI-Timestamp');
|
|
|
|
if (!is_null($wopiHeaderTime) && $wopiHeaderTime != Helper::toISO8601($file->getMTime())) {
|
2018-07-09 11:37:08 +03:00
|
|
|
$this->logger->debug('Document timestamp mismatch ! WOPI client says mtime {headerTime} but storage says {storageTime}', [
|
2017-11-22 18:23:55 +03:00
|
|
|
'headerTime' => $wopiHeaderTime,
|
2018-07-09 22:25:35 +03:00
|
|
|
'storageTime' => Helper::toISO8601($file->getMTime())
|
2017-11-22 18:23:55 +03:00
|
|
|
]);
|
|
|
|
// Tell WOPI client about this conflict.
|
|
|
|
return new JSONResponse(['LOOLStatusCode' => self::LOOL_STATUS_DOC_CHANGED], Http::STATUS_CONFLICT);
|
|
|
|
}
|
2017-06-06 19:01:47 +03:00
|
|
|
}
|
|
|
|
|
2016-12-06 23:09:40 +03:00
|
|
|
$content = fopen('php://input', 'rb');
|
2017-03-10 11:25:22 +03:00
|
|
|
// Setup the FS which is needed to emit hooks (versioning).
|
|
|
|
\OC_Util::tearDownFS();
|
2018-04-09 13:55:36 +03:00
|
|
|
if (!$isPutRelative) {
|
|
|
|
\OC_Util::setupFS($wopi->getOwnerUid());
|
|
|
|
} else {
|
|
|
|
\OC_Util::setupFS($wopi->getEditorUid());
|
|
|
|
}
|
2017-05-19 22:47:23 +03:00
|
|
|
|
|
|
|
// Set the user to register the change under his name
|
2018-07-09 11:37:08 +03:00
|
|
|
$editor = $this->userManager->get($wopi->getEditorUid());
|
2017-06-01 20:56:50 +03:00
|
|
|
if (!is_null($editor)) {
|
2017-06-06 19:01:47 +03:00
|
|
|
\OC::$server->getUserSession()->setUser($editor);
|
|
|
|
}
|
2017-05-19 22:47:23 +03:00
|
|
|
|
2016-12-06 23:09:40 +03:00
|
|
|
$file->putContent($content);
|
2017-11-22 18:23:55 +03:00
|
|
|
|
|
|
|
if ($isPutRelative) {
|
|
|
|
// generate a token for the new file (the user still has to be
|
|
|
|
// logged in)
|
2018-04-09 13:55:36 +03:00
|
|
|
list(, $wopiToken) = $this->tokenManager->getToken($file->getId(), null, $wopi->getEditorUid());
|
2017-11-22 18:23:55 +03:00
|
|
|
|
|
|
|
$wopi = 'index.php/apps/richdocuments/wopi/files/' . $file->getId() . '_' . $this->config->getSystemValue('instanceid') . '?access_token=' . $wopiToken;
|
2018-07-09 11:37:08 +03:00
|
|
|
$url = $this->urlGenerator->getAbsoluteURL($wopi);
|
2017-11-22 18:23:55 +03:00
|
|
|
|
|
|
|
return new JSONResponse([ 'Name' => $file->getName(), 'Url' => $url ], Http::STATUS_OK);
|
|
|
|
}
|
2018-07-09 22:25:35 +03:00
|
|
|
|
|
|
|
return new JSONResponse(['LastModifiedTime' => Helper::toISO8601($file->getMTime())]);
|
2016-12-06 23:09:40 +03:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new JSONResponse([], Http::STATUS_INTERNAL_SERVER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2017-11-22 18:23:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an access token and a fileId, replaces the files with the request body.
|
|
|
|
* Expects a valid token in access_token parameter.
|
|
|
|
* Just actually routes to the PutFile, the implementation of PutFile
|
|
|
|
* handles both saving and saving as.* Given an access token and a fileId, replaces the files with the request body.
|
|
|
|
*
|
|
|
|
* @PublicPage
|
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $fileId
|
|
|
|
* @param string $access_token
|
|
|
|
* @return JSONResponse
|
|
|
|
*/
|
|
|
|
public function putRelativeFile($fileId,
|
|
|
|
$access_token) {
|
|
|
|
return $this->putFile($fileId, $access_token);
|
|
|
|
}
|
2016-12-06 23:09:40 +03:00
|
|
|
}
|