Passed
Push — master ( b7bfe2...dd9d46 )
by Christoph
19:39 queued 11s
created

RefreshWebcalService::refreshSubscription()   C

Complexity

Conditions 13
Paths 30

Size

Total Lines 69
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 41
c 1
b 0
f 0
nc 30
nop 2
dl 0
loc 69
rs 6.6166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2020, Thomas Citharel <[email protected]>
7
 *
8
 * @author Georg Ehrke <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Citharel <[email protected]>
11
 *
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
namespace OCA\DAV\CalDAV\WebcalCaching;
30
31
use Exception;
32
use GuzzleHttp\HandlerStack;
33
use GuzzleHttp\Middleware;
34
use OCA\DAV\CalDAV\CalDavBackend;
35
use OCP\AppFramework\Utility\ITimeFactory;
36
use OCP\Http\Client\IClientService;
37
use OCP\IConfig;
38
use OCP\ILogger;
39
use Psr\Http\Message\RequestInterface;
40
use Psr\Http\Message\ResponseInterface;
41
use Sabre\DAV\Exception\BadRequest;
42
use Sabre\DAV\PropPatch;
43
use Sabre\DAV\Xml\Property\Href;
44
use Sabre\VObject\Component;
45
use Sabre\VObject\DateTimeParser;
46
use Sabre\VObject\InvalidDataException;
47
use Sabre\VObject\ParseException;
48
use Sabre\VObject\Reader;
49
use Sabre\VObject\Splitter\ICalendar;
50
use function count;
51
52
class RefreshWebcalService {
53
54
	/** @var CalDavBackend */
55
	private $calDavBackend;
56
57
	/** @var IClientService */
58
	private $clientService;
59
60
	/** @var IConfig */
61
	private $config;
62
63
	/** @var ILogger */
64
	private $logger;
65
66
	public const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate';
67
	public const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms';
68
	public const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments';
69
	public const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos';
70
71
	/**
72
	 * RefreshWebcalJob constructor.
73
	 *
74
	 * @param CalDavBackend $calDavBackend
75
	 * @param IClientService $clientService
76
	 * @param IConfig $config
77
	 * @param ILogger $logger
78
	 */
79
	public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, ILogger $logger) {
80
		$this->calDavBackend = $calDavBackend;
81
		$this->clientService = $clientService;
82
		$this->config = $config;
83
		$this->logger = $logger;
84
	}
85
86
	/**
87
	 * @param string $principalUri
88
	 * @param string $uri
89
	 */
90
	public function refreshSubscription(string $principalUri, string $uri) {
91
		$subscription = $this->getSubscription($principalUri, $uri);
92
		$mutations = [];
93
		if (!$subscription) {
94
			return;
95
		}
96
97
		$webcalData = $this->queryWebcalFeed($subscription, $mutations);
98
		if (!$webcalData) {
99
			return;
100
		}
101
102
		$stripTodos = ($subscription[self::STRIP_TODOS] ?? 1) === 1;
103
		$stripAlarms = ($subscription[self::STRIP_ALARMS] ?? 1) === 1;
104
		$stripAttachments = ($subscription[self::STRIP_ATTACHMENTS] ?? 1) === 1;
105
106
		try {
107
			$splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING);
0 ignored issues
show
Bug introduced by
$webcalData of type string is incompatible with the type resource expected by parameter $input of Sabre\VObject\Splitter\ICalendar::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
			$splitter = new ICalendar(/** @scrutinizer ignore-type */ $webcalData, Reader::OPTION_FORGIVING);
Loading history...
108
109
			// we wait with deleting all outdated events till we parsed the new ones
110
			// in case the new calendar is broken and `new ICalendar` throws a ParseException
111
			// the user will still see the old data
112
			$this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']);
113
114
			while ($vObject = $splitter->getNext()) {
115
				/** @var Component $vObject */
116
				$uid = null;
117
				$compName = null;
118
119
				foreach ($vObject->getComponents() as $component) {
120
					if ($component->name === 'VTIMEZONE') {
121
						continue;
122
					}
123
124
					$uid = $component->{'UID'}->getValue();
125
					$compName = $component->name;
126
127
					if ($stripAlarms) {
128
						unset($component->{'VALARM'});
129
					}
130
					if ($stripAttachments) {
131
						unset($component->{'ATTACH'});
132
					}
133
				}
134
135
				if ($stripTodos && $compName === 'VTODO') {
136
					continue;
137
				}
138
139
				$uri = $uid . '.ics';
140
				$calendarData = $vObject->serialize();
141
				try {
142
					$this->calDavBackend->createCalendarObject($subscription['id'], $uri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
143
				} catch(BadRequest $ex) {
144
					$this->logger->logException($ex);
145
				}
146
			}
147
148
			$newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData);
149
			if ($newRefreshRate) {
150
				$mutations[self::REFRESH_RATE] = $newRefreshRate;
151
			}
152
153
			$this->updateSubscription($subscription, $mutations);
154
		} catch(ParseException $ex) {
155
			$subscriptionId = $subscription['id'];
156
157
			$this->logger->logException($ex);
158
			$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error");
159
		}
