diff --git a/apps/dav/appinfo/app.php b/apps/dav/appinfo/app.php index 089aaeb6c7841..36b3a39ab17c5 100644 --- a/apps/dav/appinfo/app.php +++ b/apps/dav/appinfo/app.php @@ -48,6 +48,34 @@ function(GenericEvent $event) use ($app) { } ); +$eventDispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', + function(GenericEvent $event) use ($app) { + $jobList = $app->getContainer()->getServer()->getJobList(); + $subscriptionData = $event->getArgument('subscriptionData'); + + $jobList->add(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [ + 'principaluri' => $subscriptionData['principaluri'], + 'uri' => $subscriptionData['uri'] + ]); + } +); + +$eventDispatcher->addListener('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', + function(GenericEvent $event) use ($app) { + $jobList = $app->getContainer()->getServer()->getJobList(); + $subscriptionData = $event->getArgument('subscriptionData'); + + $jobList->remove(\OCA\DAV\BackgroundJob\RefreshWebcalJob::class, [ + 'principaluri' => $subscriptionData['principaluri'], + 'uri' => $subscriptionData['uri'] + ]); + + /** @var \OCA\DAV\CalDAV\CalDavBackend $calDavBackend */ + $calDavBackend = $app->getContainer()->query(\OCA\DAV\CalDAV\CalDavBackend::class); + $calDavBackend->purgeAllCachedEventsForSubscription($subscriptionData['id']); + } +); + $eventHandler = function() use ($app) { try { $job = $app->getContainer()->query(\OCA\DAV\BackgroundJob\UpdateCalendarResourcesRoomsBackgroundJob::class); diff --git a/apps/dav/appinfo/info.xml b/apps/dav/appinfo/info.xml index d296661e8ac06..47b6a77c109eb 100644 --- a/apps/dav/appinfo/info.xml +++ b/apps/dav/appinfo/info.xml @@ -5,7 +5,7 @@ WebDAV WebDAV endpoint WebDAV endpoint - 1.7.1 + 1.7.2 agpl owncloud.org DAV @@ -30,6 +30,7 @@ OCA\DAV\Migration\FixBirthdayCalendarComponent OCA\DAV\Migration\CalDAVRemoveEmptyValue OCA\DAV\Migration\BuildCalendarSearchIndex + OCA\DAV\Migration\RefreshWebcalJobRegistrar diff --git a/apps/dav/composer/composer/autoload_classmap.php b/apps/dav/composer/composer/autoload_classmap.php index fe7557f7e085e..5a9ca32c05067 100644 --- a/apps/dav/composer/composer/autoload_classmap.php +++ b/apps/dav/composer/composer/autoload_classmap.php @@ -14,6 +14,7 @@ 'OCA\\DAV\\BackgroundJob\\CleanupDirectLinksJob' => $baseDir . '/../lib/BackgroundJob/CleanupDirectLinksJob.php', 'OCA\\DAV\\BackgroundJob\\CleanupInvitationTokenJob' => $baseDir . '/../lib/BackgroundJob/CleanupInvitationTokenJob.php', 'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => $baseDir . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php', + 'OCA\\DAV\\BackgroundJob\\RefreshWebcalJob' => $baseDir . '/../lib/BackgroundJob/RefreshWebcalJob.php', 'OCA\\DAV\\BackgroundJob\\UpdateCalendarResourcesRoomsBackgroundJob' => $baseDir . '/../lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php', 'OCA\\DAV\\CalDAV\\Activity\\Backend' => $baseDir . '/../lib/CalDAV/Activity/Backend.php', 'OCA\\DAV\\CalDAV\\Activity\\Filter\\Calendar' => $baseDir . '/../lib/CalDAV/Activity/Filter/Calendar.php', @@ -27,6 +28,8 @@ 'OCA\\DAV\\CalDAV\\Activity\\Setting\\Todo' => $baseDir . '/../lib/CalDAV/Activity/Setting/Todo.php', 'OCA\\DAV\\CalDAV\\BirthdayCalendar\\EnablePlugin' => $baseDir . '/../lib/CalDAV/BirthdayCalendar/EnablePlugin.php', 'OCA\\DAV\\CalDAV\\BirthdayService' => $baseDir . '/../lib/CalDAV/BirthdayService.php', + 'OCA\\DAV\\CalDAV\\CachedSubscription' => $baseDir . '/../lib/CalDAV/CachedSubscription.php', + 'OCA\\DAV\\CalDAV\\CachedSubscriptionObject' => $baseDir . '/../lib/CalDAV/CachedSubscriptionObject.php', 'OCA\\DAV\\CalDAV\\CalDavBackend' => $baseDir . '/../lib/CalDAV/CalDavBackend.php', 'OCA\\DAV\\CalDAV\\Calendar' => $baseDir . '/../lib/CalDAV/Calendar.php', 'OCA\\DAV\\CalDAV\\CalendarHome' => $baseDir . '/../lib/CalDAV/CalendarHome.php', @@ -57,6 +60,7 @@ 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\PropFilter' => $baseDir . '/../lib/CalDAV/Search/Xml/Filter/PropFilter.php', 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\SearchTermFilter' => $baseDir . '/../lib/CalDAV/Search/Xml/Filter/SearchTermFilter.php', 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport' => $baseDir . '/../lib/CalDAV/Search/Xml/Request/CalendarSearchReport.php', + 'OCA\\DAV\\CalDAV\\WebcalCaching\\Plugin' => $baseDir . '/../lib/CalDAV/WebcalCaching/Plugin.php', 'OCA\\DAV\\Capabilities' => $baseDir . '/../lib/Capabilities.php', 'OCA\\DAV\\CardDAV\\AddressBook' => $baseDir . '/../lib/CardDAV/AddressBook.php', 'OCA\\DAV\\CardDAV\\AddressBookImpl' => $baseDir . '/../lib/CardDAV/AddressBookImpl.php', @@ -148,6 +152,7 @@ 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\CalDAVRemoveEmptyValue' => $baseDir . '/../lib/Migration/CalDAVRemoveEmptyValue.php', 'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => $baseDir . '/../lib/Migration/FixBirthdayCalendarComponent.php', + 'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => $baseDir . '/../lib/Migration/RefreshWebcalJobRegistrar.php', 'OCA\\DAV\\Migration\\Version1004Date20170825134824' => $baseDir . '/../lib/Migration/Version1004Date20170825134824.php', 'OCA\\DAV\\Migration\\Version1004Date20170919104507' => $baseDir . '/../lib/Migration/Version1004Date20170919104507.php', 'OCA\\DAV\\Migration\\Version1004Date20170924124212' => $baseDir . '/../lib/Migration/Version1004Date20170924124212.php', @@ -155,6 +160,7 @@ 'OCA\\DAV\\Migration\\Version1005Date20180413093149' => $baseDir . '/../lib/Migration/Version1005Date20180413093149.php', 'OCA\\DAV\\Migration\\Version1005Date20180530124431' => $baseDir . '/../lib/Migration/Version1005Date20180530124431.php', 'OCA\\DAV\\Migration\\Version1006Date20180619154313' => $baseDir . '/../lib/Migration/Version1006Date20180619154313.php', + 'OCA\\DAV\\Migration\\Version1006Date20180628111625' => $baseDir . '/../lib/Migration/Version1006Date20180628111625.php', 'OCA\\DAV\\Migration\\Version1007Date20181007225117' => $baseDir . '/../lib/Migration/Version1007Date20181007225117.php', 'OCA\\DAV\\RootCollection' => $baseDir . '/../lib/RootCollection.php', 'OCA\\DAV\\Server' => $baseDir . '/../lib/Server.php', diff --git a/apps/dav/composer/composer/autoload_static.php b/apps/dav/composer/composer/autoload_static.php index 1668f1270f55a..8231eddc5d185 100644 --- a/apps/dav/composer/composer/autoload_static.php +++ b/apps/dav/composer/composer/autoload_static.php @@ -29,6 +29,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\BackgroundJob\\CleanupDirectLinksJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupDirectLinksJob.php', 'OCA\\DAV\\BackgroundJob\\CleanupInvitationTokenJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupInvitationTokenJob.php', 'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php', + 'OCA\\DAV\\BackgroundJob\\RefreshWebcalJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/RefreshWebcalJob.php', 'OCA\\DAV\\BackgroundJob\\UpdateCalendarResourcesRoomsBackgroundJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php', 'OCA\\DAV\\CalDAV\\Activity\\Backend' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Backend.php', 'OCA\\DAV\\CalDAV\\Activity\\Filter\\Calendar' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Filter/Calendar.php', @@ -42,6 +43,8 @@ class ComposerStaticInitDAV 'OCA\\DAV\\CalDAV\\Activity\\Setting\\Todo' => __DIR__ . '/..' . '/../lib/CalDAV/Activity/Setting/Todo.php', 'OCA\\DAV\\CalDAV\\BirthdayCalendar\\EnablePlugin' => __DIR__ . '/..' . '/../lib/CalDAV/BirthdayCalendar/EnablePlugin.php', 'OCA\\DAV\\CalDAV\\BirthdayService' => __DIR__ . '/..' . '/../lib/CalDAV/BirthdayService.php', + 'OCA\\DAV\\CalDAV\\CachedSubscription' => __DIR__ . '/..' . '/../lib/CalDAV/CachedSubscription.php', + 'OCA\\DAV\\CalDAV\\CachedSubscriptionObject' => __DIR__ . '/..' . '/../lib/CalDAV/CachedSubscriptionObject.php', 'OCA\\DAV\\CalDAV\\CalDavBackend' => __DIR__ . '/..' . '/../lib/CalDAV/CalDavBackend.php', 'OCA\\DAV\\CalDAV\\Calendar' => __DIR__ . '/..' . '/../lib/CalDAV/Calendar.php', 'OCA\\DAV\\CalDAV\\CalendarHome' => __DIR__ . '/..' . '/../lib/CalDAV/CalendarHome.php', @@ -72,6 +75,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\PropFilter' => __DIR__ . '/..' . '/../lib/CalDAV/Search/Xml/Filter/PropFilter.php', 'OCA\\DAV\\CalDAV\\Search\\Xml\\Filter\\SearchTermFilter' => __DIR__ . '/..' . '/../lib/CalDAV/Search/Xml/Filter/SearchTermFilter.php', 'OCA\\DAV\\CalDAV\\Search\\Xml\\Request\\CalendarSearchReport' => __DIR__ . '/..' . '/../lib/CalDAV/Search/Xml/Request/CalendarSearchReport.php', + 'OCA\\DAV\\CalDAV\\WebcalCaching\\Plugin' => __DIR__ . '/..' . '/../lib/CalDAV/WebcalCaching/Plugin.php', 'OCA\\DAV\\Capabilities' => __DIR__ . '/..' . '/../lib/Capabilities.php', 'OCA\\DAV\\CardDAV\\AddressBook' => __DIR__ . '/..' . '/../lib/CardDAV/AddressBook.php', 'OCA\\DAV\\CardDAV\\AddressBookImpl' => __DIR__ . '/..' . '/../lib/CardDAV/AddressBookImpl.php', @@ -163,6 +167,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Migration\\BuildCalendarSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildCalendarSearchIndexBackgroundJob.php', 'OCA\\DAV\\Migration\\CalDAVRemoveEmptyValue' => __DIR__ . '/..' . '/../lib/Migration/CalDAVRemoveEmptyValue.php', 'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => __DIR__ . '/..' . '/../lib/Migration/FixBirthdayCalendarComponent.php', + 'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => __DIR__ . '/..' . '/../lib/Migration/RefreshWebcalJobRegistrar.php', 'OCA\\DAV\\Migration\\Version1004Date20170825134824' => __DIR__ . '/..' . '/../lib/Migration/Version1004Date20170825134824.php', 'OCA\\DAV\\Migration\\Version1004Date20170919104507' => __DIR__ . '/..' . '/../lib/Migration/Version1004Date20170919104507.php', 'OCA\\DAV\\Migration\\Version1004Date20170924124212' => __DIR__ . '/..' . '/../lib/Migration/Version1004Date20170924124212.php', @@ -170,6 +175,7 @@ class ComposerStaticInitDAV 'OCA\\DAV\\Migration\\Version1005Date20180413093149' => __DIR__ . '/..' . '/../lib/Migration/Version1005Date20180413093149.php', 'OCA\\DAV\\Migration\\Version1005Date20180530124431' => __DIR__ . '/..' . '/../lib/Migration/Version1005Date20180530124431.php', 'OCA\\DAV\\Migration\\Version1006Date20180619154313' => __DIR__ . '/..' . '/../lib/Migration/Version1006Date20180619154313.php', + 'OCA\\DAV\\Migration\\Version1006Date20180628111625' => __DIR__ . '/..' . '/../lib/Migration/Version1006Date20180628111625.php', 'OCA\\DAV\\Migration\\Version1007Date20181007225117' => __DIR__ . '/..' . '/../lib/Migration/Version1007Date20181007225117.php', 'OCA\\DAV\\RootCollection' => __DIR__ . '/..' . '/../lib/RootCollection.php', 'OCA\\DAV\\Server' => __DIR__ . '/..' . '/../lib/Server.php', diff --git a/apps/dav/lib/BackgroundJob/RefreshWebcalJob.php b/apps/dav/lib/BackgroundJob/RefreshWebcalJob.php new file mode 100644 index 0000000000000..798a072f516c8 --- /dev/null +++ b/apps/dav/lib/BackgroundJob/RefreshWebcalJob.php @@ -0,0 +1,437 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\DAV\BackgroundJob; + +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; +use OC\BackgroundJob\Job; +use OCA\DAV\CalDAV\CalDavBackend; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\Http\Client\IClientService; +use OCP\IConfig; +use OCP\ILogger; +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Sabre\DAV\Exception\BadRequest; +use Sabre\DAV\PropPatch; +use Sabre\DAV\Xml\Property\Href; +use Sabre\VObject\Component; +use Sabre\VObject\DateTimeParser; +use Sabre\VObject\InvalidDataException; +use Sabre\VObject\ParseException; +use Sabre\VObject\Reader; +use Sabre\VObject\Splitter\ICalendar; + +class RefreshWebcalJob extends Job { + + /** @var CalDavBackend */ + private $calDavBackend; + + /** @var IClientService */ + private $clientService; + + /** @var IConfig */ + private $config; + + /** @var ILogger */ + private $logger; + + /** @var ITimeFactory */ + private $timeFactory; + + /** @var array */ + private $subscription; + + /** + * RefreshWebcalJob constructor. + * + * @param CalDavBackend $calDavBackend + * @param IClientService $clientService + * @param IConfig $config + * @param ILogger $logger + * @param ITimeFactory $timeFactory + */ + public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) { + $this->calDavBackend = $calDavBackend; + $this->clientService = $clientService; + $this->config = $config; + $this->logger = $logger; + $this->timeFactory = $timeFactory; + } + + /** + * this function is called at most every hour + * + * @inheritdoc + */ + public function execute($jobList, ILogger $logger = null) { + $subscription = $this->getSubscription($this->argument['principaluri'], $this->argument['uri']); + if (!$subscription) { + return; + } + + // if no refresh rate was configured, just refresh once a week + $subscriptionId = $subscription['id']; + $refreshrate = $subscription['refreshrate'] ?? 'P1W'; + + try { + /** @var \DateInterval $dateInterval */ + $dateInterval = DateTimeParser::parseDuration($refreshrate); + } catch(InvalidDataException $ex) { + $this->logger->logException($ex); + $this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid"); + return; + } + + $interval = $this->getIntervalFromDateInterval($dateInterval); + if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) { + return; + } + + parent::execute($jobList, $logger); + } + + /** + * @param array $argument + */ + protected function run($argument) { + $subscription = $this->getSubscription($argument['principaluri'], $argument['uri']); + $mutations = []; + if (!$subscription) { + return; + } + + $webcalData = $this->queryWebcalFeed($subscription, $mutations); + if (!$webcalData) { + return; + } + + $stripTodos = $subscription['striptodos'] ?? 1; + $stripAlarms = $subscription['stripalarms'] ?? 1; + $stripAttachments = $subscription['stripattachments'] ?? 1; + + try { + $splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING); + + // we wait with deleting all outdated events till we parsed the new ones + // in case the new calendar is broken and `new ICalendar` throws a ParseException + // the user will still see the old data + $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']); + + while ($vObject = $splitter->getNext()) { + /** @var Component $vObject */ + $uid = null; + $compName = null; + + foreach ($vObject->getComponents() as $component) { + if ($component->name === 'VTIMEZONE') { + continue; + } + + $uid = $component->{'UID'}->getValue(); + $compName = $component->name; + + if ($stripAlarms) { + unset($component->{'VALARM'}); + } + if ($stripAttachments) { + unset($component->{'ATTACH'}); + } + } + + if ($stripTodos && $compName === 'VTODO') { + continue; + } + + $uri = $uid . '.ics'; + $calendarData = $vObject->serialize(); + try { + $this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData); + } catch(BadRequest $ex) { + $this->logger->logException($ex); + } + } + + $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData); + if ($newRefreshRate) { + $mutations['{http://apple.com/ns/ical/}refreshrate'] = $newRefreshRate; + } + + $this->updateSubscription($subscription, $mutations); + } catch(ParseException $ex) { + $subscriptionId = $subscription['id']; + + $this->logger->logException($ex); + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error"); + } + } + + /** + * gets webcal feed from remote server + * + * @param array $subscription + * @param array &$mutations + * @return null|string + */ + private function queryWebcalFeed(array $subscription, array &$mutations) { + $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 Crawler'); + })); + $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->getAppValue('dav', 'webcalAllowLocalAccess', 'no'); + $subscriptionId = $subscription['id']; + $url = $this->cleanURL($subscription['source']); + if ($url === null) { + return null; + } + + if ($allowLocalAccess !== 'yes') { + $host = parse_url($url, PHP_URL_HOST); + // remove brackets from IPv6 addresses + if (strpos($host, '[') === 0 && substr($host, -1) === ']') { + $host = substr($host, 1, -1); + } + + if ($host === 'localhost' || substr($host, -6) === '.local' || substr($host, -10) === '.localhost' || + preg_match('/(^127\.)|(^192\.168\.)|(^10\.)|(^172\.1[6-9]\.)|(^172\.2[0-9]\.)|(^172\.3[0-1]\.)|(^::1$)|(^[fF][cCdD])/', $host)) { + $this->logger->warning("Subscription $subscriptionId was not refreshed because it violates local access rules"); + return null; + } + } + + try { + $params = [ + 'allow_redirects' => [ + 'redirects' => 10 + ], + 'handler' => $handlerStack, + ]; + + $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) { + $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); + } + + $contentType = $response->getHeader('Content-Type'); + $contentType = explode(';', $contentType, 2)[0]; + switch($contentType) { + case 'text/calendar': + return $body; + + 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->debug("Subscription $subscriptionId could not be parsed"); + 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->debug("Subscription $subscriptionId could not be parsed"); + return null; + } + return $xCalendar->serialize(); + + default: + return null; + } + } catch(\Exception $ex) { + $this->logger->logException($ex); + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error"); + + return null; + } + } + + /** + * loads subscription from backend and store it locally + * + * @param string $principalUri + * @param string $uri + * @return array|null + */ + private function getSubscription(string $principalUri, string $uri) { + if ($this->subscription) { + return $this->subscription; + } + + $subscriptions = array_values(array_filter( + $this->calDavBackend->getSubscriptionsForUser($principalUri), + function($sub) use ($uri) { + return $sub['uri'] === $uri; + } + )); + + if (\count($subscriptions) === 0) { + return null; + } + + $this->subscription = $subscriptions[0]; + return $this->subscription; + } + + /** + * get total number of seconds from DateInterval object + * + * @param \DateInterval $interval + * @return int + */ + private function getIntervalFromDateInterval(\DateInterval $interval):int { + return $interval->s + + ($interval->i * 60) + + ($interval->h * 60 * 60) + + ($interval->d * 60 * 60 * 24) + + ($interval->m * 60 * 60 * 24 * 30) + + ($interval->y * 60 * 60 * 24 * 365); + } + + /** + * check if: + * - current subscription stores a refreshrate + * - the webcal feed suggests a refreshrate + * - return suggested refreshrate if user didn't set a custom one + * + * @param array $subscription + * @param string $webcalData + * @return string|null + */ + private function checkWebcalDataForRefreshRate($subscription, $webcalData) { + // if there is no refreshrate stored in the database, check the webcal feed + // whether it suggests any refresh rate and store that in the database + if (isset($subscription['refreshrate']) && $subscription['refreshrate'] !== null) { + return null; + } + + /** @var Component\VCalendar $vCalendar */ + $vCalendar = Reader::read($webcalData); + + $newRefreshrate = null; + if (isset($vCalendar->{'X-PUBLISHED-TTL'})) { + $newRefreshrate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue(); + } + if (isset($vCalendar->{'REFRESH-INTERVAL'})) { + $newRefreshrate = $vCalendar->{'REFRESH-INTERVAL'}->getValue(); + } + + if (!$newRefreshrate) { + return null; + } + + // check if new refresh rate is even valid + try { + DateTimeParser::parseDuration($newRefreshrate); + } catch(InvalidDataException $ex) { + return null; + } + + return $newRefreshrate; + } + + /** + * update subscription stored in database + * used to set: + * - refreshrate + * - source + * + * @param array $subscription + * @param array $mutations + */ + private function updateSubscription(array $subscription, array $mutations) { + if (empty($mutations)) { + return; + } + + $propPatch = new PropPatch($mutations); + $this->calDavBackend->updateSubscription($subscription['id'], $propPatch); + $propPatch->commit(); + } + + /** + * 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) { + $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; + } +} diff --git a/apps/dav/lib/CalDAV/CachedSubscription.php b/apps/dav/lib/CalDAV/CachedSubscription.php new file mode 100644 index 0000000000000..ca4a3fb4a5ce8 --- /dev/null +++ b/apps/dav/lib/CalDAV/CachedSubscription.php @@ -0,0 +1,175 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\DAV\CalDAV; + +use Sabre\CalDAV\Backend\BackendInterface; +use Sabre\DAV\Exception\MethodNotAllowed; +use Sabre\DAV\Exception\NotFound; +use Sabre\DAV\PropPatch; + +/** + * Class CachedSubscription + * + * @package OCA\DAV\CalDAV + * @property BackendInterface|CalDavBackend $caldavBackend + */ +class CachedSubscription extends \Sabre\CalDAV\Calendar { + + /** + * @return string + */ + public function getPrincipalURI():string { + return $this->calendarInfo['principaluri']; + } + + /** + * @return array + */ + public function getACL():array { + return [ + [ + 'privilege' => '{DAV:}read', + 'principal' => $this->getOwner(), + 'protected' => true, + ] + ]; + } + + /** + * @return array + */ + public function getChildACL():array { + return $this->getACL(); + } + + /** + * @return null|string + */ + public function getOwner() { + if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) { + return $this->calendarInfo['{http://owncloud.org/ns}owner-principal']; + } + return parent::getOwner(); + } + + /** + * + */ + public function delete() { + $this->caldavBackend->deleteSubscription($this->calendarInfo['id']); + } + + /** + * @param PropPatch $propPatch + */ + public function propPatch(PropPatch $propPatch) { + $this->caldavBackend->updateSubscription($this->calendarInfo['id'], $propPatch); + } + + /** + * @param string $name + * @return CalendarObject|\Sabre\CalDAV\ICalendarObject + * @throws NotFound + */ + public function getChild($name) { + $obj = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $name); + if (!$obj) { + throw new NotFound('Calendar object not found'); + } + + $obj['acl'] = $this->getChildACL(); + return new CalendarObject($this->caldavBackend, $this->calendarInfo, $obj); + + } + + /** + * @return array + */ + public function getChildren():array { + $objs = $this->caldavBackend->getCachedCalendarObjects($this->calendarInfo['id']); + + $children = []; + foreach($objs as $obj) { + $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj); + } + + return $children; + } + + /** + * @param array $paths + * @return array + */ + public function getMultipleChildren(array $paths):array { + $objs = $this->caldavBackend->getMultipleCachedCalendarObjects($this->calendarInfo['id'], $paths); + + $children = []; + foreach($objs as $obj) { + $children[] = new CachedSubscriptionObject($this->caldavBackend, $this->calendarInfo, $obj); + } + + return $children; + } + + /** + * @param string $name + * @param null $calendarData + * @return null|string|void + * @throws MethodNotAllowed + */ + public function createFile($name, $calendarData = null) { + throw new MethodNotAllowed('Creating objects in cached subscription is not allowed'); + } + + /** + * @param string $name + * @return bool + */ + public function childExists($name):bool { + $obj = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $name); + if (!$obj) { + return false; + } + + return true; + } + + /** + * @param array $filters + * @return array + */ + public function calendarQuery(array $filters):array { + return $this->caldavBackend->cachedCalendarQuery($this->calendarInfo['id'], $filters); + } + + /** + * CachedSubscriptions don't support sync-tokens for now + * Clients will have to do a multi get etag for now + * + * @return null + */ + public function getSyncToken() { + return null; + } +} diff --git a/apps/dav/lib/CalDAV/CachedSubscriptionObject.php b/apps/dav/lib/CalDAV/CachedSubscriptionObject.php new file mode 100644 index 0000000000000..cdae7c22aa671 --- /dev/null +++ b/apps/dav/lib/CalDAV/CachedSubscriptionObject.php @@ -0,0 +1,64 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\DAV\CalDAV; + +use Sabre\DAV\Exception\MethodNotAllowed; + +/** + * Class CachedSubscriptionObject + * + * @package OCA\DAV\CalDAV + * @property CalDavBackend $caldavBackend + */ +class CachedSubscriptionObject extends \Sabre\CalDAV\CalendarObject { + + /** + * @inheritdoc + */ + public function get() { + // Pre-populating the 'calendardata' is optional, if we don't have it + // already we fetch it from the backend. + if (!isset($this->objectData['calendardata'])) { + $this->objectData = $this->caldavBackend->getCachedCalendarObject($this->calendarInfo['id'], $this->objectData['uri']); + } + + return $this->objectData['calendardata']; + } + + /** + * @param resource|string $calendarData + * @return string|void + * @throws MethodNotAllowed + */ + public function put($calendarData) { + throw new MethodNotAllowed('Creating objects in cached subscription is not allowed'); + } + + /** + * @throws MethodNotAllowed + */ + public function delete() { + throw new MethodNotAllowed('Deleting objects in cached subscription is not allowed'); + } +} diff --git a/apps/dav/lib/CalDAV/CalDavBackend.php b/apps/dav/lib/CalDAV/CalDavBackend.php index df10b62fc5b64..695cba27cea92 100644 --- a/apps/dav/lib/CalDAV/CalDavBackend.php +++ b/apps/dav/lib/CalDAV/CalDavBackend.php @@ -1,7 +1,7 @@ * @author Joas Schilling @@ -372,6 +372,10 @@ function getCalendarsForUser($principalUri) { return array_values($calendars); } + /** + * @param $principalUri + * @return array + */ public function getUsersOwnCalendars($principalUri) { $principalUri = $this->convertPrincipal($principalUri, true); $fields = array_values($this->propertyMap); @@ -417,6 +421,10 @@ public function getUsersOwnCalendars($principalUri) { } + /** + * @param $uid + * @return string + */ private function getUserDisplayName($uid) { if (!isset($this->userDisplayNames[$uid])) { $user = $this->userManager->get($uid); @@ -601,6 +609,10 @@ public function getCalendarByUri($principal, $uri) { return $calendar; } + /** + * @param $calendarId + * @return array|null + */ public function getCalendarById($calendarId) { $fields = array_values($this->propertyMap); $fields[] = 'id'; @@ -647,6 +659,48 @@ public function getCalendarById($calendarId) { return $calendar; } + /** + * @param $subscriptionId + */ + public function getSubscriptionById($subscriptionId) { + $fields = array_values($this->subscriptionPropertyMap); + $fields[] = 'id'; + $fields[] = 'uri'; + $fields[] = 'source'; + $fields[] = 'principaluri'; + $fields[] = 'lastmodified'; + + $query = $this->db->getQueryBuilder(); + $query->select($fields) + ->from('calendarsubscriptions') + ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) + ->orderBy('calendarorder', 'asc'); + $stmt =$query->execute(); + + $row = $stmt->fetch(\PDO::FETCH_ASSOC); + $stmt->closeCursor(); + if ($row === false) { + return null; + } + + $subscription = [ + 'id' => $row['id'], + 'uri' => $row['uri'], + 'principaluri' => $row['principaluri'], + 'source' => $row['source'], + 'lastmodified' => $row['lastmodified'], + '{' . Plugin::NS_CALDAV . '}supported-calendar-component-set' => new SupportedCalendarComponentSet(['VTODO', 'VEVENT']), + ]; + + foreach($this->subscriptionPropertyMap as $xmlName=>$dbName) { + if (!is_null($row[$dbName])) { + $subscription[$xmlName] = $row[$dbName]; + } + } + + return $subscription; + } + /** * Creates a new calendar for a principal. * @@ -841,24 +895,42 @@ function deleteAllSharesByUser($principaluri) { * @param mixed $calendarId * @return array */ - function getCalendarObjects($calendarId) { + function getCalendarObjects($calendarId):array { + return $this->commonGetCalendarObjects('calendarobjects', 'calendarid', $calendarId); + } + + /** + * @param mixed $subscriptionId + * @return array + */ + public function getCachedCalendarObjects($subscriptionId):array { + return $this->commonGetCalendarObjects('calendarsubscrobjects', 'subscriptionid', $subscriptionId); + } + + /** + * @param string $tablename + * @param string $selector + * @param mixed $id + * @return array + */ + private function commonGetCalendarObjects(string $tablename, string $selector, $id):array { $query = $this->db->getQueryBuilder(); - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'componenttype', 'classification']) - ->from('calendarobjects') - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); + $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'componenttype', 'classification']) + ->from($tablename) + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))); $stmt = $query->execute(); $result = []; foreach($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) { $result[] = [ - 'id' => $row['id'], - 'uri' => $row['uri'], - 'lastmodified' => $row['lastmodified'], - 'etag' => '"' . $row['etag'] . '"', - 'calendarid' => $row['calendarid'], - 'size' => (int)$row['size'], - 'component' => strtolower($row['componenttype']), - 'classification'=> (int)$row['classification'] + 'id' => $row['id'], + 'uri' => $row['uri'], + 'lastmodified' => $row['lastmodified'], + 'etag' => '"' . $row['etag'] . '"', + 'calendarid' => $row[$selector], + 'size' => (int)$row['size'], + 'component' => strtolower($row['componenttype']), + 'classification'=> (int)$row['classification'] ]; } @@ -882,27 +954,48 @@ function getCalendarObjects($calendarId) { * @return array|null */ function getCalendarObject($calendarId, $objectUri) { + return $this->commonGetCalendarObject('calendarobjects', 'calendarid', $calendarId, $objectUri); + } + + /** + * @param $subscriptionId + * @param $objectUri + * @return array + */ + public function getCachedCalendarObject($subscriptionId, $objectUri) { + return $this->commonGetCalendarObject('calendarsubscrobjects', 'subscriptionid', $subscriptionId, $objectUri); + } + /** + * @param string $tablename + * @param string $selector + * @param $id + * @param $objectUri + * @return array + */ + private function commonGetCalendarObject(string $tablename, string $selector, $id, $objectUri):array { $query = $this->db->getQueryBuilder(); - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) - ->from('calendarobjects') - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) - ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); + $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'calendardata', 'componenttype', 'classification']) + ->from($tablename) + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))) + ->andWhere($query->expr()->eq('uri', $query->createNamedParameter($objectUri))); $stmt = $query->execute(); $row = $stmt->fetch(\PDO::FETCH_ASSOC); - if(!$row) return null; + if(!$row) { + return null; + } return [ - 'id' => $row['id'], - 'uri' => $row['uri'], - 'lastmodified' => $row['lastmodified'], - 'etag' => '"' . $row['etag'] . '"', - 'calendarid' => $row['calendarid'], - 'size' => (int)$row['size'], - 'calendardata' => $this->readBlob($row['calendardata']), - 'component' => strtolower($row['componenttype']), - 'classification'=> (int)$row['classification'] + 'id' => $row['id'], + 'uri' => $row['uri'], + 'lastmodified' => $row['lastmodified'], + 'etag' => '"' . $row['etag'] . '"', + 'calendarid' => $row[$selector], + 'size' => (int)$row['size'], + 'calendardata' => $this->readBlob($row['calendardata']), + 'component' => strtolower($row['componenttype']), + 'classification'=> (int)$row['classification'] ]; } @@ -919,6 +1012,26 @@ function getCalendarObject($calendarId, $objectUri) { * @return array */ function getMultipleCalendarObjects($calendarId, array $uris) { + return $this->commonGetMultipleCachedCalendarObjects('calendarobjects', 'calendarid', $calendarId, $uris); + } + + /** + * @param $subscripionId + * @param array $uris + * @return array + */ + function getMultipleCachedCalendarObjects($subscripionId, array $uris) { + return $this->commonGetMultipleCachedCalendarObjects('calendarsubscrobjects', 'subscriptionid', $subscripionId, $uris); + } + + /** + * @param string $tablename + * @param string $selector + * @param $id + * @param array $uris + * @return array + */ + private function commonGetMultipleCachedCalendarObjects(string $tablename, string $selector, $id, array $uris) { if (empty($uris)) { return []; } @@ -927,9 +1040,9 @@ function getMultipleCalendarObjects($calendarId, array $uris) { $objects = []; $query = $this->db->getQueryBuilder(); - $query->select(['id', 'uri', 'lastmodified', 'etag', 'calendarid', 'size', 'calendardata', 'componenttype', 'classification']) - ->from('calendarobjects') - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))) + $query->select(['id', 'uri', 'lastmodified', 'etag', $selector, 'size', 'calendardata', 'componenttype', 'classification']) + ->from($tablename) + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))) ->andWhere($query->expr()->in('uri', $query->createParameter('uri'))); foreach ($chunks as $uris) { @@ -942,7 +1055,7 @@ function getMultipleCalendarObjects($calendarId, array $uris) { 'uri' => $row['uri'], 'lastmodified' => $row['lastmodified'], 'etag' => '"' . $row['etag'] . '"', - 'calendarid' => $row['calendarid'], + 'calendarid' => $row[$selector], 'size' => (int)$row['size'], 'calendardata' => $this->readBlob($row['calendardata']), 'component' => strtolower($row['componenttype']), @@ -951,6 +1064,7 @@ function getMultipleCalendarObjects($calendarId, array $uris) { } $result->closeCursor(); } + return $objects; } @@ -1174,7 +1288,27 @@ function deleteCalendarObject($calendarId, $objectUri) { * @param array $filters * @return array */ - function calendarQuery($calendarId, array $filters) { + function calendarQuery($calendarId, array $filters):array { + return $this->commonCalendarQuery('calendarobjects', 'calendarid', $calendarId, $filters); + } + + /** + * @param $subscriptionId + * @param array $filters + * @return array + */ + public function cachedCalendarQuery($subscriptionId, array $filters):array { + return $this->commonCalendarQuery('calendarsubscrobjects', 'subscriptionid', $subscriptionId, $filters); + } + + /** + * @param string $tablename + * @param string $selector + * @param $id + * @param array $filters + * @return array + */ + private function commonCalendarQuery(string $tablename, string $selector, $id, array $filters) { $componentType = null; $requirePostFilter = true; $timeRange = null; @@ -1210,8 +1344,8 @@ function calendarQuery($calendarId, array $filters) { } $query = $this->db->getQueryBuilder(); $query->select($columns) - ->from('calendarobjects') - ->where($query->expr()->eq('calendarid', $query->createNamedParameter($calendarId))); + ->from($tablename) + ->where($query->expr()->eq($selector, $query->createNamedParameter($id))); if ($componentType) { $query->andWhere($query->expr()->eq('componenttype', $query->createNamedParameter($componentType))); @@ -1236,13 +1370,13 @@ function calendarQuery($calendarId, array $filters) { } catch(ParseException $ex) { $this->logger->logException($ex, [ 'app' => 'dav', - 'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'] + 'message' => 'Caught parsing exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri'] ]); continue; } catch (InvalidDataException $ex) { $this->logger->logException($ex, [ 'app' => 'dav', - 'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$calendarId.' uri:'.$row['uri'] + 'message' => 'Caught invalid data exception for calendar data. This usually indicates invalid calendar data. calendar-id:'.$id.' uri:'.$row['uri'] ]); continue; } @@ -1821,7 +1955,16 @@ function createSubscription($principalUri, $uri, array $properties) { ->values($valuesToInsert) ->execute(); - return $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); + $subscriptionId = $this->db->lastInsertId('*PREFIX*calendarsubscriptions'); + + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::createSubscription', new GenericEvent( + '\OCA\DAV\CalDAV\CalDavBackend::createSubscription', + [ + 'subscriptionId' => $subscriptionId, + 'subscriptionData' => $this->getSubscriptionById($subscriptionId), + ])); + + return $subscriptionId; } /** @@ -1869,6 +2012,14 @@ function updateSubscription($subscriptionId, PropPatch $propPatch) { $query->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) ->execute(); + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', new GenericEvent( + '\OCA\DAV\CalDAV\CalDavBackend::updateSubscription', + [ + 'subscriptionId' => $subscriptionId, + 'subscriptionData' => $this->getSubscriptionById($subscriptionId), + 'propertyMutations' => $mutations, + ])); + return true; }); @@ -1881,6 +2032,13 @@ function updateSubscription($subscriptionId, PropPatch $propPatch) { * @return void */ function deleteSubscription($subscriptionId) { + $this->dispatcher->dispatch('\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', new GenericEvent( + '\OCA\DAV\CalDAV\CalDavBackend::deleteSubscription', + [ + 'subscriptionId' => $subscriptionId, + 'subscriptionData' => $this->getSubscriptionById($subscriptionId), + ])); + $query = $this->db->getQueryBuilder(); $query->delete('calendarsubscriptions') ->where($query->expr()->eq('id', $query->createNamedParameter($subscriptionId))) @@ -2111,6 +2269,10 @@ public function getDenormalizedData($calendarData) { } + /** + * @param $cardData + * @return bool|string + */ private function readBlob($cardData) { if (is_resource($cardData)) { return stream_get_contents($cardData); @@ -2301,6 +2463,44 @@ public function deleteAllBirthdayCalendars() { } } + /** + * @param $subscriptionId + */ + public function purgeAllCachedEventsForSubscription($subscriptionId) { + $query = $this->db->getQueryBuilder(); + $query->delete('calendarsubscrobjects') + ->where($query->expr()->eq('subscriptionid', $query->createNamedParameter($subscriptionId))); + $query->execute(); + } + + /** + * @param $subscriptionId + * @param $objectUri + * @param $calendarData + * @return string + * @throws DAV\Exception\BadRequest + */ + public function addCachedEvent($subscriptionId, $objectUri, $calendarData) { + $extraData = $this->getDenormalizedData($calendarData); + + $query = $this->db->getQueryBuilder(); + $query->insert('calendarsubscrobjects') + ->values([ + 'subscriptionid' => $query->createNamedParameter($subscriptionId), + 'uri' => $query->createNamedParameter($objectUri), + 'calendardata' => $query->createNamedParameter($calendarData, IQueryBuilder::PARAM_LOB), + 'lastmodified' => $query->createNamedParameter(time()), + 'etag' => $query->createNamedParameter($extraData['etag']), + 'size' => $query->createNamedParameter($extraData['size']), + 'componenttype' => $query->createNamedParameter($extraData['componentType']), + 'firstoccurence' => $query->createNamedParameter($extraData['firstOccurence']), + 'lastoccurence' => $query->createNamedParameter($extraData['lastOccurence']), + 'classification' => $query->createNamedParameter($extraData['classification']), + 'uid' => $query->createNamedParameter($extraData['uid']), + ]) + ->execute(); + } + /** * read VCalendar data into a VCalendar object * @@ -2349,6 +2549,13 @@ protected function getCalendarObjectId($calendarId, $uri) { return (int)$objectIds['id']; } + /** + * return legacy endpoint principal name to new principal name + * + * @param $principalUri + * @param $toV2 + * @return string + */ private function convertPrincipal($principalUri, $toV2) { if ($this->principalBackend->getPrincipalPrefix() === 'principals') { list(, $name) = Uri\split($principalUri); @@ -2360,6 +2567,11 @@ private function convertPrincipal($principalUri, $toV2) { return $principalUri; } + /** + * adds information about an owner to the calendar data + * + * @param $calendarInfo + */ private function addOwnerPrincipal(&$calendarInfo) { $ownerPrincipalKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_OWNCLOUD . '}owner-principal'; $displaynameKey = '{' . \OCA\DAV\DAV\Sharing\Plugin::NS_NEXTCLOUD . '}owner-displayname'; diff --git a/apps/dav/lib/CalDAV/CalendarHome.php b/apps/dav/lib/CalDAV/CalendarHome.php index 6700b1b249638..9ff71410f87c8 100644 --- a/apps/dav/lib/CalDAV/CalendarHome.php +++ b/apps/dav/lib/CalDAV/CalendarHome.php @@ -42,6 +42,9 @@ class CalendarHome extends \Sabre\CalDAV\CalendarHome { /** @var \OCP\IConfig */ private $config; + /** @var bool */ + private $returnCachedSubscriptions=false; + public function __construct(BackendInterface $caldavBackend, $principalInfo) { parent::__construct($caldavBackend, $principalInfo); $this->l10n = \OC::$server->getL10N('dav'); @@ -91,7 +94,11 @@ function getChildren() { // If the backend supports subscriptions, we'll add those as well, if ($this->caldavBackend instanceof SubscriptionSupport) { foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { - $objects[] = new Subscription($this->caldavBackend, $subscription); + if ($this->returnCachedSubscriptions) { + $objects[] = new CachedSubscription($this->caldavBackend, $subscription); + } else { + $objects[] = new Subscription($this->caldavBackend, $subscription); + } } } @@ -123,6 +130,10 @@ function getChild($name) { if ($this->caldavBackend instanceof SubscriptionSupport) { foreach ($this->caldavBackend->getSubscriptionsForUser($this->principalInfo['uri']) as $subscription) { if ($subscription['uri'] === $name) { + if ($this->returnCachedSubscriptions) { + return new CachedSubscription($this->caldavBackend, $subscription); + } + return new Subscription($this->caldavBackend, $subscription); } } @@ -141,4 +152,11 @@ function calendarSearch(array $filters, $limit=null, $offset=null) { $principalUri = $this->principalInfo['uri']; return $this->caldavBackend->calendarSearch($principalUri, $filters, $limit, $offset); } + + /** + * + */ + public function enableCachedSubscriptionsForThisRequest() { + $this->returnCachedSubscriptions = true; + } } diff --git a/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php b/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php new file mode 100644 index 0000000000000..3ec759cba1d4f --- /dev/null +++ b/apps/dav/lib/CalDAV/WebcalCaching/Plugin.php @@ -0,0 +1,145 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ +namespace OCA\DAV\CalDAV\WebcalCaching; + +use OCA\DAV\CalDAV\CalendarHome; +use OCP\IRequest; +use Sabre\DAV\Exception\NotFound; +use Sabre\DAV\Server; +use Sabre\DAV\ServerPlugin; +use Sabre\HTTP\RequestInterface; +use Sabre\HTTP\ResponseInterface; + +class Plugin extends ServerPlugin { + + /** + * list of regular expressions for calendar user agents, + * that do not support subscriptions on their own + * + * @var string[] + */ + const ENABLE_FOR_CLIENTS = []; + + /** + * @var bool + */ + private $enabled=false; + + /** + * @var Server + */ + private $server; + + /** + * Plugin constructor. + * + * @param IRequest $request + */ + public function __construct(IRequest $request) { + if ($request->isUserAgent(self::ENABLE_FOR_CLIENTS)) { + $this->enabled = true; + } + + $magicHeader = $request->getHeader('X-NC-CALDAV-WEBCAL-CACHING'); + if ($magicHeader === 'ON') { + $this->enabled = true; + } + } + + /** + * This initializes the plugin. + * + * This function is called by Sabre\DAV\Server, after + * addPlugin is called. + * + * This method should set up the required event subscriptions. + * + * @param Server $server + */ + public function initialize(Server $server) { + $this->server = $server; + $server->on('beforeMethod', [$this, 'beforeMethod']); + } + + /** + * @param RequestInterface $request + * @param ResponseInterface $response + */ + public function beforeMethod(RequestInterface $request, ResponseInterface $response) { + if (!$this->enabled) { + return; + } + + $path = $request->getPath(); + $pathParts = explode('/', ltrim($path, '/')); + if (\count($pathParts) < 2) { + return; + } + + // $calendarHomePath will look like: calendars/username + $calendarHomePath = $pathParts[0] . '/' . $pathParts[1]; + try { + $calendarHome = $this->server->tree->getNodeForPath($calendarHomePath); + if (!($calendarHome instanceof CalendarHome)) { + //how did we end up here? + return; + } + + $calendarHome->enableCachedSubscriptionsForThisRequest(); + } catch(NotFound $ex) { + return; + } + } + + /** + * @return bool + */ + public function isCachingEnabledForThisRequest():bool { + return $this->enabled; + } + + /** + * This method should return a list of server-features. + * + * This is for example 'versioning' and is added to the DAV: header + * in an OPTIONS response. + * + * @return string[] + */ + public function getFeatures():array { + return ['nc-calendar-webcal-cache']; + } + + /** + * Returns a plugin name. + * + * Using this name other plugins will be able to access other plugins + * using Sabre\DAV\Server::getPlugin + * + * @return string + */ + public function getPluginName():string { + return 'nc-calendar-webcal-cache'; + } +} diff --git a/apps/dav/lib/Migration/RefreshWebcalJobRegistrar.php b/apps/dav/lib/Migration/RefreshWebcalJobRegistrar.php new file mode 100644 index 0000000000000..912e0aec98e52 --- /dev/null +++ b/apps/dav/lib/Migration/RefreshWebcalJobRegistrar.php @@ -0,0 +1,83 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\DAV\Migration; + +use OCA\DAV\BackgroundJob\RefreshWebcalJob; +use OCP\BackgroundJob\IJobList; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\IRepairStep; + +class RefreshWebcalJobRegistrar implements IRepairStep { + + /** @var IDBConnection */ + private $connection; + + /** @var IJobList */ + private $jobList; + + /** + * FixBirthdayCalendarComponent constructor. + * + * @param IDBConnection $connection + * @param IJobList $jobList + */ + public function __construct(IDBConnection $connection, IJobList $jobList) { + $this->connection = $connection; + $this->jobList = $jobList; + } + + /** + * @inheritdoc + */ + public function getName() { + return 'Registering background jobs to update cache for webcal calendars'; + } + + /** + * @inheritdoc + */ + public function run(IOutput $output) { + $query = $this->connection->getQueryBuilder(); + $query->select(['principaluri', 'uri']) + ->from('calendarsubscriptions'); + $stmt = $query->execute(); + + $count = 0; + while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) { + $args = [ + 'principaluri' => $row['principaluri'], + 'uri' => $row['uri'], + ]; + + if (!$this->jobList->has(RefreshWebcalJob::class, $args)) { + $this->jobList->add(RefreshWebcalJob::class, $args); + $count++; + } + } + + $output->info("Added $count background jobs to update webcal calendars"); + } +} diff --git a/apps/dav/lib/Migration/Version1006Date20180628111625.php b/apps/dav/lib/Migration/Version1006Date20180628111625.php new file mode 100644 index 0000000000000..cbd2075f53449 --- /dev/null +++ b/apps/dav/lib/Migration/Version1006Date20180628111625.php @@ -0,0 +1,109 @@ + + * + * @author Georg Ehrke + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\DAV\Migration; + +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +class Version1006Date20180628111625 extends SimpleMigrationStep { + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('calendarsubscrobjects')) { + $table = $schema->createTable('calendarsubscrobjects'); + $table->addColumn('id', Type::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + 'length' => 11, + 'unsigned' => true, + ]); + $table->addColumn('calendardata', Type::BLOB, [ + 'notnull' => false, + ]); + $table->addColumn('uri', Type::STRING, [ + 'notnull' => false, + 'length' => 255, + ]); + $table->addColumn('subscriptionid', Type::BIGINT, [ + 'notnull' => true, + 'length' => 11, + 'unsigned' => true, + ]); + $table->addColumn('lastmodified', Type::INTEGER, [ + 'notnull' => false, + 'length' => 10, + 'unsigned' => true, + ]); + $table->addColumn('etag', Type::STRING, [ + 'notnull' => false, + 'length' => 32, + ]); + $table->addColumn('size', Type::BIGINT, [ + 'notnull' => true, + 'length' => 11, + 'unsigned' => true, + ]); + $table->addColumn('componenttype', Type::STRING, [ + 'notnull' => false, + 'length' => 8, + ]); + $table->addColumn('firstoccurence', Type::BIGINT, [ + 'notnull' => false, + 'length' => 11, + 'unsigned' => true, + ]); + $table->addColumn('lastoccurence', Type::BIGINT, [ + 'notnull' => false, + 'length' => 11, + 'unsigned' => true, + ]); + $table->addColumn('uid', Type::STRING, [ + 'notnull' => false, + 'length' => 255, + ]); + $table->addColumn('classification', Type::INTEGER, [ + 'notnull' => false, + 'default' => 0, + ]); + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index'); + } + + return $schema; + } +} + + + diff --git a/apps/dav/lib/Server.php b/apps/dav/lib/Server.php index fc8bc91c80abb..dd07081172b65 100644 --- a/apps/dav/lib/Server.php +++ b/apps/dav/lib/Server.php @@ -148,7 +148,11 @@ public function __construct(IRequest $request, $baseUri) { if (\OC::$server->getConfig()->getAppValue('dav', 'sendInvitations', 'yes') === 'yes') { $this->server->addPlugin(\OC::$server->query(\OCA\DAV\CalDAV\Schedule\IMipPlugin::class)); } - $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); + $webcalCachingPlugin = new CalDAV\WebcalCaching\Plugin($request); + $this->server->addPlugin($webcalCachingPlugin); + if (!$webcalCachingPlugin->isCachingEnabledForThisRequest()) { + $this->server->addPlugin(new \Sabre\CalDAV\Subscriptions\Plugin()); + } $this->server->addPlugin(new \Sabre\CalDAV\Notifications\Plugin()); $this->server->addPlugin(new DAV\Sharing\Plugin($authBackend, \OC::$server->getRequest())); $this->server->addPlugin(new \OCA\DAV\CalDAV\Publishing\PublishPlugin( diff --git a/apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php b/apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php new file mode 100644 index 0000000000000..9fe964165dad6 --- /dev/null +++ b/apps/dav/tests/unit/BackgroundJob/RefreshWebcalJobTest.php @@ -0,0 +1,240 @@ + + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\DAV\Tests\unit\BackgroundJob; + +use GuzzleHttp\HandlerStack; +use OCA\DAV\BackgroundJob\RefreshWebcalJob; +use OCA\DAV\CalDAV\CalDavBackend; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\IJobList; +use OCP\Http\Client\IClient; +use OCP\Http\Client\IClientService; +use OCP\Http\Client\IResponse; +use OCP\IConfig; +use OCP\ILogger; +use Test\TestCase; + +class RefreshWebcalJobTest extends TestCase { + + /** @var CalDavBackend | \PHPUnit_Framework_MockObject_MockObject */ + private $caldavBackend; + + /** @var IClientService | \PHPUnit_Framework_MockObject_MockObject */ + private $clientService; + + /** @var IConfig | \PHPUnit_Framework_MockObject_MockObject */ + private $config; + + /** @var ILogger | \PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var ITimeFactory | \PHPUnit_Framework_MockObject_MockObject */ + private $timeFactory; + + /** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */ + private $jobList; + + protected function setUp() { + parent::setUp(); + + $this->caldavBackend = $this->createMock(CalDavBackend::class); + $this->clientService = $this->createMock(IClientService::class); + $this->config = $this->createMock(IConfig::class); + $this->logger = $this->createMock(ILogger::class); + $this->timeFactory = $this->createMock(ITimeFactory::class); + + $this->jobList = $this->createMock(IJobList::class); + } + + /** + * @param string $body + * @param string $contentType + * @param string $result + * + * @dataProvider runDataProvider + */ + public function testRun(string $body, string $contentType, string $result) { + $backgroundJob = new RefreshWebcalJob($this->caldavBackend, + $this->clientService, $this->config, $this->logger, $this->timeFactory); + + $backgroundJob->setArgument([ + 'principaluri' => 'principals/users/testuser', + 'uri' => 'sub123', + ]); + $backgroundJob->setLastRun(0); + + $this->timeFactory->expects($this->once()) + ->method('getTime') + ->with() + ->will($this->returnValue(1000000000)); + + $this->caldavBackend->expects($this->once()) + ->method('getSubscriptionsForUser') + ->with('principals/users/testuser') + ->will($this->returnValue([ + [ + 'id' => 99, + 'uri' => 'sub456', + 'refreshreate' => 'P1D', + 'striptodos' => 1, + 'stripalarms' => 1, + 'stripattachments' => 1, + 'source' => 'webcal://foo.bar/bla' + ], + [ + 'id' => 42, + 'uri' => 'sub123', + 'refreshreate' => 'P1H', + 'striptodos' => 1, + 'stripalarms' => 1, + 'stripattachments' => 1, + 'source' => 'webcal://foo.bar/bla2' + ], + ])); + + $client = $this->createMock(IClient::class); + $response = $this->createMock(IResponse::class); + $this->clientService->expects($this->once()) + ->method('newClient') + ->with() + ->will($this->returnValue($client)); + + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'webcalAllowLocalAccess', 'no') + ->will($this->returnValue('no')); + + $client->expects($this->once()) + ->method('get') + ->with('https://foo.bar/bla2', $this->callback(function($obj) { + return $obj['allow_redirects']['redirects'] === 10 && $obj['handler'] instanceof HandlerStack; + })) + ->will($this->returnValue($response)); + + $response->expects($this->once()) + ->method('getBody') + ->with() + ->will($this->returnValue($body)); + $response->expects($this->once()) + ->method('getHeader') + ->with('Content-Type') + ->will($this->returnValue($contentType)); + + $this->caldavBackend->expects($this->once()) + ->method('purgeAllCachedEventsForSubscription') + ->with(42); + + $this->caldavBackend->expects($this->once()) + ->method('addCachedEvent') + ->with(42, '12345.ics', $result); + + $backgroundJob->execute($this->jobList, $this->logger); + } + + /** + * @return array + */ + public function runDataProvider():array { + return [ + [ + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.1//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", + 'text/calendar;charset=utf8', + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.2//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nUID:12345\r\nDTSTAMP:20160218T133704Z\r\nDTSTART;VALUE=DATE:19000101\r\nDTEND;VALUE=DATE:19000102\r\nRRULE:FREQ=YEARLY\r\nSUMMARY:12345's Birthday (1900)\r\nTRANSP:TRANSPARENT\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n", + ], + [ + '["vcalendar",[["prodid",{},"text","-//Example Corp.//Example Client//EN"],["version",{},"text","2.0"]],[["vtimezone",[["last-modified",{},"date-time","2004-01-10T03:28:45Z"],["tzid",{},"text","US/Eastern"]],[["daylight",[["dtstart",{},"date-time","2000-04-04T02:00:00"],["rrule",{},"recur",{"freq":"YEARLY","byday":"1SU","bymonth":4}],["tzname",{},"text","EDT"],["tzoffsetfrom",{},"utc-offset","-05:00"],["tzoffsetto",{},"utc-offset","-04:00"]],[]],["standard",[["dtstart",{},"date-time","2000-10-26T02:00:00"],["rrule",{},"recur",{"freq":"YEARLY","byday":"1SU","bymonth":10}],["tzname",{},"text","EST"],["tzoffsetfrom",{},"utc-offset","-04:00"],["tzoffsetto",{},"utc-offset","-05:00"]],[]]]],["vevent",[["dtstamp",{},"date-time","2006-02-06T00:11:21Z"],["dtstart",{"tzid":"US/Eastern"},"date-time","2006-01-02T14:00:00"],["duration",{},"duration","PT1H"],["recurrence-id",{"tzid":"US/Eastern"},"date-time","2006-01-04T12:00:00"],["summary",{},"text","Event #2"],["uid",{},"text","12345"]],[]]]]', + 'application/calendar+json', + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.2//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VTIMEZONE\r\nLAST-MODIFIED:20040110T032845Z\r\nTZID:US/Eastern\r\nBEGIN:DAYLIGHT\r\nDTSTART:20000404T020000\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=4\r\nTZNAME:EDT\r\nTZOFFSETFROM:-0500\r\nTZOFFSETTO:-0400\r\nEND:DAYLIGHT\r\nBEGIN:STANDARD\r\nDTSTART:20001026T020000\r\nRRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=10\r\nTZNAME:EST\r\nTZOFFSETFROM:-0400\r\nTZOFFSETTO:-0500\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\nBEGIN:VEVENT\r\nDTSTAMP:20060206T001121Z\r\nDTSTART;TZID=US/Eastern:20060102T140000\r\nDURATION:PT1H\r\nRECURRENCE-ID;TZID=US/Eastern:20060104T120000\r\nSUMMARY:Event #2\r\nUID:12345\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n" + ], + [ + '-//Example Inc.//Example Client//EN2.02006-02-06T00:11:21ZUS/Eastern2006-01-04T14:00:00PT1HUS/Eastern2006-01-04T12:00:00Event #2 bis12345', + 'application/calendar+xml', + "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Sabre//Sabre VObject 4.1.2//EN\r\nCALSCALE:GREGORIAN\r\nBEGIN:VEVENT\r\nDTSTAMP:20060206T001121Z\r\nDTSTART;TZID=US/Eastern:20060104T140000\r\nDURATION:PT1H\r\nRECURRENCE-ID;TZID=US/Eastern:20060104T120000\r\nSUMMARY:Event #2 bis\r\nUID:12345\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n" + ] + ]; + } + + /** + * @dataProvider runLocalURLDataProvider + * + * @param string $source + */ + public function testRunLocalURL($source) { + $backgroundJob = new RefreshWebcalJob($this->caldavBackend, + $this->clientService, $this->config, $this->logger, $this->timeFactory); + + $backgroundJob->setArgument([ + 'principaluri' => 'principals/users/testuser', + 'uri' => 'sub123', + ]); + $backgroundJob->setLastRun(0); + + $this->timeFactory->expects($this->once()) + ->method('getTime') + ->with() + ->will($this->returnValue(1000000000)); + + $this->caldavBackend->expects($this->once()) + ->method('getSubscriptionsForUser') + ->with('principals/users/testuser') + ->will($this->returnValue([ + [ + 'id' => 42, + 'uri' => 'sub123', + 'refreshreate' => 'P1H', + 'striptodos' => 1, + 'stripalarms' => 1, + 'stripattachments' => 1, + 'source' => $source + ], + ])); + + $client = $this->createMock(IClient::class); + $this->clientService->expects($this->once()) + ->method('newClient') + ->with() + ->will($this->returnValue($client)); + + $this->config->expects($this->once()) + ->method('getAppValue') + ->with('dav', 'webcalAllowLocalAccess', 'no') + ->will($this->returnValue('no')); + + $client->expects($this->never()) + ->method('get'); + + $backgroundJob->execute($this->jobList, $this->logger); + } + + public function runLocalURLDataProvider():array { + return [ + ['localhost/foo.bar'], + ['[::1]/bla.blub'], + ['192.168.0.1'], + ['10.0.0.1'], + ['another-host.local'], + ['service.localhost'], + ['!@#$'], // test invalid url + ]; + } +} \ No newline at end of file diff --git a/apps/dav/tests/unit/CalDAV/CachedSubscriptionObjectTest.php b/apps/dav/tests/unit/CalDAV/CachedSubscriptionObjectTest.php new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/apps/dav/tests/unit/CalDAV/CachedSubscriptionTest.php b/apps/dav/tests/unit/CalDAV/CachedSubscriptionTest.php new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/apps/dav/tests/unit/CalDAV/WebcalCaching/PluginTest.php b/apps/dav/tests/unit/CalDAV/WebcalCaching/PluginTest.php new file mode 100644 index 0000000000000..e69de29bb2d1d diff --git a/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php b/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php new file mode 100644 index 0000000000000..c7c3b526612bc --- /dev/null +++ b/apps/dav/tests/unit/Migration/RefreshWebcalJobRegistrarTest.php @@ -0,0 +1,146 @@ + + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace OCA\DAV\Tests\unit\DAV\Migration; + +use OCA\DAV\BackgroundJob\RefreshWebcalJob; +use OCA\DAV\Migration\RefreshWebcalJobRegistrar; +use OCP\BackgroundJob\IJobList; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use Test\TestCase; + +class RefreshWebcalJobRegistrarTest extends TestCase { + + /** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject */ + private $db; + + /** @var IJobList | \PHPUnit_Framework_MockObject_MockObject */ + private $jobList; + + /** @var RefreshWebcalJobRegistrar */ + private $migration; + + protected function setUp() { + parent::setUp(); + + $this->db = $this->createMock(IDBConnection::class); + $this->jobList = $this->createMock(IJobList::class); + + $this->migration = new RefreshWebcalJobRegistrar($this->db, $this->jobList); + } + + public function testGetName() { + $this->assertEquals($this->migration->getName(), 'Registering background jobs to update cache for webcal calendars'); + } + + public function testRun() { + $output = $this->createMock(IOutput::class); + + $queryBuilder = $this->createMock(IQueryBuilder::class); + $statement = $this->createMock(\Doctrine\DBAL\Driver\Statement::class); + + $this->db->expects($this->once()) + ->method('getQueryBuilder') + ->will($this->returnValue($queryBuilder)); + + $queryBuilder->expects($this->at(0)) + ->method('select') + ->with(['principaluri', 'uri']) + ->will($this->returnValue($queryBuilder)); + $queryBuilder->expects($this->at(1)) + ->method('from') + ->with('calendarsubscriptions') + ->will($this->returnValue($queryBuilder)); + $queryBuilder->expects($this->at(2)) + ->method('execute') + ->will($this->returnValue($statement)); + + $statement->expects($this->at(0)) + ->method('fetch') + ->with(\PDO::FETCH_ASSOC) + ->will($this->returnValue([ + 'principaluri' => 'foo1', + 'uri' => 'bar1', + ])); + $statement->expects($this->at(1)) + ->method('fetch') + ->with(\PDO::FETCH_ASSOC) + ->will($this->returnValue([ + 'principaluri' => 'foo2', + 'uri' => 'bar2', + ])); + $statement->expects($this->at(2)) + ->method('fetch') + ->with(\PDO::FETCH_ASSOC) + ->will($this->returnValue([ + 'principaluri' => 'foo3', + 'uri' => 'bar3', + ])); + $statement->expects($this->at(0)) + ->method('fetch') + ->with(\PDO::FETCH_ASSOC) + ->will($this->returnValue(null)); + + $this->jobList->expects($this->at(0)) + ->method('has') + ->with(RefreshWebcalJob::class, [ + 'principaluri' => 'foo1', + 'uri' => 'bar1', + ]) + ->will($this->returnValue(false)); + $this->jobList->expects($this->at(1)) + ->method('add') + ->with(RefreshWebcalJob::class, [ + 'principaluri' => 'foo1', + 'uri' => 'bar1', + ]); + $this->jobList->expects($this->at(2)) + ->method('has') + ->with(RefreshWebcalJob::class, [ + 'principaluri' => 'foo2', + 'uri' => 'bar2', + ]) + ->will($this->returnValue(true)); + $this->jobList->expects($this->at(3)) + ->method('has') + ->with(RefreshWebcalJob::class, [ + 'principaluri' => 'foo3', + 'uri' => 'bar3', + ]) + ->will($this->returnValue(false)); + $this->jobList->expects($this->at(4)) + ->method('add') + ->with(RefreshWebcalJob::class, [ + 'principaluri' => 'foo3', + 'uri' => 'bar3', + ]); + + $output->expects($this->once()) + ->method('info') + ->with('Added 2 background jobs to update webcal calendars'); + + $this->migration->run($output); + } + +} \ No newline at end of file