]> git.mxchange.org Git - friendica-addons.git/blob - dav/virtual_cal_source_friendica.inc.php
privacy_image_cache: checking if the cached file really is an image
[friendica-addons.git] / dav / virtual_cal_source_friendica.inc.php
1 <?php
2
3 class FriendicaVirtualCalSourceBackend extends VirtualCalSourceBackend
4 {
5
6         /**
7          * @static
8          * @return int
9          */
10         static public function getNamespace()
11         {
12                 return CALDAV_NAMESPACE_FRIENDICA_NATIVE;
13         }
14
15         /**
16          * @static
17          * @param int $uid
18          * @param int $namespace_id
19          * @throws Sabre_DAV_Exception_NotFound
20          * @return void
21          */
22         static function createCache($uid = 0, $namespace_id = 0)
23         {
24         }
25
26
27         static private function row2array($row, $timezone, $hostname, $uid, $namespace_id) {
28                 $v = new vcalendar();
29                 $v->setConfig('unique_id', $hostname);
30
31                 $v->setProperty('method', 'PUBLISH');
32                 $v->setProperty("x-wr-calname", "AnimexxCal");
33                 $v->setProperty("X-WR-CALDESC", "Animexx Calendar");
34                 $v->setProperty("X-WR-TIMEZONE", $timezone);
35
36                 if ($row["adjust"]) {
37                         $start = datetime_convert('UTC', date_default_timezone_get(), $row["start"]);
38                         $finish = datetime_convert('UTC', date_default_timezone_get(), $row["finish"]);
39                 } else {
40                         $start = $row["start"];
41                         $finish = $row["finish"];
42                 }
43                 $allday      = (strpos($start, "00:00:00") !== false && strpos($finish, "00:00:00") !== false);
44
45                 /*
46
47                 if ($allday) {
48                         $dat = Datetime::createFromFormat("Y-m-d H:i:s", $finish_tmp);
49                         $dat->sub(new DateInterval("P1D"));
50                         $finish = datetime_convert("UTC", date_default_timezone_get(), $dat->format("Y-m-d H:i:s"));
51                         var_dump($finish);
52                 }
53                 */
54
55                 // 2012-06-29 - change to Friendica new event behaviour where summary is present and required,
56                 // but use desc for older events where summary wasn't present or required (but desc was)
57
58                 $subject     = (($row["summary"]) ? $row["summary"] : substr(preg_replace("/\[[^\]]*\]/", "", $row["desc"]), 0, 100));
59                 $description = (($row["desc"]) ? preg_replace("/\[[^\]]*\]/", "", $row["desc"]) : $row["summary"]);
60
61                 $vevent = dav_create_vevent(wdcal_mySql2icalTime($row["start"]), wdcal_mySql2icalTime($row["finish"]), false);
62                 $vevent->setLocation(icalendar_sanitize_string($row["location"]));
63                 $vevent->setSummary(icalendar_sanitize_string($subject));
64                 $vevent->setDescription(icalendar_sanitize_string($description));
65
66                 $v->setComponent($vevent);
67                 $ical  = $v->createCalendar();
68                 return array(
69                         "uid"              => $uid,
70                         "namespace"        => CALDAV_NAMESPACE_FRIENDICA_NATIVE,
71                         "namespace_id"     => $namespace_id,
72                         "date"             => $row["edited"],
73                         "data_uri"         => "friendica-" . $namespace_id . "-" . $row["id"] . "@" . $hostname,
74                         "data_subject"     => $subject,
75                         "data_location"    => $row["location"],
76                         "data_description" => $description,
77                         "data_start"       => $start,
78                         "data_end"         => $finish,
79                         "data_allday"      => $allday,
80                         "data_type"        => $row["type"],
81                         "ical"             => $ical,
82                         "ical_size"        => strlen($ical),
83                         "ical_etag"        => md5($ical),
84                 );
85
86         }
87
88         /**
89          * @static
90          * @param int $uid
91          * @param int $namespace_id
92          * @param string|int $date_from
93          * @param string|int $date_to
94          * @throws Sabre_DAV_Exception_NotFound
95          * @return array
96          */
97         static public function getItemsByTime($uid = 0, $namespace_id = 0, $date_from = "", $date_to = "")
98         {
99                 $uid          = IntVal($uid);
100                 $namespace_id = IntVal($namespace_id);
101
102                 switch ($namespace_id) {
103                         case CALDAV_FRIENDICA_MINE:
104                                 $sql_where = " AND cid = 0";
105                                 break;
106                         case CALDAV_FRIENDICA_CONTACTS:
107                                 $sql_where = " AND cid > 0";
108                                 break;
109                         default:
110                                 throw new Sabre_DAV_Exception_NotFound();
111                 }
112
113                 if ($date_from != "") {
114                         if (is_numeric($date_from)) $sql_where .= " AND `finish` >= '" . date("Y-m-d H:i:s", $date_from) . "'";
115                         else $sql_where .= " AND `finish` >= '" . dbesc($date_from) . "'";
116                 }
117                 if ($date_to != "") {
118                         if (is_numeric($date_to)) $sql_where .= " AND `start` <= '" . date("Y-m-d H:i:s", $date_to) . "'";
119                         else $sql_where .= " AND `start` <= '" . dbesc($date_to) . "'";
120                 }
121
122                 $ret  = array();
123                 $a    = get_app();
124                 $host = $a->get_hostname();
125
126
127                 $r    = q("SELECT * FROM `event` WHERE `uid` = %d " . $sql_where . " ORDER BY `start`", $uid);
128                 foreach ($r as $row) $ret[] =self::row2array($row, $a->timezone, $host, $uid, $namespace_id);
129
130                 return $ret;
131         }
132
133
134         /**
135          * @static
136          * @param int $uid
137          * @param string $uri
138          * @throws Sabre_DAV_Exception_NotFound
139          * @return array
140          */
141         static public function getItemsByUri($uid = 0, $uri)
142         {
143                 $x = explode("-", $uri);
144                 if ($x[0] != "friendica") throw new Sabre_DAV_Exception_NotFound();
145
146                 $namespace_id = IntVal($x[1]);
147                 switch ($namespace_id) {
148                         case CALDAV_FRIENDICA_MINE:
149                                 $sql_where = " AND cid = 0";
150                                 break;
151                         case CALDAV_FRIENDICA_CONTACTS:
152                                 $sql_where = " AND cid > 0";
153                                 break;
154                         default:
155                                 throw new Sabre_DAV_Exception_NotFound();
156                 }
157
158                 $a    = get_app();
159                 $host = $a->get_hostname();
160
161                 $r    = q("SELECT * FROM `event` WHERE `uid` = %d AND id = %d " . $sql_where, $uid, IntVal($x[2]));
162                 if (count($r) != 1) throw new Sabre_DAV_Exception_NotFound();
163                 $ret =self::row2array($r[0], $a->timezone, $host, $uid, $namespace_id);
164
165                 return $ret;
166         }
167
168
169 }