Merge branch 'master' of github.com:DAViCal/davical into github
[davical.git] / inc / caldav-REPORT.php
blobccf356caf6d1bc5ecab0d3d215146df7e5e1374d
1 <?php
2 /**
3 * CalDAV Server - handle REPORT method
5 * @package davical
6 * @subpackage caldav
7 * @author Andrew McMillan <andrew@morphoss.com>
8 * @copyright Catalyst .Net Ltd, Morphoss Ltd
9 * @license http://gnu.org/copyleft/gpl.html GNU GPL v2
11 dbg_error_log("REPORT", "method handler");
13 require_once("XMLDocument.php");
14 require_once('DAVResource.php');
16 require_once('RRule-v2.php');
18 if ( ! ini_get('open_basedir') && (isset($c->dbg['ALL']) || (isset($c->dbg['report']) && $c->dbg['report'])) ) {
19 $fh = fopen('/var/log/davical/REPORT.debug','w');
20 if ( $fh ) {
21 fwrite($fh,$request->raw_post);
22 fclose($fh);
26 if ( !isset($request->xml_tags) ) {
27 $request->DoResponse( 406, translate("REPORT body contains no XML data!") );
29 $position = 0;
30 $xmltree = BuildXMLTree( $request->xml_tags, $position);
31 if ( !is_object($xmltree) ) {
32 $request->DoResponse( 406, translate("REPORT body is not valid XML data!") );
35 $target = new DAVResource($request->path);
37 if ( $xmltree->GetNSTag() != 'DAV::principal-property-search'
38 && $xmltree->GetNSTag() != 'DAV::principal-property-search-set' ) {
39 $target->NeedPrivilege( array('DAV::read', 'urn:ietf:params:xml:ns:caldav:read-free-busy'), true ); // They may have either
42 require_once("vCalendar.php");
44 $reportnum = -1;
45 $report = array();
46 $denied = array();
47 $unsupported = array();
48 if ( isset($prop_filter) ) unset($prop_filter);
50 if ( $xmltree->GetNSTag() == 'urn:ietf:params:xml:ns:caldav:free-busy-query' ) {
51 include("caldav-REPORT-freebusy.php");
52 exit; // Not that the above include should return anyway
55 $reply = new XMLDocument( array( "DAV:" => "" ) );
56 switch( $xmltree->GetNSTag() ) {
57 case 'DAV::principal-property-search':
58 include("caldav-REPORT-principal.php");
59 exit; // Not that it should return anyway.
60 case 'DAV::principal-search-property-set':
61 include("caldav-REPORT-pps-set.php");
62 exit; // Not that it should return anyway.
63 case 'DAV::sync-collection':
64 if ( $target->IsExternal() ) {
65 require_once("external-fetch.php");
66 update_external ( $target );
68 include("caldav-REPORT-sync-collection.php");
69 exit; // Not that it should return anyway.
70 case 'DAV::expand-property':
71 if ( $target->IsExternal() ) {
72 require_once("external-fetch.php");
73 update_external ( $target );
75 include("caldav-REPORT-expand-property.php");
76 exit; // Not that it should return anyway.
77 case 'DAV::principal-match':
78 include("caldav-REPORT-principal-match.php");
79 exit; // Not that it should return anyway.
84 /**
85 * Return XML for a single component from the DB
87 * @param array $properties The properties for this component
88 * @param string $item The DB row data for this component
90 * @return string An XML document which is the response for the component
92 function component_to_xml( $properties, $item ) {
93 global $session, $c, $request, $reply;
95 dbg_error_log("REPORT","Building XML Response for item '%s'", $item->dav_name );
97 $denied = array();
98 $unsupported = array();
99 $caldav_data = $item->caldav_data;
100 $displayname = preg_replace( '{^.*/}', '', $item->dav_name );
101 $type = 'unknown';
102 $contenttype = 'text/plain';
103 switch( strtoupper($item->caldav_type) ) {
104 case 'VJOURNAL':
105 case 'VEVENT':
106 case 'VTODO':
107 $displayname = $item->summary;
108 $type = 'calendar';
109 $contenttype = 'text/calendar';
110 if ( isset($properties['urn:ietf:params:xml:ns:caldav:calendar-data']) || isset($properties['DAV::displayname']) ) {
111 if ( !$request->AllowedTo('all') && $session->user_no != $item->user_no ) {
112 // the user is not admin / owner of this calendar looking at his calendar and can not admin the other cal
113 if ( $item->class == 'CONFIDENTIAL' || !$request->AllowedTo('read') ) {
114 dbg_error_log("REPORT","Anonymising confidential event for: %s", $item->dav_name );
115 $vcal = new vCalendar( $caldav_data );
116 $caldav_data = $vcal->Confidential()->Render();
117 $displayname = translate('Busy');
122 if ( isset($c->hide_alarm) && $c->hide_alarm ) {
123 $dav_resource = new DAVResource($item->dav_name);
124 if ( isset($properties['urn:ietf:params:xml:ns:caldav:calendar-data']) && !$dav_resource->HavePrivilegeTo('write') ) {
125 dbg_error_log("REPORT","Stripping event alarms for: %s", $item->dav_name );
126 $vcal = new vCalendar($caldav_data);
127 $vcal->ClearComponents('VALARM');
128 $caldav_data = $vcal->Render();
131 break;
133 case 'VCARD':
134 $displayname = $item->fn;
135 $type = 'vcard';
136 $contenttype = 'text/vcard';
137 break;
140 $url = ConstructURL($item->dav_name);
142 $prop = new XMLElement("prop");
143 $need_resource = false;
144 foreach( $properties AS $full_tag => $v ) {
145 $base_tag = preg_replace('{^.*:}', '', $full_tag );
146 switch( $full_tag ) {
147 case 'DAV::getcontentlength':
148 $contentlength = strlen($caldav_data);
149 $prop->NewElement($base_tag, $contentlength );
150 break;
151 case 'DAV::getlastmodified':
152 $prop->NewElement($base_tag, ISODateToHTTPDate($item->modified) );
153 break;
154 case 'urn:ietf:params:xml:ns:caldav:calendar-data':
155 if ( $type == 'calendar' ) $reply->CalDAVElement($prop, $base_tag, '<![CDATA['.$caldav_data.']]>' );
156 else $unsupported[] = $base_tag;
157 break;
158 case 'urn:ietf:params:xml:ns:carddav:address-data':
159 if ( $type == 'vcard' ) $reply->CardDAVElement($prop, $base_tag, '<![CDATA['.$caldav_data.']]>' );
160 else $unsupported[] = $base_tag;
161 break;
162 case 'DAV::getcontenttype':
163 $prop->NewElement($base_tag, $contenttype );
164 break;
165 case 'DAV::current-user-principal':
166 $prop->NewElement("current-user-principal", $request->current_user_principal_xml);
167 break;
168 case 'DAV::displayname':
169 $prop->NewElement($base_tag, $displayname );
170 break;
171 case 'DAV::resourcetype':
172 $prop->NewElement($base_tag); // Just an empty resourcetype for a non-collection.
173 break;
174 case 'DAV::getetag':
175 $prop->NewElement($base_tag, '"'.$item->dav_etag.'"' );
176 break;
177 case '"current-user-privilege-set"':
178 $prop->NewElement($base_tag, privileges($request->permissions) );
179 break;
180 default:
181 // It's harder. We need the DAVResource() to get this one.
182 $need_resource = true;
184 if ( $need_resource ) break;
186 $href = new XMLElement("href", $url );
187 if ( $need_resource ) {
188 if ( !isset($dav_resource) ) $dav_resource = new DAVResource($item->dav_name);
189 $elements = $dav_resource->GetPropStat(array_keys($properties), $reply);
190 array_unshift($elements, $href);
192 else {
193 $elements = array($href);
194 $status = new XMLElement("status", "HTTP/1.1 200 OK" );
195 $elements[] = new XMLElement( "propstat", array( $prop, $status) );
196 if ( count($denied) > 0 ) {
197 $status = new XMLElement("status", "HTTP/1.1 403 Forbidden" );
198 $noprop = new XMLElement("prop");
199 foreach( $denied AS $k => $v ) {
200 $reply->NSElement($noprop, $v);
202 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
205 if ( ! $request->PreferMinimal() && count($unsupported) > 0 ) {
206 $status = new XMLElement("status", "HTTP/1.1 404 Not Found" );
207 $noprop = new XMLElement("prop");
208 foreach( $unsupported AS $k => $v ) {
209 $reply->NSElement($noprop, $v);
211 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
215 $response = new XMLElement( "response", $elements );
217 return $response;
220 if ( $target->IsExternal() ) {
221 require_once("external-fetch.php");
222 update_external ( $target );
225 // These reports are always allowed to see the resource_data because they are special
226 $c->sync_resource_data_ok = true;
228 if ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:caldav:calendar-query" ) {
229 $calquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:caldav:calendar-query/*");
230 include("caldav-REPORT-calquery.php");
232 elseif ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:caldav:calendar-multiget" ) {
233 $mode = 'caldav';
234 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:caldav:calendar-multiget');
235 include("caldav-REPORT-multiget.php");
237 elseif ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:carddav:addressbook-multiget" ) {
238 $mode = 'carddav';
239 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-multiget');
240 include("caldav-REPORT-multiget.php");
242 elseif ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:carddav:addressbook-query" ) {
243 $cardquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:carddav:addressbook-query/*");
244 include("caldav-REPORT-cardquery.php");
246 else {
247 dbg_error_log( 'ERROR', "Request for unsupported report type '%s'.", $xmltree->GetNSTag() );
248 $request->PreconditionFailed( 403, 'DAV::supported-report', sprintf( '"%s" is not a supported report type', $xmltree->GetNSTag()) );