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_MAX_YEAR", date("Y") + 5);
29 function getCurMicrotime()
31 list($usec, $sec) = explode(" ", microtime());
32 return sprintf("%14.0f", $sec * 10000 + $usec * 10000);
33 } // function getCurMicrotime
40 $cur = getCurMicrotime();
41 if ($GLOBALS["debug_time_last"] > 0) {
42 echo "Zeit: " . ($cur - $GLOBALS["debug_time_last"]) . "<br>\n";
44 $GLOBALS["debug_time_last"] = $cur;
49 * @param string $username
52 function dav_compat_username2id($username = "")
54 $x = q("SELECT `uid` FROM `user` WHERE `nickname`='%s' AND `account_removed` = 0 AND `account_expired` = 0", dbesc($username));
55 if (count($x) == 1) return $x[0]["uid"];
63 function dav_compat_id2username($id = 0)
65 $x = q("SELECT `nickname` FROM `user` WHERE `uid` = %i AND `account_removed` = 0 AND `account_expired` = 0", IntVal($id));
66 if (count($x) == 1) return $x[0]["nickname"];
73 function dav_compat_get_curr_user_id()
76 return IntVal($a->user["uid"]);
81 * @param string $principalUri
84 function dav_compat_principal2uid($principalUri = "")
86 if (strlen($principalUri) == 0) return null;
87 if ($principalUri[0] == "/") $principalUri = substr($principalUri, 1);
88 if (strpos($principalUri, "principals/users/") !== 0) return null;
89 $username = substr($principalUri, strlen("principals/users/"));
90 return dav_compat_username2id($username);
94 * @param string $principalUri
97 function dav_compat_principal2namespace($principalUri = "")
99 if (strlen($principalUri) == 0) return null;
100 if ($principalUri[0] == "/") $principalUri = substr($principalUri, 1);
102 if (strpos($principalUri, "principals/users/") !== 0) return null;
103 $username = substr($principalUri, strlen("principals/users/"));
104 return array("namespace" => CALDAV_NAMESPACE_PRIVATE, "namespace_id" => dav_compat_username2id($username));
111 function dav_compat_currentUserPrincipal()
114 return "principals/users/" . strtolower($a->user["nickname"]);
119 * @param string $name
120 * @return null|string
122 function dav_compat_getRequestVar($name = "")
124 if (isset($_REQUEST[$name])) return $_REQUEST[$name];
130 * @return null|string
132 function dav_compat_parse_text_serverside($text)
134 return dav_compat_getRequestVar($text);
140 function dav_compat_redirect($uri = "")
149 function dav_compat_get_max_private_calendars()
155 * @param int $namespace
156 * @param int $namespace_id
158 * @param array $calendar
159 * @return Sabre_CalDAV_Backend_Common
162 function wdcal_calendar_factory($namespace, $namespace_id, $uri, $calendar = null)
164 switch ($namespace) {
165 case CALDAV_NAMESPACE_PRIVATE:
166 if ($uri == CALDAV_FRIENDICA_MINE || $uri == CALDAV_FRIENDICA_CONTACTS) return Sabre_CalDAV_Backend_Friendica::getInstance();
167 else return Sabre_CalDAV_Backend_Private::getInstance();
169 throw new Exception("Calendar Namespace not found");
173 * @param int $calendar_id
174 * @return Sabre_CalDAV_Backend_Common
177 function wdcal_calendar_factory_by_id($calendar_id)
179 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendar_id);
180 return wdcal_calendar_factory($calendar["namespace"], $calendar["namespace_id"], $calendar["uri"], $calendar);
184 * @param int $user_id
185 * @param bool $withcheck
188 function wdcal_create_std_calendars_get_statements($user_id, $withcheck = true)
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, IntVal($user_id), dbesc($uri));
200 if (count($cals) == 0 || !$withcheck) $stms[] =
201 sprintf("INSERT INTO %s%scalendars (`namespace`, `namespace_id`, `displayname`, `timezone`, `ctag`, `uri`, `has_vevent`, `has_vtodo`)
202 VALUES (%d, %d, '%s', '%s', 1, '%s', 1, 0)",
203 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, IntVal($user_id), dbesc($name), dbesc($a->timezone), dbesc($uri));
210 function wdcal_create_std_calendars()
213 if (!local_user()) return;
215 $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"]));
216 if ($privates[0]["num"] > 0) return;
218 $stms = wdcal_create_std_calendars_get_statements($a->user["uid"]);
219 foreach ($stms as $stmt) q($stmt);