From f71bba3747d700f89cd140ac201ea066568bf613 Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Wed, 23 Jun 2010 14:22:26 +1200 Subject: [PATCH] Work on making this work for CardDAV. --- inc/caldav-REPORT-multiget.php | 2 +- inc/caldav-REPORT.php | 134 +++++++++++++++++++++++++++++++++ 2 files changed, 135 insertions(+), 1 deletion(-) diff --git a/inc/caldav-REPORT-multiget.php b/inc/caldav-REPORT-multiget.php index 607fc0d8..e66501fa 100644 --- a/inc/caldav-REPORT-multiget.php +++ b/inc/caldav-REPORT-multiget.php @@ -108,7 +108,7 @@ if ( $qry->Exec('REPORT',__LINE__,__FILE__) && $qry->rows() > 0 ) { $expanded = expand_event_instances($ics, $expand_range_start, $expand_range_end); $calendar_object->caldav_data = $expanded->Render(); } - $responses[] = calendar_to_xml( $properties, $calendar_object ); + $responses[] = component_to_xml( $properties, $calendar_object ); } } diff --git a/inc/caldav-REPORT.php b/inc/caldav-REPORT.php index b4a8b0a3..ebf07341 100644 --- a/inc/caldav-REPORT.php +++ b/inc/caldav-REPORT.php @@ -167,6 +167,140 @@ function calendar_to_xml( $properties, $item ) { return $response; } + +/** +* Return XML for a single component from the DB +* +* @param array $properties The properties for this component +* @param string $item The DB row data for this component +* +* @return string An XML document which is the response for the component +*/ +function component_to_xml( $properties, $item ) { + global $session, $c, $request, $reply; + + dbg_error_log("REPORT","Building XML Response for item '%s'", $item->dav_name ); + + $denied = array(); + $unsupported = array(); + $caldav_data = $item->caldav_data; + $displayname = preg_replace( '{^.*/}', '', $item->dav_name ); + $type = 'unknown'; + $contenttype = 'text/plain'; + switch( $item->caldav_type ) { + case 'VJOURNAL': + case 'VEVENT': + case 'VTODO': + $displayname = $item->summary; + $type = 'calendar'; + $contenttype = 'text/calendar'; + break; + + case 'VCARD': + $displayname = $item->fn; + $type = 'vcard'; + $contenttype = 'text/x-vcard'; + break; + } + if ( isset($properties['calendar-data']) || isset($properties['displayname']) ) { + if ( !$request->AllowedTo('all') && $session->user_no != $item->user_no ){ + // the user is not admin / owner of this calendarlooking at his calendar and can not admin the other cal + /** @todo We should examine the ORGANIZER and ATTENDEE fields in the event. If this person is there then they should see this */ + if ( $type == 'calendar' && $item->class == 'CONFIDENTIAL' || !$request->AllowedTo('read') ) { + $ical = new iCalComponent( $caldav_data ); + $resources = $ical->GetComponents('VTIMEZONE',false); + $first = $resources[0]; + + // if the event is confidential we fake one that just says "Busy" + $confidential = new iCalComponent(); + $confidential->SetType($first->GetType()); + $confidential->AddProperty( 'SUMMARY', translate('Busy') ); + $confidential->AddProperty( 'CLASS', 'CONFIDENTIAL' ); + $confidential->SetProperties( $first->GetProperties('DTSTART'), 'DTSTART' ); + $confidential->SetProperties( $first->GetProperties('RRULE'), 'RRULE' ); + $confidential->SetProperties( $first->GetProperties('DURATION'), 'DURATION' ); + $confidential->SetProperties( $first->GetProperties('DTEND'), 'DTEND' ); + $confidential->SetProperties( $first->GetProperties('UID'), 'UID' ); + $ical->SetComponents(array($confidential),$confidential->GetType()); + + $caldav_data = $ical->Render(); + $displayname = translate('Busy'); + } + } + } + + $url = ConstructURL($item->dav_name); + + $prop = new XMLElement("prop"); + foreach( $properties AS $k => $v ) { + switch( $k ) { + case 'getcontentlength': + $contentlength = strlen($caldav_data); + $prop->NewElement($k, $contentlength ); + break; + case 'calendar-data': + if ( $type == 'calendar' ) $reply->CalDAVElement($prop, $k, $caldav_data ); + else $unsupported[] = $k; + break; + case 'address-data': + if ( $type == 'vcard' ) $reply->CardDAVElement($prop, $k, $caldav_data ); + else $unsupported[] = $k; + break; + case 'getcontenttype': + $prop->NewElement($k, $contenttype ); + break; + case 'current-user-principal': + $prop->NewElement("current-user-principal", $request->current_user_principal_xml); + break; + case 'displayname': + $prop->NewElement($k, $displayname ); + break; + case 'resourcetype': + $prop->NewElement($k); // Just an empty resourcetype for a non-collection. + break; + case 'getetag': + $prop->NewElement($k, '"'.$item->dav_etag.'"' ); + break; + case '"current-user-privilege-set"': + $prop->NewElement($k, privileges($request->permissions) ); + break; + case 'SOME-DENIED-PROPERTY': /** indicating the style for future expansion */ + $denied[] = $k; + break; + default: + dbg_error_log( 'REPORT', "Request for unsupported property '%s' of calendar item.", $v ); + $unsupported[] = $k; + } + } + $status = new XMLElement("status", "HTTP/1.1 200 OK" ); + + $propstat = new XMLElement( "propstat", array( $prop, $status) ); + $href = new XMLElement("href", $url ); + $elements = array($href,$propstat); + + if ( count($denied) > 0 ) { + $status = new XMLElement("status", "HTTP/1.1 403 Forbidden" ); + $noprop = new XMLElement("prop"); + foreach( $denied AS $k => $v ) { + $noprop->NewElement( strtolower($v) ); + } + $elements[] = new XMLElement( "propstat", array( $noprop, $status) ); + } + + if ( count($unsupported) > 0 ) { + $status = new XMLElement("status", "HTTP/1.1 404 Not Found" ); + $noprop = new XMLElement("prop"); + foreach( $unsupported AS $k => $v ) { + $noprop->NewElement( strtolower($v) ); + } + $elements[] = new XMLElement( "propstat", array( $noprop, $status) ); + } + + $response = new XMLElement( "response", $elements ); + + return $response; +} + if ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:caldav:calendar-query" ) { $calquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:caldav:calendar-query/*"); include("caldav-REPORT-calquery.php");