dolibarr  18.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  *
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 
32 function encode($string)
33 {
34  return str_replace(";", "\;", (dol_quoted_printable_encode($string)));
35 }
36 
37 
46 function 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 
88 class 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 
162  public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
163  {
164  //$this->properties["N;".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
165  $this->properties["N"] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
166  $this->filename = "$first%20$family.vcf";
167  if (empty($this->properties["FN"])) {
168  $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
169  }
170  }
171 
178  public function setBirthday($date)
179  {
180  // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426 for vcard v3
181  // $date format is YYYYMMDD or ISO8601 for vcard v4
182  $this->properties["BDAY"] = dol_print_date($date, 'dayxcard');
183  }
184 
199  public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "", $label = "")
200  {
201  // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
202  $key = "ADR";
203  if ($type != "") {
204  $key .= ";".$type;
205  }
206  if ($label != "") {
207  $key .= ';LABEL="'.encode($label).'"';
208  }
209  $key .= ";".$this->encoding;
210  $this->properties[$key] = encode($postoffice).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
211 
212  //if ($this->properties["LABEL;".$type.";".$this->encoding] == '') {
213  //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
214  //}
215  }
216 
231  public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME")
232  {
233  $label = "";
234  if ($postoffice != "") {
235  $label .= "$postoffice\r\n";
236  }
237  if ($extended != "") {
238  $label .= "$extended\r\n";
239  }
240  if ($street != "") {
241  $label .= "$street\r\n";
242  }
243  if ($zip != "") {
244  $label .= "$zip ";
245  }
246  if ($city != "") {
247  $label .= "$city\r\n";
248  }
249  if ($region != "") {
250  $label .= "$region\r\n";
251  }
252  if ($country != "") {
253  $country .= "$country\r\n";
254  }
255 
256  $this->properties["LABEL;$type;".$this->encoding] = encode($label);
257  }
258 
266  public function setEmail($address, $type = "")
267  {
268  $key = "EMAIL";
269  if ($type != "") {
270  $key .= ";".$type;
271  }
272  $this->properties[$key] = $address;
273  }
274 
281  public function setNote($note)
282  {
283  $this->properties["NOTE;".$this->encoding] = encode($note);
284  }
285 
292  public function setTitle($title)
293  {
294  $this->properties["TITLE;".$this->encoding] = encode($title);
295  }
296 
297 
304  public function setOrg($org)
305  {
306  $this->properties["ORG;".$this->encoding] = encode($org);
307  }
308 
309 
316  public function setProdId($prodid)
317  {
318  $this->properties["PRODID"] = encode($prodid);
319  }
320 
321 
328  public function setUID($uid)
329  {
330  $this->properties["UID"] = encode($uid);
331  }
332 
333 
341  public function setURL($url, $type = "")
342  {
343  // $type may be WORK | HOME
344  $key = "URL";
345  if ($type != "") {
346  $key .= ";$type";
347  }
348  $this->properties[$key] = $url;
349  }
350 
356  public function getVCard()
357  {
358  $text = "BEGIN:VCARD\r\n";
359  $text .= "VERSION:4.0\r\n"; // With V4, all encoding are UTF-8
360  //$text.= "VERSION:2.1\r\n";
361  foreach ($this->properties as $key => $value) {
362  $newkey = preg_replace('/-.*$/', '', $key); // remove suffix -twitter, -facebook, ...
363  $text .= $newkey.":".$value."\r\n";
364  }
365  $text .= "REV:".date("Ymd")."T".date("His")."Z\r\n";
366  //$text .= "MAILER: Dolibarr\r\n";
367  $text .= "END:VCARD\r\n";
368  return $text;
369  }
370 
376  public function getFileName()
377  {
378  return $this->filename;
379  }
380 
391  public function buildVCardString($object, $company, $langs, $urlphoto = '')
392  {
393  global $dolibarr_main_instance_unique_id;
394 
395  $this->setProdId('Dolibarr '.DOL_VERSION);
396 
397  $this->setUid('DOLIBARR-USERID-'.dol_trunc(md5('vcard'.$dolibarr_main_instance_unique_id), 8, 'right', 'UTF-8', 1).'-'.$object->id);
398  $this->setName($object->lastname, $object->firstname, "", $object->civility_code, "");
399  $this->setFormattedName($object->getFullName($langs, 1));
400 
401  if ($urlphoto) {
402  $mimetype = dol_mimetype($urlphoto);
403  if ($mimetype) {
404  $this->setPhoto($mimetype, $urlphoto);
405  }
406  }
407 
408  if ($object->office_phone) {
409  $this->setPhoneNumber($object->office_phone, "TYPE=WORK,VOICE");
410  }
411  /* disabled
412  if ($object->personal_mobile) {
413  $this->setPhoneNumber($object->personal_mobile, "TYPE=CELL,VOICE");
414  }*/
415  if ($object->user_mobile) {
416  $this->setPhoneNumber($object->user_mobile, "TYPE=CELL,VOICE");
417  }
418  if ($object->office_fax) {
419  $this->setPhoneNumber($object->office_fax, "TYPE=WORK,FAX");
420  }
421 
422  if (!empty($object->socialnetworks)) {
423  foreach ($object->socialnetworks as $key => $val) {
424  $urlsn = '';
425  if ($key == 'linkedin') {
426  if (!preg_match('/^http/', $val)) {
427  $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
428  } else {
429  $urlsn = $val;
430  }
431  } elseif ($key == 'youtube') {
432  if (!preg_match('/^http/', $val)) {
433  $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
434  } else {
435  $urlsn = $val;
436  }
437  } else {
438  if (!preg_match('/^http/', $val)) {
439  $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
440  } else {
441  $urlsn = $val;
442  }
443  }
444  if ($urlsn) {
445  $this->properties["SOCIALPROFILE;TYPE=WORK-".$key] = $key.':'.$urlsn;
446  }
447  }
448  }
449 
450  $country = $object->country_code ? $object->country : '';
451 
452  if ($object->address || $object->town || $object->state || $object->zip || $object->country) {
453  $this->setAddress("", "", $object->address, $object->town, $object->state, $object->zip, $country, "");
454  //$this->setLabel("", "", $object->address, $object->town, $object->state, $object->zip, $country, "TYPE=HOME");
455  }
456 
457  if ($object->email) {
458  $this->setEmail($object->email, "TYPE=WORK");
459  }
460  /* disabled
461  if ($object->personal_email) {
462  $this->setEmail($object->personal_email, "TYPE=HOME");
463  } */
464  if ($object->note_public) {
465  $this->setNote($object->note_public);
466  }
467  if ($object->job) {
468  $this->setTitle($object->job);
469  }
470 
471  // For user, $object->url is not defined
472  // For contact, $object->url is not defined
473  if (!empty($object->url)) {
474  $this->setURL($object->url, "");
475  }
476 
477  if (is_object($company)) {
478  // Si user linked to a thirdparty and not a physical people
479  if ($company->typent_code != 'TE_PRIVATE') {
480  $this->setOrg($company->name);
481  }
482 
483  $this->setURL($company->url, "");
484 
485  if ($company->phone && $company->phone != $object->office_phone) {
486  $this->setPhoneNumber($company->phone, "TYPE=WORK,VOICE");
487  }
488  if ($company->fax && $company->fax != $object->office_fax) {
489  $this->setPhoneNumber($company->fax, "TYPE=WORK,FAX");
490  }
491  if ($company->address || $company->town || $company->state || $company->zip || $company->country) {
492  $this->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK");
493  }
494 
495  if ($company->email && $company->email != $object->email) {
496  $this->setEmail($company->email, "TYPE=WORK");
497  }
498 
499  /*
500  if (!empty($company->socialnetworks)) {
501  foreach ($company->socialnetworks as $key => $val) {
502  $urlsn = '';
503  if ($key == 'linkedin') {
504  if (!preg_match('/^http/', $val)) {
505  $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
506  } else {
507  $urlsn = $val;
508  }
509  } elseif ($key == 'youtube') {
510  if (!preg_match('/^http/', $val)) {
511  $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
512  } else {
513  $urlsn = $val;
514  }
515  } else {
516  if (!preg_match('/^http/', $val)) {
517  $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
518  } else {
519  $urlsn = $val;
520  }
521  }
522  if ($urlsn) {
523  $this->properties["socialProfile;type=".$key] = $urlsn;
524  }
525  }
526  }
527  */
528  }
529 
530  // Birthday
531  if ($object->birth) {
532  $this->setBirthday($object->birth);
533  }
534 
535  // Return VCard string
536  return $this->getVCard();
537  }
538 
539 
540  /* Example from Microsoft Outlook 2019
541 
542  BEGIN:VCARD
543  VERSION:2.1
544 
545  N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
546  FN:Sir surename secondname forename jun.
547  ORG:Companyname
548  TITLE:position
549  TEL;WORK;VOICE:work-phone-number
550  TEL;HOME;VOICE:private-phone-number
551  TEL;CELL;VOICE:mobile-phone-number
552  TEL;WORK;FAX:fax-phone-number
553  ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
554  LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
555  =0D=0A=
556  012345 town region
557  X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
558  URL;WORK:www.mywebpage.de
559  EMAIL;PREF;INTERNET:test1@test1.de
560  EMAIL;INTERNET:test2@test2.de
561  EMAIL;INTERNET:test3@test3.de
562  X-MS-IMADDRESS:test@jabber.org
563  REV:20200424T104242Z
564 
565  END:VCARD
566  */
567 }
vCard\setTitle
setTitle($title)
mise en forme de la fonction
Definition: vcard.class.php:292
vCard\setPhoto
setPhoto($type, $photo)
mise en forme de la photo warning NON TESTE !
Definition: vcard.class.php:133
vCard\setNote
setNote($note)
mise en forme de la note
Definition: vcard.class.php:281
dol_trunc
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.
Definition: functions.lib.php:3950
vCard\setEmail
setEmail($address, $type="")
Add a e-mail address to this vCard.
Definition: vcard.class.php:266
vCard\setBirthday
setBirthday($date)
mise en forme de l'anniversaire
Definition: vcard.class.php:178
vCard
Class to buld vCard files.
Definition: vcard.class.php:88
dol_mimetype
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
Definition: functions.lib.php:10170
dol_quoted_printable_encode
dol_quoted_printable_encode($input, $line_max=76)
Taken from php documentation comments No more used.
Definition: vcard.class.php:46
vCard\setName
setName($family="", $first="", $additional="", $prefix="", $suffix="")
mise en forme du nom complet
Definition: vcard.class.php:162
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2566
vCard\setAddress
setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="", $label="")
Address.
Definition: vcard.class.php:199
vCard\setProdId
setProdId($prodid)
mise en forme du logiciel generateur
Definition: vcard.class.php:316
vCard\buildVCardString
buildVCardString($object, $company, $langs, $urlphoto='')
Return a VCARD string See RFC https://datatracker.ietf.org/doc/html/rfc6350.
Definition: vcard.class.php:391
vCard\setURL
setURL($url, $type="")
mise en forme de l'url
Definition: vcard.class.php:341
vCard\setLabel
setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME")
Address (old standard)
Definition: vcard.class.php:231
vCard\setUID
setUID($uid)
mise en forme du logiciel generateur
Definition: vcard.class.php:328
vCard\getVCard
getVCard()
permet d'obtenir une vcard
Definition: vcard.class.php:356
vCard\setFormattedName
setFormattedName($name)
mise en forme du nom formate
Definition: vcard.class.php:147
vCard\setPhoneNumber
setPhoneNumber($number, $type="")
mise en forme du numero de telephone
Definition: vcard.class.php:113
vCard\getFileName
getFileName()
permet d'obtenir le nom de fichier
Definition: vcard.class.php:376
vCard\setOrg
setOrg($org)
mise en forme de la societe
Definition: vcard.class.php:304
encode
encode($string)
Encode a string for vCard.
Definition: vcard.class.php:32