]> git.mxchange.org Git - friendica-addons.git/blob - dav/common/calendar.fnk.php
Merge remote branch 'upstream/master'
[friendica-addons.git] / dav / common / calendar.fnk.php
1 <?php
2
3
4 class vcard_source_data_email
5 {
6         public $email, $type;
7
8         function __construct($type, $email)
9         {
10                 $this->email = $email;
11                 $this->type  = $type;
12         }
13 }
14
15 class vcard_source_data_homepage
16 {
17         public $homepage, $type;
18
19         function __construct($type, $homepage)
20         {
21                 $this->homepage = $homepage;
22                 $this->type     = $type;
23         }
24 }
25
26 class vcard_source_data_telephone
27 {
28         public $telephone, $type;
29
30         function __construct($type, $telephone)
31         {
32                 $this->telephone = $telephone;
33                 $this->type      = $type;
34         }
35 }
36
37 class vcard_source_data_socialnetwork
38 {
39         public $nick, $type, $url;
40
41         function __construct($type, $nick, $url)
42         {
43                 $this->nick = $nick;
44                 $this->type = $type;
45                 $this->url  = $url;
46         }
47 }
48
49 class vcard_source_data_address
50 {
51         public $street, $street2, $zip, $city, $country, $type;
52 }
53
54 class vcard_source_data_photo
55 {
56         public $binarydata;
57         public $width, $height;
58         public $type;
59 }
60
61 class vcard_source_data
62 {
63         function __construct($name_first, $name_middle, $name_last)
64         {
65                 $this->name_first  = $name_first;
66                 $this->name_middle = $name_middle;
67                 $this->name_last   = $name_last;
68         }
69
70         public $name_first, $name_middle, $name_last;
71         public $last_update;
72         public $picture_data;
73
74         /** @var array|vcard_source_data_telephone[] $telephones */
75         public $telephones;
76
77         /** @var array|vcard_source_data_homepage[] $homepages */
78         public $homepages;
79
80         /** @var array|vcard_source_data_socialnetwork[] $socialnetworks */
81         public $socialnetworks;
82
83         /** @var array|vcard_source_data_email[] $email */
84         public $emails;
85
86         /** @var array|vcard_source_data_addresses[] $addresses */
87         public $addresses;
88
89         /** @var vcard_source_data_photo */
90         public $photo;
91 }
92
93 ;
94
95
96 /**
97  * @param vcard_source_data $vcardsource
98  * @return string
99  */
100 function vcard_source_compile($vcardsource)
101 {
102         $str = "BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Friendica//DAV-Plugin//EN\r\n";
103         $str .= "N:" . str_replace(";", ",", $vcardsource->name_last) . ";" . str_replace(";", ",", $vcardsource->name_first) . ";" . str_replace(";", ",", $vcardsource->name_middle) . ";;\r\n";
104         $str .= "FN:" . str_replace(";", ",", $vcardsource->name_first) . " " . str_replace(";", ",", $vcardsource->name_middle) . " " . str_replace(";", ",", $vcardsource->name_last) . "\r\n";
105         $str .= "REV:" . str_replace(" ", "T", $vcardsource->last_update) . "Z\r\n";
106
107         $item_count = 0;
108         for ($i = 0; $i < count($vcardsource->homepages); $i++) {
109                 if ($i == 0) $str .= "URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
110                 else {
111                         $c = ++$item_count;
112                         $str .= "item$c.URL;type=" . $vcardsource->homepages[0]->type . ":" . $vcardsource->homepages[0]->homepage . "\r\n";
113                         $str .= "item$c.X-ABLabel:_\$!<HomePage>!\$_\r\n";
114                 }
115         }
116
117         if (is_object($vcardsource->photo)) {
118                 $data = base64_encode($vcardsource->photo->binarydata);
119                 $str .= "PHOTO;ENCODING=BASE64;TYPE=" . $vcardsource->photo->type . ":" . $data . "\r\n";
120         }
121
122         if (isset($vcardsource->socialnetworks) && is_array($vcardsource->socialnetworks)) foreach ($vcardsource->socialnetworks as $netw) switch ($netw->type) {
123                 case "dfrn":
124                         $str .= "X-SOCIALPROFILE;type=dfrn;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
125                         break;
126                 case "facebook":
127                         $str .= "X-SOCIALPROFILE;type=facebook;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
128                         break;
129                 case "twitter":
130                         $str .= "X-SOCIALPROFILE;type=twitter;x-user=" . $netw->nick . ":" . $netw->url . "\r\n";
131                         break;
132         }
133
134         $str .= "END:VCARD\r\n";
135         return $str;
136 }
137
138
139 /**
140  * @param array $start
141  * @param array $end
142  * @param bool $allday
143  * @return vevent
144  */
145 function dav_create_vevent($start, $end, $allday)
146 {
147         if ($end["year"] < $start["year"] ||
148                 ($end["year"] == $start["year"] && $end["month"] < $start["month"]) ||
149                 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] < $start["day"]) ||
150                 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] < $start["hour"]) ||
151                 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] < $start["minute"]) ||
152                 ($end["year"] == $start["year"] && $end["month"] == $start["month"] && $end["day"] == $start["day"] && $end["hour"] == $start["hour"] && $end["minute"] == $start["minute"] && $end["second"] < $start["second"])
153         ) {
154                 $end = $start;
155         } // DTEND muss <= DTSTART
156
157         $vevent = new vevent();
158         if ($allday) {
159                 $vevent->setDtstart($start["year"], $start["month"], $start["day"], FALSE, FALSE, FALSE, FALSE, array("VALUE"=> "DATE"));
160                 $end = IntVal(mktime(0, 0, 0, $end["month"], $end["day"], $end["year"]) + 3600 * 24);
161
162                 // If a DST change occurs on the current day
163                 $end += IntVal(date("Z", ($end - 3600 * 24)) - date("Z", $end));
164
165                 $vevent->setDtend(date("Y", $end), date("m", $end), date("d", $end), FALSE, FALSE, FALSE, FALSE, array("VALUE"=> "DATE"));
166         } else {
167                 $vevent->setDtstart($start["year"], $start["month"], $start["day"], $start["hour"], $start["minute"], $start["second"], FALSE, array("VALUE"=> "DATE-TIME"));
168                 $vevent->setDtend($end["year"], $end["month"], $end["day"], $end["hour"], $end["minute"], $end["second"], FALSE, array("VALUE"=> "DATE-TIME"));
169         }
170         return $vevent;
171 }
172
173
174 /**
175  * @param int $phpDate (UTC)
176  * @return string (Lokalzeit)
177  */
178 function wdcal_php2MySqlTime($phpDate)
179 {
180         return date("Y-m-d H:i:s", $phpDate);
181 }
182
183 /**
184  * @param string $sqlDate
185  * @return int
186  */
187 function wdcal_mySql2PhpTime($sqlDate)
188 {
189         $ts = DateTime::createFromFormat("Y-m-d H:i:s", $sqlDate);
190         return $ts->format("U");
191 }
192
193 /**
194  * @param string $myqlDate
195  * @return array
196  */
197 function wdcal_mySql2icalTime($myqlDate)
198 {
199         $x             = explode(" ", $myqlDate);
200         $y             = explode("-", $x[0]);
201         $ret           = array("year"=> $y[0], "month"=> $y[1], "day"=> $y[2]);
202         $y             = explode(":", $x[1]);
203         $ret["hour"]   = $y[0];
204         $ret["minute"] = $y[1];
205         $ret["second"] = $y[2];
206         return $ret;
207 }
208
209
210 /**
211  * @param string $str
212  * @return string
213  */
214 function icalendar_sanitize_string($str = "")
215 {
216         $str = str_replace("\r\n", "\n", $str);
217         $str = str_replace("\n\r", "\n", $str);
218         $str = str_replace("\r", "\n", $str);
219         return $str;
220 }
221
222
223 /**
224  * @param DBClass_friendica_calendars $calendar
225  * @param DBClass_friendica_calendarobjects $calendarobject
226  */
227 function renderCalDavEntry_data(&$calendar, &$calendarobject)
228 {
229         $a = get_app();
230
231         $v = new vcalendar();
232         $v->setConfig('unique_id', $a->get_hostname());
233         $v->parse($calendarobject->calendardata);
234         $v->sort();
235
236         $eventArray = $v->selectComponents(2009, 1, 1, date("Y") + 2, 12, 30);
237
238         $start_min = $end_max = "";
239
240         $allday   = $summary = $vevent = $rrule = $color = $start = $end = null;
241         $location = $description = "";
242
243         foreach ($eventArray as $yearArray) {
244                 foreach ($yearArray as $monthArray) {
245                         foreach ($monthArray as $day => $dailyEventsArray) {
246                                 foreach ($dailyEventsArray as $vevent) {
247                                         /** @var $vevent vevent  */
248                                         $start  = "";
249                                         $rrule  = "NULL";
250                                         $allday = 0;
251
252                                         $dtstart = $vevent->getProperty('X-CURRENT-DTSTART');
253                                         if (is_array($dtstart)) {
254                                                 $start = "'" . $dtstart[1] . "'";
255                                                 if (strpos($dtstart[1], ":") === false) $allday = 1;
256                                         } else {
257                                                 $dtstart = $vevent->getProperty('dtstart');
258                                                 if (isset($dtstart["day"]) && $dtstart["day"] == $day) { // Mehrtägige Events nur einmal rein
259                                                         if (isset($dtstart["hour"])) $start = "'" . $dtstart["year"] . "-" . $dtstart["month"] . "-" . $dtstart["day"] . " " . $dtstart["hour"] . ":" . $dtstart["minute"] . ":" . $dtstart["secont"] . "'";
260                                                         else {
261                                                                 $start  = "'" . $dtstart["year"] . "-" . $dtstart["month"] . "-" . $dtstart["day"] . " 00:00:00'";
262                                                                 $allday = 1;
263                                                         }
264                                                 }
265                                         }
266
267                                         $dtend = $vevent->getProperty('X-CURRENT-DTEND');
268                                         if (is_array($dtend)) {
269                                                 $end = "'" . $dtend[1] . "'";
270                                                 if (strpos($dtend[1], ":") === false) $allday = 1;
271                                         } else {
272                                                 $dtend = $vevent->getProperty('dtend');
273                                                 if (isset($dtend["hour"])) $end = "'" . $dtend["year"] . "-" . $dtend["month"] . "-" . $dtend["day"] . " " . $dtend["hour"] . ":" . $dtend["minute"] . ":" . $dtend["second"] . "'";
274                                                 else {
275                                                         $end    = "'" . $dtend["year"] . "-" . $dtend["month"] . "-" . $dtend["day"] . " 00:00:00' - INTERVAL 1 SECOND";
276                                                         $allday = 1;
277                                                 }
278                                         }
279                                         $summary     = $vevent->getProperty('summary');
280                                         $description = $vevent->getProperty('description');
281                                         $location    = $vevent->getProperty('location');
282                                         $rrule_prob  = $vevent->getProperty('rrule');
283                                         if ($rrule_prob != null) {
284                                                 $rrule = $vevent->createRrule();
285                                                 $rrule = "'" . dbesc($rrule) . "'";
286                                         }
287                                         $color_ = $vevent->getProperty("X-ANIMEXX-COLOR");
288                                         $color  = (is_array($color_) ? $color_[1] : "NULL");
289
290                                         if ($start_min == "" || preg_replace("/[^0-9]/", "", $start) < preg_replace("/[^0-9]/", "", $start_min)) $start_min = $start;
291                                         if ($end_max == "" || preg_replace("/[^0-9]/", "", $end) > preg_replace("/[^0-9]/", "", $start_min)) $end_max = $end;
292                                 }
293                         }
294                 }
295         }
296
297         if ($start_min != "") {
298
299                 if ($allday && mb_strlen($end_max) == 12) {
300                         $x       = explode("-", str_replace("'", "", $end_max));
301                         $time    = mktime(0, 0, 0, IntVal($x[1]), IntVal($x[2]), IntVal($x[0]));
302                         $end_max = date("'Y-m-d H:i:s'", ($time - 1));
303                 }
304
305                 q("INSERT INTO %s%sjqcalendar (`uid`, `namespace`, `namespace_id`, `ical_uri`, `Subject`, `Location`, `Description`, `StartTime`, `EndTime`, `IsAllDayEvent`, `RecurringRule`, `Color`)
306                         VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', %s, %s, %d, '%s', '%s')",
307                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
308                         IntVal($calendar->uid), IntVal($calendarobject->namespace), IntVal($calendarobject->namespace_id), dbesc($calendarobject->uri), dbesc($summary),
309                         dbesc($location), dbesc(str_replace("\\n", "\n", $description)), $start_min, $end_max, IntVal($allday), dbesc($rrule), dbesc($color)
310                 );
311
312                 foreach ($vevent->components as $comp) {
313                         /** @var $comp calendarComponent */
314                         $trigger   = $comp->getProperty("TRIGGER");
315                         $sql_field = ($trigger["relatedStart"] ? $start : $end);
316                         $sql_op    = ($trigger["before"] ? "DATE_SUB" : "DATE_ADD");
317                         $num       = "";
318                         $rel_type  = "";
319                         $rel_value = 0;
320                         if (isset($trigger["second"])) {
321                                 $num       = IntVal($trigger["second"]) . " SECOND";
322                                 $rel_type  = "second";
323                                 $rel_value = IntVal($trigger["second"]);
324                         }
325                         if (isset($trigger["minute"])) {
326                                 $num       = IntVal($trigger["minute"]) . " MINUTE";
327                                 $rel_type  = "minute";
328                                 $rel_value = IntVal($trigger["minute"]);
329                         }
330                         if (isset($trigger["hour"])) {
331                                 $num       = IntVal($trigger["hour"]) . " HOUR";
332                                 $rel_type  = "hour";
333                                 $rel_value = IntVal($trigger["hour"]);
334                         }
335                         if (isset($trigger["day"])) {
336                                 $num       = IntVal($trigger["day"]) . " DAY";
337                                 $rel_type  = "day";
338                                 $rel_value = IntVal($trigger["day"]);
339                         }
340                         if (isset($trigger["week"])) {
341                                 $num       = IntVal($trigger["week"]) . " WEEK";
342                                 $rel_type  = "week";
343                                 $rel_value = IntVal($trigger["week"]);
344                         }
345                         if (isset($trigger["month"])) {
346                                 $num       = IntVal($trigger["month"]) . " MONTH";
347                                 $rel_type  = "month";
348                                 $rel_value = IntVal($trigger["month"]);
349                         }
350                         if (isset($trigger["year"])) {
351                                 $num       = IntVal($trigger["year"]) . " YEAR";
352                                 $rel_type  = "year";
353                                 $rel_value = IntVal($trigger["year"]);
354                         }
355                         if ($trigger["before"]) $rel_value *= -1;
356
357                         if ($rel_type != "") {
358                                 $not_date = "$sql_op($sql_field, INTERVAL $num)";
359                                 q("INSERT INTO %s%snotifications (`uid`, `ical_uri`, `rel_type`, `rel_value`, `alert_date`, `notified`) VALUES ('%s', '%s', '%s', '%s', %s, IF(%s < NOW(), 1, 0))",
360                                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX,
361                                         IntVal($calendar->uid), dbesc($calendarobject->uri), dbesc($rel_type), IntVal($rel_value), $not_date, $not_date);
362                         }
363                 }
364         }
365 }
366
367
368 /**
369  *
370  */
371 function renderAllCalDavEntries()
372 {
373         q("DELETE FROM %s%sjqcalendar", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
374         q("DELETE FROM %s%snotifications", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
375         $calendars = q("SELECT * FROM %s%scalendars", CALDAV_SQL_DB, CALDAV_SQL_PREFIX);
376         $anz       = count($calendars);
377         $i         = 0;
378         foreach ($calendars as $calendar) {
379                 $cal = new DBClass_friendica_calendars($calendar);
380                 $i++;
381                 if (($i % 100) == 0) echo "$i / $anz\n";
382                 $calobjs = q("SELECT * FROM %s%scalendarobjects WHERE `namespace` = %d AND `namespace_id` = %d",
383                         CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($calendar["namespace"]), IntVal($calendar["namespace_id"]));
384                 foreach ($calobjs as $calobj) {
385                         $obj = new DBClass_friendica_calendarobjects($calobj);
386                         renderCalDavEntry_data($cal, $obj);
387                 }
388         }
389 }
390
391
392 /**
393  * @param string $uri
394  * @return bool
395  */
396 function renderCalDavEntry_uri($uri)
397 {
398         q("DELETE FROM %s%sjqcalendar WHERE `ical_uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($uri));
399         q("DELETE FROM %s%snotifications WHERE `ical_uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($uri));
400
401         $calobj = q("SELECT * FROM %s%scalendarobjects WHERE `uri` = '%s'", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, dbesc($uri));
402         if (count($calobj) == 0) return false;
403         $cal       = new DBClass_friendica_calendarobjects($calobj[0]);
404         $calendars = q("SELECT * FROM %s%scalendars WHERE `namespace`=%d AND `namespace_id`=%d", CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($cal->namespace), IntVal($cal->namespace_id));
405         $calendar  = new DBClass_friendica_calendars($calendars[0]);
406         renderCalDavEntry_data($calendar, $cal);
407         return true;
408 }
409
410
411 /**
412  * @param $user_id
413  * @return array|DBClass_friendica_calendars[]
414  */
415 function dav_getMyCals($user_id)
416 {
417         $d    = q("SELECT * FROM %s%scalendars WHERE `uid` = %d ORDER BY `calendarorder` ASC",
418                 CALDAV_SQL_DB, CALDAV_SQL_PREFIX, IntVal($user_id), CALDAV_NAMESPACE_PRIVATE
419         );
420         $cals = array();
421         foreach ($d as $e) $cals[] = new DBClass_friendica_calendars($e);
422         return $cals;
423 }
424
425
426 /**
427  * @param mixed $obj
428  * @return string
429  */
430 function wdcal_jsonp_encode($obj)
431 {
432         $str = json_encode($obj);
433         if (isset($_REQUEST["callback"])) {
434                 $str = $_REQUEST["callback"] . "(" . $str . ")";
435         }
436         return $str;
437 }
438
439
440 /**
441  * @param string $day
442  * @param int $weekstartday
443  * @param int $num_days
444  * @param string $type
445  * @return array
446  */
447 function wdcal_get_list_range_params($day, $weekstartday, $num_days, $type)
448 {
449         $phpTime = IntVal($day);
450         switch ($type) {
451                 case "month":
452                         $st = mktime(0, 0, 0, date("m", $phpTime), 1, date("Y", $phpTime));
453                         $et = mktime(0, 0, -1, date("m", $phpTime) + 1, 1, date("Y", $phpTime));
454                         break;
455                 case "week":
456                         //suppose first day of a week is monday
457                         $monday = date("d", $phpTime) - date('N', $phpTime) + 1;
458                         //echo date('N', $phpTime);
459                         $st = mktime(0, 0, 0, date("m", $phpTime), $monday, date("Y", $phpTime));
460                         $et = mktime(0, 0, -1, date("m", $phpTime), $monday + 7, date("Y", $phpTime));
461                         break;
462                 case "multi_days":
463                         //suppose first day of a week is monday
464                         $monday = date("d", $phpTime) - date('N', $phpTime) + $weekstartday;
465                         //echo date('N', $phpTime);
466                         $st = mktime(0, 0, 0, date("m", $phpTime), $monday, date("Y", $phpTime));
467                         $et = mktime(0, 0, -1, date("m", $phpTime), $monday + $num_days, date("Y", $phpTime));
468                         break;
469                 case "day":
470                         $st = mktime(0, 0, 0, date("m", $phpTime), date("d", $phpTime), date("Y", $phpTime));
471                         $et = mktime(0, 0, -1, date("m", $phpTime), date("d", $phpTime) + 1, date("Y", $phpTime));
472                         break;
473                 default:
474                         return array(0, 0);
475         }
476         return array($st, $et);
477 }
478
479
480 /**
481  *
482  */
483 function wdcal_print_feed($base_path = "")
484 {
485         $user_id = dav_compat_get_curr_user_id();
486         $cals    = array();
487         if (isset($_REQUEST["cal"])) foreach ($_REQUEST["cal"] as $c) {
488                 $x              = explode("-", $c);
489                 $calendarSource = wdcal_calendar_factory($user_id, $x[0], $x[1]);
490                 $calp           = $calendarSource->getPermissionsCalendar($user_id);
491                 if ($calp["read"]) $cals[] = $calendarSource;
492         }
493
494         $ret = null;
495         /** @var $cals array|AnimexxCalSource[] */
496
497         $method = $_GET["method"];
498         switch ($method) {
499                 case "add":
500                         $cs = null;
501                         foreach ($cals as $c) if ($cs == null) {
502                                 $x = $c->getPermissionsCalendar($user_id);
503                                 if ($x["read"]) $cs = $c;
504                         }
505                         if ($cs == null) {
506                                 echo wdcal_jsonp_encode(array('IsSuccess' => false,
507                                                                                           'Msg'       => t('No access')));
508                                 killme();
509                         }
510                         try {
511                                 $start  = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarStartTime"]));
512                                 $end    = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarEndTime"]));
513                                 $newuri = $cs->addItem($start, $end, $_REQUEST["CalendarTitle"], $_REQUEST["IsAllDayEvent"]);
514                                 $ret    = array(
515                                         'IsSuccess' => true,
516                                         'Msg'       => 'add success',
517                                         'Data'      => $newuri,
518                                 );
519
520                         } catch (Exception $e) {
521                                 $ret = array(
522                                         'IsSuccess' => false,
523                                         'Msg'       => $e->__toString(),
524                                 );
525                         }
526                         break;
527                 case "list":
528                         $weekstartday = (isset($_REQUEST["weekstartday"]) ? IntVal($_REQUEST["weekstartday"]) : 1); // 1 = Monday
529                         $num_days     = (isset($_REQUEST["num_days"]) ? IntVal($_REQUEST["num_days"]) : 7);
530                         $ret          = null;
531
532                         $date          = wdcal_get_list_range_params($_REQUEST["showdate"], $weekstartday, $num_days, $_REQUEST["viewtype"]);
533                         $ret           = array();
534                         $ret['events'] = array();
535                         $ret["issort"] = true;
536                         $ret["start"]  = $date[0];
537                         $ret["end"]    = $date[1];
538                         $ret['error']  = null;
539
540                         foreach ($cals as $c) {
541                                 $events        = $c->listItemsByRange($date[0], $date[1], $base_path);
542                                 $ret["events"] = array_merge($ret["events"], $events);
543                         }
544
545                         $tmpev = array();
546                         foreach ($ret["events"] as $e) {
547                                 if (!isset($tmpev[$e["start"]])) $tmpev[$e["start"]] = array();
548                                 $tmpev[$e["start"]][] = $e;
549                         }
550                         ksort($tmpev);
551                         $ret["events"] = array();
552                         foreach ($tmpev as $e) foreach ($e as $f) $ret["events"][] = $f;
553
554                         break;
555                 case "update":
556                         $found = false;
557                         $start = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarStartTime"]));
558                         $end   = wdcal_mySql2icalTime(wdcal_php2MySqlTime($_REQUEST["CalendarEndTime"]));
559                         foreach ($cals as $c) try {
560                                 $permissions_item = $c->getPermissionsItem($user_id, $_REQUEST["calendarId"], "");
561                                 if ($permissions_item["write"]) {
562                                         $c->updateItem($_REQUEST["calendarId"], $start, $end);
563                                         $found = true;
564                                 }
565                         } catch (Exception $e) {
566                         }
567                         ;
568
569                         if ($found) {
570                                 $ret = array(
571                                         'IsSuccess' => true,
572                                         'Msg'       => 'Succefully',
573                                 );
574                         } else {
575                                 echo wdcal_jsonp_encode(array('IsSuccess' => false,
576                                                                                           'Msg'       => t('No access')));
577                                 killme();
578                         }
579
580                         try {
581                         } catch (Exception $e) {
582                                 $ret = array(
583                                         'IsSuccess' => false,
584                                         'Msg'       => $e->__toString(),
585                                 );
586                         }
587                         break;
588                 case "remove":
589                         $found = false;
590                         foreach ($cals as $c) try {
591                                 $permissions_item = $c->getPermissionsItem($user_id, $_REQUEST["calendarId"], "");
592                                 if ($permissions_item["write"]) $c->removeItem($_REQUEST["calendarId"]);
593                         } catch (Exception $e) {
594                         }
595
596                         if ($found) {
597                                 $ret = array(
598                                         'IsSuccess' => true,
599                                         'Msg'       => 'Succefully',
600                                 );
601                         } else {
602                                 echo wdcal_jsonp_encode(array('IsSuccess' => false,
603                                                                                           'Msg'       => t('No access')));
604                                 killme();
605                         }
606                         break;
607         }
608         echo wdcal_jsonp_encode($ret);
609         killme();
610 }
611