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