fix for $c->hide_TODO processing and user-agent extension
[davical.git] / inc / caldav-REPORT-multiget.php
blob594b2422112242879b2dedb81aac8b0e46f89df1
1 <?php
2 /**
3 * Handle the calendar-multiget REPORT request.
4 */
6 $responses = array();
8 $need_expansion = false;
9 function check_for_expansion( $calendar_data_node ) {
10 global $need_expansion, $expand_range_start, $expand_range_end;
12 if ( !class_exists('DateTime') ) return; /** We don't support expansion on PHP5.1 */
14 $expansion = $calendar_data_node->GetElements('urn:ietf:params:xml:ns:caldav:expand');
15 if ( isset($expansion[0]) ) {
16 $need_expansion = true;
17 $expand_range_start = $expansion[0]->GetAttribute('start');
18 $expand_range_end = $expansion[0]->GetAttribute('end');
19 if ( isset($expand_range_start) ) $expand_range_start = new RepeatRuleDateTime($expand_range_start);
20 if ( isset($expand_range_end) ) $expand_range_end = new RepeatRuleDateTime($expand_range_end);
25 /**
26 * Build the array of properties to include in the report output
29 $proptype = $qry_content[0]->GetNSTag();
30 $properties = array();
31 switch( $proptype ) {
32 case 'DAV::prop':
33 $qry_props = $xmltree->GetPath('/*/'.$proptype.'/*');
34 foreach( $qry_content[0]->GetElements() AS $k => $v ) {
35 $properties[$v->GetNSTag()] = 1;
36 if ( $v->GetNSTag() == 'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
38 break;
40 case 'DAV::allprop':
41 $properties['DAV::allprop'] = 1;
42 if ( $qry_content[1]->GetNSTag() == 'DAV::include' ) {
43 foreach( $qry_content[1]->GetElements() AS $k => $v ) {
44 $include_properties[] = $v->GetNSTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
45 if ( $v->GetNSTag() == 'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
48 break;
50 default:
51 $properties[$proptype] = 1;
53 if ( empty($properties) ) $properties['DAV::allprop'] = 1;
55 $collection = new DAVResource($request->path);
56 $bound_from = $collection->bound_from();
58 /**
59 * Build the href list for the IN ( href, href, href, ... ) clause.
61 $mg_hrefs = $xmltree->GetPath('/*/DAV::href');
62 $href_in = '';
63 $params = array();
64 foreach( $mg_hrefs AS $k => $v ) {
65 /**
66 * We need this to work if they specified a relative *or* a full path, so we strip off
67 * anything up to the matching request->path (which will include any http...) and then
68 * put the $bound_from prefix back on.
70 $rawurl = rawurldecode($v->GetContent());
71 $path_pos = strpos($rawurl,$request->path);
72 if ( $path_pos === false ) {
73 $href = $bound_from . $rawurl;
75 else {
76 $href = $bound_from . substr( $rawurl, $path_pos + strlen($request->path));
78 @dbg_error_log("REPORT", 'Reporting on href "%s"', $href );
79 $href_in .= ($href_in == '' ? '' : ', ');
80 $href_in .= ':href'.$k;
81 $params[':href'.$k] = $href;
84 $where = " WHERE caldav_data.collection_id = " . $collection->resource_id();
85 $where .= " AND caldav_data.dav_name IN ( $href_in ) ";
87 if ( $mode == 'caldav' ) {
88 if ( $collection->Privileges() != privilege_to_bits('DAV::all') ) {
89 $where .= " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
92 $sql = 'SELECT calendar_item.*, addressbook_resource.*, caldav_data.* FROM caldav_data
93 LEFT JOIN calendar_item USING(dav_id, user_no, dav_name, collection_id)
94 LEFT JOIN addressbook_resource USING(dav_id)
95 LEFT JOIN collection USING(collection_id)';
97 /**
98 * @todo: Add stanzas for missing rows, so we don't just return a blank multistatus but
99 * actually return <response> stanzas with a 404 for each absent href. We could do
100 * this relatively easily with an array_flip($params) and remove each matching dav_name
101 * as we process it.
103 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $where .= " ORDER BY caldav_data.dav_id";
104 $qry = new AwlQuery( $sql . $where, $params );
105 if ( $qry->Exec('REPORT',__LINE__,__FILE__) && $qry->rows() > 0 ) {
106 while( $dav_object = $qry->Fetch() ) {
107 if ( $bound_from != $collection->dav_name() ) {
108 $dav_object->dav_name = str_replace( $bound_from, $collection->dav_name(), $dav_object->dav_name);
110 if ( $need_expansion ) {
111 $vResource = new vComponent($dav_object->caldav_data);
112 $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end);
113 $dav_object->caldav_data = $expanded->Render();
115 $responses[] = component_to_xml( $properties, $dav_object );
119 $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
121 $request->XMLResponse( 207, $multistatus );