]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/calendar.fnk.php
1bfe97e2fd20b09579fd19f1434b48b862bd20f3
[friendica-addons.git] / dav / common / calendar.fnk.php
1 <?php
2
3
4 define("DAV_ACL_READ", "{DAV:}read");
5 define("DAV_ACL_WRITE", "{DAV:}write");
6 define("DAV_DISPLAYNAME", "{DAV:}displayname");
7 define("DAV_CALENDARCOLOR", "{http://apple.com/ns/ical/}calendar-color");
8
9
10 class DAVVersionMismatchException extends Exception {}
11
12
13 class vcard_source_data_email
14 {
15         public $email, $type;
16
17         function __construct($type, $email)
18         {
19                 $this->email = $email;
20                 $this->type  = $type;
21         }
22 }
23
24 class vcard_source_data_homepage
25 {
26         public $homepage, $type;
27
28         function __construct($type, $homepage)
29         {
30                 $this->homepage = $homepage;
31                 $this->type     = $type;
32         }
33 }
34
35 class vcard_source_data_telephone
36 {
37         public $telephone, $type;
38
39         function __construct($type, $telephone)
40         {
41                 $this->telephone = $telephone;
42                 $this->type      = $type;
43         }
44 }
45
46 class vcard_source_data_socialnetwork
47 {
48         public $nick, $type, $url;
49
50         function __construct($type, $nick, $url)
51         {
52                 $this->nick = $nick;
53                 $this->type = $type;
54                 $this->url  = $url;
55         }
56 }
57
58 class vcard_source_data_address
59 {
60         public $street, $street2, $zip, $city, $country, $type;
61 }
62
63 class vcard_source_data_photo
64 {
65         public $binarydata;
66         public $width, $height;
67         public $type;
68 }
69
70 class vcard_source_data
71 {
72         function __construct($name_first, $name_middle, $name_last)
73         {
74                 $this->name_first  = $name_first;
75                 $this->name_middle = $name_middle;
76                 $this->name_last   = $name_last;
77         }
78
79         public $name_first, $name_middle, $name_last;
80         public $last_update;
81         public $picture_data;
82
83         /** @var array|vcard_source_data_telephone[] $telephones */
84         public $telephones;
85
86         /** @var array|vcard_source_data_homepage[] $homepages */
87         public $homepages;
88
89         /** @var array|vcard_source_data_socialnetwork[] $socialnetworks */
90         public $socialnetworks;
91
92         /** @var array|vcard_source_data_email[] $email */
93         public $emails;
94
95         /** @var array|vcard_source_data_address[] $addresses */
96         public $addresses;
97
98         /** @var vcard_source_data_photo */
99         public $photo;
100 }
101
102 ;
103
104
105 /**
106  * @param vcard_source_data $vcardsource
107  * @return string
108  */
109 function vcard_source_compile($vcardsource)
110 {
111         $str = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n";
112         $str .= "N:" . str_replace(";", ",", $vcardsource->name_last) . ";" . str_replace(";", ",", $vcardsource->name_first) . ";" . str_replace(";", ",", $vcardsource->name_middle) . ";;\r\n";
113         $str .= "FN:" . str_replace(";", ",", $vcardsource->name_first) . " " . str_replace(";", ",", $vcardsource->name_middle) . " " . str_replace(";", ",", $vcardsource->name_last) . "\r\n";
114         $str .= "REV:" . str_replace(" ", "T", $vcardsource->last_update) . "Z\r\n";
115
116         $item_count = 0;
117         for ($i = 0; $i < count($vcardsource->homepages); $i++) {
118                 if ($i == 0) $str .= "URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
119                 else {
120                         $c = ++$item_count;
121                         $str .= "item$c.URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
122                         $str .= "item$c.X-ABLabel:_\$!<HomePage>!\$_\r\n";
123                 }
124         }
125
126         if (is_object($vcardsource->photo)) {
127                 $data = base64_encode($vcardsource->photo->binarydata);
128                 $str .= "PHOTO;ENCODING=BASE64;TYPE=" . $vcardsource->photo->type . ":" . $data . "\r\n";
129         }
130
131         if (isset($vcardsource->socialnetworks) && is_array($vcardsource->socialnetworks)) foreach ($vcardsource->socialnetworks as $netw) switch ($netw->type) {
132                 case "dfrn":
133                         $str .= "X-SOCIALPROFILE;type=dfrn;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
134                         break;
135                 case "facebook":
136                         $str .= "X-SOCIALPROFILE;type=facebook;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
137                         break;
138                 case "twitter":
139                         $str .= "X-SOCIALPROFILE;type=twitter;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
140                         break;
141         }
142
143         $str .= "END:VCARD\r\n";
144         return $str;
145 }
146
147
148 /**
149  * @param int $phpDate (UTC)
150  * @return string (Lokalzeit)
151  */
152 function wdcal_php2MySqlTime($phpDate)
153 {
154         return date("Y-m-d H:i:s", $phpDate);
155 }
156
157 /**
158  * @param string $sqlDate
159  * @return int
160  */
161 function wdcal_mySql2PhpTime($sqlDate)
162 {
163         $ts = DateTime::createFromFormat("Y-m-d H:i:s", $sqlDate);
164         return $ts->format("U");
165 }
166
167 /**
168  * @param string $myqlDate
169  * @return array
170  */
171 function wdcal_mySql2icalTime($myqlDate)
172 {
173         $x             = explode(" ", $myqlDate);
174         $y             = explode("-", $x[0]);
175         $ret           = array("year"=> $y[0], "month"=> $y[1], "day"=> $y[2]);
176         $y             = explode(":", $x[1]);
177         $ret["hour"]   = $y[0];
178         $ret["minute"] = $y[1];
179         $ret["second"] = $y[2];
180         return $ret;
181 }
182
183
184 /**
185  * @param string $str
186  * @return string
187  */
188 function icalendar_sanitize_string($str = "")
189 {
190         return preg_replace("/[\\r\\n]+/siu", "\r\n", $str);
191 }
192
193
194 /**
195  * @return Sabre_CalDAV_AnimexxCalendarRootNode
196  */
197 function dav_createRootCalendarNode()
198 {
199         $caldavBackend_std       = Sabre_CalDAV_Backend_Private::getInstance();
200         $caldavBackend_community = Sabre_CalDAV_Backend_Friendica::getInstance();
201
202         return new Sabre_CalDAV_AnimexxCalendarRootNode(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
203                 $caldavBackend_std,
204                 $caldavBackend_community,
205         ));
206 }
207
208 /**
209  * @return Sabre_CardDAV_AddressBookRootFriendica
210  */
211 function dav_createRootContactsNode()
212 {
213         $carddavBackend_std       = Sabre_CardDAV_Backend_Std::getInstance();
214         $carddavBackend_community = Sabre_CardDAV_Backend_FriendicaCommunity::getInstance();
215
216         return new Sabre_CardDAV_AddressBookRootFriendica(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
217                 $carddavBackend_std,
218                 $carddavBackend_community,
219         ));
220 }
221
222
223 /**
224  * @param bool $force_authentication
225  * @param bool $needs_caldav
226  * @param bool $needs_carddav
227  * @return Sabre_DAV_Server
228  */
229 function dav_create_server($force_authentication = false, $needs_caldav = true, $needs_carddav = true)
230 {
231         $arr = array(
232                 new Sabre_DAV_SimpleCollection('principals', array(
233                         new Sabre_CalDAV_Principal_Collection(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), "principals/users"),
234                 )),
235         );
236         if ($needs_caldav) $arr[] = dav_createRootCalendarNode();
237         if ($needs_carddav) $arr[] = dav_createRootContactsNode();
238
239
240         $tree = new Sabre_DAV_SimpleCollection('root', $arr);
241
242 // The object tree needs in turn to be passed to the server class
243         $server = new Sabre_DAV_Server($tree);
244
245         $server->setBaseUri(CALDAV_URL_PREFIX);
246
247         $authPlugin = new Sabre_DAV_Auth_Plugin(Sabre_DAV_Auth_Backend_Std::getInstance(), 'SabreDAV');
248         $server->addPlugin($authPlugin);
249
250         $aclPlugin                      = new Sabre_DAVACL_Plugin_Friendica();
251         $aclPlugin->defaultUsernamePath = "principals/users";
252         $server->addPlugin($aclPlugin);
253
254         if ($needs_caldav) {
255                 $caldavPlugin = new Sabre_CalDAV_Plugin();
256                 $server->addPlugin($caldavPlugin);
257         }
258         if ($needs_carddav) {
259                 $carddavPlugin = new Sabre_CardDAV_Plugin();
260                 $server->addPlugin($carddavPlugin);
261         }
262
263         if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
264
265         return $server;
266 }
267
268
269 /**
270  * @param Sabre_DAV_Server $server
271  * @param string $with_privilege
272  * @return array|Sabre_CalDAV_Calendar[]
273  */
274 function dav_get_current_user_calendars(&$server, $with_privilege = "")
275 {
276         if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
277
278         $a             = get_app();
279         $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
280
281         /** @var Sabre_CalDAV_AnimexxUserCalendars $tree  */
282         $tree = $server->tree->getNodeForPath($calendar_path);
283         /** @var array|Sabre_CalDAV_Calendar[] $calendars  */
284         $children = $tree->getChildren();
285
286         $calendars = array();
287         /** @var Sabre_DAVACL_Plugin $aclplugin  */
288         $aclplugin = $server->getPlugin("acl");
289         foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar")) {
290                 if ($with_privilege != "") {
291                         $caluri = $calendar_path . $child->getName();
292                         if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child;
293                 } else {
294                         $calendars[] = $child;
295                 }
296         }
297         return $calendars;
298 }
299
300
301 /**
302  * @param Sabre_DAV_Server $server
303  * @param Sabre_CalDAV_Calendar $calendar
304  * @param string $calendarobject_uri
305  * @param string $with_privilege
306  * @return null|Sabre_VObject_Component_VCalendar
307  */
308 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
309 {
310         $obj = $calendar->getChild($calendarobject_uri);
311
312         if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
313
314         $a   = get_app();
315         $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
316
317         /** @var Sabre_DAVACL_Plugin $aclplugin  */
318         $aclplugin = $server->getPlugin("acl");
319         if (!$aclplugin->checkPrivileges($uri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) return null;
320
321         $data    = $obj->get();
322         $vObject = Sabre_VObject_Reader::read($data);
323
324         return $vObject;
325 }
326
327
328 /**
329  * @param Sabre_DAV_Server $server
330  * @param int $id
331  * @param string $with_privilege
332  * @return null|Sabre_CalDAV_Calendar
333  */
334 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
335 {
336         $calendars = dav_get_current_user_calendars($server, $with_privilege);
337
338         $calendar = null;
339         foreach ($calendars as $cal) {
340                 $prop = $cal->getProperties(array("id"));
341                 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
342         }
343
344         return $calendar;
345 }
346
347
348 /**
349  * @param string $uid
350  * @return Sabre_VObject_Component_VCalendar $vObject
351  */
352 function dav_create_empty_vevent($uid = "")
353 {
354         $a = get_app();
355         if ($uid == "") $uid = uniqid();
356         return Sabre_VObject_Reader::read("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\nBEGIN:VEVENT\r\nUID:" . $uid . "@" . $a->get_hostname() .
357                 "\r\nDTSTAMP:" . date("Ymd") . "T" . date("His") . "Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
358 }
359
360
361 /**
362  * @param Sabre_VObject_Component_VCalendar $vObject
363  * @return Sabre_VObject_Component_VEvent|null
364  */
365 function dav_get_eventComponent(&$vObject)
366 {
367         $component     = null;
368         $componentType = "";
369         foreach ($vObject->getComponents() as $component) {
370                 if ($component->name !== 'VTIMEZONE') {
371                         $componentType = $component->name;
372                         break;
373                 }
374         }
375         if ($componentType != "VEVENT") return null;
376
377         return $component;
378 }