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");
11 class vcard_source_data_email
15 function __construct($type, $email)
17 $this->email = $email;
22 class vcard_source_data_homepage
24 public $homepage, $type;
26 function __construct($type, $homepage)
28 $this->homepage = $homepage;
33 class vcard_source_data_telephone
35 public $telephone, $type;
37 function __construct($type, $telephone)
39 $this->telephone = $telephone;
44 class vcard_source_data_socialnetwork
46 public $nick, $type, $url;
48 function __construct($type, $nick, $url)
56 class vcard_source_data_address
58 public $street, $street2, $zip, $city, $country, $type;
61 class vcard_source_data_photo
64 public $width, $height;
68 class vcard_source_data
70 function __construct($name_first, $name_middle, $name_last)
72 $this->name_first = $name_first;
73 $this->name_middle = $name_middle;
74 $this->name_last = $name_last;
77 public $name_first, $name_middle, $name_last;
81 /** @var array|vcard_source_data_telephone[] $telephones */
84 /** @var array|vcard_source_data_homepage[] $homepages */
87 /** @var array|vcard_source_data_socialnetwork[] $socialnetworks */
88 public $socialnetworks;
90 /** @var array|vcard_source_data_email[] $email */
93 /** @var array|vcard_source_data_address[] $addresses */
96 /** @var vcard_source_data_photo */
104 * @param vcard_source_data $vcardsource
107 function vcard_source_compile($vcardsource)
109 $str = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n";
110 $str .= "N:" . str_replace(";", ",", $vcardsource->name_last) . ";" . str_replace(";", ",", $vcardsource->name_first) . ";" . str_replace(";", ",", $vcardsource->name_middle) . ";;\r\n";
111 $str .= "FN:" . str_replace(";", ",", $vcardsource->name_first) . " " . str_replace(";", ",", $vcardsource->name_middle) . " " . str_replace(";", ",", $vcardsource->name_last) . "\r\n";
112 $str .= "REV:" . str_replace(" ", "T", $vcardsource->last_update) . "Z\r\n";
115 for ($i = 0; $i < count($vcardsource->homepages); $i++) {
116 if ($i == 0) $str .= "URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
119 $str .= "item$c.URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
120 $str .= "item$c.X-ABLabel:_\$!<HomePage>!\$_\r\n";
124 if (is_object($vcardsource->photo)) {
125 $data = base64_encode($vcardsource->photo->binarydata);
126 $str .= "PHOTO;ENCODING=BASE64;TYPE=" . $vcardsource->photo->type . ":" . $data . "\r\n";
129 if (isset($vcardsource->socialnetworks) && is_array($vcardsource->socialnetworks)) foreach ($vcardsource->socialnetworks as $netw) switch ($netw->type) {
131 $str .= "X-SOCIALPROFILE;type=dfrn;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
134 $str .= "X-SOCIALPROFILE;type=facebook;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
137 $str .= "X-SOCIALPROFILE;type=twitter;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
141 $str .= "END:VCARD\r\n";
147 * @param int $phpDate (UTC)
148 * @return string (Lokalzeit)
150 function wdcal_php2MySqlTime($phpDate)
152 return date("Y-m-d H:i:s", $phpDate);
156 * @param string $sqlDate
159 function wdcal_mySql2PhpTime($sqlDate)
161 $ts = DateTime::createFromFormat("Y-m-d H:i:s", $sqlDate);
162 return $ts->format("U");
166 * @param string $myqlDate
169 function wdcal_mySql2icalTime($myqlDate)
171 $x = explode(" ", $myqlDate);
172 $y = explode("-", $x[0]);
173 $ret = array("year"=> $y[0], "month"=> $y[1], "day"=> $y[2]);
174 $y = explode(":", $x[1]);
175 $ret["hour"] = $y[0];
176 $ret["minute"] = $y[1];
177 $ret["second"] = $y[2];
186 function icalendar_sanitize_string($str = "")
188 return preg_replace("/[\\r\\n]+/siu", "\r\n", $str);
193 * @return Sabre_CalDAV_AnimexxCalendarRootNode
195 function dav_createRootCalendarNode()
197 $caldavBackend_std = Sabre_CalDAV_Backend_Private::getInstance();
198 $caldavBackend_community = Sabre_CalDAV_Backend_Friendica::getInstance();
200 return new Sabre_CalDAV_AnimexxCalendarRootNode(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
202 $caldavBackend_community,
207 * @return Sabre_CardDAV_AddressBookRootFriendica
209 function dav_createRootContactsNode()
211 $carddavBackend_std = Sabre_CardDAV_Backend_Std::getInstance();
212 $carddavBackend_community = Sabre_CardDAV_Backend_FriendicaCommunity::getInstance();
214 return new Sabre_CardDAV_AddressBookRootFriendica(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
216 $carddavBackend_community,
222 * @param bool $force_authentication
223 * @param bool $needs_caldav
224 * @param bool $needs_carddav
225 * @return Sabre_DAV_Server
227 function dav_create_server($force_authentication = false, $needs_caldav = true, $needs_carddav = true)
230 new Sabre_DAV_SimpleCollection('principals', array(
231 new Sabre_CalDAV_Principal_Collection(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), "principals/users"),
234 if ($needs_caldav) $arr[] = dav_createRootCalendarNode();
235 if ($needs_carddav) $arr[] = dav_createRootContactsNode();
238 $tree = new Sabre_DAV_SimpleCollection('root', $arr);
240 // The object tree needs in turn to be passed to the server class
241 $server = new Sabre_DAV_Server($tree);
243 $server->setBaseUri(CALDAV_URL_PREFIX);
245 $authPlugin = new Sabre_DAV_Auth_Plugin(Sabre_DAV_Auth_Backend_Std::getInstance(), 'SabreDAV');
246 $server->addPlugin($authPlugin);
248 $aclPlugin = new Sabre_DAVACL_Plugin_Friendica();
249 $aclPlugin->defaultUsernamePath = "principals/users";
250 $server->addPlugin($aclPlugin);
253 $caldavPlugin = new Sabre_CalDAV_Plugin();
254 $server->addPlugin($caldavPlugin);
256 if ($needs_carddav) {
257 $carddavPlugin = new Sabre_CardDAV_Plugin();
258 $server->addPlugin($carddavPlugin);
261 if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
268 * @param Sabre_DAV_Server $server
269 * @param string $with_privilege
270 * @return array|Sabre_CalDAV_Calendar[]
272 function dav_get_current_user_calendars(&$server, $with_privilege = "")
274 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
277 $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
279 /** @var Sabre_CalDAV_AnimexxUserCalendars $tree */
280 $tree = $server->tree->getNodeForPath($calendar_path);
281 /** @var array|Sabre_CalDAV_Calendar[] $calendars */
282 $children = $tree->getChildren();
284 $calendars = array();
285 /** @var Sabre_DAVACL_Plugin $aclplugin */
286 $aclplugin = $server->getPlugin("acl");
287 foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar")) {
288 if ($with_privilege != "") {
289 $caluri = $calendar_path . $child->getName();
290 if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child;
292 $calendars[] = $child;
300 * @param Sabre_DAV_Server $server
301 * @param Sabre_CalDAV_Calendar $calendar
302 * @param string $calendarobject_uri
303 * @param string $with_privilege
304 * @return null|Sabre_VObject_Component_VEvent
306 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
308 $obj = $calendar->getChild($calendarobject_uri);
310 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
313 $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
315 /** @var Sabre_DAVACL_Plugin $aclplugin */
316 $aclplugin = $server->getPlugin("acl");
317 if (!$aclplugin->checkPrivileges($uri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) return null;
320 $vObject = Sabre_VObject_Reader::read($data);
327 * @param Sabre_DAV_Server $server
329 * @param string $with_privilege
330 * @return null|Sabre_CalDAV_Calendar
332 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
334 $calendars = dav_get_current_user_calendars($server, $with_privilege);
337 foreach ($calendars as $cal) {
338 $prop = $cal->getProperties(array("id"));
339 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
348 * @return Sabre_VObject_Component_VEvent $vObject
350 function dav_create_empty_vevent($uid = "")
353 if ($uid == "") $uid = uniqid();
354 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() .
355 "\r\nDTSTAMP:" . date("Ymd") . "T" . date("His") . "Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
360 * @param Sabre_VObject_Component_VCalendar $vObject
361 * @return Sabre_VObject_Component_VEvent|null
363 function dav_get_eventComponent(&$vObject)
367 foreach ($vObject->getComponents() as $component) {
368 if ($component->name !== 'VTIMEZONE') {
369 $componentType = $component->name;
373 if ($componentType != "VEVENT") return null;