/**
 * @param int $calendar_id
 * @return Sabre_CalDAV_Backend_Common
 * @throws Exception
 */
function wdcal_calendar_factory_by_id($calendar_id)
{
    $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendar_id);
    return wdcal_calendar_factory($calendar["namespace"], $calendar["namespace_id"], $calendar["uri"], $calendar);
}
Example #2
0
/**
 * @param string $uri
 * @param string $recurr_uri
 * @return string
 */
function wdcal_getEditPage($uri, $recurr_uri)
{
    $a = get_app();
    $localization = wdcal_local::getInstanceByUser($a->user["uid"]);
    if ($uri != "" && $uri != "new") {
        $o = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `ical_uri` = '%s' AND `ical_recurr_uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $a->user["uid"], dbesc($uri), dbesc($recurr_uri));
        if (count($o) != 1) {
            return t('Not found');
        }
        $event = $o[0];
        $calendarSource = wdcal_calendar_factory($a->user["uid"], $event["namespace"], $event["namespace_id"]);
        $permissions = $calendarSource->getPermissionsItem($a->user["uid"], $uri, $recurr_uri, $event);
        if (!$permissions["write"]) {
            return t('No access');
        }
        $n = q("SELECT * FROM %s%snotifications WHERE `uid` = %d AND `ical_uri` = '%s' AND `ical_recurr_uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $a->user["uid"], dbesc($uri), dbesc($recurr_uri));
        if (count($n) > 0) {
            $notification_type = $n[0]["rel_type"];
            $notification_value = -1 * $n[0]["rel_value"];
            $notification = true;
        } else {
            if ($event["IsAllDayEvent"]) {
                $notification_type = "hour";
                $notification_value = 24;
            } else {
                $notification_type = "minute";
                $notification_value = 60;
            }
            $notification = false;
        }
    } elseif (isset($_REQUEST["start"]) && $_REQUEST["start"] > 0) {
        $event = array("id" => 0, "Subject" => $_REQUEST["title"], "Location" => "", "Description" => "", "StartTime" => $_REQUEST["start"], "EndTime" => $_REQUEST["end"], "IsAllDayEvent" => $_REQUEST["isallday"], "Color" => null, "RecurringRule" => null);
        if ($_REQUEST["isallday"]) {
            $notification_type = "hour";
            $notification_value = 24;
        } else {
            $notification_type = "hour";
            $notification_value = 1;
        }
        $notification = true;
    } else {
        $event = array("id" => 0, "Subject" => "", "Location" => "", "Description" => "", "StartTime" => "", "EndTime" => "", "IsAllDayEvent" => "", "Color" => null, "RecurringRule" => null);
        $notification_type = "hour";
        $notification_value = 1;
        $notification = true;
    }
    $out = "<a href='" . $a->get_baseurl() . "/dav/wdcal/'>" . t("Go back to the calendar") . "</a><br><br>";
    $out .= "<form method='POST' action='" . $a->get_baseurl() . "/dav/wdcal/{$uri}/edit/'><input type='hidden' name='form_security_token' value='" . get_form_security_token('caledit') . "'>\n";
    $out .= "<label for='cal_subject'>Subject:</label>\n\t\t<input name='color' id='cal_color' value='" . (strlen($event["Color"]) != 7 ? "#5858ff" : escape_tags($event["Color"])) . "'>\n\t\t<input name='subject' id='cal_subject' value='" . escape_tags($event["Subject"]) . "'><br>\n";
    $out .= "<label for='cal_allday'>Is All-Day event:</label><input type='checkbox' name='allday' id='cal_allday' " . ($event["IsAllDayEvent"] ? "checked" : "") . "><br>\n";
    $out .= "<label for='cal_startdate'>Starts:</label>";
    $out .= "<input name='start_date' value='" . $localization->dateformat_datepicker_php(wdcal_mySql2PhpTime($event["StartTime"])) . "' id='cal_start_date'>";
    $out .= "<input name='start_time' value='" . substr($event["StartTime"], 11, 5) . "' id='cal_start_time'>";
    $out .= "<br>\n";
    $out .= "<label for='cal_enddate'>Ends:</label>";
    $out .= "<input name='end_date' value='" . $localization->dateformat_datepicker_php(wdcal_mySql2PhpTime($event["EndTime"])) . "' id='cal_end_date'>";
    $out .= "<input name='end_time' value='" . substr($event["EndTime"], 11, 5) . "' id='cal_end_time'>";
    $out .= "<br>\n";
    $out .= "<label for='cal_location'>Location:</label><input name='location' id='cal_location' value='" . escape_tags($event["Location"]) . "'><br>\n";
    $out .= "<label for='event-desc-textarea'>" . t("Description") . ":</label> <textarea id='event-desc-textarea' name='wdcal_desc' style='vertical-align: top; width: 400px; height: 100px;'>" . escape_tags($event["Description"]) . "</textarea>";
    $out .= "<br style='clear: both;'>";
    $out .= "<label for='notification'>" . t('Notification') . ":</label>";
    $out .= '<input type="checkbox" name="notification" id="notification" ';
    if ($notification) {
        $out .= "checked";
    }
    $out .= '> ';
    $out .= '<span id="notification_detail" style="display: none;">
			<input name="notification_value" value="' . $notification_value . '" size="3">
			<select name="notification_type" size="1">
				<option value="minute" ';
    if ($notification_type == "minute") {
        $out .= "selected";
    }
    $out .= '> ' . t('Minutes') . '</option>
				<option value="hour" ';
    if ($notification_type == "hour") {
        $out .= "selected";
    }
    $out .= '> ' . t('Hours') . '</option>
				<option value="day" ';
    if ($notification_type == "day") {
        echo "selected";
    }
    $out .= '> ' . t('Days') . '</option>
			</select> ' . t('before') . '
		</span><br><br>';
    $out .= "<script>\$(function() {\n\t\twdcal_edit_init('" . $localization->dateformat_datepicker_js() . "');\n\t});</script>";
    $out .= "<input type='submit' name='save' value='Save'></form>";
    return $out;
}
/**
 *
 */
