|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CMS Pico - Create websites using Pico CMS for Nextcloud. |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2017, Maxence Lange (<[email protected]>) |
|
6
|
|
|
* @copyright Copyright (c) 2019, Daniel Rudolf (<[email protected]>) |
|
7
|
|
|
* |
|
8
|
|
|
* @license GNU AGPL version 3 or any later version |
|
9
|
|
|
* |
|
10
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
11
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
12
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
13
|
|
|
* License, or (at your option) any later version. |
|
14
|
|
|
* |
|
15
|
|
|
* This program is distributed in the hope that it will be useful, |
|
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
18
|
|
|
* GNU Affero General Public License for more details. |
|
19
|
|
|
* |
|
20
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
21
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
declare(strict_types=1); |
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\CMSPico\Controller; |
|
27
|
|
|
|
|
28
|
|
|
use OCA\CMSPico\AppInfo\Application; |
|
29
|
|
|
use OCA\CMSPico\Exceptions\AssetInvalidPathException; |
|
30
|
|
|
use OCA\CMSPico\Exceptions\AssetNotFoundException; |
|
31
|
|
|
use OCA\CMSPico\Exceptions\AssetNotPermittedException; |
|
32
|
|
|
use OCA\CMSPico\Exceptions\FilesystemNotLocalException; |
|
33
|
|
|
use OCA\CMSPico\Exceptions\PageInvalidPathException; |
|
34
|
|
|
use OCA\CMSPico\Exceptions\PageNotFoundException; |
|
35
|
|
|
use OCA\CMSPico\Exceptions\PageNotPermittedException; |
|
36
|
|
|
use OCA\CMSPico\Exceptions\PicoRuntimeException; |
|
37
|
|
|
use OCA\CMSPico\Exceptions\ThemeNotCompatibleException; |
|
38
|
|
|
use OCA\CMSPico\Exceptions\ThemeNotFoundException; |
|
39
|
|
|
use OCA\CMSPico\Exceptions\WebsiteInvalidFilesystemException; |
|
40
|
|
|
use OCA\CMSPico\Exceptions\WebsiteInvalidOwnerException; |
|
41
|
|
|
use OCA\CMSPico\Exceptions\WebsiteNotFoundException; |
|
42
|
|
|
use OCA\CMSPico\Exceptions\WebsiteNotPermittedException; |
|
43
|
|
|
use OCA\CMSPico\Http\InternalServerErrorResponse; |
|
44
|
|
|
use OCA\CMSPico\Http\NotFoundResponse; |
|
45
|
|
|
use OCA\CMSPico\Http\NotModifiedResponse; |
|
46
|
|
|
use OCA\CMSPico\Http\NotPermittedResponse; |
|
47
|
|
|
use OCA\CMSPico\Http\PicoAssetResponse; |
|
48
|
|
|
use OCA\CMSPico\Http\PicoErrorResponse; |
|
49
|
|
|
use OCA\CMSPico\Http\PicoPageResponse; |
|
50
|
|
|
use OCA\CMSPico\Service\WebsitesService; |
|
51
|
|
|
use OCP\AppFramework\Controller; |
|
52
|
|
|
use OCP\AppFramework\Http\RedirectResponse; |
|
53
|
|
|
use OCP\AppFramework\Http\Response; |
|
54
|
|
|
use OCP\IL10N; |
|
55
|
|
|
use OCP\IRequest; |
|
56
|
|
|
use OCP\IURLGenerator; |
|
57
|
|
|
use OCP\IUserSession; |
|
58
|
|
|
|
|
59
|
|
|
class PicoController extends Controller |
|
60
|
|
|
{ |
|
61
|
|
|
/** @var IURLGenerator */ |
|
62
|
|
|
private $urlGenerator; |
|
63
|
|
|
|
|
64
|
|
|
/** @var IUserSession */ |
|
65
|
|
|
private $userSession; |
|
66
|
|
|
|
|
67
|
|
|
/** @var IL10N */ |
|
68
|
|
|
private $l10n; |
|
69
|
|
|
|
|
70
|
|
|
/** @var WebsitesService */ |
|
71
|
|
|
private $websitesService; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* PicoController constructor. |
|
75
|
|
|
* |
|
76
|
|
|
* @param IRequest $request |
|
77
|
|
|
* @param IURLGenerator $urlGenerator |
|
78
|
|
|
* @param IUserSession $userSession |
|
79
|
|
|
* @param IL10N $l10n |
|
80
|
|
|
* @param WebsitesService $websitesService |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function __construct( |
|
83
|
|
|
IRequest $request, |
|
84
|
|
|
IURLGenerator $urlGenerator, |
|
85
|
|
|
IUserSession $userSession, |
|
86
|
|
|
IL10N $l10n, |
|
87
|
|
|
WebsitesService $websitesService |
|
88
|
|
|
) { |
|
89
|
1 |
|
parent::__construct(Application::APP_NAME, $request); |
|
90
|
|
|
|
|
91
|
1 |
|
$this->urlGenerator = $urlGenerator; |
|
92
|
1 |
|
$this->userSession = $userSession; |
|
93
|
1 |
|
$this->l10n = $l10n; |
|
94
|
1 |
|
$this->websitesService = $websitesService; |
|
95
|
1 |
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @PublicPage |
|
99
|
|
|
* @NoCSRFRequired |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $site |
|
102
|
|
|
* @param string $page |
|
103
|
|
|
* @param bool $proxyRequest |
|
104
|
|
|
* |
|
105
|
|
|
* @return Response |
|
106
|
|
|
*/ |
|
107
|
6 |
|
public function getPage(string $site, string $page, bool $proxyRequest = false): Response |
|
108
|
|
|
{ |
|
109
|
6 |
|
$userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null; |
|
110
|
|
|
|
|
111
|
|
|
try { |
|
112
|
6 |
|
$picoPage = $this->websitesService->getPage($site, $page, $userId, $proxyRequest); |
|
113
|
5 |
|
return new PicoPageResponse($picoPage); |
|
114
|
1 |
|
} catch (WebsiteNotFoundException | WebsiteInvalidOwnerException $e) { |
|
115
|
|
|
return new NotFoundResponse($this->l10n->t( |
|
116
|
|
|
'The requested website could not be found on the server. Maybe the website was deleted?' |
|
117
|
|
|
)); |
|
118
|
1 |
|
} catch (WebsiteInvalidFilesystemException $e) { |
|
119
|
|
|
return new InternalServerErrorResponse($this->l10n->t( |
|
120
|
|
|
'The file and directory structure of this website appears to be broken and thus could not be accessed.' |
|
121
|
|
|
)); |
|
122
|
1 |
|
} catch (WebsiteNotPermittedException $e) { |
|
123
|
1 |
|
if ($userId === null) { |
|
124
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute( |
|
125
|
|
|
'core.login.showLoginForm', |
|
126
|
|
|
[ 'redirect_url' => $this->request->getRequestUri() ] |
|
127
|
|
|
)); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
return new NotPermittedResponse($this->l10n->t( |
|
131
|
1 |
|
'You don\'t have access to this private website. Maybe the share was deleted or has expired?' |
|
132
|
|
|
)); |
|
133
|
|
|
} catch (FilesystemNotLocalException $e) { |
|
134
|
|
|
return new InternalServerErrorResponse($this->l10n->t( |
|
135
|
|
|
'This website is hosted on a non-local storage and thus could not be accessed.' |
|
136
|
|
|
)); |
|
137
|
|
|
} catch (ThemeNotFoundException $e) { |
|
138
|
|
|
return new InternalServerErrorResponse($this->l10n->t( |
|
139
|
|
|
'This website uses a theme that could not be found on the server and thus could not be built.' |
|
140
|
|
|
)); |
|
141
|
|
|
} catch (ThemeNotCompatibleException $e) { |
|
142
|
|
|
return new InternalServerErrorResponse($this->l10n->t( |
|
143
|
|
|
'This website uses a incompatible theme and thus could not be built.' |
|
144
|
|
|
)); |
|
145
|
|
|
} catch (PageInvalidPathException | PageNotFoundException $e) { |
|
146
|
|
|
return new NotFoundResponse($this->l10n->t( |
|
147
|
|
|
'The requested website page could not be found on the server. Maybe the page was deleted?' |
|
148
|
|
|
)); |
|
149
|
|
|
} catch (PageNotPermittedException $e) { |
|
150
|
|
|
return new NotPermittedResponse($this->l10n->t( |
|
151
|
|
|
'You don\'t have access to this website page. Maybe the share was deleted or has expired?' |
|
152
|
|
|
)); |
|
153
|
|
|
} catch (PicoRuntimeException $e) { |
|
154
|
|
|
$errorMessage = $this->l10n->t( |
|
155
|
|
|
'The requested website page could not be built, so that the server was unable to complete your request.' |
|
156
|
|
|
); |
|
157
|
|
|
return new PicoErrorResponse($errorMessage, $e); |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* @PublicPage |
|
163
|
|
|
* @NoCSRFRequired |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $site |
|
166
|
|
|
* @param string $asset |
|
167
|
|
|
* @param string $assetsETag |
|
168
|
|
|
* |
|
169
|
|
|
* @return Response |
|
170
|
|
|
*/ |
|
171
|
3 |
|
public function getAsset(string $site, string $asset, string $assetsETag = ''): Response |
|
172
|
|
|
{ |
|
173
|
3 |
|
$userId = $this->userSession->isLoggedIn() ? $this->userSession->getUser()->getUID() : null; |
|
174
|
|
|
|
|
175
|
|
|
try { |
|
176
|
3 |
|
$picoAsset = $this->websitesService->getAsset($site, $asset, $userId); |
|
177
|
|
|
|
|
178
|
3 |
|
$response = new PicoAssetResponse($picoAsset, (bool) $assetsETag); |
|
179
|
|
|
|
|
180
|
3 |
|
$assetETag = $picoAsset->getETag(); |
|
181
|
3 |
|
$clientETag = $this->request->getHeader('If-None-Match'); |
|
182
|
3 |
|
if ($assetETag && $clientETag) { |
|
183
|
|
|
if (preg_match('/^"?' . preg_quote($assetETag, '/') . '(?>"?$|-)/', $clientETag)) { |
|
184
|
|
|
return new NotModifiedResponse($response); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
3 |
|
return $response; |
|
189
|
|
|
} catch (WebsiteNotFoundException | WebsiteInvalidOwnerException $e) { |
|
190
|
|
|
return new NotFoundResponse($this->l10n->t( |
|
191
|
|
|
'The requested website could not be found on the server. Maybe the website was deleted?' |
|
192
|
|
|
)); |
|
193
|
|
|
} catch (WebsiteInvalidFilesystemException $e) { |
|
194
|
|
|
return new InternalServerErrorResponse($this->l10n->t( |
|
195
|
|
|
'The file and directory structure of this website appears to be broken und thus could not be accessed.' |
|
196
|
|
|
)); |
|
197
|
|
|
} catch (WebsiteNotPermittedException $e) { |
|
198
|
|
|
if ($userId === null) { |
|
199
|
|
|
return new RedirectResponse($this->urlGenerator->linkToRoute( |
|
200
|
|
|
'core.login.showLoginForm', |
|
201
|
|
|
[ 'redirect_url' => $this->request->getRequestUri() ] |
|
202
|
|
|
)); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return new NotPermittedResponse($this->l10n->t( |
|
206
|
|
|
'You don\'t have access to this private website. Maybe the share was deleted or has expired?' |
|
207
|
|
|
)); |
|
208
|
|
|
} catch (FilesystemNotLocalException $e) { |
|
209
|
|
|
return new InternalServerErrorResponse($this->l10n->t( |
|
210
|
|
|
'This website is hosted on a non-local storage and thus could not be accessed.' |
|
211
|
|
|
)); |
|
212
|
|
|
} catch (AssetInvalidPathException | AssetNotFoundException $e) { |
|
213
|
|
|
return new NotFoundResponse($this->l10n->t( |
|
214
|
|
|
'The requested website asset could not be found on the server. Maybe the asset was deleted?' |
|
215
|
|
|
)); |
|
216
|
|
|
} catch (AssetNotPermittedException $e) { |
|
217
|
|
|
return new NotPermittedResponse($this->l10n->t( |
|
218
|
|
|
'You don\'t have access to this website asset. Maybe the share was deleted or has expired?' |
|
219
|
|
|
)); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
|