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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
|
<?php
namespace Sabre\VObject;
/**
* This utility converts vcards from one version to another.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class VCardConverter
{
/**
* Converts a vCard object to a new version.
*
* targetVersion must be one of:
* Document::VCARD21
* Document::VCARD30
* Document::VCARD40
*
* Currently only 3.0 and 4.0 as input and output versions.
*
* 2.1 has some minor support for the input version, it's incomplete at the
* moment though.
*
* If input and output version are identical, a clone is returned.
*
* @param int $targetVersion
*/
public function convert(Component\VCard $input, $targetVersion)
{
$inputVersion = $input->getDocumentType();
if ($inputVersion === $targetVersion) {
return clone $input;
}
if (!in_array($inputVersion, [Document::VCARD21, Document::VCARD30, Document::VCARD40])) {
throw new \InvalidArgumentException('Only vCard 2.1, 3.0 and 4.0 are supported for the input data');
}
if (!in_array($targetVersion, [Document::VCARD30, Document::VCARD40])) {
throw new \InvalidArgumentException('You can only use vCard 3.0 or 4.0 for the target version');
}
$newVersion = Document::VCARD40 === $targetVersion ? '4.0' : '3.0';
$output = new Component\VCard([
'VERSION' => $newVersion,
]);
// We might have generated a default UID. Remove it!
unset($output->UID);
foreach ($input->children() as $property) {
$this->convertProperty($input, $output, $property, $targetVersion);
}
return $output;
}
/**
* Handles conversion of a single property.
*
* @param int $targetVersion
*/
protected function convertProperty(Component\VCard $input, Component\VCard $output, Property $property, $targetVersion)
{
// Skipping these, those are automatically added.
if (in_array($property->name, ['VERSION', 'PRODID'])) {
return;
}
$parameters = $property->parameters();
$valueType = null;
if (isset($parameters['VALUE'])) {
$valueType = $parameters['VALUE']->getValue();
unset($parameters['VALUE']);
}
if (!$valueType) {
$valueType = $property->getValueType();
}
if (Document::VCARD30 !== $targetVersion && 'PHONE-NUMBER' === $valueType) {
$valueType = null;
}
$newProperty = $output->createProperty(
$property->name,
$property->getParts(),
[], // parameters will get added a bit later.
$valueType
);
if (Document::VCARD30 === $targetVersion) {
if ($property instanceof Property\Uri && in_array($property->name, ['PHOTO', 'LOGO', 'SOUND'])) {
$newProperty = $this->convertUriToBinary($output, $newProperty);
} elseif ($property instanceof Property\VCard\DateAndOrTime) {
// In vCard 4, the birth year may be optional. This is not the
// case for vCard 3. Apple has a workaround for this that
// allows applications that support Apple's extension still
// omit birthyears in vCard 3, but applications that do not
// support this, will just use a random birthyear. We're
// choosing 1604 for the birthyear, because that's what apple
// uses.
$parts = DateTimeParser::parseVCardDateTime($property->getValue());
if (is_null($parts['year'])) {
$newValue = '1604-'.$parts['month'].'-'.$parts['date'];
$newProperty->setValue($newValue);
$newProperty['X-APPLE-OMIT-YEAR'] = '1604';
}
if ('ANNIVERSARY' == $newProperty->name) {
// Microsoft non-standard anniversary
$newProperty->name = 'X-ANNIVERSARY';
// We also need to add a new apple property for the same
// purpose. This apple property needs a 'label' in the same
// group, so we first need to find a groupname that doesn't
// exist yet.
$x = 1;
while ($output->select('ITEM'.$x.'.')) {
++$x;
}
$output->add('ITEM'.$x.'.X-ABDATE', $newProperty->getValue(), ['VALUE' => 'DATE-AND-OR-TIME']);
$output->add('ITEM'.$x.'.X-ABLABEL', '_$!<Anniversary>!$_');
}
} elseif ('KIND' === $property->name) {
switch (strtolower($property->getValue())) {
case 'org':
// vCard 3.0 does not have an equivalent to KIND:ORG,
// but apple has an extension that means the same
// thing.
$newProperty = $output->createProperty('X-ABSHOWAS', 'COMPANY');
break;
case 'individual':
// Individual is implicit, so we skip it.
return;
case 'group':
// OS X addressbook property
$newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-KIND', 'GROUP');
break;
}
} elseif ('MEMBER' === $property->name) {
$newProperty = $output->createProperty('X-ADDRESSBOOKSERVER-MEMBER', $property->getValue());
}
} elseif (Document::VCARD40 === $targetVersion) {
// These properties were removed in vCard 4.0
if (in_array($property->name, ['NAME', 'MAILER', 'LABEL', 'CLASS'])) {
return;
}
if ($property instanceof Property\Binary) {
$newProperty = $this->convertBinaryToUri($output, $newProperty, $parameters);
} elseif ($property instanceof Property\VCard\DateAndOrTime && isset($parameters['X-APPLE-OMIT-YEAR'])) {
// If a property such as BDAY contained 'X-APPLE-OMIT-YEAR',
// then we're stripping the year from the vcard 4 value.
$parts = DateTimeParser::parseVCardDateTime($property->getValue());
if ($parts['year'] === $property['X-APPLE-OMIT-YEAR']->getValue()) {
$newValue = '--'.$parts['month'].'-'.$parts['date'];
$newProperty->setValue($newValue);
}
// Regardless if the year matched or not, we do need to strip
// X-APPLE-OMIT-YEAR.
unset($parameters['X-APPLE-OMIT-YEAR']);
}
switch ($property->name) {
case 'X-ABSHOWAS':
if ('COMPANY' === strtoupper($property->getValue())) {
$newProperty = $output->createProperty('KIND', 'ORG');
}
break;
case 'X-ADDRESSBOOKSERVER-KIND':
if ('GROUP' === strtoupper($property->getValue())) {
$newProperty = $output->createProperty('KIND', 'GROUP');
}
break;
case 'X-ADDRESSBOOKSERVER-MEMBER':
$newProperty = $output->createProperty('MEMBER', $property->getValue());
break;
case 'X-ANNIVERSARY':
$newProperty->name = 'ANNIVERSARY';
// If we already have an anniversary property with the same
// value, ignore.
foreach ($output->select('ANNIVERSARY') as $anniversary) {
if ($anniversary->getValue() === $newProperty->getValue()) {
return;
}
}
break;
case 'X-ABDATE':
// Find out what the label was, if it exists.
if (!$property->group) {
break;
}
$label = $input->{$property->group.'.X-ABLABEL'};
// We only support converting anniversaries.
if (!$label || '_$!<Anniversary>!$_' !== $label->getValue()) {
break;
}
// If we already have an anniversary property with the same
// value, ignore.
foreach ($output->select('ANNIVERSARY') as $anniversary) {
if ($anniversary->getValue() === $newProperty->getValue()) {
return;
}
}
$newProperty->name = 'ANNIVERSARY';
break;
// Apple's per-property label system.
case 'X-ABLABEL':
if ('_$!<Anniversary>!$_' === $newProperty->getValue()) {
// We can safely remove these, as they are converted to
// ANNIVERSARY properties.
return;
}
break;
}
}
// set property group
$newProperty->group = $property->group;
if (Document::VCARD40 === $targetVersion) {
$this->convertParameters40($newProperty, $parameters);
} else {
$this->convertParameters30($newProperty, $parameters);
}
// Lastly, we need to see if there's a need for a VALUE parameter.
//
// We can do that by instantiating a empty property with that name, and
// seeing if the default valueType is identical to the current one.
$tempProperty = $output->createProperty($newProperty->name);
if ($tempProperty->getValueType() !== $newProperty->getValueType()) {
$newProperty['VALUE'] = $newProperty->getValueType();
}
$output->add($newProperty);
}
/**
* Converts a BINARY property to a URI property.
*
* vCard 4.0 no longer supports BINARY properties.
*
* @param Property\Uri $property the input property
* @param $parameters list of parameters that will eventually be added to
* the new property
*
* @return Property\Uri
*/
protected function convertBinaryToUri(Component\VCard $output, Property\Binary $newProperty, array &$parameters)
{
$value = $newProperty->getValue();
$newProperty = $output->createProperty(
$newProperty->name,
null, // no value
[], // no parameters yet
'URI' // Forcing the BINARY type
);
$mimeType = 'application/octet-stream';
// See if we can find a better mimetype.
if (isset($parameters['TYPE'])) {
$newTypes = [];
foreach ($parameters['TYPE']->getParts() as $typePart) {
if (in_array(
strtoupper($typePart),
['JPEG', 'PNG', 'GIF']
)) {
$mimeType = 'image/'.strtolower($typePart);
} else {
$newTypes[] = $typePart;
}
}
// If there were any parameters we're not converting to a
// mime-type, we need to keep them.
if ($newTypes) {
$parameters['TYPE']->setParts($newTypes);
} else {
unset($parameters['TYPE']);
}
}
$newProperty->setValue('data:'.$mimeType.';base64,'.base64_encode($value));
return $newProperty;
}
/**
* Converts a URI property to a BINARY property.
*
* In vCard 4.0 attachments are encoded as data: uri. Even though these may
* be valid in vCard 3.0 as well, we should convert those to BINARY if
* possible, to improve compatibility.
*
* @param Property\Uri $property the input property
*
* @return Property\Binary|null
*/
protected function convertUriToBinary(Component\VCard $output, Property\Uri $newProperty)
{
$value = $newProperty->getValue();
// Only converting data: uris
if ('data:' !== substr($value, 0, 5)) {
return $newProperty;
}
$newProperty = $output->createProperty(
$newProperty->name,
null, // no value
[], // no parameters yet
'BINARY'
);
$mimeType = substr($value, 5, strpos($value, ',') - 5);
if (strpos($mimeType, ';')) {
$mimeType = substr($mimeType, 0, strpos($mimeType, ';'));
$newProperty->setValue(base64_decode(substr($value, strpos($value, ',') + 1)));
} else {
$newProperty->setValue(substr($value, strpos($value, ',') + 1));
}
unset($value);
$newProperty['ENCODING'] = 'b';
switch ($mimeType) {
case 'image/jpeg':
$newProperty['TYPE'] = 'JPEG';
break;
case 'image/png':
$newProperty['TYPE'] = 'PNG';
break;
case 'image/gif':
$newProperty['TYPE'] = 'GIF';
break;
}
return $newProperty;
}
/**
* Adds parameters to a new property for vCard 4.0.
*/
protected function convertParameters40(Property $newProperty, array $parameters)
{
// Adding all parameters.
foreach ($parameters as $param) {
// vCard 2.1 allowed parameters with no name
if ($param->noName) {
$param->noName = false;
}
switch ($param->name) {
// We need to see if there's any TYPE=PREF, because in vCard 4
// that's now PREF=1.
case 'TYPE':
foreach ($param->getParts() as $paramPart) {
if ('PREF' === strtoupper($paramPart)) {
$newProperty->add('PREF', '1');
} else {
$newProperty->add($param->name, $paramPart);
}
}
break;
// These no longer exist in vCard 4
case 'ENCODING':
case 'CHARSET':
break;
default:
$newProperty->add($param->name, $param->getParts());
break;
}
}
}
/**
* Adds parameters to a new property for vCard 3.0.
*/
protected function convertParameters30(Property $newProperty, array $parameters)
{
// Adding all parameters.
foreach ($parameters as $param) {
// vCard 2.1 allowed parameters with no name
if ($param->noName) {
$param->noName = false;
}
switch ($param->name) {
case 'ENCODING':
// This value only existed in vCard 2.1, and should be
// removed for anything else.
if ('QUOTED-PRINTABLE' !== strtoupper($param->getValue())) {
$newProperty->add($param->name, $param->getParts());
}
break;
/*
* Converting PREF=1 to TYPE=PREF.
*
* Any other PREF numbers we'll drop.
*/
case 'PREF':
if ('1' == $param->getValue()) {
$newProperty->add('TYPE', 'PREF');
}
break;
default:
$newProperty->add($param->name, $param->getParts());
break;
}
}
}
}
|