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
|
|
|
}catch(\Illuminate\Contracts\Encryption\DecryptException $e){ |
96
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, $e->getMessage())); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var $calendarMain \plunner\Calendar |
101
|
|
|
*/ |
102
|
|
|
$calendarMain = $this->calendar->calendar; |
|
|
|
|
103
|
|
|
|
104
|
|
|
//remove old timeslots |
105
|
|
|
$calendarMain->timeslots()->delete(); |
106
|
|
|
foreach($events as $event){ |
107
|
|
|
if(!($event = $this->parseEvent($event))) |
108
|
|
|
\Event::fire(new CaldavErrorEvent($this->calendar, 'problem during the parsing an event')); |
109
|
|
|
$calendarMain->timeslots()->create($event); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param EventInterface $event |
115
|
|
|
* @return \DateTime[]|null |
116
|
|
|
*/ |
117
|
|
|
private function parseEvent(EventInterface $event) |
118
|
|
|
{ |
119
|
|
|
$pattern = "/^((DTSTART;)|(DTEND;))(.*)\$/m"; |
120
|
|
|
if(preg_match_all($pattern, $event->getData(), $matches)){ |
121
|
|
|
if(!isset($matches[4]) || count($matches[4]) != 2) |
122
|
|
|
return null; |
123
|
|
|
$ret = []; |
124
|
|
|
if($tmp = $this->parseDate($matches[4][0])) |
125
|
|
|
$ret['time_start'] = $tmp; |
126
|
|
|
else |
127
|
|
|
return null; |
128
|
|
|
if($tmp = $this->parseDate($matches[4][1])) |
129
|
|
|
$ret['time_end'] = $tmp; |
130
|
|
|
else |
131
|
|
|
return null; |
132
|
|
|
return $ret; |
133
|
|
|
} |
134
|
|
|
return null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param String $date |
139
|
|
|
* @return \DateTime|null|false |
140
|
|
|
*/ |
141
|
|
|
private function parseDate($date) |
142
|
|
|
{ |
143
|
|
|
$pattern = "/^((TZID=)|(VALUE=))(.*):(.*)\$/m"; |
144
|
|
|
if(preg_match_all($pattern, $date, $matches)){ |
145
|
|
|
if($matches[1][0] == 'TZID=') |
146
|
|
|
{ |
147
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0], new \DateTimeZone($matches[4][0])); |
148
|
|
|
}else if($matches[1][0] == 'VALUE=' && $matches[4][0] == 'DATE') |
149
|
|
|
{ |
150
|
|
|
return \DateTime::createFromFormat('Ymd\THis', $matches[5][0].'T000000'); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
return null; |
154
|
|
|
} |
155
|
|
|
} |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.