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