Completed
Pull Request — master (#605)
by Thomas
03:42
created

ViewController   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 231
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 94.59%

Importance

Changes 9
Bugs 1 Features 0
Metric Value
c 9
b 1
f 0
dl 0
loc 231
rs 10
ccs 35
cts 37
cp 0.9459
wmc 16
lcom 2
cbo 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
B index() 0 29 3
B publicIndex() 0 28 3
A getTimezone() 0 11 2
A getTimezoneWithRegion() 0 3 1
A getTimezoneWithSubRegion() 0 3 1
A getTimezoneList() 0 7 1
A sendEmailPublicLink() 0 12 1
A sendEmail() 0 22 3
1
<?php
2
/**
3
 * ownCloud - 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;
27
use OC\L10N\L10N;
28
use OCP\AppFramework\Controller;
29
use OCP\AppFramework\Http\ContentSecurityPolicy;
30
use OCP\AppFramework\Http\DataDisplayResponse;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\AppFramework\Http\NotFoundResponse;
33
use OCP\AppFramework\Http\TemplateResponse;
34
use OCP\Defaults;
35
use OCP\IConfig;
36
use OCP\IRequest;
37
use OCP\IUserSession;
38
use OCP\Mail\IMailer;
39
40
class ViewController extends Controller {
41
42
	/**
43
	 * @var IConfig
44
	 */
45
	private $config;
46
47
	/**
48
	 * @var IUserSession
49
	 */
50
	private $userSession;
51
52
	/**
53 8
	 * @var IMailer
54
	 */
55 8
	private $mailer;
56 8
57 8
	/**
58 8
	 * @var L10N
59
	 */
60
	private $l10n;
61
62
	/**
63
	 * @var Defaults
64
	 */
65
	private $defaults;
66 4
67 4
	/**
68
	 * @param string $appName
69 4
	 * @param IRequest $request an instance of the request
70 4
	 * @param IUserSession $userSession
71 2
	 * @param IConfig $config
72
	 * @param IMailer $mailer
73
	 * @param L10N $l10N
74 2
	 * @param Defaults $defaults
75 2
	 */
76 2
	public function __construct($appName, IRequest $request,
77
								IUserSession $userSession, IConfig $config, IMailer $mailer, L10N $l10N, Defaults $defaults) {
78 2
		parent::__construct($appName, $request);
79 2
		$this->config = $config;
80
		$this->userSession = $userSession;
81 2
		$this->mailer = $mailer;
82
		$this->l10n = $l10N;
83 2
		$this->defaults = $defaults;
84 2
	}
85 2
86 2
	/**
87
	 * @NoAdminRequired
88 2
	 * @NoCSRFRequired
89
	 *
90
	 * @return TemplateResponse
91
	 */
92
	public function index() {
93
		$runningOn = $this->config->getSystemValue('version');
94
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
95
96
		$supportsClass = $runningOnServer91OrLater;
97 4
		$assetPipelineBroken = !$runningOnServer91OrLater;
98 4
99 2
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
100
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
101
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
102 2
		}
103
104 2
		$user = $this->userSession->getUser();
105 2
		$userId = $user->getUID();
106 2
		$emailAddress = $user->getEMailAddress();
107
108
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
109
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
110
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
111
		
112
		return new TemplateResponse('calendar', 'main', [
113
			'appVersion' => $appVersion,
114
			'defaultView' => $defaultView,
115
			'emailAddress' => $emailAddress,
116
			'skipPopover' => $skipPopover,
117 2
			'supportsClass' => $supportsClass,
118 2
			'isPublic' => false,
119
		]);
120
	}
121
122
	/**
123
	 * @PublicPage
124
	 * @NoCSRFRequired
125
	 *
126
	 * @return TemplateResponse
127
	 */
128
	public function publicIndex() {
129
		$runningOn = $this->config->getSystemValue('version');
130
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
131
132
		$supportsClass = $runningOnServer91OrLater;
133
		$assetPipelineBroken = !$runningOnServer91OrLater;
134
135
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
136
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
137
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
138
		}
