]> git.mxchange.org Git - friendica-addons.git/blob - dav/friendica/main.php
Move friendica-specific parts into an own subdirectory
[friendica-addons.git] / dav / friendica / 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
34         require_once (__DIR__ . "/../common/dav_caldav_backend_common.inc.php");
35         require_once (__DIR__ . "/../common/dav_caldav_backend_private.inc.php");
36         require_once (__DIR__ . "/../common/dav_caldav_backend_virtual.inc.php");
37         require_once (__DIR__ . "/../common/dav_caldav_root.inc.php");
38         require_once (__DIR__ . "/../common/dav_user_calendars.inc.php");
39         require_once (__DIR__ . "/../common/dav_caldav_calendar_virtual.inc.php");
40         require_once (__DIR__ . "/../common/dav_caldav_calendar_private.inc.php");
41
42         require_once (__DIR__ . "/../common/dav_carddav_root.inc.php");
43         require_once (__DIR__ . "/../common/dav_carddav_backend_common.inc.php");
44         require_once (__DIR__ . "/../common/dav_carddav_backend_virtual.inc.php");
45         require_once (__DIR__ . "/../common/dav_carddav_backend_private.inc.php");
46         require_once (__DIR__ . "/../common/dav_user_addressbooks.inc.php");
47
48         require_once (__DIR__ . "/../common/wdcal_configuration.php");
49         require_once (__DIR__ . "/../common/wdcal_backend.inc.php");
50
51         require_once (__DIR__ . "/dav_friendica_principal.inc.php");
52         require_once (__DIR__ . "/dav_friendica_auth.inc.php");
53         require_once (__DIR__ . "/dav_carddav_backend_virtual_friendica.inc.php");
54         require_once (__DIR__ . "/dav_caldav_backend_virtual_friendica.inc.php");
55         require_once (__DIR__ . "/FriendicaACLPlugin.inc.php");
56
57         require_once (__DIR__ . "/../common/wdcal_edit.inc.php");
58         require_once (__DIR__ . "/calendar.friendica.fnk.php");
59         require_once (__DIR__ . "/layout.fnk.php");
60 }
61
62
63 /**
64  * @param App $a
65  */
66 function dav_init(&$a)
67 {
68
69         /*
70          * Recommended settings:
71          * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
72          */
73
74         ini_set("display_errors", 1);
75         error_reporting(E_ALL);
76
77         dav_include_files();
78
79         if (false) {
80                 dbg(true);
81                 error_reporting(E_ALL);
82                 ini_set("display_errors", 1);
83         }
84
85         wdcal_create_std_calendars();
86         wdcal_create_std_addressbooks();
87         wdcal_addRequiredHeaders();
88
89         if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
90
91                 if ($a->argc >= 3 && $a->argv[2] == "feed") {
92                         wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
93                         killme();
94                 }
95                 return;
96         }
97         if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
98                 echo wdcal_getEditPage_exception_selector();
99                 killme();
100         }
101
102         if ($a->argc >= 2 && $a->argv[1] == "settings") {
103                 return;
104         }
105
106
107         if (isset($_REQUEST["test"])) {
108                 renderAllCalDavEntries();
109         }
110
111
112         $server  = dav_create_server();
113
114         $browser = new Sabre_DAV_Browser_Plugin();
115         $server->addPlugin($browser);
116
117         $server->exec();
118
119         killme();
120 }
121
122 /**
123  * @return string
124  */
125 function dav_content()
126 {
127         $a = get_app();
128         if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
129                 return login();
130         }
131
132         $x = "";
133         try {
134                 if ($a->argv[1] == "settings") {
135                         return wdcal_getSettingsPage($a);
136                 } elseif ($a->argv[1] == "wdcal") {
137                         if (isset($a->argv[2]) && strlen($a->argv[2]) > 0) {
138                                 if ($a->argv[2] == "new") {
139                                         $o = "";
140                                         if (isset($_REQUEST["save"])) {
141                                                 check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
142                                                 $ret = wdcal_postEditPage("new", "", $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
143                                                 if ($ret["ok"]) notice($ret["msg"]);
144                                                 else info($ret["msg"]);
145                                                 goaway($a->get_baseurl() . "/dav/wdcal/");
146                                         }
147                                         $o .= wdcal_getNewPage();
148                                         return $o;
149                                 } else {
150                                         $calendar_id = IntVal($a->argv[2]);
151                                         if (isset($a->argv[3]) && $a->argv[3] == "ics-export") {
152                                                 wdcal_print_user_ics($calendar_id);
153                                         } elseif (isset($a->argv[3]) && $a->argv[3] == "ics-import") {
154                                                 return wdcal_import_user_ics($calendar_id);
155                                         } elseif (isset($a->argv[3]) && $a->argv[3] > 0) {
156                                                 if (isset($a->argv[4]) && $a->argv[4] == "edit") {
157                                                         $o = "";
158                                                         if (isset($_REQUEST["save"])) {
159                                                                 check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
160                                                                 $ret = wdcal_postEditPage($a->argv[3], $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
161                                                                 if ($ret["ok"]) notice($ret["msg"]);
162                                                                 else info($ret["msg"]);
163                                                                 goaway($a->get_baseurl() . "/dav/wdcal/");
164                                                         }
165                                                         $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
166                                                         return $o;
167                                                 } else {
168                                                         return wdcal_getDetailPage($calendar_id, $a->argv[3]);
169                                                 }
170                                         } else {
171                                                 // @TODO Edit Calendar
172                                         }
173                                 }
174                         } else {
175                                 $server = dav_create_server(true, true, false);
176                                 $cals   = dav_get_current_user_calendars($server, DAV_ACL_READ);
177                                 $x      = wdcal_printCalendar($cals, array(), $a->get_baseurl() . "/dav/wdcal/feed/", "week", 0, 200);
178                         }
179                 }
180         } catch (DAVVersionMismatchException $e) {
181                 $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.");
182         }
183         return $x;
184 }
185
186
187 /**
188  * @param App $a
189  * @param object $b
190  */
191 function dav_event_created_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_event_updated_hook(&$a, &$b)
204 {
205         dav_include_files();
206         // @TODO Updating the cache instead of completely invalidating and rebuilding it
207         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
208         Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
209 }
210
211 /**
212  * @param App $a
213  * @param object $b
214  */
215 function dav_profile_tabs_hook(&$a, &$b)
216 {
217         $b["tabs"][] = array(
218                 "label" => t('Calendar'),
219                 "url"   => $a->get_baseurl() . "/dav/wdcal/",
220                 "sel"   => "",
221                 "title" => t('Extended calendar with CalDAV-support'),
222         );
223 }
224
225
226 /**
227  * @param App $a
228  * @param object $b
229  */
230 function dav_cron(&$a, &$b)
231 {
232         dav_include_files();
233
234         $r = q("SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW()", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
235         foreach ($r as $not) {
236                 q("UPDATE %s%snotifications SET `notified` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["id"]);
237                 $event    = q("SELECT * FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendarobject_id"]);
238                 $calendar = q("SELECT * FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendar_id"]);
239                 $users    = array();
240                 if (count($calendar) != 1 || count($event) == 0) continue;
241                 switch ($calendar[0]["namespace"]) {
242                         case CALDAV_NAMESPACE_PRIVATE:
243                                 $user = q("SELECT * FROM user WHERE `uid` = %d AND `blocked` = 0", $calendar[0]["namespace_id"]);
244                                 if (count($user) != 1) continue;
245                                 $users[] = $user[0];
246                                 break;
247                 }
248                 switch ($not["action"]) {
249                         case "email":
250                         case "display": // @TODO implement "Display"
251                                 foreach ($users as $user) {
252                                         $find      = array("%to%", "%event%", "%url%");
253                                         $repl      = array($user["username"], $event[0]["Summary"], $a->get_baseurl() . "/dav/wdcal/" . $calendar[0]["id"] . "/" . $not["calendarobject_id"] . "/");
254                                         $text_text = str_replace($find, $repl, "Hi %to%!\n\nThe event \"%event%\" is about to begin:\n%url%");
255                                         $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>");
256                                         $params    = array(
257                                                 'fromName'             => FRIENDICA_PLATFORM,
258                                                 'fromEmail'            => t('noreply') . '@' . $a->get_hostname(),
259                                                 'replyTo'              => t('noreply') . '@' . $a->get_hostname(),
260                                                 'toEmail'              => $user["email"],
261                                                 'messageSubject'       => t("Notification: " . $event[0]["Summary"]),
262                                                 'htmlVersion'          => $text_html,
263                                                 'textVersion'          => $text_text,
264                                                 'additionalMailHeader' => "",
265                                         );
266                                         require_once('include/enotify.php');
267                                         enotify::send($params);
268                                 }
269                                 break;
270                 }
271         }
272 }
273
274
275 /**
276  * @param App $a
277  * @param null|object $o
278  */
279 function dav_plugin_admin_post(&$a = null, &$o = null)
280 {
281         check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
282
283         dav_include_files();
284         require_once(__DIR__ . "/database-init.inc.php");
285
286         if (isset($_REQUEST["install"])) {
287                 $errs = dav_create_tables();
288                 if (count($errs) == 0) info(t('The database tables have been installed.') . EOL);
289                 else notice(t("An error occurred during the installation.") . EOL);
290         }
291         if (isset($_REQUEST["upgrade"])) {
292                 $errs = dav_upgrade_tables();
293                 if (count($errs) == 0) {
294                         renderAllCalDavEntries();
295                         info(t('The database tables have been updated.') . EOL);
296                 }
297                 else notice(t("An error occurred during the update.") . EOL);
298         }
299 }
300
301 /**
302  * @param App $a
303  * @param string $o
304  */
305 function dav_plugin_admin(&$a, &$o)
306 {
307         dav_include_files();
308         require_once(__DIR__ . "/database-init.inc.php");
309
310         $dbstatus = dav_check_tables();
311
312         $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("dav_admin_save") . '">';
313         $o .= '<i>' . t("No system-wide settings yet.") . '</i><br><br>';
314
315
316         $o .= '<h3>' . t('Database status') . '</h3>';
317         switch ($dbstatus) {
318                 case 0:
319                         $o .= t('Installed');
320                         break;
321                 case 1:
322                         $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') . "'>";
323                         break;
324                 case -1:
325                         $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
326                         break;
327                 case -2:
328                 default:
329                         $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.");
330                         break;
331         }
332         $o .= "<br><br>";
333
334         $o .= "<h3>" . t("Troubleshooting") . "</h3>";
335         $o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
336         $o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
337         switch ($dbstatus) {
338                 case 1:
339                         $tables = dav_get_update_statements(1);
340                         foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
341                         break;
342                 default:
343                         $tables = dav_get_create_statements();
344                         foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
345                         break;
346         }
347         $o .= "</pre></blockquote>";
348 }