4 define("DAV_ACL_READ", "{DAV:}read");
5 define("DAV_ACL_WRITE", "{DAV:}write");
6 define("DAV_DISPLAYNAME", "{DAV:}displayname");
9 class vcard_source_data_email
13 function __construct($type, $email)
15 $this->email = $email;
20 class vcard_source_data_homepage
22 public $homepage, $type;
24 function __construct($type, $homepage)
26 $this->homepage = $homepage;
31 class vcard_source_data_telephone
33 public $telephone, $type;
35 function __construct($type, $telephone)
37 $this->telephone = $telephone;
42 class vcard_source_data_socialnetwork
44 public $nick, $type, $url;
46 function __construct($type, $nick, $url)
54 class vcard_source_data_address
56 public $street, $street2, $zip, $city, $country, $type;
59 class vcard_source_data_photo
62 public $width, $height;
66 class vcard_source_data
68 function __construct($name_first, $name_middle, $name_last)
70 $this->name_first = $name_first;
71 $this->name_middle = $name_middle;
72 $this->name_last = $name_last;
75 public $name_first, $name_middle, $name_last;
79 /** @var array|vcard_source_data_telephone[] $telephones */
82 /** @var array|vcard_source_data_homepage[] $homepages */
85 /** @var array|vcard_source_data_socialnetwork[] $socialnetworks */
86 public $socialnetworks;
88 /** @var array|vcard_source_data_email[] $email */
91 /** @var array|vcard_source_data_address[] $addresses */
94 /** @var vcard_source_data_photo */
102 * @param vcard_source_data $vcardsource
105 function vcard_source_compile($vcardsource)
107 $str = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n";
108 $str .= "N:" . str_replace(";", ",", $vcardsource->name_last) . ";" . str_replace(";", ",", $vcardsource->name_first) . ";" . str_replace(";", ",", $vcardsource->name_middle) . ";;\r\n";
109 $str .= "FN:" . str_replace(";", ",", $vcardsource->name_first) . " " . str_replace(";", ",", $vcardsource->name_middle) . " " . str_replace(";", ",", $vcardsource->name_last) . "\r\n";
110 $str .= "REV:" . str_replace(" ", "T", $vcardsource->last_update) . "Z\r\n";
113 for ($i = 0; $i < count($vcardsource->homepages); $i++) {
114 if ($i == 0) $str .= "URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
117 $str .= "item$c.URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
118 $str .= "item$c.X-ABLabel:_\$!<HomePage>!\$_\r\n";
122 if (is_object($vcardsource->photo)) {
123 $data = base64_encode($vcardsource->photo->binarydata);
124 $str .= "PHOTO;ENCODING=BASE64;TYPE=" . $vcardsource->photo->type . ":" . $data . "\r\n";
127 if (isset($vcardsource->socialnetworks) && is_array($vcardsource->socialnetworks)) foreach ($vcardsource->socialnetworks as $netw) switch ($netw->type) {
129 $str .= "X-SOCIALPROFILE;type=dfrn;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
132 $str .= "X-SOCIALPROFILE;type=facebook;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
135 $str .= "X-SOCIALPROFILE;type=twitter;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
139 $str .= "END:VCARD\r\n";
145 * @param int $phpDate (UTC)
146 * @return string (Lokalzeit)
148 function wdcal_php2MySqlTime($phpDate)
150 return date("Y-m-d H:i:s", $phpDate);
154 * @param string $sqlDate
157 function wdcal_mySql2PhpTime($sqlDate)
159 $ts = DateTime::createFromFormat("Y-m-d H:i:s", $sqlDate);
160 return $ts->format("U");
164 * @param string $myqlDate
167 function wdcal_mySql2icalTime($myqlDate)
169 $x = explode(" ", $myqlDate);
170 $y = explode("-", $x[0]);
171 $ret = array("year"=> $y[0], "month"=> $y[1], "day"=> $y[2]);
172 $y = explode(":", $x[1]);
173 $ret["hour"] = $y[0];
174 $ret["minute"] = $y[1];
175 $ret["second"] = $y[2];
184 function icalendar_sanitize_string($str = "")
186 $str = str_replace("\r\n", "\n", $str);
187 $str = str_replace("\n\r", "\n", $str);
188 $str = str_replace("\r", "\n", $str);
189 $str = str_replace("\n\n", "\n", $str);
190 $str = str_replace("\n\n", "\n", $str);
196 * @return Sabre_CalDAV_AnimexxCalendarRootNode
198 function dav_createRootCalendarNode()
200 $caldavBackend_std = Sabre_CalDAV_Backend_Private::getInstance();
201 $caldavBackend_community = Sabre_CalDAV_Backend_Friendica::getInstance();
203 return new Sabre_CalDAV_AnimexxCalendarRootNode(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
205 $caldavBackend_community,
210 * @return Sabre_CardDAV_AddressBookRootFriendica
212 function dav_createRootContactsNode()
214 $carddavBackend_std = Sabre_CardDAV_Backend_Std::getInstance();
215 $carddavBackend_community = Sabre_CardDAV_Backend_FriendicaCommunity::getInstance();
217 return new Sabre_CardDAV_AddressBookRootFriendica(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), array(
219 $carddavBackend_community,
225 * @param bool $force_authentication
226 * @param bool $needs_caldav
227 * @param bool $needs_carddav
228 * @return Sabre_DAV_Server
230 function dav_create_server($force_authentication = false, $needs_caldav = true, $needs_carddav = true)
233 new Sabre_DAV_SimpleCollection('principals', array(
234 new Sabre_CalDAV_Principal_Collection(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), "principals/users"),
237 if ($needs_caldav) $arr[] = dav_createRootCalendarNode();
238 if ($needs_carddav) $arr[] = dav_createRootContactsNode();
241 $tree = new Sabre_DAV_SimpleCollection('root', $arr);
243 // The object tree needs in turn to be passed to the server class
244 $server = new Sabre_DAV_Server($tree);
246 $server->setBaseUri(CALDAV_URL_PREFIX);
248 $authPlugin = new Sabre_DAV_Auth_Plugin(Sabre_DAV_Auth_Backend_Std::getInstance(), 'SabreDAV');
249 $server->addPlugin($authPlugin);
251 $aclPlugin = new Sabre_DAVACL_Plugin_Friendica();
252 $aclPlugin->defaultUsernamePath = "principals/users";
253 $server->addPlugin($aclPlugin);
256 $caldavPlugin = new Sabre_CalDAV_Plugin();
257 $server->addPlugin($caldavPlugin);
259 if ($needs_carddav) {
260 $carddavPlugin = new Sabre_CardDAV_Plugin();
261 $server->addPlugin($carddavPlugin);
264 if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
271 * @param Sabre_DAV_Server $server
272 * @param string $with_privilege
273 * @return array|Sabre_CalDAV_Calendar[]
275 function dav_get_current_user_calendars(&$server, $with_privilege = "")
277 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
280 $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
282 /** @var Sabre_CalDAV_AnimexxUserCalendars $tree */
283 $tree = $server->tree->getNodeForPath($calendar_path);
284 /** @var array|Sabre_CalDAV_Calendar[] $calendars */
285 $children = $tree->getChildren();
287 $calendars = array();
288 /** @var Sabre_DAVACL_Plugin $aclplugin */
289 $aclplugin = $server->getPlugin("acl");
290 foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar")) {
291 if ($with_privilege != "") {
292 $caluri = $calendar_path . $child->getName();
293 if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child;
295 $calendars[] = $child;
303 * @param Sabre_DAV_Server $server
304 * @param Sabre_CalDAV_Calendar $calendar
305 * @param string $calendarobject_uri
306 * @param string $with_privilege
307 * @return null|Sabre_VObject_Component_VEvent
309 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
311 $obj = $calendar->getChild($calendarobject_uri);
313 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
316 $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
318 /** @var Sabre_DAVACL_Plugin $aclplugin */
319 $aclplugin = $server->getPlugin("acl");
320 if (!$aclplugin->checkPrivileges($uri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) return null;
323 $vObject = Sabre_VObject_Reader::read($data);
330 * @param Sabre_DAV_Server $server
332 * @param string $with_privilege
333 * @return null|Sabre_CalDAV_Calendar
335 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
337 $calendars = dav_get_current_user_calendars($server, $with_privilege);
340 foreach ($calendars as $cal) {
341 $prop = $cal->getProperties(array("id"));
342 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
351 * @return Sabre_VObject_Component_VEvent $vObject
353 function dav_create_empty_vevent($uid = "")
356 if ($uid == "") $uid = uniqid();
357 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() .
358 "\r\nDTSTAMP:" . date("Ymd") . "T" . date("His") . "Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n");
365 * @param Sabre_VObject_Component_VEvent $vObject
366 * @return Sabre_VObject_Component_VEvent|null
368 function dav_get_eventComponent(&$vObject)
372 foreach ($vObject->getComponents() as $component) {
373 if ($component->name !== 'VTIMEZONE') {
374 $componentType = $component->name;
378 if ($componentType != "VEVENT") return null;