]> git.mxchange.org Git - friendica-addons.git/blob - dav/main.php
Merge branch 'master' of git://github.com/friendica/friendica-addons
[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         try {
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         } catch (DAVVersionMismatchException $e) {
169                 $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.");
170         }
171         return $x;
172 }
173
174
175 /**
176  * @param App $a
177  * @param object $b
178  */
179 function dav_event_created_hook(&$a, &$b)
180 {
181         dav_include_files();
182         // @TODO Updating the cache instead of completely invalidating and rebuilding it
183         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
184         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
185 }
186
187 /**
188  * @param App $a
189  * @param object $b
190  */
191 function dav_event_updated_hook(&$a, &$b)
192 {
193         dav_include_files();
194         // @TODO Updating the cache instead of completely invalidating and rebuilding it
195         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
196         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
197 }
198
199 /**
200  * @param App $a
201  * @param object $b
202  */
203 function dav_profile_tabs_hook(&$a, &$b)
204 {
205         $b["tabs"][] = array(
206                 "label" => t('Calendar'),
207                 "url"   => $a->get_baseurl() . "/dav/wdcal/",
208                 "sel"   => "",
209                 "title" => t('Extended calendar with CalDAV-support'),
210         );
211 }
212
213
214 /**
215  * @param App $a
216  * @param object $b
217  */
218 function dav_cron(&$a, &$b)
219 {
220         dav_include_files();
221
222         $r = q("SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW()", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
223         foreach ($r as $not) {
224                 q("UPDATE %s%snotifications SET `notified` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["id"]);
225                 $event    = q("SELECT * FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendarobject_id"]);
226                 $calendar = q("SELECT * FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendar_id"]);
227                 $users    = array();
228                 if (count($calendar) != 1 || count($event) == 0) continue;
229                 switch ($calendar[0]["namespace"]) {
230                         case CALDAV_NAMESPACE_PRIVATE:
231                                 $user = q("SELECT * FROM user WHERE `uid` = %d AND `blocked` = 0", $calendar[0]["namespace_id"]);
232                                 if (count($user) != 1) continue;
233                                 $users[] = $user[0];
234                                 break;
235                 }
236                 switch ($not["action"]) {
237                         case "email":
238                         case "display": // @TODO implement "Display"
239                                 foreach ($users as $user) {
240                                         $find      = array("%to%", "%event%", "%url%");
241                                         $repl      = array($user["username"], $event[0]["Summary"], $a->get_baseurl() . "/dav/wdcal/" . $calendar[0]["id"] . "/" . $not["calendarobject_id"] . "/");
242                                         $text_text = str_replace($find, $repl, "Hi %to%!\n\nThe event \"%event%\" is about to begin:\n%url%");
243                                         $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>");
244                                         $params    = array(
245                                                 'fromName'             => FRIENDICA_PLATFORM,
246                                                 'fromEmail'            => t('noreply') . '@' . $a->get_hostname(),
247                                                 'replyTo'              => t('noreply') . '@' . $a->get_hostname(),
248                                                 'toEmail'              => $user["email"],
249                                                 'messageSubject'       => t("Notification: " . $event[0]["Summary"]),
250                                                 'htmlVersion'          => $text_html,
251                                                 'textVersion'          => $text_text,
252                                                 'additionalMailHeader' => "",
253                                         );
254                                         require_once('include/enotify.php');
255                                         enotify::send($params);
256                                 }
257                                 break;
258                 }
259         }
260 }
261
262
263 /**
264  * @param App $a
265  * @param null|object $o
266  */
267 function dav_plugin_admin_post(&$a = null, &$o = null)
268 {
269         check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
270
271         dav_include_files();
272         require_once(__DIR__ . "/database-init.inc.php");
273
274         if (isset($_REQUEST["install"])) {
275                 $errs = dav_create_tables();
276                 if (count($errs) == 0) info(t('The database tables have been installed.') . EOL);
277                 else notice(t("An error occurred during the installation.") . EOL);
278         }
279         if (isset($_REQUEST["upgrade"])) {
280                 $errs = dav_upgrade_tables();
281                 if (count($errs) == 0) {
282                         renderAllCalDavEntries();
283                         info(t('The database tables have been updated.') . EOL);
284                 }
285                 else notice(t("An error occurred during the update.") . EOL);
286         }
287 }
288
289 /**
290  * @param App $a
291  * @param string $o
292  */
293 function dav_plugin_admin(&$a, &$o)
294 {
295         dav_include_files();
296         require_once(__DIR__ . "/database-init.inc.php");
297
298         $dbstatus = dav_check_tables();
299
300         $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("dav_admin_save") . '">';
301         $o .= '<i>' . t("No system-wide settings yet.") . '</i><br><br>';
302
303
304         $o .= '<h3>' . t('Database status') . '</h3>';
305         switch ($dbstatus) {
306                 case 0:
307                         $o .= t('Installed');
308                         break;
309                 case 1:
310                         $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') . "'>";
311                         break;
312                 case -1:
313                         $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
314                         break;
315                 case -2:
316                 default:
317                         $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.");
318                         break;
319         }
320         $o .= "<br><br>";
321
322         $o .= "<h3>" . t("Troubleshooting") . "</h3>";
323         $o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
324         $o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
325         switch ($dbstatus) {
326                 case 1:
327                         $tables = dav_get_update_statements(1);
328                         foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
329                         break;
330                 default:
331                         $tables = dav_get_create_statements();
332                         foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
333                         break;
334         }
335         $o .= "</pre></blockquote>";
336 }