Connection.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\DAV\CalDAV\WebcalCaching;
  8. use Exception;
  9. use GuzzleHttp\RequestOptions;
  10. use OCP\Http\Client\IClientService;
  11. use OCP\Http\Client\LocalServerException;
  12. use OCP\IAppConfig;
  13. use Psr\Log\LoggerInterface;
  14. use Sabre\VObject\Reader;
  15. class Connection {
  16. public function __construct(private IClientService $clientService,
  17. private IAppConfig $config,
  18. private LoggerInterface $logger) {
  19. }
  20. /**
  21. * gets webcal feed from remote server
  22. */
  23. public function queryWebcalFeed(array $subscription): ?string {
  24. $subscriptionId = $subscription['id'];
  25. $url = $this->cleanURL($subscription['source']);
  26. if ($url === null) {
  27. return null;
  28. }
  29. $allowLocalAccess = $this->config->getValueString('dav', 'webcalAllowLocalAccess', 'no');
  30. $params = [
  31. 'nextcloud' => [
  32. 'allow_local_address' => $allowLocalAccess === 'yes',
  33. ],
  34. RequestOptions::HEADERS => [
  35. 'User-Agent' => 'Nextcloud Webcal Service',
  36. 'Accept' => 'text/calendar, application/calendar+json, application/calendar+xml',
  37. ],
  38. ];
  39. $user = parse_url($subscription['source'], PHP_URL_USER);
  40. $pass = parse_url($subscription['source'], PHP_URL_PASS);
  41. if ($user !== null && $pass !== null) {
  42. $params[RequestOptions::AUTH] = [$user, $pass];
  43. }
  44. try {
  45. $client = $this->clientService->newClient();
  46. $response = $client->get($url, $params);
  47. } catch (LocalServerException $ex) {
  48. $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules", [
  49. 'exception' => $ex,
  50. ]);
  51. return null;
  52. } catch (Exception $ex) {
  53. $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error", [
  54. 'exception' => $ex,
  55. ]);
  56. return null;
  57. }
  58. $body = $response->getBody();
  59. $contentType = $response->getHeader('Content-Type');
  60. $contentType = explode(';', $contentType, 2)[0];
  61. switch ($contentType) {
  62. case 'application/calendar+json':
  63. try {
  64. $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING);
  65. } catch (Exception $ex) {
  66. // In case of a parsing error return null
  67. $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
  68. return null;
  69. }
  70. return $jCalendar->serialize();
  71. case 'application/calendar+xml':
  72. try {
  73. $xCalendar = Reader::readXML($body);
  74. } catch (Exception $ex) {
  75. // In case of a parsing error return null
  76. $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
  77. return null;
  78. }
  79. return $xCalendar->serialize();
  80. case 'text/calendar':
  81. default:
  82. try {
  83. $vCalendar = Reader::read($body);
  84. } catch (Exception $ex) {
  85. // In case of a parsing error return null
  86. $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
  87. return null;
  88. }
  89. return $vCalendar->serialize();
  90. }
  91. }
  92. /**
  93. * This method will strip authentication information and replace the
  94. * 'webcal' or 'webcals' protocol scheme
  95. *
  96. * @param string $url
  97. * @return string|null
  98. */
  99. private function cleanURL(string $url): ?string {
  100. $parsed = parse_url($url);
  101. if ($parsed === false) {
  102. return null;
  103. }
  104. if (isset($parsed['scheme']) && $parsed['scheme'] === 'http') {
  105. $scheme = 'http';
  106. } else {
  107. $scheme = 'https';
  108. }
  109. $host = $parsed['host'] ?? '';
  110. $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
  111. $path = $parsed['path'] ?? '';
  112. $query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
  113. $fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';
  114. $cleanURL = "$scheme://$host$port$path$query$fragment";
  115. // parse_url is giving some weird results if no url and no :// is given,
  116. // so let's test the url again
  117. $parsedClean = parse_url($cleanURL);
  118. if ($parsedClean === false || !isset($parsedClean['host'])) {
  119. return null;
  120. }
  121. return $cleanURL;
  122. }
  123. }