4 $uri = parse_url($a->get_baseurl());
6 if (isset($uri["path"]) && strlen($uri["path"]) > 1) {
7 $path = $uri["path"] . "/";
10 define("CALDAV_SQL_DB", "");
11 define("CALDAV_SQL_PREFIX", "dav_");
12 define("CALDAV_URL_PREFIX", $path . "dav/");
14 define("CALDAV_NAMESPACE_PRIVATE", 1);
16 define("CALDAV_FRIENDICA_MINE", "friendica-mine");
17 define("CALDAV_FRIENDICA_CONTACTS", "friendica-contacts");
19 $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"] = array(CALDAV_FRIENDICA_MINE, CALDAV_FRIENDICA_CONTACTS);
21 define("CARDDAV_NAMESPACE_COMMUNITYCONTACTS", 1);
22 define("CARDDAV_NAMESPACE_PHONECONTACTS", 2);
24 define("CALDAV_DB_VERSION", 2);
26 define("CALDAV_MAX_YEAR", date("Y") + 5);
31 function getCurMicrotime()
33 list($usec, $sec) = explode(" ", microtime());
34 return sprintf("%14.0f", $sec * 10000 + $usec * 10000);
35 } // function getCurMicrotime
42 $cur = getCurMicrotime();
43 if ($GLOBALS["debug_time_last"] > 0) {
44 echo "Zeit: " . ($cur - $GLOBALS["debug_time_last"]) . "<br>\n";
46 $GLOBALS["debug_time_last"] = $cur;
51 * @param string $username
54 function dav_compat_username2id($username = "")
56 $x = q("SELECT `uid` FROM `user` WHERE `nickname`='%s' AND `account_removed` = 0 AND `account_expired` = 0", dbesc($username));
57 if (count($x) == 1) return $x[0]["uid"];
65 function dav_compat_id2username($id = 0)
67 $x = q("SELECT `nickname` FROM `user` WHERE `uid` = %i AND `account_removed` = 0 AND `account_expired` = 0", IntVal($id));
68 if (count($x) == 1) return $x[0]["nickname"];
75 function dav_compat_get_curr_user_id()
78 return IntVal($a->user["uid"]);
83 * @param string $principalUri
86 function dav_compat_principal2uid($principalUri = "")
88 if (strlen($principalUri) == 0) return null;
89 if ($principalUri[0] == "/") $principalUri = substr($principalUri, 1);
90 if (strpos($principalUri, "principals/users/") !== 0) return null;
91 $username = substr($principalUri, strlen("principals/users/"));
92 return dav_compat_username2id($username);
96 * @param string $principalUri
99 function dav_compat_principal2namespace($principalUri = "")
101 if (strlen($principalUri) == 0) return null;
102 if ($principalUri[0] == "/") $principalUri = substr($principalUri, 1);
104 if (strpos($principalUri, "principals/users/") !== 0) return null;
105 $username = substr($principalUri, strlen("principals/users/"));
106 return array("namespace" => CALDAV_NAMESPACE_PRIVATE, "namespace_id" => dav_compat_username2id($username));
110 function dav_compat_currentUserPrincipal() {
112 return "principals/users/" . strtolower($a->user["nickname"]);
117 * @param string $name
118 * @return null|string
120 function dav_compat_getRequestVar($name = "")
122 if (isset($_REQUEST[$name])) return $_REQUEST[$name];
128 * @return null|string
130 function dav_compat_parse_text_serverside($text)
132 return dav_compat_getRequestVar($text);
138 function dav_compat_redirect($uri = "")
147 function dav_compat_get_max_private_calendars()
153 * @param int $namespace
154 * @param int $namespace_id
156 * @param array $calendar
157 * @return Sabre_CalDAV_Backend_Common
160 function wdcal_calendar_factory($namespace, $namespace_id, $uri, $calendar = null)
162 switch ($namespace) {
163 case CALDAV_NAMESPACE_PRIVATE:
164 if ($uri == CALDAV_FRIENDICA_MINE || $uri == CALDAV_FRIENDICA_CONTACTS) return Sabre_CalDAV_Backend_Friendica::getInstance();
165 else return Sabre_CalDAV_Backend_Private::getInstance();
167 throw new Exception("Calendar Namespace not found");
171 * @param int $calendar_id
172 * @return Sabre_CalDAV_Backend_Common
175 function wdcal_calendar_factory_by_id($calendar_id) {
176 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendar_id);
177 return wdcal_calendar_factory($calendar["namespace"], $calendar["namespace_id"], $calendar["uri"], $calendar);
184 function wdcal_create_std_calendars()
187 if (!local_user()) return;
189 $privates = q("SELECT COUNT(*) num FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]));
190 if ($privates[0]["num"] > 0) return;
193 'private' => t("Private Calendar"),
194 CALDAV_FRIENDICA_MINE => t("Friendica Events: Mine"),
195 CALDAV_FRIENDICA_CONTACTS => t("Friendica Events: Contacts"),
197 foreach ($uris as $uri => $name) {
198 $cals = q("SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d AND `uri` = '%s'",
199 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, $a->user["uid"], dbesc($uri));
200 if (count($cals) == 0) {
201 q("INSERT INTO %s%scalendars (`namespace`, `namespace_id`, `displayname`, `timezone`, `ctag`, `uri`, `has_vevent`, `has_vtodo`) VALUES (%d, %d, '%s', '%s', 1, '%s', 1, 0)",
202 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]), dbesc($name), dbesc($a->timezone), dbesc($uri)