3 require_once('include/security.php');
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');
14 function dav_uninstall()
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');
27 function dav_include_files()
29 require_once (__DIR__ . "/SabreDAV/lib/Sabre/autoload.php");
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");
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");
51 require_once (__DIR__ . "/common/wdcal_edit.inc.php");
52 require_once (__DIR__ . "/calendar.friendica.fnk.php");
53 require_once (__DIR__ . "/layout.fnk.php");
60 function dav_init(&$a)
64 * Recommended settings:
65 * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
72 error_reporting(E_ALL);
73 ini_set("display_errors", 1);
76 wdcal_create_std_calendars();
77 wdcal_addRequiredHeaders();
79 if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
81 if ($a->argc >= 3 && $a->argv[2] == "feed") {
82 wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
87 if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
88 echo wdcal_getEditPage_exception_selector();
92 if ($a->argc >= 2 && $a->argv[1] == "settings") {
97 if (isset($_REQUEST["test"])) {
98 renderAllCalDavEntries();
102 $server = dav_create_server();
104 $browser = new Sabre_DAV_Browser_Plugin();
105 $server->addPlugin($browser);
107 $aclPlugin = new Sabre_DAVACL_Plugin_Friendica();
108 $aclPlugin->defaultUsernamePath = "principals/users";
109 $server->addPlugin($aclPlugin);
119 function dav_content()
122 if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
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") {
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/");
141 $o .= wdcal_getNewPage();
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") {
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/");
159 $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
162 return wdcal_getDetailPage($calendar_id, $a->argv[3]);
165 // @TODO Edit Calendar
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);
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.");
185 function dav_event_created_hook(&$a, &$b)
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);
197 function dav_event_updated_hook(&$a, &$b)
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);
209 function dav_profile_tabs_hook(&$a, &$b)
211 $b["tabs"][] = array(
212 "label" => t('Calendar'),
213 "url" => $a->get_baseurl() . "/dav/wdcal/",
215 "title" => t('Extended calendar with CalDAV-support'),
224 function dav_cron(&$a, &$b)
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"]);
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;
242 switch ($not["action"]) {
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>");
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' => "",
260 require_once('include/enotify.php');
261 enotify::send($params);
271 * @param null|object $o
273 function dav_plugin_admin_post(&$a = null, &$o = null)
275 check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
278 require_once(__DIR__ . "/database-init.inc.php");
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);
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);
291 else notice(t("An error occurred during the update.") . EOL);
299 function dav_plugin_admin(&$a, &$o)
302 require_once(__DIR__ . "/database-init.inc.php");
304 $dbstatus = dav_check_tables();
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>';
310 $o .= '<h3>' . t('Database status') . '</h3>';
313 $o .= t('Installed');
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') . "'>";
319 $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
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.");
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>";
333 $tables = dav_get_update_statements(1);
334 foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
337 $tables = dav_get_create_statements();
338 foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
341 $o .= "</pre></blockquote>";