dolibarr 21.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 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
34function encode($string)
35{
36 return str_replace(";", "\;", (dol_quoted_printable_encode($string)));
37}
38
39
48function dol_quoted_printable_encode($input, $line_max = 76)
49{
50 $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
51 $lines = preg_split("/(\?:\r\n|\r|\n)/", $input);
52 $eol = "\r\n";
53 $linebreak = "=0D=0A";
54 $escape = "=";
55 $output = "";
56
57 $num = count($lines);
58 for ($j = 0; $j < $num; $j++) {
59 $line = $lines[$j];
60 $linlen = strlen($line);
61 $newline = "";
62 for ($i = 0; $i < $linlen; $i++) {
63 $c = substr($line, $i, 1);
64 $dec = ord($c);
65 if (($dec == 32) && ($i == ($linlen - 1))) { // convert space at eol only
66 $c = "=20";
67 } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { // always encode "\t", which is *not* required
68 $h2 = floor($dec / 16);
69 $h1 = floor($dec % 16);
70 $c = $escape.$hex[(int) $h2].$hex[(int) $h1];
71 }
72 if ((strlen($newline) + strlen($c)) >= $line_max) { // CRLF is not counted
73 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
74 $newline = " ";
75 }
76 $newline .= $c;
77 } // end of for
78 $output .= $newline;
79 if ($j < count($lines) - 1) {
80 $output .= $linebreak;
81 }
82 }
83 return trim($output);
84}
85
86
90class vCard
91{
95 public $properties;
96
100 public $filename;
101
105 public $encoding = "ENCODING=QUOTED-PRINTABLE";
106
107
115 public function setPhoneNumber($number, $type = "")
116 {
117 // 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"
118 $key = "TEL";
119 if ($type != "") {
120 $key .= ";".$type;
121 }
122 $key .= ";VALUE=uri";
123 //$key .= ";".$this->encoding;
124 $this->properties[$key] = 'tel:'.$number;
125 }
126
135 public function setPhoto($type, $photo)
136 {
137 // $type = "GIF" | "JPEG"
138 //$this->properties["PHOTO;MEDIATYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
139 $this->properties["PHOTO;MEDIATYPE=$type"] = $photo; // must be url of photo
140 //$this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo); // must be content of image
141 }
142
149 public function setFormattedName($name)
150 {
151 $stringencoded = encode($name);
152 $stringnotencoded = $name;
153
154 $key = "FN";
155 if ($stringencoded != $stringnotencoded) {
156 $key .= ";".$this->encoding;
157 }
158
159 $this->properties[$key] = $stringencoded;
160 }
161
173 public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
174 {
175 //$this->properties["N;".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
176 $this->properties["N"] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
177
178 $this->filename = "$first%20$family.vcf";
179
180 if (empty($this->properties["FN"])) {
181 $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
182 }
183 }
184
191 public function setBirthday($date)
192 {
193 // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426 for vcard v3
194 // $date format is YYYYMMDD or ISO8601 for vcard v4
195 $this->properties["BDAY"] = dol_print_date($date, 'dayxcard');
196 }
197
212 public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "", $label = "")
213 {
214 // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
215 $key = "ADR";
216 if ($type != "") {
217 $key .= ";".$type;
218 }
219 if ($label != "") {
220 $key .= ';LABEL="'.encode($label).'"';
221 }
222
223 $stringencoded = encode($postoffice).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
224 $stringnotencoded = $postoffice.";".$extended.";".$street.";".$city.";".$region.";".$zip.";".$country;
225
226 if ($stringencoded != $stringnotencoded) {
227 $key .= ";".$this->encoding;
228 }
229
230 $this->properties[$key] = $stringencoded;
231
232 //if ($this->properties["LABEL;".$type.";".$this->encoding] == '') {
233 //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
234 //}
235 }
236
251 public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME")
252 {
253 $label = "";
254 if ($postoffice != "") {
255 $label .= "$postoffice\r\n";
256 }
257 if ($extended != "") {
258 $label .= "$extended\r\n";
259 }
260 if ($street != "") {
261 $label .= "$street\r\n";
262 }
263 if ($zip != "") {
264 $label .= "$zip ";
265 }
266 if ($city != "") {
267 $label .= "$city\r\n";
268 }
269 if ($region != "") {
270 $label .= "$region\r\n";
271 }
272 if ($country != "") {
273 $country .= "$country\r\n";
274 }
275
276 $this->properties["LABEL;$type;".$this->encoding] = encode($label);
277 }
278
286 public function setEmail($address, $type = "")
287 {
288 $key = "EMAIL";
289 if ($type === "PREF") {
290 $key .= ";PREF=1";
291 } elseif (!empty($type)) {
292 if (stripos($type, 'TYPE=') === 0) {
293 $key .= ";".$type;
294 } else {
295 $key .= ";TYPE=".dol_strtolower($type);
296 }
297 }
298 $this->properties[$key] = $address;
299 }
300
307 public function setNote($note)
308 {
309 $this->properties["NOTE;".$this->encoding] = encode($note);
310 }
311
318 public function setTitle($title)
319 {
320 $this->properties["TITLE;".$this->encoding] = encode($title);
321 }
322
323
330 public function setOrg($org)
331 {
332 $this->properties["ORG;".$this->encoding] = encode($org);
333 }
334
335
342 public function setProdId($prodid)
343 {
344 $this->properties["PRODID"] = encode($prodid);
345 }
346
347
354 public function setUID($uid)
355 {
356 $this->properties["UID"] = encode($uid);
357 }
358
359
367 public function setURL($url, $type = "")
368 {
369 // $type may be WORK | HOME
370 $key = "URL";
371 if ($type != "") {
372 $key .= ";$type";
373 }
374 $this->properties[$key] = $url;
375 }
376
382 public function getVCard()
383 {
384 $text = "BEGIN:VCARD\r\n";
385 $text .= "VERSION:4.0\r\n"; // With V4, all encoding are UTF-8
386 //$text.= "VERSION:2.1\r\n";
387 foreach ($this->properties as $key => $value) {
388 $newkey = preg_replace('/-.*$/', '', $key); // remove suffix -twitter, -facebook, ...
389 $text .= $newkey.":".$value."\r\n";
390 }
391 $text .= "REV:".date("Ymd")."T".date("His")."Z\r\n";
392 //$text .= "MAILER: Dolibarr\r\n";
393 $text .= "END:VCARD\r\n";
394
395 return $text;
396 }
397
403 public function getFileName()
404 {
405 return $this->filename;
406 }
407
419 public function buildVCardString($object, $company, $langs, $urlphoto = '', $outdir = '')
420 {
421 global $dolibarr_main_instance_unique_id;
422
423 $this->setProdId('Dolibarr '.DOL_VERSION);
424
425 $this->setUID('DOL-USERID-'.dol_trunc(md5('vcard'.$dolibarr_main_instance_unique_id), 8, 'right', 'UTF-8', 1).'-'.$object->id);
426 $this->setName($object->lastname, $object->firstname, "", $object->civility_code, "");
427 $this->setFormattedName($object->getFullName($langs, 1));
428
429 if ($urlphoto) {
430 $mimetype = dol_mimetype($urlphoto);
431 if ($mimetype) {
432 $this->setPhoto($mimetype, $urlphoto);
433 }
434 }
435
436 if ($object->office_phone) {
437 $this->setPhoneNumber($object->office_phone, "TYPE=WORK,VOICE");
438 }
439 if ($object->office_fax) {
440 $this->setPhoneNumber($object->office_fax, "TYPE=WORK,FAX");
441 }
442 /* disabled
443 if ($object->personal_mobile) {
444 $this->setPhoneNumber($object->personal_mobile, "TYPE=CELL,VOICE");
445 }*/
446 if ($object->user_mobile) {
447 $this->setPhoneNumber($object->user_mobile, "TYPE=CELL,VOICE");
448 }
449
450 if (!empty($object->socialnetworks)) {
451 foreach ($object->socialnetworks as $key => $val) {
452 if (empty($val)) { // Discard social network if empty
453 continue;
454 }
455 $urlsn = '';
456 if ($key == 'linkedin') {
457 if (!preg_match('/^http/', $val)) {
458 $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
459 } else {
460 $urlsn = $val;
461 }
462 } elseif ($key == 'youtube') {
463 if (!preg_match('/^http/', $val)) {
464 $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
465 } else {
466 $urlsn = $val;
467 }
468 } else {
469 if (!preg_match('/^http/', $val)) {
470 $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
471 } else {
472 $urlsn = $val;
473 }
474 }
475 if ($urlsn) {
476 $this->properties["SOCIALPROFILE;TYPE=WORK-".$key] = $key.':'.$urlsn;
477 }
478 }
479 }
480
481 $country = $object->country_code ? $object->country : '';
482
483 // User address
484 $addressalreadyset = 0;
485 if (!($object->element != 'user') || getDolUserInt('USER_PUBLIC_SHOW_ADDRESS', 0, $object)) {
486 if ($object->address || $object->town || $object->state || $object->zip || $object->country) {
487 $this->setAddress("", "", $object->address, $object->town, $object->state, $object->zip, $country, "");
488 $addressalreadyset = 1;
489 }
490 }
491
492 if ($object->email) {
493 $this->setEmail($object->email, "TYPE=WORK");
494 }
495 /* disabled
496 if ($object->personal_email) {
497 $this->setEmail($object->personal_email, "TYPE=HOME");
498 } */
499 /*if ($object->note_public) {
500 $this->setNote($object->note_public);
501 }*/
502 if ($object->job) {
503 $this->setTitle($object->job);
504 }
505
506 // For user, $object->url is not defined
507 // For contact, $object->url is not defined
508 if (!empty($object->url)) {
509 $this->setURL($object->url, "");
510 }
511
512 if (is_object($company)) {
513 // Si user linked to a thirdparty and not a physical people
514 if ($company->typent_code != 'TE_PRIVATE') {
515 $this->setOrg($company->name);
516 }
517
518 $this->setURL($company->url, "");
519
520 if ($company->phone && empty($object->office_phone)) { // If we already set the type TYPE=WORK,VOICE with office_phone
521 $this->setPhoneNumber($company->phone, "TYPE=WORK,VOICE");
522 }
523 if ($company->fax && empty($object->office_fax)) { // If we already set the type TYPE=WORK,FAX with office_phone
524 $this->setPhoneNumber($company->fax, "TYPE=WORK,FAX");
525 }
526 if (($company->address || $company->town || $company->state || $company->zip || $company->country) && !$addressalreadyset) {
527 $this->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK");
528 }
529
530 if ($company->email && empty($object->email)) {
531 $this->setEmail($company->email, "TYPE=WORK");
532 }
533
534 /*
535 if (!empty($company->socialnetworks)) {
536 foreach ($company->socialnetworks as $key => $val) {
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 $this->properties["socialProfile;type=".$key] = $urlsn;
559 }
560 }
561 }
562 */
563 }
564
565 // Birthday
566 if (!($object->element != 'user') || getDolUserInt('USER_PUBLIC_SHOW_BIRTH', 0, $object)) {
567 if ($object->birth) {
568 $this->setBirthday($object->birth);
569 }
570 }
571
572 if ($outdir) {
573 $outfilename = $outdir.'/virtualcard_'.$object->element.'_'.$object->id.'.vcf';
574
575 file_put_contents($outfilename, $this->getVCard());
576 dolChmod($outfilename);
577
578 return $outfilename;
579 }
580
581 // Return VCard string
582 return $this->getVCard();
583 }
584
585
586 /* Example from Microsoft Outlook 2019
587
588 BEGIN:VCARD
589 VERSION:2.1
590
591 N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
592 FN:Sir surename secondname forename jun.
593 ORG:Companyname
594 TITLE:position
595 TEL;WORK;VOICE:work-phone-number
596 TEL;HOME;VOICE:private-phone-number
597 TEL;CELL;VOICE:mobile-phone-number
598 TEL;WORK;FAX:fax-phone-number
599 ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
600 LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
601 =0D=0A=
602 012345 town region
603 X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
604 URL;WORK:www.mywebpage.de
605 EMAIL;PREF;INTERNET:test1@test1.de
606 EMAIL;INTERNET:test2@test2.de
607 EMAIL;INTERNET:test3@test3.de
608 X-MS-IMADDRESS:test@jabber.org
609 REV:20200424T104242Z
610
611 END:VCARD
612 */
613}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
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)
setUID($uid)
mise en forme du logiciel generateur
getFileName()
Return name of a file.
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_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.
dolChmod($filepath, $newmask='')
Change mod of a file.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
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)
Encode a string for vCard.
dol_quoted_printable_encode($input, $line_max=76)
Taken from php documentation comments No more used.