160
	}
161
162
	/**
163
	 * loads subscription from backend
164
	 *
165
	 * @param string $principalUri
166
	 * @param string $uri
167
	 * @return array|null
168
	 */
169
	public function getSubscription(string $principalUri, string $uri) {
170
		$subscriptions = array_values(array_filter(
171
			$this->calDavBackend->getSubscriptionsForUser($principalUri),
172
			function($sub) use ($uri) {
173
				return $sub['uri'] === $uri;
174
			}
175
		));
176
177
		if (count($subscriptions) === 0) {
178
			return null;
179
		}
180
181
		return $subscriptions[0];
182
	}
183
184
	/**
185
	 * gets webcal feed from remote server
186
	 *
187
	 * @param array $subscription
188
	 * @param array &$mutations
189
	 * @return null|string
190
	 */
191
	private function queryWebcalFeed(array $subscription, array &$mutations) {
192
		$client = $this->clientService->newClient();
193
194
		$didBreak301Chain = false;
195
		$latestLocation = null;
196
197
		$handlerStack = HandlerStack::create();
198
		$handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) {
199
			return $request
200
				->withHeader('Accept', 'text/calendar, application/calendar+json, application/calendar+xml')
201
				->withHeader('User-Agent', 'Nextcloud Webcal Crawler');
202
		}));
203
		$handlerStack->push(Middleware::mapResponse(function(ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) {
204
			if (!$didBreak301Chain) {
205
				if ($response->getStatusCode() !== 301) {
206
					$didBreak301Chain = true;
207
				} else {
208
					$latestLocation = $response->getHeader('Location');
209
				}
210
			}
211
			return $response;
212
		}));
213
214
		$allowLocalAccess = $this->config->getAppValue('dav', 'webcalAllowLocalAccess', 'no');
215
		$subscriptionId = $subscription['id'];
216
		$url = $this->cleanURL($subscription['source']);
217
		if ($url === null) {
218
			return null;
219
		}
220
221
		if ($allowLocalAccess !== 'yes') {
222
			$host = strtolower(parse_url($url, PHP_URL_HOST));
223
			// remove brackets from IPv6 addresses
224
			if (strpos($host, '[') === 0 && substr($host, -1) === ']') {
225
				$host = substr($host, 1, -1);
226
			}
227
228
			// Disallow localhost and local network
229
			if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost') {
230
				$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
231
				return null;
232
			}
233
234
			// Disallow hostname only
235
			if (substr_count($host, '.') === 0) {
236
				$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
237
				return null;
238
			}
239
240
			if ((bool)filter_var($host, FILTER_VALIDATE_IP) && !filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
241
				$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
242
				return null;
243
			}
244
245
			// Also check for IPv6 IPv4 nesting, because that's not covered by filter_var
246
			if ((bool)filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) && substr_count($host, '.') > 0) {
247
				$delimiter = strrpos($host, ':'); // Get last colon
248
				$ipv4Address = substr($host, $delimiter + 1);
249
250
				if (!filter_var($ipv4Address, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
251
					$this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules");
252
					return null;
253
				}
254
			}
255
		}
256
257
		try {
258
			$params = [
259
				'allow_redirects' => [
260
					'redirects' => 10
261
				],
262
				'handler' => $handlerStack,
263
			];
264
265
			$user = parse_url($subscription['source'], PHP_URL_USER);
266
			$pass = parse_url($subscription['source'], PHP_URL_PASS);
267
			if ($user !== null && $pass !== null) {
268
				$params['auth'] = [$user, $pass];
269
			}
270
271
			$response = $client->get($url, $params);
272
			$body = $response->getBody();
273
274
			if ($latestLocation) {
275
				$mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation);
276
			}
