|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | +namespace OCA\DAV\CalDAV\WebcalCaching; |
| 10 | + |
| 11 | +use Exception; |
| 12 | +use GuzzleHttp\HandlerStack; |
| 13 | +use GuzzleHttp\Middleware; |
| 14 | +use OCP\Http\Client\IClientService; |
| 15 | +use OCP\Http\Client\LocalServerException; |
| 16 | +use OCP\IAppConfig; |
| 17 | +use Psr\Http\Message\RequestInterface; |
| 18 | +use Psr\Http\Message\ResponseInterface; |
| 19 | +use Psr\Log\LoggerInterface; |
| 20 | +use Sabre\DAV\Xml\Property\Href; |
| 21 | +use Sabre\VObject\Reader; |
| 22 | + |
| 23 | +class Connection { |
| 24 | + public function __construct(private IClientService $clientService, |
| 25 | + private IAppConfig $config, |
| 26 | + private LoggerInterface $logger) { |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * gets webcal feed from remote server |
| 31 | + */ |
| 32 | + public function queryWebcalFeed(array $subscription, array &$mutations): ?string { |
| 33 | + $client = $this->clientService->newClient(); |
| 34 | + |
| 35 | + $didBreak301Chain = false; |
| 36 | + $latestLocation = null; |
| 37 | + |
| 38 | + $handlerStack = HandlerStack::create(); |
| 39 | + $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { |
| 40 | + return $request |
| 41 | + ->withHeader('Accept', 'text/calendar, application/calendar+json, application/calendar+xml') |
| 42 | + ->withHeader('User-Agent', 'Nextcloud Webcal Service'); |
| 43 | + })); |
| 44 | + $handlerStack->push(Middleware::mapResponse(function (ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) { |
| 45 | + if (!$didBreak301Chain) { |
| 46 | + if ($response->getStatusCode() !== 301) { |
| 47 | + $didBreak301Chain = true; |
| 48 | + } else { |
| 49 | + $latestLocation = $response->getHeader('Location'); |
| 50 | + } |
| 51 | + } |
| 52 | + return $response; |
| 53 | + })); |
| 54 | + |
| 55 | + $allowLocalAccess = $this->config->getValueString('dav', 'webcalAllowLocalAccess', 'no'); |
| 56 | + $subscriptionId = $subscription['id']; |
| 57 | + $url = $this->cleanURL($subscription['source']); |
| 58 | + if ($url === null) { |
| 59 | + return null; |
| 60 | + } |
| 61 | + |
| 62 | + try { |
| 63 | + $params = [ |
| 64 | + 'allow_redirects' => [ |
| 65 | + 'redirects' => 10 |
| 66 | + ], |
| 67 | + 'handler' => $handlerStack, |
| 68 | + 'nextcloud' => [ |
| 69 | + 'allow_local_address' => $allowLocalAccess === 'yes', |
| 70 | + ] |
| 71 | + ]; |
| 72 | + |
| 73 | + $user = parse_url($subscription['source'], PHP_URL_USER); |
| 74 | + $pass = parse_url($subscription['source'], PHP_URL_PASS); |
| 75 | + if ($user !== null && $pass !== null) { |
| 76 | + $params['auth'] = [$user, $pass]; |
| 77 | + } |
| 78 | + |
| 79 | + $response = $client->get($url, $params); |
| 80 | + $body = $response->getBody(); |
| 81 | + |
| 82 | + if ($latestLocation !== null) { |
| 83 | + $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); |
| 84 | + } |
| 85 | + |
| 86 | + $contentType = $response->getHeader('Content-Type'); |
| 87 | + $contentType = explode(';', $contentType, 2)[0]; |
| 88 | + switch ($contentType) { |
| 89 | + case 'application/calendar+json': |
| 90 | + try { |
| 91 | + $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING); |
| 92 | + } catch (Exception $ex) { |
| 93 | + // In case of a parsing error return null |
| 94 | + $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); |
| 95 | + return null; |
| 96 | + } |
| 97 | + return $jCalendar->serialize(); |
| 98 | + |
| 99 | + case 'application/calendar+xml': |
| 100 | + try { |
| 101 | + $xCalendar = Reader::readXML($body); |
| 102 | + } catch (Exception $ex) { |
| 103 | + // In case of a parsing error return null |
| 104 | + $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); |
| 105 | + return null; |
| 106 | + } |
| 107 | + return $xCalendar->serialize(); |
| 108 | + |
| 109 | + case 'text/calendar': |
| 110 | + default: |
| 111 | + try { |
| 112 | + $vCalendar = Reader::read($body); |
| 113 | + } catch (Exception $ex) { |
| 114 | + // In case of a parsing error return null |
| 115 | + $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]); |
| 116 | + return null; |
| 117 | + } |
| 118 | + return $vCalendar->serialize(); |
| 119 | + } |
| 120 | + } catch (LocalServerException $ex) { |
| 121 | + $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules", [ |
| 122 | + 'exception' => $ex, |
| 123 | + ]); |
| 124 | + |
| 125 | + return null; |
| 126 | + } catch (Exception $ex) { |
| 127 | + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error", [ |
| 128 | + 'exception' => $ex, |
| 129 | + ]); |
| 130 | + |
| 131 | + return null; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * This method will strip authentication information and replace the |
| 137 | + * 'webcal' or 'webcals' protocol scheme |
| 138 | + * |
| 139 | + * @param string $url |
| 140 | + * @return string|null |
| 141 | + */ |
| 142 | + private function cleanURL(string $url): ?string { |
| 143 | + $parsed = parse_url($url); |
| 144 | + if ($parsed === false) { |
| 145 | + return null; |
| 146 | + } |
| 147 | + |
| 148 | + if (isset($parsed['scheme']) && $parsed['scheme'] === 'http') { |
| 149 | + $scheme = 'http'; |
| 150 | + } else { |
| 151 | + $scheme = 'https'; |
| 152 | + } |
| 153 | + |
| 154 | + $host = $parsed['host'] ?? ''; |
| 155 | + $port = isset($parsed['port']) ? ':' . $parsed['port'] : ''; |
| 156 | + $path = $parsed['path'] ?? ''; |
| 157 | + $query = isset($parsed['query']) ? '?' . $parsed['query'] : ''; |
| 158 | + $fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : ''; |
| 159 | + |
| 160 | + $cleanURL = "$scheme://$host$port$path$query$fragment"; |
| 161 | + // parse_url is giving some weird results if no url and no :// is given, |
| 162 | + // so let's test the url again |
| 163 | + $parsedClean = parse_url($cleanURL); |
| 164 | + if ($parsedClean === false || !isset($parsedClean['host'])) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + |
| 168 | + return $cleanURL; |
| 169 | + } |
| 170 | +} |
0 commit comments