vcard.class.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. {
  108. // $type = "GIF" | "JPEG"
  109. $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
  110. }
  111. /**
  112. * mise en forme du nom formate
  113. *
  114. * @param string $name Name
  115. * @return void
  116. */
  117. function setFormattedName($name)
  118. {
  119. $this->properties["FN;CHARSET=".$this->encoding] = encode($name);
  120. }
  121. /**
  122. * mise en forme du nom complet
  123. *
  124. * @param string $family Family
  125. * @param string $first First
  126. * @param string $additional Additionnal
  127. * @param string $prefix Prefix
  128. * @param string $suffix Suffix
  129. * @return void
  130. */
  131. function setName($family="", $first="", $additional="", $prefix="", $suffix="")
  132. {
  133. $this->properties["N;CHARSET=".$this->encoding] = encode($family).";".encode($first).";".encode($additional).";".encode($prefix).";".encode($suffix);
  134. $this->filename = "$first%20$family.vcf";
  135. if (empty($this->properties["FN"])) $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
  136. }
  137. /**
  138. * mise en forme de l'anniversaire
  139. *
  140. * @param timestamp $date Date
  141. * @return void
  142. */
  143. function setBirthday($date)
  144. {
  145. // $date format is YYYY-MM-DD - RFC 2425 and RFC 2426
  146. $this->properties["BDAY"] = dol_print_date($date, 'dayrfc');
  147. }
  148. /**
  149. * mise en forme de l'adresse
  150. *
  151. * @param string $postoffice Postoffice
  152. * @param string $extended Extended
  153. * @param string $street Street
  154. * @param string $city City
  155. * @param string $region Region
  156. * @param string $zip Zip
  157. * @param string $country Country
  158. * @param string $type Type
  159. * @return void
  160. */
  161. function setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
  162. {
  163. // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
  164. $key = "ADR";
  165. if ($type!="") $key.= ";$type";
  166. $key.= ";CHARSET=".$this->encoding;
  167. $this->properties[$key] = ";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
  168. if ($this->properties["LABEL;$type;CHARSET=".$this->encoding] == "")
  169. {
  170. //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
  171. }
  172. }
  173. /**
  174. * mise en forme du label
  175. *
  176. * @param string $postoffice Postoffice
  177. * @param string $extended Extended
  178. * @param string $street Street
  179. * @param string $city City
  180. * @param string $region Region
  181. * @param string $zip Zip
  182. * @param string $country Country
  183. * @param string $type Type
  184. * @return void
  185. */
  186. function setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL")
  187. {
  188. $label = "";
  189. if ($postoffice!="") $label.= "$postoffice\r\n";
  190. if ($extended!="") $label.= "$extended\r\n";
  191. if ($street!="") $label.= "$street\r\n";
  192. if ($zip!="") $label.= "$zip ";
  193. if ($city!="") $label.= "$city\r\n";
  194. if ($region!="") $label.= "$region\r\n";
  195. if ($country!="") $country.= "$country\r\n";
  196. $this->properties["LABEL;$type;CHARSET=".$this->encoding] = encode($label);
  197. }
  198. /**
  199. * mise en forme de l'email
  200. *
  201. * @param string $address EMail
  202. * @param string $type Vcard type
  203. * @return void
  204. */
  205. function setEmail($address,$type="internet,pref")
  206. {
  207. $this->properties["EMAIL;TYPE=".$type] = $address;
  208. }
  209. /**
  210. * mise en forme de la note
  211. *
  212. * @param string $note Note
  213. * @return void
  214. */
  215. function setNote($note)
  216. {
  217. $this->properties["NOTE;CHARSET=".$this->encoding] = encode($note);
  218. }
  219. /**
  220. * mise en forme de la fonction
  221. *
  222. * @param string $title Title
  223. * @return void
  224. */
  225. function setTitle($title)
  226. {
  227. $this->properties["TITLE;CHARSET=".$this->encoding] = encode($title);
  228. }
  229. /**
  230. * mise en forme de la societe
  231. *
  232. * @param string $org Org
  233. * @return void
  234. */
  235. function setOrg($org)
  236. {
  237. $this->properties["ORG;CHARSET=".$this->encoding] = encode($org);
  238. }
  239. /**
  240. * mise en forme du logiciel generateur
  241. *
  242. * @param string $prodid Prodid
  243. * @return void
  244. */
  245. function setProdId($prodid)
  246. {
  247. $this->properties["PRODID;CHARSET=".$this->encoding] = encode($prodid);
  248. }
  249. /**
  250. * mise en forme du logiciel generateur
  251. *
  252. * @param string $uid Uid
  253. * @return void
  254. */
  255. function setUID($uid)
  256. {
  257. $this->properties["UID;CHARSET=".$this->encoding] = encode($uid);
  258. }
  259. /**
  260. * mise en forme de l'url
  261. *
  262. * @param string $url URL
  263. * @param string $type Type
  264. * @return void
  265. */
  266. function setURL($url, $type="")
  267. {
  268. // $type may be WORK | HOME
  269. $key = "URL";
  270. if ($type!="") $key.= ";$type";
  271. $this->properties[$key] = $url;
  272. }
  273. /**
  274. * permet d'obtenir une vcard
  275. *
  276. * @return string
  277. */
  278. function getVCard()
  279. {
  280. $text = "BEGIN:VCARD\r\n";
  281. //$text.= "VERSION:3.0\r\n";
  282. $text.= "VERSION:2.1\r\n";
  283. foreach($this->properties as $key => $value)
  284. {
  285. $text.= "$key:$value\r\n";
  286. }
  287. $text.= "REV:".date("Y-m-d")."T".date("H:i:s")."Z\r\n";
  288. $text.= "MAILER: Dolibarr\r\n";
  289. $text.= "END:VCARD\r\n";
  290. return $text;
  291. }
  292. /**
  293. * permet d'obtenir le nom de fichier
  294. *
  295. * @return string Filename
  296. */
  297. function getFileName()
  298. {
  299. return $this->filename;
  300. }
  301. }