Download vCard Script


/ Published in: PHP
Save to your folder(s)

Features: Complies to version 2.1 of the vCard specification
Supports the following attributes:

* Name
* Formatted Name
* Phone and fax numbers
* Birthday
* Address
* Address label
* email address
* notes
* URL


Copy this code and paste it in your HTML
  1. <?
  2. /***************************************************************************
  3.  
  4. PHP vCard class v2.0
  5. (c) Kai Blankenhorn
  6. www.bitfolge.de/en
  7.  
  8.  
  9. This program is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU General Public License
  11. as published by the Free Software Foundation; either version 2
  12. of the License, or (at your option) any later version.
  13.  
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18.  
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22.  
  23. ***************************************************************************/
  24.  
  25.  
  26. function encode($string) {
  27. return escape(quoted_printable_encode($string));
  28. }
  29.  
  30. function escape($string) {
  31. return str_replace(";","\;",$string);
  32. }
  33.  
  34. // taken from PHP documentation comments
  35. function quoted_printable_encode($input, $line_max = 76) {
  36. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  37. $lines = preg_split("/(?:
  38. |\r|\n)/", $input);
  39. $eol = "
  40. ";
  41. $linebreak = "=0D=0A";
  42. $escape = "=";
  43. $output = "";
  44.  
  45. for ($j=0;$j<count($lines);$j++) {
  46. $line = $lines[$j];
  47. $linlen = strlen($line);
  48. $newline = "";
  49. for($i = 0; $i < $linlen; $i++) {
  50. $c = substr($line, $i, 1);
  51. $dec = ord($c);
  52. if ( ($dec == 32) && ($i == ($linlen - 1)) ) { // convert space at eol only
  53. $c = "=20";
  54. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  55. $h2 = floor($dec/16); $h1 = floor($dec%16);
  56. $c = $escape.$hex["$h2"].$hex["$h1"];
  57. }
  58. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  59. $output .= $newline.$escape.$eol; // soft line break; " =
  60. " is okay
  61. $newline = " ";
  62. }
  63. $newline .= $c;
  64. } // end of for
  65. $output .= $newline;
  66. if ($j<count($lines)-1) $output .= $linebreak;
  67. }
  68. return trim($output);
  69. }
  70.  
  71. class vCard {
  72. var $properties;
  73. var $filename;
  74.  
  75. function setPhoneNumber($number, $type="") {
  76. // 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"
  77. $key = "TEL";
  78. if ($type!="") $key .= ";".$type;
  79. $key.= ";ENCODING=QUOTED-PRINTABLE";
  80. $this->properties[$key] = quoted_printable_encode($number);
  81. }
  82.  
  83. // UNTESTED !!!
  84. function setPhoto($type, $photo) { // $type = "GIF" | "JPEG"
  85. $this->properties["PHOTO;TYPE=$type;ENCODING=BASE64"] = base64_encode($photo);
  86. }
  87.  
  88. function setFormattedName($name) {
  89. $this->properties["FN"] = quoted_printable_encode($name);
  90. }
  91.  
  92. function setName($family="", $first="", $additional="", $prefix="", $suffix="") {
  93. $this->properties["N"] = "$family;$first;$additional;$prefix;$suffix";
  94. $this->filename = "$first%20$family.vcf";
  95. if ($this->properties["FN"]=="") $this->setFormattedName(trim("$prefix $first $additional $family $suffix"));
  96. }
  97.  
  98. function setBirthday($date) { // $date format is YYYY-MM-DD
  99. $this->properties["BDAY"] = $date;
  100. }
  101.  
  102. function setAddress($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL") {
  103. // $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
  104. $key = "ADR";
  105. if ($type!="") $key.= ";$type";
  106. $key.= ";ENCODING=QUOTED-PRINTABLE";
  107. $this->properties[$key] = encode($name).";".encode($extended).";".encode($street).";".encode($city).";".encode($region).";".encode($zip).";".encode($country);
  108.  
  109. if ($this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"] == "") {
  110. //$this->setLabel($postoffice, $extended, $street, $city, $region, $zip, $country, $type);
  111. }
  112. }
  113.  
  114. function setLabel($postoffice="", $extended="", $street="", $city="", $region="", $zip="", $country="", $type="HOME;POSTAL") {
  115. $label = "";
  116. if ($postoffice!="") $label.= "$postoffice
  117. ";
  118. if ($extended!="") $label.= "$extended
  119. ";
  120. if ($street!="") $label.= "$street
  121. ";
  122. if ($zip!="") $label.= "$zip ";
  123. if ($city!="") $label.= "$city
  124. ";
  125. if ($region!="") $label.= "$region
  126. ";
  127. if ($country!="") $country.= "$country
  128. ";
  129.  
  130. $this->properties["LABEL;$type;ENCODING=QUOTED-PRINTABLE"] = quoted_printable_encode($label);
  131. }
  132.  
  133. function setEmail($address) {
  134. $this->properties["EMAIL;INTERNET"] = $address;
  135. }
  136.  
  137. function setNote($note) {
  138. $this->properties["NOTE;ENCODING=QUOTED-PRINTABLE"] = quoted_printable_encode($note);
  139. }
  140.  
  141. function setURL($url, $type="") {
  142. // $type may be WORK | HOME
  143. $key = "URL";
  144. if ($type!="") $key.= ";$type";
  145. $this->properties[$key] = $url;
  146. }
  147.  
  148. function getVCard() {
  149. $text = "BEGIN:VCARD
  150. ";
  151. $text.= "VERSION:2.1
  152. ";
  153. foreach($this->properties as $key => $value) {
  154. $text.= "$key:$value
  155. ";
  156. }
  157. $text.= "REV:".date("Y-m-d")."T".date("H:i:s")."Z
  158. ";
  159. $text.= "MAILER:PHP vCard class by Kai Blankenhorn
  160. ";
  161. $text.= "END:VCARD
  162. ";
  163. return $text;
  164. }
  165.  
  166. function getFileName() {
  167. return $this->filename;
  168. }
  169. }
  170.  
  171.  
  172. // USAGE EXAMPLE
  173.  
  174. $v = new vCard();
  175.  
  176. $v->setPhoneNumber("+49 23 456789", "PREF;HOME;VOICE");
  177. $v->setName("Mustermann", "Thomas", "", "Herr");
  178. $v->setBirthday("1960-07-31");
  179. $v->setAddress("", "", "Musterstrasse 20", "Musterstadt", "", "98765", "Deutschland");
  180. $v->setEmail("thomas.mustermann@thomas-mustermann.de");
  181. $v->setNote("You can take some notes here.
  182. Multiple lines are supported via \\r\\n.");
  183. $v->setURL("http://www.thomas-mustermann.de", "WORK");
  184.  
  185. $output = $v->getVCard();
  186. $filename = $v->getFileName();
  187.  
  188. Header("Content-Disposition: attachment; filename=$filename");
  189. Header("Content-Length: ".strlen($output));
  190. Header("Connection: close");
  191. Header("Content-Type: text/x-vCard; name=$filename");
  192.  
  193. echo $output;
  194. ?>

URL: http://www.bitfolge.de/phpvcard-en.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.