Passed
Push — master ( 33a369...69c51b )
by Georg
31s
created

ViewController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 5
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
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 27
	public function __construct($appName, IRequest $request, IUserSession $userSession,
60
								IConfig $config, IURLGenerator $urlGenerator) {
61 27
		parent::__construct($appName, $request);
62 27
		$this->config = $config;
63 27
		$this->userSession = $userSession;
64 27
		$this->urlGenerator = $urlGenerator;
65 27
	}
66
67
	/**
68
	 * @NoAdminRequired
69
	 * @NoCSRFRequired
70
	 *
71
	 * @return TemplateResponse
72
	 */
73 13
	public function index() {
74 13
		if ($this->needsAssetPipelineWarning()) {
75 3
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
76
		}
77
78 10
		$templateParameters = $this->getTemplateParams();
79
80 10
		$user = $this->userSession->getUser();
81 10
		$userId = $user->getUID();
82 10
		$emailAddress = $user->getEMailAddress();
83
84 10
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
85 10
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
86 10
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
87 10
		$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 10
		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 10
		if ($initialView === null) {
102 2
			$initialView = 'month';
103
		}
104
105 10
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
106 10
			'initialView' => $initialView,
107 10
			'emailAddress' => $emailAddress,
108 10
			'skipPopover' => $skipPopover,
109 10
			'weekNumbers' => $weekNumbers,
110 10
			'firstRun' => $firstRun,
111
			'isPublic' => false,
112
			'isEmbedded' => false,
113 10
			'token' => '',
114
		]));
115
	}
116
117
	/**
118
	 * @PublicPage
119
	 * @NoCSRFRequired
120
	 *
121
	 * @param string $token
122
	 *
123
	 * @return TemplateResponse
124
	 */
125 7
	public function publicIndexWithBranding($token) {
126 7
		if ($this->needsAssetPipelineWarning()) {
127
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
128
		}
129
130 7
		$templateParameters = $this->getTemplateParams();
131 7
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
132 7
		$params = array_merge($templateParameters, $publicTemplateParameters);
133 7
		$params['isEmbedded'] = false;
134
135 7
		return new TemplateResponse('calendar', 'public', $params, 'base');
136
	}
137
138
	/**
139
	 * @PublicPage
140
	 * @NoCSRFRequired
141
	 *
142
	 * @param string $token
143
	 *
144
	 * @return TemplateResponse
145
	 */
146 7
	public function publicIndexForEmbedding($token) {
147 7
		if ($this->needsAssetPipelineWarning()) {
148
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
149
		}
150
151 7
		$templateParameters = $this->getTemplateParams();
152 7
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
153 7
		$params = array_merge($templateParameters, $publicTemplateParameters);
154 7
		$params['isEmbedded'] = true;
155
156 7
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
157
158 7
		$response->addHeader('X-Frame-Options', 'ALLOW');
159 7
		$csp = new ContentSecurityPolicy();
160 7
		$csp->addAllowedScriptDomain('*');
161 7
		$response->setContentSecurityPolicy($csp);
162
163 7
		return $response;
164
	}
165
166
	/**
167
	 * @PublicPage
168
	 * @NoCSRFRequired
169
	 *
170
	 * @param string $token
171
	 *
172
	 * @return TemplateResponse
173
	 */
174
	public function publicIndexForEmbeddingLegacy($token) {
175
		return $this->publicIndexForEmbedding($token);
176
	}
177
178
	/**
179
	 * get common parameters used for all three routes
180
	 * @return array
181
	 */
182 24
	private function getTemplateParams() {
183 24
		$runningOn = $this->config->getSystemValue('version');
184 24
		$runningOnNextcloud10OrLater = version_compare($runningOn, '9.1', '>=');
185 24
		$runningOnNextcloud11OrLater = version_compare($runningOn, '11', '>=');
186 24
		$runningOnNextcloud12OrLater = version_compare($runningOn, '12', '>=');
187
188 24
		$supportsClass = $runningOnNextcloud10OrLater;
189 24
		$needsAutosize = !$runningOnNextcloud11OrLater;
190 24
		$shareeCanEditShares = !$runningOnNextcloud12OrLater;
191 24
		$shareeCanEditCalendarProperties = $runningOnNextcloud12OrLater;
192
193 24
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
194 24
		$webCalWorkaround = $runningOnNextcloud10OrLater ? 'no' : 'yes';
195 24
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
196 24
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
197
198
		return [
199 24
			'appVersion' => $appVersion,
200 24
			'supportsClass' => $supportsClass,
201 24
			'isIE' => $isIE,
202 24
			'webCalWorkaround' => $webCalWorkaround,
203 24
			'needsAutosize' => $needsAutosize,
204 24
			'defaultColor' => $defaultColor,
205 24
			'shareeCanEditShares' => $shareeCanEditShares ? 'yes' : 'no',
206 24
			'shareeCanEditCalendarProperties' => $shareeCanEditCalendarProperties ? 'yes' : 'no',
207
		];
208
	}
209
210
	/**
211
	 * get common parameters for public sites
212
	 * @param string $token
213
	 * @return array
214
	 */
215 14
	private function getPublicTemplateParameters($token) {
216 14
		$shareURL = $this->request->getServerProtocol() . '://';
217 14
		$shareURL .= $this->request->getServerHost();
218 14
		$shareURL .= $this->request->getRequestUri();
219
220 14
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
221 14
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
222
223 14
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
224 14
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
225
226 14
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
227
228 14
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
229 14
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
230
231
		return [
232 14
			'initialView' => 'month',
233 14
			'emailAddress' => '',
234 14
			'skipPopover' => 'no',
235 14
			'weekNumbers' => 'no',
236 14
			'firstRun' => 'no',
237 14
			'webCalWorkaround' => 'no',
238
			'isPublic' => true,
239 14
			'shareURL' => $shareURL,
240 14
			'previewImage' => $previewImage,
241 14
			'webcalURL' => $webcalUrl,
242 14
			'downloadURL' => $downloadUrl,
243 14
			'token' => $token,
244
		];
245
	}
246
247
	/**
248
	 * check if we need to show the asset pipeline warning
249
	 * @return bool
250
	 */
251 27
	private function needsAssetPipelineWarning() {
252 27
		$runningOn = $this->config->getSystemValue('version');
253 27
		$assetPipelineBroken = version_compare($runningOn, '9.1', '<');
254 27
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
255
256 27
		return ($isAssetPipelineEnabled && $assetPipelineBroken);
257
	}
258
}
259