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