|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Circles - Bring cloud-users closer together. |
|
6
|
|
|
* |
|
7
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
8
|
|
|
* later. See the COPYING file. |
|
9
|
|
|
* |
|
10
|
|
|
* @author Maxence Lange <[email protected]> |
|
11
|
|
|
* @copyright 2017 |
|
12
|
|
|
* @license GNU AGPL version 3 or any later version |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
namespace OCA\Circles\Model; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
use daita\MySmallPhpTools\Traits\TArrayTools; |
|
34
|
|
|
use JsonSerializable; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class DavCard |
|
39
|
|
|
* |
|
40
|
|
|
* @package OCA\Circles\Model |
|
41
|
|
|
*/ |
|
42
|
|
|
class DavCard implements JsonSerializable { |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
use TArrayTools; |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
const TYPE_CONTACT = 1; |
|
49
|
|
|
const TYPE_LOCAL = 2; |
|
50
|
|
|
// const TYPE_FEDERATED = 3; |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
/** @var int */ |
|
54
|
|
|
private $addressBookId = 0; |
|
55
|
|
|
|
|
56
|
|
|
/** @var string */ |
|
57
|
|
|
private $owner = ''; |
|
58
|
|
|
|
|
59
|
|
|
/** @var string */ |
|
60
|
|
|
private $cardUri = ''; |
|
61
|
|
|
|
|
62
|
|
|
/** @var string */ |
|
63
|
|
|
private $contactId = ''; |
|
64
|
|
|
|
|
65
|
|
|
/** @var string */ |
|
66
|
|
|
private $fn = ''; |
|
67
|
|
|
|
|
68
|
|
|
/** @var array */ |
|
69
|
|
|
private $emails = []; |
|
70
|
|
|
|
|
71
|
|
|
/** @var array */ |
|
72
|
|
|
private $clouds = []; |
|
73
|
|
|
|
|
74
|
|
|
/** @var array */ |
|
75
|
|
|
private $groups = []; |
|
76
|
|
|
|
|
77
|
|
|
/** @var Circle[] */ |
|
78
|
|
|
private $circles = []; |
|
79
|
|
|
|
|
80
|
|
|
/** @var string */ |
|
81
|
|
|
private $userId = ''; |
|
82
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
public function __construct() { |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return int |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getAddressBookId(): int { |
|
92
|
|
|
return $this->addressBookId; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param int $addressBookId |
|
97
|
|
|
* |
|
98
|
|
|
* @return DavCard |
|
99
|
|
|
*/ |
|
100
|
|
|
public function setAddressBookId(int $addressBookId): self { |
|
101
|
|
|
$this->addressBookId = $addressBookId; |
|
102
|
|
|
|
|
103
|
|
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getOwner(): string { |
|
111
|
|
|
return $this->owner; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $owner |
|
116
|
|
|
* |
|
117
|
|
|
* @return DavCard |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setOwner(string $owner): self { |
|
120
|
|
|
$this->owner = $owner; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getCardUri(): string { |
|
130
|
|
|
return $this->cardUri; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $cardUri |
|
135
|
|
|
* |
|
136
|
|
|
* @return DavCard |
|
137
|
|
|
*/ |
|
138
|
|
|
public function setCardUri(string $cardUri): self { |
|
139
|
|
|
$this->cardUri = $cardUri; |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getUniqueId(): string { |
|
149
|
|
|
return $this->getAddressBookId() . '/' . $this->getCardUri(); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return string |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getContactId(): string { |
|
157
|
|
|
return $this->contactId; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* @param string $contactId |
|
162
|
|
|
* |
|
163
|
|
|
* @return DavCard |
|
164
|
|
|
*/ |
|
165
|
|
|
public function setContactId(string $contactId): self { |
|
166
|
|
|
$this->contactId = $contactId; |
|
167
|
|
|
|
|
168
|
|
|
return $this; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return string |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getFn(): string { |
|
176
|
|
|
return $this->fn; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @param string $fn |
|
181
|
|
|
* |
|
182
|
|
|
* @return DavCard |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setFn(string $fn): self { |
|
185
|
|
|
$this->fn = $fn; |
|
186
|
|
|
|
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* @return array |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getEmails(): array { |
|
195
|
|
|
return $this->emails; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param array $emails |
|
200
|
|
|
* |
|
201
|
|
|
* @return DavCard |
|
202
|
|
|
*/ |
|
203
|
|
|
public function setEmails(array $emails): self { |
|
204
|
|
|
$this->emails = $emails; |
|
205
|
|
|
|
|
206
|
|
|
return $this; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* @return array |
|
212
|
|
|
*/ |
|
213
|
|
|
public function getClouds(): array { |
|
214
|
|
|
return $this->clouds; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param array $clouds |
|
219
|
|
|
* |
|
220
|
|
|
* @return DavCard |
|
221
|
|
|
*/ |
|
222
|
|
|
public function setClouds(array $clouds): self { |
|
223
|
|
|
$this->clouds = $clouds; |
|
224
|
|
|
|
|
225
|
|
|
return $this; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @return array |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getGroups(): array { |
|
233
|
|
|
return $this->groups; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param array $groups |
|
238
|
|
|
* |
|
239
|
|
|
* @return DavCard |
|
240
|
|
|
*/ |
|
241
|
|
|
public function setGroups(array $groups): self { |
|
242
|
|
|
$this->groups = $groups; |
|
243
|
|
|
|
|
244
|
|
|
return $this; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return Circle[] |
|
250
|
|
|
*/ |
|
251
|
|
|
public function getCircles(): array { |
|
252
|
|
|
return $this->circles; |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* @param Circle $circle |
|
257
|
|
|
* |
|
258
|
|
|
* @return $this |
|
259
|
|
|
*/ |
|
260
|
|
|
public function addCircle(Circle $circle): self { |
|
261
|
|
|
$this->circles[] = $circle; |
|
262
|
|
|
|
|
263
|
|
|
return $this; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
/** |
|
267
|
|
|
* @param array $circles |
|
268
|
|
|
* |
|
269
|
|
|
* @return DavCard |
|
270
|
|
|
*/ |
|
271
|
|
|
public function setCircles(array $circles): self { |
|
272
|
|
|
$this->circles = $circles; |
|
273
|
|
|
|
|
274
|
|
|
return $this; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @return string |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getUserId(): string { |
|
282
|
|
|
return $this->userId; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param string $userId |
|
287
|
|
|
* |
|
288
|
|
|
* @return DavCard |
|
289
|
|
|
*/ |
|
290
|
|
|
public function setUserId(string $userId): self { |
|
291
|
|
|
$this->userId = $userId; |
|
292
|
|
|
|
|
293
|
|
|
return $this; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
|
|
297
|
|
|
/** |
|
298
|
|
|
* @param array $data |
|
299
|
|
|
*/ |
|
300
|
|
|
public function import(array $data) { |
|
301
|
|
|
$this->setAddressBookId($this->get('addressBookId', $data)); |
|
302
|
|
|
$this->setOwner($this->get('owner', $data)); |
|
303
|
|
|
$this->setCardUri($this->get('cardUri', $data)); |
|
304
|
|
|
$this->setContactId($this->get('contactId', $data)); |
|
305
|
|
|
$this->setFn($this->get('fn', $data)); |
|
306
|
|
|
$this->setEmails($this->getArray('emails', $data)); |
|
307
|
|
|
$this->setClouds($this->getArray('clouds', $data)); |
|
308
|
|
|
$this->setGroups($this->getArray('groups', $data)); |
|
309
|
|
|
$this->setUserId($this->get('userId', $data)); |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* @param string $dav |
|
315
|
|
|
*/ |
|
316
|
|
|
public function importFromDav(string $dav) { |
|
317
|
|
|
$data = $this->parseDav($dav); |
|
318
|
|
|
|
|
319
|
|
|
$this->setContactId($this->get('UID', $data)); |
|
320
|
|
|
$this->setFn($this->get('FN', $data)); |
|
321
|
|
|
$this->setEmails($this->getArray('EMAILS', $data)); |
|
322
|
|
|
$this->setClouds($this->getArray('CLOUDS', $data)); |
|
323
|
|
|
$this->setGroups($this->getArray('CATEGORIES', $data)); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* get essential data from the dav content |
|
329
|
|
|
* (also don't think we need regex) |
|
330
|
|
|
* |
|
331
|
|
|
* @param string $dav |
|
332
|
|
|
* |
|
333
|
|
|
* @return array |
|
334
|
|
|
*/ |
|
335
|
|
|
private function parseDav(string $dav): array { |
|
336
|
|
|
$result = [ |
|
337
|
|
|
'UID' => '', |
|
338
|
|
|
'FN' => '', |
|
339
|
|
|
'EMAILS' => [], |
|
340
|
|
|
'CLOUDS' => [], |
|
341
|
|
|
'CATEGORIES' => [] |
|
342
|
|
|
]; |
|
343
|
|
|
|
|
344
|
|
|
$data = preg_split('/\R/', $dav); |
|
345
|
|
|
foreach ($data as $entry) { |
|
346
|
|
|
if (trim($entry) === '' || strpos($entry, ':') === false) { |
|
347
|
|
|
continue; |
|
348
|
|
|
} |
|
349
|
|
|
list($k, $v) = explode(':', $entry, 2); |
|
350
|
|
|
|
|
351
|
|
|
$k = strtoupper($k); |
|
352
|
|
|
if (strpos($entry, ';') !== false) { |
|
353
|
|
|
list($k) = explode(';', $entry, 2); |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
switch ($k) { |
|
357
|
|
|
case 'UID': |
|
358
|
|
|
case 'FN': |
|
359
|
|
|
$result[$k] = $v; |
|
360
|
|
|
break; |
|
361
|
|
|
|
|
362
|
|
|
case 'EMAIL': |
|
363
|
|
|
if ($v !== '') { |
|
364
|
|
|
$result['EMAILS'][] = $v; |
|
365
|
|
|
} |
|
366
|
|
|
break; |
|
367
|
|
|
|
|
368
|
|
|
case 'CLOUD': |
|
369
|
|
|
if ($v !== '') { |
|
370
|
|
|
$result['CLOUDS'][] = $v; |
|
371
|
|
|
} |
|
372
|
|
|
break; |
|
373
|
|
|
|
|
374
|
|
|
case 'CATEGORIES': |
|
375
|
|
|
if (strpos($v, ',') === false) { |
|
376
|
|
|
$result['CATEGORIES'] = [$v]; |
|
377
|
|
|
} else { |
|
378
|
|
|
$result['CATEGORIES'] = explode(',', $v); |
|
379
|
|
|
} |
|
380
|
|
|
break; |
|
381
|
|
|
} |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
return $result; |
|
385
|
|
|
|
|
386
|
|
|
} |
|
387
|
|
|
|
|
388
|
|
|
|
|
389
|
|
|
/** |
|
390
|
|
|
* @return array |
|
391
|
|
|
*/ |
|
392
|
|
|
public function jsonSerialize(): array { |
|
393
|
|
|
return [ |
|
394
|
|
|
'addressBookId' => $this->getAddressBookId(), |
|
395
|
|
|
'owner' => $this->getOwner(), |
|
396
|
|
|
'cardUri' => $this->getCardUri(), |
|
397
|
|
|
'contactId' => $this->getContactId(), |
|
398
|
|
|
'uniqueId' => $this->getUniqueId(), |
|
399
|
|
|
'fn' => $this->getFn(), |
|
400
|
|
|
'emails' => $this->getEmails(), |
|
401
|
|
|
'clouds' => $this->getClouds(), |
|
402
|
|
|
'groups' => $this->getGroups(), |
|
403
|
|
|
'userId' => $this->getUserId() |
|
404
|
|
|
]; |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
} |
|
408
|
|
|
|