]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/calendar.fnk.php
Refactoring to make common/ more independent of the framework
[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         $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 ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
253
254         return $server;
255 }
256
257
258 /**
259  * @param Sabre_DAV_Server $server
260  * @param string $with_privilege
261  * @return array|Sabre_CalDAV_Calendar[]
262  */
263 function dav_get_current_user_calendars(&$server, $with_privilege = "")
264 {
265         if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
266
267         $a             = get_app();
268         $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
269
270         /** @var Sabre_CalDAV_AnimexxUserCalendars $tree  */
271         $tree = $server->tree->getNodeForPath($calendar_path);
272         /** @var array|Sabre_CalDAV_Calendar[] $calendars  */
273         $children = $tree->getChildren();
274
275         $calendars = array();
276         /** @var Sabre_DAVACL_Plugin $aclplugin  */
277         $aclplugin = $server->getPlugin("acl");
278         foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar")) {
279                 if ($with_privilege != "") {
280                         $caluri = $calendar_path . $child->getName();
281                         if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child;
282                 } else {
283                         $calendars[] = $child;
284                 }
285         }
286         return $calendars;
287 }
288
289
290 /**
291  * @param Sabre_DAV_Server $server
292  * @param Sabre_CalDAV_Calendar $calendar
293  * @param string $calendarobject_uri
294  * @param string $with_privilege
295  * @return null|Sabre_VObject_Component_VCalendar
296  */
297 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
298 {
299         $obj = $calendar->getChild($calendarobject_uri);
300
301         if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
302
303         $a   = get_app();
304         $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
305
306         /** @var Sabre_DAVACL_Plugin $aclplugin  */
307         $aclplugin = $server->getPlugin("acl");
308         if (!$aclplugin->checkPrivileges($uri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) return null;
309
310         $data    = $obj->get();
311         $vObject = Sabre_VObject_Reader::read($data);
312
313         return $vObject;
314 }
315
316
317 /**
318  * @param Sabre_DAV_Server $server
319  * @param int $id
320  * @param string $with_privilege
321  * @return null|Sabre_CalDAV_Calendar
322  */
323 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
324 {
325         $calendars = dav_get_current_user_calendars($server, $with_privilege);
326
327         $calendar = null;
328         foreach ($calendars as $cal) {
329                 $prop = $cal->getProperties(array("id"));
330                 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
331         }
332
333         return $calendar;
334 }
335
336
337 /**
338  * @param string $uid
339  * @return Sabre_VObject_Component_VCalendar $vObject
340  */
341 function dav_create_empty_vevent($uid = "")
342 {
343         $a = get_app();
344         if ($uid == "") $uid = uniqid();
345         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() .
346                 "\r\nDTSTAMP:" . date("Ymd") . "T" . date("His") . "Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
347 }
348
349
350 /**
351  * @param Sabre_VObject_Component_VCalendar $vObject
352  * @return Sabre_VObject_Component_VEvent|null
353  */
354 function dav_get_eventComponent(&$vObject)
355 {
356         $component     = null;
357         $componentType = "";
358         foreach ($vObject->getComponents() as $component) {
359                 if ($component->name !== 'VTIMEZONE') {
360                         $componentType = $component->name;
361                         break;
362                 }
363         }
364         if ($componentType != "VEVENT") return null;
365
366         return $component;
367 }