DAViCal
caldav-GET-functions.php
1<?php
12require_once("iCalendar.php");
13require_once("DAVResource.php");
14
15function obfuscated_event( $icalendar ) {
16 // The user is not admin / owner of this calendar looking at his calendar and can not admin the other cal,
17 // or maybe they don't have *read* access but they got here, so they must at least have free/busy access
18 // so we will present an obfuscated version of the event that just says "Busy" (translated :-)
19 $confidential = new iCalComponent();
20 $confidential->SetType($icalendar->GetType());
21 $confidential->AddProperty( 'SUMMARY', translate('Busy') );
22 $confidential->AddProperty( 'CLASS', 'CONFIDENTIAL' );
23 $confidential->SetProperties( $icalendar->GetProperties('DTSTART'), 'DTSTART' );
24 $confidential->SetProperties( $icalendar->GetProperties('RRULE'), 'RRULE' );
25 $confidential->SetProperties( $icalendar->GetProperties('DURATION'), 'DURATION' );
26 $confidential->SetProperties( $icalendar->GetProperties('DTEND'), 'DTEND' );
27 $confidential->SetProperties( $icalendar->GetProperties('UID'), 'UID' );
28 $confidential->SetProperties( $icalendar->GetProperties('CREATED'), 'CREATED' );
29
30 return $confidential;
31}
32
33function export_iCalendar( DAVResource $dav_resource ) {
34 global $session, $c, $request;
35 if ( ! $dav_resource->IsCalendar() && !(isset($c->get_includes_subcollections) && $c->get_includes_subcollections) ) {
37 header("Allow: PROPFIND,PROPPATCH,OPTIONS,MKCOL,REPORT,DELETE");
38 $request->DoResponse( 405, translate("GET requests on collections are only supported for calendars.") );
39 }
40
45 if ( isset($c->get_includes_subcollections) && $c->get_includes_subcollections ) {
46 $where = 'caldav_data.collection_id IN ';
47 $where .= '(SELECT bound_source_id FROM dav_binding WHERE dav_binding.dav_name ~ :path_match ';
48 $where .= 'UNION ';
49 $where .= 'SELECT collection_id FROM collection WHERE collection.dav_name ~ :path_match) ';
50 $params = array( ':path_match' => '^'.$dav_resource->dav_name() );
51 $distinct = 'DISTINCT ON (calendar_item.uid) ';
52 }
53 else {
54 $where = 'caldav_data.collection_id = :collection_id ';
55 $params = array( ':collection_id' => $dav_resource->resource_id() );
56 $distinct = '';
57 }
58 $sql = 'SELECT caldav_data, class, caldav_type, user_no, logged_user ';
59 $sql .= 'FROM (';
60 $sql .= 'SELECT '.$distinct.' caldav_data, class, caldav_type, calendar_item.user_no, logged_user, calendar_item.dav_name, calendar_item.dtstart, calendar_item.dtend ';
61 $sql .= 'FROM collection INNER JOIN caldav_data USING(collection_id) ';
62 $sql .= 'INNER JOIN calendar_item USING ( dav_id ) WHERE '.$where;
63 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= ' ORDER BY calendar_item.uid, calendar_item.dav_id';
64 $sql .= ') AS s1';
65 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= ' ORDER BY user_no, dtstart, dtend';
66
67 $qry = new AwlQuery( $sql, $params );
68 if ( !$qry->Exec("GET",__LINE__,__FILE__) ) {
69 $request->DoResponse( 500, translate("Database Error") );
70 }
71
76 $vcal = new iCalComponent();
77 $vcal->VCalendar();
78 $displayname = $dav_resource->GetProperty('displayname');
79 if ( isset($displayname) ) {
80 $vcal->AddProperty("X-WR-CALNAME", $displayname);
81 }
82 if ( !empty($c->auto_refresh_duration) ) {
83 $vcal->AddProperty("X-APPLE-AUTO-REFRESH-INTERVAL", $c->auto_refresh_duration);
84 $vcal->AddProperty("AUTO-REFRESH", $c->auto_refresh_duration);
85 $vcal->AddProperty("X-PUBLISHED-TTL", $c->auto_refresh_duration);
86 }
87
88 $need_zones = array();
89 $timezones = array();
90 while( $event = $qry->Fetch() ) {
91 $ical = new iCalComponent( $event->caldav_data );
92
94 $event_zones = $ical->GetComponents('VTIMEZONE',true);
95 foreach( $event_zones AS $k => $tz ) {
96 $tzid = $tz->GetPValue('TZID');
97 if ( !isset($tzid) ) continue ;
98 if ( $tzid != '' && !isset($timezones[$tzid]) ) {
99 $timezones[$tzid] = $tz;
100 }
101 }
102
104 $comps = $ical->GetComponents('VTIMEZONE',false);
105 foreach( $comps AS $k => $comp ) {
106 $tzid = $comp->GetPParamValue('DTSTART', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
107 $tzid = $comp->GetPParamValue('DUE', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
108 $tzid = $comp->GetPParamValue('DTEND', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
109
110 if ( $dav_resource->HavePrivilegeTo('all',false) || $session->user_no == $event->user_no || $session->user_no == $event->logged_user
111 || ( isset($session->email) && $c->allow_get_email_visibility && $comp->IsAttendee($session->email) ) ) {
116 $vcal->AddComponent($comp);
117 continue;
118 }
120 if ( $event->class == 'PRIVATE' ) continue;
121
122 if ( ! $dav_resource->HavePrivilegeTo('DAV::read') || $event->class == 'CONFIDENTIAL' ) {
123 $vcal->AddComponent(obfuscated_event($comp));
124 }
125 elseif ( isset($c->hide_alarm) && $c->hide_alarm ) {
126 // Otherwise we hide the alarms (if configured to)
127 $comp->ClearComponents('VALARM');
128 $vcal->AddComponent($comp);
129 }
130 else {
131 $vcal->AddComponent($comp);
132 }
133 }
134 }
135
137 foreach( $need_zones AS $tzid => $v ) {
138 if ( isset($timezones[$tzid]) ) $vcal->AddComponent($timezones[$tzid]);
139 }
140
141 return $vcal->Render();
142}