]> git.mxchange.org Git - friendica-addons.git/blobdiff - dav/main.php
More refactoring, mainly of the addressbook/VCARD-part
[friendica-addons.git] / dav / main.php
index b71ef61965512f4b00a9c611df035ae8a3b6bf10..c36d00840a5eac1cd1d8f03b336116a19ca227be 100644 (file)
@@ -7,6 +7,7 @@ function dav_install()
        register_hook('event_created', 'addon/dav/dav.php', 'dav_event_created_hook');
        register_hook('event_updated', 'addon/dav/dav.php', 'dav_event_updated_hook');
        register_hook('profile_tabs', 'addon/dav/dav.php', 'dav_profile_tabs_hook');
+       register_hook('cron', 'addon/dav/dav.php', 'dav_cron');
 }
 
 
@@ -15,6 +16,7 @@ function dav_uninstall()
        unregister_hook('event_created', 'addon/dav/dav.php', 'dav_event_created_hook');
        unregister_hook('event_updated', 'addon/dav/dav.php', 'dav_event_updated_hook');
        unregister_hook('profile_tabs', 'addon/dav/dav.php', 'dav_profile_tabs_hook');
+       unregister_hook('cron', 'addon/dav/dav.php', 'dav_cron');
 }
 
 
@@ -22,59 +24,57 @@ function dav_module()
 {
 }
 
