+/**
+ * @param int $calendar_id
+ * @return string
+ */
+function wdcal_import_user_ics($calendar_id) {
+ $a = get_app();
+ $calendar_id = IntVal($calendar_id);
+ $o = "";
+
+ $server = dav_create_server(true, true, false);
+ $calendar = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_WRITE);
+ if (!$calendar) goaway($a->get_baseurl() . "/dav/wdcal/");
+
+ if (isset($_REQUEST["save"])) {
+ check_form_security_token_redirectOnErr('/dav/settings/', 'icsimport');
+
+ if ($_FILES["ics_file"]["tmp_name"] != "" && is_uploaded_file($_FILES["ics_file"]["tmp_name"])) try {
+ $text = file_get_contents($_FILES["ics_file"]["tmp_name"]);
+
+ /** @var Sabre_VObject_Component_VCalendar $vObject */
+ $vObject = Sabre_VObject_Reader::read($text);
+ $comp = $vObject->getComponents();
+ $imported = array();
+ foreach ($comp as $c) try {
+ /** @var Sabre_VObject_Component_VEvent $c */
+ $uid = $c->__get("UID")->value;
+ if (!isset($imported[$uid])) $imported[$uid] = "";
+ $imported[$uid] .= $c->serialize();
+ } catch (Exception $e) {
+ notice(t("Something went wrong when trying to import the file. Sorry. Maybe some events were imported anyway."));
+ }
+
+ if (isset($_REQUEST["overwrite"])) {
+ $children = $calendar->getChildren();
+ foreach ($children as $child) {
+ /** @var Sabre_CalDAV_CalendarObject $child */
+ $child->delete();
+ }
+ $i = 1;
+ } else {
+ $i = 0;
+ $children = $calendar->getChildren();
+ foreach ($children as $child) {
+ /** @var Sabre_CalDAV_CalendarObject $child */
+ $name = $child->getName();
+ if (preg_match("/import\-([0-9]+)\.ics/siu", $name, $matches)) {
+ if ($matches[1] > $i) $i = $matches[1];
+ };
+ }
+ $i++;
+ }
+
+ foreach ($imported as $object) try {
+
+ $str = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n";
+ $str .= trim($object);
+ $str .= "\r\nEND:VCALENDAR\r\n";
+
+ $calendar->createFile("import-" . $i . ".ics", $str);
+ $i++;
+ } catch (Exception $e) {
+ notice(t("Something went wrong when trying to import the file. Sorry."));
+ }
+
+ $o = t("The ICS-File has been imported.");
+ } catch (Exception $e) {
+ notice(t("Something went wrong when trying to import the file. Sorry. Maybe some events were imported anyway."));
+ } else {
+ notice(t("No file was uploaded."));
+ }
+ }
+
+
+ $o .= "<a href='" . $a->get_baseurl() . "/dav/wdcal/'>" . t("Go back to the calendar") . "</a><br><br>";
+
+ $num = q("SELECT COUNT(*) num FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendar_id);
+
+ $o .= "<h2>" . t("Import a ICS-file") . "</h2>";
+ $o .= '<form method="POST" action="' . $a->get_baseurl() . '/dav/wdcal/' . $calendar_id . '/ics-import/" enctype="multipart/form-data">';
+ $o .= "<input type='hidden' name='form_security_token' value='" . get_form_security_token('icsimport') . "'>\n";
+ $o .= "<label for='ics_file'>" . t("ICS-File") . "</label><input type='file' name='ics_file' id='ics_file'><br>\n";
+ if ($num[0]["num"] > 0) $o .= "<label for='overwrite'>" . str_replace("#num#", $num[0]["num"], t("Overwrite all #num# existing events")) . "</label> <input name='overwrite' id='overwrite' type='checkbox'><br>\n";
+ $o .= "<input type='submit' name='save' value='" . t("Upload") . "'>";
+ $o .= '</form>';
+
+ return $o;
+}
+
+