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