1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright Copyright (c) 2018, Roeland Jago Douma <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @author Roeland Jago Douma <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @license GNU AGPL version 3 or any later version |
8
|
|
|
* |
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
12
|
|
|
* License, or (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU Affero General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
21
|
|
|
* |
22
|
|
|
*/ |
23
|
|
|
namespace OCA\Richdocuments\Controller; |
24
|
|
|
|
25
|
|
|
use OCA\Richdocuments\AppConfig; |
26
|
|
|
use OCA\Richdocuments\Db\DirectMapper; |
27
|
|
|
use OCA\Richdocuments\Service\FederationService; |
28
|
|
|
use OCA\Richdocuments\TemplateManager; |
29
|
|
|
use OCA\Richdocuments\TokenManager; |
30
|
|
|
use OCP\AppFramework\Controller; |
31
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
32
|
|
|
use OCP\AppFramework\Http; |
33
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy; |
34
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
35
|
|
|
use OCP\AppFramework\Http\RedirectResponse; |
36
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
37
|
|
|
use OCP\Files\IRootFolder; |
38
|
|
|
use OCP\Files\Node; |
39
|
|
|
use OCP\Files\NotFoundException; |
40
|
|
|
use OCP\IConfig; |
41
|
|
|
use OCP\IRequest; |
42
|
|
|
|
43
|
|
|
class DirectViewController extends Controller { |
44
|
|
|
/** @var IRootFolder */ |
45
|
|
|
private $rootFolder; |
46
|
|
|
|
47
|
|
|
/** @var TokenManager */ |
48
|
|
|
private $tokenManager; |
49
|
|
|
|
50
|
|
|
/** @var DirectMapper */ |
51
|
|
|
private $directMapper; |
52
|
|
|
|
53
|
|
|
/** @var IConfig */ |
54
|
|
|
private $config; |
55
|
|
|
|
56
|
|
|
/** @var AppConfig */ |
57
|
|
|
private $appConfig; |
58
|
|
|
|
59
|
|
|
/** @var TemplateManager */ |
60
|
|
|
private $templateManager; |
61
|
|
|
|
62
|
|
|
/** @var FederationService */ |
63
|
|
|
private $federationService; |
64
|
|
|
|
65
|
|
View Code Duplication |
public function __construct( |
|
|
|
|
66
|
|
|
$appName, |
67
|
|
|
IRequest $request, |
68
|
|
|
IRootFolder $rootFolder, |
69
|
|
|
TokenManager $tokenManager, |
70
|
|
|
DirectMapper $directMapper, |
71
|
|
|
IConfig $config, |
72
|
|
|
AppConfig $appConfig, |
73
|
|
|
TemplateManager $templateManager, |
74
|
|
|
FederationService $federationService |
75
|
|
|
) { |
76
|
|
|
parent::__construct($appName, $request); |
77
|
|
|
|
78
|
|
|
$this->rootFolder = $rootFolder; |
79
|
|
|
$this->tokenManager = $tokenManager; |
80
|
|
|
$this->directMapper = $directMapper; |
81
|
|
|
$this->config = $config; |
82
|
|
|
$this->appConfig = $appConfig; |
83
|
|
|
$this->templateManager = $templateManager; |
84
|
|
|
$this->federationService = $federationService; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @NoAdminRequired |
89
|
|
|
* @NoCSRFRequired |
90
|
|
|
* @PublicPage |
91
|
|
|
* |
92
|
|
|
* @param string $token |
93
|
|
|
* @return JSONResponse|RedirectResponse|TemplateResponse |
94
|
|
|
* @throws NotFoundException |
95
|
|
|
*/ |
96
|
|
|
public function show($token) { |
97
|
|
|
try { |
98
|
|
|
$direct = $this->directMapper->getByToken($token); |
99
|
|
|
} catch (DoesNotExistException $e) { |
100
|
|
|
$params = [ |
101
|
|
|
'errors' => [['error' => $e->getMessage()]] |
102
|
|
|
]; |
103
|
|
|
return new TemplateResponse('core', 'error', $params, 'guest'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Delete the token. They are for 1 time use only |
107
|
|
|
$this->directMapper->delete($direct); |
|
|
|
|
108
|
|
|
|
109
|
|
|
$folder = $this->rootFolder->getUserFolder($direct->getUid()); |
110
|
|
|
if ($this->templateManager->isTemplate($direct->getFileid())) { |
111
|
|
|
$item = $this->templateManager->get($direct->getFileid()); |
112
|
|
|
if ($direct->getTemplateDestination() === 0 || $direct->getTemplateDestination() === null) { |
113
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
try { |
117
|
|
|
list($urlSrc, $token) = $this->tokenManager->getTokenForTemplate($item, $direct->getUid(), $direct->getTemplateDestination(), true); |
118
|
|
|
} catch (\Exception $e) { |
119
|
|
|
return new JSONResponse([], Http::STATUS_BAD_REQUEST); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$relativePath = '/new.odt'; |
123
|
|
|
|
124
|
|
|
} else { |
125
|
|
|
try { |
126
|
|
|
$item = $folder->getById($direct->getFileid())[0]; |
127
|
|
|
if(!($item instanceof Node)) { |
128
|
|
|
throw new \Exception(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** Open file from remote collabora */ |
132
|
|
|
$federatedUrl = $this->federationService->getRemoteRedirectURL($item, $direct); |
|
|
|
|
133
|
|
View Code Duplication |
if ($federatedUrl !== null) { |
|
|
|
|
134
|
|
|
$response = new RedirectResponse($federatedUrl); |
135
|
|
|
$response->addHeader('X-Frame-Options', 'ALLOW'); |
136
|
|
|
return $response; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
list($urlSrc, $token) = $this->tokenManager->getToken($item->getId(), null, $direct->getUid(), true); |
140
|
|
|
} catch (\Exception $e) { |
141
|
|
|
$params = [ |
142
|
|
|
'errors' => [['error' => $e->getMessage()]] |
143
|
|
|
]; |
144
|
|
|
return new TemplateResponse('core', 'error', $params, 'guest'); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
$relativePath = $folder->getRelativePath($item->getPath()); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
try { |
151
|
|
|
$params = [ |
152
|
|
|
'permissions' => $item->getPermissions(), |
153
|
|
|
'title' => $item->getName(), |
154
|
|
|
'fileId' => $item->getId() . '_' . $this->config->getSystemValue('instanceid'), |
155
|
|
|
'token' => $token, |
156
|
|
|
'urlsrc' => $urlSrc, |
157
|
|
|
'path' => $relativePath, |
158
|
|
|
'instanceId' => $this->config->getSystemValue('instanceid'), |
159
|
|
|
'canonical_webroot' => $this->appConfig->getAppValue('canonical_webroot'), |
160
|
|
|
'direct' => true, |
161
|
|
|
]; |
162
|
|
|
|
163
|
|
|
$response = new TemplateResponse('richdocuments', 'documents', $params, 'empty'); |
164
|
|
|
$policy = new ContentSecurityPolicy(); |
165
|
|
|
$policy->allowInlineScript(true); |
|
|
|
|
166
|
|
|
$policy->addAllowedFrameDomain($this->appConfig->getAppValue('public_wopi_url')); |
167
|
|
|
$response->setContentSecurityPolicy($policy); |
168
|
|
|
return $response; |
169
|
|
|
} catch (\Exception $e) { |
170
|
|
|
$params = [ |
171
|
|
|
'errors' => [['error' => $e->getMessage()]] |
172
|
|
|
]; |
173
|
|
|
return new TemplateResponse('core', 'error', $params, 'guest'); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.