Update refresh-alarms script to newer style initialisation.
[davical.git] / inc / caldav-REPORT.php
blobc100e5d83f149ac596580e681bcdaed95bfedf12
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($properties['calendar-data']) && isset($c->hide_alarm) && $c->hide_alarm && !$request->HavePrivilegeTo('write') ) {
132 dbg_error_log("REPORT","Stripping event alarms for: %s", $item->dav_name );
133 $vcal = new vCalendar($caldav_data);
134 $vcal->ClearComponents('VALARM');
135 $caldav_data = $vcal->Render();
139 $url = ConstructURL($item->dav_name);
141 $prop = new XMLElement("prop");
142 foreach( $properties AS $k => $v ) {
143 switch( $k ) {
144 case 'getcontentlength':
145 $contentlength = strlen($caldav_data);
146 $prop->NewElement($k, $contentlength );
147 break;
148 case 'getlastmodified':
149 $prop->NewElement($k, ISODateToHTTPDate($item->modified) );
150 break;
151 case 'calendar-data':
152 if ( $type == 'calendar' ) $reply->CalDAVElement($prop, $k, $caldav_data );
153 else $unsupported[] = $k;
154 break;
155 case 'address-data':
156 if ( $type == 'vcard' ) $reply->CardDAVElement($prop, $k, $caldav_data );
157 else $unsupported[] = $k;
158 break;
159 case 'getcontenttype':
160 $prop->NewElement($k, $contenttype );
161 break;
162 case 'current-user-principal':
163 $prop->NewElement("current-user-principal", $request->current_user_principal_xml);
164 break;
165 case 'displayname':
166 $prop->NewElement($k, $displayname );
167 break;
168 case 'resourcetype':
169 $prop->NewElement($k); // Just an empty resourcetype for a non-collection.
170 break;
171 case 'getetag':
172 $prop->NewElement($k, '"'.$item->dav_etag.'"' );
173 break;
174 case '"current-user-privilege-set"':
175 $prop->NewElement($k, privileges($request->permissions) );
176 break;
177 case 'SOME-DENIED-PROPERTY': /** indicating the style for future expansion */
178 $denied[] = $k;
179 break;
180 default:
181 dbg_error_log( 'REPORT', "Request for unsupported property '%s' of calendar item.", $v );
182 $unsupported[] = $k;
185 $status = new XMLElement("status", "HTTP/1.1 200 OK" );
187 $propstat = new XMLElement( "propstat", array( $prop, $status) );
188 $href = new XMLElement("href", $url );
189 $elements = array($href,$propstat);
191 if ( count($denied) > 0 ) {
192 $status = new XMLElement("status", "HTTP/1.1 403 Forbidden" );
193 $noprop = new XMLElement("prop");
194 foreach( $denied AS $k => $v ) {
195 $noprop->NewElement( strtolower($v) );
197 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
200 if ( count($unsupported) > 0 ) {
201 $status = new XMLElement("status", "HTTP/1.1 404 Not Found" );
202 $noprop = new XMLElement("prop");
203 foreach( $unsupported AS $k => $v ) {
204 $noprop->NewElement( strtolower($v) );
206 $elements[] = new XMLElement( "propstat", array( $noprop, $status) );
209 $response = new XMLElement( "response", $elements );
211 return $response;
214 if ( $target->IsExternal() ) {
215 require_once("external-fetch.php");
216 update_external ( $target );
219 if ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:caldav:calendar-query" ) {
220 $calquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:caldav:calendar-query/*");
221 include("caldav-REPORT-calquery.php");
223 elseif ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:caldav:calendar-multiget" ) {
224 $mode = 'caldav';
225 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:caldav:calendar-multiget');
226 include("caldav-REPORT-multiget.php");
228 elseif ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:carddav:addressbook-multiget" ) {
229 $mode = 'carddav';
230 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-multiget');
231 include("caldav-REPORT-multiget.php");
233 elseif ( $xmltree->GetTag() == "urn:ietf:params:xml:ns:carddav:addressbook-query" ) {
234 $cardquery = $xmltree->GetPath("/urn:ietf:params:xml:ns:carddav:addressbook-query/*");
235 include("caldav-REPORT-cardquery.php");
237 else {
238 dbg_error_log( 'ERROR', "Request for unsupported report type '%s'.", $xmltree->GetTag() );
239 $request->PreconditionFailed( 403, 'DAV::supported-report', sprintf( '"%s" is not a supported report type', $xmltree->GetTag()) );