document closed Debian bug
[davical.git] / inc / caldav-REPORT-cardquery.php
blobfdc82149be1734fad6b1ad650fa4dbaf00ace798
1 <?php
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;
16 /**
17 * Build the array of properties to include in the report output
19 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-query');
20 $proptype = $qry_content[0]->GetNSTag();
21 $properties = array();
22 switch( $proptype ) {
23 case 'DAV::prop':
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);
29 break;
31 case 'DAV::allprop':
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(); /** $include_properties is referenced in DAVResource where allprop is expanded */
36 if ( $v->GetNSTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
39 break;
41 default:
42 $properties[$proptype] = 1;
44 if ( empty($properties) ) $properties['DAV::allprop'] = 1;
46 /**
47 * There can only be *one* FILTER element.
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) != 1 ) {
51 /* $qry_filters = $qry_filters[0]; // There can only be one FILTER element
53 else { */
54 $qry_filters = false;
58 /**
59 * While we can construct our SQL to apply some filters in the query, other filters
60 * need to be checked against the retrieved record. This is for handling those ones.
62 * @param array $filter An array of XMLElement which is the filter definition
63 * @param string $item The database row retrieved for this calendar item
65 * @return boolean True if the check succeeded, false otherwise.
67 function apply_filter( $filters, $item ) {
68 global $session, $c, $request;
70 if ( count($filters) == 0 ) return true;
72 dbg_error_log("cardquery","Applying filter for item '%s'", $item->dav_name );
73 $vcard = new vComponent( $item->caldav_data );
74 return $vcard->TestFilter($filters);
78 /**
79 * Process a filter fragment returning an SQL fragment
81 $need_post_filter = false;
82 $matchnum = 0;
83 function SqlFilterCardDAV( $filter, $components, $property = null, $parameter = null ) {
84 global $need_post_filter, $target_collection, $matchnum;
85 $sql = "";
86 $params = array();
87 if ( !is_array($filter) ) {
88 dbg_error_log( "cardquery", "Filter is of type '%s', but should be an array of XML Tags.", gettype($filter) );
91 foreach( $filter AS $k => $v ) {
92 $tag = $v->GetNSTag();
93 dbg_error_log("cardquery", "Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
95 $not_defined = "";
96 switch( $tag ) {
97 case 'urn:ietf:params:xml:ns:carddav:text-match':
98 $search = $v->GetContent();
99 $negate = $v->GetAttribute("negate-condition");
100 $collation = $v->GetAttribute("collation");
101 switch( strtolower($collation) ) {
102 case 'i;octet':
103 $comparison = 'LIKE';
104 break;
105 case 'i;ascii-casemap':
106 case 'i;unicode-casemap':
107 default:
108 $comparison = 'ILIKE';
109 break;
111 $pname = ':text_match_'.$matchnum++;
112 $match_type = $v->GetAttribute("match-type");
113 switch( strtolower($match_type) ) {
114 case 'starts-with':
115 $params[$pname] = $search.'%';
116 break;
117 case 'ends-with':
118 $params[$pname] = '%'.$search;
119 break;
120 case 'equals':
121 $params[$pname] = $search;
122 case 'contains':
123 default:
124 $params[$pname] = '%'.$search.'%';
125 break;
127 dbg_error_log("cardquery", " text-match: (%s%s %s '%s') ", (isset($negate) && strtolower($negate) == "yes" ? "NOT ": ""),
128 $property, $comparison, $params[$pname] );
129 $sql .= sprintf( "AND (%s%s %s $pname) ", (isset($negate) && strtolower($negate) == "yes" ? "NOT ": ""),
130 $property, $comparison );
131 break;
133 case 'urn:ietf:params:xml:ns:carddav:prop-filter':
134 $propertyname = $v->GetAttribute("name");
135 switch( $propertyname ) {
136 case 'VERSION':
137 case 'UID':
138 case 'NICKNAME':
139 case 'FN':
140 case 'NOTE':
141 case 'ORG':
142 case 'URL':
143 case 'FBURL':
144 case 'CALADRURI':
145 case 'CALURI':
146 $property = strtolower($propertyname);
147 break;
149 case 'N':
150 $property = 'name';
151 break;
153 default:
154 $need_post_filter = true;
155 dbg_error_log("cardquery", "Could not handle 'prop-filter' on %s in SQL", $propertyname );
156 continue;
158 $subfilter = $v->GetContent();
159 $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
160 if ( $success === false ) continue; else {
161 $sql .= $success['sql'];
162 $params = array_merge( $params, $success['params'] );
164 break;
166 case 'urn:ietf:params:xml:ns:carddav:param-filter':
167 $need_post_filter = true;
168 return false; /** Figure out how to handle PARAM-FILTER conditions in the SQL */
170 $parameter = $v->GetAttribute("name");
171 $subfilter = $v->GetContent();
172 $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
173 if ( $success === false ) continue; else {
174 $sql .= $success['sql'];
175 $params = array_merge( $params, $success['params'] );
177 break;
180 default:
181 dbg_error_log("cardquery", "Could not handle unknown tag '%s' in calendar query report", $tag );
182 break;
185 dbg_error_log("cardquery", "Generated SQL was '%s'", $sql );
186 return array( 'sql' => $sql, 'params' => $params );
191 * Something that we can handle, at least roughly correctly.
194 $responses = array();
195 $target_collection = new DAVResource($request->path);
196 $bound_from = $target_collection->bound_from();
197 if ( !$target_collection->Exists() ) {
198 $request->DoResponse( 404 );
200 if ( ! $target_collection->IsAddressbook() ) {
201 $request->DoResponse( 403, translate('The addressbook-query report must be run against an addressbook collection') );
205 * @todo Once we are past DB version 1.2.1 we can change this query more radically. The best performance to
206 * date seems to be:
207 * SELECT caldav_data.*,address_item.* FROM collection JOIN address_item USING (collection_id,user_no)
208 * JOIN caldav_data USING (dav_id) WHERE collection.dav_name = '/user1/home/'
209 * AND caldav_data.caldav_type = 'VEVENT' ORDER BY caldav_data.user_no, caldav_data.dav_name;
212 $params = array();
213 $where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
214 if ( is_array($qry_filters) ) {
215 dbg_log_array( 'cardquery', 'qry_filters', $qry_filters, true );
216 $components = array();
217 $filter_fragment = SqlFilterCardDAV( $qry_filters, $components );
218 if ( $filter_fragment !== false ) {
219 $where .= ' '.$filter_fragment['sql'];
220 $params = $filter_fragment['params'];
223 else {
224 dbg_error_log( 'cardquery', 'No query filters' );
227 $sql = 'SELECT * FROM caldav_data INNER JOIN addressbook_resource USING(dav_id)'. $where;
228 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY dav_id";
229 $qry = new AwlQuery( $sql, $params );
230 if ( $qry->Exec("cardquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
231 while( $address_object = $qry->Fetch() ) {
232 if ( !$need_post_filter || apply_filter( $qry_filters, $address_object ) ) {
233 if ( $bound_from != $target_collection->dav_name() ) {
234 $address_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $address_object->dav_name);
236 if ( count($address_data_properties) > 0 ) {
237 $vcard = new VCard($address_object->caldav_data);
238 $vcard->MaskProperties($address_data_properties);
239 $address_object->caldav_data = $vcard->Render();
241 $responses[] = component_to_xml( $properties, $address_object );
245 $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
247 $request->XMLResponse( 207, $multistatus );