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