Aleksander Machniak
2016-03-15 348ec70f1293e95544469c6e6d08d125ab21fcf2
commit | author | age
922a1f 1 <?php
AM 2
a95874 3 /**
922a1f 4  +-----------------------------------------------------------------------+
AM 5  | This file is part of the Roundcube Webmail client                     |
6  | Copyright (C) 2008-2012, The Roundcube Dev Team                       |
7  |                                                                       |
8  | Licensed under the GNU General Public License version 3 or            |
9  | any later version with exceptions for skins & plugins.                |
10  | See the README file for a full license statement.                     |
11  |                                                                       |
12  | PURPOSE:                                                              |
13  |   Logical representation of a vcard address record                    |
14  +-----------------------------------------------------------------------+
15  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
16  | Author: Aleksander Machniak <alec@alec.pl>                            |
17  +-----------------------------------------------------------------------+
18 */
19
20 /**
21  * Logical representation of a vcard-based address record
22  * Provides functions to parse and export vCard data format
23  *
24  * @package    Framework
25  * @subpackage Addressbook
26  */
27 class rcube_vcard
28 {
8cacec 29     private static $values_decoded = false;
AM 30     private $raw = array(
31         'FN' => array(),
32         'N'  => array(array('','','','','')),
33     );
34     private static $fieldmap = array(
35         'phone'    => 'TEL',
36         'birthday' => 'BDAY',
37         'website'  => 'URL',
38         'notes'    => 'NOTE',
39         'email'    => 'EMAIL',
40         'address'  => 'ADR',
41         'jobtitle' => 'TITLE',
42         'department'  => 'X-DEPARTMENT',
43         'gender'      => 'X-GENDER',
44         'maidenname'  => 'X-MAIDENNAME',
45         'anniversary' => 'X-ANNIVERSARY',
46         'assistant'   => 'X-ASSISTANT',
47         'manager'     => 'X-MANAGER',
48         'spouse'      => 'X-SPOUSE',
49         'edit'        => 'X-AB-EDIT',
79367a 50         'groups'      => 'CATEGORIES',
8cacec 51     );
AM 52     private $typemap = array(
53         'IPHONE'   => 'mobile',
54         'CELL'     => 'mobile',
55         'WORK,FAX' => 'workfax',
56     );
57     private $phonetypemap = array(
58         'HOME1'       => 'HOME',
59         'BUSINESS1'   => 'WORK',
60         'BUSINESS2'   => 'WORK2',
61         'BUSINESSFAX' => 'WORK,FAX',
62         'MOBILE'      => 'CELL',
63     );
64     private $addresstypemap = array(
65         'BUSINESS' => 'WORK',
66     );
67     private $immap = array(
68         'X-JABBER' => 'jabber',
69         'X-ICQ'    => 'icq',
70         'X-MSN'    => 'msn',
71         'X-AIM'    => 'aim',
72         'X-YAHOO'  => 'yahoo',
73         'X-SKYPE'  => 'skype',
74         'X-SKYPE-USERNAME' => 'skype',
75     );
922a1f 76
8cacec 77     public $business = false;
AM 78     public $displayname;
79     public $surname;
80     public $firstname;
81     public $middlename;
82     public $nickname;
83     public $organization;
84     public $email = array();
922a1f 85
8cacec 86     public static $eol = "\r\n";
922a1f 87
AM 88
8cacec 89     /**
AM 90      * Constructor
91      */
92     public function __construct($vcard = null, $charset = RCUBE_CHARSET, $detect = false, $fieldmap = array())
93     {
c027ba 94         if (!empty($fieldmap)) {
8cacec 95             $this->extend_fieldmap($fieldmap);
AM 96         }
922a1f 97
8cacec 98         if (!empty($vcard)) {
AM 99             $this->load($vcard, $charset, $detect);
100         }
922a1f 101     }
AM 102
8cacec 103     /**
AM 104      * Load record from (internal, unfolded) vcard 3.0 format
105      *
106      * @param string vCard string to parse
107      * @param string Charset of string values
108      * @param boolean True if loading a 'foreign' vcard and extra heuristics for charset detection is required
109      */
110     public function load($vcard, $charset = RCUBE_CHARSET, $detect = false)
111     {
112         self::$values_decoded = false;
fcb7d4 113         $this->raw = self::vcard_decode(self::cleanup($vcard));
922a1f 114
8cacec 115         // resolve charset parameters
AM 116         if ($charset == null) {
117             $this->raw = self::charset_convert($this->raw);
118         }
119         // vcard has encoded values and charset should be detected
120         else if ($detect && self::$values_decoded
121             && ($detected_charset = self::detect_encoding(self::vcard_encode($this->raw)))
122             && $detected_charset != RCUBE_CHARSET
123         ) {
124             $this->raw = self::charset_convert($this->raw, $detected_charset);
125         }
922a1f 126
8cacec 127         // find well-known address fields
AM 128         $this->displayname  = $this->raw['FN'][0][0];
129         $this->surname      = $this->raw['N'][0][0];
130         $this->firstname    = $this->raw['N'][0][1];
131         $this->middlename   = $this->raw['N'][0][2];
132         $this->nickname     = $this->raw['NICKNAME'][0][0];
133         $this->organization = $this->raw['ORG'][0][0];
134         $this->business     = ($this->raw['X-ABSHOWAS'][0][0] == 'COMPANY') || (join('', (array)$this->raw['N'][0]) == '' && !empty($this->organization));
922a1f 135
8cacec 136         foreach ((array)$this->raw['EMAIL'] as $i => $raw_email) {
AM 137             $this->email[$i] = is_array($raw_email) ? $raw_email[0] : $raw_email;
138         }
922a1f 139
8cacec 140         // make the pref e-mail address the first entry in $this->email
AM 141         $pref_index = $this->get_type_index('EMAIL', 'pref');
142         if ($pref_index > 0) {
143             $tmp = $this->email[0];
144             $this->email[0] = $this->email[$pref_index];
145             $this->email[$pref_index] = $tmp;
146         }
f0405b 147
TB 148         // fix broken vcards from Outlook that only supply ORG but not the required N or FN properties
149         if (!strlen(trim($this->displayname . $this->surname . $this->firstname)) && strlen($this->organization)) {
150             $this->displayname = $this->organization;
151         }
922a1f 152     }
AM 153
8cacec 154     /**
AM 155      * Return vCard data as associative array to be unsed in Roundcube address books
156      *
157      * @return array Hash array with key-value pairs
158      */
159     public function get_assoc()
160     {
161         $out     = array('name' => $this->displayname);
162         $typemap = $this->typemap;
922a1f 163
8cacec 164         // copy name fields to output array
AM 165         foreach (array('firstname','surname','middlename','nickname','organization') as $col) {
166             if (strlen($this->$col)) {
167                 $out[$col] = $this->$col;
922a1f 168             }
8cacec 169         }
AM 170
171         if ($this->raw['N'][0][3])
172             $out['prefix'] = $this->raw['N'][0][3];
173         if ($this->raw['N'][0][4])
174             $out['suffix'] = $this->raw['N'][0][4];
175
176         // convert from raw vcard data into associative data for Roundcube
177         foreach (array_flip(self::$fieldmap) as $tag => $col) {
178             foreach ((array)$this->raw[$tag] as $i => $raw) {
179                 if (is_array($raw)) {
180                     $k       = -1;
181                     $key     = $col;
182                     $subtype = '';
183
184                     if (!empty($raw['type'])) {
185                         $combined = join(',', self::array_filter((array)$raw['type'], 'internet,pref', true));
186                         $combined = strtoupper($combined);
187
188                         if ($typemap[$combined]) {
189                             $subtype = $typemap[$combined];
190                         }
191                         else if ($typemap[$raw['type'][++$k]]) {
192                             $subtype = $typemap[$raw['type'][$k]];
193                         }
194                         else {
195                             $subtype = strtolower($raw['type'][$k]);
196                         }
197
198                         while ($k < count($raw['type']) && ($subtype == 'internet' || $subtype == 'pref')) {
7e3298 199                             $subtype = $typemap[$raw['type'][++$k]] ?: strtolower($raw['type'][$k]);
8cacec 200                         }
AM 201                     }
202
203                     // read vcard 2.1 subtype
204                     if (!$subtype) {
205                         foreach ($raw as $k => $v) {
206                             if (!is_numeric($k) && $v === true && ($k = strtolower($k))
207                                 && !in_array($k, array('pref','internet','voice','base64'))
208                             ) {
209                                 $k_uc    = strtoupper($k);
7e3298 210                                 $subtype = $typemap[$k_uc] ?: $k;
8cacec 211                                 break;
AM 212                             }
213                         }
214                     }
215
216                     // force subtype if none set
217                     if (!$subtype && preg_match('/^(email|phone|address|website)/', $key)) {
218                         $subtype = 'other';
219                     }
220
221                     if ($subtype) {
222                         $key .= ':' . $subtype;
223                     }
224
225                     // split ADR values into assoc array
226                     if ($tag == 'ADR') {
227                         list(,, $value['street'], $value['locality'], $value['region'], $value['zipcode'], $value['country']) = $raw;
228                         $out[$key][] = $value;
229                     }
230                     else {
231                         $out[$key][] = $raw[0];
232                     }
233                 }
234                 else {
235                     $out[$col][] = $raw;
236                 }
237             }
238         }
239
240         // handle special IM fields as used by Apple
241         foreach ($this->immap as $tag => $type) {
242             foreach ((array)$this->raw[$tag] as $i => $raw) {
243                 $out['im:'.$type][] = $raw[0];
244             }
245         }
246
247         // copy photo data
248         if ($this->raw['PHOTO']) {
249             $out['photo'] = $this->raw['PHOTO'][0][0];
250         }
251
252         return $out;
253     }
254
255     /**
256      * Convert the data structure into a vcard 3.0 string
257      */
258     public function export($folded = true)
259     {
260         $vcard = self::vcard_encode($this->raw);
261         return $folded ? self::rfc2425_fold($vcard) : $vcard;
262     }
263
264     /**
265      * Clear the given fields in the loaded vcard data
266      *
267      * @param array List of field names to be reset
268      */
269     public function reset($fields = null)
270     {
271         if (!$fields) {
272             $fields = array_merge(array_values(self::$fieldmap), array_keys($this->immap),
273                 array('FN','N','ORG','NICKNAME','EMAIL','ADR','BDAY'));
274         }
275
276         foreach ($fields as $f) {
277             unset($this->raw[$f]);
278         }
279
280         if (!$this->raw['N']) {
281             $this->raw['N'] = array(array('','','','',''));
282         }
283         if (!$this->raw['FN']) {
284             $this->raw['FN'] = array();
285         }
286
287         $this->email = array();
288     }
289
290     /**
291      * Setter for address record fields
292      *
293      * @param string Field name
294      * @param string Field value
295      * @param string Type/section name
296      */
297     public function set($field, $value, $type = 'HOME')
298     {
299         $field   = strtolower($field);
300         $type_uc = strtoupper($type);
301
302         switch ($field) {
303         case 'name':
304         case 'displayname':
305             $this->raw['FN'][0][0] = $this->displayname = $value;
306             break;
307
308         case 'surname':
309             $this->raw['N'][0][0] = $this->surname = $value;
310             break;
311
312         case 'firstname':
313             $this->raw['N'][0][1] = $this->firstname = $value;
314             break;
315
316         case 'middlename':
317             $this->raw['N'][0][2] = $this->middlename = $value;
318             break;
319
320         case 'prefix':
321             $this->raw['N'][0][3] = $value;
322             break;
323
324         case 'suffix':
325             $this->raw['N'][0][4] = $value;
326             break;
327
328         case 'nickname':
329             $this->raw['NICKNAME'][0][0] = $this->nickname = $value;
330             break;
331
332         case 'organization':
333             $this->raw['ORG'][0][0] = $this->organization = $value;
334             break;
335
336         case 'photo':
337             if (strpos($value, 'http:') === 0) {
338                 // TODO: fetch file from URL and save it locally?
339                 $this->raw['PHOTO'][0] = array(0 => $value, 'url' => true);
922a1f 340             }
AM 341             else {
8cacec 342                 $this->raw['PHOTO'][0] = array(0 => $value, 'base64' => (bool) preg_match('![^a-z0-9/=+-]!i', $value));
AM 343             }
344             break;
345
346         case 'email':
347             $this->raw['EMAIL'][] = array(0 => $value, 'type' => array_filter(array('INTERNET', $type_uc)));
348             $this->email[] = $value;
349             break;
350
351         case 'im':
352             // save IM subtypes into extension fields
353             $typemap = array_flip($this->immap);
354             if ($field = $typemap[strtolower($type)]) {
355                 $this->raw[$field][] = array(0 => $value);
356             }
357             break;
358
359         case 'birthday':
360         case 'anniversary':
52830e 361             if (($val = rcube_utils::anytodatetime($value)) && ($fn = self::$fieldmap[$field])) {
TB 362                 $this->raw[$fn][] = array(0 => $val->format('Y-m-d'), 'value' => array('date'));
8cacec 363             }
AM 364             break;
365
366         case 'address':
367             if ($this->addresstypemap[$type_uc]) {
368                 $type = $this->addresstypemap[$type_uc];
922a1f 369             }
AM 370
8cacec 371             $value = $value[0] ? $value : array('', '', $value['street'], $value['locality'], $value['region'], $value['zipcode'], $value['country']);
922a1f 372
8cacec 373             // fall through if not empty
AM 374             if (!strlen(join('', $value))) {
922a1f 375                 break;
AM 376             }
377
8cacec 378         default:
AM 379             if ($field == 'phone' && $this->phonetypemap[$type_uc]) {
380                 $type = $this->phonetypemap[$type_uc];
5983ee 381             }
922a1f 382
8cacec 383             if (($tag = self::$fieldmap[$field]) && (is_array($value) || strlen($value))) {
AM 384                 $index = count($this->raw[$tag]);
385                 $this->raw[$tag][$index] = (array)$value;
386                 if ($type) {
387                     $typemap = array_flip($this->typemap);
7e3298 388                     $this->raw[$tag][$index]['type'] = explode(',', $typemap[$type_uc] ?: $type);
8cacec 389                 }
AM 390             }
bd8252 391             else {
AM 392                 unset($this->raw[$tag]);
393             }
394
8cacec 395             break;
922a1f 396         }
AM 397     }
398
8cacec 399     /**
AM 400      * Setter for individual vcard properties
401      *
402      * @param string VCard tag name
403      * @param array Value-set of this vcard property
404      * @param boolean Set to true if the value-set should be appended instead of replacing any existing value-set
405      */
406     public function set_raw($tag, $value, $append = false)
407     {
408         $index = $append ? count($this->raw[$tag]) : 0;
409         $this->raw[$tag][$index] = (array)$value;
922a1f 410     }
AM 411
8cacec 412     /**
AM 413      * Find index with the '$type' attribute
414      *
415      * @param string Field name
9e4246 416      *
8cacec 417      * @return int Field index having $type set
AM 418      */
9e4246 419     private function get_type_index($field)
8cacec 420     {
AM 421         $result = 0;
422         if ($this->raw[$field]) {
423             foreach ($this->raw[$field] as $i => $data) {
424                 if (is_array($data['type']) && in_array_nocase('pref', $data['type'])) {
425                     $result = $i;
426                 }
427             }
922a1f 428         }
8cacec 429
AM 430         return $result;
431     }
432
433     /**
434      * Convert a whole vcard (array) to UTF-8.
435      * If $force_charset is null, each member value that has a charset parameter will be converted
436      */
437     private static function charset_convert($card, $force_charset = null)
438     {
439         foreach ($card as $key => $node) {
440             foreach ($node as $i => $subnode) {
441                 if (is_array($subnode) && (($charset = $force_charset) || ($subnode['charset'] && ($charset = $subnode['charset'][0])))) {
442                     foreach ($subnode as $j => $value) {
443                         if (is_numeric($j) && is_string($value)) {
444                             $card[$key][$i][$j] = rcube_charset::convert($value, $charset);
445                         }
446                     }
447                     unset($card[$key][$i]['charset']);
448                 }
449             }
922a1f 450         }
AM 451
8cacec 452         return $card;
AM 453     }
922a1f 454
8cacec 455     /**
AM 456      * Extends fieldmap definition
457      */
458     public function extend_fieldmap($map)
459     {
460         if (is_array($map)) {
461             self::$fieldmap = array_merge($map, self::$fieldmap);
922a1f 462         }
AM 463     }
464
8cacec 465     /**
AM 466      * Factory method to import a vcard file
467      *
468      * @param string vCard file content
469      *
470      * @return array List of rcube_vcard objects
471      */
472     public static function import($data)
473     {
474         $out = array();
922a1f 475
8cacec 476         // check if charsets are specified (usually vcard version < 3.0 but this is not reliable)
AM 477         if (preg_match('/charset=/i', substr($data, 0, 2048))) {
478             $charset = null;
922a1f 479         }
8cacec 480         // detect charset and convert to utf-8
AM 481         else if (($charset = self::detect_encoding($data)) && $charset != RCUBE_CHARSET) {
482             $data = rcube_charset::convert($data, $charset);
483             $data = preg_replace(array('/^[\xFE\xFF]{2}/', '/^\xEF\xBB\xBF/', '/^\x00+/'), '', $data); // also remove BOM
484             $charset = RCUBE_CHARSET;
485         }
922a1f 486
8cacec 487         $vcard_block    = '';
922a1f 488         $in_vcard_block = false;
AM 489
3725cf 490         foreach (preg_split("/[\r\n]+/", $data) as $line) {
8cacec 491             if ($in_vcard_block && !empty($line)) {
AM 492                 $vcard_block .= $line . "\n";
922a1f 493             }
8cacec 494
AM 495             $line = trim($line);
496
497             if (preg_match('/^END:VCARD$/i', $line)) {
498                 // parse vcard
fcb7d4 499                 $obj = new rcube_vcard($vcard_block, $charset, true, self::$fieldmap);
38c195 500                 // FN and N is required by vCard format (RFC 2426)
AM 501                 // on import we can be less restrictive, let's addressbook decide
502                 if (!empty($obj->displayname) || !empty($obj->surname) || !empty($obj->firstname) || !empty($obj->email)) {
8cacec 503                     $out[] = $obj;
AM 504                 }
505
506                 $in_vcard_block = false;
922a1f 507             }
8cacec 508             else if (preg_match('/^BEGIN:VCARD$/i', $line)) {
AM 509                 $vcard_block    = $line . "\n";
510                 $in_vcard_block = true;
511             }
922a1f 512         }
AM 513
8cacec 514         return $out;
922a1f 515     }
AM 516
8cacec 517     /**
AM 518      * Normalize vcard data for better parsing
519      *
520      * @param string vCard block
521      *
522      * @return string Cleaned vcard block
523      */
196114 524     public static function cleanup($vcard)
8cacec 525     {
AM 526         // convert Apple X-ABRELATEDNAMES into X-* fields for better compatibility
527         $vcard = preg_replace_callback(
528             '/item(\d+)\.(X-ABRELATEDNAMES)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./s',
529             array('self', 'x_abrelatednames_callback'),
530             $vcard);
922a1f 531
99d596 532         // Cleanup
AM 533         $vcard = preg_replace(array(
534                 // convert special types (like Skype) to normal type='skype' classes with this simple regex ;)
fcb7d4 535                 '/item(\d+)\.(TEL|EMAIL|URL)([^:]*?):(.*?)item\1.X-ABLabel:(?:_\$!<)?([\w-() ]*)(?:>!\$_)?./si',
AM 536                 '/^item\d*\.X-AB.*$/mi',  // remove cruft like item1.X-AB*
537                 '/^item\d*\./mi',         // remove item1.ADR instead of ADR
99d596 538                 '/\n+/',                 // remove empty lines
AM 539                 '/^(N:[^;\R]*)$/m',      // if N doesn't have any semicolons, add some
540             ),
541             array(
542                 '\2;type=\5\3:\4',
543                 '',
544                 '',
545                 "\n",
546                 '\1;;;;',
547             ), $vcard);
922a1f 548
8cacec 549         // convert X-WAB-GENDER to X-GENDER
AM 550         if (preg_match('/X-WAB-GENDER:(\d)/', $vcard, $matches)) {
551             $value = $matches[1] == '2' ? 'male' : 'female';
552             $vcard = preg_replace('/X-WAB-GENDER:\d/', 'X-GENDER:' . $value, $vcard);
553         }
554
555         return $vcard;
922a1f 556     }
AM 557
8cacec 558     private static function x_abrelatednames_callback($matches)
AM 559     {
560         return 'X-' . strtoupper($matches[5]) . $matches[3] . ':'. $matches[4];
561     }
922a1f 562
8cacec 563     private static function rfc2425_fold_callback($matches)
AM 564     {
565         // chunk_split string and avoid lines breaking multibyte characters
566         $c = 71;
567         $out .= substr($matches[1], 0, $c);
568         for ($n = $c; $c < strlen($matches[1]); $c++) {
569             // break if length > 75 or mutlibyte character starts after position 71
570             if ($n > 75 || ($n > 71 && ord($matches[1][$c]) >> 6 == 3)) {
571                 $out .= "\r\n ";
572                 $n = 0;
922a1f 573             }
8cacec 574             $out .= $matches[1][$c];
AM 575             $n++;
922a1f 576         }
AM 577
8cacec 578         return $out;
922a1f 579     }
AM 580
8cacec 581     public static function rfc2425_fold($val)
AM 582     {
583         return preg_replace_callback('/([^\n]{72,})/', array('self', 'rfc2425_fold_callback'), $val);
922a1f 584     }
AM 585
8cacec 586     /**
AM 587      * Decodes a vcard block (vcard 3.0 format, unfolded)
588      * into an array structure
589      *
590      * @param string vCard block to parse
591      *
592      * @return array Raw data structure
593      */
594     private static function vcard_decode($vcard)
595     {
596         // Perform RFC2425 line unfolding and split lines
b231c8 597         $vcard  = preg_replace(array("/\r/", "/\n\s+/"), '', $vcard);
AM 598         $lines  = explode("\n", $vcard);
599         $result = array();
922a1f 600
8cacec 601         for ($i=0; $i < count($lines); $i++) {
b231c8 602             if (!($pos = strpos($lines[$i], ':'))) {
8cacec 603                 continue;
b231c8 604             }
922a1f 605
b231c8 606             $prefix = substr($lines[$i], 0, $pos);
AM 607             $data   = substr($lines[$i], $pos+1);
608
609             if (preg_match('/^(BEGIN|END)$/i', $prefix)) {
8cacec 610                 continue;
b231c8 611             }
922a1f 612
8cacec 613             // convert 2.1-style "EMAIL;internet;home:" to 3.0-style "EMAIL;TYPE=internet;TYPE=home:"
b231c8 614             if ($result['VERSION'][0] == "2.1"
AM 615                 && preg_match('/^([^;]+);([^:]+)/', $prefix, $regs2)
8cacec 616                 && !preg_match('/^TYPE=/i', $regs2[2])
AM 617             ) {
b231c8 618                 $prefix = $regs2[1];
8cacec 619                 foreach (explode(';', $regs2[2]) as $prop) {
b231c8 620                     $prefix .= ';' . (strpos($prop, '=') ? $prop : 'TYPE='.$prop);
8cacec 621                 }
AM 622             }
922a1f 623
b231c8 624             if (preg_match_all('/([^\\;]+);?/', $prefix, $regs2)) {
8cacec 625                 $entry = array();
AM 626                 $field = strtoupper($regs2[1][0]);
627                 $enc   = null;
922a1f 628
8cacec 629                 foreach($regs2[1] as $attrid => $attr) {
a649e0 630                     $attr = preg_replace('/[\s\t\n\r\0\x0B]/', '', $attr);
8cacec 631                     if ((list($key, $value) = explode('=', $attr)) && $value) {
AM 632                         if ($key == 'ENCODING') {
633                             $value = strtoupper($value);
634                             // add next line(s) to value string if QP line end detected
635                             if ($value == 'QUOTED-PRINTABLE') {
636                                 while (preg_match('/=$/', $lines[$i])) {
b231c8 637                                     $data .= "\n" . $lines[++$i];
8cacec 638                                 }
AM 639                             }
b231c8 640                             $enc = $value == 'BASE64' ? 'B' : $value;
8cacec 641                         }
AM 642                         else {
643                             $lc_key = strtolower($key);
644                             $entry[$lc_key] = array_merge((array)$entry[$lc_key], (array)self::vcard_unquote($value, ','));
645                         }
646                     }
647                     else if ($attrid > 0) {
648                         $entry[strtolower($key)] = true;  // true means attr without =value
649                     }
650                 }
922a1f 651
8cacec 652                 // decode value
AM 653                 if ($enc || !empty($entry['base64'])) {
654                     // save encoding type (#1488432)
655                     if ($enc == 'B') {
656                         $entry['encoding'] = 'B';
657                         // should we use vCard 3.0 instead?
658                         // $entry['base64'] = true;
659                     }
b231c8 660
7e3298 661                     $data = self::decode_value($data, $enc ?: 'base64');
b231c8 662                 }
AM 663                 else if ($field == 'PHOTO') {
664                     // vCard 4.0 data URI, "PHOTO:data:image/jpeg;base64,..."
665                     if (preg_match('/^data:[a-z\/_-]+;base64,/i', $data, $m)) {
666                         $entry['encoding'] = $enc = 'B';
667                         $data = substr($data, strlen($m[0]));
668                         $data = self::decode_value($data, 'base64');
669                     }
8cacec 670                 }
AM 671
672                 if ($enc != 'B' && empty($entry['base64'])) {
b231c8 673                     $data = self::vcard_unquote($data);
8cacec 674                 }
AM 675
b231c8 676                 $entry = array_merge($entry, (array) $data);
AM 677                 $result[$field][] = $entry;
8cacec 678             }
AM 679         }
680
b231c8 681         unset($result['VERSION']);
AM 682
683         return $result;
8cacec 684     }
AM 685
686     /**
687      * Decode a given string with the encoding rule from ENCODING attributes
688      *
689      * @param string String to decode
690      * @param string Encoding type (quoted-printable and base64 supported)
691      *
692      * @return string Decoded 8bit value
693      */
694     private static function decode_value($value, $encoding)
695     {
696         switch (strtolower($encoding)) {
697         case 'quoted-printable':
698             self::$values_decoded = true;
699             return quoted_printable_decode($value);
700
701         case 'base64':
702         case 'b':
703             self::$values_decoded = true;
704             return base64_decode($value);
705
706         default:
707             return $value;
708         }
709     }
710
711     /**
712      * Encodes an entry for storage in our database (vcard 3.0 format, unfolded)
713      *
714      * @param array Raw data structure to encode
715      *
716      * @return string vCard encoded string
717      */
718     static function vcard_encode($data)
719     {
720         foreach ((array)$data as $type => $entries) {
721             // valid N has 5 properties
722             while ($type == "N" && is_array($entries[0]) && count($entries[0]) < 5) {
723                 $entries[0][] = "";
724             }
725
726             // make sure FN is not empty (required by RFC2426)
727             if ($type == "FN" && empty($entries)) {
728                 $entries[0] = $data['EMAIL'][0][0];
729             }
730
731             foreach ((array)$entries as $entry) {
732                 $attr = '';
733                 if (is_array($entry)) {
734                     $value = array();
735                     foreach ($entry as $attrname => $attrvalues) {
736                         if (is_int($attrname)) {
737                             if (!empty($entry['base64']) || $entry['encoding'] == 'B') {
738                                 $attrvalues = base64_encode($attrvalues);
739                             }
740                             $value[] = $attrvalues;
741                         }
742                         else if (is_bool($attrvalues)) {
428764 743                             // true means just a tag, not tag=value, as in PHOTO;BASE64:...
8cacec 744                             if ($attrvalues) {
348ec7 745                                 // vCard v3 uses ENCODING=b (#1489183)
428764 746                                 if ($attrname == 'base64') {
348ec7 747                                     $attr .= ";ENCODING=b";
428764 748                                 }
AM 749                                 else {
750                                     $attr .= strtoupper(";$attrname");
751                                 }
8cacec 752                             }
AM 753                         }
754                         else {
755                             foreach((array)$attrvalues as $attrvalue) {
756                                 $attr .= strtoupper(";$attrname=") . self::vcard_quote($attrvalue, ',');
757                             }
758                         }
759                     }
760                 }
761                 else {
762                     $value = $entry;
763                 }
764
765                 // skip empty entries
766                 if (self::is_empty($value)) {
767                     continue;
768                 }
769
770                 $vcard .= self::vcard_quote($type) . $attr . ':' . self::vcard_quote($value) . self::$eol;
771             }
772         }
773
774         return 'BEGIN:VCARD' . self::$eol . 'VERSION:3.0' . self::$eol . $vcard . 'END:VCARD';
775     }
776
777     /**
778      * Join indexed data array to a vcard quoted string
779      *
780      * @param array Field data
781      * @param string Separator
782      *
783      * @return string Joined and quoted string
784      */
79367a 785     public static function vcard_quote($s, $sep = ';')
8cacec 786     {
AM 787         if (is_array($s)) {
788             foreach($s as $part) {
789                 $r[] = self::vcard_quote($part, $sep);
790             }
791             return(implode($sep, (array)$r));
792         }
793
79367a 794         return strtr($s, array('\\' => '\\\\', "\r" => '', "\n" => '\n', $sep => '\\'.$sep));
8cacec 795     }
AM 796
797     /**
798      * Split quoted string
799      *
800      * @param string vCard string to split
801      * @param string Separator char/string
802      *
803      * @return array List with splited values
804      */
805     private static function vcard_unquote($s, $sep = ';')
806     {
21106b 807         // break string into parts separated by $sep
AM 808         if (!empty($sep)) {
809             // Handle properly backslash escaping (#1488896)
810             $rep1 = array("\\\\" => "\010", "\\$sep" => "\007");
811             $rep2 = array("\007" => "\\$sep", "\010" => "\\\\");
812
813             if (count($parts = explode($sep, strtr($s, $rep1))) > 1) {
814                 foreach ($parts as $s) {
815                     $result[] = self::vcard_unquote(strtr($s, $rep2));
816                 }
817                 return $result;
8cacec 818             }
3a0dc8 819
a649e0 820             $s = trim(strtr($s, $rep2));
8cacec 821         }
AM 822
3a0dc8 823         // some implementations (GMail) use non-standard backslash before colon (#1489085)
AM 824         // we will handle properly any backslashed character - removing dummy backslahes
825         // return strtr($s, array("\r" => '', '\\\\' => '\\', '\n' => "\n", '\N' => "\n", '\,' => ',', '\;' => ';'));
826
827         $s   = str_replace("\r", '', $s);
828         $pos = 0;
829
830         while (($pos = strpos($s, '\\', $pos)) !== false) {
831             $next = substr($s, $pos + 1, 1);
832             if ($next == 'n' || $next == 'N') {
833                 $s = substr_replace($s, "\n", $pos, 2);
834             }
835             else {
836                 $s = substr_replace($s, '', $pos, 1);
837             }
838
839             $pos += 1;
840         }
841
842         return $s;
8cacec 843     }
AM 844
845     /**
846      * Check if vCard entry is empty: empty string or an array with
847      * all entries empty.
848      *
849      * @param mixed $value Attribute value (string or array)
850      *
851      * @return bool True if the value is empty, False otherwise
852      */
853     private static function is_empty($value)
854     {
855         foreach ((array)$value as $v) {
856             if (((string)$v) !== '') {
857                 return false;
858             }
859         }
860
861         return true;
862     }
863
864     /**
865      * Extract array values by a filter
866      *
867      * @param array Array to filter
868      * @param keys Array or comma separated list of values to keep
869      * @param boolean Invert key selection: remove the listed values
870      *
871      * @return array The filtered array
872      */
873     private static function array_filter($arr, $values, $inverse = false)
874     {
875         if (!is_array($values)) {
876             $values = explode(',', $values);
877         }
878
879         $result = array();
880         $keep   = array_flip((array)$values);
881
882         foreach ($arr as $key => $val) {
883             if ($inverse != isset($keep[strtolower($val)])) {
884                 $result[$key] = $val;
885             }
886         }
887
888         return $result;
889     }
890
891     /**
892      * Returns UNICODE type based on BOM (Byte Order Mark)
893      *
894      * @param string Input string to test
895      *
896      * @return string Detected encoding
897      */
898     private static function detect_encoding($string)
899     {
900         $fallback = rcube::get_instance()->config->get('default_charset', 'ISO-8859-1'); // fallback to Latin-1
901
902         return rcube_charset::detect($string, $fallback);
903     }
922a1f 904 }