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