vcard.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. /**
  20. * \file htdocs/core/class/vcard.class.php
  21. * \brief Class to manage vCard files
  22. */
  23. /**
  24. * Encode a string for vCard
  25. *
  26. * @param string $string String to encode
  27. * @return string String encoded
  28. */
  29. function encode($string)
  30. {
  31. return str_replace(";", "\;", (dol_quoted_printable_encode(utf8_decode($string))));
  32. }
  33. /**
  34. * Taken from php documentation comments
  35. * No more used
  36. *
  37. * @param string $input String
  38. * @param int $line_max Max length of lines
  39. * @return string Encoded string
  40. */
  41. function dol_quoted_printable_encode($input, $line_max = 76)
  42. {
  43. $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
  44. $lines = preg_split("/(\?:\r\n|\r|\n)/", $input);
  45. $eol = "\r\n";
  46. $linebreak = "=0D=0A";
  47. $escape = "=";
  48. $output = "";
  49. $num = count($lines);
  50. for ($j = 0; $j < $num; $j++)
  51. {
  52. $line = $lines[$j];
  53. $linlen = strlen($line);
  54. $newline = "";
  55. for ($i = 0; $i < $linlen; $i++) {
  56. $c = substr($line, $i, 1);
  57. $dec = ord($c);
  58. if (($dec == 32) && ($i == ($linlen - 1))) { // convert space at eol only
  59. $c = "=20";
  60. } elseif (($dec == 61) || ($dec < 32) || ($dec > 126)) { // always encode "\t", which is *not* required
  61. $h2 = floor($dec / 16); $h1 = floor($dec % 16);
  62. $c = $escape.$hex["$h2"].$hex["$h1"];
  63. }
  64. if ((strlen($newline) + strlen($c)) >= $line_max) { // CRLF is not counted
  65. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  66. $newline = " ";
  67. }
  68. $newline .= $c;
  69. } // end of for
  70. $output .= $newline;
  71. if ($j < count($lines) - 1) $output .= $linebreak;
  72. }
  73. return trim($output);
  74. }
  75. /**
  76. * Class to buld vCard files
  77. */
  78. class vCard
  79. {
  80. public $properties;
  81. public $filename;
  82. //var $encoding="UTF-8";
  83. public $encoding = "ISO-8859-1;ENCODING=QUOTED-PRINTABLE";
  84. /**
  85. * mise en forme du numero de telephone
  86. *
  87. * @param int $number numero de telephone
  88. * @param string $type Type
  89. * @return void
  90. */
  91. public function setPhoneNumber($number, $type = "")
  92. {
  93. // 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"
  94. $key = "TEL";
  95. if ($type != "") $key .= ";".$type;
  96. $key .= ";CHARSET=".$this->encoding;
  97. $this->properties[$key] = encode($number);
  98. }
  99. /**
  100. * mise en forme de la photo
  101. * warning NON TESTE !
  102. *
  103. * @param string $type Type
  104. * @param string $photo Photo
  105. * @return void
  106. */
  107. public function setPhoto($type, $photo)
  108. {
  109. // $type = "GIF" | "JPEG"
  110. $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
  111. }
  112. /**
  113. * mise en forme du nom formate
  114. *
  115. * @param string $name Name
  116. * @return void
  117. */
  118. public function setFormattedName($name)
  119. {
  120. $this->properties["FN;CHARSET=".$this->encoding] = encode($name);
  121. }
  122. /**
  123. * mise en forme du nom complet
  124. *
  125. * @param string $family Family name
  126. * @param string $first First name
  127. * @param string $additional Additional (e.g. second name, nick name)
  128. * @param string $prefix Prefix (e.g. "Mr.", "Ms.", "Prof.")
  129. * @param string $suffix Suffix (e.g. "sen." for senior, "jun." for junior)
  130. * @return void
  131. */
  132. public function setName($family = "", $first = "", $additional = "", $prefix = "", $suffix = "")
  133. {
  134. $this->properties["N;CHARSET=".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
  135. $this->filename = "$first%20$family.vcf";
  136. if (empty($this->properties["FN"])) $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
  137. }
  138. /**
  139. * mise en forme de l'anniversaire
  140. *
  141. * @param integer $date Date
  142. * @return void
  143. */
  144. public function setBirthday($date)
  145. {
  146. // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426
  147. $this->properties["BDAY"] = dol_print_date($date, 'dayrfc');
  148. }
  149. /**
  150. * mise en forme de l'adresse
  151. *
  152. * @param string $postoffice Postoffice
  153. * @param string $extended Extended
  154. * @param string $street Street
  155. * @param string $city City
  156. * @param string $region Region
  157. * @param string $zip Zip
  158. * @param string $country Country
  159. * @param string $type Type
  160. * @return void
  161. */
  162. public function setAddress($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME;POSTAL")
  163. {
  164. // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
  165. $key = "ADR";
  166. if ($type != "") $key .= ";$type";
  167. $key .= ";CHARSET=".$this->encoding;
  168. $this->properties[$key] = ";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
  169. if ($this->properties["LABEL;$type;CHARSET=".$this->encoding] == "")
  170. {
  171. //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
  172. }
  173. }
  174. /**
  175. * mise en forme du label
  176. *
  177. * @param string $postoffice Postoffice
  178. * @param string $extended Extended
  179. * @param string $street Street
  180. * @param string $city City
  181. * @param string $region Region
  182. * @param string $zip Zip
  183. * @param string $country Country
  184. * @param string $type Type
  185. * @return void
  186. */
  187. public function setLabel($postoffice = "", $extended = "", $street = "", $city = "", $region = "", $zip = "", $country = "", $type = "HOME;POSTAL")
  188. {
  189. $label = "";
  190. if ($postoffice != "") $label .= "$postoffice\r\n";
  191. if ($extended != "") $label .= "$extended\r\n";
  192. if ($street != "") $label .= "$street\r\n";
  193. if ($zip != "") $label .= "$zip ";
  194. if ($city != "") $label .= "$city\r\n";
  195. if ($region != "") $label .= "$region\r\n";
  196. if ($country != "") $country .= "$country\r\n";
  197. $this->properties["LABEL;$type;CHARSET=".$this->encoding] = encode($label);
  198. }
  199. /**
  200. * Add a e-mail address to this vCard
  201. *
  202. * @param string $address E-mail address
  203. * @param string $type (optional) The type of the e-mail (typical "PREF;INTERNET" or "INTERNET")
  204. * @return void
  205. */
  206. public function setEmail($address, $type = "TYPE=INTERNET;PREF")
  207. {
  208. $key = "EMAIL";
  209. if ($type != "") $key .= ";".$type;
  210. $this->properties[$key] = $address;
  211. }
  212. /**
  213. * mise en forme de la note
  214. *
  215. * @param string $note Note
  216. * @return void
  217. */
  218. public function setNote($note)
  219. {
  220. $this->properties["NOTE;CHARSET=".$this->encoding] = encode($note);
  221. }
  222. /**
  223. * mise en forme de la fonction
  224. *
  225. * @param string $title Title
  226. * @return void
  227. */
  228. public function setTitle($title)
  229. {
  230. $this->properties["TITLE;CHARSET=".$this->encoding] = encode($title);
  231. }
  232. /**
  233. * mise en forme de la societe
  234. *
  235. * @param string $org Org
  236. * @return void
  237. */
  238. public function setOrg($org)
  239. {
  240. $this->properties["ORG;CHARSET=".$this->encoding] = encode($org);
  241. }
  242. /**
  243. * mise en forme du logiciel generateur
  244. *
  245. * @param string $prodid Prodid
  246. * @return void
  247. */
  248. public function setProdId($prodid)
  249. {
  250. $this->properties["PRODID;CHARSET=".$this->encoding] = encode($prodid);
  251. }
  252. /**
  253. * mise en forme du logiciel generateur
  254. *
  255. * @param string $uid Uid
  256. * @return void
  257. */
  258. public function setUID($uid)
  259. {
  260. $this->properties["UID;CHARSET=".$this->encoding] = encode($uid);
  261. }
  262. /**
  263. * mise en forme de l'url
  264. *
  265. * @param string $url URL
  266. * @param string $type Type
  267. * @return void
  268. */
  269. public function setURL($url, $type = "")
  270. {
  271. // $type may be WORK | HOME
  272. $key = "URL";
  273. if ($type != "") $key .= ";$type";
  274. $this->properties[$key] = $url;
  275. }
  276. /**
  277. * permet d'obtenir une vcard
  278. *
  279. * @return string
  280. */
  281. public function getVCard()
  282. {
  283. $text = "BEGIN:VCARD\r\n";
  284. $text .= "VERSION:3.0\r\n";
  285. //$text.= "VERSION:2.1\r\n";
  286. foreach ($this->properties as $key => $value)
  287. {
  288. $text .= "$key:$value\r\n";
  289. }
  290. $text .= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
  291. $text .= "MAILER: Dolibarr\r\n";
  292. $text .= "END:VCARD\r\n";
  293. return $text;
  294. }
  295. /**
  296. * permet d'obtenir le nom de fichier
  297. *
  298. * @return string Filename
  299. */
  300. public function getFileName()
  301. {
  302. return $this->filename;
  303. }
  304. /* Example from Microsoft Outlook 2019
  305. BEGIN:VCARD
  306. VERSION:2.1
  307. N;LANGUAGE=de:surename;forename;secondname;Sir;jun.
  308. FN:Sir surename secondname forename jun.
  309. ORG:Companyname
  310. TITLE:position
  311. TEL;WORK;VOICE:work-phone-number
  312. TEL;HOME;VOICE:private-phone-number
  313. TEL;CELL;VOICE:mobile-phone-number
  314. TEL;WORK;FAX:fax-phone-number
  315. ADR;WORK;PREF:;;street and number;town;region;012345;Deutschland
  316. LABEL;WORK;PREF;ENCODING=QUOTED-PRINTABLE:street and number=0D=0A=
  317. =0D=0A=
  318. 012345 town region
  319. X-MS-OL-DEFAULT-POSTAL-ADDRESS:2
  320. URL;WORK:www.mywebpage.de
  321. EMAIL;PREF;INTERNET:test1@test1.de
  322. EMAIL;INTERNET:test2@test2.de
  323. EMAIL;INTERNET:test3@test3.de
  324. X-MS-IMADDRESS:test@jabber.org
  325. REV:20200424T104242Z
  326. END:VCARD
  327. */
  328. }