function wdcal_print_feed($base_path = "")
{
    $user_id = dav_compat_get_curr_user_id();
    $cals = array();
    if (isset($_REQUEST["cal"])) {
        foreach ($_REQUEST["cal"] as $c) {
            $x = explode("-", $c);
            $calendarSource = wdcal_calendar_factory($user_id, $x[0], $x[1]);
            $calp = $calendarSource->getPermissionsCalendar($user_id);
            if ($calp["read"]) {
                $cals[] = $calendarSource;
            }
        }
    }
    $ret = null;
    /** @var $cals array|AnimexxCalSource[] */
    $method = $_GET["method"];
    switch ($method) {
        case "add":
            $cs = null;
            foreach ($cals as $c) {
                if ($cs == null) {
                    $x = $c->getPermissionsCalendar($user_id);
                    if ($x["read"]) {
                        $cs = $c;
                    }
                }
            }
            if ($cs == null) {
                echo wdcal_jsonp_encode(array('IsSuccess' => false, 'Msg' => t('No access')));
                killme();
            }
            try {
                $start = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarStartTime"]));
                $end = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarEndTime"]));
                $newuri = $cs->addItem($start, $end, $_REQUEST["CalendarTitle"], $_REQUEST["IsAllDayEvent"]);
                $ret = array('IsSuccess' => true, 'Msg' => 'add success', 'Data' => $newuri);
            } catch (Exception $e) {
                $ret = array('IsSuccess' => false, 'Msg' => $e->__toString());
            }
            break;
        case "list":
            $weekstartday = isset($_REQUEST["weekstartday"]) ? IntVal($_REQUEST["weekstartday"]) : 1;
            // 1 = Monday
            $num_days = isset($_REQUEST["num_days"]) ? IntVal($_REQUEST["num_days"]) : 7;
            $ret = null;
            $date = wdcal_get_list_range_params($_REQUEST["showdate"], $weekstartday, $num_days, $_REQUEST["viewtype"]);
            $ret = array();
            $ret['events'] = array();
            $ret["issort"] = true;
            $ret["start"] = $date[0];
            $ret["end"] = $date[1];
            $ret['error'] = null;
            foreach ($cals as $c) {
                $events = $c->listItemsByRange($date[0], $date[1], $base_path);
                $ret["events"] = array_merge($ret["events"], $events);
            }
            $tmpev = array();
            foreach ($ret["events"] as $e) {
                if (!isset($tmpev[$e["start"]])) {
                    $tmpev[$e["start"]] = array();
                }
                $tmpev[$e["start"]][] = $e;
            }
            ksort($tmpev);
            $ret["events"] = array();
            foreach ($tmpev as $e) {
                foreach ($e as $f) {
                    $ret["events"][] = $f;
                }
            }
            break;
        case "update":
            $found = false;
            $start = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarStartTime"]));
            $end = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarEndTime"]));
            foreach ($cals as $c) {
                try {
                    $permissions_item = $c->getPermissionsItem($user_id, $_REQUEST["calendarId"], "");
                    if ($permissions_item["write"]) {
                        $c->updateItem($_REQUEST["calendarId"], $start, $end);
                        $found = true;
                    }
                } catch (Exception $e) {
                }
            }
            if ($found) {
                $ret = array('IsSuccess' => true, 'Msg' => 'Succefully');
            } else {
                echo wdcal_jsonp_encode(array('IsSuccess' => false, 'Msg' => t('No access')));
                killme();
            }
            try {
            } catch (Exception $e) {
                $ret = array('IsSuccess' => false, 'Msg' => $e->__toString());
            }
            break;
        case "remove":
            $found = false;
            foreach ($cals as $c) {
                try {
                    $permissions_item = $c->getPermissionsItem($user_id, $_REQUEST["calendarId"], "");
                    if ($permissions_item["write"]) {
                        $c->removeItem($_REQUEST["calendarId"]);
                    }
                } catch (Exception $e) {
                }
            }
            if ($found) {
                $ret = array('IsSuccess' => true, 'Msg' => 'Succefully');
            } else {
                echo wdcal_jsonp_encode(array('IsSuccess' => false, 'Msg' => t('No access')));
                killme();
            }
            break;
    }
    echo wdcal_jsonp_encode($ret);
    killme();
}
/**
 * @param App $a
 * @return string
 */
