mirror of
https://gitlab.com/davical-project/davical.git
synced 2026-05-31 03:34:18 +00:00
Add initial support for addressbook-query REPORT.
This commit is contained in:
parent
4b37c632f6
commit
f0f708ec38
233
inc/caldav-REPORT-cardquery.php
Normal file
233
inc/caldav-REPORT-cardquery.php
Normal file
@ -0,0 +1,233 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require_once('vcard.php');
|
||||||
|
|
||||||
|
$address_data_properties = array();
|
||||||
|
function get_address_properties( $address_data_xml ) {
|
||||||
|
global $address_data_properties;
|
||||||
|
$expansion = $address_data_xml->GetElements();
|
||||||
|
foreach( $expansion AS $k => $v ) {
|
||||||
|
$address_data_properties[strtoupper($v->GetAttribute('name'))] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Build the array of properties to include in the report output
|
||||||
|
*/
|
||||||
|
$qry_content = $xmltree->GetContent('urn:ietf:params:xml:ns:carddav:addressbook-query');
|
||||||
|
$proptype = $qry_content[0]->GetTag();
|
||||||
|
$properties = array();
|
||||||
|
switch( $proptype ) {
|
||||||
|
case 'DAV::prop':
|
||||||
|
$qry_props = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/'.$proptype.'/*');
|
||||||
|
foreach( $qry_content[0]->GetElements() AS $k => $v ) {
|
||||||
|
$propertyname = preg_replace( '/^.*:/', '', $v->GetTag() );
|
||||||
|
$properties[$propertyname] = 1;
|
||||||
|
if ( $v->GetTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'DAV::allprop':
|
||||||
|
$properties['allprop'] = 1;
|
||||||
|
if ( $qry_content[1]->GetTag() == 'DAV::include' ) {
|
||||||
|
foreach( $qry_content[1]->GetElements() AS $k => $v ) {
|
||||||
|
$include_properties[] = $v->GetTag(); /** $include_properties is referenced in DAVResource where allprop is expanded */
|
||||||
|
if ( $v->GetTag() == 'urn:ietf:params:xml:ns:carddav:address-data' ) get_address_properties($v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$propertyname = preg_replace( '/^.*:/', '', $proptype );
|
||||||
|
$properties[$propertyname] = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* There can only be *one* FILTER element.
|
||||||
|
*/
|
||||||
|
$qry_filters = $xmltree->GetPath('/urn:ietf:params:xml:ns:carddav:addressbook-query/urn:ietf:params:xml:ns:carddav:filter/*');
|
||||||
|
if ( count($qry_filters) != 1 ) {
|
||||||
|
/* $qry_filters = $qry_filters[0]; // There can only be one FILTER element
|
||||||
|
}
|
||||||
|
else { */
|
||||||
|
$qry_filters = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* While we can construct our SQL to apply some filters in the query, other filters
|
||||||
|
* need to be checked against the retrieved record. This is for handling those ones.
|
||||||
|
*
|
||||||
|
* @param array $filter An array of XMLElement which is the filter definition
|
||||||
|
* @param string $item The database row retrieved for this calendar item
|
||||||
|
*
|
||||||
|
* @return boolean True if the check succeeded, false otherwise.
|
||||||
|
*/
|
||||||
|
function apply_filter( $filters, $item ) {
|
||||||
|
global $session, $c, $request;
|
||||||
|
|
||||||
|
if ( count($filters) == 0 ) return true;
|
||||||
|
|
||||||
|
dbg_error_log("cardquery","Applying filter for item '%s'", $item->dav_name );
|
||||||
|
$vcard = new vComponent( $item->caldav_data );
|
||||||
|
return $vcard->TestFilter($filters);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process a filter fragment returning an SQL fragment
|
||||||
|
*/
|
||||||
|
$need_post_filter = false;
|
||||||
|
$matchnum = 0;
|
||||||
|
function SqlFilterCardDAV( $filter, $components, $property = null, $parameter = null ) {
|
||||||
|
global $need_post_filter, $target_collection, $matchnum;
|
||||||
|
$sql = "";
|
||||||
|
$params = array();
|
||||||
|
if ( !is_array($filter) ) {
|
||||||
|
dbg_error_log( "cardquery", "Filter is of type '%s', but should be an array of XML Tags.", gettype($filter) );
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach( $filter AS $k => $v ) {
|
||||||
|
$tag = $v->GetTag();
|
||||||
|
dbg_error_log("cardquery", "Processing $tag into SQL - %d, '%s', %d\n", count($components), $property, isset($parameter) );
|
||||||
|
|
||||||
|
$not_defined = "";
|
||||||
|
switch( $tag ) {
|
||||||
|
case 'urn:ietf:params:xml:ns:carddav:text-match':
|
||||||
|
$search = $v->GetContent();
|
||||||
|
$negate = $v->GetAttribute("negate-condition");
|
||||||
|
$collation = $v->GetAttribute("collation");
|
||||||
|
switch( strtolower($collation) ) {
|
||||||
|
case 'i;octet':
|
||||||
|
$comparison = 'LIKE';
|
||||||
|
break;
|
||||||
|
case 'i;ascii-casemap':
|
||||||
|
case 'i;unicode-casemap':
|
||||||
|
default:
|
||||||
|
$comparison = 'ILIKE';
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
$pname = ':text_match_'.$matchnum++;
|
||||||
|
$params[$pname] = '%'.$search.'%';
|
||||||
|
dbg_error_log("cardquery", " text-match: (%s%s %s '%s') ", (isset($negate) && strtolower($negate) == "yes" ? "NOT ": ""),
|
||||||
|
$property, $comparison, $params[$pname] );
|
||||||
|
$sql .= sprintf( "AND (%s%s %s $pname) ", (isset($negate) && strtolower($negate) == "yes" ? "NOT ": ""),
|
||||||
|
$property, $comparison );
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'urn:ietf:params:xml:ns:carddav:prop-filter':
|
||||||
|
$propertyname = $v->GetAttribute("name");
|
||||||
|
switch( $propertyname ) {
|
||||||
|
case 'VERSION':
|
||||||
|
case 'UID':
|
||||||
|
case 'NICKNAME':
|
||||||
|
case 'FN':
|
||||||
|
case 'NOTE':
|
||||||
|
case 'ORG':
|
||||||
|
case 'URL':
|
||||||
|
case 'FBURL':
|
||||||
|
case 'CALADRURI':
|
||||||
|
case 'CALURI':
|
||||||
|
$property = strtolower($propertyname);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'N':
|
||||||
|
$property = 'name';
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
$need_post_filter = true;
|
||||||
|
dbg_error_log("cardquery", "Could not handle 'prop-filter' on %s in SQL", $propertyname );
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$subfilter = $v->GetContent();
|
||||||
|
$success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
|
||||||
|
if ( $success === false ) continue; else {
|
||||||
|
$sql .= $success['sql'];
|
||||||
|
$params = array_merge( $params, $success['params'] );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'urn:ietf:params:xml:ns:carddav:param-filter':
|
||||||
|
$need_post_filter = true;
|
||||||
|
return false; /** Figure out how to handle PARAM-FILTER conditions in the SQL */
|
||||||
|
/*
|
||||||
|
$parameter = $v->GetAttribute("name");
|
||||||
|
$subfilter = $v->GetContent();
|
||||||
|
$success = SqlFilterCardDAV( $subfilter, $components, $property, $parameter );
|
||||||
|
if ( $success === false ) continue; else {
|
||||||
|
$sql .= $success['sql'];
|
||||||
|
$params = array_merge( $params, $success['params'] );
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
*/
|
||||||
|
|
||||||
|
default:
|
||||||
|
dbg_error_log("cardquery", "Could not handle unknown tag '%s' in calendar query report", $tag );
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dbg_error_log("cardquery", "Generated SQL was '%s'", $sql );
|
||||||
|
return array( 'sql' => $sql, 'params' => $params );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Something that we can handle, at least roughly correctly.
|
||||||
|
*/
|
||||||
|
|
||||||
|
$responses = array();
|
||||||
|
$target_collection = new DAVResource($request->path);
|
||||||
|
$bound_from = $target_collection->bound_from();
|
||||||
|
if ( !$target_collection->Exists() ) {
|
||||||
|
$request->DoResponse( 404 );
|
||||||
|
}
|
||||||
|
if ( ! ($target_collection->IsAddressbook() || $target_collection->IsSchedulingCollection()) ) {
|
||||||
|
$request->DoResponse( 403, translate('The addressbook-query report must be run against an addressbook collection') );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @todo Once we are past DB version 1.2.1 we can change this query more radically. The best performance to
|
||||||
|
* date seems to be:
|
||||||
|
* SELECT caldav_data.*,address_item.* FROM collection JOIN address_item USING (collection_id,user_no)
|
||||||
|
* JOIN caldav_data USING (dav_id) WHERE collection.dav_name = '/user1/home/'
|
||||||
|
* AND caldav_data.caldav_type = 'VEVENT' ORDER BY caldav_data.user_no, caldav_data.dav_name;
|
||||||
|
*/
|
||||||
|
|
||||||
|
$params = array();
|
||||||
|
$where = ' WHERE caldav_data.collection_id = ' . $target_collection->resource_id();
|
||||||
|
if ( is_array($qry_filters) ) {
|
||||||
|
dbg_log_array( 'cardquery', 'qry_filters', $qry_filters, true );
|
||||||
|
$components = array();
|
||||||
|
$filter_fragment = SqlFilterCardDAV( $qry_filters, $components );
|
||||||
|
if ( $filter_fragment !== false ) {
|
||||||
|
$where .= ' '.$filter_fragment['sql'];
|
||||||
|
$params = $filter_fragment['params'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
dbg_error_log( 'cardquery', 'No query filters' );
|
||||||
|
}
|
||||||
|
|
||||||
|
$sql = 'SELECT * FROM caldav_data INNER JOIN addressbook_resource USING(dav_id)'. $where;
|
||||||
|
if ( isset($c->strict_result_ordering) && $c->strict_result_ordering ) $sql .= " ORDER BY dav_id";
|
||||||
|
$qry = new AwlQuery( $sql, $params );
|
||||||
|
if ( $qry->Exec("cardquery",__LINE__,__FILE__) && $qry->rows() > 0 ) {
|
||||||
|
while( $address_object = $qry->Fetch() ) {
|
||||||
|
if ( !$need_post_filter || apply_filter( $qry_filters, $address_object ) ) {
|
||||||
|
if ( $bound_from != $target_collection->dav_name() ) {
|
||||||
|
$address_object->dav_name = str_replace( $bound_from, $target_collection->dav_name(), $address_object->dav_name);
|
||||||
|
}
|
||||||
|
if ( count($address_data_properties) > 0 ) {
|
||||||
|
$vcard = new VCard($address_object->caldav_data);
|
||||||
|
$vcard->MaskProperties($address_data_properties);
|
||||||
|
$address_object->caldav_data = $vcard->Render();
|
||||||
|
}
|
||||||
|
$responses[] = component_to_xml( $properties, $address_object );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() );
|
||||||
|
|
||||||
|
$request->XMLResponse( 207, $multistatus );
|
||||||
@ -38,22 +38,22 @@ note=:note, org=:org, url=:url, fburl=:fburl, caladruri=:caladruri, caluri=:calu
|
|||||||
$sql = 'INSERT INTO addressbook_resource ( dav_id, version, uid, nickname, fn, n, note, org, url, fburl, caladruri, caluri )
|
$sql = 'INSERT INTO addressbook_resource ( dav_id, version, uid, nickname, fn, n, note, org, url, fburl, caladruri, caluri )
|
||||||
VALUES( :dav_id, :version, :uid, :nickname, :fn, :name, :note, :org, :url, :fburl, :caladruri, :caluri )';
|
VALUES( :dav_id, :version, :uid, :nickname, :fn, :name, :note, :org, :url, :fburl, :caladruri, :caluri )';
|
||||||
}
|
}
|
||||||
|
|
||||||
$params = array( ':dav_id' => $dav_id );
|
$params = array( ':dav_id' => $dav_id );
|
||||||
|
|
||||||
/**
|
$wanted = array('VERSION' => true, 'UID' => true, 'NICKNAME' => true, 'FN' => true, 'N' => true,
|
||||||
* @TODO: Most of these allow duplicates, so we should save the preferred one in the user's language in such cases.
|
'NOTE'=> true, 'ORG' => true, 'URL' => true, 'FBURL' => true, 'CALADRURI' => true, 'CALURI' => true);
|
||||||
*/
|
$properties = $this->GetProperties( $wanted );
|
||||||
$params[':version'] = $this->GetPValue('VERSION');
|
foreach( $wanted AS $k => $v ) {
|
||||||
$params[':uid'] = $this->GetPValue('UID');
|
$pname = ':' . strtolower($k);
|
||||||
$params[':nickname'] = $this->GetPValue('NICKNAME');
|
if ( $pname == ':n' ) $pname = ':name';
|
||||||
$params[':fn'] = $this->GetPValue('FN');
|
$params[$pname] = null;
|
||||||
$params[':name'] = $this->GetPValue('N');
|
}
|
||||||
$params[':note'] = $this->GetPValue('NOTE');
|
foreach( $properties AS $k => $v ) {
|
||||||
$params[':org'] = $this->GetPValue('ORG');
|
$pname = ':' . strtolower($v->Name());
|
||||||
$params[':url'] = $this->GetPValue('URL');
|
if ( $pname == ':n' ) $pname = ':name';
|
||||||
$params[':fburl'] = $this->GetPValue('FBURL');
|
if ( !isset($params[$pname]) /** @TODO: or this is one is in the user's language */ ) $params[$pname] = $v->Value();
|
||||||
$params[':caladruri'] = $this->GetPValue('CALADRURI');
|
}
|
||||||
$params[':caluri'] = $this->GetPValue('CALURI');
|
|
||||||
|
|
||||||
$qry->QDo( $sql, $params );
|
$qry->QDo( $sql, $params );
|
||||||
|
|
||||||
|
|||||||
30
testing/tests/carddav/2030-REPORT-addressbook-query.result
Normal file
30
testing/tests/carddav/2030-REPORT-addressbook-query.result
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
HTTP/1.1 207 Multi-Status
|
||||||
|
Date: Dow, 01 Jan 2000 00:00:00 GMT
|
||||||
|
DAV: 1, 2, 3, access-control, calendar-access, calendar-schedule
|
||||||
|
DAV: extended-mkcol, calendar-proxy, bind, addressbook, calendar-auto-schedule
|
||||||
|
ETag: "8c1e871ceac19694850a8898e844d4b4"
|
||||||
|
Content-Length: 655
|
||||||
|
Content-Type: text/xml; charset="utf-8"
|
||||||
|
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<multistatus xmlns="DAV:" xmlns:VC="urn:ietf:params:xml:ns:carddav">
|
||||||
|
<response>
|
||||||
|
<href>/caldav.php/user1/addressbook/andrew_mcmillan.vcf</href>
|
||||||
|
<propstat>
|
||||||
|
<prop>
|
||||||
|
<getetag>"24bae2a7f81c0a79413a656c06d8326b"</getetag>
|
||||||
|
<VC:address-data>BEGIN:VCARD
|
||||||
|
VERSION:3.0
|
||||||
|
UID:pas-id-4BB0719600000000
|
||||||
|
EMAIL;X-EVOLUTION-UI-SLOT=2;TYPE=HOME:andrew@mcmillan.net.nz
|
||||||
|
EMAIL;X-EVOLUTION-UI-SLOT=1;TYPE=WORK:andrew@morphoss.com
|
||||||
|
N:McMillan;Andrew;Watson;;
|
||||||
|
FN:Andrew Watson McMillan
|
||||||
|
NICKNAME:karora
|
||||||
|
END:VCARD
|
||||||
|
</VC:address-data>
|
||||||
|
</prop>
|
||||||
|
<status>HTTP/1.1 200 OK</status>
|
||||||
|
</propstat>
|
||||||
|
</response>
|
||||||
|
</multistatus>
|
||||||
36
testing/tests/carddav/2030-REPORT-addressbook-query.test
Normal file
36
testing/tests/carddav/2030-REPORT-addressbook-query.test
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
#
|
||||||
|
# Request a REPORT
|
||||||
|
#
|
||||||
|
TYPE=REPORT
|
||||||
|
URL=http://mycaldav/caldav.php/user1/addressbook/
|
||||||
|
|
||||||
|
HEADER=Accept: text/xml,application/xml
|
||||||
|
HEADER=Content-Type: text/xml
|
||||||
|
HEADER=Depth: 1
|
||||||
|
|
||||||
|
HEAD
|
||||||
|
|
||||||
|
BEGINDATA
|
||||||
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
|
<C:addressbook-query xmlns:D="DAV:"
|
||||||
|
xmlns:C="urn:ietf:params:xml:ns:carddav">
|
||||||
|
<D:prop>
|
||||||
|
<D:getetag/>
|
||||||
|
<C:address-data>
|
||||||
|
<C:prop name="VERSION"/>
|
||||||
|
<C:prop name="UID"/>
|
||||||
|
<C:prop name="N"/>
|
||||||
|
<C:prop name="NICKNAME"/>
|
||||||
|
<C:prop name="EMAIL"/>
|
||||||
|
<C:prop name="FN"/>
|
||||||
|
</C:address-data>
|
||||||
|
</D:prop>
|
||||||
|
<C:filter>
|
||||||
|
<C:prop-filter name="NICKNAME">
|
||||||
|
<C:text-match collation="i;unicode-casemap"
|
||||||
|
match-type="equals"
|
||||||
|
>karora</C:text-match>
|
||||||
|
</C:prop-filter>
|
||||||
|
</C:filter>
|
||||||
|
</C:addressbook-query>
|
||||||
|
ENDDATA
|
||||||
Loading…
x
Reference in New Issue
Block a user