dolibarr 25.0.0-alpha
vcard.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) Kai Blankenhorn <kaib@bitfolge.de>
3 * Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.org>
4 * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
5 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
35function encode($string)
36{
37 return str_replace(array("\\", ";", ",", "\r\n", "\r", "\n"), array("\\\\", "\\;", "\\,", "\\n", "\\n", "\\n"), (string) $string);
38}
39
40
49function dol_quoted_printable_encode($input, $line_max = 76)
50{
51 $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
52 $lines = preg_split("/(?:\r\n|\r|\n)/", $input);
53 $eol = "\r\n";
54 $linebreak = "=0D=0A";
55 $escape = "=";
56 $output = "";
57
58 $num = count($lines);
59 for ($j = 0; $j < $num; $j++) {
60 $line = $lines[$j];
61 $linlen = strlen($line);
62 $newline = "";
63 for ($i = 0; $i < $linlen; $i++) {
64 $c = substr($line, $i, 1);
65 $dec = ord($c);
66 if (($dec == 32) && ($i == ($linlen - 1))) { // convert space at eol only
67 $c = "=20";
68 } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { // always encode "\t", which is *not* required
69 $h2 = floor($dec / 16);
70 $h1 = floor($dec % 16);
71 $c = $escape.$hex[(int) $h2].$hex[(int) $h1];
72 }
73 if ((strlen($newline) + strlen($c)) >= $line_max) { // CRLF is not counted
74 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
75 $newline = " ";
76 }
77 $newline .= $c;
78 } // end of for
79 $output .= $newline;
80 if ($j < count($lines) - 1) {
81 $output .= $linebreak;
82 }
83 }
84 return trim($output);
85}
86
87
91class vCard
92{
96 public $properties = [];
97
101 public $filename;
102
107 public $encoding = "CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE";
108
109
116 private function cleanValue($value)
117 {
118 return trim((string) preg_replace('/[\x00-\x1F\x7F]+/', ' ', (string) $value));
119 }
120
127 private function normalizeType($type)
128 {
129 $params = array();
130 $types = array();
131 foreach (explode(';', $this->cleanValue($type)) as $param) {
132 $param = trim($param);
133 if ($param === '') {
134 continue;
135 }
136 if (stripos($param, 'TYPE=') === 0) {
137 $types[] = substr($param, 5);
138 } elseif (strpos($param, '=') === false) {
139 $types[] = $param;
140 } else {
141 $params[] = $param;
142 }
143 }
144 if ($types) {
145 array_unshift($params, 'TYPE='.implode(',', $types));
146 }
147 return implode(';', $params);
148 }
149
157 public function setPhoneNumber($number, $type = "")
158 {
159 // type may be PREF | WORK | HOME | VOICE | FAX | MSG | CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO or any senseful combination, e.g. "PREF;WORK;VOICE"
160 $number = $this->cleanValue($number);
161 if ($number === '') {
162 return;
163 }
164 $key = "TEL";
165 if ($type != "") {
166 $key .= ";".$this->normalizeType($type);
167 }
168 $key .= ";VALUE=uri";
169 // A tel URI has no space and no separator other than '-'
170 $this->properties[$key] = 'tel:'.preg_replace('/[\s.()\/]+/', '', $number);
171 }
172
181 public function setPhoto($type, $photo)
182 {
183 // $type = "GIF" | "JPEG"
184 $photo = $this->cleanValue($photo);
185 if ($photo === '') {
186 return;
187 }
188 //$this->properties["PHOTO;MEDIATYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
189 $this->properties["PHOTO;MEDIATYPE=$type"] = $photo; // must be url of photo
190 //$this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo); // must be content of image
191 }
192
199 public function setFormattedName($name)
200 {
201 if (trim((string) $name) === '') {
202 return;
203 }
204 $this->properties["FN"] = encode($name);
205 }
206
218 public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
219 {
220 $this->properties["N"] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
221
222 $this->filename = trim("$first $family").'.vcf';
223
224 if (empty($this->properties["FN"])) {
225 $this->setFormattedName(trim((string) preg_replace('/\s+/', ' ', "$prefix $first $additional $family $suffix")));
226 }
227 }
228
235 public function setBirthday($date)
236 {
237 // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426 for vcard v3
238 // $date format is YYYYMMDD or ISO8601 for vcard v4
239 $this->properties["BDAY"] = dol_print_date($date, 'dayxcard');
240 }
241
256 public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "", $label = "")
257 {
258 // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
259 if (trim($postoffice.$extended.$street.$city.$region.$zip.$country) === '') {
260 return;
261 }
262 $key = "ADR";
263 if ($type != "") {
264 $key .= ";".$this->normalizeType($type);
265 }
266 if ($label != "") {
267 $key .= ';LABEL="'.str_replace('"', "'", $this->cleanValue($label)).'"'; // A parameter has no escape for the quote and the line break
268 }
269
270 $this->properties[$key] = encode($postoffice).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
271 }
272
287 public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME")
288 {
289 $label = "";
290 if ($postoffice != "") {
291 $label .= "$postoffice\r\n";
292 }
293 if ($extended != "") {
294 $label .= "$extended\r\n";
295 }
296 if ($street != "") {
297 $label .= "$street\r\n";
298 }
299 if ($zip != "") {
300 $label .= "$zip ";
301 }
302 if ($city != "") {
303 $label .= "$city\r\n";
304 }
305 if ($region != "") {
306 $label .= "$region\r\n";
307 }
308 if ($country != "") {
309 $label .= "$country\r\n";
310 }
311
312 $this->properties["LABEL;".$this->normalizeType($type)] = encode($label);
313 }
314
322 public function setEmail($address, $type = "")
323 {
324 $address = $this->cleanValue($address);
325 if ($address === '') {
326 return;
327 }
328 $key = "EMAIL";
329 if ($type === "PREF") {
330 $key .= ";PREF=1";
331 } elseif (!empty($type)) {
332 if (stripos($type, 'TYPE=') === 0) {
333 $key .= ";".$this->normalizeType($type);
334 } else {
335 $key .= ";TYPE=".dol_strtolower($type);
336 }
337 }
338 $this->properties[$key] = $address;
339 }
340
347 public function setNote($note)
348 {
349 if (trim((string) $note) === '') {
350 return;
351 }
352 $this->properties["NOTE"] = encode($note);
353 }
354
361 public function setTitle($title)
362 {
363 if (trim((string) $title) === '') {
364 return;
365 }
366 $this->properties["TITLE"] = encode($title);
367 }
368
369
376 public function setOrg($org)
377 {
378 if (trim((string) $org) === '') {
379 return;
380 }
381 $this->properties["ORG"] = encode($org);
382 }
383
384
391 public function setProdId($prodid)
392 {
393 $this->properties["PRODID"] = encode($prodid);
394 }
395
396
403 public function setUID($uid)
404 {
405 $this->properties["UID"] = encode($uid);
406 }
407
408
416 public function setURL($url, $type = "")
417 {
418 // $type may be WORK | HOME
419 $url = $this->cleanValue($url);
420 if ($url === '') {
421 return;
422 }
423 $key = "URL";
424 if ($type != "") {
425 $key .= ";".$this->normalizeType($type);
426 }
427 $this->properties[$key] = $url;
428 }
429
435 public function getVCard()
436 {
437 $text = "BEGIN:VCARD\r\n";
438 $text .= "VERSION:4.0\r\n"; // With V4, all encoding are UTF-8
439 //$text.= "VERSION:2.1\r\n";
440 foreach ($this->properties as $key => $value) {
441 $newkey = $key;
442 if (strpos($key, 'SOCIALPROFILE;') === 0) {
443 $newkey = preg_replace('/-[A-Za-z0-9_]*$/', '', $key); // remove suffix -twitter, -facebook, ... (only there to have a different key for each network)
444 }
445 $text .= $this->foldLine($newkey.":".$value);
446 }
447 $text .= "REV:".dol_print_date(dol_now(), 'dayhourxcard', 'gmt')."\r\n";
448 //$text .= "MAILER: Dolibarr\r\n";
449 $text .= "END:VCARD\r\n";
450
451 return $text;
452 }
453
461 private function foldLine($line)
462 {
463 $result = '';
464 $max = 75;
465 while (strlen($line) > $max) {
466 $cut = $max;
467 while ($cut > 0 && (ord($line[$cut]) & 0xC0) == 0x80) { // Do not cut in the middle of a UTF-8 character
468 $cut--;
469 }
470 $result .= substr($line, 0, $cut)."\r\n";
471 $line = ' '.substr($line, $cut);
472 }
473 return $result.$line."\r\n";
474 }
475
481 public function getFileName()
482 {
483 $filename = trim(dol_sanitizeFileName(trim((string) $this->filename)));
484 if ($filename === '' || $filename === '.vcf') {
485 $filename = 'vcard.vcf';
486 }
487 return $filename;
488 }
489
501 public function buildVCardString($object, $company, $langs, $urlphoto = '', $outdir = '')
502 {
503 global $dolibarr_main_instance_unique_id;
504
505 $this->setProdId('Dolibarr '.DOL_VERSION);
506
507 $this->setUID('DOL-USERID-'.dol_trunc(md5('vcard'.$dolibarr_main_instance_unique_id), 8, 'right', 'UTF-8', 1).'-'.$object->id);
508 $this->setName($object->lastname, $object->firstname, "", $object->civility_code, "");
509 $this->setFormattedName($object->getFullName($langs, 1));
510
511 if ($urlphoto) {
512 $mimetype = dol_mimetype($urlphoto);
513 if ($mimetype) {
514 $this->setPhoto($mimetype, $urlphoto);
515 }
516 }
517
518 if ($object->office_phone) {
519 $this->setPhoneNumber($object->office_phone, "TYPE=WORK,VOICE");
520 }
521 if ($object->office_fax) {
522 $this->setPhoneNumber($object->office_fax, "TYPE=WORK,FAX");
523 }
524 /* disabled
525 if ($object->personal_mobile) {
526 $this->setPhoneNumber($object->personal_mobile, "TYPE=CELL,VOICE");
527 }*/
528 if ($object->user_mobile) {
529 $this->setPhoneNumber($object->user_mobile, "TYPE=CELL,VOICE");
530 }
531
532 if (!empty($object->socialnetworks)) {
533 foreach ($object->socialnetworks as $key => $val) {
534 if (empty($val)) { // Discard social network if empty
535 continue;
536 }
537 $urlsn = '';
538 if ($key == 'linkedin') {
539 if (!preg_match('/^http/', $val)) {
540 $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
541 } else {
542 $urlsn = $val;
543 }
544 } elseif ($key == 'youtube') {
545 if (!preg_match('/^http/', $val)) {
546 $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
547 } else {
548 $urlsn = $val;
549 }
550 } else {
551 if (!preg_match('/^http/', $val)) {
552 $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
553 } else {
554 $urlsn = $val;
555 }
556 }
557 if ($urlsn) {
558 $urlsn = $this->cleanValue($urlsn);
559 $this->properties["SOCIALPROFILE;TYPE=WORK-".preg_replace('/[^A-Za-z0-9_]/', '', $key)] = $key.':'.$urlsn;
560 }
561 }
562 }
563
564 $country = $object->country_code ? $object->country : '';
565
566 // User address
567 $addressalreadyset = 0;
568 if (!($object->element != 'user') || getDolUserInt('USER_PUBLIC_SHOW_ADDRESS', 0, $object)) {
569 if ($object->address || $object->town || $object->state || $object->zip || $object->country) {
570 $this->setAddress("", "", $object->address, $object->town, $object->state, $object->zip, $country, "");
571 $addressalreadyset = 1;
572 }
573 }
574
575 if ($object->email) {
576 $this->setEmail($object->email, "TYPE=WORK");
577 }
578 /* disabled
579 if ($object->personal_email) {
580 $this->setEmail($object->personal_email, "TYPE=HOME");
581 } */
582 /*if ($object->note_public) {
583 $this->setNote($object->note_public);
584 }*/
585 if ($object->job) {
586 $this->setTitle($object->job);
587 }
588
589 // For user, $object->url is not defined
590 // For contact, $object->url is not defined
591 if (!empty($object->url)) {
592 $this->setURL($object->url ?? "");
593 }
594
595 if (is_object($company)) {
596 // Si user linked to a thirdparty and not a physical people
597 if ($company->typent_code != 'TE_PRIVATE') {
598 $this->setOrg($company->name);
599 }
600
601 if (!empty($company->url)) {
602 $this->setURL($company->url, "");
603 }
604
605 if ($company->phone && empty($object->office_phone)) { // If we already set the type TYPE=WORK,VOICE with office_phone
606 $this->setPhoneNumber($company->phone, "TYPE=WORK,VOICE");
607 }
608 if ($company->fax && empty($object->office_fax)) { // If we already set the type TYPE=WORK,FAX with office_phone
609 $this->setPhoneNumber($company->fax, "TYPE=WORK,FAX");
610 }
611 if (($company->address || $company->town || $company->state || $company->zip || $company->country) && !$addressalreadyset) {
612 $this->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK");
613 }
614
615 if ($company->email && empty($object->email)) {
616 $this->setEmail($company->email, "TYPE=WORK");
617 }
618
619 /*
620 if (!empty($company->socialnetworks)) {
621 foreach ($company->socialnetworks as $key => $val) {
622 $urlsn = '';
623 if ($key == 'linkedin') {
624 if (!preg_match('/^http/', $val)) {
625 $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
626 } else {
627 $urlsn = $val;
628 }
629 } elseif ($key == 'youtube') {
630 if (!preg_match('/^http/', $val)) {
631 $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
632 } else {
633 $urlsn = $val;
634 }
635 } else {
636 if (!preg_match('/^http/', $val)) {
637 $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
638 } else {
639 $urlsn = $val;
640 }
641 }
642 if ($urlsn) {
643 $this->properties["socialProfile;type=".$key] = $urlsn;
644 }
645 }
646 }
647 */
648 }
649
650 // Birthday
651 if (!($object->element != 'user') || getDolUserInt('USER_PUBLIC_SHOW_BIRTH', 0, $object)) {
652 if ($object->birth) {
653 $this->setBirthday($object->birth);
654 }
655 }
656
657 if ($outdir) {
658 $outfilename = $outdir.'/virtualcard_'.$object->element.'_'.$object->id.'.vcf';
659
660 file_put_contents($outfilename, $this->getVCard());
661 dolChmod($outfilename);
662
663 return $outfilename;
664 }
665
666 // Return VCard string
667 return $this->getVCard();
668 }
669
670
671 /* Example from Microsoft Outlook 2019
672
673 BEGIN:VCARD
674 VERSION:2.1
675
676 N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
677 FN:Sir surename secondname forename jun.
678 ORG:Companyname
679 TITLE:position
680 TEL;WORK;VOICE:work-phone-number
681 TEL;HOME;VOICE:private-phone-number
682 TEL;CELL;VOICE:mobile-phone-number
683 TEL;WORK;FAX:fax-phone-number
684 ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
685 LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
686 =0D=0A=
687 012345 town region
688 X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
689 URL;WORK:www.mywebpage.de
690 EMAIL;PREF;INTERNET:test1@test1.de
691 EMAIL;INTERNET:test2@test2.de
692 EMAIL;INTERNET:test3@test3.de
693 X-MS-IMADDRESS:test@jabber.org
694 REV:20200424T104242Z
695
696 END:VCARD
697 */
698}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$c
Definition line.php:353
Class to build vCard files.
setEmail($address, $type="")
Add a e-mail address to this vCard.
setTitle($title)
mise en forme de la fonction
setPhoneNumber($number, $type="")
Format phone number.
setPhoto($type, $photo)
Format photo.
setBirthday($date)
Format the birth date.
setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME")
Address (old standard)
foldLine($line)
Return a line of the card folded as RFC 6350 section 3.2 asks: no more than 75 octets,...
cleanValue($value)
Remove the line breaks and control characters of a value written as is, so it can not add a property ...
setUID($uid)
mise en forme du logiciel generateur
getFileName()
Return name of a file, safe to use in a HTTP header and as a file name (no accent,...
normalizeType($type)
Return the parameters of a type in the vCard 4.0 form: "TYPE=WORK;VOICE" (old form,...
setOrg($org)
mise en forme de la societe
buildVCardString($object, $company, $langs, $urlphoto='', $outdir='')
Return a VCARD string See RFC https://datatracker.ietf.org/doc/html/rfc6350.
setProdId($prodid)
mise en forme du logiciel generateur
setFormattedName($name)
Format name.
setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="", $label="")
Address.
setName($family="", $first="", $additional="", $prefix="", $suffix="")
Format the name.
getVCard()
Return string of a vcard.
setNote($note)
mise en forme de la note
setURL($url, $type="")
mise en forme de l'url
dol_now($mode='gmt')
Return date for now.
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
getDolUserInt($key, $default=0, $tmpuser=null)
Return Dolibarr user constant int value.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '...' if string larger than length.
encode($string)
Escape a text value for a vCard 4.0 (RFC 6350 section 3.4): backslash, semicolon, comma and line brea...
dol_quoted_printable_encode($input, $line_max=76)
Taken from php documentation comments No more used.