1 | <?php |
---|
2 | |
---|
3 | $responses = array(); |
---|
4 | |
---|
5 | |
---|
6 | /** |
---|
7 | * Build the array of properties to include in the report output |
---|
8 | */ |
---|
9 | $searches = $xmltree->GetPath('/DAV::principal-property-search/DAV::property-search'); |
---|
10 | dbg_log_array( "principal", "SEARCH", $searches, true ); |
---|
11 | |
---|
12 | $clause_joiner = " AND "; |
---|
13 | $CS_search_test = $xmltree->GetAttribute('test'); |
---|
14 | if ( isset($CS_search_test) && $CS_search_test == 'anyof' ) { |
---|
15 | $clause_joiner = " OR "; |
---|
16 | } |
---|
17 | |
---|
18 | $params = array(); |
---|
19 | $where = ""; |
---|
20 | foreach( $searches AS $k => $search ) { |
---|
21 | $qry_props = $search->GetPath('/DAV::property-search/DAV::prop/*'); // There may be many |
---|
22 | $match = $search->GetPath('/DAV::property-search/DAV::match'); // There may only be one |
---|
23 | dbg_log_array( "principal", "MATCH", $match, true ); |
---|
24 | $match = $match[0]->GetContent(); |
---|
25 | $subwhere = ""; |
---|
26 | foreach( $qry_props AS $k1 => $v1 ) { |
---|
27 | if ( $subwhere != "" ) $subwhere .= " OR "; |
---|
28 | switch( $v1->GetTag() ) { |
---|
29 | case 'DAV::displayname': |
---|
30 | $subwhere .= ' displayname ILIKE :displayname_match '; |
---|
31 | $params[':displayname_match'] = '%'.$match.'%'; |
---|
32 | break; |
---|
33 | |
---|
34 | case 'urn:ietf:params:xml:ns:caldav:calendar-user-address-set': |
---|
35 | $match = preg_replace('{^.*/caldav.php/([^/]+)(/.*)?$}', '\\1', $match); |
---|
36 | $match = preg_replace('{^mailto:}', '', $match); |
---|
37 | $subwhere .= ' (email ILIKE :user_address_match OR username ILIKE :user_address_match) '; |
---|
38 | $params[':user_address_match'] = '%'.$match.'%'; |
---|
39 | break; |
---|
40 | |
---|
41 | case 'urn:ietf:params:xml:ns:caldav:calendar-home-set': |
---|
42 | $match = preg_replace('{^.*/caldav.php}', '', $match); |
---|
43 | $subwhere .= ' dav_name LIKE :calendar_home_match '; |
---|
44 | $params[':calendar_home_match'] = $match.'%'; |
---|
45 | break; |
---|
46 | |
---|
47 | default: |
---|
48 | /** |
---|
49 | * @todo We should handle a lot more properties here. principal-URL seems a likely one to be used. |
---|
50 | * @todo We should catch the unsupported properties in the query and fire back an error indicating so. |
---|
51 | */ |
---|
52 | dbg_error_log("principal", "Unhandled tag '%s' to match '%s'\n", $v1->GetTag(), $match ); |
---|
53 | } |
---|
54 | } |
---|
55 | if ( $subwhere != "" ) { |
---|
56 | $where .= sprintf( "%s(%s)", ($where == "" ? "" : $clause_joiner), $subwhere ); |
---|
57 | } |
---|
58 | } |
---|
59 | if ( $where != "" ) $where = "WHERE $where"; |
---|
60 | $sql = "SELECT * FROM dav_principal $where ORDER BY principal_id LIMIT 100"; |
---|
61 | $qry = new AwlQuery($sql, $params); |
---|
62 | |
---|
63 | |
---|
64 | $get_props = $xmltree->GetPath('/DAV::principal-property-search/DAV::prop/*'); |
---|
65 | $properties = array(); |
---|
66 | foreach( $get_props AS $k1 => $v1 ) { |
---|
67 | $properties[] = $v1->GetTag(); |
---|
68 | } |
---|
69 | |
---|
70 | if ( $qry->Exec("REPORT",__LINE__,__FILE__) && $qry->rows() > 0 ) { |
---|
71 | while( $row = $qry->Fetch() ) { |
---|
72 | $principal = new DAVResource($row); |
---|
73 | $responses[] = $principal->RenderAsXML( $properties, $reply ); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | $multistatus = new XMLElement( "multistatus", $responses, $reply->GetXmlNsArray() ); |
---|
78 | |
---|
79 | $request->XMLResponse( 207, $multistatus ); |
---|