3 include_once(
'vCalendar.php');
8 $qry_content = $xmltree->GetContent(
'urn:ietf:params:xml:ns:caldav:calendar-query');
10 $properties = array();
11 $include_properties = array();
12 $need_expansion =
false;
13 foreach ($qry_content as $idx => $qqq)
15 $proptype = $qry_content[$idx]->GetNSTag();
18 $qry_props = $xmltree->GetPath(
'/urn:ietf:params:xml:ns:caldav:calendar-query/'.$proptype.
'/*');
19 foreach( $qry_content[$idx]->GetElements() AS $k => $v ) {
20 $propertyname = $v->GetNSTag();
21 $properties[$propertyname] = 1;
22 if ( $propertyname ==
'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
27 $properties[
'DAV::allprop'] = 1;
28 if ( $qry_content[$idx]->GetNSTag() ==
'DAV::include' ) {
29 foreach( $qry_content[$idx]->GetElements() AS $k => $v ) {
30 $include_properties[] = $v->GetNSTag();
31 if ( $v->GetNSTag() ==
'urn:ietf:params:xml:ns:caldav:calendar-data' ) check_for_expansion($v);
37 if ( empty($properties) ) $properties[
'DAV::allprop'] = 1;
45 $qry_filters = $xmltree->GetPath(
'/urn:ietf:params:xml:ns:caldav:calendar-query/urn:ietf:params:xml:ns:caldav:filter/*');
46 if ( count($qry_filters) != 1 ) $qry_filters =
false;
58 function calquery_apply_filter( $filters, $item ) {
59 global $session, $c, $request;
61 if ( count($filters) == 0 )
return true;
63 dbg_error_log(
"calquery",
"Applying filter for item '%s'", $item->dav_name );
64 $ical =
new vCalendar( $item->caldav_data );
65 return $ical->StartFilter($filters);
73 $need_post_filter =
false;
75 $parameter_match_num = 0;
76 function SqlFilterFragment( $filter, $components, $property = null, $parameter = null) {
77 global $need_post_filter, $range_filter, $target_collection, $parameter_match_num;
80 if ( !is_array($filter) ) {
81 dbg_error_log(
"calquery",
"Filter is of type '%s', but should be an array of XML Tags.", gettype($filter) );
84 foreach( $filter AS $k => $v ) {
85 $tag = $v->GetNSTag();
86 dbg_error_log(
"calquery",
"Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
90 case 'urn:ietf:params:xml:ns:caldav:is-not-defined':
91 $not_defined =
"not-";
92 case 'urn:ietf:params:xml:ns:caldav:is-defined':
93 if ( isset( $parameter ) ) {
94 $need_post_filter =
true;
95 dbg_error_log(
"calquery",
"Could not handle 'is-%sdefined' on property %s, parameter %s in SQL", $not_defined, $property, $parameter );
98 if ( isset( $property ) ) {
105 if ( ! $target_collection->IsSchedulingCollection() ) {
106 $property_defined_match =
"IS NOT NULL";
111 $property_defined_match =
"IS NOT NULL";
115 $property_defined_match =
"LIKE '_%'";
117 $sql .= sprintf(
"AND %s %s%s ", $property, $not_defined, $property_defined_match );
121 case 'urn:ietf:params:xml:ns:caldav:time-range':
126 $start_column = ($components[
sizeof($components)-1] ==
'VTODO' ?
"due" :
'dtend');
127 $finish_column =
'dtstart';
129 $start = $v->GetAttribute(
"start");
130 $finish = $v->GetAttribute(
"end");
133 $params[
':time_range_start'] = $start;
135 if ( isset($finish) )
136 $params[
':time_range_end'] = $finish;
139 $legacy_start_cond =
"($start_column IS NULL AND $finish_column > :time_range_start) OR $start_column > :time_range_start";
140 $legacy_end_cond =
"$finish_column < :time_range_end";
142 $new_start_cond =
"last_instance_end IS NULL OR last_instance_end >= :time_range_start";
143 $new_end_cond =
"first_instance_start <= :time_range_end";
145 if (!isset($start) && !isset($end)) {
146 $legacy_cond =
"true";
148 }
else if (!isset($start) && isset($end)) {
149 $legacy_cond = $legacy_end_cond;
150 $new_cond = $new_end_cond;
151 }
else if (isset($start) && !isset($end)) {
152 $legacy_cond = $legacy_start_cond;
153 $new_cond = $new_start_cond;
154 }
else if (isset($start) && isset($end)) {
155 $legacy_cond =
"($legacy_end_cond) AND ($legacy_start_cond)";
156 $new_cond =
"($new_end_cond) AND ($new_start_cond)";
160 (first_instance_start IS NULL AND rrule IS NOT NULL OR $finish_column IS NULL OR ($legacy_cond)) 161 OR (first_instance_start IS NOT NULL AND ($new_cond)) 164 @dbg_error_log(
'calquery',
'filter-sql: %s', $sql);
165 @dbg_error_log(
'calquery',
'time-range-start: %s, time-range-end: %s, ', $params[
':time_range_start'], $params[
':time_range_end']);
170 case 'urn:ietf:params:xml:ns:caldav:text-match':
171 $search = $v->GetContent();
172 $negate = $v->GetAttribute(
"negate-condition");
173 $collation = $v->GetAttribute(
"collation");
174 switch( strtolower($collation) ) {
176 $comparison =
'LIKE';
178 case 'i;ascii-casemap':
180 $comparison =
'ILIKE';
184 $params[
':text_match_'.$parameter_match_num] =
'%'.$search.
'%';
185 $fragment = sprintf(
'AND (%s%s %s :text_match_%s) ',
186 (isset($negate) && strtolower($negate) ==
"yes" ? $property.
' IS NULL OR NOT ':
''),
187 $property, $comparison, $parameter_match_num );
188 $parameter_match_num++;
190 dbg_error_log(
'calquery',
' text-match: %s', $fragment );
194 case 'urn:ietf:params:xml:ns:caldav:comp-filter':
195 $comp_filter_name = $v->GetAttribute(
"name");
196 if ( $comp_filter_name !=
'VCALENDAR' && count($components) == 0 ) {
197 $sql .=
"AND caldav_data.caldav_type = :component_name_filter ";
198 $params[
':component_name_filter'] = $comp_filter_name;
199 $components[] = $comp_filter_name;
201 $subfilter = $v->GetContent();
202 if ( is_array( $subfilter ) ) {
203 $success = SqlFilterFragment( $subfilter, $components, $property, $parameter );
204 if ( $success ===
false )
continue 2;
else {
205 $sql .= $success[
'sql'];
206 $params = array_merge( $params, $success[
'params'] );
211 case 'urn:ietf:params:xml:ns:caldav:prop-filter':
212 $propertyname = $v->GetAttribute(
"name");
213 switch( $propertyname ) {
214 case 'PERCENT-COMPLETE':
215 $subproperty =
'percent_complete';
233 $subproperty =
'calendar_item.'.strtolower($propertyname);
238 $need_post_filter =
true;
240 dbg_error_log(
"calquery",
"Could not handle 'prop-filter' on %s in SQL", $propertyname );
243 if ( isset($subproperty) ) {
244 $subfilter = $v->GetContent();
245 $success = SqlFilterFragment( $subfilter, $components, $subproperty, $parameter );
246 if ( $success ===
false )
continue 2;
else {
247 $sql .= $success[
'sql'];
248 $params = array_merge( $params, $success[
'params'] );
253 case 'urn:ietf:params:xml:ns:caldav:param-filter':
254 $need_post_filter =
true;
256 $parameter = $v->GetAttribute(
"name");
257 $subfilter = $v->GetContent();
258 $success = SqlFilterFragment( $subfilter, $components, $property, $parameter );
259 if ( $success ===
false )
continue 2;
else {
260 $sql .= $success[
'sql'];
261 $params = array_merge( $params, $success[
'params'] );
266 dbg_error_log(
"calquery",
"Could not handle unknown tag '%s' in calendar query report", $tag );
270 dbg_error_log(
"calquery",
"Generated SQL was '%s'", $sql );
271 return array(
'sql' => $sql,
'params' => $params );
282 function BuildSqlFilter( $filter ) {
283 $components = array();
284 if ( $filter->GetNSTag() ==
"urn:ietf:params:xml:ns:caldav:comp-filter" && $filter->GetAttribute(
"name") ==
"VCALENDAR" )
285 $filter = $filter->GetContent();
287 dbg_error_log(
"calquery",
"Got bizarre CALDAV:FILTER[%s=%s]] which does not contain comp-filter = VCALENDAR!!", $filter->GetNSTag(), $filter->GetAttribute(
"name") );
289 return SqlFilterFragment( $filter, $components );
297 $responses = array();
298 $target_collection =
new DAVResource($request->path);
299 $bound_from = $target_collection->bound_from();
300 if ( !$target_collection->Exists() ) {
301 $request->DoResponse( 404 );
306 if ( ! ($target_collection->IsCalendar() || $target_collection->IsSchedulingCollection()) ) {
307 if ( !(isset($c->allow_recursive_report) && $c->allow_recursive_report) ) {
308 $request->DoResponse( 403, translate(
'The calendar-query report must be run against a calendar or a scheduling collection') );
310 else if ( $request->path ==
'/' || $target_collection->IsPrincipal() || $target_collection->IsAddressbook() ) {
311 $request->DoResponse( 403, translate(
'The calendar-query report may not be run against that URL.') );
316 $where =
'WHERE caldav_data.collection_id IN ';
317 $where .=
'(SELECT bound_source_id FROM dav_binding WHERE dav_binding.dav_name ~ :path_match ';
319 $where .=
'SELECT collection_id FROM collection WHERE collection.dav_name ~ :path_match) ';
320 $distinct =
'DISTINCT ON (calendar_item.uid) ';
321 $params[
':path_match'] =
'^'.$target_collection->bound_from();
324 $where =
' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
328 if ( is_array($qry_filters) ) {
329 dbg_log_array(
"calquery",
"qry_filters", $qry_filters,
true );
330 $components = array();
331 $filter_fragment = SqlFilterFragment( $qry_filters, $components );
332 if ( $filter_fragment !==
false ) {
333 $where .=
' '.$filter_fragment[
'sql'];
334 $params = array_merge( $params, $filter_fragment[
'params']);
337 if ( $target_collection->Privileges() != privilege_to_bits(
'DAV::all') ) {
338 $where .=
" AND (calendar_item.class != 'PRIVATE' OR calendar_item.class IS NULL) ";
341 if ( isset($c->hide_TODO) && ($c->hide_TODO ===
true || (is_string($c->hide_TODO) && preg_match($c->hide_TODO, $_SERVER[
'HTTP_USER_AGENT']))) && ! $target_collection->HavePrivilegeTo(
'all') ) {
342 $where .=
" AND caldav_data.caldav_type NOT IN ('VTODO') ";
345 if ( isset($c->hide_older_than) && intval($c->hide_older_than > 0) ) {
346 $where .=
" AND (CASE WHEN caldav_data.caldav_type<>'VEVENT' OR calendar_item.dtstart IS NULL OR calendar_item.rrule IS NOT NULL THEN true ELSE calendar_item.dtstart > (now() - interval '".intval($c->hide_older_than).
" days') END) ";
349 $sql =
'SELECT '.$distinct.
' caldav_data.*,calendar_item.*,collection.timezone AS collection_tzid FROM collection INNER JOIN caldav_data USING(collection_id) INNER JOIN calendar_item USING(dav_id) '. $where;
350 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .=
" ORDER BY caldav_data.dav_id";
351 $qry =
new AwlQuery( $sql, $params );
352 if ( $qry->Exec(
"calquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
353 while( $dav_object = $qry->Fetch() ) {
355 if ( !$need_post_filter || calquery_apply_filter( $qry_filters, $dav_object ) ) {
356 if ( $bound_from != $target_collection->dav_name() ) {
357 $dav_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $dav_object->dav_name);
359 if ( $need_expansion ) {
360 $vResource =
new vComponent($dav_object->caldav_data);
361 $expanded = getVCalendarRange($vResource, $dav_object->collection_tzid);
362 if ( !$expanded->overlaps($range_filter) )
continue;
364 $expanded = expand_event_instances($vResource, $expand_range_start, $expand_range_end, $expand_as_floating , $dav_object->collection_tzid);
366 if ( $expanded->ComponentCount() == 0 )
continue;
367 if ( $need_expansion ) $dav_object->caldav_data = $expanded->Render();
369 else if ( isset($range_filter) ) {
370 $vResource =
new vComponent($dav_object->caldav_data);
371 $expanded = getVCalendarRange($vResource);
372 dbg_error_log(
'calquery',
'Expanded to %s:%s which might overlap %s:%s',
373 $expanded->from, $expanded->until, $range_filter->from, $range_filter->until );
374 if ( !$expanded->overlaps($range_filter) )
continue;
376 $responses[] = component_to_xml( $properties, $dav_object );
379 catch( Exception $e ) {
380 dbg_error_log(
'ERROR',
'Exception handling "%s" - skipping', $dav_object->dav_name);
385 $multistatus =
new XMLElement(
"multistatus", $responses, $reply->GetXmlNsArray() );
387 $request->XMLResponse( 207, $multistatus );