277
278
			$contentType = $response->getHeader('Content-Type');
279
			$contentType = explode(';', $contentType, 2)[0];
280
			switch($contentType) {
281
				case 'application/calendar+json':
282
					try {
283
						$jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING);
284
					} catch(Exception $ex) {
285
						// In case of a parsing error return null
286
						$this->logger->debug("Subscription $subscriptionId could not be parsed");
287
						return null;
288
					}
289
					return $jCalendar->serialize();
290
291
				case 'application/calendar+xml':
292
					try {
293
						$xCalendar = Reader::readXML($body);
294
					} catch(Exception $ex) {
295
						// In case of a parsing error return null
296
						$this->logger->debug("Subscription $subscriptionId could not be parsed");
297
						return null;
298
					}
299
					return $xCalendar->serialize();
300
301
				case 'text/calendar':
302
				default:
303
					try {
304
						$vCalendar = Reader::read($body);
305
					} catch(Exception $ex) {
306
						// In case of a parsing error return null
307
						$this->logger->debug("Subscription $subscriptionId could not be parsed");
308
						return null;
309
					}
310
					return $vCalendar->serialize();
311
			}
312
		} catch(Exception $ex) {
313
			$this->logger->logException($ex);
314
			$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error");
315
316
			return null;
317
		}
318
	}
319
320
	/**
321
	 * check if:
322
	 *  - current subscription stores a refreshrate
323
	 *  - the webcal feed suggests a refreshrate
324
	 *  - return suggested refreshrate if user didn't set a custom one
325
	 *
326
	 * @param array $subscription
327
	 * @param string $webcalData
328
	 * @return string|null
329
	 */
330
	private function checkWebcalDataForRefreshRate($subscription, $webcalData) {
331
		// if there is no refreshrate stored in the database, check the webcal feed
332
		// whether it suggests any refresh rate and store that in the database
333
		if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
334
			return null;
335
		}
336
337
		/** @var Component\VCalendar $vCalendar */
338
		$vCalendar = Reader::read($webcalData);
339
340
		$newRefreshRate = null;
341
		if (isset($vCalendar->{'X-PUBLISHED-TTL'})) {
342
			$newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue();
343
		}
344
		if (isset($vCalendar->{'REFRESH-INTERVAL'})) {
345
			$newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue();
346
		}
347
348
		if (!$newRefreshRate) {
349
			return null;
350
		}
351
352
		// check if new refresh rate is even valid
353
		try {
354
			DateTimeParser::parseDuration($newRefreshRate);
355
		} catch(InvalidDataException $ex) {
356
			return null;
357
		}
358
359
		return $newRefreshRate;
360
	}
361
362
	/**
363
	 * update subscription stored in database
364
	 * used to set:
365
	 *  - refreshrate
366
	 *  - source
367
	 *
368
	 * @param array $subscription
369
	 * @param array $mutations
370
	 */
371
	private function updateSubscription(array $subscription, array $mutations) {
372
		if (empty($mutations)) {
373
			return;
374
		}
375
376
		$propPatch = new PropPatch($mutations);
377
		$this->calDavBackend->updateSubscription($subscription['id'], $propPatch);
378
		$propPatch->commit();
379
	}
380
381
	/**
382
	 * This method will strip authentication information and replace the
383
	 * 'webcal' or 'webcals' protocol scheme
384
	 *
385
	 * @param string $url
386
	 * @return string|null
387
	 */
388
	private function cleanURL(string $url) {
389
		$parsed = parse_url($url);
390
		if ($parsed === false) {
391
			return null;
392
		}
393
394
		if (isset($parsed['scheme']) && $parsed['scheme'] === 'http') {
395
			$scheme = 'http';
396
		} else {
397
			$scheme = 'https';
398
		}
399
400
		$host = $parsed['host'] ?? '';
401
		$port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
402
		$path = $parsed['path'] ?? '';
403
		$query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
404
		$fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';
405
406
		$cleanURL = "$scheme://$host$port$path$query$fragment";
407
		// parse_url is giving some weird results if no url and no :// is given,
408
		// so let's test the url again
409
		$parsedClean = parse_url($cleanURL);
410
		if ($parsedClean === false || !isset($parsedClean['host'])) {
411
			return null;
412
		}
413
414
		return $cleanURL;
415
	}
416
}
417