Completed
Pull Request — master (#822)
by
unknown
19:08
created

ViewController::publicIndexForEmbedding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 15
ccs 11
cts 11
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 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 7
	public function index() {
74 7
		$templateParameters = $this->getTemplateParams();
75
76 7
		$user = $this->userSession->getUser();
77 7
		$userId = $user->getUID();
78 7
		$emailAddress = $user->getEMailAddress();
79
80 7
		$initialView = $this->config->getUserValue($userId, $this->appName, 'currentView', null);
81 7
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
82 7
		$weekNumbers = $this->config->getUserValue($userId, $this->appName, 'showWeekNr', 'no');
83 7
		$firstRun = $this->config->getUserValue($userId, $this->appName, 'firstRun', null);
84 7
		$timezone = $this->config->getUserValue($userId, $this->appName, 'timezone', 'automatic');
85
86
		// the default view will be saved as soon as a user
87
		// opens the calendar app, therefore this is a good
88
		// indication if the calendar was used before
89 7
		if ($firstRun === null) {
90 2
			if ($initialView === null) {
91 1
				$firstRun = 'yes';
92
			} else {
93 1
				$this->config->setUserValue($userId, $this->appName, 'firstRun', 'no');
94 1
				$firstRun = 'no';
95
			}
96
		}
97
98 7
		if ($initialView === null) {
99 2
			$initialView = 'month';
100
		}
101
102 7
		return new TemplateResponse('calendar', 'main', array_merge($templateParameters, [
103 7
			'initialView' => $initialView,
104 7
			'emailAddress' => $emailAddress,
105 7
			'skipPopover' => $skipPopover,
106 7
			'weekNumbers' => $weekNumbers,
107 7
			'firstRun' => $firstRun,
108
			'isPublic' => false,
109
			'isEmbedded' => false,
110 7
			'token' => '',
111 7
			'timezone' => $timezone,
112
		]));
113
	}
114
115
	/**
116
	 * @PublicPage
117
	 * @NoCSRFRequired
118
	 *
119
	 * @param string $token
120
	 *
121
	 * @return TemplateResponse
122
	 */
123 4
	public function publicIndexWithBranding($token) {
124 4
		$templateParameters = $this->getTemplateParams();
125 4
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
126 4
		$params = array_merge($templateParameters, $publicTemplateParameters);
127 4
		$params['isEmbedded'] = false;
128
129 4
		return new TemplateResponse('calendar', 'public', $params, 'base');
130
	}
131
132
	/**
133
         * @PublicPage
134
         * @NoCSRFRequired
135
         *
136
         * @param string $token
137
         * @param string $rendering
138
         * @param string $passwordtoken
0 ignored issues
show
Documentation introduced by
There is no parameter named $passwordtoken. Did you maybe mean $passwordToken?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
139
         * @param string $otolayerid
0 ignored issues
show
Documentation introduced by
There is no parameter named $otolayerid. Did you maybe mean $oToLayerID?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
140
         * @return TemplateResponse
141
         */
142
        public function publicIndexScheduling($token,$rendering,$passwordToken,$oToLayerID) {
143
                $templateParameters = $this->getTemplateParams();
144
                $publicTemplateParameters = $this->getPublicTemplateParameters($token);
145
                $params = array_merge($templateParameters, $publicTemplateParameters);
146
                $params = array_merge($params, ['rendering' => $rendering]);
147
		$params['passwordToken'] = $passwordToken;
148
		$params['oToLayerID'] = $oToLayerID;
149
                $params['isEmbedded'] = false;
150
                return new TemplateResponse('calendar', 'public', $params, 'base');
151
        }
152 4
153 4
	/**
154 4
         * @PublicPage
155 4
         * @NoCSRFRequired
156 4
         *
157
         * @param string $token
158 4
         * @param string $rendering
159
         * @return TemplateResponse
160 4
         */
161 4
        public function publicIndexRendering($token,$rendering) {
162 4
                $templateParameters = $this->getTemplateParams();
163 4
                $publicTemplateParameters = $this->getPublicTemplateParameters($token);
164
                $params = array_merge($templateParameters, $publicTemplateParameters);
165 4
		$params = array_merge($params, ['rendering' => $rendering]);
166
		$params['isEmbedded'] = false;
167
                return new TemplateResponse('calendar', 'public', $params, 'base');
168
        }
169
170
	/**
171
	 * @PublicPage
172
	 * @NoCSRFRequired
173
	 *
174
	 * @param string $token
175
	 *
176
	 * @return TemplateResponse
177
	 */
178
	public function publicIndexWithBrandingAndFancyName($token) {
179
		return $this->publicIndexWithBranding($token);
180
	}
181
182
	/**
183
	 * @PublicPage
184 15
	 * @NoCSRFRequired
185 15
	 *
186 15
	 * @param string $token
187
	 *
188 15
	 * @return TemplateResponse
189 15
	 */
190
	public function publicIndexForEmbedding($token) {
191 15
		$templateParameters = $this->getTemplateParams();
192 15
		$publicTemplateParameters = $this->getPublicTemplateParameters($token);
193 15
		$params = array_merge($templateParameters, $publicTemplateParameters);
194 15
		$params['isEmbedded'] = true;
195
196
		$response = new TemplateResponse('calendar', 'main', $params, 'public');
197 15
198 15
		$response->addHeader('X-Frame-Options', 'ALLOW');
199 15
		$csp = new ContentSecurityPolicy();
200 15
		$csp->addAllowedScriptDomain('*');
201 15
		$response->setContentSecurityPolicy($csp);
202 15
203 15
		return $response;
204
	}
205
206
	/**
207
	 * @PublicPage
208
	 * @NoCSRFRequired
209
	 *
210
	 * @param string $token
211
	 *
212 8
	 * @return TemplateResponse
213 8
	 */
214 8
	public function publicIndexForEmbeddingLegacy($token) {
215 8
		return $this->publicIndexForEmbedding($token);
216
	}
217 8
218 8
	/**
219
	 * get common parameters used for all three routes
220 8
	 * @return array
221 8
	 */
222
	private function getTemplateParams() {
223 8
		$runningOn = $this->config->getSystemValue('version');
224
		$runningOnNextcloud12OrLater = version_compare($runningOn, '12', '>=');
225 8
226 8
		$shareeCanEditShares = !$runningOnNextcloud12OrLater;
227
		$shareeCanEditCalendarProperties = $runningOnNextcloud12OrLater;
228
229 8
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
230 8
		$isIE = $this->request->isUserAgent([Request::USER_AGENT_IE]);
231 8
		$defaultColor = $this->config->getAppValue('theming', 'color', '#0082C9');
232 8
		$canSharePublicLink = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
233 8
234
		return [
235 8
			'appVersion' => $appVersion,
236 8
			'isIE' => $isIE,
237 8
			'defaultColor' => $defaultColor,
238 8
			'shareeCanEditShares' => $shareeCanEditShares ? 'yes' : 'no',
239 8
			'shareeCanEditCalendarProperties' => $shareeCanEditCalendarProperties ? 'yes' : 'no',
240 8
			'canSharePublicLink' => $canSharePublicLink,
241
			'timezone' => 'automatic',
242
			'passwordToken' => '',
243
                        'oToLayerID' => '',
244
		];
245
	}
246
247
	/**
248
	 * get common parameters for public sites
249
	 * @param string $token
250
	 * @return array
251
	 */
252
	private function getPublicTemplateParameters($token) {
253
		$shareURL = $this->request->getServerProtocol() . '://';
254
		$shareURL .= $this->request->getServerHost();
255
		$shareURL .= $this->request->getRequestUri();
256
257
		$relativeImagePath = $this->urlGenerator->imagePath('core', 'favicon-touch.png');
258
		$previewImage = $this->urlGenerator->getAbsoluteURL($relativeImagePath);
259
260
		$remoteBase = $this->urlGenerator->linkTo('', 'remote.php');
261
		$remoteBase .= '/dav/public-calendars/' . $token . '?export';
262
263
		$downloadUrl = $this->urlGenerator->getAbsoluteURL($remoteBase);
264
265
		$protocolLength = strlen($this->request->getServerProtocol()) + 3;
266
		$webcalUrl = 'webcal://' . substr($downloadUrl, $protocolLength);
267
268
		return [
269
			'initialView' => 'month',
270
			'emailAddress' => '',
271
			'skipPopover' => 'no',
272
			'weekNumbers' => 'no',
273
			'firstRun' => 'no',
274
			'isPublic' => true,
275
			'shareURL' => $shareURL,
276
			'previewImage' => $previewImage,
277
			'webcalURL' => $webcalUrl,
278
			'downloadURL' => $downloadUrl,
279
			'token' => $token,
280
			'timezone' => 'automatic',
281
		];
282
	}
283
}
284