Get the name right.
[davical.git] / inc / caldav-REPORT.php
blob5eff90fb10704fe15a1e24912081b58b69a4fb76
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->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( $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($item->dav_name);
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 $need_resource = false;
146 foreach( $properties AS $full_tag => $v ) {
147 $base_tag = preg_replace('{^.*:}', '', $full_tag );
148 switch( $full_tag ) {
149 case 'DAV::getcontentlength':
150 $contentlength = strlen($caldav_data);
151 $prop->NewElement($base_tag, $contentlength );
152 break;
153 case 'DAV::getlastmodified':
154 $prop->NewElement($base_tag, ISODateToHTTPDate($item->modified) );
155 break;
156 case 'urn:ietf:params:xml:ns:caldav:calendar-data':
157 if ( $type == 'calendar' ) $reply->CalDAVElement($prop, $base_tag, $caldav_data );
158 else $unsupported[] = $base_tag;
159 break;
160 case 'urn:ietf:params:xml:ns:carddav:address-data':
161 if ( $type == 'vcard' ) $reply->CardDAVElement($prop, $base_tag, $caldav_data );
162 else $unsupported[] = $base_tag;
163 break;
164 case 'DAV::getcontenttype':
165 $prop->NewElement($base_tag, $contenttype );
166 break;
167 case 'DAV::current-user-principal':
168 $prop->NewElement("current-user-principal", $request->current_user_principal_xml);
169 break;
170 case 'DAV::displayname':
171 $prop->NewElement($base_tag, $displayname );
172 break;
173 case 'DAV::resourcetype':
174 $prop->NewElement($base_tag); // Just an empty resourcetype for a non-collection.
175 break;
176 case 'DAV::getetag':
177 $prop->NewElement($base_tag, '"'.$item->dav_etag.'"' );
178 break;
179 case '"current-user-privilege-set"':
180 $prop->NewElement($base_tag, privileges($request->permissions) );
181 break;
182 default:
183 // It's harder. We need the DAVResource() to get this one.
184 $need_resource = true;
186 if ( $need_resource ) break;
188 $href = new XMLElement("href", $url );
189 if ( $need_resource ) {
190 if ( !isset($dav_resource) ) $dav_resource = new DAVResource($item->dav_name);
191 $elements = $dav_resource->GetPropStat(array_keys($properties), $reply);
192 array_unshift($elements, $href);
194 else {
195 $elements = array($href);
196 $status = new XMLElement("status", "HTTP/1.1 200 OK" );
197 $elements[] = new XMLElement( "propstat", array( $prop, $status) );
198 if ( count($denied) > 0 ) {
199 $status = new XMLElement("status", "HTTP/1.1 403 Forbidden" );
200 $noprop = new XMLElement("prop");
201 foreach( $denied AS $k => $v ) {
202 $reply->NSElement($noprop, $v);
204 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
207 if ( ! $request->PreferMinimal() && count($unsupported) > 0 ) {
208 $status = new XMLElement("status", "HTTP/1.1 404 Not Found" );
209 $noprop = new XMLElement("prop");
210 foreach( $unsupported AS $k => $v ) {
211 $reply->NSElement($noprop, $v);
213 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
217 $response = new XMLElement( "response", $elements );
219 return $response;
222 if ( $target->IsExternal() ) {
223 require_once("external-fetch.php");
224 update_external ( $target );
227 if ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:caldav:calendar-query" ) {
228 $calquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:caldav:calendar-query/*");
229 include("caldav-REPORT-calquery.php");
231 elseif ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:caldav:calendar-multiget" ) {
232 $mode = 'caldav';
233 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:caldav:calendar-multiget');
234 include("caldav-REPORT-multiget.php");
236 elseif ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:carddav:addressbook-multiget" ) {
237 $mode = 'carddav';
238 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-multiget');
239 include("caldav-REPORT-multiget.php");
241 elseif ( $xmltree->GetNSTag() == "urn:ietf:params:xml:ns:carddav:addressbook-query" ) {
242 $cardquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:carddav:addressbook-query/*");
243 include("caldav-REPORT-cardquery.php");
245 else {
246 dbg_error_log( 'ERROR', "Request for unsupported report type '%s'.", $xmltree->GetNSTag() );
247 $request->PreconditionFailed( 403, 'DAV::supported-report', sprintf( '"%s" is not a supported report type', $xmltree->GetNSTag()) );