dolibarr 20.0.2
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
33function encode($string)
34{
35 return str_replace(";", "\;", (dol_quoted_printable_encode($string)));
36}
37
38
47function dol_quoted_printable_encode($input, $line_max = 76)
48{
49 $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
50 $lines = preg_split("/(\?:\r\n|\r|\n)/", $input);
51 $eol = "\r\n";
52 $linebreak = "=0D=0A";
53 $escape = "=";
54 $output = "";
55
56 $num = count($lines);
57 for ($j = 0; $j < $num; $j++) {
58 $line = $lines[$j];
59 $linlen = strlen($line);
60 $newline = "";
61 for ($i = 0; $i < $linlen; $i++) {
62 $c = substr($line, $i, 1);
63 $dec = ord($c);
64 if (($dec == 32) && ($i == ($linlen - 1))) { // convert space at eol only
65 $c = "=20";
66 } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { // always encode "\t", which is *not* required
67 $h2 = floor($dec / 16);
68 $h1 = floor($dec % 16);
69 $c = $escape.$hex["$h2"].$hex["$h1"];
70 }
71 if ((strlen($newline) + strlen($c)) >= $line_max) { // CRLF is not counted
72 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
73 $newline = " ";
74 }
75 $newline .= $c;
76 } // end of for
77 $output .= $newline;
78 if ($j < count($lines) - 1) {
79 $output .= $linebreak;
80 }
81 }
82 return trim($output);
83}
84
85
89class vCard
90{
94 public $properties;
95
99 public $filename;
100
104 public $encoding = "CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE";
105
106
114 public function setPhoneNumber($number, $type = "")
115 {
116 // 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"
117 $key = "TEL";
118 if ($type != "") {
119 $key .= ";".$type;
120 }
121 $key .= ";VALUE=uri";
122 //$key .= ";".$this->encoding;
123 $this->properties[$key] = 'tel:'.$number;
124 }
125
134 public function setPhoto($type, $photo)
135 {
136 // $type = "GIF" | "JPEG"
137 //$this->properties["PHOTO;MEDIATYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
138 $this->properties["PHOTO;MEDIATYPE=$type"] = $photo; // must be url of photo
139 //$this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo); // must be content of image
140 }
141
148 public function setFormattedName($name)
149 {
150 $this->properties["FN;".$this->encoding] = encode($name);
151 }
152
164 public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
165 {
166 //$this->properties["N;".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
167 $this->properties["N;".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
168 $this->filename = "$first%20$family.vcf";
169 if (empty($this->properties["FN"])) {
170 $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
171 }
172 }
173
180 public function setBirthday($date)
181 {
182 // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426 for vcard v3
183 // $date format is YYYYMMDD or ISO8601 for vcard v4
184 $this->properties["BDAY"] = dol_print_date($date, 'dayxcard');
185 }
186
201 public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "", $label = "")
202 {
203 // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
204 $key = "ADR";
205 if ($type != "") {
206 $key .= ";".$type;
207 }
208 if ($label != "") {
209 $key .= ';LABEL="'.encode($label).'"';
210 }
211 $key .= ";".$this->encoding;
212 $this->properties[$key] = encode($postoffice).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
213
214 //if ($this->properties["LABEL;".$type.";".$this->encoding] == '') {
215 //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
216 //}
217 }
218
233 public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME")
234 {
235 $label = "";
236 if ($postoffice != "") {
237 $label .= "$postoffice\r\n";
238 }
239 if ($extended != "") {
240 $label .= "$extended\r\n";
241 }
242 if ($street != "") {
243 $label .= "$street\r\n";
244 }
245 if ($zip != "") {
246 $label .= "$zip ";
247 }
248 if ($city != "") {
249 $label .= "$city\r\n";
250 }
251 if ($region != "") {
252 $label .= "$region\r\n";
253 }
254 if ($country != "") {
255 $country .= "$country\r\n";
256 }
257
258 $this->properties["LABEL;$type;".$this->encoding] = encode($label);
259 }
260
268 public function setEmail($address, $type = "")
269 {
270 $key = "EMAIL";
271 if ($type == "PREF") {
272 $key .= ";PREF=1";
273 } elseif (!empty($type)) {
274 $key .= ";TYPE=".dol_strtolower($type);
275 }
276 $this->properties[$key] = $address;
277 }
278
285 public function setNote($note)
286 {
287 $this->properties["NOTE;".$this->encoding] = encode($note);
288 }
289
296 public function setTitle($title)
297 {
298 $this->properties["TITLE;".$this->encoding] = encode($title);
299 }
300
301
308 public function setOrg($org)
309 {
310 $this->properties["ORG;".$this->encoding] = encode($org);
311 }
312
313
320 public function setProdId($prodid)
321 {
322 $this->properties["PRODID"] = encode($prodid);
323 }
324
325
332 public function setUID($uid)
333 {
334 $this->properties["UID"] = encode($uid);
335 }
336
337
345 public function setURL($url, $type = "")
346 {
347 // $type may be WORK | HOME
348 $key = "URL";
349 if ($type != "") {
350 $key .= ";$type";
351 }
352 $this->properties[$key] = $url;
353 }
354
360 public function getVCard()
361 {
362 $text = "BEGIN:VCARD\r\n";
363 $text .= "VERSION:4.0\r\n"; // With V4, all encoding are UTF-8
364 //$text.= "VERSION:2.1\r\n";
365 foreach ($this->properties as $key => $value) {
366 $newkey = preg_replace('/(?<!QUOTED|UTF)-.*$/', '', $key); // remove suffix -twitter, -facebook, ...
367 $text .= $newkey.":".$value."\r\n";
368 }
369 $text .= "REV:".date("Ymd")."T".date("His")."Z\r\n";
370 //$text .= "MAILER: Dolibarr\r\n";
371 $text .= "END:VCARD\r\n";
372
373 return $text;
374 }
375
381 public function getFileName()
382 {
383 return $this->filename;
384 }
385
397 public function buildVCardString($object, $company, $langs, $urlphoto = '', $outdir = '')
398 {
399 global $dolibarr_main_instance_unique_id;
400
401 $this->setProdId('Dolibarr '.DOL_VERSION);
402
403 $this->setUID('DOLIBARR-USERID-'.dol_trunc(md5('vcard'.$dolibarr_main_instance_unique_id), 8, 'right', 'UTF-8', 1).'-'.$object->id);
404 $this->setName($object->lastname, $object->firstname, "", $object->civility_code, "");
405 $this->setFormattedName($object->getFullName($langs, 1));
406
407 if ($urlphoto) {
408 $mimetype = dol_mimetype($urlphoto);
409 if ($mimetype) {
410 $this->setPhoto($mimetype, $urlphoto);
411 }
412 }
413
414 if ($object->office_phone) {
415 $this->setPhoneNumber($object->office_phone, "TYPE=WORK,VOICE");
416 }
417 /* disabled
418 if ($object->personal_mobile) {
419 $this->setPhoneNumber($object->personal_mobile, "TYPE=CELL,VOICE");
420 }*/
421 if ($object->user_mobile) {
422 $this->setPhoneNumber($object->user_mobile, "TYPE=CELL,VOICE");
423 }
424 if ($object->office_fax) {
425 $this->setPhoneNumber($object->office_fax, "TYPE=WORK,FAX");
426 }
427
428 if (!empty($object->socialnetworks)) {
429 foreach ($object->socialnetworks as $key => $val) {
430 if (empty($val)) { // Disacard social network if empty
431 continue;
432 }
433 $urlsn = '';
434 if ($key == 'linkedin') {
435 if (!preg_match('/^http/', $val)) {
436 $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
437 } else {
438 $urlsn = $val;
439 }
440 } elseif ($key == 'youtube') {
441 if (!preg_match('/^http/', $val)) {
442 $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
443 } else {
444 $urlsn = $val;
445 }
446 } else {
447 if (!preg_match('/^http/', $val)) {
448 $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
449 } else {
450 $urlsn = $val;
451 }
452 }
453 if ($urlsn) {
454 $this->properties["SOCIALPROFILE;TYPE=WORK-".$key] = $key.':'.$urlsn;
455 }
456 }
457 }
458
459 $country = $object->country_code ? $object->country : '';
460
461 // User address
462 if (!($object->element != 'user') || getDolUserInt('USER_PUBLIC_SHOW_ADDRESS', 0, $object)) {
463 if ($object->address || $object->town || $object->state || $object->zip || $object->country) {
464 $this->setAddress("", "", $object->address, $object->town, $object->state, $object->zip, $country, "");
465 //$this->setLabel("", "", $object->address, $object->town, $object->state, $object->zip, $country, "TYPE=HOME");
466 }
467 }
468
469 if ($object->email) {
470 $this->setEmail($object->email, "TYPE=WORK");
471 }
472 /* disabled
473 if ($object->personal_email) {
474 $this->setEmail($object->personal_email, "TYPE=HOME");
475 } */
476 if ($object->note_public) {
477 $this->setNote($object->note_public);
478 }
479 if ($object->job) {
480 $this->setTitle($object->job);
481 }
482
483 // For user, $object->url is not defined
484 // For contact, $object->url is not defined
485 if (!empty($object->url)) {
486 $this->setURL($object->url, "");
487 }
488
489 if (is_object($company)) {
490 // Si user linked to a thirdparty and not a physical people
491 if ($company->typent_code != 'TE_PRIVATE') {
492 $this->setOrg($company->name);
493 }
494
495 $this->setURL($company->url, "");
496
497 if ($company->phone && $company->phone != $object->office_phone) {
498 $this->setPhoneNumber($company->phone, "TYPE=WORK,VOICE");
499 }
500 if ($company->fax && $company->fax != $object->office_fax) {
501 $this->setPhoneNumber($company->fax, "TYPE=WORK,FAX");
502 }
503 if ($company->address || $company->town || $company->state || $company->zip || $company->country) {
504 $this->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK");
505 }
506
507 if ($company->email && $company->email != $object->email) {
508 $this->setEmail($company->email, "TYPE=WORK");
509 }
510
511 /*
512 if (!empty($company->socialnetworks)) {
513 foreach ($company->socialnetworks as $key => $val) {
514 $urlsn = '';
515 if ($key == 'linkedin') {
516 if (!preg_match('/^http/', $val)) {
517 $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
518 } else {
519 $urlsn = $val;
520 }
521 } elseif ($key == 'youtube') {
522 if (!preg_match('/^http/', $val)) {
523 $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
524 } else {
525 $urlsn = $val;
526 }
527 } else {
528 if (!preg_match('/^http/', $val)) {
529 $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
530 } else {
531 $urlsn = $val;
532 }
533 }
534 if ($urlsn) {
535 $this->properties["socialProfile;type=".$key] = $urlsn;
536 }
537 }
538 }
539 */
540 }
541
542 // Birthday
543 if (!($object->element != 'user') || getDolUserInt('USER_PUBLIC_SHOW_BIRTH', 0, $object)) {
544 if ($object->birth) {
545 $this->setBirthday($object->birth);
546 }
547 }
548
549 if ($outdir) {
550 $outfilename = $outdir.'/virtualcard_'.$object->element.'_'.$object->id.'.vcf';
551
552 file_put_contents($outfilename, $this->getVCard());
553 dolChmod($outfilename);
554
555 return $outfilename;
556 }
557
558 // Return VCard string
559 return $this->getVCard();
560 }
561
562
563 /* Example from Microsoft Outlook 2019
564
565 BEGIN:VCARD
566 VERSION:2.1
567
568 N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
569 FN:Sir surename secondname forename jun.
570 ORG:Companyname
571 TITLE:position
572 TEL;WORK;VOICE:work-phone-number
573 TEL;HOME;VOICE:private-phone-number
574 TEL;CELL;VOICE:mobile-phone-number
575 TEL;WORK;FAX:fax-phone-number
576 ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
577 LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
578 =0D=0A=
579 012345 town region
580 X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
581 URL;WORK:www.mywebpage.de
582 EMAIL;PREF;INTERNET:test1@test1.de
583 EMAIL;INTERNET:test2@test2.de
584 EMAIL;INTERNET:test3@test3.de
585 X-MS-IMADDRESS:test@jabber.org
586 REV:20200424T104242Z
587
588 END:VCARD
589 */
590}
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.