3 use Friendica\Core\Addon;
4 use Friendica\Core\L10n;
5 use Friendica\Module\Login;
6 use Friendica\Util\Emailer;
8 require_once('include/security.php');
10 function dav_install()
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');
19 function dav_uninstall()
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');
32 function dav_include_files()
34 require_once (__DIR__ . "/../SabreDAV/lib/Sabre/autoload.php");
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");
57 require_once (__DIR__ . "/../common/calendar.fnk.php");
58 require_once (__DIR__ . "/../common/calendar_rendering.fnk.php");
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");
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");
74 require_once (__DIR__ . "/../common/wdcal_configuration.php");
75 require_once (__DIR__ . "/../common/wdcal_backend.inc.php");
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");
83 require_once (__DIR__ . "/../common/wdcal_edit.inc.php");
84 require_once (__DIR__ . "/calendar.friendica.fnk.php");
85 require_once (__DIR__ . "/layout.fnk.php");
92 function dav_init(&$a)
96 * Recommended settings:
97 * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
100 ini_set("display_errors", 1);
101 error_reporting(E_ALL);
107 error_reporting(E_ALL);
108 ini_set("display_errors", 1);
111 wdcal_create_std_calendars();
112 wdcal_create_std_addressbooks();
113 wdcal_addRequiredHeaders();
115 if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
117 if ($a->argc >= 3 && $a->argv[2] == "feed") {
118 wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
123 if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
124 echo wdcal_getEditPage_exception_selector();
128 if ($a->argc >= 2 && $a->argv[1] == "settings") {
133 if (isset($_REQUEST["test"])) {
134 renderAllCalDavEntries();
138 $server = dav_create_server();
140 $browser = new Sabre_DAV_Browser_Plugin();
141 $server->addPlugin($browser);
151 function dav_content()
154 if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
155 return Login::form();
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") {
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/');
173 $o .= wdcal_getNewPage();
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") {
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/');
191 $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
194 return wdcal_getDetailPage($calendar_id, $a->argv[3]);
197 // @TODO Edit Calendar
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);
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.");
217 function dav_event_created_hook(&$a, &$b)
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);
229 function dav_event_updated_hook(&$a, &$b)
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);
241 function dav_profile_tabs_hook(&$a, &$b)
244 "label" => L10n::t('Calendar'),
245 "url" => $a->get_baseurl() . "/dav/wdcal/",
247 "title" => L10n::t('Extended calendar with CalDAV-support'),
256 function dav_cron(&$a, &$b)
260 $r = q("SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW()", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
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"]);
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;
275 switch ($not["action"]) {
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>");
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' => "",
293 Emailer::send($params);
304 * @param null|object $o
306 function dav_addon_admin_post(&$a = null, &$o = null)
308 check_form_security_token_redirectOnErr('/admin/addons/dav', 'dav_admin_save');
311 require_once(__DIR__ . "/database-init.inc.php");
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);
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);
331 function dav_addon_admin(&$a, &$o)
334 require_once(__DIR__ . "/database-init.inc.php");
336 $dbstatus = dav_check_tables();
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>';
342 $o .= '<h3>' . L10n::t('Database status') . '</h3>';
345 $o .= L10n::t('Installed');
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') . "'>";
352 $o .= L10n::t('Not installed') . "<br><br><input type='submit' name='install' value='" . L10n::t('Install') . "'>";
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.");
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>";
366 $tables = dav_get_update_statements($dbstatus);
367 foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
370 $tables = dav_get_create_statements();
371 foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
374 $o .= "</pre></blockquote>";