Issues (263)

lib/Controller/PageController.php (13 issues)

1
<?php
2
/**
3
 * Nextcloud - Gallery
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Robin Appelman <[email protected]>
9
 * @author Olivier Paroz <[email protected]>
10
 *
11
 * @copyright Robin Appelman 2017
12
 * @copyright Olivier Paroz 2017
13
 */
14
15
namespace OCA\Gallery\Controller;
16
17
use OCP\AppFramework\Http\Template\ExternalShareMenuAction;
0 ignored issues
show
The type OCP\AppFramework\Http\Te...ExternalShareMenuAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use OCP\AppFramework\Http\Template\LinkMenuAction;
0 ignored issues
show
The type OCP\AppFramework\Http\Template\LinkMenuAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use OCP\AppFramework\Http\Template\PublicTemplateResponse;
0 ignored issues
show
The type OCP\AppFramework\Http\Te...\PublicTemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use OCP\AppFramework\Http\Template\SimpleMenuAction;
0 ignored issues
show
The type OCP\AppFramework\Http\Template\SimpleMenuAction was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use OCP\IL10N;
0 ignored issues
show
The type OCP\IL10N was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use OCP\IURLGenerator;
0 ignored issues
show
The type OCP\IURLGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use OCP\IRequest;
0 ignored issues
show
The type OCP\IRequest was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use OCP\IConfig;
0 ignored issues
show
The type OCP\IConfig was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
26
use OCP\AppFramework\Controller;
0 ignored issues
show
The type OCP\AppFramework\Controller was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use OCP\AppFramework\Http;
0 ignored issues
show
The type OCP\AppFramework\Http was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use OCP\AppFramework\Http\TemplateResponse;
0 ignored issues
show
The type OCP\AppFramework\Http\TemplateResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use OCP\AppFramework\Http\RedirectResponse;
0 ignored issues
show
The type OCP\AppFramework\Http\RedirectResponse was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
31
use OCA\Gallery\Environment\Environment;
32
use OCA\Gallery\Http\ImageResponse;
33
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
34
35
/**
36
 * Generates templates for the landing page from within ownCloud, the public
37
 * gallery and error pages
38
 *
39
 * @package OCA\Gallery\Controller
40
 */
