]> 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_DB_VERSION", 2);
25
26 define("CALDAV_MAX_YEAR", date("Y") + 5);
27
28 /**
29  * @return int
30  */
31 function getCurMicrotime()
32 {
33         list($usec, $sec) = explode(" ", microtime());
34         return sprintf("%14.0f", $sec * 10000 + $usec * 10000);
35 } // function getCurMicrotime
36
37 /**
38  *
39  */
40 function debug_time()
41 {
42         $cur = getCurMicrotime();
43         if ($GLOBALS["debug_time_last"] > 0) {
44                 echo "Zeit: " . ($cur - $GLOBALS["debug_time_last"]) . "<br>\n";
45         }
46         $GLOBALS["debug_time_last"] = $cur;
47 }
48
49
50 /**
51  * @param string $username
52  * @return int|null
53  */
54 function dav_compat_username2id($username = "")
55 {
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"];
58         return null;
59 }
60
61 /**
62  * @param int $id
63  * @return string
64  */
65 function dav_compat_id2username($id = 0)
66 {
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"];
69         return "";
70 }
71
72 /**
73  * @return int
74  */
75 function dav_compat_get_curr_user_id()
76 {
77         $a = get_app();
78         return IntVal($a->user["uid"]);
79 }
80
81
82 /**
83  * @param string $principalUri
84  * @return int|null
85  */
86 function dav_compat_principal2uid($principalUri = "")
87 {
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);
93 }
94
95 /**
96  * @param string $principalUri
97  * @return array|null
98  */
99 function dav_compat_principal2namespace($principalUri = "")
100 {
101         if (strlen($principalUri) == 0) return null;
102         if ($principalUri[0] == "/") $principalUri = substr($principalUri, 1);
103
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));
107 }
108
109
110 function dav_compat_currentUserPrincipal() {
111         $a = get_app();
112         return "principals/users/" . strtolower($a->user["nickname"]);
113 }
114
115
116 /**
117  * @param string $name
118  * @return null|string
119  */
120 function dav_compat_getRequestVar($name = "")
121 {
122         if (isset($_REQUEST[$name])) return $_REQUEST[$name];
123         else return null;
124 }
125
126 /**
127  * @param $text
128  * @return null|string
129  */
130 function dav_compat_parse_text_serverside($text)
131 {
132         return dav_compat_getRequestVar($text);
133 }
134
135 /**
136  * @param string $uri
137  */
138 function dav_compat_redirect($uri = "")
139 {
140         goaway($uri);
141 }
142
143
144 /**
145  * @return null|int
146  */
147 function dav_compat_get_max_private_calendars()
148 {
149         return null;
150 }
151
152 /**
153  * @param int $namespace
154  * @param int $namespace_id
155  * @param string $uri
156  * @param array $calendar
157  * @return Sabre_CalDAV_Backend_Common
158  * @throws Exception
159  */
160 function wdcal_calendar_factory($namespace, $namespace_id, $uri, $calendar = null)
161 {
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();
166         }
167         throw new Exception("Calendar Namespace not found");
168 }
169
170 /**
171  * @param int $calendar_id
172  * @return Sabre_CalDAV_Backend_Common
173  * @throws Exception
174  */
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);
178 }
179
180
181
182 /**
183  */
184 function wdcal_create_std_calendars()
185 {
186         $a = get_app();
187         if (!local_user()) return;
188
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;
191
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, $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)
203                         );
204                 }
205         }
206
207 }