3 class Sabre_CalDAV_Backend_Friendica extends Sabre_CalDAV_Backend_Virtual
7 * @var null|Sabre_CalDAV_Backend_Friendica
9 private static $instance = null;
13 * @return Sabre_CalDAV_Backend_Friendica
15 public static function getInstance()
17 if (self::$instance == null) {
18 self::$instance = new Sabre_CalDAV_Backend_Friendica();
20 return self::$instance;
26 public function getNamespace()
28 return CALDAV_NAMESPACE_PRIVATE;
34 * @param int $calendarId
35 * @throws Sabre_DAV_Exception_NotFound
38 protected static function createCache_internal($calendarId)
40 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
42 switch ($calendar["uri"]) {
43 case CALDAV_FRIENDICA_MINE:
44 $sql_where = " AND cid = 0";
46 case CALDAV_FRIENDICA_CONTACTS:
47 $sql_where = " AND cid > 0";
50 throw new Sabre_DAV_Exception_NotFound();
53 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
55 foreach ($r as $row) {
56 $uid = $calendar["uri"] . "-" . $row["id"];
57 $vevent = dav_create_empty_vevent($uid);
58 $component = dav_get_eventComponent($vevent);
61 $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
62 $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
64 $start = $row["start"];
65 $finish = $row["finish"];
68 $summary = ($row["summary"] != "" ? $row["summary"] : $row["desc"]);
69 $desc = ($row["summary"] != "" ? $row["desc"] : "");
70 $component->add("SUMMARY", icalendar_sanitize_string($summary));
71 $component->add("LOCATION", icalendar_sanitize_string($row["location"]));
72 $component->add("DESCRIPTION", icalendar_sanitize_string($desc));
74 $ts_start = wdcal_mySql2PhpTime($start);
75 $ts_end = wdcal_mySql2PhpTime($start);
77 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
78 $type = ($allday ? Sabre_VObject_Property_DateTime::DATE : Sabre_VObject_Property_DateTime::LOCALTZ);
80 $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
81 $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
82 $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
83 $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
85 $component->add($datetime_start);
86 $component->add($datetime_end);
88 $data = $vevent->serialize();
90 q("INSERT INTO %s%scal_virtual_object_cache (`calendar_id`, `data_uri`, `data_summary`, `data_location`, `data_start`, `data_end`, `data_allday`, `data_type`,
91 `calendardata`, `size`, `etag`) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', %d, '%s')",
92 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendarId, dbesc($uid), dbesc($summary), dbesc($row["location"]), dbesc($row["start"]), dbesc($row["finish"]),
93 ($allday ? 1 : 0), dbesc(($row["type"] == "birthday" ? "birthday" : "")), dbesc($data), strlen($data), md5($data));
102 * @param array $calendar
103 * @param string $base_path
106 private function jqcal2wdcal($row, $calendar, $base_path)
108 if ($row["adjust"]) {
109 $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
110 $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
112 $start = $row["start"];
113 $finish = $row["finish"];
116 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
118 $summary = (($row["summary"]) ? $row["summary"] : substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100));
121 "jq_id" => $row["id"],
122 "ev_id" => $row["id"],
123 "summary" => escape_tags($summary),
124 "start" => wdcal_mySql2PhpTime($start),
125 "end" => wdcal_mySql2PhpTime($finish),
126 "is_allday" => ($allday ? 1 : 0),
127 "is_moredays" => (substr($start, 0, 10) != substr($finish, 0, 10)),
128 "is_recurring" => ($row["type"] == "birthday"),
129 "color" => "#f8f8ff",
131 "is_editable_quick" => 0,
132 "location" => $row["location"],
134 "has_notification" => 0,
135 "url_detail" => $base_path . "/events/event/" . $row["id"],
137 "special_type" => ($row["type"] == "birthday" ? "birthday" : ""),
143 * @param int $calendarId
144 * @param string $date_from
145 * @param string $date_to
146 * @param string $base_path
147 * @throws Sabre_DAV_Exception_NotFound
150 public function listItemsByRange($calendarId, $date_from, $date_to, $base_path)
152 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
154 if ($calendar["namespace"] != CALDAV_NAMESPACE_PRIVATE) throw new Sabre_DAV_Exception_NotFound();
156 switch ($calendar["uri"]) {
157 case CALDAV_FRIENDICA_MINE:
158 $sql_where = " AND cid = 0";
160 case CALDAV_FRIENDICA_CONTACTS:
161 $sql_where = " AND cid > 0";
164 throw new Sabre_DAV_Exception_NotFound();
167 if ($date_from != "") {
168 if (is_numeric($date_from)) $sql_where .= " AND `finish` >= '" . date("Y-m-d H:i:s", $date_from) . "'";
169 else $sql_where .= " AND `finish` >= '" . dbesc($date_from) . "'";
171 if ($date_to != "") {
172 if (is_numeric($date_to)) $sql_where .= " AND `start` <= '" . date("Y-m-d H:i:s", $date_to) . "'";
173 else $sql_where .= " AND `start` <= '" . dbesc($date_to) . "'";
177 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
180 foreach ($r as $row) {
181 $r = $this->jqcal2wdcal($row, $calendar, $a->get_baseurl());
182 $r["calendar_id"] = $calendar["id"];
191 * Returns a list of calendars for a principal.
193 * Every project is an array with the following keys:
194 * * id, a unique id that will be used by other functions to modify the
195 * calendar. This can be the same as the uri or a database key.
196 * * uri, which the basename of the uri with which the calendar is
198 * * principaluri. The owner of the calendar. Almost always the same as
199 * principalUri passed to this method.
201 * Furthermore it can contain webdav properties in clark notation. A very
202 * common one is '{DAV:}displayname'.
204 * @param string $principalUri
207 public function getCalendarsForUser($principalUri)
209 $n = dav_compat_principal2namespace($principalUri);
210 if ($n["namespace"] != $this->getNamespace()) return array();
212 $cals = q("SELECT * FROM %s%scalendars WHERE `namespace` = %d AND `namespace_id` = %d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $this->getNamespace(), IntVal($n["namespace_id"]));
214 foreach ($cals as $cal) {
215 if (!in_array($cal["uri"], $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"])) continue;
219 "uri" => $cal["uri"],
220 "principaluri" => $principalUri,
221 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag'] ? $cal['ctag'] : '0',
222 "calendar_class" => "Sabre_CalDAV_Calendar_Virtual",
224 foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field];
233 * @param int $calendar_id
234 * @param int $calendarobject_id
237 function getItemDetailRedirect($calendar_id, $calendarobject_id)
240 $item = q("SELECT `id` FROM `item` WHERE `event-id` = %d AND `uid` = %d AND deleted = 0", IntVal($calendarobject_id), $a->user["uid"]);
241 if (count($item) == 0) return "/events/";
242 return "/display/" . $a->user["nickname"] . "/" . IntVal($item[0]["id"]);