forked from jens-maus/carddav2fb
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBackend.php
344 lines (305 loc) · 10 KB
/
Backend.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
<?php
namespace Andig\CardDav;
use Andig\Http\ClientTrait;
use Sabre\VObject\Document;
use Sabre\VObject\Reader;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
/**
* @author Christian Putzke <[email protected]>
* @author Andreas Goetz <[email protected]>
* @author Volker Püschel <[email protected]>
* @copyright Christian Putzke
* @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
*/
class Backend
{
use ClientTrait;
/**
* CardDAV server url
* @var string
*/
private $url;
/**
* VCard File URL Extension
* @var string
*/
private $vcard_extension = '.vcf';
/**
* Progress callback
* @var callable
*/
private $callback;
/**
* Set substitutions of links to embedded data
* @var array
*/
private $substitutes = [];
/**
* Cached http client
* @var Client|null
*/
private $client;
/**
* Request method
* @var string
*/
private $method;
/**
* Constructor
* Sets the CardDAV server url
*
* @param string $url CardDAV server url
*/
public function __construct(string $url = null, $method = 'REPORT')
{
if ($url) {
$this->setUrl($url);
}
$this->setClientOptions([
'headers' => [
'Depth' => 1
]
]);
$this->method = $method;
}
/**
* Set the properties/elements to be substituted
*
* @param array $elements the properties whose value should be replaced ('LOGO', 'KEY', 'PHOTO' or 'SOUND')
*/
public function setSubstitutes($elements)
{
foreach ($elements as $element) {
$this->substitutes[] = strtoupper($element);
}
}
/**
* Set and normalize server url
*
* @param string $url
* @return void
*/
public function setUrl(string $url)
{
$this->url = rtrim($url, '/') . '/';
// workaround for providers that don't use the default .vcf extension
if (strpos($this->url, "google.com")) {
$this->vcard_extension = '';
}
}
/**
* Set progress callback
*/
public function setProgress(callable $callback=null)
{
$this->callback = $callback;
}
/**
* Execute progress callback
*/
protected function progress()
{
if (is_callable($this->callback)) {
($this->callback)();
}
}
/**
* Get initialized http client. Improves download performance by up to x7
*
* @return Client
*/
private function getCachedClient(): Client
{
if (!$this->client) {
$this->client = $this->getClient();
}
return $this->client;
}
/**
* Gets all vCards including additional information from the CardDAV server
*
* @return array All parsed Vcards from backend
*/
public function getVcards(): array
{
if ($this->method == 'PROPFIND') {
$body = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<D:propfind xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
<D:prop>
<C:address-data content-type="text/vcard"/>
<D:getetag/>
</D:prop>
</D:propfind>
EOD;
} else {
$body = <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<C:addressbook-query xmlns:D="DAV:" xmlns:C="urn:ietf:params:xml:ns:carddav">
<D:prop>
<C:address-data content-type="text/vcard"/>
<D:getetag/>
</D:prop>
</C:addressbook-query>
EOD;
}
try {
$response = $this->getCachedClient()->request($this->method, $this->url, [
'headers' => [
'Content-Type' => 'application/xml',
],
'body' => $body
]);
} catch (RequestException $e) {
if ($e->hasResponse() && 404 == $e->getResponse()->getStatusCode()) {
error_log('Ignoring empty response from carddav REPORT request');
return [];
}
// bubble anything else
throw $e;
}
$cards = [];
$body = $this->stripNamespaces((string)$response->getBody());
$xml = new \SimpleXMLElement($body, LIBXML_PARSEHUGE);
// NOTE: instead of stripping the namespaces they could also be queried using xpath:
// $xml->registerXPathNamespace('dav', 'DAV:');
// $xml->registerXPathNamespace('card', 'urn:ietf:params:xml:ns:carddav');
// $xml->xpath('//dav:response/dav:propstat/dav:prop/card:address-data');
foreach ($xml->response as $response) {
foreach ($response->propstat->prop as $prop) {
$content = (string)$prop->{'address-data'};
if (empty($content)) { // PROPFIND: first node is empty
continue;
}
$vcard = Reader::read($content);
$vcard = $this->enrichVcard($vcard);
$cards[] = $vcard;
$this->progress();
}
}
return $cards;
}
/**
* If elements are declared as to be substituted,
* the data from possibly linked sources are embedded directly into the vCard
*
* @param Document $vcard single parsed vCard
* @param string $property the property whose value is to be replaced ('LOGO', 'KEY', 'PHOTO' or 'SOUND')
* @return Document single vCard with embedded value
*/
private function embedBase64(Document $vcard, string $property): Document
{
if ($embedded = $this->getLinkedData($vcard->$property)) { // get the data from the external URL or false
if ($vcard->VERSION == '3.0') { // the different vCard versions must be considered
unset($vcard->$property); // delete the old property
$vcard->add($property, $embedded['data'], ['TYPE' => strtoupper($embedded['subtype']), 'ENCODING' => 'b']);
} elseif ($vcard->VERSION == '4.0') {
unset($vcard->$property); // delete the old property
$vcard->add($property, 'data:' . $embedded['mimetype'] . ';base64,' . base64_encode($embedded['data']));
}
}
return $vcard;
}
/**
* Delivers an array including the previously linked data and its mime type details
* a mime type is composed of a type, a subtype, and optional parameters (e.g. "; charset=UTF-8")
*
* @param string $uri URL of the external linked data
* @return bool|array ['mimetype', e.g. "image/jpeg"
* 'type', e.g. "audio"
* 'subtype', e.g. "mpeg"
* 'parameters', whatever
* 'data'] the base64 encoded data
*/
private function getLinkedData(string $uri)
{
$response = $this->getCachedClient()->request('GET', $uri, ['http_errors' => false]);
if ($response->getStatusCode() != 200) {
return false;
}
$contentType = $response->getHeader('Content-Type');
@list($mimeType, $parameters) = explode(';', $contentType[0], 2);
@list($type, $subType) = explode('/', $mimeType);
$externalData = [
'mimetype' => $mimeType ?? '',
'type' => $type ?? '',
'subtype' => $subType ?? '',
'parameters' => $parameters ?? '',
'data' => (string)$response->getBody(),
];
return $externalData;
}
/**
* enrich the vcard with
* ->FULLNAME (equal to ->FN)
* ->LASTNAME, ->FIRSTNAME etc. extracted from ->N
* ->PHOTO with embedded data from linked sources (equal for KEY, LOGO or SOUND)
*
* @param Document $vcard
* @return Document
*/
public function enrichVcard(Document $vcard): Document
{
if (isset($vcard->{'X-ADDRESSBOOKSERVER-KIND'}) && $vcard->{'X-ADDRESSBOOKSERVER-KIND'} == 'group') {
return $vcard;
}
if (isset($vcard->FN)) { // redundant for downward compatibility
$vcard->FULLNAME = (string)$vcard->FN;
}
if (isset($vcard->N)) { // add 'N'-values to additional separate fields
foreach ($this->parseName($vcard->N) as $key => $value) {
if (!empty($value)) {
$vcard->$key = $value;
}
}
}
foreach (['PHOTO', 'LOGO', 'SOUND', 'KEY'] as $property) { // replace of linked data by embedded
if (!isset($vcard->$property)) {
continue;
}
if (in_array($property, $this->substitutes) && preg_match("/^http/", $vcard->$property)) {
$vcard = $this->embedBase64($vcard, $property);
}
}
return $vcard;
}
/**
* Cleans CardDAV XML response
* https://stackoverflow.com/questions/1245902/remove-namespace-from-xml-using-php
*
* @param string $xml CardDAV XML response
* @return string Cleaned CardDAV XML response
*/
private function stripNamespaces($xml)
{
// Gets rid of all namespace definitions
$xml = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $xml);
// Gets rid of all namespace references
$xml = preg_replace('/(<\/*)[^!>:]+:/', '$1', $xml);
return $xml;
}
/**
* split the values from 'N' into separate fields
*
* @param string $value
* @return array
*/
private function parseName($value)
{
@list(
$lastname,
$firstname,
$additional,
$prefix,
$suffix
) = explode(';', $value);
return [
'LASTNAME' => $lastname,
'FIRSTNAME' => $firstname,
'ADDITIONAL' => $additional,
'PREFIX' => $prefix,
'SUFFIX' => $suffix,
];
}
}