vcard.class.php 9.7 KB

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