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 $address_data_properties[strtoupper($v->GetAttribute('name'))] = true;
16 * Build the array of properties to include in the report output
18 $qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-query');
19 $proptype = $qry_content[0]->GetTag();
20 $properties = array();
23 $qry_props = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/'.$proptype.'/*');
24 foreach( $qry_content[0]->GetElements() AS $k => $v ) {
25 $propertyname = preg_replace( '/^.*:/', '', $v->GetTag() );
26 $properties[$propertyname] = 1;
27 if ( $v->GetTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
32 $properties['allprop'] = 1;
33 if ( $qry_content[1]->GetTag() == 'DAV::include' ) {
34 foreach( $qry_content[1]->GetElements() AS $k => $v ) {
35 $include_properties[] = $v->GetTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
36 if ( $v->GetTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
42 $propertyname = preg_replace( '/^.*:/', '', $proptype );
43 $properties[$propertyname] = 1;
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
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);
79 * Process a filter fragment returning an SQL fragment
81 $need_post_filter = false;
83 function SqlFilterCardDAV( $filter, $components, $property = null, $parameter = null ) {
84 global $need_post_filter, $target_collection, $matchnum;
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 ) {
93 dbg_error_log("cardquery", "Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
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) ) {
103 $comparison = 'LIKE';
105 case 'i;ascii-casemap':
106 case 'i;unicode-casemap':
108 $comparison = 'ILIKE';
111 $pname = ':text_match_'.$matchnum++
;
112 $params[$pname] = '%'.$search.'%';
113 dbg_error_log("cardquery", " text-match: (%s%s %s '%s') ", (isset($negate) && strtolower($negate) == "yes" ?
"NOT ": ""),
114 $property, $comparison, $params[$pname] );
115 $sql .= sprintf( "AND (%s%s %s $pname) ", (isset($negate) && strtolower($negate) == "yes" ?
"NOT ": ""),
116 $property, $comparison );
119 case 'urn:ietf:params:xml:ns:carddav:prop-filter':
120 $propertyname = $v->GetAttribute("name");
121 switch( $propertyname ) {
132 $property = strtolower($propertyname);
140 $need_post_filter = true;
141 dbg_error_log("cardquery", "Could not handle 'prop-filter' on %s in SQL", $propertyname );
144 $subfilter = $v->GetContent();
145 $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
146 if ( $success === false ) continue; else {
147 $sql .= $success['sql'];
148 $params = array_merge( $params, $success['params'] );
152 case 'urn:ietf:params:xml:ns:carddav:param-filter':
153 $need_post_filter = true;
154 return false; /** Figure out how to handle PARAM-FILTER conditions in the SQL */
156 $parameter = $v->GetAttribute("name");
157 $subfilter = $v->GetContent();
158 $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
159 if ( $success === false ) continue; else {
160 $sql .= $success['sql'];
161 $params = array_merge( $params, $success['params'] );
167 dbg_error_log("cardquery", "Could not handle unknown tag '%s' in calendar query report", $tag );
171 dbg_error_log("cardquery", "Generated SQL was '%s'", $sql );
172 return array( 'sql' => $sql, 'params' => $params );
177 * Something that we can handle, at least roughly correctly.
180 $responses = array();
181 $target_collection = new DAVResource($request->path
);
182 $bound_from = $target_collection->bound_from();
183 if ( !$target_collection->Exists() ) {
184 $request->DoResponse( 404 );
186 if ( ! ($target_collection->IsAddressbook() ||
$target_collection->IsSchedulingCollection()) ) {
187 $request->DoResponse( 403, translate('The addressbook-query report must be run against an addressbook collection') );
191 * @todo Once we are past DB version 1.2.1 we can change this query more radically. The best performance to
193 * SELECT caldav_data.*,address_item.* FROM collection JOIN address_item USING (collection_id,user_no)
194 * JOIN caldav_data USING (dav_id) WHERE collection.dav_name = '/user1/home/'
195 * AND caldav_data.caldav_type = 'VEVENT' ORDER BY caldav_data.user_no, caldav_data.dav_name;
199 $where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
200 if ( is_array($qry_filters) ) {
201 dbg_log_array( 'cardquery', 'qry_filters', $qry_filters, true );
202 $components = array();
203 $filter_fragment = SqlFilterCardDAV( $qry_filters, $components );
204 if ( $filter_fragment !== false ) {
205 $where .= ' '.$filter_fragment['sql'];
206 $params = $filter_fragment['params'];
210 dbg_error_log( 'cardquery', 'No query filters' );
213 $sql = 'SELECT * FROM caldav_data INNER JOIN addressbook_resource USING(dav_id)'. $where;
214 if ( isset($c->strict_result_ordering
) && $c->strict_result_ordering
) $sql .= " ORDER BY dav_id";
215 $qry = new AwlQuery( $sql, $params );
216 if ( $qry->Exec("cardquery",__LINE__
,__FILE__
) && $qry->rows() > 0 ) {
217 while( $address_object = $qry->Fetch() ) {
218 if ( !$need_post_filter ||
apply_filter( $qry_filters, $address_object ) ) {
219 if ( $bound_from != $target_collection->dav_name() ) {
220 $address_object->dav_name
= str_replace( $bound_from, $target_collection->dav_name(), $address_object->dav_name
);
222 if ( count($address_data_properties) > 0 ) {
223 $vcard = new VCard($address_object->caldav_data
);
224 $vcard->MaskProperties($address_data_properties);
225 $address_object->caldav_data
= $vcard->Render();
227 $responses[] = component_to_xml( $properties, $address_object );
231 $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
233 $request->XMLResponse( 207, $multistatus );