-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(webcal): only update modified and deleted events from webcal calendars #46723
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+718
−426
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\DAV\CalDAV\WebcalCaching; | ||
|
|
||
| use Exception; | ||
| use GuzzleHttp\HandlerStack; | ||
| use GuzzleHttp\Middleware; | ||
| use OCP\Http\Client\IClientService; | ||
| use OCP\Http\Client\LocalServerException; | ||
| use OCP\IAppConfig; | ||
| use Psr\Http\Message\RequestInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Log\LoggerInterface; | ||
| use Sabre\DAV\Xml\Property\Href; | ||
| use Sabre\VObject\Reader; | ||
|
|
||
| class Connection { | ||
| public function __construct(private IClientService $clientService, | ||
| private IAppConfig $config, | ||
| private LoggerInterface $logger) { | ||
| } | ||
|
|
||
| /** | ||
| * gets webcal feed from remote server | ||
| */ | ||
| public function queryWebcalFeed(array $subscription, array &$mutations): ?string { | ||
| $client = $this->clientService->newClient(); | ||
|
|
||
| $didBreak301Chain = false; | ||
| $latestLocation = null; | ||
|
|
||
| $handlerStack = HandlerStack::create(); | ||
| $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { | ||
| return $request | ||
| ->withHeader('Accept', 'text/calendar, application/calendar+json, application/calendar+xml') | ||
| ->withHeader('User-Agent', 'Nextcloud Webcal Service'); | ||
| })); | ||
| $handlerStack->push(Middleware::mapResponse(function (ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) { | ||
| if (!$didBreak301Chain) { | ||
| if ($response->getStatusCode() !== 301) { | ||
| $didBreak301Chain = true; | ||
| } else { | ||
| $latestLocation = $response->getHeader('Location'); | ||
| } | ||
| } | ||
| return $response; | ||
| })); | ||
|
|
||
| $allowLocalAccess = $this->config->getValueString('dav', 'webcalAllowLocalAccess', 'no'); | ||
| $subscriptionId = $subscription['id']; | ||
| $url = $this->cleanURL($subscription['source']); | ||
| if ($url === null) { | ||
| return null; | ||
| } | ||
|
|
||
| try { | ||
| $params = [ | ||
| 'allow_redirects' => [ | ||
| 'redirects' => 10 | ||
| ], | ||
| 'handler' => $handlerStack, | ||
| 'nextcloud' => [ | ||
| 'allow_local_address' => $allowLocalAccess === 'yes', | ||
| ] | ||
| ]; | ||
|
|
||
| $user = parse_url($subscription['source'], PHP_URL_USER); | ||
| $pass = parse_url($subscription['source'], PHP_URL_PASS); | ||
| if ($user !== null && $pass !== null) { | ||
| $params['auth'] = [$user, $pass]; | ||
| } | ||
|
|
||
| $response = $client->get($url, $params); | ||
| $body = $response->getBody(); | ||
|
|
||
| if ($latestLocation !== null) { | ||
| $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); | ||
| } | ||
|
|
||
| $contentType = $response->getHeader('Content-Type'); | ||
| $contentType = explode(';', $contentType, 2)[0]; | ||
| switch ($contentType) { | ||
| case 'application/calendar+json': | ||
| try { | ||
| $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING); | ||
| } catch (Exception $ex) { | ||
| // In case of a parsing error return null | ||
| $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); | ||
| return null; | ||
| } | ||
| return $jCalendar->serialize(); | ||
|
|
||
| case 'application/calendar+xml': | ||
| try { | ||
| $xCalendar = Reader::readXML($body); | ||
| } catch (Exception $ex) { | ||
| // In case of a parsing error return null | ||
| $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); | ||
| return null; | ||
| } | ||
| return $xCalendar->serialize(); | ||
|
|
||
| case 'text/calendar': | ||
| default: | ||
| try { | ||
| $vCalendar = Reader::read($body); | ||
| } catch (Exception $ex) { | ||
| // In case of a parsing error return null | ||
| $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); | ||
| return null; | ||
| } | ||
| return $vCalendar->serialize(); | ||
| } | ||
| } catch (LocalServerException $ex) { | ||
| $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules", [ | ||
| 'exception' => $ex, | ||
| ]); | ||
|
|
||
| return null; | ||
| } catch (Exception $ex) { | ||
| $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error", [ | ||
| 'exception' => $ex, | ||
| ]); | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * This method will strip authentication information and replace the | ||
| * 'webcal' or 'webcals' protocol scheme | ||
| * | ||
| * @param string $url | ||
| * @return string|null | ||
| */ | ||
| private function cleanURL(string $url): ?string { | ||
| $parsed = parse_url($url); | ||
| if ($parsed === false) { | ||
| return null; | ||
| } | ||
|
|
||
| if (isset($parsed['scheme']) && $parsed['scheme'] === 'http') { | ||
| $scheme = 'http'; | ||
| } else { | ||
| $scheme = 'https'; | ||
| } | ||
|
|
||
| $host = $parsed['host'] ?? ''; | ||
| $port = isset($parsed['port']) ? ':' . $parsed['port'] : ''; | ||
| $path = $parsed['path'] ?? ''; | ||
| $query = isset($parsed['query']) ? '?' . $parsed['query'] : ''; | ||
| $fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : ''; | ||
|
|
||
| $cleanURL = "$scheme://$host$port$path$query$fragment"; | ||
| // parse_url is giving some weird results if no url and no :// is given, | ||
| // so let's test the url again | ||
| $parsedClean = parse_url($cleanURL); | ||
| if ($parsedClean === false || !isset($parsedClean['host'])) { | ||
| return null; | ||
| } | ||
|
|
||
| return $cleanURL; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / Psalm
MissingReturnType