vcard.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /* Copyright (C) Kai Blankenhorn <kaib@bitfolge.de>
  3. * Copyright (C) 2005-2017 Laurent Destailleur <eldy@users.sourceforge.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/core/class/vcard.class.php
  20. * \brief Class to manage vCard files
  21. */
  22. /**
  23. * Encode a string for vCard
  24. *
  25. * @param string $string String to encode
  26. * @return string String encoded
  27. */
  28. function encode($string)
  29. {
  30. return str_replace(";","\;",(dol_quoted_printable_encode(utf8_decode($string))));
  31. }
  32. /**
  33. * Taken from php documentation comments
  34. * No more used
  35. *
  36. * @param string $input String
  37. * @param int $line_max Max length of lines
  38. * @return string Encoded string
  39. */
  40. function dol_quoted_printable_encode($input, $line_max=76)
  41. {
  42. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  43. $lines = preg_split("/(\?:\r\n|\r|\n)/", $input);
  44. $eol = "\r\n";
  45. $linebreak = "=0D=0A";
  46. $escape = "=";
  47. $output = "";
  48. $num = count($lines);
  49. for ($j = 0; $j < $num; $j++)
  50. {
  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); $h1 = floor($dec%16);
  61. $c = $escape.$hex["$h2"].$hex["$h1"];
  62. }
  63. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  64. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  65. $newline = " ";
  66. }
  67. $newline .= $c;
  68. } // end of for
  69. $output .= $newline;
  70. if ($j<count($lines)-1) $output .= $linebreak;
  71. }
  72. return trim($output);
  73. }
  74. /**
  75. * Class to buld vCard files
  76. */
  77. class vCard
  78. {
  79. var $properties;
  80. var $filename;
  81. //var $encoding="UTF-8";
  82. var $encoding="ISO-8859-1;ENCODING=QUOTED-PRINTABLE";
  83. /**
  84. * mise en forme du numero de telephone
  85. *
  86. * @param int $number numero de telephone
  87. * @param string $type Type
  88. * @return void
  89. */
  90. function setPhoneNumber($number, $type="")
  91. {
  92. // 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"
  93. $key = "TEL";
  94. if ($type!="") $key .= ";".$type;
  95. $key.= ";CHARSET=".$this->encoding;
  96. $this->properties[$key] = encode($number);
  97. }
  98. /**
  99. * mise en forme de la photo
  100. * warning NON TESTE !
  101. *
  102. * @param string $type Type
  103. * @param string $photo Photo
  104. * @return void
  105. */
  106. function setPhoto($type, $photo)
  107. { // $type = "GIF" | "JPEG"
  108. $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
  109. }
  110. /**
  111. * mise en forme du nom formate
  112. *
  113. * @param string $name Name
  114. * @return void
  115. */
  116. function setFormattedName($name)
  117. {
  118. $this->properties["FN;CHARSET=".$this->encoding] = encode($name);
  119. }
  120. /**
  121. * mise en forme du nom complet
  122. *
  123. * @param string $family Family
  124. * @param string $first First
  125. * @param string $additional Additionnal
  126. * @param string $prefix Prefix
  127. * @param string $suffix Suffix
  128. * @return void
  129. */
  130. function setName($family="", $first="", $additional="", $prefix="", $suffix="")
  131. {
  132. $this->properties["N;CHARSET=".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
  133. $this->filename = "$first%20$family.vcf";
  134. if (empty($this->properties["FN"])) $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
  135. }
  136. /**
  137. * mise en forme de l'anniversaire
  138. *
  139. * @param timestamp $date Date
  140. * @return void
  141. */
  142. function setBirthday($date)
  143. {
  144. // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426
  145. $this->properties["BDAY"] = dol_print_date($date, 'dayrfc');
  146. }
  147. /**
  148. * mise en forme de l'adresse
  149. *
  150. * @param string $postoffice Postoffice
  151. * @param string $extended Extended
  152. * @param string $street Street
  153. * @param string $city City
  154. * @param string $region Region
  155. * @param string $zip Zip
  156. * @param string $country Country
  157. * @param string $type Type
  158. * @return void
  159. */
  160. function setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
  161. {
  162. // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
  163. $key = "ADR";
  164. if ($type!="") $key.= ";$type";
  165. $key.= ";CHARSET=".$this->encoding;
  166. $this->properties[$key] = ";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
  167. if ($this->properties["LABEL;$type;CHARSET=".$this->encoding] == "")
  168. {
  169. //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
  170. }
  171. }
  172. /**
  173. * mise en forme du label
  174. *
  175. * @param string $postoffice Postoffice
  176. * @param string $extended Extended
  177. * @param string $street Street
  178. * @param string $city City
  179. * @param string $region Region
  180. * @param string $zip Zip
  181. * @param string $country Country
  182. * @param string $type Type
  183. * @return void
  184. */
  185. function setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL") {
  186. $label = "";
  187. if ($postoffice!="") $label.= "$postoffice\r\n";
  188. if ($extended!="") $label.= "$extended\r\n";
  189. if ($street!="") $label.= "$street\r\n";
  190. if ($zip!="") $label.= "$zip ";
  191. if ($city!="") $label.= "$city\r\n";
  192. if ($region!="") $label.= "$region\r\n";
  193. if ($country!="") $country.= "$country\r\n";
  194. $this->properties["LABEL;$type;CHARSET=".$this->encoding] = encode($label);
  195. }
  196. /**
  197. * mise en forme de l'email
  198. *
  199. * @param string $address EMail
  200. * @param string $type Vcard type
  201. * @return void
  202. */
  203. function setEmail($address,$type="internet,pref")
  204. {
  205. $this->properties["EMAIL;TYPE=".$type] = $address;
  206. }
  207. /**
  208. * mise en forme de la note
  209. *
  210. * @param string $note Note
  211. * @return void
  212. */
  213. function setNote($note)
  214. {
  215. $this->properties["NOTE;CHARSET=".$this->encoding] = encode($note);
  216. }
  217. /**
  218. * mise en forme de la fonction
  219. *
  220. * @param string $title Title
  221. * @return void
  222. */
  223. function setTitle($title)
  224. {
  225. $this->properties["TITLE;CHARSET=".$this->encoding] = encode($title);
  226. }
  227. /**
  228. * mise en forme de la societe
  229. *
  230. * @param string $org Org
  231. * @return void
  232. */
  233. function setOrg($org)
  234. {
  235. $this->properties["ORG;CHARSET=".$this->encoding] = encode($org);
  236. }
  237. /**
  238. * mise en forme du logiciel generateur
  239. *
  240. * @param string $prodid Prodid
  241. * @return void
  242. */
  243. function setProdId($prodid)
  244. {
  245. $this->properties["PRODID;CHARSET=".$this->encoding] = encode($prodid);
  246. }
  247. /**
  248. * mise en forme du logiciel generateur
  249. *
  250. * @param string $uid Uid
  251. * @return void
  252. */
  253. function setUID($uid)
  254. {
  255. $this->properties["UID;CHARSET=".$this->encoding] = encode($uid);
  256. }
  257. /**
  258. * mise en forme de l'url
  259. *
  260. * @param string $url URL
  261. * @param string $type Type
  262. * @return void
  263. */
  264. function setURL($url, $type="")
  265. {
  266. // $type may be WORK | HOME
  267. $key = "URL";
  268. if ($type!="") $key.= ";$type";
  269. $this->properties[$key] = $url;
  270. }
  271. /**
  272. * permet d'obtenir une vcard
  273. *
  274. * @return string
  275. */
  276. function getVCard()
  277. {
  278. $text = "BEGIN:VCARD\r\n";
  279. //$text.= "VERSION:3.0\r\n";
  280. $text.= "VERSION:2.1\r\n";
  281. foreach($this->properties as $key => $value)
  282. {
  283. $text.= "$key:$value\r\n";
  284. }
  285. $text.= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
  286. $text.= "MAILER: Dolibarr\r\n";
  287. $text.= "END:VCARD\r\n";
  288. return $text;
  289. }
  290. /**
  291. * permet d'obtenir le nom de fichier
  292. *
  293. * @return string Filename
  294. */
  295. function getFileName()
  296. {
  297. return $this->filename;
  298. }
  299. }