dolibarr  13.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  *
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(utf8_decode($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  {
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); $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) $output .= $linebreak;
78  }
79  return trim($output);
80 }
81 
82 
86 class vCard
87 {
91  public $properties;
92 
96  public $filename;
97 
101  public $encoding = "ISO-8859-1;ENCODING=QUOTED-PRINTABLE";
102 
103 
111  public function setPhoneNumber($number, $type = "")
112  {
113  // 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"
114  $key = "TEL";
115  if ($type != "") $key .= ";".$type;
116  $key .= ";CHARSET=".$this->encoding;
117  $this->properties[$key] = encode($number);
118  }
119 
128  public function setPhoto($type, $photo)
129  {
130  // $type = "GIF" | "JPEG"
131  $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
132  }
133 
140  public function setFormattedName($name)
141  {
142  $this->properties["FN;CHARSET=".$this->encoding] = encode($name);
143  }
144 
155  public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
156  {
157  $this->properties["N;CHARSET=".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
158  $this->filename = "$first%20$family.vcf";
159  if (empty($this->properties["FN"])) $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
160  }
161 
168  public function setBirthday($date)
169  {
170  // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426
171  $this->properties["BDAY"] = dol_print_date($date, 'dayrfc');
172  }
173 
187  public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME;POSTAL")
188  {
189  // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
190  $key = "ADR";
191  if ($type != "") $key .= ";$type";
192  $key .= ";CHARSET=".$this->encoding;
193  $this->properties[$key] = ";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
194 
195  if ($this->properties["LABEL;$type;CHARSET=".$this->encoding] == "")
196  {
197  //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
198  }
199  }
200 
214  public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME;POSTAL")
215  {
216  $label = "";
217  if ($postoffice != "") $label .= "$postoffice\r\n";
218  if ($extended != "") $label .= "$extended\r\n";
219  if ($street != "") $label .= "$street\r\n";
220  if ($zip != "") $label .= "$zip ";
221  if ($city != "") $label .= "$city\r\n";
222  if ($region != "") $label .= "$region\r\n";
223  if ($country != "") $country .= "$country\r\n";
224 
225  $this->properties["LABEL;$type;CHARSET=".$this->encoding] = encode($label);
226  }
227 
235  public function setEmail($address, $type = "TYPE=INTERNET;PREF")
236  {
237  $key = "EMAIL";
238  if ($type != "") $key .= ";".$type;
239  $this->properties[$key] = $address;
240  }
241 
248  public function setNote($note)
249  {
250  $this->properties["NOTE;CHARSET=".$this->encoding] = encode($note);
251  }
252 
259  public function setTitle($title)
260  {
261  $this->properties["TITLE;CHARSET=".$this->encoding] = encode($title);
262  }
263 
264 
271  public function setOrg($org)
272  {
273  $this->properties["ORG;CHARSET=".$this->encoding] = encode($org);
274  }
275 
276 
283  public function setProdId($prodid)
284  {
285  $this->properties["PRODID;CHARSET=".$this->encoding] = encode($prodid);
286  }
287 
288 
295  public function setUID($uid)
296  {
297  $this->properties["UID;CHARSET=".$this->encoding] = encode($uid);
298  }
299 
300 
308  public function setURL($url, $type = "")
309  {
310  // $type may be WORK | HOME
311  $key = "URL";
312  if ($type != "") $key .= ";$type";
313  $this->properties[$key] = $url;
314  }
315 
321  public function getVCard()
322  {
323  $text = "BEGIN:VCARD\r\n";
324  $text .= "VERSION:3.0\r\n";
325  //$text.= "VERSION:2.1\r\n";
326  foreach ($this->properties as $key => $value)
327  {
328  $text .= "$key:$value\r\n";
329  }
330  $text .= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
331  $text .= "MAILER: Dolibarr\r\n";
332  $text .= "END:VCARD\r\n";
333  return $text;
334  }
335 
341  public function getFileName()
342  {
343  return $this->filename;
344  }
345 
346  /* Example from Microsoft Outlook 2019
347 
348  BEGIN:VCARD
349  VERSION:2.1
350 
351  N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
352  FN:Sir surename secondname forename jun.
353  ORG:Companyname
354  TITLE:position
355  TEL;WORK;VOICE:work-phone-number
356  TEL;HOME;VOICE:private-phone-number
357  TEL;CELL;VOICE:mobile-phone-number
358  TEL;WORK;FAX:fax-phone-number
359  ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
360  LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
361  =0D=0A=
362  012345 town region
363  X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
364  URL;WORK:www.mywebpage.de
365  EMAIL;PREF;INTERNET:test1@test1.de
366  EMAIL;INTERNET:test2@test2.de
367  EMAIL;INTERNET:test3@test3.de
368  X-MS-IMADDRESS:test@jabber.org
369  REV:20200424T104242Z
370 
371  END:VCARD
372  */
373 }
setBirthday($date)
mise en forme de l&#39;anniversaire
dol_quoted_printable_encode($input, $line_max=76)
Taken from php documentation comments No more used.
Definition: vcard.class.php:46
setEmail($address, $type="TYPE=INTERNET;PREF")
Add a e-mail address to this vCard.
setName($family="", $first="", $additional="", $prefix="", $suffix="")
mise en forme du nom complet
setOrg($org)
mise en forme de la societe
encode($string)
Encode a string for vCard.
Definition: vcard.class.php:32
setPhoto($type, $photo)
mise en forme de la photo warning NON TESTE !
Class to buld vCard files.
Definition: vcard.class.php:86
setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
mise en forme du label
getVCard()
permet d&#39;obtenir une vcard
setPhoneNumber($number, $type="")
mise en forme du numero de telephone
setURL($url, $type="")
mise en forme de l&#39;url
setProdId($prodid)
mise en forme du logiciel generateur
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
getFileName()
permet d&#39;obtenir le nom de fichier
setTitle($title)
mise en forme de la fonction
setUID($uid)
mise en forme du logiciel generateur
setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
mise en forme de l&#39;adresse
setFormattedName($name)
mise en forme du nom formate
setNote($note)
mise en forme de la note