dolibarr  16.0.5
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  $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 = "ISO-8859-1;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 .= ";CHARSET=".$this->encoding;
121  $this->properties[$key] = encode($number);
122  }
123 
132  public function setPhoto($type, $photo)
133  {
134  // $type = "GIF" | "JPEG"
135  $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
136  }
137 
144  public function setFormattedName($name)
145  {
146  $this->properties["FN;CHARSET=".$this->encoding] = encode($name);
147  }
148 
159  public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
160  {
161  $this->properties["N;CHARSET=".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
162  $this->filename = "$first%20$family.vcf";
163  if (empty($this->properties["FN"])) {
164  $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
165  }
166  }
167 
174  public function setBirthday($date)
175  {
176  // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426
177  $this->properties["BDAY"] = dol_print_date($date, 'dayrfc');
178  }
179 
193  public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME;POSTAL")
194  {
195  // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
196  $key = "ADR";
197  if ($type != "") {
198  $key .= ";".$type;
199  }
200  $key .= ";CHARSET=".$this->encoding;
201  $this->properties[$key] = ";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
202 
203  //if ($this->properties["LABEL;".$type.";CHARSET=".$this->encoding] == '') {
204  //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
205  //}
206  }
207 
221  public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME;POSTAL")
222  {
223  $label = "";
224  if ($postoffice != "") {
225  $label .= "$postoffice\r\n";
226  }
227  if ($extended != "") {
228  $label .= "$extended\r\n";
229  }
230  if ($street != "") {
231  $label .= "$street\r\n";
232  }
233  if ($zip != "") {
234  $label .= "$zip ";
235  }
236  if ($city != "") {
237  $label .= "$city\r\n";
238  }
239  if ($region != "") {
240  $label .= "$region\r\n";
241  }
242  if ($country != "") {
243  $country .= "$country\r\n";
244  }
245 
246  $this->properties["LABEL;$type;CHARSET=".$this->encoding] = encode($label);
247  }
248 
256  public function setEmail($address, $type = "TYPE=INTERNET;PREF")
257  {
258  $key = "EMAIL";
259  if ($type != "") {
260  $key .= ";".$type;
261  }
262  $this->properties[$key] = $address;
263  }
264 
271  public function setNote($note)
272  {
273  $this->properties["NOTE;CHARSET=".$this->encoding] = encode($note);
274  }
275 
282  public function setTitle($title)
283  {
284  $this->properties["TITLE;CHARSET=".$this->encoding] = encode($title);
285  }
286 
287 
294  public function setOrg($org)
295  {
296  $this->properties["ORG;CHARSET=".$this->encoding] = encode($org);
297  }
298 
299 
306  public function setProdId($prodid)
307  {
308  $this->properties["PRODID;CHARSET=".$this->encoding] = encode($prodid);
309  }
310 
311 
318  public function setUID($uid)
319  {
320  $this->properties["UID;CHARSET=".$this->encoding] = encode($uid);
321  }
322 
323 
331  public function setURL($url, $type = "")
332  {
333  // $type may be WORK | HOME
334  $key = "URL";
335  if ($type != "") {
336  $key .= ";$type";
337  }
338  $this->properties[$key] = $url;
339  }
340 
346  public function getVCard()
347  {
348  $text = "BEGIN:VCARD\r\n";
349  $text .= "VERSION:3.0\r\n";
350  //$text.= "VERSION:2.1\r\n";
351  foreach ($this->properties as $key => $value) {
352  $text .= "$key:$value\r\n";
353  }
354  $text .= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
355  $text .= "MAILER: Dolibarr\r\n";
356  $text .= "END:VCARD\r\n";
357  return $text;
358  }
359 
365  public function getFileName()
366  {
367  return $this->filename;
368  }
369 
370  /* Example from Microsoft Outlook 2019
371 
372  BEGIN:VCARD
373  VERSION:2.1
374 
375  N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
376  FN:Sir surename secondname forename jun.
377  ORG:Companyname
378  TITLE:position
379  TEL;WORK;VOICE:work-phone-number
380  TEL;HOME;VOICE:private-phone-number
381  TEL;CELL;VOICE:mobile-phone-number
382  TEL;WORK;FAX:fax-phone-number
383  ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
384  LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
385  =0D=0A=
386  012345 town region
387  X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
388  URL;WORK:www.mywebpage.de
389  EMAIL;PREF;INTERNET:test1@test1.de
390  EMAIL;INTERNET:test2@test2.de
391  EMAIL;INTERNET:test3@test3.de
392  X-MS-IMADDRESS:test@jabber.org
393  REV:20200424T104242Z
394 
395  END:VCARD
396  */
397 }
vCard\setTitle
setTitle($title)
mise en forme de la fonction
Definition: vcard.class.php:282
vCard\setPhoto
setPhoto($type, $photo)
mise en forme de la photo warning NON TESTE !
Definition: vcard.class.php:132
vCard\setNote
setNote($note)
mise en forme de la note
Definition: vcard.class.php:271
vCard\setBirthday
setBirthday($date)
mise en forme de l'anniversaire
Definition: vcard.class.php:174
vCard
Class to buld vCard files.
Definition: vcard.class.php:88
vCard\setLabel
setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
mise en forme du label
Definition: vcard.class.php:221
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\setEmail
setEmail($address, $type="TYPE=INTERNET;PREF")
Add a e-mail address to this vCard.
Definition: vcard.class.php:256
vCard\setName
setName($family="", $first="", $additional="", $prefix="", $suffix="")
mise en forme du nom complet
Definition: vcard.class.php:159
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:2514
vCard\setProdId
setProdId($prodid)
mise en forme du logiciel generateur
Definition: vcard.class.php:306
vCard\setURL
setURL($url, $type="")
mise en forme de l'url
Definition: vcard.class.php:331
vCard\setAddress
setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
mise en forme de l'adresse
Definition: vcard.class.php:193
vCard\setUID
setUID($uid)
mise en forme du logiciel generateur
Definition: vcard.class.php:318
vCard\getVCard
getVCard()
permet d'obtenir une vcard
Definition: vcard.class.php:346
vCard\setFormattedName
setFormattedName($name)
mise en forme du nom formate
Definition: vcard.class.php:144
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:365
vCard\setOrg
setOrg($org)
mise en forme de la societe
Definition: vcard.class.php:294
encode
encode($string)
Encode a string for vCard.
Definition: vcard.class.php:32