]> git.mxchange.org Git - friendica-addons.git/blob - dav/calendar.friendica.fnk.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[friendica-addons.git] / dav / calendar.friendica.fnk.php
1 <?php
2
3 $a    = get_app();
4 $uri  = parse_url($a->get_baseurl());
5 $path = "/";
6 if (isset($uri["path"]) && strlen($uri["path"]) > 1) {
7         $path = $uri["path"] . "/";
8 }
9
10 define("CALDAV_SQL_DB", "");
11 define("CALDAV_SQL_PREFIX", "dav_");
12 define("CALDAV_URL_PREFIX", $path . "dav/");
13
14 define("CALDAV_NAMESPACE_PRIVATE", 1);
15
16 define("CALDAV_FRIENDICA_MINE", "friendica-mine");
17 define("CALDAV_FRIENDICA_CONTACTS", "friendica-contacts");
18
19 $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"] = array(CALDAV_FRIENDICA_MINE, CALDAV_FRIENDICA_CONTACTS);
20
21 define("CARDDAV_NAMESPACE_COMMUNITYCONTACTS", 1);
22 define("CARDDAV_NAMESPACE_PHONECONTACTS", 2);
23
24 define("CALDAV_MAX_YEAR", date("Y") + 5);
25
26 /**
27  * @return int
28  */
29 function getCurMicrotime()
30 {
31         list($usec, $sec) = explode(" ", microtime());
32         return sprintf("%14.0f", $sec * 10000 + $usec * 10000);
33 } // function getCurMicrotime
34
35 /**
36  *
37  */
38 function debug_time()
39 {
40         $cur = getCurMicrotime();
41         if ($GLOBALS["debug_time_last"] > 0) {
42                 echo "Zeit: " . ($cur - $GLOBALS["debug_time_last"]) . "<br>\n";
43         }
44         $GLOBALS["debug_time_last"] = $cur;
45 }
46
47
48 /**
49  * @param string $username
50  * @return int|null
51  */
52 function dav_compat_username2id($username = "")
53 {
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"];
56         return null;
57 }
58
59 /**
60  * @param int $id
61  * @return string
62  */
63 function dav_compat_id2username($id = 0)
64 {
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"];
67         return "";
68 }
69
70 /**
71  * @return int
72  */
73 function dav_compat_get_curr_user_id()
74 {
75         $a = get_app();
76         return IntVal($a->user["uid"]);
77 }
78
79
80 /**
81  * @param string $principalUri
82  * @return int|null
83  */
84 function dav_compat_principal2uid($principalUri = "")
85 {
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);
91 }
92
93 /**
94  * @param string $principalUri
95  * @return array|null
96  */
97 function dav_compat_principal2namespace($principalUri = "")
98 {
99         if (strlen($principalUri) == 0) return null;
100         if ($principalUri[0] == "/") $principalUri = substr($principalUri, 1);
101
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));
105 }
106
107
108 /**
109  * @return string
110  */
111 function dav_compat_currentUserPrincipal()
112 {
113         $a = get_app();
114         return "principals/users/" . strtolower($a->user["nickname"]);
115 }
116
117
118 /**
119  * @param string $name
120  * @return null|string
121  */
122 function dav_compat_getRequestVar($name = "")
123 {
124         if (isset($_REQUEST[$name])) return $_REQUEST[$name];
125         else return null;
126 }
127
128 /**
129  * @param $text
130  * @return null|string
131  */
132 function dav_compat_parse_text_serverside($text)
133 {
134         return dav_compat_getRequestVar($text);
135 }
136
137 /**
138  * @param string $uri
139  */
140 function dav_compat_redirect($uri = "")
141 {
142         goaway($uri);
143 }
144
145
146 /**
147  * @return null|int
148  */
149 function dav_compat_get_max_private_calendars()
150 {
151         return null;
152 }
153
154 /**
155  * @param int $namespace
156  * @param int $namespace_id
157  * @param string $uri
158  * @param array $calendar
159  * @return Sabre_CalDAV_Backend_Common
160  * @throws Exception
161  */
162 function wdcal_calendar_factory($namespace, $namespace_id, $uri, $calendar = null)
163 {
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();
168         }
169         throw new Exception("Calendar Namespace not found");
170 }
171
172 /**
173  * @param int $calendar_id
174  * @return Sabre_CalDAV_Backend_Common
175  * @throws Exception
176  */
177 function wdcal_calendar_factory_by_id($calendar_id)
178 {
179         $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendar_id);
180         return wdcal_calendar_factory($calendar["namespace"], $calendar["namespace_id"], $calendar["uri"], $calendar);
181 }
182
183 /**
184  * @param int $user_id
185  * @param bool $withcheck
186  * @return array
187  */
188 function wdcal_create_std_calendars_get_statements($user_id, $withcheck = true)
189 {
190         $stms = array();
191         $a = get_app();
192         $uris = array(
193                 'private'                 => t("Private Calendar"),
194                 CALDAV_FRIENDICA_MINE     => t("Friendica Events: Mine"),
195                 CALDAV_FRIENDICA_CONTACTS => t("Friendica Events: Contacts"),
196         );
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));
204         }
205         return $stms;
206 }
207
208 /**
209  */
210 function wdcal_create_std_calendars()
211 {
212         $a = get_app();
213         if (!local_user()) return;
214
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;
217
218         $stms = wdcal_create_std_calendars_get_statements($a->user["uid"]);
219         foreach ($stms as $stmt) q($stmt);
220 }