Connection.php 4.0 KB

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