Fix reference to $domain should be $this->domain.
[davical.git] / inc / caldav-REPORT.php
blob1da78f1fd658663064ed8fe827db8d0cc213babf
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('/tmp/REPORT.txt','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->GetTag() != 'DAV::principal-property-search'
38 && $xmltree->GetTag() != '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->GetTag() == '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->GetTag() ) {
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( $item->caldav_type ) {
104 case 'VJOURNAL':
105 case 'VEVENT':
106 case 'VTODO':
107 $displayname = $item->summary;
108 $type = 'calendar';
109 $contenttype = 'text/calendar';
110 break;
112 case 'VCARD':
113 $displayname = $item->fn;
114 $type = 'vcard';
115 $contenttype = 'text/vcard';
116 break;
118 if ( $type == 'calendar' ) {
119 if ( isset($properties['calendar-data']) || isset($properties['displayname']) ) {
120 if ( !$request->AllowedTo('all') && $session->user_no != $item->user_no ) {
121 // the user is not admin / owner of this calendar looking at his calendar and can not admin the other cal
122 if ( $item->class == 'CONFIDENTIAL' || !$request->AllowedTo('read') ) {
123 dbg_error_log("REPORT","Anonymising confidential event for: %s", $item->dav_name );
124 $vcal = new vCalendar( $caldav_data );
125 $caldav_data = $vcal->Confidential()->Render();
126 $displayname = translate('Busy');
131 if ( isset($c->hide_alarm) && $c->hide_alarm ) {
132 $dav_resource = new DAVResource($request->path);
133 if ( isset($properties['calendar-data']) && !$dav_resource->HavePrivilegeTo('write') ) {
134 dbg_error_log("REPORT","Stripping event alarms for: %s", $item->dav_name );
135 $vcal = new vCalendar($caldav_data);
136 $vcal->ClearComponents('VALARM');
137 $caldav_data = $vcal->Render();
142 $url = ConstructURL($item->dav_name);
144 $prop = new XMLElement("prop");
145 foreach( $properties AS $k => $v ) {
146 switch( $k ) {
147 case 'getcontentlength':
148 $contentlength = strlen($caldav_data);
149 $prop->NewElement($k, $contentlength );
150 break;
151 case 'getlastmodified':
152 $prop->NewElement($k, ISODateToHTTPDate($item->modified) );
153 break;
154 case 'calendar-data':
155 if ( $type == 'calendar' ) $reply->CalDAVElement($prop, $k, $caldav_data );
156 else $unsupported[] = $k;
157 break;
158 case 'address-data':
159 if ( $type == 'vcard' ) $reply->CardDAVElement($prop, $k, $caldav_data );
160 else $unsupported[] = $k;
161 break;
162 case 'getcontenttype':
163 $prop->NewElement($k, $contenttype );
164 break;
165 case 'current-user-principal':
166 $prop->NewElement("current-user-principal", $request->current_user_principal_xml);
167 break;
168 case 'displayname':
169 $prop->NewElement($k, $displayname );
170 break;
171 case 'resourcetype':
172 $prop->NewElement($k); // Just an empty resourcetype for a non-collection.
173 break;
174 case 'getetag':
175 $prop->NewElement($k, '"'.$item->dav_etag.'"' );
176 break;
177 case '"current-user-privilege-set"':
178 $prop->NewElement($k, privileges($request->permissions) );
179 break;
180 case 'SOME-DENIED-PROPERTY': /** indicating the style for future expansion */
181 $denied[] = $k;
182 break;
183 default:
184 dbg_error_log( 'REPORT', "Request for unsupported property '%s' of calendar item.", $v );
185 $unsupported[] = $k;
188 $status = new XMLElement("status", "HTTP/1.1 200 OK" );
190 $propstat = new XMLElement( "propstat", array( $prop, $status) );
191 $href = new XMLElement("href", $url );
192 $elements = array($href,$propstat);
194 if ( count($denied) > 0 ) {
195 $status = new XMLElement("status", "HTTP/1.1 403 Forbidden" );
196 $noprop = new XMLElement("prop");
197 foreach( $denied AS $k => $v ) {
198 $noprop->NewElement( strtolower($v) );
200 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
203 if ( !(isset($request->brief_response) && $request->brief_response) && count($unsupported) > 0 ) {
204 $status = new XMLElement("status", "HTTP/1.1 404 Not Found" );
205 $noprop = new XMLElement("prop");
206 foreach( $unsupported AS $k => $v ) {
207 $noprop->NewElement( strtolower($v) );
209 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
212 $response = new XMLElement( "response", $elements );
214 return $response;
217 if ( $target->IsExternal() ) {
218 require_once("external-fetch.php");
219 update_external ( $target );
222 if ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:caldav:calendar-query" ) {
223 $calquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:caldav:calendar-query/*");
224 include("caldav-REPORT-calquery.php");
226 elseif ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:caldav:calendar-multiget" ) {
227 $mode = 'caldav';
228 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:caldav:calendar-multiget');
229 include("caldav-REPORT-multiget.php");
231 elseif ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:carddav:addressbook-multiget" ) {
232 $mode = 'carddav';
233 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-multiget');
234 include("caldav-REPORT-multiget.php");
236 elseif ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:carddav:addressbook-query" ) {
237 $cardquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:carddav:addressbook-query/*");
238 include("caldav-REPORT-cardquery.php");
240 else {
241 dbg_error_log( 'ERROR', "Request for unsupported report type '%s'.", $xmltree->GetTag() );
242 $request->PreconditionFailed( 403, 'DAV::supported-report', sprintf( '"%s" is not a supported report type', $xmltree->GetTag()) );