Passed
Pull Request — master (#372)
by Georg
05:28
created

ViewController::index()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 41
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 26
nc 7
nop 0
dl 0
loc 41
ccs 24
cts 24
cp 1
crap 5
rs 8.439
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
		]));
113
	}
114
115
	/**
116
	 * @PublicPage
117
	 * @NoCSRFRequired
118
	 *
119
	 * @param string $token
120
	 *
121
	 * @return TemplateResponse
122
	 */
123
	public function publicIndexWithBranding($token) {
124
		if ($this->needsAssetPipelineWarning()) {
125
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
126
		}
127
128
		$templateParameters = $this->getTemplateParams();
129
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
130
		$params = array_merge($templateParameters, $publicTemplateParameters);
131
132
		return new TemplateResponse('calendar', 'public', $params, 'base');
133
	}
134
135
	/**
136
	 * @PublicPage
137
	 * @NoCSRFRequired
138
	 *
139
	 * @param string $token
140
	 *
141
	 * @return TemplateResponse
142
	 */
143 5
	public function publicIndex($token) {
144 5
		if ($this->needsAssetPipelineWarning()) {
145
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
146
		}
147
148 5
		$templateParameters = $this->getTemplateParams();
149 5
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
150 5
		$params = array_merge($templateParameters, $publicTemplateParameters);
151
152 5
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
153
154 5
		$response->addHeader('X-Frame-Options', 'ALLOW');
155 5
		$csp = new ContentSecurityPolicy();
156 5
		$csp->addAllowedScriptDomain('*');
157 5
		$response->setContentSecurityPolicy($csp);
158
159 5
		return $response;
160
	}
161
162
	/**
163
	 * get common parameters used for all three routes
164
	 * @return array
165
	 */
166 13
	private function getTemplateParams() {
167 13
		$runningOn = $this->config->getSystemValue('version');
168 13
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
169 13
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
170
171 13
		$supportsClass = $runningOnNextcloud10OrLater;
172 13
		$needsAutosize = !$runningOnNextcloud11OrLater;
173
174 13
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
175 13
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
176 13
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
177 13
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
178
179
		return [
180 13
			'appVersion' => $appVersion,
181 13
			'supportsClass' => $supportsClass,
182 13
			'isIE' => $isIE,
183 13
			'webCalWorkaround' => $webCalWorkaround,
184 13
			'needsAutosize' => $needsAutosize,
185 13
			'defaultColor' => $defaultColor,
186
		];
187
	}
188
189
	/**
190
	 * get common parameters for public sites
191
	 * @param string $token
192
	 * @return array
193
	 */
194 5
	private function getPublicTemplateParameters($token) {
195 5
		$shareURL = $this->request->getServerProtocol() . '://';
196 5
		$shareURL .= $this->request->getServerHost();
197 5
		$shareURL .= $this->request->getRequestUri();
198
199 5
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
200 5
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
201
202 5
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
203 5
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
204
205 5
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
206
207 5
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
208 5
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
209
210
		return [
211 5
			'initialView' => 'month',
212 5
			'emailAddress' => '',
213 5
			'skipPopover' => 'no',
214 5
			'weekNumbers' => 'no',
215 5
			'firstRun' => 'no',
216 5
			'webCalWorkaround' => 'no',
217
			'isPublic' => true,
218 5
			'shareURL' => $shareURL,
219 5
			'previewImage' => $previewImage,
220 5
			'webcalURL' => $webcalUrl,
221 5
			'downloadURL' => $downloadUrl,
222
		];
223
	}
224
225
	/**
226
	 * check if we need to show the asset pipeline warning
227
	 * @return bool
228
	 */
229 15
	private function needsAssetPipelineWarning() {
230 15
		$runningOn = $this->config->getSystemValue('version');
231 15
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
232 15
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
233
234 15
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
235
	}
236
}
237