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();
103 $browser = new Sabre_DAV_Browser_Plugin();
104 $server->addPlugin($browser);
113 function dav_content()
116 if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
122 if ($a->argv[1] == "settings") {
123 return wdcal_getSettingsPage($a);
124 } elseif ($a->argv[1] == "wdcal") {
125 if (isset($a->argv[2]) && strlen($a->argv[2]) > 0) {
126 if ($a->argv[2] == "new") {
128 if (isset($_REQUEST["save"])) {
129 check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
130 $ret = wdcal_postEditPage("new", "", $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
131 if ($ret["ok"]) notice($ret["msg"]);
132 else info($ret["msg"]);
133 goaway($a->get_baseurl() . "/dav/wdcal/");
135 $o .= wdcal_getNewPage();
138 $calendar_id = IntVal($a->argv[2]);
139 if (isset($a->argv[3]) && $a->argv[3] == "ics-export") {
140 wdcal_print_user_ics($calendar_id);
141 } elseif (isset($a->argv[3]) && $a->argv[3] == "ics-import") {
142 return wdcal_import_user_ics($calendar_id);
143 } elseif (isset($a->argv[3]) && $a->argv[3] > 0) {
144 if (isset($a->argv[4]) && $a->argv[4] == "edit") {
146 if (isset($_REQUEST["save"])) {
147 check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
148 $ret = wdcal_postEditPage($a->argv[3], $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
149 if ($ret["ok"]) notice($ret["msg"]);
150 else info($ret["msg"]);
151 goaway($a->get_baseurl() . "/dav/wdcal/");
153 $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
156 return wdcal_getDetailPage($calendar_id, $a->argv[3]);
159 // @TODO Edit Calendar
163 $server = dav_create_server(true, true, false);
164 $cals = dav_get_current_user_calendars($server, DAV_ACL_READ);
165 $x = wdcal_printCalendar($cals, array(), $a->get_baseurl() . "/dav/wdcal/feed/", "week", 0, 200);
176 function dav_event_created_hook(&$a, &$b)
179 // @TODO Updating the cache instead of completely invalidating and rebuilding it
180 Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
181 Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
188 function dav_event_updated_hook(&$a, &$b)
191 // @TODO Updating the cache instead of completely invalidating and rebuilding it
192 Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
193 Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
200 function dav_profile_tabs_hook(&$a, &$b)
202 $b["tabs"][] = array(
203 "label" => t('Calendar'),
204 "url" => $a->get_baseurl() . "/dav/wdcal/",
206 "title" => t('Extended calendar with CalDAV-support'),
216 function dav_cron(&$a, &$b)
220 $r = q("SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW()", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
221 foreach ($r as $not) {
222 q("UPDATE %s%snotifications SET `notified` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["id"]);
223 $event = q("SELECT * FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendarobject_id"]);
224 $calendar = q("SELECT * FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB,CALDAV_SQL_PREFIX, $not["calendar_id"]);
226 if (count($calendar) != 1 || count($event) == 0) continue;
227 switch ($calendar[0]["namespace"]) {
228 case CALDAV_NAMESPACE_PRIVATE:
229 $user = q("SELECT * FROM user WHERE `uid` = %d AND `blocked` = 0", $calendar[0]["namespace_id"]);
230 if (count($user) != 1) continue;
234 switch ($not["action"]) {
235 case "email": case "display": // @TODO implement "Display"
236 foreach ($users as $user) {
237 $find = array( "%to%" , "%event%", "%url%");
238 $repl = array($user["username"], $event[0]["Summary"], $a->get_baseurl() . "/dav/wdcal/" . $calendar[0]["id"] . "/" . $not["calendarobject_id"] . "/");
239 $text_text = str_replace($find, $repl, "Hi %to%!\n\nThe event \"%event%\" is about to begin:\n%url%");
240 $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>");
242 'fromName' => FRIENDICA_PLATFORM,
243 'fromEmail' => t('noreply') . '@' . $a->get_hostname(),
244 'replyTo' => t('noreply') . '@' . $a->get_hostname(),
245 'toEmail' => $user["email"],
246 'messageSubject' => t("Notification: " . $event[0]["Summary"]),
247 'htmlVersion' => $text_html,
248 'textVersion' => $text_text,
249 'additionalMailHeader' => "",
251 require_once('include/enotify.php');
252 enotify::send($params);
264 * @param null|object $o
266 function dav_plugin_admin_post(&$a = null, &$o = null)
268 check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
271 require_once(__DIR__ . "/database-init.inc.php");
273 if (isset($_REQUEST["install"])) {
274 $errs = dav_create_tables();
275 if (count($errs) == 0) info(t('The database tables have been installed.') . EOL);
276 else notice(t("An error occurred during the installation.") . EOL);
278 if (isset($_REQUEST["upgrade"])) {
279 $errs = dav_upgrade_tables();
280 if (count($errs) == 0) info(t('The database tables have been updated.') . EOL);
281 else notice(t("An error occurred during the update.") . EOL);
289 function dav_plugin_admin(&$a, &$o)
292 require_once(__DIR__ . "/database-init.inc.php");
294 $dbstatus = dav_check_tables();
296 $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("dav_admin_save") . '">';
297 $o .= '<i>' . t("No system-wide settings yet.") . '</i><br><br>';
300 $o .= '<h3>' . t('Database status') . '</h3>';
303 $o .= t('Installed');
306 $o .= t('Upgrade needed') . "<br><br><input type='submit' name='upgrade' value='" . t('Upgrade') . "'>";
309 $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
314 $o .= "<h3>" . t("Troubleshooting") . "</h3>";
315 $o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
316 $o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
317 $tables = dav_get_create_statements();
318 foreach ($tables as $t) $o .= escape_tags($t . "\n\n");
319 $o .= "</pre></blockquote>";