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

PageController::index()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2

Importance

Changes 3
Bugs 2 Features 2
Metric Value
c 3
b 2
f 2
dl 0
loc 24
ccs 13
cts 13
cp 1
rs 8.9713
cc 2
eloc 16
nc 2
nop 0
crap 2
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('files', 'ajax/upload.php'),
101 3
				'publicUploadEnabled' => $this->appConfig->getAppValue('core', 'shareapi_allow_public_upload', 'yes'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 106 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
102 3
				'mailNotificationEnabled' => $this->appConfig->getAppValue('core', 'shareapi_allow_mail_notification', 'no'),
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 113 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
103 3
				'mailPublicNotificationEnabled' => $this->appConfig->getAppValue('core', 'shareapi_allow_public_notification', 'no')
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 120 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

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