4
+-----------------------------------------------------------------------+
5
| program/include/rcube_vcard.php |
7
| This file is part of the Roundcube Webmail client |
8
| Copyright (C) 2008-2011, The Roundcube Dev Team |
9
| Licensed under the GNU GPL |
12
| Logical representation of a vcard address record |
13
+-----------------------------------------------------------------------+
14
| Author: Thomas Bruederli <roundcube@gmail.com> |
15
+-----------------------------------------------------------------------+
17
$Id: rcube_vcard.php 5160 2011-09-05 07:40:18Z thomasb $
23
* Logical representation of a vcard-based address record
24
* Provides functions to parse and export vCard data format
26
* @package Addressbook
27
* @author Thomas Bruederli <roundcube@gmail.com>
31
private static $values_decoded = false;
34
'N' => array(array('','','','','')),
36
private static $fieldmap = array(
43
'jobtitle' => 'TITLE',
44
'department' => 'X-DEPARTMENT',
45
'gender' => 'X-GENDER',
46
'maidenname' => 'X-MAIDENNAME',
47
'anniversary' => 'X-ANNIVERSARY',
48
'assistant' => 'X-ASSISTANT',
49
'manager' => 'X-MANAGER',
50
'spouse' => 'X-SPOUSE',
51
'edit' => 'X-AB-EDIT',
53
private $typemap = array('iPhone' => 'mobile', 'CELL' => 'mobile', 'WORK,FAX' => 'workfax');
54
private $phonetypemap = array('HOME1' => 'HOME', 'BUSINESS1' => 'WORK', 'BUSINESS2' => 'WORK2', 'BUSINESSFAX' => 'WORK,FAX');
55
private $addresstypemap = array('BUSINESS' => 'WORK');
56
private $immap = array('X-JABBER' => 'jabber', 'X-ICQ' => 'icq', 'X-MSN' => 'msn', 'X-AIM' => 'aim', 'X-YAHOO' => 'yahoo', 'X-SKYPE' => 'skype', 'X-SKYPE-USERNAME' => 'skype');
58
public $business = false;
66
public $email = array();
68
public static $eol = "\r\n";
73
public function __construct($vcard = null, $charset = RCMAIL_CHARSET, $detect = false, $fieldmap = array())
76
$this->extend_fieldmap($fieldmap);
79
$this->load($vcard, $charset, $detect);
84
* Load record from (internal, unfolded) vcard 3.0 format
86
* @param string vCard string to parse
87
* @param string Charset of string values
88
* @param boolean True if loading a 'foreign' vcard and extra heuristics for charset detection is required
90
public function load($vcard, $charset = RCMAIL_CHARSET, $detect = false)
92
self::$values_decoded = false;
93
$this->raw = self::vcard_decode($vcard);
95
// resolve charset parameters
96
if ($charset == null) {
97
$this->raw = self::charset_convert($this->raw);
99
// vcard has encoded values and charset should be detected
100
else if ($detect && self::$values_decoded &&
101
($detected_charset = self::detect_encoding(self::vcard_encode($this->raw))) && $detected_charset != RCMAIL_CHARSET) {
102
$this->raw = self::charset_convert($this->raw, $detected_charset);
105
// consider FN empty if the same as the primary e-mail address
106
if ($this->raw['FN'][0][0] == $this->raw['EMAIL'][0][0])
107
$this->raw['FN'][0][0] = '';
109
// find well-known address fields
110
$this->displayname = $this->raw['FN'][0][0];
111
$this->surname = $this->raw['N'][0][0];
112
$this->firstname = $this->raw['N'][0][1];
113
$this->middlename = $this->raw['N'][0][2];
114
$this->nickname = $this->raw['NICKNAME'][0][0];
115
$this->organization = $this->raw['ORG'][0][0];
116
$this->business = ($this->raw['X-ABSHOWAS'][0][0] == 'COMPANY') || (join('', (array)$this->raw['N'][0]) == '' && !empty($this->organization));
118
foreach ((array)$this->raw['EMAIL'] as $i => $raw_email)
119
$this->email[$i] = is_array($raw_email) ? $raw_email[0] : $raw_email;
121
// make the pref e-mail address the first entry in $this->email
122
$pref_index = $this->get_type_index('EMAIL', 'pref');
123
if ($pref_index > 0) {
124
$tmp = $this->email[0];
125
$this->email[0] = $this->email[$pref_index];
126
$this->email[$pref_index] = $tmp;
132
* Return vCard data as associative array to be unsed in Roundcube address books
134
* @return array Hash array with key-value pairs
136
public function get_assoc()
138
$out = array('name' => $this->displayname);
139
$typemap = $this->typemap;
141
// copy name fields to output array
142
foreach (array('firstname','surname','middlename','nickname','organization') as $col) {
143
if (strlen($this->$col))
144
$out[$col] = $this->$col;
147
if ($this->raw['N'][0][3])
148
$out['prefix'] = $this->raw['N'][0][3];
149
if ($this->raw['N'][0][4])
150
$out['suffix'] = $this->raw['N'][0][4];
152
// convert from raw vcard data into associative data for Roundcube
153
foreach (array_flip(self::$fieldmap) as $tag => $col) {
154
foreach ((array)$this->raw[$tag] as $i => $raw) {
155
if (is_array($raw)) {
160
if (!empty($raw['type'])) {
161
$combined = join(',', self::array_filter((array)$raw['type'], 'internet,pref', true));
162
$subtype = $typemap[$combined] ? $typemap[$combined] : ($typemap[$raw['type'][++$k]] ? $typemap[$raw['type'][$k]] : strtolower($raw['type'][$k]));
163
while ($k < count($raw['type']) && ($subtype == 'internet' || $subtype == 'pref'))
164
$subtype = $typemap[$raw['type'][++$k]] ? $typemap[$raw['type'][$k]] : strtolower($raw['type'][$k]);
167
// read vcard 2.1 subtype
169
foreach ($raw as $k => $v) {
170
if (!is_numeric($k) && $v === true && !in_array(strtolower($k), array('pref','internet','voice','base64'))) {
171
$subtype = $typemap[$k] ? $typemap[$k] : strtolower($k);
177
// force subtype if none set
178
if (!$subtype && preg_match('/^(email|phone|address|website)/', $key))
182
$key .= ':' . $subtype;
184
// split ADR values into assoc array
186
list(,, $value['street'], $value['locality'], $value['region'], $value['zipcode'], $value['country']) = $raw;
187
$out[$key][] = $value;
190
$out[$key][] = $raw[0];
198
// handle special IM fields as used by Apple
199
foreach ($this->immap as $tag => $type) {
200
foreach ((array)$this->raw[$tag] as $i => $raw) {
201
$out['im:'.$type][] = $raw[0];
206
if ($this->raw['PHOTO'])
207
$out['photo'] = $this->raw['PHOTO'][0][0];
214
* Convert the data structure into a vcard 3.0 string
216
public function export($folded = true)
218
$vcard = self::vcard_encode($this->raw);
219
return $folded ? self::rfc2425_fold($vcard) : $vcard;
224
* Clear the given fields in the loaded vcard data
226
* @param array List of field names to be reset
228
public function reset($fields = null)
231
$fields = array_merge(array_values(self::$fieldmap), array_keys($this->immap), array('FN','N','ORG','NICKNAME','EMAIL','ADR','BDAY'));
233
foreach ($fields as $f)
234
unset($this->raw[$f]);
236
if (!$this->raw['N'])
237
$this->raw['N'] = array(array('','','','',''));
238
if (!$this->raw['FN'])
239
$this->raw['FN'] = array();
241
$this->email = array();
246
* Setter for address record fields
248
* @param string Field name
249
* @param string Field value
250
* @param string Type/section name
252
public function set($field, $value, $type = 'HOME')
254
$field = strtolower($field);
255
$type_uc = strtoupper($type);
256
$typemap = array_flip($this->typemap);
261
$this->raw['FN'][0][0] = $value;
265
$this->raw['N'][0][0] = $value;
269
$this->raw['N'][0][1] = $value;
273
$this->raw['N'][0][2] = $value;
277
$this->raw['N'][0][3] = $value;
281
$this->raw['N'][0][4] = $value;
285
$this->raw['NICKNAME'][0][0] = $value;
289
$this->raw['ORG'][0][0] = $value;
293
if (strpos($value, 'http:') === 0) {
294
// TODO: fetch file from URL and save it locally?
295
$this->raw['PHOTO'][0] = array(0 => $value, 'URL' => true);
298
$encoded = !preg_match('![^a-z0-9/=+-]!i', $value);
299
$this->raw['PHOTO'][0] = array(0 => $encoded ? $value : base64_encode($value), 'BASE64' => true);
304
$this->raw['EMAIL'][] = array(0 => $value, 'type' => array_filter(array('INTERNET', $type_uc)));
305
$this->email[] = $value;
309
// save IM subtypes into extension fields
310
$typemap = array_flip($this->immap);
311
if ($field = $typemap[strtolower($type)])
312
$this->raw[$field][] = array(0 => $value);
316
if ($val = rcube_strtotime($value))
317
$this->raw['BDAY'][] = array(0 => date('Y-m-d', $val), 'value' => array('date'));
321
if ($this->addresstypemap[$type_uc])
322
$type = $this->addresstypemap[$type_uc];
324
$value = $value[0] ? $value : array('', '', $value['street'], $value['locality'], $value['region'], $value['zipcode'], $value['country']);
326
// fall through if not empty
327
if (!strlen(join('', $value)))
331
if ($field == 'phone' && $this->phonetypemap[$type_uc])
332
$type = $this->phonetypemap[$type_uc];
334
if (($tag = self::$fieldmap[$field]) && (is_array($value) || strlen($value))) {
335
$index = count($this->raw[$tag]);
336
$this->raw[$tag][$index] = (array)$value;
338
$this->raw[$tag][$index]['type'] = explode(',', ($typemap[$type] ? $typemap[$type] : $type));
345
* Setter for individual vcard properties
347
* @param string VCard tag name
348
* @param array Value-set of this vcard property
349
* @param boolean Set to true if the value-set should be appended instead of replacing any existing value-set
351
public function set_raw($tag, $value, $append = false)
353
$index = $append ? count($this->raw[$tag]) : 0;
354
$this->raw[$tag][$index] = (array)$value;
359
* Find index with the '$type' attribute
361
* @param string Field name
362
* @return int Field index having $type set
364
private function get_type_index($field, $type = 'pref')
367
if ($this->raw[$field]) {
368
foreach ($this->raw[$field] as $i => $data) {
369
if (is_array($data['type']) && in_array_nocase('pref', $data['type']))
379
* Convert a whole vcard (array) to UTF-8.
380
* If $force_charset is null, each member value that has a charset parameter will be converted
382
private static function charset_convert($card, $force_charset = null)
384
foreach ($card as $key => $node) {
385
foreach ($node as $i => $subnode) {
386
if (is_array($subnode) && (($charset = $force_charset) || ($subnode['charset'] && ($charset = $subnode['charset'][0])))) {
387
foreach ($subnode as $j => $value) {
388
if (is_numeric($j) && is_string($value))
389
$card[$key][$i][$j] = rcube_charset_convert($value, $charset);
391
unset($card[$key][$i]['charset']);
401
* Extends fieldmap definition
403
public function extend_fieldmap($map)
406
self::$fieldmap = array_merge($map, self::$fieldmap);
411
* Factory method to import a vcard file
413
* @param string vCard file content
414
* @return array List of rcube_vcard objects
416
public static function import($data)
420
// check if charsets are specified (usually vcard version < 3.0 but this is not reliable)
421
if (preg_match('/charset=/i', substr($data, 0, 2048)))
423
// detect charset and convert to utf-8
424
else if (($charset = self::detect_encoding($data)) && $charset != RCMAIL_CHARSET) {
425
$data = rcube_charset_convert($data, $charset);
426
$data = preg_replace(array('/^[\xFE\xFF]{2}/', '/^\xEF\xBB\xBF/', '/^\x00+/'), '', $data); // also remove BOM
427
$charset = RCMAIL_CHARSET;
431
$in_vcard_block = false;
433
foreach (preg_split("/[\r\n]+/", $data) as $i => $line) {
434
if ($in_vcard_block && !empty($line))
435
$vcard_block .= $line . "\n";
439
if (preg_match('/^END:VCARD$/i', $line)) {
441
$obj = new rcube_vcard(self::cleanup($vcard_block), $charset, true, self::$fieldmap);
442
if (!empty($obj->displayname) || !empty($obj->email))
445
$in_vcard_block = false;
447
else if (preg_match('/^BEGIN:VCARD$/i', $line)) {
448
$vcard_block = $line . "\n";
449
$in_vcard_block = true;
458
* Normalize vcard data for better parsing
460
* @param string vCard block
461
* @return string Cleaned vcard block
463
private static function cleanup($vcard)
465
// Convert special types (like Skype) to normal type='skype' classes with this simple regex ;)
466
$vcard = preg_replace(
467
'/item(\d+)\.(TEL|EMAIL|URL)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./s',
471
// convert Apple X-ABRELATEDNAMES into X-* fields for better compatibility
472
$vcard = preg_replace_callback(
473
'/item(\d+)\.(X-ABRELATEDNAMES)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./s',
474
array('self', 'x_abrelatednames_callback'),
477
// Remove cruft like item1.X-AB*, item1.ADR instead of ADR, and empty lines
478
$vcard = preg_replace(array('/^item\d*\.X-AB.*$/m', '/^item\d*\./m', "/\n+/"), array('', '', "\n"), $vcard);
480
// convert X-WAB-GENDER to X-GENDER
481
if (preg_match('/X-WAB-GENDER:(\d)/', $vcard, $matches)) {
482
$value = $matches[1] == '2' ? 'male' : 'female';
483
$vcard = preg_replace('/X-WAB-GENDER:\d/', 'X-GENDER:' . $value, $vcard);
486
// if N doesn't have any semicolons, add some
487
$vcard = preg_replace('/^(N:[^;\R]*)$/m', '\1;;;;', $vcard);
492
private static function x_abrelatednames_callback($matches)
494
return 'X-' . strtoupper($matches[5]) . $matches[3] . ':'. $matches[4];
497
private static function rfc2425_fold_callback($matches)
499
// chunk_split string and avoid lines breaking multibyte characters
501
$out .= substr($matches[1], 0, $c);
502
for ($n = $c; $c < strlen($matches[1]); $c++) {
503
// break if length > 75 or mutlibyte character starts after position 71
504
if ($n > 75 || ($n > 71 && ord($matches[1][$c]) >> 6 == 3)) {
508
$out .= $matches[1][$c];
515
public static function rfc2425_fold($val)
517
return preg_replace_callback('/([^\n]{72,})/', array('self', 'rfc2425_fold_callback'), $val);
522
* Decodes a vcard block (vcard 3.0 format, unfolded)
523
* into an array structure
525
* @param string vCard block to parse
526
* @return array Raw data structure
528
private static function vcard_decode($vcard)
530
// Perform RFC2425 line unfolding and split lines
531
$vcard = preg_replace(array("/\r/", "/\n\s+/"), '', $vcard);
532
$lines = explode("\n", $vcard);
535
for ($i=0; $i < count($lines); $i++) {
536
if (!preg_match('/^([^:]+):(.+)$/', $lines[$i], $line))
539
if (preg_match('/^(BEGIN|END)$/i', $line[1]))
542
// convert 2.1-style "EMAIL;internet;home:" to 3.0-style "EMAIL;TYPE=internet;TYPE=home:"
543
if (($data['VERSION'][0] == "2.1") && preg_match('/^([^;]+);([^:]+)/', $line[1], $regs2) && !preg_match('/^TYPE=/i', $regs2[2])) {
544
$line[1] = $regs2[1];
545
foreach (explode(';', $regs2[2]) as $prop)
546
$line[1] .= ';' . (strpos($prop, '=') ? $prop : 'TYPE='.$prop);
549
if (preg_match_all('/([^\\;]+);?/', $line[1], $regs2)) {
551
$field = strtoupper($regs2[1][0]);
553
foreach($regs2[1] as $attrid => $attr) {
554
if ((list($key, $value) = explode('=', $attr)) && $value) {
555
$value = trim($value);
556
if ($key == 'ENCODING') {
557
// add next line(s) to value string if QP line end detected
558
while ($value == 'QUOTED-PRINTABLE' && preg_match('/=$/', $lines[$i]))
559
$line[2] .= "\n" . $lines[++$i];
561
$line[2] = self::decode_value($line[2], $value);
564
$entry[strtolower($key)] = array_merge((array)$entry[strtolower($key)], (array)self::vcard_unquote($value, ','));
566
else if ($attrid > 0) {
567
$entry[$key] = true; // true means attr without =value
571
$entry = array_merge($entry, (array)self::vcard_unquote($line[2]));
572
$data[$field][] = $entry;
576
unset($data['VERSION']);
582
* Decode a given string with the encoding rule from ENCODING attributes
584
* @param string String to decode
585
* @param string Encoding type (quoted-printable and base64 supported)
586
* @return string Decoded 8bit value
588
private static function decode_value($value, $encoding)
590
switch (strtolower($encoding)) {
591
case 'quoted-printable':
592
self::$values_decoded = true;
593
return quoted_printable_decode($value);
596
self::$values_decoded = true;
597
return base64_decode($value);
606
* Encodes an entry for storage in our database (vcard 3.0 format, unfolded)
608
* @param array Raw data structure to encode
609
* @return string vCard encoded string
611
static function vcard_encode($data)
613
foreach((array)$data as $type => $entries) {
614
/* valid N has 5 properties */
615
while ($type == "N" && is_array($entries[0]) && count($entries[0]) < 5)
618
// make sure FN is not empty (required by RFC2426)
619
if ($type == "FN" && empty($entries))
620
$entries[0] = $data['EMAIL'][0][0];
622
foreach((array)$entries as $entry) {
624
if (is_array($entry)) {
626
foreach($entry as $attrname => $attrvalues) {
627
if (is_int($attrname))
628
$value[] = $attrvalues;
629
elseif ($attrvalues === true)
630
$attr .= ";$attrname"; // true means just tag, not tag=value, as in PHOTO;BASE64:...
632
foreach((array)$attrvalues as $attrvalue)
633
$attr .= ";$attrname=" . self::vcard_quote($attrvalue, ',');
641
// skip empty entries
642
if (self::is_empty($value))
645
$vcard .= self::vcard_quote($type) . $attr . ':' . self::vcard_quote($value) . self::$eol;
649
return 'BEGIN:VCARD' . self::$eol . 'VERSION:3.0' . self::$eol . $vcard . 'END:VCARD';
654
* Join indexed data array to a vcard quoted string
656
* @param array Field data
657
* @param string Separator
658
* @return string Joined and quoted string
660
private static function vcard_quote($s, $sep = ';')
663
foreach($s as $part) {
664
$r[] = self::vcard_quote($part, $sep);
666
return(implode($sep, (array)$r));
669
return strtr($s, array('\\' => '\\\\', "\r" => '', "\n" => '\n', ',' => '\,', ';' => '\;'));
675
* Split quoted string
677
* @param string vCard string to split
678
* @param string Separator char/string
679
* @return array List with splitted values
681
private static function vcard_unquote($s, $sep = ';')
683
// break string into parts separated by $sep, but leave escaped $sep alone
684
if (count($parts = explode($sep, strtr($s, array("\\$sep" => "\007")))) > 1) {
685
foreach($parts as $s) {
686
$result[] = self::vcard_unquote(strtr($s, array("\007" => "\\$sep")), $sep);
691
return strtr($s, array("\r" => '', '\\\\' => '\\', '\n' => "\n", '\N' => "\n", '\,' => ',', '\;' => ';', '\:' => ':'));
697
* Check if vCard entry is empty: empty string or an array with
700
* @param mixed $value Attribute value (string or array)
702
* @return bool True if the value is empty, False otherwise
704
private static function is_empty($value)
706
foreach ((array)$value as $v) {
707
if (((string)$v) !== '') {
716
* Extract array values by a filter
718
* @param array Array to filter
719
* @param keys Array or comma separated list of values to keep
720
* @param boolean Invert key selection: remove the listed values
721
* @return array The filtered array
723
private static function array_filter($arr, $values, $inverse = false)
725
if (!is_array($values))
726
$values = explode(',', $values);
729
$keep = array_flip((array)$values);
730
foreach ($arr as $key => $val)
731
if ($inverse != isset($keep[strtolower($val)]))
732
$result[$key] = $val;
738
* Returns UNICODE type based on BOM (Byte Order Mark)
740
* @param string Input string to test
741
* @return string Detected encoding
743
private static function detect_encoding($string)
745
if (substr($string, 0, 4) == "\0\0\xFE\xFF") return 'UTF-32BE'; // Big Endian
746
if (substr($string, 0, 4) == "\xFF\xFE\0\0") return 'UTF-32LE'; // Little Endian
747
if (substr($string, 0, 2) == "\xFE\xFF") return 'UTF-16BE'; // Big Endian
748
if (substr($string, 0, 2) == "\xFF\xFE") return 'UTF-16LE'; // Little Endian
749
if (substr($string, 0, 3) == "\xEF\xBB\xBF") return 'UTF-8';
752
if ($string[0] == "\0" && $string[1] == "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-32BE';
753
if ($string[0] != "\0" && $string[1] == "\0" && $string[2] == "\0" && $string[3] == "\0") return 'UTF-32LE';
754
if ($string[0] == "\0" && $string[1] != "\0" && $string[2] == "\0" && $string[3] != "\0") return 'UTF-16BE';
755
if ($string[0] != "\0" && $string[1] == "\0" && $string[2] != "\0" && $string[3] == "\0") return 'UTF-16LE';
757
// use mb_detect_encoding()
758
$encodings = array('UTF-8', 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3',
759
'ISO-8859-4', 'ISO-8859-5', 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9',
760
'ISO-8859-10', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
761
'WINDOWS-1252', 'WINDOWS-1251', 'BIG5', 'GB2312');
763
if (function_exists('mb_detect_encoding') && ($enc = mb_detect_encoding($string, $encodings)))
766
// No match, check for UTF-8
767
// from http://w3.org/International/questions/qa-forms-utf-8.html
769
[\x09\x0A\x0D\x20-\x7E]
770
| [\xC2-\xDF][\x80-\xBF]
771
| \xE0[\xA0-\xBF][\x80-\xBF]
772
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}
773
| \xED[\x80-\x9F][\x80-\xBF]
774
| \xF0[\x90-\xBF][\x80-\xBF]{2}
775
| [\xF1-\xF3][\x80-\xBF]{3}
776
| \xF4[\x80-\x8F][\x80-\xBF]{2}
777
)*\z/xs', substr($string, 0, 2048)))
780
return rcmail::get_instance()->config->get('default_charset', 'ISO-8859-1'); # fallback to Latin-1