]> git.mxchange.org Git - friendica-addons.git/blob - dav/main.php
Notifications by E-Mail implemented
[friendica-addons.git] / dav / main.php
1 <?php
2
3 require_once('include/security.php');
4
5 function dav_install()
6 {
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');
11 }
12
13
14 function dav_uninstall()
15 {
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');
20 }
21
22
23 function dav_module()
24 {
25 }
26
27 function dav_include_files()
28 {
29         require_once (__DIR__ . "/SabreDAV/lib/Sabre/autoload.php");
30
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");
44
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");
50
51         require_once (__DIR__ . "/common/wdcal_edit.inc.php");
52         require_once (__DIR__ . "/calendar.friendica.fnk.php");
53         require_once (__DIR__ . "/layout.fnk.php");
54 }
55
56
57 /**
58  * @param App $a
59  */
60 function dav_init(&$a)
61 {
62
63         /*
64          * Recommended settings:
65          * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
66          */
67
68         dav_include_files();
69
70         if (true) {
71                 dbg(true);
72                 error_reporting(E_ALL);
73                 ini_set("display_errors", 1);
74         }
75
76         wdcal_create_std_calendars();
77         wdcal_addRequiredHeaders();
78
79         if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
80
81                 if ($a->argc >= 3 && $a->argv[2] == "feed") {
82                         wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
83                         killme();
84                 }
85                 return;
86         }
87         if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
88                 echo wdcal_getEditPage_exception_selector();
89                 killme();
90         }
91
92         if ($a->argc >= 2 && $a->argv[1] == "settings") {
93                 return;
94         }
95
96
97         if (isset($_REQUEST["test"])) {
98                 renderAllCalDavEntries();
99         }
100
101
102         $server  = dav_create_server();
103         $browser = new Sabre_DAV_Browser_Plugin();
104         $server->addPlugin($browser);
105         $server->exec();
106
107         killme();
108 }
109
110 /**
111  * @return string
112  */
113 function dav_content()
114 {
115         $a = get_app();
116         if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
117                 return login();
118         }
119
120         $x = "";
121
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") {
127                                 $o = "";
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/");
134                                 }
135                                 $o .= wdcal_getNewPage();
136                                 return $o;
137                         } else {
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") {
145                                                 $o = "";
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/");
152                                                 }
153                                                 $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
154                                                 return $o;
155                                         } else {
156                                                 return wdcal_getDetailPage($calendar_id, $a->argv[3]);
157                                         }
158                                 } else {
159                                         // @TODO Edit Calendar
160                                 }
161                         }
162                 } else {
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);
166                 }
167         }
168         return $x;
169 }
170
171
172 /**
173  * @param App $a
174  * @param object $b
175  */
176 function dav_event_created_hook(&$a, &$b)
177 {
178         dav_include_files();
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);
182 }
183
184 /**
185  * @param App $a
186  * @param object $b
187  */
188 function dav_event_updated_hook(&$a, &$b)
189 {
190         dav_include_files();
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);
194 }
195
196 /**
197  * @param App $a
198  * @param object $b
199  */
200 function dav_profile_tabs_hook(&$a, &$b)
201 {
202         $b["tabs"][] = array(
203                 "label" => t('Calendar'),
204                 "url"   => $a->get_baseurl() . "/dav/wdcal/",
205                 "sel"   => "",
206                 "title" => t('Extended calendar with CalDAV-support'),
207         );
208 }
209
210
211
212 /**
213  * @param App $a
214  * @param object $b
215  */
216 function dav_cron(&$a, &$b)
217 {
218         dav_include_files();
219
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"]);
225                 $users = array();
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;
231                                 $users[] = $user[0];
232                                 break;
233                 }
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>");
241                                         $params = array(
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' => "",
250                                         );
251                                         require_once('include/enotify.php');
252                                         enotify::send($params);
253                                 }
254                                 break;
255                 }
256         }
257 }
258
259
260
261
262 /**
263  * @param App $a
264  * @param null|object $o
265  */
266 function dav_plugin_admin_post(&$a = null, &$o = null)
267 {
268         check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
269
270         dav_include_files();
271         require_once(__DIR__ . "/database-init.inc.php");
272
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);
277         }
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);
282         }
283 }
284
285 /**
286  * @param App $a
287  * @param string $o
288  */
289 function dav_plugin_admin(&$a, &$o)
290 {
291         dav_include_files();
292         require_once(__DIR__ . "/database-init.inc.php");
293
294         $dbstatus = dav_check_tables();
295
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>';
298
299
300         $o .= '<h3>' . t('Database status') . '</h3>';
301         switch ($dbstatus) {
302                 case 0:
303                         $o .= t('Installed');
304                         break;
305                 case 1:
306                         $o .= t('Upgrade needed') . "<br><br><input type='submit' name='upgrade' value='" . t('Upgrade') . "'>";
307                         break;
308                 case -1:
309                         $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
310                         break;
311         }
312         $o .= "<br><br>";
313
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>";
320 }