Passed
Pull Request — master (#372)
by Georg
04:34
created

ViewController::publicIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Calendar App
4
 *
5
 * @author Georg Ehrke
6
 * @copyright 2016 Georg Ehrke <[email protected]>
7
 * @author Raghu Nayyar
8
 * @copyright 2016 Raghu Nayyar <[email protected]>
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
12
 * License as published by the Free Software Foundation; either
13
 * version 3 of the License, or any later version.
14
 *
15
 * This library is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public
21
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\Calendar\Controller;
25
26
use OC\AppFramework\Http\Request;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Http\ContentSecurityPolicy;
29
use OCP\AppFramework\Http\TemplateResponse;
30
use OCP\IConfig;
31
use OCP\IRequest;
32
use OCP\IUserSession;
33
use OCP\IURLGenerator;
34
35
class ViewController extends Controller {
36
37
	/**
38
	 * @var IConfig
39
	 */
40
	private $config;
41
42
	/**
43
	 * @var IURLGenerator
44
	 */
45
	private $urlGenerator;
46
47
	/**
48
	 * @var IUserSession
49
	 */
50
	private $userSession;
51
52
	/**
53
	 * @param string $appName
54
	 * @param IRequest $request an instance of the request
55
	 * @param IUserSession $userSession
56
	 * @param IConfig $config
57
	 * @param IURLGenerator $urlGenerator
58
	 */
59 15
	public function __construct($appName, IRequest $request, IUserSession $userSession,
60
								IConfig $config, IURLGenerator $urlGenerator) {
61 15
		parent::__construct($appName, $request);
62 15
		$this->config = $config;
63 15
		$this->userSession = $userSession;
64 15
		$this->urlGenerator = $urlGenerator;
65 15
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 *
71
	 * @return TemplateResponse
72
	 */
73 10
	public function index() {
74 10
		if ($this->needsAssetPipelineWarning()) {
75 2
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
76
		}
77
78 8
		$templateParameters = $this->getTemplateParams();
79
80 8
		$user = $this->userSession->getUser();
81 8
		$userId = $user->getUID();
82 8
		$emailAddress = $user->getEMailAddress();
83
84 8
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
85 8
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
86 8
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
87 8
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
88
89
		// the default view will be saved as soon as a user
90
		// opens the calendar app, therefore this is a good
91
		// indication if the calendar was used before
92 8
		if ($firstRun === null) {
93 2
			if ($initialView === null) {
94 1
				$firstRun = 'yes';
95
			} else {
96 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
97 1
				$firstRun = 'no';
98
			}
99
		}
100
101 8
		if ($initialView === null) {
102 2
			$initialView = 'month';
103
		}
104
105 8
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
106 8
			'initialView' => $initialView,
107 8
			'emailAddress' => $emailAddress,
108 8
			'skipPopover' => $skipPopover,
109 8
			'weekNumbers' => $weekNumbers,
110 8
			'firstRun' => $firstRun,
111
			'isPublic' => false,
112 8
			'token' => '',
113
		]));
114
	}
115
116
	/**
117
	 * @PublicPage
118
	 * @NoCSRFRequired
119
	 *
120
	 * @param string $token
121
	 *
122
	 * @return TemplateResponse
123
	 */
124
	public function publicIndexWithBranding($token) {
125
		if ($this->needsAssetPipelineWarning()) {
126
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
127
		}
128
129
		$templateParameters = $this->getTemplateParams();
130
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
131
		$params = array_merge($templateParameters, $publicTemplateParameters);
132
133
		return new TemplateResponse('calendar', 'public', $params, 'base');
134
	}
135
136
	/**
137
	 * @PublicPage
138
	 * @NoCSRFRequired
139
	 *
140
	 * @param string $token
141
	 *
142
	 * @return TemplateResponse
143
	 */
144 5
	public function publicIndex($token) {
145 5
		if ($this->needsAssetPipelineWarning()) {
146
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
147
		}
148
149 5
		$templateParameters = $this->getTemplateParams();
150 5
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
151 5
		$params = array_merge($templateParameters, $publicTemplateParameters);
152
153 5
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
154
155 5
		$response->addHeader('X-Frame-Options', 'ALLOW');
156 5
		$csp = new ContentSecurityPolicy();
157 5
		$csp->addAllowedScriptDomain('*');
158 5
		$response->setContentSecurityPolicy($csp);
159
160 5
		return $response;
161
	}
162
163
	/**
164
	 * get common parameters used for all three routes
165
	 * @return array
166
	 */
167 13
	private function getTemplateParams() {
168 13
		$runningOn = $this->config->getSystemValue('version');
169 13
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
170 13
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
171
172 13
		$supportsClass = $runningOnNextcloud10OrLater;
173 13
		$needsAutosize = !$runningOnNextcloud11OrLater;
174
175 13
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
176 13
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
177 13
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
178 13
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
179
180
		return [
181 13
			'appVersion' => $appVersion,
182 13
			'supportsClass' => $supportsClass,
183 13
			'isIE' => $isIE,
184 13
			'webCalWorkaround' => $webCalWorkaround,
185 13
			'needsAutosize' => $needsAutosize,
186 13
			'defaultColor' => $defaultColor,
187
		];
188
	}
189
190
	/**
191
	 * get common parameters for public sites
192
	 * @param string $token
193
	 * @return array
194
	 */
195 5
	private function getPublicTemplateParameters($token) {
196 5
		$shareURL = $this->request->getServerProtocol() . '://';
197 5
		$shareURL .= $this->request->getServerHost();
198 5
		$shareURL .= $this->request->getRequestUri();
199
200 5
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
201 5
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
202
203 5
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
204 5
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
205
206 5
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
207
208 5
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
209 5
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
210
211
		return [
212 5
			'initialView' => 'month',
213 5
			'emailAddress' => '',
214 5
			'skipPopover' => 'no',
215 5
			'weekNumbers' => 'no',
216 5
			'firstRun' => 'no',
217 5
			'webCalWorkaround' => 'no',
218
			'isPublic' => true,
219 5
			'shareURL' => $shareURL,
220 5
			'previewImage' => $previewImage,
221 5
			'webcalURL' => $webcalUrl,
222 5
			'downloadURL' => $downloadUrl,
223 5
			'token' => $token,
224
		];
225
	}
226
227
	/**
228
	 * check if we need to show the asset pipeline warning
229
	 * @return bool
230
	 */
231 15
	private function needsAssetPipelineWarning() {
232 15
		$runningOn = $this->config->getSystemValue('version');
233 15
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
234 15
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
235
236 15
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
237
	}
238
}
239