Completed
Pull Request — master (#39)
by Olivier
09:42
created

PageController   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 213
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 2 Features 2
Metric Value
wmc 12
c 3
b 2
f 2
lcom 1
cbo 2
dl 0
loc 213
ccs 72
cts 72
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 15 1
B index() 0 32 2
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
			$params = [
99 3
				'appName'                       => $appName,
100 3
				'uploadUrl'                     => $this->urlGenerator->linkTo(
101 3
					'files', 'ajax/upload.php'
102
				),
103 3
				'publicUploadEnabled'           => $this->appConfig->getAppValue(
104 3
					'core', 'shareapi_allow_public_upload', 'yes'
105
				),
106 3
				'mailNotificationEnabled'       => $this->appConfig->getAppValue(
107 3
					'core', 'shareapi_allow_mail_notification', 'no'
108
				),
109 3
				'mailPublicNotificationEnabled' => $this->appConfig->getAppValue(
110 3
					'core', 'shareapi_allow_public_notification', 'no'
111
				)
112
			];
113
114
			// Will render the page using the template found in templates/index.php
115 3
			$response = new TemplateResponse($appName, 'index', $params);
116 3
			$this->addContentSecurityToResponse($response);
117
118 3
			return $response;
119
		}
120
	}
121
122
	/**
123
	 * @PublicPage
124
	 * @NoCSRFRequired
125
	 *
126
	 * Shows the albums and pictures or redirects to the download location the token gives access to
127
	 *
128
	 * @param string $token
129
	 * @param null|string $filename
130
	 *
131
	 * @return TemplateResponse|ImageResponse|RedirectResponse
132
	 */
133 4
	public function publicIndex($token, $filename) {
134 4
		$node = $this->environment->getSharedNode();
135 4
		if ($node->getType() === 'dir') {
136 1
			return $this->showPublicPage($token);
137
		} else {
138 3
			$url = $this->urlGenerator->linkToRoute(
139 3
				$this->appName . '.files_public.download',
140
				[
141 3
					'token'    => $token,
142 3
					'fileId'   => $node->getId(),
143 3
					'filename' => $filename
144
				]
145
			);
146
147 3
			return new RedirectResponse($url);
148
		}
149
	}
150
151
	/**
152
	 * @PublicPage
153
	 * @NoCSRFRequired
154
	 * @Guest
155
	 *
156
	 * Generates an error page based on the error code
157
	 *
158
	 * @param int $code
159
	 *
160
	 * @return TemplateResponse
161
	 */
162 3
	public function errorPage($code) {
163 3
		$appName = $this->appName;
164 3
		$message = $this->request->getCookie('galleryErrorMessage');
165
		$params = [
166 3
			'appName' => $appName,
167 3
			'message' => $message,
168 3
			'code'    => $code,
169
		];
170
171 3
		$errorTemplate = new TemplateResponse($appName, 'index', $params, 'guest');
172 3
		$errorTemplate->setStatus($code);
173 3
		$errorTemplate->invalidateCookie('galleryErrorMessage');
174
175 3
		return $errorTemplate;
176
	}
177
178
	/**
179
	 * Adds the domain "data:" to the allowed image domains
180
	 * this function is called by reference
181
	 *
182
	 * @param TemplateResponse $response
183
	 */
184 4
	private function addContentSecurityToResponse($response) {
185 4
		$csp = new Http\ContentSecurityPolicy();
186 4
		$csp->addAllowedImageDomain("data:");
187 4
		$csp->addAllowedFontDomain("data:");
188 4
		$response->setContentSecurityPolicy($csp);
189 4
	}
190
191
	/**
192
	 * @PublicPage
193
	 * @NoCSRFRequired
194
	 * @Guest
195
	 *
196
	 * Returns the slideshow template
197
	 *
198
	 * @return TemplateResponse
199
	 */
200 1
	public function slideshow() {
201 1
		return new TemplateResponse($this->appName, 'slideshow', [], 'blank');
202
	}
203
204
	/**
205
	 * Shows the albums and pictures the token gives access to
206
	 *
207
	 * @param $token
208
	 *
209
	 * @return TemplateResponse
210
	 */
211 1
	private function showPublicPage($token) {
212 1
		$albumName = $this->environment->getSharedFolderName();
213 1
		list($server2ServerSharing, $protected) = $this->getServer2ServerProperties();
214
215
		// Parameters sent to the template
216
		$params = [
217 1
			'appName'              => $this->appName,
218 1
			'token'                => $token,
219 1
			'displayName'          => $this->environment->getDisplayName(),
220 1
			'albumName'            => $albumName,
221 1
			'server2ServerSharing' => $server2ServerSharing,
222 1
			'protected'            => $protected,
223 1
			'filename'             => $albumName
224
		];
225
226
		// Will render the page using the template found in templates/public.php
227 1
		$response = new TemplateResponse($this->appName, 'public', $params, 'public');
228 1
		$this->addContentSecurityToResponse($response);
229
230 1
		return $response;
231
	}
232
233
	/**
234
	 * Determines if we can add external shared to this instance
235
	 *
236
	 * @return array<bool,string>
237
	 */
238 1
	private function getServer2ServerProperties() {
239 1
		$server2ServerSharing = $this->appConfig->getAppValue(
240 1
			'files_sharing', 'outgoing_server2server_share_enabled', 'yes'
241
		);
242 1
		$server2ServerSharing = ($server2ServerSharing === 'yes') ? true : false;
243 1
		$password = $this->environment->getSharePassword();
244 1
		$passwordProtected = ($password) ? 'true' : 'false';
245
246 1
		return [$server2ServerSharing, $passwordProtected];
247
	}
248
}
249