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;
35 public static function getBackendTypeName() {
36 return t("Friendicy-Native events");
41 * @param int $calendarId
42 * @throws Sabre_DAV_Exception_NotFound
45 protected static function createCache_internal($calendarId)
47 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
49 switch ($calendar["uri"]) {
50 case CALDAV_FRIENDICA_MINE:
51 $sql_where = " AND cid = 0";
53 case CALDAV_FRIENDICA_CONTACTS:
54 $sql_where = " AND cid > 0";
57 throw new Sabre_DAV_Exception_NotFound();
60 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
62 foreach ($r as $row) {
63 $uid = $calendar["uri"] . "-" . $row["id"];
64 $vevent = dav_create_empty_vevent($uid);
65 $component = dav_get_eventComponent($vevent);
68 $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
69 $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
71 $start = $row["start"];
72 $finish = $row["finish"];
75 $summary = ($row["summary"] != "" ? $row["summary"] : $row["desc"]);
76 $desc = ($row["summary"] != "" ? $row["desc"] : "");
77 $component->add("SUMMARY", icalendar_sanitize_string($summary));
78 $component->add("LOCATION", icalendar_sanitize_string($row["location"]));
79 $component->add("DESCRIPTION", icalendar_sanitize_string($desc));
81 $ts_start = wdcal_mySql2PhpTime($start);
82 $ts_end = wdcal_mySql2PhpTime($start);
84 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
85 $type = ($allday ? Sabre_VObject_Property_DateTime::DATE : Sabre_VObject_Property_DateTime::LOCALTZ);
87 $datetime_start = new Sabre_VObject_Property_DateTime("DTSTART");
88 $datetime_start->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_start)), $type);
89 $datetime_end = new Sabre_VObject_Property_DateTime("DTEND");
90 $datetime_end->setDateTime(new DateTime(date("Y-m-d H:i:s", $ts_end)), $type);
92 $component->add($datetime_start);
93 $component->add($datetime_end);
95 $data = $vevent->serialize();
97 q("INSERT INTO %s%scal_virtual_object_cache (`calendar_id`, `data_uri`, `data_summary`, `data_location`, `data_start`, `data_end`, `data_allday`, `data_type`,
98 `calendardata`, `size`, `etag`) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', %d, '%s')",
99 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendarId, dbesc($uid), dbesc($summary), dbesc($row["location"]), dbesc($row["start"]), dbesc($row["finish"]),
100 ($allday ? 1 : 0), dbesc(($row["type"] == "birthday" ? "birthday" : "")), dbesc($data), strlen($data), md5($data));
109 * @param array $calendar
110 * @param string $base_path
113 private function jqcal2wdcal($row, $calendar, $base_path)
115 if ($row["adjust"]) {
116 $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
117 $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
119 $start = $row["start"];
120 $finish = $row["finish"];
123 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
125 $summary = (($row["summary"]) ? $row["summary"] : substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100));
128 "jq_id" => $row["id"],
129 "ev_id" => $row["id"],
130 "summary" => escape_tags($summary),
131 "start" => wdcal_mySql2PhpTime($start),
132 "end" => wdcal_mySql2PhpTime($finish),
133 "is_allday" => ($allday ? 1 : 0),
134 "is_moredays" => (substr($start, 0, 10) != substr($finish, 0, 10)),
135 "is_recurring" => ($row["type"] == "birthday"),
138 "is_editable_quick" => 0,
139 "location" => $row["location"],
141 "has_notification" => 0,
142 "url_detail" => $base_path . "/events/event/" . $row["id"],
144 "special_type" => ($row["type"] == "birthday" ? "birthday" : ""),
150 * @param int $calendarId
151 * @param string $date_from
152 * @param string $date_to
153 * @param string $base_path
154 * @throws Sabre_DAV_Exception_NotFound
157 public function listItemsByRange($calendarId, $date_from, $date_to, $base_path)
159 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
161 if ($calendar["namespace"] != CALDAV_NAMESPACE_PRIVATE) throw new Sabre_DAV_Exception_NotFound();
163 switch ($calendar["uri"]) {
164 case CALDAV_FRIENDICA_MINE:
165 $sql_where = " AND cid = 0";
167 case CALDAV_FRIENDICA_CONTACTS:
168 $sql_where = " AND cid > 0";
171 throw new Sabre_DAV_Exception_NotFound();
174 if ($date_from != "") {
175 if (is_numeric($date_from)) $sql_where .= " AND `finish` >= '" . date("Y-m-d H:i:s", $date_from) . "'";
176 else $sql_where .= " AND `finish` >= '" . dbesc($date_from) . "'";
178 if ($date_to != "") {
179 if (is_numeric($date_to)) $sql_where .= " AND `start` <= '" . date("Y-m-d H:i:s", $date_to) . "'";
180 else $sql_where .= " AND `start` <= '" . dbesc($date_to) . "'";
184 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
187 foreach ($r as $row) {
188 $r = $this->jqcal2wdcal($row, $calendar, $a->get_baseurl());
189 $r["calendar_id"] = $calendar["id"];
198 * Returns a list of calendars for a principal.
200 * Every project is an array with the following keys:
201 * * id, a unique id that will be used by other functions to modify the
202 * calendar. This can be the same as the uri or a database key.
203 * * uri, which the basename of the uri with which the calendar is
205 * * principaluri. The owner of the calendar. Almost always the same as
206 * principalUri passed to this method.
208 * Furthermore it can contain webdav properties in clark notation. A very
209 * common one is '{DAV:}displayname'.
211 * @param string $principalUri
214 public function getCalendarsForUser($principalUri)
216 $n = dav_compat_principal2namespace($principalUri);
217 if ($n["namespace"] != $this->getNamespace()) return array();
219 $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"]));
221 foreach ($cals as $cal) {
222 if (!in_array($cal["uri"], $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"])) continue;
226 "uri" => $cal["uri"],
227 "principaluri" => $principalUri,
228 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag'] ? $cal['ctag'] : '0',
229 "calendar_class" => "Sabre_CalDAV_Calendar_Virtual",
231 foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field];
240 * @param int $calendar_id
241 * @param int $calendarobject_id
244 function getItemDetailRedirect($calendar_id, $calendarobject_id)
247 $item = q("SELECT `id` FROM `item` WHERE `event-id` = %d AND `uid` = %d AND deleted = 0", IntVal($calendarobject_id), $a->user["uid"]);
248 if (count($item) == 0) return "/events/";
249 return "/display/" . $a->user["nickname"] . "/" . IntVal($item[0]["id"]);