3 * Handle the calendar-multiget REPORT request.
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);
26 * Build the array of properties to include in the report output
29 $proptype = $qry_content[0]->GetTag();
30 $properties = array();
33 $qry_props = $xmltree->GetPath('/*/'.$proptype.'/*');
34 foreach( $qry_content[0]->GetElements() AS $k => $v ) {
35 $propertyname = preg_replace( '/^.*:/', '', $v->GetTag() );
36 $properties[$propertyname] = 1;
37 if ( $v->GetTag() == 'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
42 $properties['allprop'] = 1;
43 if ( $qry_content[1]->GetTag() == 'DAV::include' ) {
44 foreach( $qry_content[1]->GetElements() AS $k => $v ) {
45 $include_properties[] = $v->GetTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
46 if ( $v->GetTag() == 'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
52 $propertyname = preg_replace( '/^.*:/', '', $proptype );
53 $properties[$propertyname] = 1;
56 $collection = new DAVResource($request->path
);
57 $bound_from = $collection->bound_from();
60 * Build the href list for the IN ( href, href, href, ... ) clause.
62 $mg_hrefs = $xmltree->GetPath('/*/DAV::href');
65 foreach( $mg_hrefs AS $k => $v ) {
67 * We need this to work if they specified a relative *or* a full path, so we strip off
68 * anything up to the matching request->path (which will include any http...) and then
69 * put the $bound_from prefix back on.
71 $rawurl = rawurldecode($v->GetContent());
72 $path_pos = strpos($rawurl,$request->path
);
73 if ( $path_pos === false ) {
74 $href = $bound_from . $rawurl;
77 $href = $bound_from . substr( $rawurl, $path_pos +
strlen($request->path
));
79 @dbg_error_log
("REPORT", 'Reporting on href "%s"', $href );
80 $href_in .= ($href_in == '' ?
'' : ', ');
81 $href_in .= ':href'.$k;
82 $params[':href'.$k] = $href;
85 $where = " WHERE caldav_data.collection_id = " . $collection->resource_id();
86 $where .= " AND caldav_data.dav_name IN ( $href_in ) ";
88 if ( $mode == 'caldav' ) {
89 if ( $collection->Privileges() != privilege_to_bits('DAV::all') ) {
90 $where .= " AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
92 if ( isset($c->hide_TODO
) && $c->hide_TODO
&& ! $collection->HavePrivilegeTo('all') ) {
93 $where .= " AND caldav_data.caldav_type NOT IN ('VTODO') ";
96 $sql = 'SELECT calendar_item.*, addressbook_resource.*, caldav_data.* FROM caldav_data
97 LEFT JOIN calendar_item USING(dav_id, user_no, dav_name, collection_id)
98 LEFT JOIN addressbook_resource USING(dav_id)
99 LEFT JOIN collection USING(collection_id)';
102 * @todo: Add stanzas for missing rows, so we don't just return a blank multistatus but
103 * actually return <response> stanzas with a 404 for each absent href. We could do
104 * this relatively easily with an array_flip($params) and remove each matching dav_name
107 if ( isset($c->strict_result_ordering
) && $c->strict_result_ordering
) $where .= " ORDER BY caldav_data.dav_id";
108 $qry = new AwlQuery( $sql . $where, $params );
109 if ( $qry->Exec('REPORT',__LINE__
,__FILE__
) && $qry->rows() > 0 ) {
110 while( $dav_object = $qry->Fetch() ) {
111 if ( $bound_from != $collection->dav_name() ) {
112 $dav_object->dav_name
= str_replace( $bound_from, $collection->dav_name(), $dav_object->dav_name
);
114 if ( $need_expansion ) {
115 $vResource = new vComponent($dav_object->caldav_data
);
116 $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end);
117 $dav_object->caldav_data
= $expanded->Render();
119 $responses[] = component_to_xml( $properties, $dav_object );
123 $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
125 $request->XMLResponse( 207, $multistatus );