3 use Friendica\Util\DateTimeFormat;
5 define("DAV_ACL_READ", "{DAV:}read");
6 define("DAV_ACL_WRITE", "{DAV:}write");
7 define("DAV_DISPLAYNAME", "{DAV:}displayname");
8 define("DAV_CALENDARCOLOR", "{http://apple.com/ns/ical/}calendar-color");
11 class DAVVersionMismatchException extends Exception
16 class vcard_source_data_email
20 function __construct($type, $email)
22 $this->email = $email;
27 class vcard_source_data_homepage
29 public $homepage, $type;
31 function __construct($type, $homepage)
33 $this->homepage = $homepage;
38 class vcard_source_data_telephone
40 public $telephone, $type;
42 function __construct($type, $telephone)
44 $this->telephone = $telephone;
49 class vcard_source_data_socialnetwork
51 public $nick, $type, $url;
53 function __construct($type, $nick, $url)
61 class vcard_source_data_address
63 public $street, $street2, $zip, $city, $country, $type;
66 class vcard_source_data_photo
69 public $width, $height;
73 class vcard_source_data
75 function __construct($name_first, $name_middle, $name_last)
77 $this->name_first = $name_first;
78 $this->name_middle = $name_middle;
79 $this->name_last = $name_last;
82 public $name_first, $name_middle, $name_last;
86 /** @var array|vcard_source_data_telephone[] $telephones */
89 /** @var array|vcard_source_data_homepage[] $homepages */
92 /** @var array|vcard_source_data_socialnetwork[] $socialnetworks */
93 public $socialnetworks;
95 /** @var array|vcard_source_data_email[] $email */
98 /** @var array|vcard_source_data_address[] $addresses */
101 /** @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(DateTimeFormat::MYSQL, $phpDate);
158 * @param string $sqlDate
161 function wdcal_mySql2PhpTime($sqlDate)
163 $ts = DateTime::createFromFormat(DateTimeFormat::MYSQL, $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 $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);
205 * @return Sabre_CardDAV_AddressBookRootFriendica
207 function dav_createRootContactsNode()
209 $backends = array(Sabre_CardDAV_Backend_Std::getInstance());
210 foreach ($GLOBALS["CARDDAV_PRIVATE_SYSTEM_BACKENDS"] as $backendclass) $backends[] = $backendclass::getInstance();
212 return new Sabre_CardDAV_AddressBookRootFriendica(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), $backends);
217 * @param bool $force_authentication
218 * @param bool $needs_caldav
219 * @param bool $needs_carddav
220 * @return Sabre_DAV_Server
222 function dav_create_server($force_authentication = false, $needs_caldav = true, $needs_carddav = true)
225 new Sabre_DAV_SimpleCollection('principals', array(
226 new Sabre_CalDAV_Principal_Collection(Sabre_DAVACL_PrincipalBackend_Std::getInstance(), "principals/users"),
229 if ($needs_caldav) $arr[] = dav_createRootCalendarNode();
230 if ($needs_carddav) $arr[] = dav_createRootContactsNode();
233 $tree = new Sabre_DAV_SimpleCollection('root', $arr);
235 // The object tree needs in turn to be passed to the server class
236 $server = new Sabre_DAV_Server($tree);
238 if (CALDAV_URL_PREFIX != "") $server->setBaseUri(CALDAV_URL_PREFIX);
240 $authPlugin = new Sabre_DAV_Auth_Plugin(Sabre_DAV_Auth_Backend_Std::getInstance(), DAV_APPNAME);
241 $server->addPlugin($authPlugin);
244 $caldavPlugin = new Sabre_CalDAV_Plugin();
245 $server->addPlugin($caldavPlugin);
247 if ($needs_carddav) {
248 $carddavPlugin = new Sabre_CardDAV_Plugin();
249 $server->addPlugin($carddavPlugin);
252 if ($GLOBALS["CALDAV_ACL_PLUGIN_CLASS"] != "") {
253 $aclPlugin = new $GLOBALS["CALDAV_ACL_PLUGIN_CLASS"]();
254 $aclPlugin->defaultUsernamePath = "principals/users";
255 $server->addPlugin($aclPlugin);
257 $aclPlugin = new Sabre_DAVACL_Plugin();
258 $aclPlugin->defaultUsernamePath = "principals/users";
259 $server->addPlugin($aclPlugin);
262 if ($force_authentication) $server->broadcastEvent('beforeMethod', array("GET", "/")); // Make it authenticate
269 * @param Sabre_DAV_Server $server
270 * @param string $with_privilege
271 * @return array|Sabre_CalDAV_Calendar[]
273 function dav_get_current_user_calendars(&$server, $with_privilege = "")
275 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
278 $calendar_path = "/calendars/" . strtolower($a->user["nickname"]) . "/";
280 /** @var Sabre_CalDAV_AnimexxUserCalendars $tree */
281 $tree = $server->tree->getNodeForPath($calendar_path);
282 /** @var array|Sabre_CalDAV_Calendar[] $calendars */
283 $children = $tree->getChildren();
285 $calendars = array();
286 /** @var Sabre_DAVACL_Plugin $aclplugin */
287 $aclplugin = $server->getPlugin("acl");
288 foreach ($children as $child) if (is_a($child, "Sabre_CalDAV_Calendar") || is_subclass_of($child, "Sabre_CalDAV_Calendar")) {
289 if ($with_privilege != "") {
290 $caluri = $calendar_path . $child->getName();
291 if ($aclplugin->checkPrivileges($caluri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) $calendars[] = $child;
293 $calendars[] = $child;
301 * @param Sabre_DAV_Server $server
302 * @param Sabre_CalDAV_Calendar $calendar
303 * @param string $calendarobject_uri
304 * @param string $with_privilege
305 * @return null|Sabre\VObject\Component\VCalendar
307 function dav_get_current_user_calendarobject(&$server, &$calendar, $calendarobject_uri, $with_privilege = "")
309 $obj = $calendar->getChild($calendarobject_uri);
311 if ($with_privilege == "") $with_privilege = DAV_ACL_READ;
314 $uri = "/calendars/" . strtolower($a->user["nickname"]) . "/" . $calendar->getName() . "/" . $calendarobject_uri;
316 /** @var Sabre_DAVACL_Plugin $aclplugin */
317 $aclplugin = $server->getPlugin("acl");
318 if (!$aclplugin->checkPrivileges($uri, $with_privilege, Sabre_DAVACL_Plugin::R_PARENT, false)) return null;
321 $vObject = Sabre\VObject\Reader::read($data);
328 * @param Sabre_DAV_Server $server
330 * @param string $with_privilege
331 * @return null|Sabre_CalDAV_Calendar
333 function dav_get_current_user_calendar_by_id(&$server, $id, $with_privilege = "")
335 $calendars = dav_get_current_user_calendars($server, $with_privilege);
338 foreach ($calendars as $cal) {
339 $prop = $cal->getProperties(array("id"));
340 if (isset($prop["id"]) && $prop["id"] == $id) $calendar = $cal;
349 * @return Sabre\VObject\Component\VCalendar $vObject
351 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:-//" . DAV_APPNAME . "//DAV-Plugin//EN\r\nBEGIN:VEVENT\r\nUID:" . $uid . "@" . dav_compat_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;