PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/htdocs/core/class/vcard.class.php

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