]> git.mxchange.org Git - friendica-addons.git/blob - dav/main.php
Adding and removing calendars
[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 }
11
12
13 function dav_uninstall()
14 {
15         unregister_hook('event_created', 'addon/dav/dav.php', 'dav_event_created_hook');
16         unregister_hook('event_updated', 'addon/dav/dav.php', 'dav_event_updated_hook');
17         unregister_hook('profile_tabs', 'addon/dav/dav.php', 'dav_profile_tabs_hook');
18 }
19
20
21 function dav_module()
22 {
23 }
24
25 function dav_include_files()
26 {
27         /*
28                         require_once (__DIR__ . "/SabreDAV/lib/Sabre.includes.php");
29                         require_once (__DIR__ . "/SabreDAV/lib/Sabre/VObject/includes.php");
30                         require_once (__DIR__ . "/SabreDAV/lib/Sabre/DAVACL/includes.php");
31                         require_once (__DIR__ . "/SabreDAV/lib/Sabre/CalDAV/includes.php");
32                         */
33         require_once (__DIR__ . "/SabreDAV/lib/Sabre/autoload.php");
34
35         require_once (__DIR__ . "/common/calendar.fnk.php");
36         require_once (__DIR__ . "/common/calendar_rendering.fnk.php");
37         require_once (__DIR__ . "/common/dav_caldav_backend_common.inc.php");
38         require_once (__DIR__ . "/common/dav_caldav_backend_private.inc.php");
39         require_once (__DIR__ . "/common/dav_caldav_backend_virtual.inc.php");
40         require_once (__DIR__ . "/common/dav_caldav_root.inc.php");
41         require_once (__DIR__ . "/common/dav_user_calendars.inc.php");
42         require_once (__DIR__ . "/common/dav_carddav_root.inc.php");
43         require_once (__DIR__ . "/common/dav_carddav_backend_std.inc.php");
44         require_once (__DIR__ . "/common/dav_user_addressbooks.inc.php");
45         require_once (__DIR__ . "/common/dav_caldav_calendar_virtual.inc.php");
46         require_once (__DIR__ . "/common/wdcal_configuration.php");
47         require_once (__DIR__ . "/common/wdcal_backend.inc.php");
48
49         require_once (__DIR__ . "/dav_friendica_principal.inc.php");
50         require_once (__DIR__ . "/dav_friendica_auth.inc.php");
51         require_once (__DIR__ . "/dav_carddav_backend_virtual_friendica.inc.php");
52         require_once (__DIR__ . "/dav_caldav_backend_virtual_friendica.inc.php");
53         require_once (__DIR__ . "/FriendicaACLPlugin.inc.php");
54
55         require_once (__DIR__ . "/common/wdcal_edit.inc.php");
56         require_once (__DIR__ . "/calendar.friendica.fnk.php");
57         require_once (__DIR__ . "/layout.fnk.php");
58 }
59
60
61 /**
62  * @param App $a
63  */
64 function dav_init(&$a)
65 {
66
67         /*
68          * Recommended settings:
69          * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
70          */
71
72         dav_include_files();
73
74         if (true) {
75                 dbg(true);
76                 error_reporting(E_ALL);
77                 ini_set("display_errors", 1);
78         }
79
80         wdcal_create_std_calendars();
81         wdcal_addRequiredHeaders();
82
83         if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
84
85                 if ($a->argc >= 3 && $a->argv[2] == "feed") {
86                         wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
87                         killme();
88                 }
89                 return;
90         }
91         if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
92                 echo wdcal_getEditPage_exception_selector();
93                 killme();
94         }
95
96         if ($a->argc >= 2 && $a->argv[1] == "settings") {
97                 return;
98         }
99
100
101         if (isset($_REQUEST["test"])) {
102                 renderAllCalDavEntries();
103         }
104
105
106         $server = dav_create_server();
107         $browser = new Sabre_DAV_Browser_Plugin();
108         $server->addPlugin($browser);
109         $server->exec();
110
111         killme();
112 }
113
114 /**
115  * @return string
116  */
117 function dav_content()
118 {
119         $a = get_app();
120         if (!isset($a->user["uid"]) || $a->user["uid"] == 0) {
121                 return login();
122         }
123
124         $x = "";
125
126         if ($a->argv[1] == "settings") {
127                 return wdcal_getSettingsPage($a);
128         } elseif ($a->argv[1] == "wdcal") {
129                 if (isset($a->argv[2]) && strlen($a->argv[2]) > 0) {
130                         if ($a->argv[2] == "ics") {
131                                 wdcal_print_user_ics();
132                         } elseif ($a->argv[2] == "new") {
133                                 $o = "";
134                                 if (isset($_REQUEST["save"])) {
135                                         check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
136                                         $ret = wdcal_postEditPage("new", "", $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
137                                         if ($ret["ok"]) notice($ret["msg"]);
138                                         else info($ret["msg"]);
139                                         goaway($a->get_baseurl() . "/dav/wdcal/");
140                                 }
141                                 $o .= wdcal_getNewPage();
142                                 return $o;
143                         } else {
144                                 $calendar_id = IntVal($a->argv[2]);
145                                 if (isset($a->argv[3]) && $a->argv[3] > 0) {
146                                         $recurr_uri = ""; // @TODO
147                                         if (isset($a->argv[4]) && $a->argv[4] == "edit") {
148                                                 $o = "";
149                                                 if (isset($_REQUEST["save"])) {
150                                                         check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
151                                                         $ret = wdcal_postEditPage($a->argv[3], $recurr_uri, $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
152                                                         if ($ret["ok"]) notice($ret["msg"]);
153                                                         else info($ret["msg"]);
154                                                         goaway($a->get_baseurl() . "/dav/wdcal/");
155                                                 }
156                                                 $o .= wdcal_getEditPage($calendar_id, $a->argv[3], $recurr_uri);
157                                                 return $o;
158                                         } else {
159                                                 return wdcal_getDetailPage($calendar_id, $a->argv[3], $recurr_uri);
160                                         }
161                                 } else {
162                                         // @TODO Edit Calendar
163                                 }
164                         }
165                 } else {
166                         $server = dav_create_server(true, true, false);
167                         $cals = dav_get_current_user_calendars($server, DAV_ACL_READ);
168                         $x = wdcal_printCalendar($cals, array(), $a->get_baseurl() . "/dav/wdcal/feed/", "week", 0, 200);
169                 }
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  * @param App $a
215  * @param null|object $o
216  */
217 function dav_plugin_admin_post(&$a = null, &$o = null)
218 {
219         check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
220
221         dav_include_files();
222         require_once(__DIR__ . "/database-init.inc.php");
223
224         if (isset($_REQUEST["install"])) {
225                 $errs = dav_create_tables();
226                 if (count($errs) == 0) info(t('The database tables have been installed.') . EOL);
227                 else notice(t("An error occurred during the installation.") . EOL);
228         }
229         if (isset($_REQUEST["upgrade"])) {
230                 $errs = dav_upgrade_tables();
231                 if (count($errs) == 0) info(t('The database tables have been updated.') . EOL);
232                 else notice(t("An error occurred during the update.") . EOL);
233         }
234 }
235
236 /**
237  * @param App $a
238  * @param string $o
239  */
240 function dav_plugin_admin(&$a, &$o)
241 {
242         dav_include_files();
243         require_once(__DIR__ . "/database-init.inc.php");
244
245         $dbstatus = dav_check_tables();
246
247         $o = '<input type="hidden" name="form_security_token" value="' . get_form_security_token("dav_admin_save") . '">';
248         $o .= '<i>' . t("No system-wide settings yet.") . '</i><br><br>';
249
250
251         $o .= '<h3>' . t('Database status') . '</h3>';
252         switch ($dbstatus) {
253                 case 0:
254                         $o .= t('Installed');
255                         break;
256                 case 1:
257                         $o .= t('Upgrade needed') . "<br><br><input type='submit' name='upgrade' value='" . t('Upgrade') . "'>";
258                         break;
259                 case -1:
260                         $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
261                         break;
262         }
263         $o .= "<br><br>";
264
265         $o .= "<h3>" . t("Troubleshooting") . "</h3>";
266         $o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
267         $o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
268         $tables = dav_get_create_statements();
269         foreach ($tables as $t) $o .= escape_tags($t . "\n\n");
270         $o .= "</pre></blockquote>";
271 }