Completed
Pull Request — master (#605)
by Thomas
17:48
created

ViewController::getTimezoneWithSubRegion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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
	 */
75 2
	public function __construct($appName, IRequest $request,
76 2
								IUserSession $userSession, IConfig $config, IMailer $mailer, L10N $l10N, Defaults $defaults) {
77
		parent::__construct($appName, $request);
78 2
		$this->config = $config;
79 2
		$this->userSession = $userSession;
80
		$this->mailer = $mailer;
81 2
		$this->l10n = $l10N;
82
		$this->defaults = $defaults;
83 2
	}
84 2
85 2
	/**
86 2
	 * @NoAdminRequired
87
	 * @NoCSRFRequired
88 2
	 *
89
	 * @return TemplateResponse
90
	 */
91
	public function index() {
92
		$runningOn = $this->config->getSystemValue('version');
93
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
94
95
		$supportsClass = $runningOnServer91OrLater;
96
		$assetPipelineBroken = !$runningOnServer91OrLater;
97 4
98 4
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
99 2
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
100
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
101
		}
102 2
103
		$user = $this->userSession->getUser();
104 2
		$userId = $user->getUID();
105 2
		$emailAddress = $user->getEMailAddress();
106 2
107
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
108
		$defaultView = $this->config->getUserValue($userId, $this->appName, 'currentView', 'month');
109
		$skipPopover = $this->config->getUserValue($userId, $this->appName, 'skipPopover', 'no');
110
		
111
		return new TemplateResponse('calendar', 'main', [
112
			'appVersion' => $appVersion,
113
			'defaultView' => $defaultView,
114
			'emailAddress' => $emailAddress,
115
			'skipPopover' => $skipPopover,
116
			'supportsClass' => $supportsClass,
117 2
			'isPublic' => false,
118 2
		]);
119
	}
120
121
	/**
122
	 * @PublicPage
123
	 * @NoCSRFRequired
124
	 *
125
	 * @return TemplateResponse
126
	 */
127
	public function publicIndex() {
128
		$runningOn = $this->config->getSystemValue('version');
129
		$runningOnServer91OrLater = version_compare($runningOn, '9.1', '>=');
130
131
		$supportsClass = $runningOnServer91OrLater;
132
		$assetPipelineBroken = !$runningOnServer91OrLater;
133
134
		$isAssetPipelineEnabled = $this->config->getSystemValue('asset-pipeline.enabled', false);
135
		if ($isAssetPipelineEnabled && $assetPipelineBroken) {
136
			return new TemplateResponse('calendar', 'main-asset-pipeline-unsupported');
137
		}
138
139
		$appVersion = $this->config->getAppValue($this->appName, 'installed_version');
140 4
141 4
		$response = new TemplateResponse('calendar', 'main', [
142
			'appVersion' => $appVersion,
143 4
			'defaultView' => 'month',
144 4
			'emailAddress' => '',
145 4
			'supportsClass' => $supportsClass,
146
			'isPublic' => true,
147
		], 'public');
148
		$response->addHeader('X-Frame-Options', 'ALLOW');
149
		$csp = new ContentSecurityPolicy();
150
		$csp->addAllowedScriptDomain('*');
151
		$response->setContentSecurityPolicy($csp);
152
153
		return $response;
154
	}
155
156
	/**
157
	 * @NoAdminRequired
158
	 *
159
	 * @param string $id
160
	 * @return NotFoundResponse|DataDisplayResponse
161
	 */
162
	public function getTimezone($id) {
163
		if (!in_array($id, $this->getTimezoneList())) {
164
			return new NotFoundResponse();
165
		}
166
167
		$tzData = file_get_contents(__DIR__ . '/../timezones/' . $id);
168
169
		return new DataDisplayResponse($tzData, HTTP::STATUS_OK, [
170
			'content-type' => 'text/calendar',
171
		]);
172
	}
173
174
175
	/**
176
	 * @NoAdminRequired
177
	 * @NoCSRFRequired
178
	 * @PublicPage
179
	 *
180
	 * @param $region
181
	 * @param $city
182
	 * @return DataDisplayResponse
183
	 */
184
	public function getTimezoneWithRegion($region, $city) {
185
		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 185 which is incompatible with the return type documented by OCA\Calendar\Controller\...::getTimezoneWithRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
186
	}
187
188
189
	/**
190
	 * @NoAdminRequired
191
	 * @PublicPage
192
	 * @NoCSRFRequired
193
	 *
194
	 * @param $region
195
	 * @param $subregion
196
	 * @param $city
197
	 * @return DataDisplayResponse
198
	 */
199
	public function getTimezoneWithSubRegion($region, $subregion, $city) {
200
		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 200 which is incompatible with the return type documented by OCA\Calendar\Controller\...etTimezoneWithSubRegion of type OCP\AppFramework\Http\DataDisplayResponse.
Loading history...
201
	}
202
203
204
	/**
205
	 * get a list of default timezones
206
	 *
207
	 * @return array
208
	 */
209
	private function getTimezoneList() {
210
		$allFiles = scandir(__DIR__ . '/../timezones/');
211
212
		return array_values(array_filter($allFiles, function($file) {
213
			return (substr($file, -4) === '.ics');
214
		}));
215
	}
216
217
	/**
218
	 * @param string $to
219
	 * @param string $url
220
	 * @param string $name
221
	 * @return JSONResponse
222
	 * @NoAdminRequired
223
	 */
224
	public function sendEmailPublicLink($to, $url, $name) {
225
226
		$user = $this->userSession->getUser();
227
		$username = $user->getDisplayName();
228
229
		$subject = $this->l10n->t('%s has shared the calendar "%s" with you', [$username, $name]);
230
231
		$emailTemplate = new TemplateResponse('calendar', 'mail.publication', ['subject' => $subject, 'username' => $username, 'calendarname' => $name, 'calendarurl' => $url, 'defaults' => $this->defaults], 'public');
232
		$body = $emailTemplate->render();
233
234
		return $this->sendEmail($to, $subject, $body, true);
235
	}
236
237
	/**
238
	 * @param string $target
239
	 * @param string $subject
240
	 * @param string $body
241
	 * @param boolean $useHTML
242
	 * @return JSONResponse
243
	 *
244
	 * TODO : This should be moved to a Tools class
245
	 *
246
	 */
247
	private function sendEmail($target, $subject, $body, $useHTML = false) {
248
		if (!$this->mailer->validateMailAddress($target)) {
249
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
250
		}
251
252
		$sendFromDomain = $this->config->getSystemValue('mail_domain', 'domain.org');
253
		$sendFromAddress = $this->config->getSystemValue('mail_from_address', 'owncloud');
254
		$sendFrom = $sendFromAddress . '@' . $sendFromDomain;
255
256
		$message = $this->mailer->createMessage();
257
		$message->setSubject($subject);
258
		$message->setFrom([$sendFrom => 'ownCloud Notifier']);
259
		$message->setTo([$target => 'Recipient']);
260
		if ($useHTML) {
261
			$message->setHtmlBody($body);
262
		} else {
263
			$message->setPlainBody($body);
264
		}
265
		$this->mailer->send($message);
266
267
		return new JSONResponse([], Http::STATUS_OK);
268
	}
269
}
270