-
Notifications
You must be signed in to change notification settings - Fork 65
/
pagecontroller.php
257 lines (229 loc) · 6.2 KB
/
pagecontroller.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* Gallery
*
* This file is licensed under the Affero General Public License version 3 or
* later. See the COPYING file.
*
* @author Robin Appelman <robin@icewind.nl>
* @author Olivier Paroz <galleryapps@oparoz.com>
*
* @copyright Robin Appelman 2012-2015
* @copyright Olivier Paroz 2014-2016
*/
namespace OCA\Gallery\Controller;
use OCP\IURLGenerator;
use OCP\IRequest;
use OCP\IConfig;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\AppFramework\Http\RedirectResponse;
use OCA\Gallery\Environment\Environment;
use OCA\Gallery\Http\ImageResponse;
/**
* Generates templates for the landing page from within ownCloud, the public
* gallery and error pages
*
* @package OCA\Gallery\Controller
*/
class PageController extends Controller {
/** @var Environment */
private $environment;
/** @var IURLGenerator */
private $urlGenerator;
/** @var IConfig */
private $appConfig;
/**
* Constructor
*
* @param string $appName
* @param IRequest $request
* @param Environment $environment
* @param IURLGenerator $urlGenerator
* @param IConfig $appConfig
*/
public function __construct(
$appName,
IRequest $request,
Environment $environment,
IURLGenerator $urlGenerator,
IConfig $appConfig
) {
parent::__construct($appName, $request);
$this->environment = $environment;
$this->urlGenerator = $urlGenerator;
$this->appConfig = $appConfig;
}
/**
* @NoAdminRequired
* @NoCSRFRequired
*
* Shows the albums and pictures at the root folder or a message if
* there are no pictures.
*
* This is the entry page for logged-in users accessing the app from
* within ownCloud.
* A TemplateResponse response uses a template from the templates folder
* and parameters provided here to build the page users will see
*
* @return TemplateResponse
*/
public function index() {
$appName = $this->appName;
// Parameters sent to the template
$params = $this->getIndexParameters($appName);
// Will render the page using the template found in templates/index.php
$response = new TemplateResponse($appName, 'index', $params);
$this->addContentSecurityToResponse($response);
return $response;
}
/**
* @PublicPage
* @NoCSRFRequired
*
* Shows the albums and pictures or redirects to the download location the token gives access to
*
* @param string $token
* @param null|string $filename
*
* @return TemplateResponse|ImageResponse|RedirectResponse
*/
public function publicIndex($token, $filename) {
$node = $this->environment->getSharedNode();
if ($node->getType() === 'dir') {
return $this->showPublicPage($token);
} else {
$url = $this->urlGenerator->linkToRoute(
$this->appName . '.files_public.download',
[
'token' => $token,
'fileId' => $node->getId(),
'filename' => $filename
]
);
return new RedirectResponse($url);
}
}
/**
* @PublicPage
* @NoCSRFRequired
* @Guest
*
* Generates an error page based on the error code
*
* @param int $code
*
* @return TemplateResponse
*/
public function errorPage($code) {
$appName = $this->appName;
$message = $this->request->getCookie('galleryErrorMessage');
$params = [
'appName' => $appName,
'message' => $message,
'code' => $code,
];
$errorTemplate = new TemplateResponse($appName, 'index', $params, 'guest');
$errorTemplate->setStatus($code);
$errorTemplate->invalidateCookie('galleryErrorMessage');
return $errorTemplate;
}
/**
* Adds the domain "data:" to the allowed image domains
* this function is called by reference
*
* @param TemplateResponse $response
*/
private function addContentSecurityToResponse($response) {
$csp = new Http\ContentSecurityPolicy();
$csp->addAllowedFontDomain("data:");
$response->setContentSecurityPolicy($csp);
}
/**
* @PublicPage
* @NoCSRFRequired
* @Guest
*
* Returns the slideshow template
*
* @return TemplateResponse
*/
public function slideshow() {
return new TemplateResponse($this->appName, 'slideshow', [], 'blank');
}
/**
* Returns the parameters to be used in the index function
*
* @param $appName
*
* @return array<string,string>
*/
private function getIndexParameters($appName) {
// Parameters sent to the index function
$params = [
'appName' => $appName,
'uploadUrl' => $this->urlGenerator->linkTo(
'files',
'ajax/upload.php'
),
'publicUploadEnabled' => $this->appConfig->getAppValue(
'core',
'shareapi_allow_public_upload',
'yes'
),
'mailNotificationEnabled' => $this->appConfig->getAppValue(
'core',
'shareapi_allow_mail_notification',
'no'
),
'mailPublicNotificationEnabled' => $this->appConfig->getAppValue(
'core',
'shareapi_allow_public_notification',
'no'
)
];
return $params;
}
/**
* Shows the albums and pictures the token gives access to
*
* @param $token
*
* @return TemplateResponse
*/
private function showPublicPage($token) {
$albumName = $this->environment->getSharedFolderName();
list($server2ServerSharing, $protected) = $this->getServer2ServerProperties();
// Parameters sent to the template
$params = [
'appName' => $this->appName,
'token' => $token,
'displayName' => $this->environment->getDisplayName(),
'albumName' => $albumName,
'server2ServerSharing' => $server2ServerSharing,
'protected' => $protected,
'filename' => $albumName
];
// Will render the page using the template found in templates/public.php
$response = new TemplateResponse($this->appName, 'public', $params, 'public');
$this->addContentSecurityToResponse($response);
return $response;
}
/**
* Determines if we can add external shared to this instance
*
* @return array<bool,string>
*/
private function getServer2ServerProperties() {
$server2ServerSharing = $this->appConfig->getAppValue(
'files_sharing',
'outgoing_server2server_share_enabled',
'yes'
);
$server2ServerSharing = ($server2ServerSharing === 'yes') ? true : false;
$password = $this->environment->getSharePassword();
$passwordProtected = ($password) ? 'true' : 'false';
return [$server2ServerSharing, $passwordProtected];
}
}