@@ -31,78 +31,78 @@ |
||
31 | 31 | |
32 | 32 | class Version1006Date20180628111625 extends SimpleMigrationStep { |
33 | 33 | |
34 | - /** |
|
35 | - * @param IOutput $output |
|
36 | - * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
37 | - * @param array $options |
|
38 | - * @return null|ISchemaWrapper |
|
39 | - */ |
|
40 | - public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
41 | - /** @var ISchemaWrapper $schema */ |
|
42 | - $schema = $schemaClosure(); |
|
34 | + /** |
|
35 | + * @param IOutput $output |
|
36 | + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
|
37 | + * @param array $options |
|
38 | + * @return null|ISchemaWrapper |
|
39 | + */ |
|
40 | + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
41 | + /** @var ISchemaWrapper $schema */ |
|
42 | + $schema = $schemaClosure(); |
|
43 | 43 | |
44 | - if (!$schema->hasTable('calendarsubscrobjects')) { |
|
45 | - $table = $schema->createTable('calendarsubscrobjects'); |
|
46 | - $table->addColumn('id', Type::BIGINT, [ |
|
47 | - 'autoincrement' => true, |
|
48 | - 'notnull' => true, |
|
49 | - 'length' => 11, |
|
50 | - 'unsigned' => true, |
|
51 | - ]); |
|
52 | - $table->addColumn('calendardata', Type::BLOB, [ |
|
53 | - 'notnull' => false, |
|
54 | - ]); |
|
55 | - $table->addColumn('uri', Type::STRING, [ |
|
56 | - 'notnull' => false, |
|
57 | - 'length' => 255, |
|
58 | - ]); |
|
59 | - $table->addColumn('subscriptionid', Type::BIGINT, [ |
|
60 | - 'notnull' => true, |
|
61 | - 'length' => 11, |
|
62 | - 'unsigned' => true, |
|
63 | - ]); |
|
64 | - $table->addColumn('lastmodified', Type::INTEGER, [ |
|
65 | - 'notnull' => false, |
|
66 | - 'length' => 10, |
|
67 | - 'unsigned' => true, |
|
68 | - ]); |
|
69 | - $table->addColumn('etag', Type::STRING, [ |
|
70 | - 'notnull' => false, |
|
71 | - 'length' => 32, |
|
72 | - ]); |
|
73 | - $table->addColumn('size', Type::BIGINT, [ |
|
74 | - 'notnull' => true, |
|
75 | - 'length' => 11, |
|
76 | - 'unsigned' => true, |
|
77 | - ]); |
|
78 | - $table->addColumn('componenttype', Type::STRING, [ |
|
79 | - 'notnull' => false, |
|
80 | - 'length' => 8, |
|
81 | - ]); |
|
82 | - $table->addColumn('firstoccurence', Type::BIGINT, [ |
|
83 | - 'notnull' => false, |
|
84 | - 'length' => 11, |
|
85 | - 'unsigned' => true, |
|
86 | - ]); |
|
87 | - $table->addColumn('lastoccurence', Type::BIGINT, [ |
|
88 | - 'notnull' => false, |
|
89 | - 'length' => 11, |
|
90 | - 'unsigned' => true, |
|
91 | - ]); |
|
92 | - $table->addColumn('uid', Type::STRING, [ |
|
93 | - 'notnull' => false, |
|
94 | - 'length' => 255, |
|
95 | - ]); |
|
96 | - $table->addColumn('classification', Type::INTEGER, [ |
|
97 | - 'notnull' => false, |
|
98 | - 'default' => 0, |
|
99 | - ]); |
|
100 | - $table->setPrimaryKey(['id']); |
|
101 | - $table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index'); |
|
102 | - } |
|
44 | + if (!$schema->hasTable('calendarsubscrobjects')) { |
|
45 | + $table = $schema->createTable('calendarsubscrobjects'); |
|
46 | + $table->addColumn('id', Type::BIGINT, [ |
|
47 | + 'autoincrement' => true, |
|
48 | + 'notnull' => true, |
|
49 | + 'length' => 11, |
|
50 | + 'unsigned' => true, |
|
51 | + ]); |
|
52 | + $table->addColumn('calendardata', Type::BLOB, [ |
|
53 | + 'notnull' => false, |
|
54 | + ]); |
|
55 | + $table->addColumn('uri', Type::STRING, [ |
|
56 | + 'notnull' => false, |
|
57 | + 'length' => 255, |
|
58 | + ]); |
|
59 | + $table->addColumn('subscriptionid', Type::BIGINT, [ |
|
60 | + 'notnull' => true, |
|
61 | + 'length' => 11, |
|
62 | + 'unsigned' => true, |
|
63 | + ]); |
|
64 | + $table->addColumn('lastmodified', Type::INTEGER, [ |
|
65 | + 'notnull' => false, |
|
66 | + 'length' => 10, |
|
67 | + 'unsigned' => true, |
|
68 | + ]); |
|
69 | + $table->addColumn('etag', Type::STRING, [ |
|
70 | + 'notnull' => false, |
|
71 | + 'length' => 32, |
|
72 | + ]); |
|
73 | + $table->addColumn('size', Type::BIGINT, [ |
|
74 | + 'notnull' => true, |
|
75 | + 'length' => 11, |
|
76 | + 'unsigned' => true, |
|
77 | + ]); |
|
78 | + $table->addColumn('componenttype', Type::STRING, [ |
|
79 | + 'notnull' => false, |
|
80 | + 'length' => 8, |
|
81 | + ]); |
|
82 | + $table->addColumn('firstoccurence', Type::BIGINT, [ |
|
83 | + 'notnull' => false, |
|
84 | + 'length' => 11, |
|
85 | + 'unsigned' => true, |
|
86 | + ]); |
|
87 | + $table->addColumn('lastoccurence', Type::BIGINT, [ |
|
88 | + 'notnull' => false, |
|
89 | + 'length' => 11, |
|
90 | + 'unsigned' => true, |
|
91 | + ]); |
|
92 | + $table->addColumn('uid', Type::STRING, [ |
|
93 | + 'notnull' => false, |
|
94 | + 'length' => 255, |
|
95 | + ]); |
|
96 | + $table->addColumn('classification', Type::INTEGER, [ |
|
97 | + 'notnull' => false, |
|
98 | + 'default' => 0, |
|
99 | + ]); |
|
100 | + $table->setPrimaryKey(['id']); |
|
101 | + $table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index'); |
|
102 | + } |
|
103 | 103 | |
104 | - return $schema; |
|
105 | - } |
|
104 | + return $schema; |
|
105 | + } |
|
106 | 106 | } |
107 | 107 | |
108 | 108 |
@@ -42,8 +42,6 @@ |
||
42 | 42 | use Sabre\VObject\Reader; |
43 | 43 | use Sabre\VObject\Splitter\ICalendar; |
44 | 44 | |
45 | -use GuzzleHttp\Psr7\Response; |
|
46 | - |
|
47 | 45 | class RefreshWebcalJob extends Job { |
48 | 46 | |
49 | 47 | /** @var CalDavBackend */ |
@@ -46,46 +46,46 @@ discard block |
||
46 | 46 | |
47 | 47 | class RefreshWebcalJob extends Job { |
48 | 48 | |
49 | - /** @var CalDavBackend */ |
|
50 | - private $calDavBackend; |
|
51 | - |
|
52 | - /** @var IClientService */ |
|
53 | - private $clientService; |
|
54 | - |
|
55 | - /** @var ILogger */ |
|
56 | - private $logger; |
|
57 | - |
|
58 | - /** @var ITimeFactory */ |
|
59 | - private $timeFactory; |
|
60 | - |
|
61 | - /** @var array */ |
|
62 | - private $subscription; |
|
63 | - |
|
64 | - /** |
|
65 | - * RefreshWebcalJob constructor. |
|
66 | - * |
|
67 | - * @param CalDavBackend $calDavBackend |
|
68 | - * @param IClientService $clientService |
|
69 | - * @param ILogger $logger |
|
70 | - * @param ITimeFactory $timeFactory |
|
71 | - */ |
|
72 | - public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, ILogger $logger, ITimeFactory $timeFactory) { |
|
73 | - $this->calDavBackend = $calDavBackend; |
|
74 | - $this->clientService = $clientService; |
|
75 | - $this->logger = $logger; |
|
76 | - $this->timeFactory = $timeFactory; |
|
77 | - } |
|
78 | - |
|
79 | - /** |
|
80 | - * this function is called at most every hour |
|
81 | - * |
|
82 | - * @inheritdoc |
|
83 | - */ |
|
84 | - public function execute($jobList, ILogger $logger = null) { |
|
85 | - $subscription = $this->getSubscription($this->argument['principaluri'], $this->argument['uri']); |
|
86 | - if (!$subscription) { |
|
87 | - return; |
|
88 | - } |
|
49 | + /** @var CalDavBackend */ |
|
50 | + private $calDavBackend; |
|
51 | + |
|
52 | + /** @var IClientService */ |
|
53 | + private $clientService; |
|
54 | + |
|
55 | + /** @var ILogger */ |
|
56 | + private $logger; |
|
57 | + |
|
58 | + /** @var ITimeFactory */ |
|
59 | + private $timeFactory; |
|
60 | + |
|
61 | + /** @var array */ |
|
62 | + private $subscription; |
|
63 | + |
|
64 | + /** |
|
65 | + * RefreshWebcalJob constructor. |
|
66 | + * |
|
67 | + * @param CalDavBackend $calDavBackend |
|
68 | + * @param IClientService $clientService |
|
69 | + * @param ILogger $logger |
|
70 | + * @param ITimeFactory $timeFactory |
|
71 | + */ |
|
72 | + public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, ILogger $logger, ITimeFactory $timeFactory) { |
|
73 | + $this->calDavBackend = $calDavBackend; |
|
74 | + $this->clientService = $clientService; |
|
75 | + $this->logger = $logger; |
|
76 | + $this->timeFactory = $timeFactory; |
|
77 | + } |
|
78 | + |
|
79 | + /** |
|
80 | + * this function is called at most every hour |
|
81 | + * |
|
82 | + * @inheritdoc |
|
83 | + */ |
|
84 | + public function execute($jobList, ILogger $logger = null) { |
|
85 | + $subscription = $this->getSubscription($this->argument['principaluri'], $this->argument['uri']); |
|
86 | + if (!$subscription) { |
|
87 | + return; |
|
88 | + } |
|
89 | 89 | |
90 | 90 | // // if no refresh rate was configured, just refresh once a week |
91 | 91 | // $subscriptionId = $subscription['id']; |
@@ -105,238 +105,238 @@ discard block |
||
105 | 105 | // return; |
106 | 106 | // } |
107 | 107 | |
108 | - parent::execute($jobList, $logger); |
|
109 | - } |
|
110 | - |
|
111 | - /** |
|
112 | - * @param array $argument |
|
113 | - */ |
|
114 | - protected function run($argument) { |
|
115 | - $subscription = $this->getSubscription($argument['principaluri'], $argument['uri']); |
|
116 | - $mutations = []; |
|
117 | - if (!$subscription) { |
|
118 | - return; |
|
119 | - } |
|
120 | - |
|
121 | - $webcalData = $this->queryWebcalFeed($subscription, $mutations); |
|
122 | - if (!$webcalData) { |
|
123 | - return; |
|
124 | - } |
|
125 | - |
|
126 | - $stripTodos = $subscription['striptodos'] ?? true; |
|
127 | - $stripAlarms = $subscription['stripalarms'] ?? true; |
|
128 | - $stripAttachments = $subscription['stripattachments'] ?? true; |
|
129 | - |
|
130 | - try { |
|
131 | - $splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING); |
|
132 | - |
|
133 | - // we wait with deleting all outdated events till we parsed the new ones |
|
134 | - // in case the new calendar is broken and `new ICalendar` throws a ParseException |
|
135 | - // the user will still see the old data |
|
136 | - $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']); |
|
137 | - |
|
138 | - while ($vObject = $splitter->getNext()) { |
|
139 | - /** @var Component $vObject */ |
|
140 | - $uid = null; |
|
141 | - $compName = null; |
|
142 | - |
|
143 | - foreach ($vObject->getComponents() as $component) { |
|
144 | - if ($component->name === 'VTIMEZONE') { |
|
145 | - continue; |
|
146 | - } |
|
147 | - |
|
148 | - $uid = $component->{'UID'}->getValue(); |
|
149 | - $compName = $component->name; |
|
150 | - |
|
151 | - if ($stripAlarms) { |
|
152 | - unset($component->{'VALARM'}); |
|
153 | - } |
|
154 | - if ($stripAttachments) { |
|
155 | - unset($component->{'ATTACH'}); |
|
156 | - } |
|
157 | - } |
|
158 | - |
|
159 | - if ($stripTodos && $compName === 'VTODO') { |
|
160 | - continue; |
|
161 | - } |
|
162 | - |
|
163 | - $uri = $uid . '.ics'; |
|
164 | - $calendarData = $vObject->serialize(); |
|
165 | - try { |
|
166 | - $this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData); |
|
167 | - } catch(BadRequest $ex) { |
|
168 | - $this->logger->logException($ex); |
|
169 | - } |
|
170 | - } |
|
171 | - |
|
172 | - $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData); |
|
173 | - if ($newRefreshRate) { |
|
174 | - $mutations['{http://apple.com/ns/ical/}refreshrate'] = $newRefreshRate; |
|
175 | - } |
|
176 | - |
|
177 | - $this->updateSubscription($subscription, $mutations); |
|
178 | - } catch(ParseException $ex) { |
|
179 | - $subscriptionId = $subscription['id']; |
|
180 | - |
|
181 | - $this->logger->logException($ex); |
|
182 | - $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error"); |
|
183 | - } |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * gets webcal feed from remote server |
|
188 | - * |
|
189 | - * @param array $subscription |
|
190 | - * @param array &$mutations |
|
191 | - * @return null|string |
|
192 | - */ |
|
193 | - private function queryWebcalFeed(array $subscription, array &$mutations) { |
|
194 | - $client = $this->clientService->newClient(); |
|
195 | - |
|
196 | - $didBreak301Chain = false; |
|
197 | - $latestLocation = null; |
|
198 | - |
|
199 | - $handlerStack = HandlerStack::create(); |
|
200 | - $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { |
|
201 | - return $request |
|
202 | - ->withHeader('User-Agent', 'Nextcloud Webcal Crawler'); |
|
203 | - })); |
|
204 | - $handlerStack->push(Middleware::mapResponse(function(ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) { |
|
205 | - if (!$didBreak301Chain) { |
|
206 | - if ($response->getStatusCode() !== 301) { |
|
207 | - $didBreak301Chain = true; |
|
208 | - } else { |
|
209 | - $latestLocation = $response->getHeader('Location'); |
|
210 | - } |
|
211 | - } |
|
212 | - return $response; |
|
213 | - })); |
|
214 | - |
|
215 | - try { |
|
216 | - $response = $client->get($subscription['source'], [ |
|
217 | - 'allow_redirects' => [ |
|
218 | - 'redirects' => 10 |
|
219 | - ], |
|
220 | - 'handler' => $handlerStack, |
|
221 | - ]); |
|
222 | - |
|
223 | - $body = $response->getBody(); |
|
224 | - |
|
225 | - if ($latestLocation) { |
|
226 | - $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); |
|
227 | - } |
|
228 | - |
|
229 | - return $body; |
|
230 | - } catch(\Exception $ex) { |
|
231 | - $subscriptionId = $subscription['id']; |
|
232 | - |
|
233 | - $this->logger->logException($ex); |
|
234 | - $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error"); |
|
235 | - |
|
236 | - return null; |
|
237 | - } |
|
238 | - } |
|
239 | - |
|
240 | - /** |
|
241 | - * loads subscription from backend and store it locally |
|
242 | - * |
|
243 | - * @param string $principalUri |
|
244 | - * @param string $uri |
|
245 | - * @return array|null |
|
246 | - */ |
|
247 | - private function getSubscription($principalUri, $uri) { |
|
248 | - if ($this->subscription) { |
|
249 | - return $this->subscription; |
|
250 | - } |
|
251 | - |
|
252 | - $subscriptions = array_filter( |
|
253 | - $this->calDavBackend->getSubscriptionsForUser($principalUri), |
|
254 | - function($sub) use ($uri) { |
|
255 | - return $sub['uri'] === $uri; |
|
256 | - } |
|
257 | - ); |
|
258 | - |
|
259 | - if (\count($subscriptions) === 0) { |
|
260 | - return null; |
|
261 | - } |
|
262 | - |
|
263 | - $this->subscription = $subscriptions[0]; |
|
264 | - return $this->subscription; |
|
265 | - } |
|
266 | - |
|
267 | - /** |
|
268 | - * get total number of seconds from DateInterval object |
|
269 | - * |
|
270 | - * @param \DateInterval $interval |
|
271 | - * @return int |
|
272 | - */ |
|
273 | - private function getIntervalFromDateInterval(\DateInterval $interval):int { |
|
274 | - return $interval->s |
|
275 | - + ($interval->i * 60) |
|
276 | - + ($interval->h * 60 * 60) |
|
277 | - + ($interval->d * 60 * 60 * 24) |
|
278 | - + ($interval->m * 60 * 60 * 24 * 30) |
|
279 | - + ($interval->y * 60 * 60 * 24 * 365); |
|
280 | - } |
|
281 | - |
|
282 | - /** |
|
283 | - * check if: |
|
284 | - * - current subscription stores a refreshrate |
|
285 | - * - the webcal feed suggests a refreshrate |
|
286 | - * - return suggested refreshrate if user didn't set a custom one |
|
287 | - * |
|
288 | - * @param array $subscription |
|
289 | - * @param string $webcalData |
|
290 | - * @return string|null |
|
291 | - */ |
|
292 | - private function checkWebcalDataForRefreshRate($subscription, $webcalData) { |
|
293 | - // if there is no refreshrate stored in the database, check the webcal feed |
|
294 | - // whether it suggests any refresh rate and store that in the database |
|
295 | - if (isset($subscription['refreshrate']) && $subscription['refreshrate'] !== null) { |
|
296 | - return null; |
|
297 | - } |
|
298 | - |
|
299 | - /** @var Component\VCalendar $vCalendar */ |
|
300 | - $vCalendar = Reader::read($webcalData); |
|
301 | - |
|
302 | - $newRefreshrate = null; |
|
303 | - if (isset($vCalendar->{'X-PUBLISHED-TTL'})) { |
|
304 | - $newRefreshrate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue(); |
|
305 | - } |
|
306 | - if (isset($vCalendar->{'REFRESH-INTERVAL'})) { |
|
307 | - $newRefreshrate = $vCalendar->{'REFRESH-INTERVAL'}->getValue(); |
|
308 | - } |
|
309 | - |
|
310 | - if (!$newRefreshrate) { |
|
311 | - return null; |
|
312 | - } |
|
313 | - |
|
314 | - // check if new refresh rate is even valid |
|
315 | - try { |
|
316 | - DateTimeParser::parseDuration($newRefreshrate); |
|
317 | - } catch(InvalidDataException $ex) { |
|
318 | - return null; |
|
319 | - } |
|
320 | - |
|
321 | - return $newRefreshrate; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * update subscription stored in database |
|
326 | - * used to set: |
|
327 | - * - refreshrate |
|
328 | - * - source |
|
329 | - * |
|
330 | - * @param array $subscription |
|
331 | - * @param array $mutations |
|
332 | - */ |
|
333 | - private function updateSubscription(array $subscription, array $mutations) { |
|
334 | - if (empty($mutations)) { |
|
335 | - return; |
|
336 | - } |
|
337 | - |
|
338 | - $propPatch = new PropPatch($mutations); |
|
339 | - $this->calDavBackend->updateSubscription($subscription['id'], $propPatch); |
|
340 | - $propPatch->commit(); |
|
341 | - } |
|
108 | + parent::execute($jobList, $logger); |
|
109 | + } |
|
110 | + |
|
111 | + /** |
|
112 | + * @param array $argument |
|
113 | + */ |
|
114 | + protected function run($argument) { |
|
115 | + $subscription = $this->getSubscription($argument['principaluri'], $argument['uri']); |
|
116 | + $mutations = []; |
|
117 | + if (!$subscription) { |
|
118 | + return; |
|
119 | + } |
|
120 | + |
|
121 | + $webcalData = $this->queryWebcalFeed($subscription, $mutations); |
|
122 | + if (!$webcalData) { |
|
123 | + return; |
|
124 | + } |
|
125 | + |
|
126 | + $stripTodos = $subscription['striptodos'] ?? true; |
|
127 | + $stripAlarms = $subscription['stripalarms'] ?? true; |
|
128 | + $stripAttachments = $subscription['stripattachments'] ?? true; |
|
129 | + |
|
130 | + try { |
|
131 | + $splitter = new ICalendar($webcalData, Reader::OPTION_FORGIVING); |
|
132 | + |
|
133 | + // we wait with deleting all outdated events till we parsed the new ones |
|
134 | + // in case the new calendar is broken and `new ICalendar` throws a ParseException |
|
135 | + // the user will still see the old data |
|
136 | + $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']); |
|
137 | + |
|
138 | + while ($vObject = $splitter->getNext()) { |
|
139 | + /** @var Component $vObject */ |
|
140 | + $uid = null; |
|
141 | + $compName = null; |
|
142 | + |
|
143 | + foreach ($vObject->getComponents() as $component) { |
|
144 | + if ($component->name === 'VTIMEZONE') { |
|
145 | + continue; |
|
146 | + } |
|
147 | + |
|
148 | + $uid = $component->{'UID'}->getValue(); |
|
149 | + $compName = $component->name; |
|
150 | + |
|
151 | + if ($stripAlarms) { |
|
152 | + unset($component->{'VALARM'}); |
|
153 | + } |
|
154 | + if ($stripAttachments) { |
|
155 | + unset($component->{'ATTACH'}); |
|
156 | + } |
|
157 | + } |
|
158 | + |
|
159 | + if ($stripTodos && $compName === 'VTODO') { |
|
160 | + continue; |
|
161 | + } |
|
162 | + |
|
163 | + $uri = $uid . '.ics'; |
|
164 | + $calendarData = $vObject->serialize(); |
|
165 | + try { |
|
166 | + $this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData); |
|
167 | + } catch(BadRequest $ex) { |
|
168 | + $this->logger->logException($ex); |
|
169 | + } |
|
170 | + } |
|
171 | + |
|
172 | + $newRefreshRate = $this->checkWebcalDataForRefreshRate($subscription, $webcalData); |
|
173 | + if ($newRefreshRate) { |
|
174 | + $mutations['{http://apple.com/ns/ical/}refreshrate'] = $newRefreshRate; |
|
175 | + } |
|
176 | + |
|
177 | + $this->updateSubscription($subscription, $mutations); |
|
178 | + } catch(ParseException $ex) { |
|
179 | + $subscriptionId = $subscription['id']; |
|
180 | + |
|
181 | + $this->logger->logException($ex); |
|
182 | + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error"); |
|
183 | + } |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * gets webcal feed from remote server |
|
188 | + * |
|
189 | + * @param array $subscription |
|
190 | + * @param array &$mutations |
|
191 | + * @return null|string |
|
192 | + */ |
|
193 | + private function queryWebcalFeed(array $subscription, array &$mutations) { |
|
194 | + $client = $this->clientService->newClient(); |
|
195 | + |
|
196 | + $didBreak301Chain = false; |
|
197 | + $latestLocation = null; |
|
198 | + |
|
199 | + $handlerStack = HandlerStack::create(); |
|
200 | + $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { |
|
201 | + return $request |
|
202 | + ->withHeader('User-Agent', 'Nextcloud Webcal Crawler'); |
|
203 | + })); |
|
204 | + $handlerStack->push(Middleware::mapResponse(function(ResponseInterface $response) use (&$didBreak301Chain, &$latestLocation) { |
|
205 | + if (!$didBreak301Chain) { |
|
206 | + if ($response->getStatusCode() !== 301) { |
|
207 | + $didBreak301Chain = true; |
|
208 | + } else { |
|
209 | + $latestLocation = $response->getHeader('Location'); |
|
210 | + } |
|
211 | + } |
|
212 | + return $response; |
|
213 | + })); |
|
214 | + |
|
215 | + try { |
|
216 | + $response = $client->get($subscription['source'], [ |
|
217 | + 'allow_redirects' => [ |
|
218 | + 'redirects' => 10 |
|
219 | + ], |
|
220 | + 'handler' => $handlerStack, |
|
221 | + ]); |
|
222 | + |
|
223 | + $body = $response->getBody(); |
|
224 | + |
|
225 | + if ($latestLocation) { |
|
226 | + $mutations['{http://calendarserver.org/ns/}source'] = new Href($latestLocation); |
|
227 | + } |
|
228 | + |
|
229 | + return $body; |
|
230 | + } catch(\Exception $ex) { |
|
231 | + $subscriptionId = $subscription['id']; |
|
232 | + |
|
233 | + $this->logger->logException($ex); |
|
234 | + $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error"); |
|
235 | + |
|
236 | + return null; |
|
237 | + } |
|
238 | + } |
|
239 | + |
|
240 | + /** |
|
241 | + * loads subscription from backend and store it locally |
|
242 | + * |
|
243 | + * @param string $principalUri |
|
244 | + * @param string $uri |
|
245 | + * @return array|null |
|
246 | + */ |
|
247 | + private function getSubscription($principalUri, $uri) { |
|
248 | + if ($this->subscription) { |
|
249 | + return $this->subscription; |
|
250 | + } |
|
251 | + |
|
252 | + $subscriptions = array_filter( |
|
253 | + $this->calDavBackend->getSubscriptionsForUser($principalUri), |
|
254 | + function($sub) use ($uri) { |
|
255 | + return $sub['uri'] === $uri; |
|
256 | + } |
|
257 | + ); |
|
258 | + |
|
259 | + if (\count($subscriptions) === 0) { |
|
260 | + return null; |
|
261 | + } |
|
262 | + |
|
263 | + $this->subscription = $subscriptions[0]; |
|
264 | + return $this->subscription; |
|
265 | + } |
|
266 | + |
|
267 | + /** |
|
268 | + * get total number of seconds from DateInterval object |
|
269 | + * |
|
270 | + * @param \DateInterval $interval |
|
271 | + * @return int |
|
272 | + */ |
|
273 | + private function getIntervalFromDateInterval(\DateInterval $interval):int { |
|
274 | + return $interval->s |
|
275 | + + ($interval->i * 60) |
|
276 | + + ($interval->h * 60 * 60) |
|
277 | + + ($interval->d * 60 * 60 * 24) |
|
278 | + + ($interval->m * 60 * 60 * 24 * 30) |
|
279 | + + ($interval->y * 60 * 60 * 24 * 365); |
|
280 | + } |
|
281 | + |
|
282 | + /** |
|
283 | + * check if: |
|
284 | + * - current subscription stores a refreshrate |
|
285 | + * - the webcal feed suggests a refreshrate |
|
286 | + * - return suggested refreshrate if user didn't set a custom one |
|
287 | + * |
|
288 | + * @param array $subscription |
|
289 | + * @param string $webcalData |
|
290 | + * @return string|null |
|
291 | + */ |
|
292 | + private function checkWebcalDataForRefreshRate($subscription, $webcalData) { |
|
293 | + // if there is no refreshrate stored in the database, check the webcal feed |
|
294 | + // whether it suggests any refresh rate and store that in the database |
|
295 | + if (isset($subscription['refreshrate']) && $subscription['refreshrate'] !== null) { |
|
296 | + return null; |
|
297 | + } |
|
298 | + |
|
299 | + /** @var Component\VCalendar $vCalendar */ |
|
300 | + $vCalendar = Reader::read($webcalData); |
|
301 | + |
|
302 | + $newRefreshrate = null; |
|
303 | + if (isset($vCalendar->{'X-PUBLISHED-TTL'})) { |
|
304 | + $newRefreshrate = $vCalendar->{'X-PUBLISHED-TTL'}->getValue(); |
|
305 | + } |
|
306 | + if (isset($vCalendar->{'REFRESH-INTERVAL'})) { |
|
307 | + $newRefreshrate = $vCalendar->{'REFRESH-INTERVAL'}->getValue(); |
|
308 | + } |
|
309 | + |
|
310 | + if (!$newRefreshrate) { |
|
311 | + return null; |
|
312 | + } |
|
313 | + |
|
314 | + // check if new refresh rate is even valid |
|
315 | + try { |
|
316 | + DateTimeParser::parseDuration($newRefreshrate); |
|
317 | + } catch(InvalidDataException $ex) { |
|
318 | + return null; |
|
319 | + } |
|
320 | + |
|
321 | + return $newRefreshrate; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * update subscription stored in database |
|
326 | + * used to set: |
|
327 | + * - refreshrate |
|
328 | + * - source |
|
329 | + * |
|
330 | + * @param array $subscription |
|
331 | + * @param array $mutations |
|
332 | + */ |
|
333 | + private function updateSubscription(array $subscription, array $mutations) { |
|
334 | + if (empty($mutations)) { |
|
335 | + return; |
|
336 | + } |
|
337 | + |
|
338 | + $propPatch = new PropPatch($mutations); |
|
339 | + $this->calDavBackend->updateSubscription($subscription['id'], $propPatch); |
|
340 | + $propPatch->commit(); |
|
341 | + } |
|
342 | 342 | } |
@@ -160,11 +160,11 @@ discard block |
||
160 | 160 | continue; |
161 | 161 | } |
162 | 162 | |
163 | - $uri = $uid . '.ics'; |
|
163 | + $uri = $uid.'.ics'; |
|
164 | 164 | $calendarData = $vObject->serialize(); |
165 | 165 | try { |
166 | 166 | $this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData); |
167 | - } catch(BadRequest $ex) { |
|
167 | + } catch (BadRequest $ex) { |
|
168 | 168 | $this->logger->logException($ex); |
169 | 169 | } |
170 | 170 | } |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | } |
176 | 176 | |
177 | 177 | $this->updateSubscription($subscription, $mutations); |
178 | - } catch(ParseException $ex) { |
|
178 | + } catch (ParseException $ex) { |
|
179 | 179 | $subscriptionId = $subscription['id']; |
180 | 180 | |
181 | 181 | $this->logger->logException($ex); |
@@ -197,7 +197,7 @@ discard block |
||
197 | 197 | $latestLocation = null; |
198 | 198 | |
199 | 199 | $handlerStack = HandlerStack::create(); |
200 | - $handlerStack->push(Middleware::mapRequest(function (RequestInterface $request) { |
|
200 | + $handlerStack->push(Middleware::mapRequest(function(RequestInterface $request) { |
|
201 | 201 | return $request |
202 | 202 | ->withHeader('User-Agent', 'Nextcloud Webcal Crawler'); |
203 | 203 | })); |
@@ -227,7 +227,7 @@ discard block |
||
227 | 227 | } |
228 | 228 | |
229 | 229 | return $body; |
230 | - } catch(\Exception $ex) { |
|
230 | + } catch (\Exception $ex) { |
|
231 | 231 | $subscriptionId = $subscription['id']; |
232 | 232 | |
233 | 233 | $this->logger->logException($ex); |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | // check if new refresh rate is even valid |
315 | 315 | try { |
316 | 316 | DateTimeParser::parseDuration($newRefreshrate); |
317 | - } catch(InvalidDataException $ex) { |
|
317 | + } catch (InvalidDataException $ex) { |
|
318 | 318 | return null; |
319 | 319 | } |
320 | 320 |
@@ -34,152 +34,152 @@ |
||
34 | 34 | */ |
35 | 35 | class HistoryPlugin implements EventSubscriberInterface, \IteratorAggregate, \Countable |
36 | 36 | { |
37 | - /** @var int The maximum number of requests to maintain in the history */ |
|
38 | - protected $limit = 10; |
|
39 | - |
|
40 | - /** @var array Requests and responses that have passed through the plugin */ |
|
41 | - protected $transactions = array(); |
|
42 | - |
|
43 | - public static function getSubscribedEvents() |
|
44 | - { |
|
45 | - return array('request.sent' => array('onRequestSent', 9999)); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Convert to a string that contains all request and response headers |
|
50 | - * |
|
51 | - * @return string |
|
52 | - */ |
|
53 | - public function __toString() |
|
54 | - { |
|
55 | - $lines = array(); |
|
56 | - foreach ($this->transactions as $entry) { |
|
57 | - $response = isset($entry['response']) ? $entry['response'] : ''; |
|
58 | - $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n"; |
|
59 | - } |
|
60 | - |
|
61 | - return implode("\n", $lines); |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Add a request to the history |
|
66 | - * |
|
67 | - * @param RequestInterface $request Request to add |
|
68 | - * @param Response $response Response of the request |
|
69 | - * |
|
70 | - * @return HistoryPlugin |
|
71 | - */ |
|
72 | - public function add(RequestInterface $request, Response $response = null) |
|
73 | - { |
|
74 | - if (!$response && $request->getResponse()) { |
|
75 | - $response = $request->getResponse(); |
|
76 | - } |
|
77 | - |
|
78 | - $this->transactions[] = array('request' => $request, 'response' => $response); |
|
79 | - if (count($this->transactions) > $this->getlimit()) { |
|
80 | - array_shift($this->transactions); |
|
81 | - } |
|
82 | - |
|
83 | - return $this; |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Set the max number of requests to store |
|
88 | - * |
|
89 | - * @param int $limit Limit |
|
90 | - * |
|
91 | - * @return HistoryPlugin |
|
92 | - */ |
|
93 | - public function setLimit($limit) |
|
94 | - { |
|
95 | - $this->limit = (int) $limit; |
|
96 | - |
|
97 | - return $this; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Get the request limit |
|
102 | - * |
|
103 | - * @return int |
|
104 | - */ |
|
105 | - public function getLimit() |
|
106 | - { |
|
107 | - return $this->limit; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Get all of the raw transactions in the form of an array of associative arrays containing |
|
112 | - * 'request' and 'response' keys. |
|
113 | - * |
|
114 | - * @return array |
|
115 | - */ |
|
116 | - public function getAll() |
|
117 | - { |
|
118 | - return $this->transactions; |
|
119 | - } |
|
120 | - |
|
121 | - /** |
|
122 | - * Get the requests in the history |
|
123 | - * |
|
124 | - * @return \ArrayIterator |
|
125 | - */ |
|
126 | - public function getIterator() |
|
127 | - { |
|
128 | - // Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll()) |
|
129 | - return new \ArrayIterator(array_map(function ($entry) { |
|
130 | - $entry['request']->getParams()->set('actual_response', $entry['response']); |
|
131 | - return $entry['request']; |
|
132 | - }, $this->transactions)); |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Get the number of requests in the history |
|
137 | - * |
|
138 | - * @return int |
|
139 | - */ |
|
140 | - public function count() |
|
141 | - { |
|
142 | - return count($this->transactions); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get the last request sent |
|
147 | - * |
|
148 | - * @return RequestInterface |
|
149 | - */ |
|
150 | - public function getLastRequest() |
|
151 | - { |
|
152 | - $last = end($this->transactions); |
|
153 | - |
|
154 | - return $last['request']; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Get the last response in the history |
|
159 | - * |
|
160 | - * @return Response|null |
|
161 | - */ |
|
162 | - public function getLastResponse() |
|
163 | - { |
|
164 | - $last = end($this->transactions); |
|
165 | - |
|
166 | - return isset($last['response']) ? $last['response'] : null; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * Clears the history |
|
171 | - * |
|
172 | - * @return HistoryPlugin |
|
173 | - */ |
|
174 | - public function clear() |
|
175 | - { |
|
176 | - $this->transactions = array(); |
|
177 | - |
|
178 | - return $this; |
|
179 | - } |
|
180 | - |
|
181 | - public function onRequestSent(Event $event) |
|
182 | - { |
|
183 | - $this->add($event['request'], $event['response']); |
|
184 | - } |
|
37 | + /** @var int The maximum number of requests to maintain in the history */ |
|
38 | + protected $limit = 10; |
|
39 | + |
|
40 | + /** @var array Requests and responses that have passed through the plugin */ |
|
41 | + protected $transactions = array(); |
|
42 | + |
|
43 | + public static function getSubscribedEvents() |
|
44 | + { |
|
45 | + return array('request.sent' => array('onRequestSent', 9999)); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Convert to a string that contains all request and response headers |
|
50 | + * |
|
51 | + * @return string |
|
52 | + */ |
|
53 | + public function __toString() |
|
54 | + { |
|
55 | + $lines = array(); |
|
56 | + foreach ($this->transactions as $entry) { |
|
57 | + $response = isset($entry['response']) ? $entry['response'] : ''; |
|
58 | + $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n"; |
|
59 | + } |
|
60 | + |
|
61 | + return implode("\n", $lines); |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Add a request to the history |
|
66 | + * |
|
67 | + * @param RequestInterface $request Request to add |
|
68 | + * @param Response $response Response of the request |
|
69 | + * |
|
70 | + * @return HistoryPlugin |
|
71 | + */ |
|
72 | + public function add(RequestInterface $request, Response $response = null) |
|
73 | + { |
|
74 | + if (!$response && $request->getResponse()) { |
|
75 | + $response = $request->getResponse(); |
|
76 | + } |
|
77 | + |
|
78 | + $this->transactions[] = array('request' => $request, 'response' => $response); |
|
79 | + if (count($this->transactions) > $this->getlimit()) { |
|
80 | + array_shift($this->transactions); |
|
81 | + } |
|
82 | + |
|
83 | + return $this; |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Set the max number of requests to store |
|
88 | + * |
|
89 | + * @param int $limit Limit |
|
90 | + * |
|
91 | + * @return HistoryPlugin |
|
92 | + */ |
|
93 | + public function setLimit($limit) |
|
94 | + { |
|
95 | + $this->limit = (int) $limit; |
|
96 | + |
|
97 | + return $this; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Get the request limit |
|
102 | + * |
|
103 | + * @return int |
|
104 | + */ |
|
105 | + public function getLimit() |
|
106 | + { |
|
107 | + return $this->limit; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Get all of the raw transactions in the form of an array of associative arrays containing |
|
112 | + * 'request' and 'response' keys. |
|
113 | + * |
|
114 | + * @return array |
|
115 | + */ |
|
116 | + public function getAll() |
|
117 | + { |
|
118 | + return $this->transactions; |
|
119 | + } |
|
120 | + |
|
121 | + /** |
|
122 | + * Get the requests in the history |
|
123 | + * |
|
124 | + * @return \ArrayIterator |
|
125 | + */ |
|
126 | + public function getIterator() |
|
127 | + { |
|
128 | + // Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll()) |
|
129 | + return new \ArrayIterator(array_map(function ($entry) { |
|
130 | + $entry['request']->getParams()->set('actual_response', $entry['response']); |
|
131 | + return $entry['request']; |
|
132 | + }, $this->transactions)); |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Get the number of requests in the history |
|
137 | + * |
|
138 | + * @return int |
|
139 | + */ |
|
140 | + public function count() |
|
141 | + { |
|
142 | + return count($this->transactions); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get the last request sent |
|
147 | + * |
|
148 | + * @return RequestInterface |
|
149 | + */ |
|
150 | + public function getLastRequest() |
|
151 | + { |
|
152 | + $last = end($this->transactions); |
|
153 | + |
|
154 | + return $last['request']; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Get the last response in the history |
|
159 | + * |
|
160 | + * @return Response|null |
|
161 | + */ |
|
162 | + public function getLastResponse() |
|
163 | + { |
|
164 | + $last = end($this->transactions); |
|
165 | + |
|
166 | + return isset($last['response']) ? $last['response'] : null; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * Clears the history |
|
171 | + * |
|
172 | + * @return HistoryPlugin |
|
173 | + */ |
|
174 | + public function clear() |
|
175 | + { |
|
176 | + $this->transactions = array(); |
|
177 | + |
|
178 | + return $this; |
|
179 | + } |
|
180 | + |
|
181 | + public function onRequestSent(Event $event) |
|
182 | + { |
|
183 | + $this->add($event['request'], $event['response']); |
|
184 | + } |
|
185 | 185 | } |
186 | 186 | \ No newline at end of file |
@@ -55,7 +55,7 @@ discard block |
||
55 | 55 | $lines = array(); |
56 | 56 | foreach ($this->transactions as $entry) { |
57 | 57 | $response = isset($entry['response']) ? $entry['response'] : ''; |
58 | - $lines[] = '> ' . trim($entry['request']) . "\n\n< " . trim($response) . "\n"; |
|
58 | + $lines[] = '> '.trim($entry['request'])."\n\n< ".trim($response)."\n"; |
|
59 | 59 | } |
60 | 60 | |
61 | 61 | return implode("\n", $lines); |
@@ -126,7 +126,7 @@ discard block |
||
126 | 126 | public function getIterator() |
127 | 127 | { |
128 | 128 | // Return an iterator just like the old iteration of the HistoryPlugin for BC compatibility (use getAll()) |
129 | - return new \ArrayIterator(array_map(function ($entry) { |
|
129 | + return new \ArrayIterator(array_map(function($entry) { |
|
130 | 130 | $entry['request']->getParams()->set('actual_response', $entry['response']); |
131 | 131 | return $entry['request']; |
132 | 132 | }, $this->transactions)); |