]> git.mxchange.org Git - friendica-addons.git/blob - dav/friendica/dav_caldav_backend_virtual_friendica.inc.php
Merge branch '3.6-rc'
[friendica-addons.git] / dav / friendica / dav_caldav_backend_virtual_friendica.inc.php
1 <?php
2
3 use Friendica\Core\L10n;
4 use Friendica\Util\DateTimeFormat;
5
6 class Sabre_CalDAV_Backend_Friendica extends Sabre_CalDAV_Backend_Virtual
7 {
8
9         /**
10          * @var null|Sabre_CalDAV_Backend_Friendica
11          */
12         private static $instance = null;
13
14         /**
15          * @static
16          * @return Sabre_CalDAV_Backend_Friendica
17          */
18         public static function getInstance()
19         {
20                 if (self::$instance == null) {
21                         self::$instance = new Sabre_CalDAV_Backend_Friendica();
22                 }
23                 return self::$instance;
24         }
25
26         /**
27          * @return int
28          */
29         public function getNamespace()
30         {
31                 return CALDAV_NAMESPACE_PRIVATE;
32         }
33
34         /**
35          * @static
36          * @return string
37          */
38         public static function getBackendTypeName() {
39                 return L10n::t("Friendica-Native events");
40         }
41
42         /**
43          * @static
44          * @param int $calendarId
45          * @throws Sabre_DAV_Exception_NotFound
46          * @return void
47          */
48         protected static function createCache_internal($calendarId)
49         {
50                 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
51
52                 switch ($calendar["uri"]) {
53                         case CALDAV_FRIENDICA_MINE:
54                                 $sql_where = " AND cid = 0";
55                                 break;
56                         case CALDAV_FRIENDICA_CONTACTS:
57                                 $sql_where = " AND cid > 0";
58                                 break;
59                         default:
60                                 throw new Sabre_DAV_Exception_NotFound();
61                 }
62
63                 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
64
65                 foreach ($r as $row) {
66                         $uid       = $calendar["uri"] . "-" . $row["id"];
67                         $vevent    = dav_create_empty_vevent($uid);
68                         $component = dav_get_eventComponent($vevent);
69
70                         if ($row["adjust"]) {
71                                 $start  = DateTimeFormat::local($row["start"]);
72                                 $finish = DateTimeFormat::local($row["finish"]);
73                         } else {
74                                 $start  = $row["start"];
75                                 $finish = $row["finish"];
76                         }
77
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));
83
84                         $ts_start = wdcal_mySql2PhpTime($start);
85                         $ts_end   = wdcal_mySql2PhpTime($start);
86
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);
89
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);
94
95                         $component->add($datetime_start);
96                         $component->add($datetime_end);
97
98                         $data = $vevent->serialize();
99
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));
104
105                 }
106
107         }
108
109
110         /**
111          * @param array $row
112          * @param array $calendar
113          * @param string $base_path
114          * @return array
115          */
116         private function jqcal2wdcal($row, $calendar, $base_path)
117         {
118                 if ($row["adjust"]) {
119                         $start  = DateTimeFormat::local($row["start"]);
120                         $finish = DateTimeFormat::local($row["finish"]);
121                 } else {
122                         $start  = $row["start"];
123                         $finish = $row["finish"];
124                 }
125
126                 $allday = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
127
128                 $summary = (($row["summary"]) ? $row["summary"] : substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100));
129
130                 return array(
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"),
139                         "color"             => "7878ff",
140                         "is_editable"       => 0,
141                         "is_editable_quick" => 0,
142                         "location"          => $row["location"],
143                         "attendees"         => '',
144                         "has_notification"  => 0,
145                         "url_detail"        => $base_path . "/events/event/" . $row["id"],
146                         "url_edit"          => "",
147                         "special_type"      => ($row["type"] == "birthday" ? "birthday" : ""),
148                 );
149         }
150
151
152         /**
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
158          * @return array
159          */
160         public function listItemsByRange($calendarId, $date_from, $date_to, $base_path)
161         {
162                 $calendar = Sabre_CalDAV_Backend_Common::loadCalendarById($calendarId);
163
164                 if ($calendar["namespace"] != CALDAV_NAMESPACE_PRIVATE) throw new Sabre_DAV_Exception_NotFound();
165
166                 switch ($calendar["uri"]) {
167                         case CALDAV_FRIENDICA_MINE:
168                                 $sql_where = " AND cid = 0";
169                                 break;
170                         case CALDAV_FRIENDICA_CONTACTS:
171                                 $sql_where = " AND cid > 0";
172                                 break;
173                         default:
174                                 throw new Sabre_DAV_Exception_NotFound();
175                 }
176
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) . "'";
180                 }
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) . "'";
184                 }
185                 $ret = array();
186
187                 $r = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", IntVal($calendar["namespace_id"]));
188
189                 $a = get_app();
190                 foreach ($r as $row) {
191                         $r                = $this->jqcal2wdcal($row, $calendar, $a->get_baseurl());
192                         $r["calendar_id"] = $calendar["id"];
193                         $ret[]            = $r;
194                 }
195
196                 return $ret;
197         }
198
199
200         /**
201          * Returns a list of calendars for a principal.
202          *
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
207          *    accessed.
208          *  * principaluri. The owner of the calendar. Almost always the same as
209          *    principalUri passed to this method.
210          *
211          * Furthermore it can contain webdav properties in clark notation. A very
212          * common one is '{DAV:}displayname'.
213          *
214          * @param string $principalUri
215          * @return array
216          */
217         public function getCalendarsForUser($principalUri)
218         {
219                 $n = dav_compat_principal2namespace($principalUri);
220                 if ($n["namespace"] != $this->getNamespace()) return array();
221
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"]));
223                 $ret  = array();
224                 foreach ($cals as $cal) {
225                         if (!in_array($cal["uri"], $GLOBALS["CALDAV_PRIVATE_SYSTEM_CALENDARS"])) continue;
226
227                         $dat = array(
228                                 "id"                                                      => $cal["id"],
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(array("VEVENT")),
233                                 "calendar_class"                                          => "Sabre_CalDAV_Calendar_Virtual",
234                         );
235                         foreach ($this->propertyMap as $key=> $field) $dat[$key] = $cal[$field];
236
237                         $ret[] = $dat;
238                 }
239
240                 return $ret;
241         }
242
243         /**
244          * @param int $calendar_id
245          * @param int $calendarobject_id
246          * @return string
247          */
248         function getItemDetailRedirect($calendar_id, $calendarobject_id)
249         {
250                 $a    = get_app();
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"]);
254
255         }
256 }