-
-/**
- * @param App $a
- */
-function dav_init(&$a)
+function dav_include_files()
 {
-
-       /*
-        * Recommended settings:
-        * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
-        */
-
-       require_once (__DIR__ . "/common/dbclasses/dbclass_animexx.class.php");
-       require_once (__DIR__ . "/common/dbclasses/dbclass.friendica.calendars.class.php");
-       require_once (__DIR__ . "/common/dbclasses/dbclass.friendica.jqcalendar.class.php");
-       require_once (__DIR__ . "/common/dbclasses/dbclass.friendica.notifications.class.php");
-       require_once (__DIR__ . "/common/dbclasses/dbclass.friendica.calendarobjects.class.php");
-
-       /*
-       require_once (__DIR__ . "/SabreDAV/lib/Sabre.includes.php");
-       require_once (__DIR__ . "/SabreDAV/lib/Sabre/VObject/includes.php");
-       require_once (__DIR__ . "/SabreDAV/lib/Sabre/DAVACL/includes.php");
-       require_once (__DIR__ . "/SabreDAV/lib/Sabre/CalDAV/includes.php");
-       */
        require_once (__DIR__ . "/SabreDAV/lib/Sabre/autoload.php");
 
-       $tz_before = date_default_timezone_get();
-       require_once (__DIR__ . "/iCalcreator/iCalcreator.class.php");
-       date_default_timezone_set($tz_before);
-
        require_once (__DIR__ . "/common/calendar.fnk.php");
+       require_once (__DIR__ . "/common/calendar_rendering.fnk.php");
+
        require_once (__DIR__ . "/common/dav_caldav_backend_common.inc.php");
-       require_once (__DIR__ . "/common/dav_caldav_backend.inc.php");
+       require_once (__DIR__ . "/common/dav_caldav_backend_private.inc.php");
+       require_once (__DIR__ . "/common/dav_caldav_backend_virtual.inc.php");
        require_once (__DIR__ . "/common/dav_caldav_root.inc.php");
        require_once (__DIR__ . "/common/dav_user_calendars.inc.php");
+       require_once (__DIR__ . "/common/dav_caldav_calendar_virtual.inc.php");
+       require_once (__DIR__ . "/common/dav_caldav_calendar_private.inc.php");
+
        require_once (__DIR__ . "/common/dav_carddav_root.inc.php");
-       require_once (__DIR__ . "/common/dav_carddav_backend_std.inc.php");
+       require_once (__DIR__ . "/common/dav_carddav_backend_common.inc.php");
+       require_once (__DIR__ . "/common/dav_carddav_backend_virtual.inc.php");
+       require_once (__DIR__ . "/common/dav_carddav_backend_private.inc.php");
        require_once (__DIR__ . "/common/dav_user_addressbooks.inc.php");
-       require_once (__DIR__ . "/common/virtual_cal_source_backend.inc.php");
+
        require_once (__DIR__ . "/common/wdcal_configuration.php");
-       require_once (__DIR__ . "/common/wdcal_cal_source.inc.php");
-       require_once (__DIR__ . "/common/wdcal_cal_source_private.inc.php");
+       require_once (__DIR__ . "/common/wdcal_backend.inc.php");
 
        require_once (__DIR__ . "/dav_friendica_principal.inc.php");
        require_once (__DIR__ . "/dav_friendica_auth.inc.php");
-       require_once (__DIR__ . "/dav_carddav_backend_friendica_community.inc.php");
-       require_once (__DIR__ . "/dav_caldav_backend_friendica.inc.php");
-       require_once (__DIR__ . "/virtual_cal_source_friendica.inc.php");
-       require_once (__DIR__ . "/wdcal_cal_source_friendicaevents.inc.php");
+       require_once (__DIR__ . "/dav_carddav_backend_virtual_friendica.inc.php");
+       require_once (__DIR__ . "/dav_caldav_backend_virtual_friendica.inc.php");
        require_once (__DIR__ . "/FriendicaACLPlugin.inc.php");
 
+       require_once (__DIR__ . "/common/wdcal_edit.inc.php");
        require_once (__DIR__ . "/calendar.friendica.fnk.php");
        require_once (__DIR__ . "/layout.fnk.php");
+}
+
+
+/**
+ * @param App $a
+ */
+function dav_init(&$a)
+{
+
+       /*
+        * Recommended settings:
+        * ALTER TABLE `photo` ADD INDEX ( `contact-id` )
+        */
+
+       ini_set("display_errors", 1);
+       error_reporting(E_ALL);
+
+       dav_include_files();
 
        if (false) {
                dbg(true);
@@ -83,68 +83,33 @@ function dav_init(&$a)
        }
 
        wdcal_create_std_calendars();
-
+       wdcal_create_std_addressbooks();
+       wdcal_addRequiredHeaders();
 
        if ($a->argc >= 2 && $a->argv[1] == "wdcal") {
 
                if ($a->argc >= 3 && $a->argv[2] == "feed") {
                        wdcal_print_feed($a->get_baseurl() . "/dav/wdcal/");
                        killme();
-               } elseif ($a->argc >= 3 && strlen($a->argv[2]) > 0) {
-                       wdcal_addRequiredHeadersEdit();
-               } else {
-                       wdcal_addRequiredHeaders();
                }
                return;
        }
+       if ($a->argc >= 2 && $a->argv[1] == "getExceptionDates") {
+               echo wdcal_getEditPage_exception_selector();
+               killme();
+       }
 
        if ($a->argc >= 2 && $a->argv[1] == "settings") {
                return;
        }
 
-       $authBackend              = new Sabre_DAV_Auth_Backend_Friendica();
-       $principalBackend         = new Sabre_DAVACL_PrincipalBackend_Friendica($authBackend);
-       $caldavBackend_std        = new Sabre_CalDAV_Backend_Std();
-       $caldavBackend_community  = new Sabre_CalDAV_Backend_Friendica();
-       $carddavBackend_std       = new Sabre_CardDAV_Backend_Std();
-       $carddavBackend_community = new Sabre_CardDAV_Backend_FriendicaCommunity();
-
-       if (isset($_SERVER["PHP_AUTH_USER"])) {
-               $tree = new Sabre_DAV_SimpleCollection('root', array(
-                       new Sabre_DAV_SimpleCollection('principals', array(
-                               new Sabre_CalDAV_Principal_Collection($principalBackend, "principals/users"),
-                       )),
-                       new Sabre_CalDAV_AnimexxCalendarRootNode($principalBackend, array(
-                               $caldavBackend_std,
-                               $caldavBackend_community,
-                       )),
-                       new Sabre_CardDAV_AddressBookRootFriendica($principalBackend, array(
-                               $carddavBackend_std,
-                               $carddavBackend_community,
-                       )),
-               ));
-       } else {
-               $tree = new Sabre_DAV_SimpleCollection('root', array());
-       }
-
-// The object tree needs in turn to be passed to the server class
-       $server = new Sabre_DAV_Server($tree);
-
-       $url = parse_url($a->get_baseurl());
-       $server->setBaseUri(CALDAV_URL_PREFIX);
 
-       $authPlugin = new Sabre_DAV_Auth_Plugin($authBackend, 'SabreDAV');
-       $server->addPlugin($authPlugin);
-
-       $aclPlugin                      = new Sabre_DAVACL_Plugin_Friendica();
-       $aclPlugin->defaultUsernamePath = "principals/users";
-       $server->addPlugin($aclPlugin);
+       if (isset($_REQUEST["test"])) {
+               renderAllCalDavEntries();
+       }
 
-       $caldavPlugin = new Sabre_CalDAV_Plugin();
-       $server->addPlugin($caldavPlugin);
 
-       $carddavPlugin = new Sabre_CardDAV_Plugin();
-       $server->addPlugin($carddavPlugin);
+       $server  = dav_create_server();
 
        $browser = new Sabre_DAV_Browser_Plugin();
        $server->addPlugin($browser);
@@ -165,27 +130,55 @@ function dav_content()
        }
 
        $x = "";
