Fix missing END:VCALENDAR.
[davical.git] / inc / caldav-REPORT-cardquery.php
blob4115ecd403f13b3ecee99093f0b9b129282bcaec
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]->GetTag();
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 $propertyname = preg_replace( '/^.*:/', '', $v->GetTag() );
27 $properties[$propertyname] = 1;
28 if ( $v->GetTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
30 break;
32 case 'DAV::allprop':
33 $properties['allprop'] = 1;
34 if ( $qry_content[1]->GetTag() == 'DAV::include' ) {
35 foreach( $qry_content[1]->GetElements() AS $k => $v ) {
36 $include_properties[] = $v->GetTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
37 if ( $v->GetTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
40 break;
42 default:
43 $propertyname = preg_replace( '/^.*:/', '', $proptype );
44 $properties[$propertyname] = 1;
47 /**
48 * There can only be *one* FILTER element.
50 $qry_filters = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:filter/*');
51 if ( count($qry_filters) != 1 ) {
52 /* $qry_filters = $qry_filters[0]; // There can only be one FILTER element
54 else { */
55 $qry_filters = false;
59 /**
60 * While we can construct our SQL to apply some filters in the query, other filters
61 * need to be checked against the retrieved record. This is for handling those ones.
63 * @param array $filter An array of XMLElement which is the filter definition
64 * @param string $item The database row retrieved for this calendar item
66 * @return boolean True if the check succeeded, false otherwise.
68 function apply_filter( $filters, $item ) {
69 global $session, $c, $request;
71 if ( count($filters) == 0 ) return true;
73 dbg_error_log("cardquery","Applying filter for item '%s'", $item->dav_name );
74 $vcard = new vComponent( $item->caldav_data );
75 return $vcard->TestFilter($filters);
79 /**
80 * Process a filter fragment returning an SQL fragment
82 $need_post_filter = false;
83 $matchnum = 0;
84 function SqlFilterCardDAV( $filter, $components, $property = null, $parameter = null ) {
85 global $need_post_filter, $target_collection, $matchnum;
86 $sql = "";
87 $params = array();
88 if ( !is_array($filter) ) {
89 dbg_error_log( "cardquery", "Filter is of type '%s', but should be an array of XML Tags.", gettype($filter) );
92 foreach( $filter AS $k => $v ) {
93 $tag = $v->GetTag();
94 dbg_error_log("cardquery", "Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
96 $not_defined = "";
97 switch( $tag ) {
98 case 'urn:ietf:params:xml:ns:carddav:text-match':
99 $search = $v->GetContent();
100 $negate = $v->GetAttribute("negate-condition");
101 $collation = $v->GetAttribute("collation");
102 switch( strtolower($collation) ) {
103 case 'i;octet':
104 $comparison = 'LIKE';
105 break;
106 case 'i;ascii-casemap':
107 case 'i;unicode-casemap':
108 default:
109 $comparison = 'ILIKE';
110 break;
112 $pname = ':text_match_'.$matchnum++;
113 $params[$pname] = '%'.$search.'%';
114 dbg_error_log("cardquery", " text-match: (%s%s %s '%s') ", (isset($negate) && strtolower($negate) == "yes" ? "NOT ": ""),
115 $property, $comparison, $params[$pname] );
116 $sql .= sprintf( "AND (%s%s %s $pname) ", (isset($negate) && strtolower($negate) == "yes" ? "NOT ": ""),
117 $property, $comparison );
118 break;
120 case 'urn:ietf:params:xml:ns:carddav:prop-filter':
121 $propertyname = $v->GetAttribute("name");
122 switch( $propertyname ) {
123 case 'VERSION':
124 case 'UID':
125 case 'NICKNAME':
126 case 'FN':
127 case 'NOTE':
128 case 'ORG':
129 case 'URL':
130 case 'FBURL':
131 case 'CALADRURI':
132 case 'CALURI':
133 $property = strtolower($propertyname);
134 break;
136 case 'N':
137 $property = 'name';
138 break;
140 default:
141 $need_post_filter = true;
142 dbg_error_log("cardquery", "Could not handle 'prop-filter' on %s in SQL", $propertyname );
143 continue;
145 $subfilter = $v->GetContent();
146 $success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
147 if ( $success === false ) continue; else {
148 $sql .= $success['sql'];
149 $params = array_merge( $params, $success['params'] );
151 break;
153 case 'urn:ietf:params:xml:ns:carddav:param-filter':
154 $need_post_filter = true;
155 return false; /** Figure out how to handle PARAM-FILTER conditions in the SQL */
157 $parameter = $v->GetAttribute("name");
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;
167 default:
168 dbg_error_log("cardquery", "Could not handle unknown tag '%s' in calendar query report", $tag );
169 break;
172 dbg_error_log("cardquery", "Generated SQL was '%s'", $sql );
173 return array( 'sql' => $sql, 'params' => $params );
178 * Something that we can handle, at least roughly correctly.
181 $responses = array();
182 $target_collection = new DAVResource($request->path);
183 $bound_from = $target_collection->bound_from();
184 if ( !$target_collection->Exists() ) {
185 $request->DoResponse( 404 );
187 if ( ! ($target_collection->IsAddressbook() || $target_collection->IsSchedulingCollection()) ) {
188 $request->DoResponse( 403, translate('The addressbook-query report must be run against an addressbook collection') );
192 * @todo Once we are past DB version 1.2.1 we can change this query more radically. The best performance to
193 * date seems to be:
194 * SELECT caldav_data.*,address_item.* FROM collection JOIN address_item USING (collection_id,user_no)
195 * JOIN caldav_data USING (dav_id) WHERE collection.dav_name = '/user1/home/'
196 * AND caldav_data.caldav_type = 'VEVENT' ORDER BY caldav_data.user_no, caldav_data.dav_name;
199 $params = array();
200 $where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
201 if ( is_array($qry_filters) ) {
202 dbg_log_array( 'cardquery', 'qry_filters', $qry_filters, true );
203 $components = array();
204 $filter_fragment = SqlFilterCardDAV( $qry_filters, $components );
205 if ( $filter_fragment !== false ) {
206 $where .= ' '.$filter_fragment['sql'];
207 $params = $filter_fragment['params'];
210 else {
211 dbg_error_log( 'cardquery', 'No query filters' );
214 $sql = 'SELECT * FROM caldav_data INNER JOIN addressbook_resource USING(dav_id)'. $where;
215 if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY dav_id";
216 $qry = new AwlQuery( $sql, $params );
217 if ( $qry->Exec("cardquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
218 while( $address_object = $qry->Fetch() ) {
219 if ( !$need_post_filter || apply_filter( $qry_filters, $address_object ) ) {
220 if ( $bound_from != $target_collection->dav_name() ) {
221 $address_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $address_object->dav_name);
223 if ( count($address_data_properties) > 0 ) {
224 $vcard = new VCard($address_object->caldav_data);
225 $vcard->MaskProperties($address_data_properties);
226 $address_object->caldav_data = $vcard->Render();
228 $responses[] = component_to_xml( $properties, $address_object );
232 $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
234 $request->XMLResponse( 207, $multistatus );