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");
10 class DAVVersionMismatchException extends Exception {}
13 class vcard_source_data_email
17 function __construct($type, $email)
19 $this->email = $email;
24 class vcard_source_data_homepage
26 public $homepage, $type;
28 function __construct($type, $homepage)
30 $this->homepage = $homepage;
35 class vcard_source_data_telephone
37 public $telephone, $type;
39 function __construct($type, $telephone)
41 $this->telephone = $telephone;
46 class vcard_source_data_socialnetwork
48 public $nick, $type, $url;
50 function __construct($type, $nick, $url)
58 class vcard_source_data_address
60 public $street, $street2, $zip, $city, $country, $type;
63 class vcard_source_data_photo
66 public $width, $height;
70 class vcard_source_data
72 function __construct($name_first, $name_middle, $name_last)
74 $this->name_first = $name_first;
75 $this->name_middle = $name_middle;
76 $this->name_last = $name_last;
79 public $name_first, $name_middle, $name_last;
83 /** @var array|vcard_source_data_telephone[] $telephones */
86 /** @var array|vcard_source_data_homepage[] $homepages */
89 /** @var array|vcard_source_data_socialnetwork[] $socialnetworks */
90 public $socialnetworks;
92 /** @var array|vcard_source_data_email[] $email */
95 /** @var array|vcard_source_data_address[] $addresses */
98 /** @var vcard_source_data_photo */
106 * @param vcard_source_data $vcardsource
109 function vcard_source_compile($vcardsource)
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";
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";
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";
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";
131 if (isset($vcardsource->socialnetworks) && is_array($vcardsource->socialnetworks)) foreach ($vcardsource->socialnetworks as $netw) switch ($netw->type) {
133 $str .= "X-SOCIALPROFILE;type=dfrn;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
136 $str .= "X-SOCIALPROFILE;type=facebook;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
139 $str .= "X-SOCIALPROFILE;type=twitter;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
143 $str .= "END:VCARD\r\n";
149 * @param int $phpDate (UTC)
150 * @return string (Lokalzeit)
152 function wdcal_php2MySqlTime($phpDate)
154 return date("Y-m-d H:i:s", $phpDate);
158 * @param string $sqlDate
161 function wdcal_mySql2PhpTime($sqlDate)
163 $ts = DateTime::createFromFormat("Y-m-d H:i:s", $sqlDate);
164 return $ts->format("U");
168 * @param string $myqlDate
171 function wdcal_mySql2icalTime($myqlDate)
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];
188 function icalendar_sanitize_string($str = "")
190 return preg_replace("/[\\r\\n]+/siu", "\r\n", $str);
195 * @return Sabre_CalDAV_AnimexxCalendarRootNode
197 function dav_createRootCalendarNode()
199 $caldavBackend_std = Sabre_CalDAV_Backend_Private::getInstance();
200 $caldavBackend_community = Sabre_CalDAV_Backend_Friendica::getInstance();
202 return new Sabre_CalDAV_AnimexxCalendarRootNode(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
204 $caldavBackend_community,
209 * @return Sabre_CardDAV_AddressBookRootFriendica
211 function dav_createRootContactsNode()
213 $carddavBackend_std = Sabre_CardDAV_Backend_Std::getInstance();
214 $carddavBackend_community = Sabre_CardDAV_Backend_FriendicaCommunity::getInstance();
216 return new Sabre_CardDAV_AddressBookRootFriendica(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
218 $carddavBackend_community,
224 * @param bool $force_authentication
225 * @param bool $needs_caldav
226 * @param bool $needs_carddav
227 * @return Sabre_DAV_Server
229 function dav_create_server($force_authentication = false, $needs_caldav = true, $needs_carddav = true)
232 new Sabre_DAV_SimpleCollection('principals', array(
233 new Sabre_CalDAV_Principal_Collection(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), "principals/users"),
236 if ($needs_caldav) $arr[] = dav_createRootCalendarNode();
237 if ($needs_carddav) $arr[] = dav_createRootContactsNode();
240 $tree = new Sabre_DAV_SimpleCollection('root', $arr);
242 // The object tree needs in turn to be passed to the server class
243 $server = new Sabre_DAV_Server($tree);
245 $server->setBaseUri(CALDAV_URL_PREFIX);
247 $authPlugin = new Sabre_DAV_Auth_Plugin(Sabre_DAV_Auth_Backend_Std::getInstance(), 'SabreDAV');
248 $server->addPlugin($authPlugin);
250 $aclPlugin = new Sabre_DAVACL_Plugin_Friendica();
251 $aclPlugin->defaultUsernamePath = "principals/users";
252 $server->addPlugin($aclPlugin);
255 $caldavPlugin = new Sabre_CalDAV_Plugin();
256 $server->addPlugin($caldavPlugin);
258 if ($needs_carddav) {
259 $carddavPlugin = new Sabre_CardDAV_Plugin();
260 $server->addPlugin($carddavPlugin);
263 if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
270 * @param Sabre_DAV_Server $server
271 * @param string $with_privilege
272 * @return array|Sabre_CalDAV_Calendar[]
274 function dav_get_current_user_calendars(&$server, $with_privilege = "")
276 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
279 $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
281 /** @var Sabre_CalDAV_AnimexxUserCalendars $tree */
282 $tree = $server->tree->getNodeForPath($calendar_path);
283 /** @var array|Sabre_CalDAV_Calendar[] $calendars */
284 $children = $tree->getChildren();
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;
294 $calendars[] = $child;
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
308 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
310 $obj = $calendar->getChild($calendarobject_uri);
312 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
315 $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
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;
322 $vObject = Sabre_VObject_Reader::read($data);
329 * @param Sabre_DAV_Server $server
331 * @param string $with_privilege
332 * @return null|Sabre_CalDAV_Calendar
334 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
336 $calendars = dav_get_current_user_calendars($server, $with_privilege);
339 foreach ($calendars as $cal) {
340 $prop = $cal->getProperties(array("id"));
341 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
350 * @return Sabre_VObject_Component_VCalendar $vObject
352 function dav_create_empty_vevent($uid = "")
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");
362 * @param Sabre_VObject_Component_VCalendar $vObject
363 * @return Sabre_VObject_Component_VEvent|null
365 function dav_get_eventComponent(&$vObject)
369 foreach ($vObject->getComponents() as $component) {
370 if ($component->name !== 'VTIMEZONE') {
371 $componentType = $component->name;
375 if ($componentType != "VEVENT") return null;