dolibarr  19.0.0-dev
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 == "PREF") {
270  $key .= ";PREF=1";
271  } elseif (!empty($type)) {
272  $key .= ";TYPE=".dol_strtolower($type);
273  }
274  $this->properties[$key] = $address;
275  }
276 
283  public function setNote($note)
284  {
285  $this->properties["NOTE;".$this->encoding] = encode($note);
286  }
287 
294  public function setTitle($title)
295  {
296  $this->properties["TITLE;".$this->encoding] = encode($title);
297  }
298 
299 
306  public function setOrg($org)
307  {
308  $this->properties["ORG;".$this->encoding] = encode($org);
309  }
310 
311 
318  public function setProdId($prodid)
319  {
320  $this->properties["PRODID"] = encode($prodid);
321  }
322 
323 
330  public function setUID($uid)
331  {
332  $this->properties["UID"] = encode($uid);
333  }
334 
335 
343  public function setURL($url, $type = "")
344  {
345  // $type may be WORK | HOME
346  $key = "URL";
347  if ($type != "") {
348  $key .= ";$type";
349  }
350  $this->properties[$key] = $url;
351  }
352 
358  public function getVCard()
359  {
360  $text = "BEGIN:VCARD\r\n";
361  $text .= "VERSION:4.0\r\n"; // With V4, all encoding are UTF-8
362  //$text.= "VERSION:2.1\r\n";
363  foreach ($this->properties as $key => $value) {
364  $newkey = preg_replace('/-.*$/', '', $key); // remove suffix -twitter, -facebook, ...
365  $text .= $newkey.":".$value."\r\n";
366  }
367  $text .= "REV:".date("Ymd")."T".date("His")."Z\r\n";
368  //$text .= "MAILER: Dolibarr\r\n";
369  $text .= "END:VCARD\r\n";
370  return $text;
371  }
372 
378  public function getFileName()
379  {
380  return $this->filename;
381  }
382 
393  public function buildVCardString($object, $company, $langs, $urlphoto = '')
394  {
395  global $dolibarr_main_instance_unique_id;
396 
397  $this->setProdId('Dolibarr '.DOL_VERSION);
398 
399  $this->setUid('DOLIBARR-USERID-'.dol_trunc(md5('vcard'.$dolibarr_main_instance_unique_id), 8, 'right', 'UTF-8', 1).'-'.$object->id);
400  $this->setName($object->lastname, $object->firstname, "", $object->civility_code, "");
401  $this->setFormattedName($object->getFullName($langs, 1));
402 
403  if ($urlphoto) {
404  $mimetype = dol_mimetype($urlphoto);
405  if ($mimetype) {
406  $this->setPhoto($mimetype, $urlphoto);
407  }
408  }
409 
410  if ($object->office_phone) {
411  $this->setPhoneNumber($object->office_phone, "TYPE=WORK,VOICE");
412  }
413  /* disabled
414  if ($object->personal_mobile) {
415  $this->setPhoneNumber($object->personal_mobile, "TYPE=CELL,VOICE");
416  }*/
417  if ($object->user_mobile) {
418  $this->setPhoneNumber($object->user_mobile, "TYPE=CELL,VOICE");
419  }
420  if ($object->office_fax) {
421  $this->setPhoneNumber($object->office_fax, "TYPE=WORK,FAX");
422  }
423 
424  if (!empty($object->socialnetworks)) {
425  foreach ($object->socialnetworks as $key => $val) {
426  if (empty($val)) { // Disacard social network if empty
427  continue;
428  }
429  $urlsn = '';
430  if ($key == 'linkedin') {
431  if (!preg_match('/^http/', $val)) {
432  $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
433  } else {
434  $urlsn = $val;
435  }
436  } elseif ($key == 'youtube') {
437  if (!preg_match('/^http/', $val)) {
438  $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
439  } else {
440  $urlsn = $val;
441  }
442  } else {
443  if (!preg_match('/^http/', $val)) {
444  $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
445  } else {
446  $urlsn = $val;
447  }
448  }
449  if ($urlsn) {
450  $this->properties["SOCIALPROFILE;TYPE=WORK-".$key] = $key.':'.$urlsn;
451  }
452  }
453  }
454 
455  $country = $object->country_code ? $object->country : '';
456 
457  if ($object->address || $object->town || $object->state || $object->zip || $object->country) {
458  $this->setAddress("", "", $object->address, $object->town, $object->state, $object->zip, $country, "");
459  //$this->setLabel("", "", $object->address, $object->town, $object->state, $object->zip, $country, "TYPE=HOME");
460  }
461 
462  if ($object->email) {
463  $this->setEmail($object->email, "TYPE=WORK");
464  }
465  /* disabled
466  if ($object->personal_email) {
467  $this->setEmail($object->personal_email, "TYPE=HOME");
468  } */
469  if ($object->note_public) {
470  $this->setNote($object->note_public);
471  }
472  if ($object->job) {
473  $this->setTitle($object->job);
474  }
475 
476  // For user, $object->url is not defined
477  // For contact, $object->url is not defined
478  if (!empty($object->url)) {
479  $this->setURL($object->url, "");
480  }
481 
482  if (is_object($company)) {
483  // Si user linked to a thirdparty and not a physical people
484  if ($company->typent_code != 'TE_PRIVATE') {
485  $this->setOrg($company->name);
486  }
487 
488  $this->setURL($company->url, "");
489 
490  if ($company->phone && $company->phone != $object->office_phone) {
491  $this->setPhoneNumber($company->phone, "TYPE=WORK,VOICE");
492  }
493  if ($company->fax && $company->fax != $object->office_fax) {
494  $this->setPhoneNumber($company->fax, "TYPE=WORK,FAX");
495  }
496  if ($company->address || $company->town || $company->state || $company->zip || $company->country) {
497  $this->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK");
498  }
499 
500  if ($company->email && $company->email != $object->email) {
501  $this->setEmail($company->email, "TYPE=WORK");
502  }
503 
504  /*
505  if (!empty($company->socialnetworks)) {
506  foreach ($company->socialnetworks as $key => $val) {
507  $urlsn = '';
508  if ($key == 'linkedin') {
509  if (!preg_match('/^http/', $val)) {
510  $urlsn = 'https://www.'.$key.'.com/company/'.urlencode($val);
511  } else {
512  $urlsn = $val;
513  }
514  } elseif ($key == 'youtube') {
515  if (!preg_match('/^http/', $val)) {
516  $urlsn = 'https://www.'.$key.'.com/user/'.urlencode($val);
517  } else {
518  $urlsn = $val;
519  }
520  } else {
521  if (!preg_match('/^http/', $val)) {
522  $urlsn = 'https://www.'.$key.'.com/'.urlencode($val);
523  } else {
524  $urlsn = $val;
525  }
526  }
527  if ($urlsn) {
528  $this->properties["socialProfile;type=".$key] = $urlsn;
529  }
530  }
531  }
532  */
533  }
534 
535  // Birthday
536  if ($object->birth) {
537  $this->setBirthday($object->birth);
538  }
539 
540  // Return VCard string
541  return $this->getVCard();
542  }
543 
544 
545  /* Example from Microsoft Outlook 2019
546 
547  BEGIN:VCARD
548  VERSION:2.1
549 
550  N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
551  FN:Sir surename secondname forename jun.
552  ORG:Companyname
553  TITLE:position
554  TEL;WORK;VOICE:work-phone-number
555  TEL;HOME;VOICE:private-phone-number
556  TEL;CELL;VOICE:mobile-phone-number
557  TEL;WORK;FAX:fax-phone-number
558  ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
559  LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
560  =0D=0A=
561  012345 town region
562  X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
563  URL;WORK:www.mywebpage.de
564  EMAIL;PREF;INTERNET:test1@test1.de
565  EMAIL;INTERNET:test2@test2.de
566  EMAIL;INTERNET:test3@test3.de
567  X-MS-IMADDRESS:test@jabber.org
568  REV:20200424T104242Z
569 
570  END:VCARD
571  */
572 }
Class to buld vCard files.
Definition: vcard.class.php:89
setEmail($address, $type="")
Add a e-mail address to this vCard.
setTitle($title)
mise en forme de la fonction
setPhoneNumber($number, $type="")
mise en forme du numero de telephone
setPhoto($type, $photo)
mise en forme de la photo warning NON TESTE !
setBirthday($date)
mise en forme de l'anniversaire
setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME")
Address (old standard)
setUID($uid)
mise en forme du logiciel generateur
getFileName()
permet d'obtenir le nom de fichier
setOrg($org)
mise en forme de la societe
setProdId($prodid)
mise en forme du logiciel generateur
setFormattedName($name)
mise en forme du nom formate
setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="", $label="")
Address.
setName($family="", $first="", $additional="", $prefix="", $suffix="")
mise en forme du nom complet
getVCard()
permet d'obtenir une vcard
buildVCardString($object, $company, $langs, $urlphoto='')
Return a VCARD string See RFC https://datatracker.ietf.org/doc/html/rfc6350.
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.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $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.
Definition: vcard.class.php:32
dol_quoted_printable_encode($input, $line_max=76)
Taken from php documentation comments No more used.
Definition: vcard.class.php:46