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