RefreshWebcalService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Thomas Citharel <nextcloud@tcit.fr>
  5. * @copyright Copyright (c) 2020, leith abdulla (<online-nextcloud@eleith.com>)
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author eleith <online+github@eleith.com>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Thomas Citharel <nextcloud@tcit.fr>
  12. *
  13. * @license GNU AGPL version 3 or any later version
  14. *
  15. * This program is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License as
  17. * published by the Free Software Foundation, either version 3 of the
  18. * License, or (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. *
  28. */
  29. namespace OCA\DAV\CalDAV\WebcalCaching;
  30. use Exception;
  31. use GuzzleHttp\RequestOptions;
  32. use OCA\DAV\CalDAV\CalDavBackend;
  33. use OCP\Http\Client\IClientService;
  34. use OCP\Http\Client\LocalServerException;
  35. use OCP\IAppConfig;
  36. use Psr\Log\LoggerInterface;
  37. use Sabre\DAV\Exception\BadRequest;
  38. use Sabre\DAV\PropPatch;
  39. use Sabre\VObject\Component;
  40. use Sabre\VObject\DateTimeParser;
  41. use Sabre\VObject\InvalidDataException;
  42. use Sabre\VObject\ParseException;
  43. use Sabre\VObject\Reader;
  44. use Sabre\VObject\Recur\NoInstancesException;
  45. use Sabre\VObject\Splitter\ICalendar;
  46. use Sabre\VObject\UUIDUtil;
  47. use function count;
  48. class RefreshWebcalService {
  49. private CalDavBackend $calDavBackend;
  50. private IClientService $clientService;
  51. private IAppConfig $config;
  52. /** @var LoggerInterface */
  53. private LoggerInterface $logger;
  54. public const REFRESH_RATE = '{http://apple.com/ns/ical/}refreshrate';
  55. public const STRIP_ALARMS = '{http://calendarserver.org/ns/}subscribed-strip-alarms';
  56. public const STRIP_ATTACHMENTS = '{http://calendarserver.org/ns/}subscribed-strip-attachments';
  57. public const STRIP_TODOS = '{http://calendarserver.org/ns/}subscribed-strip-todos';
  58. public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IAppConfig $config, LoggerInterface $logger) {
  59. $this->calDavBackend = $calDavBackend;
  60. $this->clientService = $clientService;
  61. $this->config = $config;
  62. $this->logger = $logger;
  63. }
  64. public function refreshSubscription(string $principalUri, string $uri): void {
  65. $subscription = $this->getSubscription($principalUri, $uri);
  66. $mutations = [];
  67. if (!$subscription) {
  68. return;
  69. }
  70. $webcalData = $this->queryWebcalFeed($subscription, $mutations);
  71. if ($webcalData === null) {
  72. return;
  73. }
  74. $stripTodos = ($subscription[self::STRIP_TODOS] ?? 1) === 1;
  75. $stripAlarms = ($subscription[self::STRIP_ALARMS] ?? 1) === 1;
  76. $stripAttachments = ($subscription[self::STRIP_ATTACHMENTS] ?? 1) === 1;
  77. try {
  78. $splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING);
  79. // we wait with deleting all outdated events till we parsed the new ones
  80. // in case the new calendar is broken and `new ICalendar` throws a ParseException
  81. // the user will still see the old data
  82. $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']);
  83. while ($vObject = $splitter->getNext()) {
  84. /** @var Component $vObject */
  85. $compName = null;
  86. foreach ($vObject->getComponents() as $component) {
  87. if ($component->name === 'VTIMEZONE') {
  88. continue;
  89. }
  90. $compName = $component->name;
  91. if ($stripAlarms) {
  92. unset($component->{'VALARM'});
  93. }
  94. if ($stripAttachments) {
  95. unset($component->{'ATTACH'});
  96. }
  97. }
  98. if ($stripTodos && $compName === 'VTODO') {
  99. continue;
  100. }
  101. $objectUri = $this->getRandomCalendarObjectUri();
  102. $calendarData = $vObject->serialize();
  103. try {
  104. $this->calDavBackend->createCalendarObject($subscription['id'], $objectUri, $calendarData, CalDavBackend::CALENDAR_TYPE_SUBSCRIPTION);
  105. } catch (NoInstancesException|BadRequest $ex) {
  106. $this->logger->error('Unable to create calendar object from subscription {subscriptionId}', ['exception' => $ex, 'subscriptionId' => $subscription['id'], 'source' => $subscription['source']]);
  107. }
  108. }
  109. $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData);
  110. if ($newRefreshRate) {
  111. $mutations[self::REFRESH_RATE] = $newRefreshRate;
  112. }
  113. $this->updateSubscription($subscription, $mutations);
  114. } catch (ParseException $ex) {
  115. $this->logger->error('Subscription {subscriptionId} could not be refreshed due to a parsing error', ['exception' => $ex, 'subscriptionId' => $subscription['id']]);
  116. }
  117. }
  118. /**
  119. * loads subscription from backend
  120. */
  121. public function getSubscription(string $principalUri, string $uri): ?array {
  122. $subscriptions = array_values(array_filter(
  123. $this->calDavBackend->getSubscriptionsForUser($principalUri),
  124. function ($sub) use ($uri) {
  125. return $sub['uri'] === $uri;
  126. }
  127. ));
  128. if (count($subscriptions) === 0) {
  129. return null;
  130. }
  131. return $subscriptions[0];
  132. }
  133. /**
  134. * gets webcal feed from remote server
  135. */
  136. private function queryWebcalFeed(array $subscription, array &$mutations): ?string {
  137. $subscriptionId = $subscription['id'];
  138. $url = $this->cleanURL($subscription['source']);
  139. if ($url === null) {
  140. return null;
  141. }
  142. $allowLocalAccess = $this->config->getValueString('dav', 'webcalAllowLocalAccess', 'no');
  143. $params = [
  144. 'nextcloud' => [
  145. 'allow_local_address' => $allowLocalAccess === 'yes',
  146. ],
  147. RequestOptions::HEADERS => [
  148. 'User-Agent' => 'Nextcloud Webcal Service',
  149. 'Accept' => 'text/calendar, application/calendar+json, application/calendar+xml',
  150. ],
  151. ];
  152. $user = parse_url($subscription['source'], PHP_URL_USER);
  153. $pass = parse_url($subscription['source'], PHP_URL_PASS);
  154. if ($user !== null && $pass !== null) {
  155. $params[RequestOptions::AUTH] = [$user, $pass];
  156. }
  157. try {
  158. $client = $this->clientService->newClient();
  159. $response = $client->get($url, $params);
  160. } catch (LocalServerException $ex) {
  161. $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules", [
  162. 'exception' => $ex,
  163. ]);
  164. return null;
  165. } catch (Exception $ex) {
  166. $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error", [
  167. 'exception' => $ex,
  168. ]);
  169. return null;
  170. }
  171. $body = $response->getBody();
  172. $contentType = $response->getHeader('Content-Type');
  173. $contentType = explode(';', $contentType, 2)[0];
  174. switch ($contentType) {
  175. case 'application/calendar+json':
  176. try {
  177. $jCalendar = Reader::readJson($body, Reader::OPTION_FORGIVING);
  178. } catch (Exception $ex) {
  179. // In case of a parsing error return null
  180. $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
  181. return null;
  182. }
  183. return $jCalendar->serialize();
  184. case 'application/calendar+xml':
  185. try {
  186. $xCalendar = Reader::readXML($body);
  187. } catch (Exception $ex) {
  188. // In case of a parsing error return null
  189. $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
  190. return null;
  191. }
  192. return $xCalendar->serialize();
  193. case 'text/calendar':
  194. default:
  195. try {
  196. $vCalendar = Reader::read($body);
  197. } catch (Exception $ex) {
  198. // In case of a parsing error return null
  199. $this->logger->warning("Subscription $subscriptionId could not be parsed", ['exception' => $ex]);
  200. return null;
  201. }
  202. return $vCalendar->serialize();
  203. }
  204. }
  205. /**
  206. * check if:
  207. * - current subscription stores a refreshrate
  208. * - the webcal feed suggests a refreshrate
  209. * - return suggested refreshrate if user didn't set a custom one
  210. *
  211. */
  212. private function checkWebcalDataForRefreshRate(array $subscription, string $webcalData): ?string {
  213. // if there is no refreshrate stored in the database, check the webcal feed
  214. // whether it suggests any refresh rate and store that in the database
  215. if (isset($subscription[self::REFRESH_RATE]) && $subscription[self::REFRESH_RATE] !== null) {
  216. return null;
  217. }
  218. /** @var Component\VCalendar $vCalendar */
  219. $vCalendar = Reader::read($webcalData);
  220. $newRefreshRate = null;
  221. if (isset($vCalendar->{'X-PUBLISHED-TTL'})) {
  222. $newRefreshRate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue();
  223. }
  224. if (isset($vCalendar->{'REFRESH-INTERVAL'})) {
  225. $newRefreshRate = $vCalendar->{'REFRESH-INTERVAL'}->getValue();
  226. }
  227. if (!$newRefreshRate) {
  228. return null;
  229. }
  230. // check if new refresh rate is even valid
  231. try {
  232. DateTimeParser::parseDuration($newRefreshRate);
  233. } catch (InvalidDataException $ex) {
  234. return null;
  235. }
  236. return $newRefreshRate;
  237. }
  238. /**
  239. * update subscription stored in database
  240. * used to set:
  241. * - refreshrate
  242. * - source
  243. *
  244. * @param array $subscription
  245. * @param array $mutations
  246. */
  247. private function updateSubscription(array $subscription, array $mutations) {
  248. if (empty($mutations)) {
  249. return;
  250. }
  251. $propPatch = new PropPatch($mutations);
  252. $this->calDavBackend->updateSubscription($subscription['id'], $propPatch);
  253. $propPatch->commit();
  254. }
  255. /**
  256. * This method will strip authentication information and replace the
  257. * 'webcal' or 'webcals' protocol scheme
  258. *
  259. * @param string $url
  260. * @return string|null
  261. */
  262. private function cleanURL(string $url): ?string {
  263. $parsed = parse_url($url);
  264. if ($parsed === false) {
  265. return null;
  266. }
  267. if (isset($parsed['scheme']) && $parsed['scheme'] === 'http') {
  268. $scheme = 'http';
  269. } else {
  270. $scheme = 'https';
  271. }
  272. $host = $parsed['host'] ?? '';
  273. $port = isset($parsed['port']) ? ':' . $parsed['port'] : '';
  274. $path = $parsed['path'] ?? '';
  275. $query = isset($parsed['query']) ? '?' . $parsed['query'] : '';
  276. $fragment = isset($parsed['fragment']) ? '#' . $parsed['fragment'] : '';
  277. $cleanURL = "$scheme://$host$port$path$query$fragment";
  278. // parse_url is giving some weird results if no url and no :// is given,
  279. // so let's test the url again
  280. $parsedClean = parse_url($cleanURL);
  281. if ($parsedClean === false || !isset($parsedClean['host'])) {
  282. return null;
  283. }
  284. return $cleanURL;
  285. }
  286. /**
  287. * Returns a random uri for a calendar-object
  288. *
  289. * @return string
  290. */
  291. public function getRandomCalendarObjectUri():string {
  292. return UUIDUtil::getUUID() . '.ics';
  293. }
  294. }