-
-       if ($a->argv[1] == "settings") {
-               return wdcal_getSettingsPage($a);
-       } elseif ($a->argv[1] == "wdcal") {
-               if ($a->argc >= 3 && strlen($a->argv[2]) > 0) {
-                       $uri        = $a->argv[2];
-                       $recurr_uri = ""; // @TODO
-                       if (isset($a->argv[3]) && $a->argv[3] == "edit") {
-                               $o = "";
-                               if (isset($_REQUEST["save"])) $o .= wdcal_postEditPage($uri, $recurr_uri);
-                               $o .= wdcal_getEditPage($uri, $recurr_uri);
-                               return $o;
+       try {
+               if ($a->argv[1] == "settings") {
+                       return wdcal_getSettingsPage($a);
+               } elseif ($a->argv[1] == "wdcal") {
+                       if (isset($a->argv[2]) && strlen($a->argv[2]) > 0) {
+                               if ($a->argv[2] == "new") {
+                                       $o = "";
+                                       if (isset($_REQUEST["save"])) {
+                                               check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
+                                               $ret = wdcal_postEditPage("new", "", $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
+                                               if ($ret["ok"]) notice($ret["msg"]);
+                                               else info($ret["msg"]);
+                                               goaway($a->get_baseurl() . "/dav/wdcal/");
+                                       }
+                                       $o .= wdcal_getNewPage();
+                                       return $o;
+                               } else {
+                                       $calendar_id = IntVal($a->argv[2]);
+                                       if (isset($a->argv[3]) && $a->argv[3] == "ics-export") {
+                                               wdcal_print_user_ics($calendar_id);
+                                       } elseif (isset($a->argv[3]) && $a->argv[3] == "ics-import") {
+                                               return wdcal_import_user_ics($calendar_id);
+                                       } elseif (isset($a->argv[3]) && $a->argv[3] > 0) {
+                                               if (isset($a->argv[4]) && $a->argv[4] == "edit") {
+                                                       $o = "";
+                                                       if (isset($_REQUEST["save"])) {
+                                                               check_form_security_token_redirectOnErr($a->get_baseurl() . "/dav/wdcal/", "caledit");
+                                                               $ret = wdcal_postEditPage($a->argv[3], $a->user["uid"], $a->timezone, $a->get_baseurl() . "/dav/wdcal/");
+                                                               if ($ret["ok"]) notice($ret["msg"]);
+                                                               else info($ret["msg"]);
+                                                               goaway($a->get_baseurl() . "/dav/wdcal/");
+                                                       }
+                                                       $o .= wdcal_getEditPage($calendar_id, $a->argv[3]);
+                                                       return $o;
+                                               } else {
+                                                       return wdcal_getDetailPage($calendar_id, $a->argv[3]);
+                                               }
+                                       } else {
+                                               // @TODO Edit Calendar
+                                       }
+                               }
                        } else {
-                               return wdcal_getDetailPage($uri, $recurr_uri);
+                               $server = dav_create_server(true, true, false);
+                               $cals   = dav_get_current_user_calendars($server, DAV_ACL_READ);
+                               $x      = wdcal_printCalendar($cals, array(), $a->get_baseurl() . "/dav/wdcal/feed/", "week", 0, 200);
                        }
-               } else {
-                       $cals      = dav_getMyCals($a->user["uid"]);
-                       $cals_show = array();
-                       foreach ($cals as $e) $cals_show[] = array("ns" => $e->namespace, "id" => $e->namespace_id, "displayname" => $e->displayname);
-                       $x = wdcal_printCalendar($cals, $cals_show, $a->get_baseurl() . "/dav/wdcal/feed/", "week", 0, 200);
                }
+       } catch (DAVVersionMismatchException $e) {
+               $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.");
        }
        return $x;
 }
@@ -197,9 +190,10 @@ function dav_content()
  */
 function dav_event_created_hook(&$a, &$b)
 {
+       dav_include_files();
        // @TODO Updating the cache instead of completely invalidating and rebuilding it
-       FriendicaVirtualCalSourceBackend::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
-       FriendicaVirtualCalSourceBackend::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
+       Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
+       Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
 }
 
 /**
@@ -208,9 +202,10 @@ function dav_event_created_hook(&$a, &$b)
  */
 function dav_event_updated_hook(&$a, &$b)
 {
+       dav_include_files();
        // @TODO Updating the cache instead of completely invalidating and rebuilding it
-       FriendicaVirtualCalSourceBackend::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
-       FriendicaVirtualCalSourceBackend::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
+       Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_CONTACTS);
+       Sabre_CalDAV_Backend_Friendica::invalidateCache($a->user["uid"], CALDAV_FRIENDICA_MINE);
 }
 
 /**
@@ -220,13 +215,63 @@ function dav_event_updated_hook(&$a, &$b)
 function dav_profile_tabs_hook(&$a, &$b)
 {
        $b["tabs"][] = array(
-       "label" => t('Calendar'),
-               "url" => $a->get_baseurl() . "/dav/wdcal/",
-               "sel" => "",
+               "label" => t('Calendar'),
+               "url"   => $a->get_baseurl() . "/dav/wdcal/",
+               "sel"   => "",
                "title" => t('Extended calendar with CalDAV-support'),
        );
 }
 
+
+/**
+ * @param App $a
+ * @param object $b
+ */
+function dav_cron(&$a, &$b)
+{
+       dav_include_files();
+
+       $r = q("SELECT * FROM %s%snotifications WHERE `notified` = 0 AND `alert_date` <= NOW()", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
+       foreach ($r as $not) {
+               q("UPDATE %s%snotifications SET `notified` = 1 WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["id"]);
+               $event    = q("SELECT * FROM %s%sjqcalendar WHERE `calendarobject_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendarobject_id"]);
+               $calendar = q("SELECT * FROM %s%scalendars WHERE `id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $not["calendar_id"]);
+               $users    = array();
+               if (count($calendar) != 1 || count($event) == 0) continue;
+               switch ($calendar[0]["namespace"]) {
+                       case CALDAV_NAMESPACE_PRIVATE:
+                               $user = q("SELECT * FROM user WHERE `uid` = %d AND `blocked` = 0", $calendar[0]["namespace_id"]);
+                               if (count($user) != 1) continue;
+                               $users[] = $user[0];
+                               break;
+               }
+               switch ($not["action"]) {
+                       case "email":
+                       case "display": // @TODO implement "Display"
+                               foreach ($users as $user) {
+                                       $find      = array("%to%", "%event%", "%url%");
+                                       $repl      = array($user["username"], $event[0]["Summary"], $a->get_baseurl() . "/dav/wdcal/" . $calendar[0]["id"] . "/" . $not["calendarobject_id"] . "/");
+                                       $text_text = str_replace($find, $repl, "Hi %to%!\n\nThe event \"%event%\" is about to begin:\n%url%");
+                                       $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>");
+                                       $params    = array(
+                                               'fromName'             => FRIENDICA_PLATFORM,
+                                               'fromEmail'            => t('noreply') . '@' . $a->get_hostname(),
+                                               'replyTo'              => t('noreply') . '@' . $a->get_hostname(),
+                                               'toEmail'              => $user["email"],
+                                               'messageSubject'       => t("Notification: " . $event[0]["Summary"]),
+                                               'htmlVersion'          => $text_html,
+                                               'textVersion'          => $text_text,
+                                               'additionalMailHeader' => "",
+                                       );
+                                       require_once('include/enotify.php');
+                                       enotify::send($params);
+                               }
+                               break;
+               }
+       }
+}
+
+
 /**
  * @param App $a
  * @param null|object $o
@@ -235,6 +280,7 @@ function dav_plugin_admin_post(&$a = null, &$o = null)
 {
        check_form_security_token_redirectOnErr('/admin/plugins/dav', 'dav_admin_save');
 
+       dav_include_files();
        require_once(__DIR__ . "/database-init.inc.php");
 
        if (isset($_REQUEST["install"])) {
@@ -242,15 +288,23 @@ function dav_plugin_admin_post(&$a = null, &$o = null)
                if (count($errs) == 0) info(t('The database tables have been installed.') . EOL);
                else notice(t("An error occurred during the installation.") . EOL);
        }
+       if (isset($_REQUEST["upgrade"])) {
+               $errs = dav_upgrade_tables();
+               if (count($errs) == 0) {
+                       renderAllCalDavEntries();
+                       info(t('The database tables have been updated.') . EOL);
+               }
+               else notice(t("An error occurred during the update.") . EOL);
+       }
 }
 
 /**
  * @param App $a
- * @param null|object $o
+ * @param string $o
  */
 function dav_plugin_admin(&$a, &$o)
 {
-
+       dav_include_files();
        require_once(__DIR__ . "/database-init.inc.php");
 
        $dbstatus = dav_check_tables();
@@ -265,18 +319,30 @@ function dav_plugin_admin(&$a, &$o)
                        $o .= t('Installed');
                        break;
                case 1:
-                       $o .= t('Upgrade needed') . "<br><br><input type='submit' name='upgrade' value='" . t('Upgrade') . "'>";
+                       $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') . "'>";
                        break;
                case -1:
                        $o .= t('Not installed') . "<br><br><input type='submit' name='install' value='" . t('Install') . "'>";
                        break;
+               case -2:
+               default:
+                       $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.");
+                       break;
        }
        $o .= "<br><br>";
 
        $o .= "<h3>" . t("Troubleshooting") . "</h3>";
        $o .= "<h4>" . t("Manual creation of the database tables:") . "</h4>";
        $o .= "<a href='#' onClick='\$(\"#sqlstatements\").show(); return false;'>" . t("Show SQL-statements") . "</a><blockquote style='display: none;' id='sqlstatements'><pre>";
-       $tables = dav_get_create_statements();
-       foreach ($tables as $t) $o .= escape_tags($t . "\n\n");
+       switch ($dbstatus) {
+               case 1:
+                       $tables = dav_get_update_statements(1);
+                       foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
+                       break;
+               default:
+                       $tables = dav_get_create_statements();
+                       foreach ($tables as $t) $o .= escape_tags($t . ";\n\n");
+                       break;
+       }
        $o .= "</pre></blockquote>";
 }