/**
 *
 */
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 string $sd
  * @param string $ed
  * @param string $base_path
  * @return array
  */
 public function listItemsByRange($sd, $ed, $base_path)
 {
     $usr_id = IntVal($this->calendarDb->uid);
     $von = wdcal_php2MySqlTime($sd);
     $bis = wdcal_php2MySqlTime($ed);
     // @TODO Events, die früher angefangen haben, aber noch andauern
     $evs = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `starttime` between '%s' and '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($von), dbesc($bis));
     $events = array();
     foreach ($evs as $row) {
         $events[] = $this->jqcal2wdcal($row, $usr_id, $base_path);
     }
     return $events;
 }
 /**
  * Updates an existing calendarobject, based on it's uri.
  *
  * It is possible return an etag from this function, which will be used in
  * the response to this PUT request. Note that the ETag must be surrounded
  * by double-quotes.
  *
  * However, you should only really return this ETag if you don't mangle the
  * calendar-data. If the result of a subsequent GET to this object is not
  * the exact same as this request body, you should omit the ETag.
  *
  * @param mixed $calendarId
  * @param string $objectUri
  * @param string $calendarData
  * @return string|null
  */
 function updateCalendarObject($calendarId, $objectUri, $calendarData)
 {
     $calendarData = icalendar_sanitize_string($calendarData);
     $extraData = $this->getDenormalizedData($calendarData);
     q("UPDATE %s%scalendarobjects SET `calendardata` = '%s', `lastmodified` = NOW(), `etag` = '%s', `size` = %d, `componentType` = '%s', `firstOccurence` = '%s', `lastOccurence` = '%s'\n\t\t\tWHERE `calendar_id` = %d AND `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($calendarData), dbesc($extraData["etag"]), IntVal($extraData["size"]), dbesc($extraData["componentType"]), dbesc(wdcal_php2MySqlTime($extraData["firstOccurence"])), dbesc(wdcal_php2MySqlTime($extraData["lastOccurence"])), IntVal($calendarId), dbesc($objectUri));
     $this->increaseCalendarCtag($calendarId);
     renderCalDavEntry_uri($objectUri);
     return '"' . $extraData['etag'] . '"';
 }