forked from mstilkerich/rcmcarddav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carddav_discovery.php
286 lines (237 loc) · 7.95 KB
/
carddav_discovery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
/*
RCM CardDAV Plugin
Copyright (C) 2011-2016 Benjamin Schieder <rcmcarddav@wegwerf.anderdonau.de>,
Michael Stilkerich <ms@mike2k.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
require_once("carddav_backend.php");
require_once("carddav_common.php");
class carddav_discovery
{
private static $helper;
/**
* Determines the location of the addressbook for the current user on the
* CardDAV server.
*
* Returns: Array of found addressbook. Each array element is array with keys:
* - name: Name of the addressbook reported by server
* - href: URL to the addressbook collection
*
* On error, false is returned.
*/
public function find_addressbooks($url, $user, $password)
{{{
if (!preg_match(';^(([^:]+)://)?(([^/:]+)(:([0-9]+))?)(/?.*)$;', $url, $match))
return false;
$protocol = $match[2]; // optional
$host = $match[4]; // mandatory
$port = $match[6]; // optional
$path = $match[7]; // optional
// plain is only used if http was explicitly given
$use_ssl = !($protocol == "http");
// setup default values if no user values given
if($use_ssl) {
$protocol = $protocol?$protocol:'https';
$port = $port ?$port :443;
} else {
$protocol = $protocol?$protocol:'http';
$port = $port ?$port :80;
}
$services = $this->find_servers($host, $use_ssl);
// Fallback: If no DNS provided, we use the data given by the user/defaults
if(count($services) == 0) {
$services[] = array(
'host' => $host,
'port' => ($port ? $port : ($use_ssl ? 443 : 80)),
'baseurl' => "$protocol://$host:$port",
);
}
$services = $this->find_baseurls($services);
// if the user specified a full URL, we try that first
if(strlen($path) > 2) {
$userspecified = array(
'host' => $host,
'port' => ($port ? $port : ($use_ssl ? 443 : 80)),
'baseurl' => "$protocol://$host:$port",
'paths' => array($path),
);
array_unshift($services, $userspecified);
}
$cdfopen_cfg = array('username'=>$user, 'password'=>$password);
// now check each of them until we find something (or don't)
foreach($services as $service) {
if (!array_key_exists('paths', $service)) {
continue;
}
$cdfopen_cfg['url'] = $service['baseurl'];
foreach($service['paths'] as $path) {
$aBooks = $this->retrieve_addressbooks($path, $cdfopen_cfg);
if(is_array($aBooks) && count($aBooks)>0)
return $aBooks;
}
}
return false;
}}}
private function retrieve_addressbooks($path, $cdfopen_cfg, $recurse=false)
{{{
$baseurl = $cdfopen_cfg['url'];
$url = carddav_common::concaturl($baseurl, $path);
$depth = ($recurse ? 1 : 0);
self::$helper->debug("SEARCHING $url (Depth: ".$depth.")");
// check if the given URL points to an addressbook
$opts = array(
'method'=>"PROPFIND",
'header'=>array("Depth: " . $depth, 'Content-Type: application/xml; charset="utf-8"'),
'content'=> <<<EOF
<?xml version="1.0" encoding="utf-8"?>
<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav"><D:prop>
<D:current-user-principal/>
<D:resourcetype />
<D:displayname />
<C:addressbook-home-set/>
</D:prop></D:propfind>
EOF
);
$reply = self::$helper->cdfopen($url, $opts, $cdfopen_cfg);
$xml = self::$helper->checkAndParseXML($reply);
if($xml === false) return false;
$aBooks = array();
// Check if we found addressbooks at the URL
$xpresult = $xml->xpath('//RCMCD:response[descendant::RCMCD:resourcetype/RCMCC:addressbook]');
foreach($xpresult as $ab) {
self::$helper->registerNamespaces($ab);
$aBook = array();
list($aBook['href']) = $ab->xpath('child::RCMCD:href');
list($aBook['name']) = $ab->xpath('descendant::RCMCD:displayname');
$aBook['href'] = (string) $aBook['href'];
$aBook['name'] = (string) $aBook['name'];
if(strlen($aBook['href']) > 0) {
$aBook['href'] = carddav_common::concaturl($baseurl, $aBook['href']);
self::$helper->debug("found abook: ".$aBook['name']." at ".$aBook['href']);
$aBooks[] = $aBook;
}
}
// found -> done
if(count($aBooks) > 0) return $aBooks;
if ($recurse === false) {
// Check some additional URLs:
$additional_urls = array();
// (1) original URL
$additional_urls[] = $url;
// (2) see if the server told us the addressbook home location
$abookhome = $xml->xpath('//RCMCC:addressbook-home-set/RCMCD:href');
if (count($abookhome) != 0) {
self::$helper->debug("addressbook home: ".$abookhome[0]);
$additional_urls[] = $abookhome[0];
}
// (3) see if we got a principal URL
$princurl = $xml->xpath('//RCMCD:current-user-principal/RCMCD:href');
if (count($princurl) != 0) {
self::$helper->debug("principal URL: ".$princurl[0]);
$additional_urls[] = $princurl[0];
}
foreach($additional_urls as $other_url) {
self::$helper->debug("Searching additional URL: $other_url");
if(strlen($other_url) <= 0) continue;
// if the server returned a full URL, adjust the base url
if (preg_match(';^[^/]+://[^/]+;', $other_url, $match)) {
$cdfopen_cfg['url'] = $match[0];
} else {
// restore original baseurl, may have changed in prev URL check
$cdfopen_cfg['url'] = $baseurl;
}
$aBooks = $this->retrieve_addressbooks($other_url, $cdfopen_cfg, $other_url != $princurl[0]);
// found -> done
if (!($aBooks === false) && count($aBooks) > 0) return $aBooks;
}
}
// (4) there is no more we can do -> fail
self::$helper->debug("no principal URL found");
return false;
}}}
// get services by querying DNS SRV records
private function find_servers($host, $ssl)
{{{
if($ssl) {
$srvpfx = '_carddavs';
$defport = 443;
$protocol = 'https';
} else {
$srvpfx = '_carddav';
$defport = 80;
$protocol = 'http';
}
$srv = "$srvpfx._tcp.$host";
// query SRV records
$dnsresults = dns_get_record($srv, DNS_SRV);
// order according to priority and weight
// TODO weight is not quite correctly handled atm, see RFC2782,
// but this is not crucial to functionality
$sortPrioWeight = function($a, $b) {
if ($a['pri'] != $b['pri']) {
return $b['pri'] - $a['pri'];
}
return $a['weight'] - $b['weight'];
};
usort($dnsresults, $sortPrioWeight);
// build results
$result = array();
foreach($dnsresults as $dnsres) {
$target = $dnsres['target'];
$port = $dnsres['port'] ? $dnsres['port'] : $defport;
$baseurl = "$protocol://$target:$port";
if($target) {
self::$helper->debug("found service: $baseurl");
$result[] = array(
'host' => $target,
'port' => $port,
'baseurl' => $baseurl,
'dnssrv' => "$srvpfx._tcp.$target",
);
}
}
return $result;
}}}
// discover path and add default paths to services
private function find_baseurls($services)
{{{
foreach($services as &$service) {
if (!array_key_exists('dnssrv', $service)) {
continue;
}
$baseurl = $service['baseurl'];
$dnssrv = $service['dnssrv'];
$paths = array();
$dnsresults = dns_get_record($dnssrv, DNS_TXT);
foreach($dnsresults as $dnsresult) {
if($dnsresult['host'] != $dnssrv) continue;
foreach($dnsresult['entries'] as $ent) {
if (preg_match('/^path=(.+)/', $ent, $match))
$paths[] = $match[1];
}
}
// as fallback try these default paths
$paths[] = '/.well-known/carddav';
$paths[] = '/';
$service['paths'] = $paths;
}
return $services;
}}}
public static function initClass()
{{{
self::$helper = new carddav_common('DISCOVERY: ');
}}}
}
carddav_discovery::initClass();
?>