|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | +/** |
| 4 | + * @copyright 2018 Georg Ehrke <oc.list@georgehrke.com> |
| 5 | + * |
| 6 | + * @author Georg Ehrke <oc.list@georgehrke.com> |
| 7 | + * |
| 8 | + * @license GNU AGPL version 3 or any later version |
| 9 | + * |
| 10 | + * This program is free software: you can redistribute it and/or modify |
| 11 | + * it under the terms of the GNU Affero General Public License as |
| 12 | + * published by the Free Software Foundation, either version 3 of the |
| 13 | + * License, or (at your option) any later version. |
| 14 | + * |
| 15 | + * This program is distributed in the hope that it will be useful, |
| 16 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | + * GNU Affero General Public License for more details. |
| 19 | + * |
| 20 | + * You should have received a copy of the GNU Affero General Public License |
| 21 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | + * |
| 23 | + */ |
| 24 | +namespace OCA\DAV\BackgroundJob; |
| 25 | + |
| 26 | +use OC\BackgroundJob\TimedJob; |
| 27 | +use OCA\DAV\CalDAV\CalDavBackend; |
| 28 | +use OCP\Http\Client\IClientService; |
| 29 | +use OCP\ILogger; |
| 30 | +use Sabre\DAV\Exception\BadRequest; |
| 31 | +use Sabre\VObject\Component; |
| 32 | +use Sabre\VObject\ParseException; |
| 33 | +use Sabre\VObject\Splitter\ICalendar; |
| 34 | + |
| 35 | +class RefreshWebcalJob extends TimedJob { |
| 36 | + |
| 37 | + /** @var CalDavBackend */ |
| 38 | + private $calDavBackend; |
| 39 | + |
| 40 | + /** @var IClientService */ |
| 41 | + private $clientService; |
| 42 | + |
| 43 | + /** @var ILogger */ |
| 44 | + private $logger; |
| 45 | + |
| 46 | + /** |
| 47 | + * RefreshWebcalJob constructor. |
| 48 | + * |
| 49 | + * @param CalDavBackend $calDavBackend |
| 50 | + * @param IClientService $clientService |
| 51 | + * @param ILogger $logger |
| 52 | + */ |
| 53 | + public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, ILogger $logger) { |
| 54 | + $this->calDavBackend = $calDavBackend; |
| 55 | + $this->clientService = $clientService; |
| 56 | + $this->logger = $logger; |
| 57 | + |
| 58 | + $this->setInterval(60*60*24); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @param array $argument |
| 63 | + */ |
| 64 | + protected function run($argument) { |
| 65 | + $principalUri = $argument['principaluri']; |
| 66 | + $uri = $argument['uri']; |
| 67 | + |
| 68 | + $subscriptions = array_filter( |
| 69 | + $this->calDavBackend->getSubscriptionsForUser($principalUri), |
| 70 | + function($sub) use ($uri) { |
| 71 | + return $sub['uri'] === $uri; |
| 72 | + } |
| 73 | + ); |
| 74 | + |
| 75 | + if (\count($subscriptions) === 0) { |
| 76 | + return; |
| 77 | + } |
| 78 | + /** @var array $subscription */ |
| 79 | + $subscription = $subscriptions[0]; |
| 80 | + |
| 81 | +// if ($subscription['refreshrate']) |
| 82 | + |
| 83 | + $webcalData = $this->queryWebcalFeed($subscription); |
| 84 | + if (!$webcalData) { |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + $stripTodos = $subscription['striptodos']; |
| 89 | + $stripAlarms = $subscription['stripalarms']; |
| 90 | + $stripAttachments = $subscription['stripattachments']; |
| 91 | + |
| 92 | + try { |
| 93 | + $splitter = new ICalendar($webcalData); |
| 94 | + |
| 95 | + // we wait with deleting all purged events till we parsed the new ones |
| 96 | + // in case the new calendar is broken and new ICalendar throws a ParseException |
| 97 | + // the user will still see the old data |
| 98 | + $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']); |
| 99 | + |
| 100 | + while ($vObject = $splitter->getNext()) { |
| 101 | + /** @var Component $vObject */ |
| 102 | + $uid = null; |
| 103 | + $compName = null; |
| 104 | + |
| 105 | + foreach ($vObject->getComponents() as $component) { |
| 106 | + if ($component->name === 'VTIMEZONE') { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + $uid = $component->{'UID'}->getValue(); |
| 111 | + $compName = $component->name; |
| 112 | + |
| 113 | + if ($stripAlarms) { |
| 114 | + unset($component->{'VALARM'}); |
| 115 | + } |
| 116 | + if ($stripAttachments) { |
| 117 | + unset($component->{'ATTACH'}); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if ($stripTodos && $compName === 'VTODO') { |
| 122 | + continue; |
| 123 | + } |
| 124 | + |
| 125 | + $uri = $uid . '.ics'; |
| 126 | + $calendarData = $vObject->serialize(); |
| 127 | + try { |
| 128 | + $this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData); |
| 129 | + } catch(BadRequest $ex) { |
| 130 | + $this->logger->logException($ex); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + } catch(ParseException $ex) { |
| 135 | + $subscriptionId = $subscription['id']; |
| 136 | + |
| 137 | + $this->logger->logException($ex); |
| 138 | + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error"); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + /** |
| 143 | + * @param array $subscription |
| 144 | + * @return null|resource |
| 145 | + */ |
| 146 | + private function queryWebcalFeed(array $subscription) { |
| 147 | + $client = $this->clientService->newClient(); |
| 148 | + try { |
| 149 | + $response = $client->get($subscription['source'], [ |
| 150 | + 'stream' => true, |
| 151 | + 'allow_redirects' => [ |
| 152 | + 'redirects' => 10 |
| 153 | + ] |
| 154 | + ]); |
| 155 | + |
| 156 | + return $response->getBody(); |
| 157 | + } catch(\Exception $ex) { |
| 158 | + $subscriptionId = $subscription['id']; |
| 159 | + |
| 160 | + $this->logger->logException($ex); |
| 161 | + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error"); |
| 162 | + |
| 163 | + return null; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments