Completed
Push — public-sharing ( 43e3cc...03ea7d )
by Thomas
22:34
created

ViewController::sendEmailPublicLink()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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