function wdcal_getSettingsPage(&$a)
{
    if (!local_user()) {
        notice(t('Permission denied.') . EOL);
        return '';
    }
    if (isset($_REQUEST["save"])) {
        check_form_security_token_redirectOnErr('/dav/settings/', 'calprop');
        set_pconfig($a->user["uid"], "dav", "dateformat", $_REQUEST["wdcal_date_format"]);
        info(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(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`)\n\t\t\t\tVALUES (%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(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(t('The calendar has been deleted.'));
    }
    $o = "";
    $o .= "<a href='" . $a->get_baseurl() . "/dav/wdcal/'>" . t("Go back to the calendar") . "</a><br><br>";
    $o .= '<h3>' . t('Calendar Settings') . '</h3>';
    $current_format = wdcal_local::getInstanceByUser($a->user["uid"]);
    $o .= '<form method="POST" action="' . $a->get_baseurl() . '/dav/settings/">';
    $o .= "<input type='hidden' name='form_security_token' value='" . get_form_security_token('calprop') . "'>\n";
    $o .= '<label for="wdcal_date_format">' . t('Date format') . ':</label><select name="wdcal_date_format" id="wdcal_date_format" size="1">';
    $classes = wdcal_local::getInstanceClasses();
    foreach ($classes as $c) {
        $o .= '<option value="' . $c::getID() . '" ';
        if ($c::getID() == $current_format::getID()) {
            $o .= 'selected';
        }
        $o .= '>' . escape_tags($c::getName()) . '</option>';
    }
    $o .= '</select><br>';
    $o .= '<label for="wdcal_time_zone">' . t('Time zone') . ':</label><input id="wdcal_time_zone" value="' . $a->timezone . '" disabled><br>';
    $o .= '<input type="submit" name="save" value="' . t('Save') . '">';
    $o .= '</form>';
    $o .= '<br><br><h3>' . t('Calendars') . '</h3>';
    $o .= '<form method="POST" action="' . $a->get_baseurl() . '/dav/settings/">';
    $o .= "<input type='hidden' name='form_security_token' value='" . get_form_security_token('calprop') . "'>\n";
    $o .= "<table><tr><th>Type</th><th>Color</th><th>Name</th><th>URI (for CalDAV)</th><th>ICS</th></tr>";
    $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"]));
    $private_max = 0;
    $num_non_virtual = 0;
    foreach ($r as $x) {
        $backend = wdcal_calendar_factory($x["namespace"], $x["namespace_id"], $x["uri"], $x);
        if (!is_subclass_of($backend, "Sabre_CalDAV_Backend_Virtual")) {
            $num_non_virtual++;
        }
    }
    foreach ($r as $x) {
        $p = explode("private-", $x["uri"]);
        if (count($p) == 2 && $p[1] > $private_max) {
            $private_max = $p[1];
        }
        $backend = wdcal_calendar_factory($x["namespace"], $x["namespace_id"], $x["uri"], $x);
        $disabled = is_subclass_of($backend, "Sabre_CalDAV_Backend_Virtual") ? "disabled" : "";
        $o .= "<tr>";
        $o .= "<td style='padding: 2px;'>" . escape_tags($backend->getBackendTypeName()) . "</td>";
        $o .= "<td style='padding: 2px; text-align: center;'><input style='margin-left: 10px; width: 70px;' class='cal_color' name='color[" . $x["id"] . "]' id='cal_color_" . $x["id"] . "' value='#" . (strlen($x["calendarcolor"]) != 6 ? "5858ff" : escape_tags($x["calendarcolor"])) . "'></td>";
        $o .= "<td style='padding: 2px;'><input style='margin-left: 10px;' name='name[" . $x["id"] . "]' value='" . escape_tags($x["displayname"]) . "' {$disabled}></td>";
        $o .= "<td style='padding: 2px;'><input style='margin-left: 10px; width: 150px;' name='uri[" . $x["id"] . "]' value='" . escape_tags($x["uri"]) . "' {$disabled}></td>";
        $o .= "<td style='padding: 2px;'><a href='" . $a->get_baseurl() . "/dav/wdcal/" . $x["id"] . "/ics-export/'>Export</a>";
        if (!is_subclass_of($backend, "Sabre_CalDAV_Backend_Virtual") && $num_non_virtual > 1) {
            $o .= " / <a href='" . $a->get_baseurl() . "/dav/wdcal/" . $x["id"] . "/ics-import/'>Import</a>";
        }
        $o .= "</td>";
        $o .= "<td style='padding: 2px; padding-left: 50px;'>";
        if (!is_subclass_of($backend, "Sabre_CalDAV_Backend_Virtual") && $num_non_virtual > 1) {
            $o .= "<a href='" . $a->get_baseurl() . "/dav/settings/?remove_cal=" . $x["id"] . "&amp;t=" . get_form_security_token("del_cal") . "' class='delete_cal'>Delete</a>";
        }
        $o .= "</td>\n";
        $o .= "</tr>\n";
    }
    $private_max++;
    $o .= "<tr class='cal_add_row' style='display: none;'>";
    $o .= "<td style='padding: 2px;'>" . escape_tags(Sabre_CalDAV_Backend_Private::getBackendTypeName()) . "</td>";
    $o .= "<td style='padding: 2px; text-align: center;'><input style='margin-left: 10px; width: 70px;' class='cal_color' name='color[new]' id='cal_color_new' value='#5858ff'></td>";
    $o .= "<td style='padding: 2px;'><input style='margin-left: 10px;' name='name[new]' value='Another calendar'></td>";
    $o .= "<td style='padding: 2px;'><input style='margin-left: 10px; width: 150px;' name='uri[new]' value='private-{$private_max}'></td>";
    $o .= "<td></td><td></td>";
    $o .= "</tr>\n";
    $o .= "</table>";
    $o .= "<div style='text-align: center;'>[<a href='#' class='calendar_add_caller'>" . t("Create a new calendar") . "</a>]</div>";
    $o .= '<input type="submit" name="save_cals" value="' . t('Save') . '">';
    $o .= '</form>';
    $baseurl = $a->get_baseurl();
    $o .= "<script>\$(function() {\n\t\twdcal_edit_calendars_start('" . $current_format->dateformat_datepicker_js() . "', '{$baseurl}/dav/');\n\t});</script>";
    $o .= "<br><h3>" . t("Limitations") . "</h3>";
    $o .= "- The native friendica events are embedded as read-only, half-transparent in the calendar.<br>";
    $o .= "<br><h3>" . t("Warning") . "</h3>";
    $o .= "This plugin still is in a very early stage of development. Expect major bugs!<br>";
    $o .= "<br><h3>" . t("Synchronization (iPhone, Thunderbird Lightning, Android, ...)") . "</h3>";
    $o .= 'This plugin enables synchronization of your dates and contacts with CalDAV- and CardDAV-enabled programs or devices.<br>
		As an example, the instructions how to set up two-way synchronization with an iPhone/iPodTouch are provided below.<br>
		Unfortunately, Android does not have native support for CalDAV or CardDAV, so an app has to be installed.<br>
		On desktops, the Lightning-extension to Mozilla Thunderbird should be able to use this plugin as a backend.<br><br>';
    $o .= '<h4>' . t('Synchronizing this calendar with the iPhone') . '</h4>';
    $o .= "<ul>\n\t<li>Go to the settings</li>\n\t<li>Mail, contacts, settings</li>\n\t<li>Add a new account</li>\n\t<li>Other...</li>\n\t<li>Calendar -> CalDAV-Account</li>\n\t<li><b>Server:</b> " . $a->get_baseurl() . "/dav/ / <b>Username/Password:</b> <em>the same as your friendica-login</em></li>\n\t</ul>";
    $o .= '<h4>' . t('Synchronizing your Friendica-Contacts with the iPhone') . '</h4>';
    $o .= "<ul>\n\t<li>Go to the settings</li>\n\t<li>Mail, contacts, settings</li>\n\t<li>Add a new account</li>\n\t<li>Other...</li>\n\t<li>Contacts -> CardDAV-Account</li>\n\t<li><b>Server:</b> " . $a->get_baseurl() . "/dav/ / <b>Username/Password:</b> <em>the same as your friendica-login</em></li>\n\t</ul>";
    return $o;
}