3 use Friendica\Core\L10n;
4 use Friendica\Util\DateTimeFormat;
6 class Sabre_CalDAV_Backend_Friendica extends Sabre_CalDAV_Backend_Virtual
10 * @var null|Sabre_CalDAV_Backend_Friendica
12 private static $instance = null;
16 * @return Sabre_CalDAV_Backend_Friendica
18 public static function getInstance()
20 if (self::$instance == null) {
21 self::$instance = new Sabre_CalDAV_Backend_Friendica();
23 return self::$instance;
29 public function getNamespace()
31 return CALDAV_NAMESPACE_PRIVATE;
38 public static function getBackendTypeName() {
39 return L10n::t("Friendica-Native events");
44 * @param int $calendarId
45 * @throws Sabre_DAV_Exception_NotFound
48 protected static function createCache_internal($calendarId)
50 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
52 switch ($calendar["uri"]) {
53 case CALDAV_FRIENDICA_MINE:
54 $sql_where = " AND cid = 0";
56 case CALDAV_FRIENDICA_CONTACTS:
57 $sql_where = " AND cid > 0";
60 throw new Sabre_DAV_Exception_NotFound();
63 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
65 foreach ($r as $row) {
66 $uid = $calendar["uri"] . "-" . $row["id"];
67 $vevent = dav_create_empty_vevent($uid);
68 $component = dav_get_eventComponent($vevent);
71 $start = DateTimeFormat::local($row["start"]);
72 $finish = DateTimeFormat::local($row["finish"]);
74 $start = $row["start"];
75 $finish = $row["finish"];
78 $summary = ($row["summary"] != "" ? $row["summary"] : $row["desc"]);
79 $desc = ($row["summary"] != "" ? $row["desc"] : "");
80 $component->add("SUMMARY", icalendar_sanitize_string($summary));
81 $component->add("LOCATION", icalendar_sanitize_string($row["location"]));
82 $component->add("DESCRIPTION", icalendar_sanitize_string($desc));
84 $ts_start = wdcal_mySql2PhpTime($start);
85 $ts_end = wdcal_mySql2PhpTime($start);
87 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
88 $type = ($allday ? Sabre\VObject\Property\DateTime::DATE : Sabre\VObject\Property\DateTime::LOCALTZ);
90 $datetime_start = new Sabre\VObject\Property\DateTime("DTSTART");
91 $datetime_start->setDateTime(new DateTime(date(DateTimeFormat::MYSQL, $ts_start)), $type);
92 $datetime_end = new Sabre\VObject\Property\DateTime("DTEND");
93 $datetime_end->setDateTime(new DateTime(date(DateTimeFormat::MYSQL, $ts_end)), $type);
95 $component->add($datetime_start);
96 $component->add($datetime_end);
98 $data = $vevent->serialize();
100 q("INSERT INTO %s%scal_virtual_object_cache (`calendar_id`, `data_uri`, `data_summary`, `data_location`, `data_start`, `data_end`, `data_allday`, `data_type`,
101 `calendardata`, `size`, `etag`) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d, '%s', '%s', %d, '%s')",
102 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $calendarId, dbesc($uid), dbesc($summary), dbesc($row["location"]), dbesc($row["start"]), dbesc($row["finish"]),
103 ($allday ? 1 : 0), dbesc(($row["type"] == "birthday" ? "birthday" : "")), dbesc($data), strlen($data), md5($data));
112 * @param array $calendar
113 * @param string $base_path
116 private function jqcal2wdcal($row, $calendar, $base_path)
118 if ($row["adjust"]) {
119 $start = DateTimeFormat::local($row["start"]);
120 $finish = DateTimeFormat::local($row["finish"]);
122 $start = $row["start"];
123 $finish = $row["finish"];
126 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
128 $summary = (($row["summary"]) ? $row["summary"] : substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100));
131 "jq_id" => $row["id"],
132 "ev_id" => $row["id"],
133 "summary" => escape_tags($summary),
134 "start" => wdcal_mySql2PhpTime($start),
135 "end" => wdcal_mySql2PhpTime($finish),
136 "is_allday" => ($allday ? 1 : 0),
137 "is_moredays" => (substr($start, 0, 10) != substr($finish, 0, 10)),
138 "is_recurring" => ($row["type"] == "birthday"),
141 "is_editable_quick" => 0,
142 "location" => $row["location"],
144 "has_notification" => 0,
145 "url_detail" => $base_path . "/events/event/" . $row["id"],
147 "special_type" => ($row["type"] == "birthday" ? "birthday" : ""),
153 * @param int $calendarId
154 * @param string $date_from
155 * @param string $date_to
156 * @param string $base_path
157 * @throws Sabre_DAV_Exception_NotFound
160 public function listItemsByRange($calendarId, $date_from, $date_to, $base_path)
162 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
164 if ($calendar["namespace"] != CALDAV_NAMESPACE_PRIVATE) throw new Sabre_DAV_Exception_NotFound();
166 switch ($calendar["uri"]) {
167 case CALDAV_FRIENDICA_MINE:
168 $sql_where = " AND cid = 0";
170 case CALDAV_FRIENDICA_CONTACTS:
171 $sql_where = " AND cid > 0";
174 throw new Sabre_DAV_Exception_NotFound();
177 if ($date_from != "") {
178 if (is_numeric($date_from)) $sql_where .= " AND `finish` >= '" . date(DateTimeFormat::MYSQL, $date_from) . "'";
179 else $sql_where .= " AND `finish` >= '" . dbesc($date_from) . "'";
181 if ($date_to != "") {
182 if (is_numeric($date_to)) $sql_where .= " AND `start` <= '" . date(DateTimeFormat::MYSQL, $date_to) . "'";
183 else $sql_where .= " AND `start` <= '" . dbesc($date_to) . "'";
187 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
190 foreach ($r as $row) {
191 $r = $this->jqcal2wdcal($row, $calendar, $a->get_baseurl());
192 $r["calendar_id"] = $calendar["id"];
201 * Returns a list of calendars for a principal.
203 * Every project is an array with the following keys:
204 * * id, a unique id that will be used by other functions to modify the
205 * calendar. This can be the same as the uri or a database key.
206 * * uri, which the basename of the uri with which the calendar is
208 * * principaluri. The owner of the calendar. Almost always the same as
209 * principalUri passed to this method.
211 * Furthermore it can contain webdav properties in clark notation. A very
212 * common one is '{DAV:}displayname'.
214 * @param string $principalUri
217 public function getCalendarsForUser($principalUri)
219 $n = dav_compat_principal2namespace($principalUri);
220 if ($n["namespace"] != $this->getNamespace()) return [];
222 $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"]));
224 foreach ($cals as $cal) {
225 if (!in_array($cal["uri"], $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"])) continue;
229 "uri" => $cal["uri"],
230 "principaluri" => $principalUri,
231 '{' . Sabre_CalDAV_Plugin::NS_CALENDARSERVER . '}getctag' => $cal['ctag'] ? $cal['ctag'] : '0',
232 '{' . Sabre_CalDAV_Plugin::NS_CALDAV . '}supported-calendar-component-set' => new Sabre_CalDAV_Property_SupportedCalendarComponentSet(["VEVENT"]),
233 "calendar_class" => "Sabre_CalDAV_Calendar_Virtual",
235 foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field];
244 * @param int $calendar_id
245 * @param int $calendarobject_id
248 function getItemDetailRedirect($calendar_id, $calendarobject_id)
251 $item = q("SELECT `id` FROM `item` WHERE `event-id` = %d AND `uid` = %d AND deleted = 0", IntVal($calendarobject_id), $a->user["uid"]);
252 if (count($item) == 0) return "/events/";
253 return "/display/" . $a->user["nickname"] . "/" . IntVal($item[0]["id"]);