]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/wdcal_cal_source_private.inc.php
Fix problems when friendica is installed in a subpath; small CSS-Fixes
[friendica-addons.git] / dav / common / wdcal_cal_source_private.inc.php
1 <?php
2
3 class AnimexxCalSourcePrivate extends AnimexxCalSource
4 {
5
6         /**
7          * @return int
8          */
9         public static function getNamespace()
10         {
11                 return CALDAV_NAMESPACE_PRIVATE;
12         }
13
14         /**
15          * @param int $user
16          * @return array
17          */
18         public function getPermissionsCalendar($user)
19         {
20                 if ($user == $this->calendarDb->uid) return array("read"=> true, "write"=> true);
21                 return array("read"=> false, "write"=> false);
22         }
23
24         /**
25          * @param int $user
26          * @param string $item_uri
27          * @param string $recurrence_uri
28          * @param null|array $item_arr
29          * @return array
30          */
31         public function getPermissionsItem($user, $item_uri, $recurrence_uri, $item_arr = null)
32         {
33                 $cal_perm = $this->getPermissionsCalendar($user);
34                 if (!$cal_perm["read"]) return array("read"=> false, "write"=> false);
35                 if (!$cal_perm["write"]) array("read"=> true, "write"=> false);
36
37                 if ($item_arr === null) {
38                         $x = q("SELECT `permission_edit` FROM %s%sjqcalendar WHERE `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s' AND `ical_recurr_uri` = '%s'",
39                                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $this->getNamespace(), $this->namespace_id, dbesc($item_uri), dbesc($recurrence_uri)
40                         );
41                         if (!$x || count($x) == 0) return array("read"=> false, "write"=> false);
42                         return array("read"=> true, "write"=> ($x[0]["permission_edit"]));
43                 } else {
44                         return array("read"=> true, "write"=> ($item_arr["permission_edit"]));
45                 }
46
47         }
48
49         /**
50          * @param string $uri
51          * @throws Sabre_DAV_Exception_NotFound
52          */
53         public function removeItem($uri){
54                 $obj_alt = q("SELECT * FROM %s%sjqcalendar WHERE namespace = %d AND namespace_id = %d AND ical_uri = '%s'",
55                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $this->getNamespace(), $this->namespace_id, dbesc($uri));
56
57                 if (count($obj_alt) == 0) throw new Sabre_DAV_Exception_NotFound("Not found");
58
59                 $calendarBackend = new Sabre_CalDAV_Backend_Std();
60                 $calendarBackend->deleteCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $obj_alt[0]["ical_uri"]);
61         }
62
63         /**
64          * @param string $uri
65          * @param array $start
66          * @param array $end
67          * @param string $subject
68          * @param bool $allday
69          * @param string $description
70          * @param string $location
71          * @param null $color
72          * @param string $timezone
73          * @param bool $notification
74          * @param null $notification_type
75          * @param null $notification_value
76          * @throws Sabre_DAV_Exception_NotFound
77          * @throws Sabre_DAV_Exception_Conflict
78          */
79         public function updateItem($uri, $start, $end, $subject = "", $allday = false, $description = "", $location = "", $color = null, $timezone = "", $notification = true, $notification_type = null, $notification_value = null)
80         {
81                 $a = get_app();
82
83                 $usr_id = IntVal($this->calendarDb->uid);
84
85                 $old = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s'",
86                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($uri));
87                 if (count($old) == 0) throw new Sabre_DAV_Exception_NotFound("Not Found 1");
88                 $old_obj = new DBClass_friendica_jqcalendar($old[0]);
89
90                 $calendarBackend = new Sabre_CalDAV_Backend_Std();
91                 $obj             = $calendarBackend->getCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $old_obj->ical_uri);
92                 if (!$obj) throw new Sabre_DAV_Exception_NotFound("Not Found 2");
93
94                 $v = new vcalendar();
95                 $v->setConfig('unique_id', $a->get_hostname());
96
97                 $v->setMethod('PUBLISH');
98                 $v->setProperty("x-wr-calname", "AnimexxCal");
99                 $v->setProperty("X-WR-CALDESC", "Animexx Calendar");
100                 $v->setProperty("X-WR-TIMEZONE", $a->timezone);
101
102                 $obj["calendardata"] = icalendar_sanitize_string($obj["calendardata"]);
103
104                 $v->parse($obj["calendardata"]);
105                 /** @var $vevent vevent */
106                 $vevent = $v->getComponent('vevent');
107
108                 if (trim($vevent->getProperty('uid')) . ".ics" != $old_obj->ical_uri)
109                         throw new Sabre_DAV_Exception_Conflict("URI != URI: " . $old_obj->ical_uri . " vs. " . trim($vevent->getProperty("uid")));
110
111                 if ($end["year"] < $start["year"] ||
112                         ($end["year"] == $start["year"] && $end["month"] < $start["month"]) ||
113                         ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] < $start["day"]) ||
114                         ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] < $start["hour"]) ||
115                         ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] < $start["minute"]) ||
116                         ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] == $start["minute"] && $end["second"] < $start["second"])
117                 ) {
118                         $end = $start;
119                         if ($end["hour"] < 23) $end["hour"]++;
120                 } // DTEND muss <= DTSTART
121
122                 if ($start["hour"] == 0 && $start["minute"] == 0 && $end["hour"] == 23 && $end["minute"] == 59) {
123                         $allday = true;
124                 }
125
126                 if ($allday) {
127                         $vevent->setDtstart($start["year"], $start["month"], $start["day"], FALSE, FALSE, FALSE, FALSE, array("VALUE"=> "DATE"));
128                         $end = mktime(0, 0, 0, $end["month"], $end["day"], $end["year"]) + 3600 * 24;
129
130                         // If a DST change occurs on the current day
131                         $end += date("Z", ($end - 3600*24)) - date("Z", $end);
132
133                         $vevent->setDtend(date("Y", $end), date("m", $end), date("d", $end), FALSE, FALSE, FALSE, FALSE, array("VALUE"=> "DATE"));
134                 } else {
135                         $vevent->setDtstart($start["year"], $start["month"], $start["day"], $start["hour"], $start["minute"], $start["second"], FALSE, array("VALUE"=> "DATE-TIME"));
136                         $vevent->setDtend($end["year"], $end["month"], $end["day"], $end["hour"], $end["minute"], $end["second"], FALSE, array("VALUE"=> "DATE-TIME"));
137                 }
138
139                 if ($subject != "") {
140                         $vevent->setProperty('LOCATION', $location);
141                         $vevent->setProperty('summary', $subject);
142                         $vevent->setProperty('description', $description);
143                 }
144                 if (!is_null($color) && $color >= 0) $vevent->setProperty("X-ANIMEXX-COLOR", $color);
145
146                 if (!$notification || $notification_type != null) {
147                         $vevent->deleteComponent("VALARM");
148
149                         if ($notification) {
150                                 $valarm = new valarm();
151
152                                 $valarm->setTrigger(
153                                         ($notification_type == "year" ? $notification_value : 0),
154                                         ($notification_type == "month" ? $notification_value : 0),
155                                         ($notification_type == "day" ? $notification_value : 0),
156                                         ($notification_type == "week" ? $notification_value : 0),
157                                         ($notification_type == "hour" ? $notification_value : 0),
158                                         ($notification_type == "minute" ? $notification_value : 0),
159                                         ($notification_type == "minute" ? $notification_value : 0),
160                                         true,
161                                         ($notification_value > 0)
162                                 );
163                                 $valarm->setProperty("ACTION", "DISPLAY");
164                                 $valarm->setProperty("DESCRIPTION", $subject);
165
166                                 $vevent->setComponent($valarm);
167                         }
168                 }
169
170
171                 $v->deleteComponent("vevent");
172                 $v->setComponent($vevent, trim($vevent->getProperty("uid")));
173                 $ical = $v->createCalendar();
174
175                 $calendarBackend->updateCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $old_obj->ical_uri, $ical);
176         }
177
178         /**
179          * @param array $start
180          * @param array $end
181          * @param string $subject
182          * @param bool $allday
183          * @param string $description
184          * @param string $location
185          * @param null $color
186          * @param string $timezone
187          * @param bool $notification
188          * @param null $notification_type
189          * @param null $notification_value
190          * @return array|string
191          */
192         public function addItem($start, $end, $subject, $allday = false, $description = "", $location = "", $color = null,
193                                                         $timezone = "", $notification = true, $notification_type = null, $notification_value = null)
194         {
195                 $a = get_app();
196
197                 $v = new vcalendar();
198                 $v->setConfig('unique_id', $a->get_hostname());
199
200                 $v->setProperty('method', 'PUBLISH');
201                 $v->setProperty("x-wr-calname", "AnimexxCal");
202                 $v->setProperty("X-WR-CALDESC", "Animexx Calendar");
203                 $v->setProperty("X-WR-TIMEZONE", $a->timezone);
204
205                 $vevent = dav_create_vevent($start, $end, $allday);
206                 $vevent->setLocation(icalendar_sanitize_string($location));
207                 $vevent->setSummary(icalendar_sanitize_string($subject));
208                 $vevent->setDescription(icalendar_sanitize_string($description));
209
210                 if (!is_null($color) && $color >= 0) $vevent->setProperty("X-ANIMEXX-COLOR", $color);
211
212                 if ($notification && $notification_type == null) {
213                         if ($allday) {
214                                 $notification_type  = "hour";
215                                 $notification_value = 24;
216                         } else {
217                                 $notification_type  = "minute";
218                                 $notification_value = 60;
219                         }
220                 }
221                 if ($notification) {
222                         $valarm = new valarm();
223
224                         $valarm->setTrigger(
225                                 ($notification_type == "year" ? $notification_value : 0),
226                                 ($notification_type == "month" ? $notification_value : 0),
227                                 ($notification_type == "day" ? $notification_value : 0),
228                                 ($notification_type == "week" ? $notification_value : 0),
229                                 ($notification_type == "hour" ? $notification_value : 0),
230                                 ($notification_type == "minute" ? $notification_value : 0),
231                                 ($notification_type == "second" ? $notification_value : 0),
232                                 true,
233                                 ($notification_value > 0)
234                         );
235                         $valarm->setAction("DISPLAY");
236                         $valarm->setDescription($subject);
237
238                         $vevent->setComponent($valarm);
239
240                 }
241
242                 $v->setComponent($vevent);
243                 $ical   = $v->createCalendar();
244                 $obj_id = trim($vevent->getProperty("UID"));
245
246                 $calendarBackend = new Sabre_CalDAV_Backend_Std();
247                 $calendarBackend->createCalendarObject($this->getNamespace() . "-" . $this->namespace_id, $obj_id . ".ics", $ical);
248
249                 return $obj_id . ".ics";
250         }
251
252         private function jqcal2wdcal($row, $usr_id, $base_path) {
253                 $evo             = new DBClass_friendica_jqcalendar($row);
254                 $not             = q("SELECT COUNT(*) num FROM %s%snotifications WHERE `ical_uri` = '%s' AND `ical_recurr_uri` = '%s'",
255                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($row["ical_uri"]), $row["ical_recurr_uri"]
256                 );
257                 $editable        = $this->getPermissionsItem($usr_id, $row["ical_uri"], $row["ical_recurr_uri"], $row);
258                 $recurring       = (is_null($evo->RecurringRule) || $evo->RecurringRule == "" || $evo->RecurringRule == "NULL" ? 0 : 1);
259
260                 $end = wdcal_mySql2PhpTime($evo->EndTime);
261                 if ($evo->IsAllDayEvent) $end -= 1;
262
263                 $arr             = array(
264                         "uri"               => $evo->ical_uri,
265                         "subject"           => escape_tags($evo->Subject),
266                         "start"             => wdcal_mySql2PhpTime($evo->StartTime),
267                         "end"               => $end,
268                         "is_allday"         => $evo->IsAllDayEvent,
269                         "is_moredays"       => 0,
270                         "is_recurring"      => $recurring,
271                         "color"             => (is_null($evo->Color) || $evo->Color == "" ? $this->calendarDb->calendarcolor : $evo->Color),
272                         "is_editable"       => ($editable ? 1 : 0),
273                         "is_editable_quick" => ($editable && !$recurring ? 1 : 0),
274                         "location"          => $evo->Location,
275                         "attendees"         => '',
276                         "has_notification"  => ($not[0]["num"] > 0 ? 1 : 0),
277                         "url_detail"        => $base_path . $evo->ical_uri . "/",
278                         "url_edit"          => $base_path . $evo->ical_uri . "/edit/",
279                         "special_type"      => "",
280                 );
281                 return $arr;
282         }
283
284         /**
285          * @param string $sd
286          * @param string $ed
287          * @param string $base_path
288          * @return array
289          */
290         public function listItemsByRange($sd, $ed, $base_path)
291         {
292
293                 $usr_id = IntVal($this->calendarDb->uid);
294
295                 $von           = wdcal_php2MySqlTime($sd);
296                 $bis           = wdcal_php2MySqlTime($ed);
297
298                 // @TODO Events, die früher angefangen haben, aber noch andauern
299                 $evs = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `starttime` between '%s' and '%s'",
300                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
301                         $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($von), dbesc($bis));
302
303                 $events = array();
304                 foreach ($evs as $row) $events[] = $this->jqcal2wdcal($row, $usr_id, $base_path);
305
306                 return $events;
307         }
308
309         /**
310          * @param string $uri
311          * @throws Sabre_DAV_Exception_NotFound
312          * @return array
313          */
314         public function getItemByUri($uri)
315         {
316                 $usr_id = IntVal($this->calendarDb->uid);
317                 $evs = q("SELECT * FROM %s%sjqcalendar WHERE `uid` = %d AND `namespace` = %d AND `namespace_id` = %d AND `ical_uri` = '%s'",
318                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
319                         $usr_id, $this->getNamespace(), $this->namespace_id, dbesc($uri));
320                 if (count($evs) == 0) throw new Sabre_DAV_Exception_NotFound();
321                 return $this->jqcal2wdcal($evs[0], $usr_id);
322         }
323
324
325         /**
326          * @param string $uri
327          * @return string
328          */
329         public function getItemDetailRedirect($uri) {
330                 return "/dav/wdcal/$uri/edit/";
331         }
332 }