]> git.mxchange.org Git - friendica-addons.git/blob - dav/main.php
a56862aecd9848908892a49545aa9a61d7ce4e4d
[friendica-addons.git] / dav / main.php
1 <?php
2
3 require_once('include/security.php');
4
5 function dav_install()
6 {
7         register_hook('event_created', 'addon/dav/dav.php', 'dav_event_created_hook');
8         register_hook('event_updated', 'addon/dav/dav.php', 'dav_event_updated_hook');
9         register_hook('profile_tabs', 'addon/dav/dav.php', 'dav_profile_tabs_hook');
10         register_hook('cron', 'addon/dav/dav.php', 'dav_cron');
11 }
12
13
14 function dav_uninstall()
15 {
16         unregister_hook('event_created', 'addon/dav/dav.php', 'dav_event_created_hook');
17         unregister_hook('event_updated', 'addon/dav/dav.php', 'dav_event_updated_hook');
18         unregister_hook('profile_tabs', 'addon/dav/dav.php', 'dav_profile_tabs_hook');
19         unregister_hook('cron', 'addon/dav/dav.php', 'dav_cron');
20 }
21
22
23 function dav_module()
24 {
25 }
26
27 function dav_include_files()
28 {
29         require_once (__DIR__ . "/SabreDAV/lib/Sabre/autoload.php");
30
31         require_once (__DIR__ . "/common/calendar.fnk.php");
32         require_once (__DIR__ . "/common/calendar_rendering.fnk.php");
33         require_once (__DIR__ . "/common/dav_caldav_backend_common.inc.php");
34         require_once (__DIR__ . "/common/dav_caldav_backend_private.inc.php");
35         require_once (__DIR__ . "/common/dav_caldav_backend_virtual.inc.php");
36         require_once (__DIR__ . "/common/dav_caldav_root.inc.php");
37         require_once (__DIR__ . "/common/dav_user_calendars.inc.php");
38         require_once (__DIR__ . "/common/dav_carddav_root.inc.php");
39         require_once (__DIR__ . "/common/dav_carddav_backend_std.inc.php");
40         require_once (__DIR__ . "/common/dav_user_addressbooks.inc.php");
41         require_once (__DIR__ . "/common/dav_caldav_calendar_virtual.inc.php");
42         require_once (__DIR__ . "/common/wdcal_configuration.php");
43         require_once (__DIR__ . "/common/wdcal_backend.inc.php");
44
45         require_once (__DIR__ . "/dav_friendica_principal.inc.php");
46         require_once (__DIR__ . "/dav_friendica_auth.inc.php");
47         require_once (__DIR__ . "/dav_carddav_backend_virtual_friendica.inc.php");
48         require_once (__DIR__ . "/dav_caldav_backend_virtual_friendica.inc.php");
49         require_once (__DIR__ . "/FriendicaACLPlugin.inc.php");
50
51         require_once (__DIR__ . "/common/wdcal_edit.inc.php");
52         require_once (__DIR__ . "/calendar.friendica.fnk.php");
53         require_once (__DIR__ . "/layout.fnk.php");
54 }
55
56
57 /**
58  * @param App $a
59  */
60 function dav_init(&$a)
61 {
62
63         /*
64          * Recommended settings:
65          * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
66          */
67
68         dav_include_files();
69
70         if (false) {
71                 dbg(true);
72                 error_reporting(E_ALL);
73                 ini_set("display_errors", 1);
74         }
75
76         wdcal_create_std_calendars();
77         wdcal_addRequiredHeaders();
78
79         if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
80
81                 if ($a->argc >= 3 && $a->argv[2] == "feed") {
82                         wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
83                         killme();
84                 }
85                 return;
86         }
87         if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
88                 echo wdcal_getEditPage_exception_selector();
89                 killme();
90         }
91
92         if ($a->argc >= 2 && $a->argv[1] == "settings") {
93                 return;
94         }
95
96
97         if (isset($_REQUEST["test"])) {
98                 renderAllCalDavEntries();
99         }
100
101
102         $server  = dav_create_server();
103
104         $browser = new Sabre_DAV_Browser_Plugin();
105         $server->addPlugin($browser);
106
107         $aclPlugin                      = new Sabre_DAVACL_Plugin_Friendica();
108         $aclPlugin->defaultUsernamePath = "principals/users";
109         $server->addPlugin($aclPlugin);
110
111         $server->exec();
112
113         killme();
114 }
115
116 /**
117  * @return string
118  */
119 function dav_content()
120 {
121         $a = get_app();
122         if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
123                 return login();
124         }
125
126         $x = "";
127         try {
128                 if ($a->argv[1] == "settings") {
129                         return wdcal_getSettingsPage($a);
130                 } elseif ($a->argv[1] == "wdcal") {
131                         if (isset($a->argv[2]) && strlen($a->argv[2]) > 0) {
132                                 if ($a->argv[2] == "new") {
133                                         $o = "";
134                                         if (isset($_REQUEST["save"])) {
135                                                 check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
136                                                 $ret = wdcal_postEditPage("new", "", $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
137                                                 if ($ret["ok"]) notice($ret["msg"]);
138                                                 else info($ret["msg"]);
139                                                 goaway($a->get_baseurl() . "/dav/wdcal/");
140                                         }
141                                         $o .= wdcal_getNewPage();
142                                         return $o;
143                                 } else {
144                                         $calendar_id = IntVal($a->argv[2]);
145                                         if (isset($a->argv[3]) && $a->argv[3] == "ics-export") {
146                                                 wdcal_print_user_ics($calendar_id);
147                                         } elseif (isset($a->argv[3]) && $a->argv[3] == "ics-import") {
148                                                 return wdcal_import_user_ics($calendar_id);
149                                         } elseif (isset($a->argv[3]) && $a->argv[3] > 0) {
150                                                 if (isset($a->argv[4]) && $a->argv[4] == "edit") {
151                                                         $o = "";
152                                                         if (isset($_REQUEST["save"])) {
153                                                                 check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
154                                                                 $ret = wdcal_postEditPage($a->argv[3], $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
155                                                                 if ($ret["ok"]) notice($ret["msg"]);
156                                                                 else info($ret["msg"]);
157                                                                 goaway($a->get_baseurl() . "/dav/wdcal/");
158                                                         }
159                                                         $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
160                                                         return $o;
161                                                 } else {
162                                                         return wdcal_getDetailPage($calendar_id, $a->argv[3]);
163                                                 }
164                                         } else {
165                                                 // @TODO Edit Calendar
166                                         }
167                                 }
168                         } else {
169                                 $server = dav_create_server(true, true, false);
170                                 $cals   = dav_get_current_user_calendars($server, DAV_ACL_READ);
171                                 $x      = wdcal_printCalendar($cals, array(), $a->get_baseurl() . "/dav/wdcal/feed/", "week", 0, 200);
172                         }
173                 }
174         } catch (DAVVersionMismatchException $e) {
175                 $x = t("The current version of this plugin has not been set up correctly. Please contact the system administrator of your installation of friendica to fix this.");
176         }
177         return $x;
178 }
179
180
181 /**
182  * @param App $a
183  * @param object $b
184  */
185 function dav_event_created_hook(&$a, &$b)
186 {
187         dav_include_files();
188         // @TODO Updating the cache instead of completely invalidating and rebuilding it
189         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
190         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
191 }
192
193 /**
194  * @param App $a
195  * @param object $b
196  */
197 function dav_event_updated_hook(&$a, &$b)
198 {
199         dav_include_files();
200         // @TODO Updating the cache instead of completely invalidating and rebuilding it
201         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
202         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
203 }
204
205 /**
206  * @param App $a
207  * @param object $b
208  */
209 function dav_profile_tabs_hook(&$a, &$b)
210 {
211         $b["tabs"][] = array(
212                 "label" => t('Calendar'),
213                 "url"   => $a->get_baseurl() . "/dav/wdcal/",
214                 "sel"   => "",
215                 "title" => t('Extended calendar with CalDAV-support'),
216         );
217 }
218
219
220 /**
221  * @param App $a
222  * @param object $b
223  */
224 function dav_cron(&$a, &$b)
225 {
226         dav_include_files();
227
228         $r = q("SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW()", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
229         foreach ($r as $not) {
230                 q("UPDATE %s%snotifications SET `notified` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["id"]);
231                 $event    = q("SELECT * FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendarobject_id"]);
232                 $calendar = q("SELECT * FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendar_id"]);
233                 $users    = array();
234                 if (count($calendar) != 1 || count($event) == 0) continue;
235                 switch ($calendar[0]["namespace"]) {
236                         case CALDAV_NAMESPACE_PRIVATE:
237                                 $user = q("SELECT * FROM user WHERE `uid` = %d AND `blocked` = 0", $calendar[0]["namespace_id"]);
238                                 if (count($user) != 1) continue;
239                                 $users[] = $user[0];
240                                 break;
241                 }
242                 switch ($not["action"]) {
243                         case "email":
244                         case "display": // @TODO implement "Display"
245                                 foreach ($users as $user) {
246                                         $find      = array("%to%", "%event%", "%url%");
247                                         $repl      = array($user["username"], $event[0]["Summary"], $a->get_baseurl() . "/dav/wdcal/" . $calendar[0]["id"] . "/" . $not["calendarobject_id"] . "/");
248                                         $text_text = str_replace($find, $repl, "Hi %to%!\n\nThe event \"%event%\" is about to begin:\n%url%");
249                                         $text_html = str_replace($find, $repl, "Hi %to%!<br>\n<br>\nThe event \"%event%\" is about to begin:<br>\n<a href='" . "%url%" . "'>%url%</a>");
250                                         $params    = array(
251                                                 'fromName'             => FRIENDICA_PLATFORM,
252                                                 'fromEmail'            => t('noreply') . '@' . $a->get_hostname(),
253                                                 'replyTo'              => t('noreply') . '@' . $a->get_hostname(),
254                                                 'toEmail'              => $user["email"],
255                                                 'messageSubject'       => t("Notification: " . $event[0]["Summary"]),
256                                                 'htmlVersion'          => $text_html,
257                                                 'textVersion'          => $text_text,
258                                                 'additionalMailHeader' => "",
259                                         );
260                                         require_once('include/enotify.php');
261                                         enotify::send($params);
262                                 }
263                                 break;
264                 }
265         }
266 }
267
268
269 /**
270  * @param App $a
271  * @param null|object $o
272  */
273 function dav_plugin_admin_post(&$a = null, &$o = null)
274 {
275         check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
276
277         dav_include_files();
278         require_once(__DIR__ . "/database-init.inc.php");
279
280         if (isset($_REQUEST["install"])) {
281                 $errs = dav_create_tables();
282                 if (count($errs) == 0) info(t('The database tables have been installed.') . EOL);
283                 else notice(t("An error occurred during the installation.") . EOL);
284         }
285         if (isset($_REQUEST["upgrade"])) {
286                 $errs = dav_upgrade_tables();
287                 if (count($errs) == 0) {
288                         renderAllCalDavEntries();
289                         info(t('The database tables have been updated.') . EOL);
290                 }
291                 else notice(t("An error occurred during the update.") . EOL);
292         }
293 }
294
295 /**
296  * @param App $a
297  * @param string $o
298  */
299 function dav_plugin_admin(&$a, &$o)
300 {
301         dav_include_files();
302         require_once(__DIR__ . "/database-init.inc.php");
303
304         $dbstatus = dav_check_tables();
305
306         $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("dav_admin_save") . '">';
307         $o .= '<i>' . t("No system-wide settings yet.") . '</i><br><br>';
308
309
310         $o .= '<h3>' . t('Database status') . '</h3>';
311         switch ($dbstatus) {
312                 case 0:
313                         $o .= t('Installed');
314                         break;
315                 case 1:
316                         $o .= "<strong>" . t('Upgrade needed') . "</strong><br>" . t("Please back up all calendar data (the tables beginning with dav_*) before proceeding. While all calendar events <i>should</i> be converted to the new database structure, it's always safe to have a backup. Below, you can have a look at the database-queries that will be made when pressing the 'update'-button.") . "<br><br><input type='submit' name='upgrade' value='" . t('Upgrade') . "'>";
317                         break;
318                 case -1:
319                         $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
320                         break;
321                 case -2:
322                 default:
323                         $o .= t('Unknown') . "<br><br>" . t("Something really went wrong. I cannot recover from this state automatically, sorry. Please go to the database backend, back up the data, and delete all tables beginning with 'dav_' manually. Afterwards, this installation routine should be able to reinitialize the tables automatically.");
324                         break;
325         }
326         $o .= "<br><br>";
327
328         $o .= "<h3>" . t("Troubleshooting") . "</h3>";
329         $o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
330         $o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
331         switch ($dbstatus) {
332                 case 1:
333                         $tables = dav_get_update_statements(1);
334                         foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
335                         break;
336                 default:
337                         $tables = dav_get_create_statements();
338                         foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
339                         break;
340         }
341         $o .= "</pre></blockquote>";
342 }