Completed
Push — stable8.2 ( 09e830...ae9bfd )
by Olivier
12:09
created

PageController   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 219
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 13
c 2
b 0
f 1
lcom 1
cbo 2
dl 0
loc 219
ccs 69
cts 69
cp 1
rs 10

9 Methods

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