3 * CalDAV Server - functions used by GET method handler
7 * @author Andrew McMillan <andrew@mcmillan.net.nz>
8 * @copyright Catalyst .Net Ltd, Morphoss Ltd <http://www.morphoss.com/>
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2 or later
12 require_once("iCalendar.php");
13 require_once("DAVResource.php");
15 function 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' );
33 function export_iCalendar( DAVResource
$dav_resource ) {
35 if ( ! $dav_resource->IsCalendar() && !(isset($c->get_includes_subcollections
) && $c->get_includes_subcollections
) ) {
36 /** RFC2616 says we must send an Allow header if we send a 405 */
37 header("Allow: PROPFIND,PROPPATCH,OPTIONS,MKCOL,REPORT,DELETE");
38 $request->DoResponse( 405, translate("GET requests on collections are only supported for calendars.") );
42 * The CalDAV specification does not define GET on a collection, but typically this is
43 * used as a .ics download for the whole collection, which is what we do also.
45 $sql = 'SELECT caldav_data, class, caldav_type, calendar_item.user_no, logged_user ';
46 $sql .= 'FROM collection INNER JOIN caldav_data USING(collection_id) INNER JOIN calendar_item USING ( dav_id ) WHERE ';
47 if ( isset($c->get_includes_subcollections
) && $c->get_includes_subcollections
) {
48 $sql .= '(collection.dav_name ~ :path_match ';
49 $sql .= 'OR collection.collection_id IN (SELECT bound_source_id FROM dav_binding WHERE dav_binding.dav_name ~ :path_match)) ';
50 $params = array( ':path_match' => '^'.$request->path
);
53 $sql .= 'caldav_data.collection_id = :collection_id ';
54 $params = array( ':collection_id' => $dav_resource->resource_id() );
56 if ( isset($c->strict_result_ordering
) && $c->strict_result_ordering
) $sql .= ' ORDER BY dav_id';
58 $qry = new AwlQuery( $sql, $params );
59 if ( !$qry->Exec("GET",__LINE__
,__FILE__
) ) {
60 $request->DoResponse( 500, translate("Database Error") );
64 * Here we are constructing a whole calendar response for this collection, including
65 * the timezones that are referred to by the events we have selected.
67 $vcal = new iCalComponent();
69 $displayname = $dav_resource->GetProperty('displayname');
70 if ( isset($displayname) ) {
71 $vcal->AddProperty("X-WR-CALNAME", $displayname);
73 if ( !empty($c->auto_refresh_duration
) ) {
74 $vcal->AddProperty("X-APPLE-AUTO-REFRESH-INTERVAL", $c->auto_refresh_duration
);
75 $vcal->AddProperty("AUTO-REFRESH", $c->auto_refresh_duration
);
78 $need_zones = array();
80 while( $event = $qry->Fetch() ) {
81 $ical = new iCalComponent( $event->caldav_data
);
83 /** Save the timezone component(s) into a minimal set for inclusion later */
84 $event_zones = $ical->GetComponents('VTIMEZONE',true);
85 foreach( $event_zones AS $k => $tz ) {
86 $tzid = $tz->GetPValue('TZID');
87 if ( !isset($tzid) ) continue ;
88 if ( $tzid != '' && !isset($timezones[$tzid]) ) {
89 $timezones[$tzid] = $tz;
93 /** Work out which ones are actually used here */
94 $comps = $ical->GetComponents('VTIMEZONE',false);
95 foreach( $comps AS $k => $comp ) {
96 $tzid = $comp->GetPParamValue('DTSTART', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
97 $tzid = $comp->GetPParamValue('DUE', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
98 $tzid = $comp->GetPParamValue('DTEND', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
100 if ( $dav_resource->HavePrivilegeTo('all',false) ||
$session->user_no
== $event->user_no ||
$session->user_no
== $event->logged_user
101 ||
( isset($session->email
) && $c->allow_get_email_visibility
&& $comp->IsAttendee($session->email
) ) ) {
103 * These people get to see all of the event, and they should always
104 * get any alarms as well.
106 $vcal->AddComponent($comp);
109 /** No visibility even of the existence of these events if they aren't admin/owner/attendee */
110 if ( $event->class == 'PRIVATE' ) continue;
112 if ( ! $dav_resource->HavePrivilegeTo('DAV::read') ||
$event->class == 'CONFIDENTIAL' ) {
113 $vcal->AddComponent(obfuscated_event($comp));
115 elseif ( isset($c->hide_alarm
) && $c->hide_alarm
) {
116 // Otherwise we hide the alarms (if configured to)
117 $comp->ClearComponents('VALARM');
118 $vcal->AddComponent($comp);
121 $vcal->AddComponent($comp);
126 /** Put the timezones on there that we need */
127 foreach( $need_zones AS $tzid => $v ) {
128 if ( isset($timezones[$tzid]) ) $vcal->AddComponent($timezones[$tzid]);
131 return $vcal->Render();