Skip to content

Commit 3adc5ff

Browse files
author
Marc Vachette
committed
catch exception if event date are in a wrong format
1 parent 818d0d0 commit 3adc5ff

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/IcalParser.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,11 @@ private function parseRow($row) {
244244

245245
// process simple dates with timezone
246246
if ($key === 'DTSTAMP' || $key === 'LAST-MODIFIED' || $key === 'CREATED' || $key === 'DTSTART' || $key === 'DTEND') {
247-
$value = new \DateTime($value, ($timezone ?: $this->timezone));
247+
try {
248+
$value = new \DateTime($value, ($timezone ?: $this->timezone));
249+
} catch (\Exception $e) {
250+
$value = null;
251+
}
248252
}
249253

250254
if ($key === 'RRULE' && preg_match_all('#(?<key>[^=;]+)=(?<value>[^;]+)#', $value, $matches, PREG_SET_ORDER)) {
@@ -298,7 +302,7 @@ public function getTimezones() {
298302
*/
299303
public function getSortedEvents() {
300304
if ($events = $this->getEvents()) {
301-
usort(
305+
usort(
302306
$events, function ($a, $b) {
303307
return $a['DTSTART'] > $b['DTSTART'];
304308
}

tests/cal/wrong_dates.ics

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
BEGIN:VCALENDAR
2+
PRODID:-//Google Inc//Google Calendar 70.9054//EN
3+
VERSION:2.0
4+
CALSCALE:GREGORIAN
5+
X-WR-CALNAME:[email protected]
6+
X-WR-TIMEZONE:America/Los_Angeles
7+
BEGIN:VTIMEZONE
8+
TZID:America/Los_Angeles
9+
X-LIC-LOCATION:America/Los_Angeles
10+
END:VTIMEZONE
11+
BEGIN:VEVENT
12+
DTSTART;VALUE=DATE:2014929
13+
DTEND;VALUE=DATE:20140930
14+
15+
CREATED:20120724T212411Z
16+
DESCRIPTION:
17+
LAST-MODIFIED:20120724T212411Z
18+
LOCATION:
19+
SEQUENCE:0
20+
STATUS:CONFIRMED
21+
SUMMARY:Really long event name thing
22+
TRANSP:OPAQUE
23+
END:VEVENT
24+
BEGIN:VEVENT
25+
DTSTART;VALUE=DATE:20140929
26+
DTEND;VALUE=DATE:2014930
27+
28+
CREATED:20120724T212411Z
29+
DESCRIPTION:
30+
LAST-MODIFIED:20120724T212411Z
31+
LOCATION:
32+
SEQUENCE:0
33+
STATUS:CONFIRMED
34+
SUMMARY:Really long event name thing
35+
TRANSP:OPAQUE
36+
END:VEVENT
37+
END:VCALENDAR

tests/wrong_dates.phpt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
/**
3+
* @author Roman Ozana <[email protected]>
4+
*/
5+
use Tester\Assert;
6+
7+
require_once __DIR__ . '/../vendor/autoload.php';
8+
\Tester\Environment::setup();
9+
10+
$cal = new \om\IcalParser();
11+
$results = $cal->parseFile(__DIR__ . '/cal/wrong_dates.ics');
12+
$events = $cal->getSortedEvents();
13+
Assert::same('29.9.2014 00:00:00', $events[1]['DTSTART']->format('j.n.Y H:i:s'));
14+
Assert::same(null, $events[1]['DTEND']);
15+
16+
Assert::same(null, $events[0]['DTSTART']);
17+
Assert::same('30.9.2014 00:00:00', $events[0]['DTEND']->format('j.n.Y H:i:s'));

0 commit comments

Comments
 (0)