|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: Claudio Cardinale <[email protected]> |
|
5
|
|
|
* Date: 03/12/15 |
|
6
|
|
|
* Time: 2.33 |
|
7
|
|
|
* This program is free software; you can redistribute it and/or |
|
8
|
|
|
* modify it under the terms of the GNU General Public License |
|
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
10
|
|
|
* of the License, or (at your option) any later version. |
|
11
|
|
|
* This program is distributed in the hope that it will be useful, |
|
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14
|
|
|
* GNU General Public License for more details. |
|
15
|
|
|
* You should have received a copy of the GNU General Public License |
|
16
|
|
|
* along with this program; if not, write to the Free Software |
|
17
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace plunner\Console\Commands\SyncCaldav; |
|
21
|
|
|
|
|
22
|
|
|
use it\thecsea\caldav_client_adapter\simple_caldav_client\SimpleCaldavAdapter; |
|
23
|
|
|
use \it\thecsea\caldav_client_adapter\EventInterface; |
|
24
|
|
|
use plunner\Caldav; |
|
25
|
|
|
use plunner\Events\CaldavErrorEvent; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Class Sync |
|
29
|
|
|
* @package plunner\Console\Commands |
|
30
|
|
|
* @author Claudio Cardinale <[email protected]> |
|
31
|
|
|
* @copyright 2015 Claudio Cardinale |
|
32
|
|
|
* @version 1.0.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
class Sync |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var Caldav |
|
38
|
|
|
*/ |
|
39
|
|
|
private $calendar; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Sync constructor. |
|
43
|
|
|
* @param Caldav $calendar |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(Caldav $calendar) |
|
46
|
|
|
{ |
|
47
|
|
|
$this->calendar = $calendar; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return Caldav |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getCalendar() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->calendar; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* perform the sync |
|
60
|
|
|
*/ |
|
61
|
|
|
public function sync() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->syncToTimeSlots(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @return array|\it\thecsea\caldav_client_adapter\EventInterface[] |
|
68
|
|
|
* @throws \it\thecsea\caldav_client_adapter\CaldavException |
|
69
|
|
|
* @thorws \Illuminate\Contracts\Encryption\DecryptException |
|
70
|
|
|
*/ |
|
71
|
|
|
private function getEvents() |
|
72
|
|
|
{ |
|
73
|
|
|
$caldavClient = new SimpleCaldavAdapter(); |
|
74
|
|
|
$caldavClient->connect($this->calendar->url, $this->calendar->username, \Crypt::decrypt($this->calendar->password)); |
|
75
|
|
|
$calendars = $caldavClient->findCalendars(); |
|
76
|
|
|
$caldavClient->setCalendar($calendars[$this->calendar->calendar_name]); |
|
77
|
|
|
/** |
|
78
|
|
|
* 26 hours before to avoid tiemezone problems and dst problems |
|
79
|
|
|
* 30 days after |
|
80
|
|
|
*/ |
|
81
|
|
|
return $caldavClient->getEvents(date('Ymd\THis\Z', time()-93600), date('Ymd\THis\Z', time()+2592000)); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* |
|
86
|
|
|
*/ |
|
87
|
|
|
private function syncToTimeSlots() |
|
88
|
|
|
{ |
|
89
|
|
|
try |
|
90
|
|
|
{ |
|
91
|
|
|
$events = $this->getEvents(); |
|
92
|
|
|
}catch (\it\thecsea\caldav_client_adapter\CaldavException $e) |
|
93
|
|
|
{ |
|
94
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
|
95
|
|
|
return ; |
|
96
|
|
|
}catch(\Illuminate\Contracts\Encryption\DecryptException $e){ |
|
97
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
|
98
|
|
|
return ; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @var $calendarMain \plunner\Calendar |
|
103
|
|
|
*/ |
|
104
|
|
|
$calendarMain = $this->calendar->Calendar; |
|
105
|
|
|
|
|
106
|
|
|
//remove old timeslots |
|
107
|
|
|
$calendarMain->timeslots()->delete(); |
|
108
|
|
|
foreach($events as $event){ |
|
109
|
|
|
if(!($event = $this->parseEvent($event))) |
|
110
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, 'problem during the parsing an event')); |
|
111
|
|
|
$calendarMain->timeslots()->create($event); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param EventInterface $event |
|
117
|
|
|
* @return \DateTime[]|null |
|
118
|
|
|
*/ |
|
119
|
|
|
private function parseEvent(EventInterface $event) |
|
120
|
|
|
{ |
|
121
|
|
|
$pattern = "/^((DTSTART;)|(DTEND;))(.*)\$/m"; |
|
122
|
|
|
if(preg_match_all($pattern, $event->getData(), $matches)){ |
|
123
|
|
|
if(!isset($matches[4]) || count($matches[4]) != 2) |
|
124
|
|
|
return null; |
|
125
|
|
|
$ret = []; |
|
126
|
|
|
if($tmp = $this->parseDate($matches[4][0])) |
|
127
|
|
|
$ret['time_start'] = $tmp; |
|
128
|
|
|
else |
|
129
|
|
|
return null; |
|
130
|
|
|
if($tmp = $this->parseDate($matches[4][1])) |
|
131
|
|
|
$ret['time_end'] = $tmp; |
|
132
|
|
|
else |
|
133
|
|
|
return null; |
|
134
|
|
|
return $ret; |
|
135
|
|
|
} |
|
136
|
|
|
return null; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param String $date |
|
141
|
|
|
* @return \DateTime|null|false |
|
142
|
|
|
*/ |
|
143
|
|
|
private function parseDate($date) |
|
144
|
|
|
{ |
|
145
|
|
|
$pattern = "/^((TZID=)|(VALUE=))(.*):(.*)\$/m"; |
|
146
|
|
|
if(preg_match_all($pattern, $date, $matches)){ |
|
147
|
|
|
if($matches[1][0] == 'TZID=') |
|
148
|
|
|
{ |
|
149
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0], new \DateTimeZone($matches[4][0])); |
|
150
|
|
|
}else if($matches[1][0] == 'VALUE=' && $matches[4][0] == 'DATE') |
|
151
|
|
|
{ |
|
152
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0].'T000000'); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
return null; |
|
156
|
|
|
} |
|
157
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.