3 require_once(
'vcard.php');
5 $address_data_properties = array();
6 function get_address_properties( $address_data_xml ) {
7 global $address_data_properties;
8 $expansion = $address_data_xml->GetElements();
9 foreach( $expansion AS $k => $v ) {
10 if ( $v instanceof XMLElement )
11 $address_data_properties[strtoupper($v->GetAttribute(
'name'))] =
true;
19 $qry_content = $xmltree->GetContent(
'urn:ietf:params:xml:ns:carddav:addressbook-query');
20 $proptype = $qry_content[0]->GetNSTag();
21 $properties = array();
24 $qry_props = $xmltree->GetPath(
'/urn:ietf:params:xml:ns:carddav:addressbook-query/'.$proptype.
'/*');
25 foreach( $qry_content[0]->GetElements() AS $k => $v ) {
26 $properties[$v->GetNSTag()] = 1;
27 if ( $v->GetNSTag() ==
'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
32 $properties[
'DAV::allprop'] = 1;
33 if ( $qry_content[1]->GetNSTag() ==
'DAV::include' ) {
34 foreach( $qry_content[1]->GetElements() AS $k => $v ) {
35 $include_properties[] = $v->GetNSTag();
36 if ( $v->GetNSTag() ==
'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
42 $properties[$proptype] = 1;
44 if ( empty($properties) ) $properties[
'DAV::allprop'] = 1;
49 $qry_filters = $xmltree->GetPath(
'/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:filter/*');
50 if ( count($qry_filters) == 0 ) {
54 $qry_filters_combination=
'OR';
55 if ( is_array($qry_filters) ) {
56 $filters_parent = $xmltree->GetPath(
'/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:filter');
57 $filters_parent = $filters_parent[0];
59 if ( $filters_parent->GetAttribute(
"test") ==
'allof' ) {
60 $qry_filters_combination=
'AND';
65 $limits = $xmltree->GetPath(
'/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:limit/urn:ietf:params:xml:ns:carddav:nresults');
66 if ( count($limits) == 1) {
67 $qry_limit = intval($limits[0]->GetContent());
80 function cardquery_apply_filter( $filters, $item, $filter_type) {
81 global $session, $c, $request;
83 if ( count($filters) == 0 )
return true;
85 dbg_error_log(
"cardquery",
"Applying filter for item '%s'", $item->dav_name );
86 $vcard =
new vComponent( $item->caldav_data );
88 if ( $filter_type ===
'AND' ) {
89 return $vcard->TestFilter($filters);
91 foreach($filters AS $filter) {
92 $filter_fragment[0] = $filter;
93 if ( $vcard->TestFilter($filter_fragment) ) {
105 $post_filters = array();
107 function SqlFilterCardDAV( $filter, $components, $property =
null, $parameter =
null ) {
108 global $post_filters, $target_collection, $matchnum;
113 if ( !is_object($filter) ) {
114 if ( empty($property) )
return false;
115 $sql .= $property .
' IS NOT NULL';
116 return array(
'sql' => $sql,
'params' => $params );
119 $tag = $filter->GetNSTag();
120 dbg_error_log(
"cardquery",
"Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
124 case 'urn:ietf:params:xml:ns:carddav:is-not-defined':
125 $sql .= $property .
' IS NULL';
128 case 'urn:ietf:params:xml:ns:carddav:text-match':
129 if ( empty($property) ) {
133 $collation = $filter->GetAttribute(
"collation");
134 switch( strtolower($collation) ) {
136 $comparison =
'LIKE';
138 case 'i;ascii-casemap':
139 case 'i;unicode-casemap':
141 $comparison =
'ILIKE';
145 $search = $filter->GetContent();
146 $match = $filter->GetAttribute(
"match-type");
147 switch( strtolower($match) ) {
151 $search = $search.
'%';
154 $search =
'%'.$search;
158 $search =
'%'.$search.
'%';
162 $pname =
':text_match_'.$matchnum++;
163 $params[$pname] = $search;
165 $negate = $filter->GetAttribute(
"negate-condition");
166 $negate = ( isset($negate) && strtolower($negate) ==
"yes" ) ?
"NOT " :
"";
167 dbg_error_log(
"cardquery",
" text-match: (%s%s %s '%s') ", $negate, $property, $comparison, $search );
168 $sql .= sprintf(
"(%s%s %s $pname)", $negate, $property, $comparison );
171 case 'urn:ietf:params:xml:ns:carddav:prop-filter':
172 $propertyname = $filter->GetAttribute(
"name");
173 switch( $propertyname ) {
184 $property = strtolower($propertyname);
192 $post_filters[] = $filter;
193 dbg_error_log(
"cardquery",
"Could not handle 'prop-filter' on %s in SQL", $propertyname );
197 $test_type = $filter->GetAttribute(
"test");
198 switch( $test_type ) {
207 $subfilters = $filter->GetContent();
208 if (count($subfilters) <= 1) {
209 $success = SqlFilterCardDAV( $subfilters[0], $components, $property, $parameter );
210 if ( $success !==
false ) {
211 $sql .= $success[
'sql'];
212 $params = array_merge( $params, $success[
'params'] );
215 $subfilter_added_counter=0;
216 foreach ($subfilters as $subfilter) {
217 $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
218 if ( $success ===
false )
continue;
else {
219 if ($subfilter_added_counter <= 0) {
220 $sql .=
'(' . $success[
'sql'];
222 $sql .= $test_type .
' ' . $success[
'sql'];
224 $params = array_merge( $params, $success[
'params'] );
225 $subfilter_added_counter++;
228 if ($subfilter_added_counter > 0) {
234 case 'urn:ietf:params:xml:ns:carddav:param-filter':
235 $post_filters[] = $filter;
249 dbg_error_log(
"cardquery",
"Could not handle unknown tag '%s' in calendar query report", $tag );
252 dbg_error_log(
"cardquery",
"Generated SQL was '%s'", $sql );
253 return array(
'sql' => $sql,
'params' => $params );
261 $responses = array();
262 $target_collection =
new DAVResource($request->path);
263 $bound_from = $target_collection->bound_from();
264 if ( !$target_collection->Exists() ) {
265 $request->DoResponse( 404 );
267 if ( ! $target_collection->IsAddressbook() ) {
268 $request->DoResponse( 403, translate(
'The addressbook-query report must be run against an addressbook collection') );
280 $where_collection =
' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
282 if ( is_array($qry_filters) ) {
283 dbg_log_array(
'cardquery',
'qry_filters', $qry_filters,
true );
285 $appended_where_counter=0;
286 foreach ($qry_filters as $qry_filter) {
287 $components = array();
288 $filter_fragment = SqlFilterCardDAV( $qry_filter, $components );
289 if ( $filter_fragment !==
false ) {
290 $filter_fragment_sql = $filter_fragment[
'sql'];
291 if ( empty($filter_fragment_sql) ) {
295 if ( $appended_where_counter == 0 ) {
296 $where .=
' AND (' . $filter_fragment_sql;
297 $params = $filter_fragment[
'params'];
299 $where .=
' ' . $qry_filters_combination .
' ' . $filter_fragment_sql;
300 $params = array_merge( $params, $filter_fragment[
'params'] );
302 $appended_where_counter++;
305 if ( $appended_where_counter > 0 ) {
310 dbg_error_log(
'cardquery',
'No query filters' );
313 $need_post_filter = !empty($post_filters);
314 if ( $need_post_filter && ( $qry_filters_combination ==
'OR' )) {
319 $post_filters = $qry_filters;
322 $sql =
'SELECT * FROM caldav_data INNER JOIN addressbook_resource USING(dav_id)'. $where_collection . $where;
323 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .=
" ORDER BY dav_id";
324 $qry =
new AwlQuery( $sql, $params );
325 if ( $qry->Exec(
"cardquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
326 while( $address_object = $qry->Fetch() ) {
327 if ( !$need_post_filter || cardquery_apply_filter( $post_filters, $address_object, $qry_filters_combination ) ) {
328 if ( $bound_from != $target_collection->dav_name() ) {
329 $address_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $address_object->dav_name);
331 if ( count($address_data_properties) > 0 ) {
332 $vcard =
new VCard($address_object->caldav_data);
333 $vcard->MaskProperties($address_data_properties);
334 $address_object->caldav_data = $vcard->Render();
336 $responses[] = component_to_xml( $properties, $address_object );
337 if ( ($qry_limit > 0) && ( count($responses) >= $qry_limit ) ) {
343 $multistatus =
new XMLElement(
"multistatus", $responses, $reply->GetXmlNsArray() );
345 $request->XMLResponse( 207, $multistatus );