]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/calendar.fnk.php
cd5efa57f3c5664f7867a34a548e2fb45fbe3a33
[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:-//" . DAV_APPNAME . "//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         $backends = array(Sabre_CalDAV_Backend_Private::getInstance());
200         foreach ($GLOBALS["CALDAV_PRIVATE_SYSTEM_BACKENDS"] as $backendclass) $backends[] = $backendclass::getInstance();
201         return new Sabre_CalDAV_AnimexxCalendarRootNode(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), $backends);
202 }
203
204 /**
205  * @return Sabre_CardDAV_AddressBookRootFriendica
206  */
207 function dav_createRootContactsNode()
208 {
209         $backends = array(Sabre_CardDAV_Backend_Std::getInstance());
210         foreach ($GLOBALS["CARDDAV_PRIVATE_SYSTEM_BACKENDS"] as $backendclass) $backends[] = $backendclass::getInstance();
211
212         return new Sabre_CardDAV_AddressBookRootFriendica(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), $backends);
213 }
214
215
216 /**
217  * @param bool $force_authentication
218  * @param bool $needs_caldav
219  * @param bool $needs_carddav
220  * @return Sabre_DAV_Server
221  */
222 function dav_create_server($force_authentication = false, $needs_caldav = true, $needs_carddav = true)
223 {
224         $arr = array(
225                 new Sabre_DAV_SimpleCollection('principals', array(
226                         new Sabre_CalDAV_Principal_Collection(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), "principals/users"),
227                 )),
228         );
229         if ($needs_caldav) $arr[] = dav_createRootCalendarNode();
230         if ($needs_carddav) $arr[] = dav_createRootContactsNode();
231
232
233         $tree = new Sabre_DAV_SimpleCollection('root', $arr);
234
235 // The object tree needs in turn to be passed to the server class
236         $server = new Sabre_DAV_Server($tree);
237
238         if (CALDAV_URL_PREFIX != "") $server->setBaseUri(CALDAV_URL_PREFIX);
239
240         $authPlugin = new Sabre_DAV_Auth_Plugin(Sabre_DAV_Auth_Backend_Std::getInstance(), 'SabreDAV');
241         $server->addPlugin($authPlugin);
242
243         if ($needs_caldav) {
244                 $caldavPlugin = new Sabre_CalDAV_Plugin();
245                 $server->addPlugin($caldavPlugin);
246         }
247         if ($needs_carddav) {
248                 $carddavPlugin = new Sabre_CardDAV_Plugin();
249                 $server->addPlugin($carddavPlugin);
250         }
251
252         if ($GLOBALS["CALDAV_ACL_PLUGIN_CLASS"] != "") {
253                 $aclPlugin                      = new $GLOBALS["CALDAV_ACL_PLUGIN_CLASS"]();
254                 $aclPlugin->defaultUsernamePath = "principals/users";
255                 $server->addPlugin($aclPlugin);
256         }
257
258         if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
259
260         return $server;
261 }
262
263
264 /**
265  * @param Sabre_DAV_Server $server
266  * @param string $with_privilege
267  * @return array|Sabre_CalDAV_Calendar[]
268  */
269 function dav_get_current_user_calendars(&$server, $with_privilege = "")
270 {
271         if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
272
273         $a             = get_app();
274         $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
275
276         /** @var Sabre_CalDAV_AnimexxUserCalendars $tree  */
277         $tree = $server->tree->getNodeForPath($calendar_path);
278         /** @var array|Sabre_CalDAV_Calendar[] $calendars  */
279         $children = $tree->getChildren();
280
281         $calendars = array();
282         /** @var Sabre_DAVACL_Plugin $aclplugin  */
283         $aclplugin = $server->getPlugin("acl");
284         foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar") || is_subclass_of($child, "Sabre_CalDAV_Calendar")) {
285                 if ($with_privilege != "") {
286                         $caluri = $calendar_path . $child->getName();
287                         if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child;
288                 } else {
289                         $calendars[] = $child;
290                 }
291         }
292         return $calendars;
293 }
294
295
296 /**
297  * @param Sabre_DAV_Server $server
298  * @param Sabre_CalDAV_Calendar $calendar
299  * @param string $calendarobject_uri
300  * @param string $with_privilege
301  * @return null|Sabre_VObject_Component_VCalendar
302  */
303 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
304 {
305         $obj = $calendar->getChild($calendarobject_uri);
306
307         if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
308
309         $a   = get_app();
310         $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
311
312         /** @var Sabre_DAVACL_Plugin $aclplugin  */
313         $aclplugin = $server->getPlugin("acl");
314         if (!$aclplugin->checkPrivileges($uri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) return null;
315
316         $data    = $obj->get();
317         $vObject = Sabre_VObject_Reader::read($data);
318
319         return $vObject;
320 }
321
322
323 /**
324  * @param Sabre_DAV_Server $server
325  * @param int $id
326  * @param string $with_privilege
327  * @return null|Sabre_CalDAV_Calendar
328  */
329 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
330 {
331         $calendars = dav_get_current_user_calendars($server, $with_privilege);
332
333         $calendar = null;
334         foreach ($calendars as $cal) {
335                 $prop = $cal->getProperties(array("id"));
336                 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
337         }
338
339         return $calendar;
340 }
341
342
343 /**
344  * @param string $uid
345  * @return Sabre_VObject_Component_VCalendar $vObject
346  */
347 function dav_create_empty_vevent($uid = "")
348 {
349         $a = get_app();
350         if ($uid == "") $uid = uniqid();
351         return Sabre_VObject_Reader::read("BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//" . DAV_APPNAME . "//DAV-Plugin//EN\r\nBEGIN:VEVENT\r\nUID:" . $uid . "@" . dav_compat_get_hostname() .
352                 "\r\nDTSTAMP:" . date("Ymd") . "T" . date("His") . "Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
353 }
354
355
356 /**
357  * @param Sabre_VObject_Component_VCalendar $vObject
358  * @return Sabre_VObject_Component_VEvent|null
359  */
360 function dav_get_eventComponent(&$vObject)
361 {
362         $component     = null;
363         $componentType = "";
364         foreach ($vObject->getComponents() as $component) {
365                 if ($component->name !== 'VTIMEZONE') {
366                         $componentType = $component->name;
367                         break;
368                 }
369         }
370         if ($componentType != "VEVENT") return null;
371
372         return $component;
373 }