41
class PageController extends Controller {
42
43
	/** @var Environment */
44
	private $environment;
45
	/** @var IURLGenerator */
46
	private $urlGenerator;
47
	/** @var IConfig */
48
	private $appConfig;
49
	/** @var EventDispatcherInterface */
50
	private $dispatcher;
51
	/** @var IL10N */
52
	private $l10n;
53
54
	/**
55
	 * Constructor
56
	 *
57
	 * @param string $appName
58
	 * @param IRequest $request
59
	 * @param Environment $environment
60
	 * @param IURLGenerator $urlGenerator
61
	 * @param IConfig $appConfig
62
	 * @param EventDispatcherInterface $dispatcher
63
	 * @param IL10N $l10n
64
	 */
65
	public function __construct(
66
		$appName,
67
		IRequest $request,
68
		Environment $environment,
69
		IURLGenerator $urlGenerator,
70
		IConfig $appConfig,
71
		EventDispatcherInterface $dispatcher,
72
		IL10N $l10n
73
	) {
74
		parent::__construct($appName, $request);
75
76
		$this->environment = $environment;
77
		$this->urlGenerator = $urlGenerator;
78
		$this->appConfig = $appConfig;
79
		$this->dispatcher = $dispatcher;
80
		$this->l10n = $l10n;
81
	}
82
83
	/**
84
	 * @NoAdminRequired
85
	 * @NoCSRFRequired
86
	 *
87
	 * Shows the albums and pictures at the root folder or a message if
88
	 * there are no pictures.
89
	 *
90
	 * This is the entry page for logged-in users accessing the app from
91
	 * within ownCloud.
92
	 * A TemplateResponse response uses a template from the templates folder
93
	 * and parameters provided here to build the page users will see
94
	 *
95
	 * @return TemplateResponse
96
	 */
97
	public function index() {
98
		$appName = $this->appName;
99
100
		// Parameters sent to the template
101
		$params = $this->getIndexParameters($appName);
102
103
		$this->dispatcher->dispatch('OCP\Share::loadSocial');
104
105
		// Will render the page using the template found in templates/index.php
106
		$response = new TemplateResponse($appName, 'index', $params);
107
		$this->addContentSecurityToResponse($response);
108
109
		return $response;
110
	}
111
112
	/**
113
	 * @PublicPage
114
	 * @NoCSRFRequired
115
	 *
116
	 * Shows the albums and pictures or redirects to the download location the token gives access to
117
	 *
118
	 * @param string $token
119
	 * @param null|string $filename
120
	 *
121
	 * @return PublicTemplateResponse|ImageResponse|RedirectResponse
122
	 */
123
	public function publicIndex($token, $filename) {
124
		$node = $this->environment->getSharedNode();
125
		if ($node->getType() === 'dir') {
126
			return $this->showPublicPage($token);
127
		} else {
128
			$url = $this->urlGenerator->linkToRoute(
129
				$this->appName . '.files_public.download',
130
				[
131
					'token'    => $token,
132
					'fileId'   => $node->getId(),
133
					'filename' => $filename
134
				]
135
			);
136
137
			return new RedirectResponse($url);
138
		}
139
	}
140
141
	/**
142
	 * @PublicPage
143
	 * @NoCSRFRequired
144
	 * @Guest
145
	 *
146
	 * Generates an error page based on the error code
147
	 *
148
	 * @param int $code
149
	 *
150
	 * @return TemplateResponse
151
	 */
152
	public function errorPage($code) {
153
		$appName = $this->appName;
154
		$message = $this->request->getCookie('galleryErrorMessage');
155
		$params = [
156
			'appName' => $appName,
157
			'message' => $message,
158
			'code'    => $code,
159
		];
160
161
		$errorTemplate = new TemplateResponse($appName, 'index', $params, 'guest');
162
		$errorTemplate->setStatus($code);
163
		$errorTemplate->invalidateCookie('galleryErrorMessage');
164
165
		return $errorTemplate;
166
	}
167
168
	/**
169
	 * Adds the domain "data:" to the allowed image domains
170
	 * this function is called by reference
171
	 *
172
	 * @param TemplateResponse $response
173
	 */
174
	private function addContentSecurityToResponse($response) {
175
		$csp = new Http\ContentSecurityPolicy();
0 ignored issues
show
The type OCP\AppFramework\Http\ContentSecurityPolicy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
176
		$csp->addAllowedFontDomain("data:");
177
		$response->setContentSecurityPolicy($csp);
178
	}
179
180
	/**
181
	 * @PublicPage
182
	 * @NoCSRFRequired
183
	 * @Guest
184
	 *
185
	 * Returns the slideshow template
186
	 *
187
	 * @return TemplateResponse
188
	 */
189
	public function slideshow() {
190
		return new TemplateResponse($this->appName, 'slideshow', [], 'blank');
191
	}
192
193
	/**
194
	 * Returns the parameters to be used in the index function
195
	 *
196
	 * @param $appName
197
	 *
198
	 * @return array<string,string>
199
	 */
200
	private function getIndexParameters($appName) {
201
202
		// Parameters sent to the index function
203
		$params = [
204
			'appName' => $appName,
205
			'uploadUrl' => $this->urlGenerator->linkTo(
206
				'files', 'ajax/upload.php'
207
			),
208
			'publicUploadEnabled' => $this->appConfig->getAppValue(
209
				'core', 'shareapi_allow_public_upload', 'yes'
210
			),
211
			'mailNotificationEnabled' => $this->appConfig->getAppValue(
212
				'core', 'shareapi_allow_mail_notification', 'no'
213
			),
214
			'mailPublicNotificationEnabled' => $this->appConfig->getAppValue(
215
				'core', 'shareapi_allow_public_notification', 'no'
216
			)
217
		];
218
219
		return $params;
220
	}
221
222
	/**
223
	 * Shows the albums and pictures the token gives access to
224
	 *
225
	 * @param $token
226
	 *
227
	 * @return TemplateResponse
228
	 */
229
	private function showPublicPage($token) {
230
		$albumName = $this->environment->getSharedFolderName();
231
		list($server2ServerSharing, $protected) = $this->getServer2ServerProperties();
232
		$downloadUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $token]);
233
234
		// Parameters sent to the template
235
		$params = [
236
			'appName'              => $this->appName,
237
			'token'                => $token,
238
			'displayName'          => $this->environment->getDisplayName(),
239
			'albumName'            => $albumName,
240
			'server2ServerSharing' => $server2ServerSharing,
241
			'protected'            => $protected,
242
			'filename'             => $albumName
243
		];
244
245
		// Will render the page using the template found in templates/public.php
246
		$response = new PublicTemplateResponse($this->appName, 'public', $params);
247
		$response->setHeaderTitle($params['albumName']);
248
		$response->setHeaderDetails($this->l10n->t('shared by %s', [$params['displayName']]));
249
		$response->setHeaderActions([
250
			new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $downloadUrl, 0),
251
			new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $downloadUrl, 10),
252
			new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $downloadUrl),
253
			new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $this->environment->getUserId(), $params['displayName'], $params['albumName'])
254
		]);
255
		$this->addContentSecurityToResponse($response);
256
257
		return $response;
258
	}
259
260
	/**
261
	 * Determines if we can add external shared to this instance
262
	 *
263
	 * @return array<bool,string>
264
	 */
265
	private function getServer2ServerProperties() {
266
		$server2ServerSharing = $this->appConfig->getAppValue(
267
			'files_sharing', 'outgoing_server2server_share_enabled', 'yes'
268
		);
269
		$server2ServerSharing = ($server2ServerSharing === 'yes') ? true : false;
270
		$password = $this->environment->getSharePassword();
271
		$passwordProtected = ($password) ? 'true' : 'false';
272
273
		return [$server2ServerSharing, $passwordProtected];
274
	}
275
}
276