139
140 4
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
141 4
142
		$response = new TemplateResponse('calendar', 'main', [
143 4
			'appVersion' => $appVersion,
144 4
			'defaultView' => 'month',
145 4
			'emailAddress' => '',
146
			'supportsClass' => $supportsClass,
147
			'isPublic' => true,
148
		], 'public');
149
		$response->addHeader('X-Frame-Options', 'ALLOW');
150
		$csp = new ContentSecurityPolicy();
151
		$csp->addAllowedScriptDomain('*');
152
		$response->setContentSecurityPolicy($csp);
153
154
		return $response;
155
	}
156
157
	/**
158
	 * @NoAdminRequired
159
	 *
160
	 * @param string $id
161
	 * @return NotFoundResponse|DataDisplayResponse
162
	 */
163
	public function getTimezone($id) {
164
		if (!in_array($id, $this->getTimezoneList())) {
165
			return new NotFoundResponse();
166
		}
167
168
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
169
170
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
171
			'content-type' => 'text/calendar',
172
		]);
173
	}
174
175
176
	/**
177
	 * @NoAdminRequired
178
	 * @NoCSRFRequired
179
	 * @PublicPage
180
	 *
181
	 * @param $region
182
	 * @param $city
183
	 * @return DataDisplayResponse
184
	 */
185
	public function getTimezoneWithRegion($region, $city) {
186
		return $this->getTimezone($region . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($region . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 186 which is incompatible with the return type documented by OCA\Calendar\Controller\...::getTimezoneWithRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
187
	}
188
189
190
	/**
191
	 * @NoAdminRequired
192
	 * @PublicPage
193
	 * @NoCSRFRequired
194
	 *
195
	 * @param $region
196
	 * @param $subregion
197
	 * @param $city
198
	 * @return DataDisplayResponse
199
	 */
200
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
201
		return $this->getTimezone($region . '-' . $subregion . '-' . $city);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getTimezone($regi...bregion . '-' . $city); of type OCP\AppFramework\Http\No...ttp\DataDisplayResponse adds the type OCP\AppFramework\Http\NotFoundResponse to the return on line 201 which is incompatible with the return type documented by OCA\Calendar\Controller\...etTimezoneWithSubRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
202
	}
203
204
205
	/**
206
	 * get a list of default timezones
207
	 *
208
	 * @return array
209
	 */
210
	private function getTimezoneList() {
211
		$allFiles = scandir(__DIR__ . '/../timezones/');
212
213
		return array_values(array_filter($allFiles, function($file) {
214
			return (substr($file, -4) === '.ics');
215
		}));
216
	}
217
218
	/**
219
	 * @param string $to
220
	 * @param string $url
221
	 * @param string $name
222
	 * @return JSONResponse
223
	 * @NoAdminRequired
224
	 */
225
	public function sendEmailPublicLink($to, $url, $name) {
226
227
		$user = $this->userSession->getUser();
228
		$username = $user->getDisplayName();
229
230
		$subject = $this->l10n->t('%s has shared the calendar "%s" with you', [$username, $name]);
231
232
		$emailTemplate = new TemplateResponse('calendar', 'mail.publication', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url, 'defaults' => $this->defaults], 'public');
233
		$body = $emailTemplate->render();
234
235
		return $this->sendEmail($to, $subject, $body, true);
236
	}
237
238
	/**
239
	 * @param string $target
240
	 * @param string $subject
241
	 * @param string $body
242
	 * @param boolean $useHTML
243
	 * @return JSONResponse
244
	 *
245
	 * TODO : This should be moved to a Tools class
246
	 *
247
	 */
248
	private function sendEmail($target, $subject, $body, $useHTML = false) {
249
		if (!$this->mailer->validateMailAddress($target)) {
250
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
251
		}
252
253
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
254
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'owncloud');
255
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
256
257
		$message = $this->mailer->createMessage();
258
		$message->setSubject($subject);
259
		$message->setFrom([$sendFrom => 'ownCloud Notifier']);
260
		$message->setTo([$target => 'Recipient']);
261
		if ($useHTML) {
262
			$message->setHtmlBody($body);
263
		} else {
264
			$message->setPlainBody($body);
265
		}
266
		$this->mailer->send($message);
267
268
		return new JSONResponse([], Http::STATUS_OK);
269
	}
270
}
271