Completed
Push — master ( da5ff1...ff6e7f )
by Olivier
20:05 queued 16:36
created

PageController::getIndexParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 21
ccs 11
cts 11
cp 1
rs 9.3142
cc 1
eloc 12
nc 1
nop 1
crap 1
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 3
			'publicUploadEnabled'           => $this->appConfig->getAppValue(
206 3
				'core', 'shareapi_allow_public_upload', 'yes'
207
			),
208 3
			'mailNotificationEnabled'       => $this->appConfig->getAppValue(
209 3
				'core', 'shareapi_allow_mail_notification', 'no'
210
			),
211 3
			'mailPublicNotificationEnabled' => $this->appConfig->getAppValue(
212 3
				'core', 'shareapi_allow_public_notification', 'no'
213
			)
214
		];
215
216 3
		return $params;
217
	}
218
219
	/**
220
	 * Shows the albums and pictures the token gives access to
221
	 *
222
	 * @param $token
223
	 *
224
	 * @return TemplateResponse
225
	 */
226 1
	private function showPublicPage($token) {
227 1
		$albumName = $this->environment->getSharedFolderName();
228 1
		list($server2ServerSharing, $protected) = $this->getServer2ServerProperties();
229
230
		// Parameters sent to the template
231
		$params = [
232 1
			'appName'              => $this->appName,
233 1
			'token'                => $token,
234 1
			'displayName'          => $this->environment->getDisplayName(),
235 1
			'albumName'            => $albumName,
236 1
			'server2ServerSharing' => $server2ServerSharing,
237 1
			'protected'            => $protected,
238 1
			'filename'             => $albumName
239
		];
240
241
		// Will render the page using the template found in templates/public.php
242 1
		$response = new TemplateResponse($this->appName, 'public', $params, 'public');
243 1
		$this->addContentSecurityToResponse($response);
244
245 1
		return $response;
246
	}
247
248
	/**
249
	 * Determines if we can add external shared to this instance
250
	 *
251
	 * @return array<bool,string>
252
	 */
253 1
	private function getServer2ServerProperties() {
254 1
		$server2ServerSharing = $this->appConfig->getAppValue(
255 1
			'files_sharing', 'outgoing_server2server_share_enabled', 'yes'
256
		);
257 1
		$server2ServerSharing = ($server2ServerSharing === 'yes') ? true : false;
258 1
		$password = $this->environment->getSharePassword();
259 1
		$passwordProtected = ($password) ? 'true' : 'false';
260
261 1
		return [$server2ServerSharing, $passwordProtected];
262
	}
263
}
264