page['htmlhead'] .= ' ' . "\r\n";
$a->page['htmlhead'] .= '' . "\r\n";
$a->page['htmlhead'] .= ' ' . "\r\n";
$a->page['htmlhead'] .= '' . "\r\n";
$a->page['htmlhead'] .= ' ' . "\r\n";
$a->page['htmlhead'] .= '' . "\r\n";
$a->page['htmlhead'] .= ' ' . "\r\n";
$a->page['htmlhead'] .= '' . "\r\n";
$a->page['htmlhead'] .= ' ' . "\r\n";
$a->page['htmlhead'] .= ' ' . "\r\n";
switch (Config::get("system", "language")) {
case "de":
$a->page['htmlhead'] .= '' . "\r\n";
$a->page['htmlhead'] .= '' . "\r\n";
break;
default:
$a->page['htmlhead'] .= '' . "\r\n";
}
$a->page['htmlhead'] .= '' . "\r\n";
$a->page['htmlhead'] .= '' . "\r\n";
}
/**
* @param int $calendar_id
*/
function wdcal_print_user_ics($calendar_id)
{
$calendar_id = IntVal($calendar_id);
$a = get_app();
header("Content-type: text/plain");
$str = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n";
$cals = q("SELECT * FROM %s%scalendars WHERE `id` = %d AND `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendar_id, CALDAV_NAMESPACE_PRIVATE, $a->user["uid"]);
if (count($cals) > 0) {
$objs = q("SELECT * FROM %s%scalendarobjects WHERE `calendar_id` = %d ORDER BY `firstOccurence`", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendar_id);
foreach ($objs as $obj) {
preg_match("/BEGIN:VEVENT(.*)END:VEVENT/siu", $obj["calendardata"], $matches);
$str2 = preg_replace("/([^\\r])\\n/siu", "\\1\r\n", $matches[0]);
$str2 = preg_replace("/MAILTO:.*[^:a-z0-9_\+äöüß\\n\\n@-]+.*(:|\\r\\n[^ ])/siU", "\\1", $str2);
$str .= $str2 . "\r\n";
}
}
$str .= "END:VCALENDAR\r\n";
echo $str;
killme();
}
/**
* @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('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 = [];
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(L10n::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(L10n::t("Something went wrong when trying to import the file. Sorry."));
}
$o = L10n::t("The ICS-File has been imported.");
} catch (Exception $e) {
notice(L10n::t("Something went wrong when trying to import the file. Sorry. Maybe some events were imported anyway."));
} else {
notice(L10n::t("No file was uploaded."));
}
}
$o .= "" . L10n::t("Go back to the calendar") . " ";
$num = q("SELECT COUNT(*) num FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendar_id);
$o .= "
" . L10n::t("Import a ICS-file") . " ";
$o .= '';
return $o;
}
/**
* @param array|Sabre_CalDAV_Calendar[] $calendars
* @param array|int[] $calendars_selected
* @param string $data_feed_url
* @param string $view
* @param int $theme
* @param int $height_diff
* @param bool $readonly
* @param string $curr_day
* @param array $add_params
* @param bool $show_nav
* @return string
*/
function wdcal_printCalendar($calendars, $calendars_selected, $data_feed_url, $view = "week", $theme = 0, $height_diff = 175, $readonly = false, $curr_day = "", $add_params = [], $show_nav = true)
{
$a = get_app();
$localization = wdcal_local::getInstanceByUser($a->user["uid"]);
if (count($calendars_selected) == 0) foreach ($calendars as $c) {
$prop = $c->getProperties(["id"]);
$calendars_selected[] = $prop["id"];
}
$opts = [
"view" => $view,
"theme" => $theme,
"readonly" => $readonly,
"height_diff" => $height_diff,
"weekstartday" => $localization->getFirstDayOfWeek(),
"data_feed_url" => $data_feed_url,
"date_format_dm1" => $localization->dateformat_js_dm1(),
"date_format_dm2" => $localization->dateformat_js_dm2(),
"date_format_dm3" => $localization->dateformat_js_dm3(),
"date_format_full" => $localization->dateformat_datepicker_js(),
"baseurl" => $a->get_baseurl() . "/dav/wdcal/",
];
$x = '
Available Calendars: ';
foreach ($calendars as $cal) {
$cal_id = $cal->getProperties(["id", DAV_DISPLAYNAME]);
$x .= ' ';
}
$x .= '
Sorry, could not load your data, please try again later
';
if ($show_nav) {
$x .= '
' . L10n::t("Date") . '
';
}
$x .= '
';
return $x;
}
/**
* @param int $calendar_id
* @param int $calendarobject_id
* @return string
*/
function wdcal_getDetailPage($calendar_id, $calendarobject_id)
{
$a = get_app();
try {
$details = null;
$server = dav_create_server(true, true, false);
$cal = dav_get_current_user_calendar_by_id($server, $calendar_id, DAV_ACL_READ);
$obj = Sabre_CalDAV_Backend_Common::loadCalendarobjectById($calendarobject_id);
dav_get_current_user_calendarobject($server, $cal, $obj["uri"], DAV_ACL_READ); // Check permissions
$calbackend = wdcal_calendar_factory_by_id($calendar_id);
$redirect = $calbackend->getItemDetailRedirect($calendar_id, $calendarobject_id);
if ($redirect !== null) goaway($redirect);
$details = $obj;
} catch (Exception $e) {
info(L10n::t("Error") . ": " . $e);
goaway('dav/wdcal/');
}
return print_r($details, true);
}
/**
* @param int $calendar_id
* @param int $uri
* @return string
*/
function wdcal_getEditPage($calendar_id, $uri)
{
$a = get_app();
$localization = wdcal_local::getInstanceByUser($a->user["uid"]);
return wdcal_getEditPage_str($localization, $a->get_baseurl(), $calendar_id, $uri);
}
/**
* @return string
*/
function wdcal_getNewPage()
{
$a = get_app();
$localization = wdcal_local::getInstanceByUser($a->user["uid"]);
return wdcal_getEditPage_str($localization, $a->get_baseurl(), 0, 0);
}
/**
* @param App $a
* @return string
*/
function wdcal_getSettingsPage(&$a)
{
if (!local_user()) {
notice(L10n::t('Permission denied.') . EOL);
return '';
}
if (isset($_REQUEST["save"])) {
check_form_security_token_redirectOnErr('/dav/settings/', 'calprop');
PConfig::set($a->user["uid"], "dav", "dateformat", $_REQUEST["wdcal_date_format"]);
info(L10n::t('The new values have been saved.'));
}
if (isset($_REQUEST["save_cals"])) {
check_form_security_token_redirectOnErr('/dav/settings/', 'calprop');
$r = q("SELECT * FROM %s%scalendars WHERE `namespace` = " . CALDAV_NAMESPACE_PRIVATE . " AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($a->user["uid"]));
foreach ($r as $cal) {
$backend = wdcal_calendar_factory($cal["namespace"], $cal["namespace_id"], $cal["uri"], $cal);
$change_sql = "";
$col = substr($_REQUEST["color"][$cal["id"]], 1);
if (strtolower($col) != strtolower($cal["calendarcolor"])) $change_sql .= ", `calendarcolor` = '" . dbesc($col) . "'";
if (!is_subclass_of($backend, "Sabre_CalDAV_Backend_Virtual")) {
if ($_REQUEST["uri"][$cal["id"]] != $cal["uri"]) $change_sql .= ", `uri` = '" . dbesc($_REQUEST["uri"][$cal["id"]]) . "'";
if ($_REQUEST["name"][$cal["id"]] != $cal["displayname"]) $change_sql .= ", `displayname` = '" . dbesc($_REQUEST["name"][$cal["id"]]) . "'";
}
if ($change_sql != "") {
q("UPDATE %s%scalendars SET `ctag` = `ctag` + 1 $change_sql WHERE `id` = %d AND `namespace_id` = %d AND `namespace_id` = %d",
CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $cal["id"], CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]));
info(L10n::t('The calendar has been updated.'));
}
}
if (isset($_REQUEST["uri"]["new"]) && $_REQUEST["uri"]["new"] != "" && $_REQUEST["name"]["new"] && $_REQUEST["name"]["new"] != "") {
$order = q("SELECT MAX(`calendarorder`) ord FROM %s%scalendars WHERE `namespace_id` = %d AND `namespace_id` = %d",
CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]));
$neworder = $order[0]["ord"] + 1;
q("INSERT INTO %s%scalendars (`namespace`, `namespace_id`, `calendarorder`, `calendarcolor`, `displayname`, `timezone`, `uri`, `has_vevent`, `ctag`)
VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', 1, 1)",
CALDAV_SQL_DB, CALDAV_SQL_PREFIX, CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]), $neworder, dbesc(strtolower(substr($_REQUEST["color"]["new"], 1))),
dbesc($_REQUEST["name"]["new"]), dbesc($a->timezone), dbesc($_REQUEST["uri"]["new"])
);
info(L10n::t('The new calendar has been created.'));
}
}
if (isset($_REQUEST["remove_cal"])) {
check_form_security_token_redirectOnErr('/dav/settings/', 'del_cal', 't');
$c = q("SELECT * FROM %s%scalendars WHERE `id` = %d AND `namespace_id` = %d AND `namespace_id` = %d",
CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($_REQUEST["remove_cal"]), CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]));
if (count($c) != 1) killme();
$calobjs = q("SELECT `id` FROM %s%scalendarobjects WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($_REQUEST["remove_cal"]));
$newcal = q("SELECT * FROM %s%scalendars WHERE `id` != %d AND `namespace_id` = %d AND `namespace_id` = %d ORDER BY `calendarcolor` LIMIT 0,1",
CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($_REQUEST["remove_cal"]), CALDAV_NAMESPACE_PRIVATE, IntVal($a->user["uid"]));
if (count($newcal) != 1) killme();
q("UPDATE %s%scalendarobjects SET `calendar_id` = %d WHERE `calendar_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($newcal[0]["id"]), IntVal($c[0]["id"]));
foreach ($calobjs as $calobj) renderCalDavEntry_calobj_id($calobj["id"]);
q("DELETE FROM %s%scalendars WHERE `id` = %s", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($_REQUEST["remove_cal"]));
q("UPDATE %s%scalendars SET `ctag` = `ctag` + 1 WHERE `id` = " . CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $newcal[0]["id"]);
info(L10n::t('The calendar has been deleted.'));
}
$o = "";
$o .= "" . L10n::t("Go back to the calendar") . " ";
$o .= '' . L10n::t('Calendar Settings') . ' ';
$current_format = wdcal_local::getInstanceByUser($a->user["uid"]);
$o .= '';
$o .= '' . L10n::t('Calendars') . ' ';
$o .= '';
$baseurl = $a->get_baseurl();
$o .= "";
$o .= "" . L10n::t("Limitations") . " ";
$o .= "- The native friendica events are embedded as read-only, half-transparent in the calendar. ";
$o .= "" . L10n::t("Warning") . " ";
$o .= "This addon still is in a very early stage of development. Expect major bugs! ";
$o .= "" . L10n::t("Synchronization (iPhone, Thunderbird Lightning, Android, ...)") . " ";
$o .= 'This addon enables synchronization of your dates and contacts with CalDAV- and CardDAV-enabled programs or devices.
As an example, the instructions how to set up two-way synchronization with an iPhone/iPodTouch are provided below.
Unfortunately, Android does not have native support for CalDAV or CardDAV, so an app has to be installed.
On desktops, the Lightning-extension to Mozilla Thunderbird should be able to use this addon as a backend. ';
$o .= '' . L10n::t('Synchronizing this calendar with the iPhone') . ' ';
$o .= "
Go to the settings
Mail, contacts, settings
Add a new account
Other...
Calendar -> CalDAV-Account
Server: " . $a->get_baseurl() . "/dav/ / Username/Password: the same as your friendica-login
";
$o .= '' . L10n::t('Synchronizing your Friendica-Contacts with the iPhone') . ' ';
$o .= "
Go to the settings
Mail, contacts, settings
Add a new account
Other...
Contacts -> CardDAV-Account
Server: " . $a->get_baseurl() . "/dav/ / Username/Password: the same as your friendica-login
";
return $o;
}