initial support for remote url BINDing
[davical.git] / inc / caldav-GET.php
blobd6885b1835d2fde4a6c3436dc7167e3b6dbd7b51
1 <?php
2 /**
3 * CalDAV Server - handle GET method
5 * @package davical
6 * @subpackage caldav
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
11 dbg_error_log("get", "GET method handler");
13 require_once("iCalendar.php");
14 require_once("DAVResource.php");
16 $dav_resource = new DAVResource($request->path);
17 $dav_resource->NeedPrivilege( array('urn:ietf:params:xml:ns:caldav:read-free-busy','DAV::read') );
18 if ( $dav_resource->IsExternal() ) {
19 update_external ( $dav_resource );
22 if ( ! $dav_resource->Exists() ) {
23 $request->DoResponse( 404, translate("Resource Not Found.") );
26 function obfuscated_event( $icalendar ) {
27 // The user is not admin / owner of this calendar looking at his calendar and can not admin the other cal,
28 // or maybe they don't have *read* access but they got here, so they must at least have free/busy access
29 // so we will present an obfuscated version of the event that just says "Busy" (translated :-)
30 $confidential = new iCalComponent();
31 $confidential->SetType($icalendar->GetType());
32 $confidential->AddProperty( 'SUMMARY', translate('Busy') );
33 $confidential->AddProperty( 'CLASS', 'CONFIDENTIAL' );
34 $confidential->SetProperties( $icalendar->GetProperties('DTSTART'), 'DTSTART' );
35 $confidential->SetProperties( $icalendar->GetProperties('RRULE'), 'RRULE' );
36 $confidential->SetProperties( $icalendar->GetProperties('DURATION'), 'DURATION' );
37 $confidential->SetProperties( $icalendar->GetProperties('DTEND'), 'DTEND' );
38 $confidential->SetProperties( $icalendar->GetProperties('UID'), 'UID' );
39 $confidential->SetProperties( $icalendar->GetProperties('CREATED'), 'CREATED' );
41 return $confidential;
45 if ( $dav_resource->IsCollection() ) {
46 if ( ! $dav_resource->IsCalendar() && !(isset($c->get_includes_subcollections) && $c->get_includes_subcollections) ) {
47 /** RFC2616 says we must send an Allow header if we send a 405 */
48 header("Allow: PROPFIND,PROPPATCH,OPTIONS,MKCOL,REPORT,DELETE");
49 $request->DoResponse( 405, translate("GET requests on collections are only supported for calendars.") );
52 /**
53 * The CalDAV specification does not define GET on a collection, but typically this is
54 * used as a .ics download for the whole collection, which is what we do also.
56 $sql = 'SELECT caldav_data, class, caldav_type, calendar_item.user_no, logged_user ';
57 $sql .= 'FROM collection INNER JOIN caldav_data USING(collection_id) INNER JOIN calendar_item USING ( dav_id ) WHERE ';
58 if ( isset($c->get_includes_subcollections) && $c->get_includes_subcollections ) {
59 $sql .= '(collection.dav_name ~ :path_match ';
60 $sql .= 'OR collection.collection_id IN (SELECT bound_source_id FROM dav_binding WHERE dav_binding.dav_name ~ :path_match)) ';
61 $params = array( ':path_match' => '^'.$request->path );
63 else {
64 $sql .= 'caldav_data.collection_id = :collection_id ';
65 $params = array( ':collection_id' => $dav_resource->resource_id() );
67 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= ' ORDER BY dav_id';
69 $qry = new AwlQuery( $sql, $params );
70 if ( !$qry->Exec("GET",__LINE__,__FILE__) ) {
71 $request->DoResponse( 500, translate("Database Error") );
74 /**
75 * Here we are constructing a whole calendar response for this collection, including
76 * the timezones that are referred to by the events we have selected.
78 $vcal = new iCalComponent();
79 $vcal->VCalendar();
80 $displayname = $dav_resource->GetProperty('displayname');
81 if ( isset($displayname) ) {
82 $vcal->AddProperty("X-WR-CALNAME", $displayname);
85 $need_zones = array();
86 $timezones = array();
87 while( $event = $qry->Fetch() ) {
88 $ical = new iCalComponent( $event->caldav_data );
90 /** Save the timezone component(s) into a minimal set for inclusion later */
91 $event_zones = $ical->GetComponents('VTIMEZONE',true);
92 foreach( $event_zones AS $k => $tz ) {
93 $tzid = $tz->GetPValue('TZID');
94 if ( !isset($tzid) ) continue ;
95 if ( $tzid != '' && !isset($timezones[$tzid]) ) {
96 $timezones[$tzid] = $tz;
100 /** Work out which ones are actually used here */
101 $comps = $ical->GetComponents('VTIMEZONE',false);
102 foreach( $comps AS $k => $comp ) {
103 $tzid = $comp->GetPParamValue('DTSTART', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
104 $tzid = $comp->GetPParamValue('DUE', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
105 $tzid = $comp->GetPParamValue('DTEND', 'TZID'); if ( isset($tzid) && !isset($need_zones[$tzid]) ) $need_zones[$tzid] = 1;
107 if ( $dav_resource->HavePrivilegeTo('all',false) || $session->user_no == $event->user_no || $session->user_no == $event->logged_user
108 || ( $c->allow_get_email_visibility && $comp->IsAttendee($session->email) ) ) {
110 * These people get to see all of the event, and they should always
111 * get any alarms as well.
113 $vcal->AddComponent($comp);
114 continue;
116 /** No visibility even of the existence of these events if they aren't admin/owner/attendee */
117 if ( $event->class == 'PRIVATE' ) continue;
119 if ( ! $dav_resource->HavePrivilegeTo('DAV::read') || $event->class == 'CONFIDENTIAL' ) {
120 $vcal->AddComponent(obfuscated_event($comp));
122 elseif ( isset($c->hide_alarm) && $c->hide_alarm ) {
123 // Otherwise we hide the alarms (if configured to)
124 $comp->ClearComponents('VALARM');
125 $vcal->AddComponent($comp);
127 else {
128 $vcal->AddComponent($comp);
133 /** Put the timezones on there that we need */
134 foreach( $need_zones AS $tzid => $v ) {
135 if ( isset($timezones[$tzid]) ) $vcal->AddComponent($timezones[$tzid]);
138 $response = $vcal->Render();
139 header( 'Content-Length: '.strlen($response) );
140 header( 'Etag: '.$dav_resource->unique_tag() );
141 $request->DoResponse( 200, ($request->method == 'HEAD' ? '' : $response), 'text/calendar; charset="utf-8"' );
145 // Just a single event then
147 $resource = $dav_resource->resource();
148 $ic = new iCalComponent( $resource->caldav_data );
150 $resource->caldav_data = preg_replace( '{(?<!\r)\n}', "\r\n", $resource->caldav_data);
152 /** Default deny... */
153 $allowed = false;
154 if ( $dav_resource->HavePrivilegeTo('all', false) || $session->user_no == $resource->user_no || $session->user_no == $resource->logged_user
155 || ( $c->allow_get_email_visibility && $ic->IsAttendee($session->email) ) ) {
157 * These people get to see all of the event, and they should always
158 * get any alarms as well.
160 $allowed = true;
162 else if ( $resource->class != 'PRIVATE' ) {
163 $allowed = true; // but we may well obfuscate it below
164 if ( ! $dav_resource->HavePrivilegeTo('DAV::read') || ( $resource->class == 'CONFIDENTIAL' && ! $request->HavePrivilegeTo('DAV::write-content') ) ) {
165 $ical = new iCalComponent( $resource->caldav_data );
166 $comps = $ical->GetComponents('VTIMEZONE',false);
167 $confidential = obfuscated_event($comps[0]);
168 $ical->SetComponents( array($confidential), $resource->caldav_type );
169 $resource->caldav_data = $ical->Render();
172 // else $resource->class == 'PRIVATE' and this person may not see it.
174 if ( ! $allowed ) {
175 $request->DoResponse( 403, translate("Forbidden") );
178 header( 'Etag: "'.$resource->dav_etag.'"' );
179 header( 'Content-Length: '.strlen($resource->caldav_data) );
181 $contenttype = 'text/plain';
182 switch( $resource->caldav_type ) {
183 case 'VJOURNAL':
184 case 'VEVENT':
185 case 'VTODO':
186 $contenttype = 'text/calendar';
187 break;
189 case 'VCARD':
190 $contenttype = 'text/vcard';
191 break;
194 $request->DoResponse( 200, ($request->method == 'HEAD' ? '' : $resource->caldav_data), $contenttype.'; charset="utf-8"' );