vcard.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /*
  3. * Filename.......: class_vcard.php
  4. * Author.........: Troy Wolf [[email protected]]
  5. * Last Modified..: 2005/07/14 13:30:00
  6. * Description....: A class to generate vCards for contact data.
  7. */
  8. class vcard {
  9. var $log;
  10. var $data; //array of this vcard's contact data
  11. var $filename; //filename for download file naming
  12. var $class; //PUBLIC, PRIVATE, CONFIDENTIAL
  13. var $revision_date;
  14. var $card;
  15. /**
  16. * Called when the object is created
  17. */
  18. public function __construct() {
  19. $this->log = "New vcard() called<br />";
  20. $this->data = array(
  21. "display_name"=>null
  22. ,"first_name"=>null
  23. ,"last_name"=>null
  24. ,"additional_name"=>null
  25. ,"name_prefix"=>null
  26. ,"name_suffix"=>null
  27. ,"nickname"=>null
  28. ,"title"=>null
  29. ,"role"=>null
  30. ,"department"=>null
  31. ,"company"=>null
  32. ,"work_po_box"=>null
  33. ,"work_extended_address"=>null
  34. ,"work_address"=>null
  35. ,"work_city"=>null
  36. ,"work_state"=>null
  37. ,"work_postal_code"=>null
  38. ,"work_country"=>null
  39. ,"home_po_box"=>null
  40. ,"home_extended_address"=>null
  41. ,"home_address"=>null
  42. ,"home_city"=>null
  43. ,"home_state"=>null
  44. ,"home_postal_code"=>null
  45. ,"home_country"=>null
  46. ,"voice_tel"=>null
  47. ,"work_tel"=>null
  48. ,"home_tel"=>null
  49. ,"cell_tel"=>null
  50. ,"fax_tel"=>null
  51. ,"pager_tel"=>null
  52. ,"email1"=>null
  53. ,"email2"=>null
  54. ,"url"=>null
  55. ,"photo"=>null
  56. ,"birthday"=>null
  57. ,"timezone"=>null
  58. ,"sort_string"=>null
  59. ,"note"=>null
  60. );
  61. return true;
  62. }
  63. /*
  64. build() method checks all the values, builds appropriate defaults for
  65. missing values, generates the vcard data string.
  66. */
  67. function build() {
  68. $this->log .= "vcard build() called<br />";
  69. /*
  70. For many of the values, if they are not passed in, we set defaults or
  71. build them based on other values.
  72. */
  73. if (!$this->class) { $this->class = "PUBLIC"; }
  74. if (!$this->data['display_name']) {
  75. $this->data['display_name'] = trim($this->data['first_name']." ".$this->data['last_name']);
  76. }
  77. if (!$this->data['sort_string']) { $this->data['sort_string'] = $this->data['last_name']; }
  78. if (!$this->data['sort_string']) { $this->data['sort_string'] = $this->data['company']; }
  79. if (!$this->data['timezone']) { $this->data['timezone'] = date("O"); }
  80. if (!$this->revision_date) { $this->revision_date = date('Y-m-d H:i:s'); }
  81. $this->card = "BEGIN:VCARD\r\n";
  82. $this->card .= "VERSION:3.0\r\n";
  83. //$this->card .= "CLASS:".$this->class."\r\n";
  84. //$this->card .= "PRODID:-//class_vcard from TroyWolf.com//NONSGML Version 1//EN\r\n";
  85. // $this->card .= "REV:".$this->revision_date."\r\n";
  86. $this->card .= "FN:".$this->data['display_name']."\r\n";
  87. $this->card .= "N:";
  88. $this->card .= $this->data['last_name'].";";
  89. $this->card .= $this->data['first_name'];
  90. if (!empty($this->data['additional_name'])) {
  91. $this->card .= ";".$this->data['additional_name'];
  92. }
  93. if (!empty($this->data['name_prefix'])) {
  94. $this->card .= ";".$this->data['name_prefix'];
  95. }
  96. if (!empty($this->data['name_suffix'])) {
  97. $this->card .= ";".$this->data['name_suffix'];
  98. }
  99. $this->card .= "\r\n";
  100. if ($this->data['nickname']) { $this->card .= "NICKNAME:".$this->data['contact_nickname']."\r\n"; }
  101. if ($this->data['title']) { $this->card .= "TITLE:".$this->data['title']."\r\n"; }
  102. if ($this->data['company']) { $this->card .= "ORG:".$this->data['company']; }
  103. if ($this->data['department']) { $this->card .= ";".$this->data['department']; }
  104. $this->card .= "\r\n";
  105. $vcard_address_type_values = array('work','home','dom','intl','postal','parcel','pref');
  106. foreach ($vcard_address_type_values as $vcard_address_type_value) {
  107. if (!empty($this->data[$vcard_address_type_value.'_po_box'])
  108. || !empty($this->data[$vcard_address_type_value.'_extended_address'])
  109. || !empty($this->data[$vcard_address_type_value.'_address'])
  110. || !empty($this->data[$vcard_address_type_value.'_city'])
  111. || !empty($this->data[$vcard_address_type_value.'_state'])
  112. || !empty($this->data[$vcard_address_type_value.'_postal_code'])
  113. || !empty($this->data[$vcard_address_type_value.'_country'])) {
  114. $this->card .= "ADR;TYPE=".$vcard_address_type_value.":";
  115. if (!empty($this->data[$vcard_address_type_value.'_po_box'])) {
  116. $this->card .= $this->data[$vcard_address_type_value.'_po_box'].";";
  117. }
  118. if (!empty($this->data[$vcard_address_type_value.'_extended_address'])) {
  119. $this->card .= $this->data[$vcard_address_type_value.'_extended_address'].";";
  120. }
  121. if (!empty($this->data[$vcard_address_type_value.'_address'])) {
  122. $this->card .= $this->data[$vcard_address_type_value.'_address'].";";
  123. }
  124. if (!empty($this->data[$vcard_address_type_value.'_city'])) {
  125. $this->card .= $this->data[$vcard_address_type_value.'_city'].";";
  126. }
  127. if (!empty($this->data[$vcard_address_type_value.'_state'])) {
  128. $this->card .= $this->data[$vcard_address_type_value.'_state'].";";
  129. }
  130. if (!empty($this->data[$vcard_address_type_value.'_postal_code'])) {
  131. $this->card .= $this->data[$vcard_address_type_value.'_postal_code'].";";
  132. }
  133. if (!empty($this->data[$vcard_address_type_value.'_country'])) {
  134. $this->card .= $this->data[$vcard_address_type_value.'_country']."";
  135. }
  136. $this->card .= "\r\n";
  137. }
  138. }
  139. if ($this->data['email1']) { $this->card .= "EMAIL;PREF=1:".$this->data['email1']."\r\n"; }
  140. if ($this->data['email2']) { $this->card .= "EMAIL;PREF=2:".$this->data['email2']."\r\n"; }
  141. if ($this->data['voice_tel']) { $this->card .= "TEL;TYPE=voice:".$this->data['voice_tel']."\r\n"; }
  142. if ($this->data['work_tel']) { $this->card .= "TEL;TYPE=work:".$this->data['work_tel']."\r\n"; }
  143. if ($this->data['home_tel']) { $this->card .= "TEL;TYPE=home:".$this->data['home_tel']."\r\n"; }
  144. if ($this->data['cell_tel']) { $this->card .= "TEL;TYPE=cell:".$this->data['cell_tel']."\r\n"; }
  145. if ($this->data['fax_tel']) { $this->card .= "TEL;TYPE=fax:".$this->data['fax_tel']."\r\n"; }
  146. if ($this->data['pager_tel']) { $this->card .= "TEL;TYPE=pager:".$this->data['pager_tel']."\r\n"; }
  147. if ($this->data['url']) { $this->card .= "URL:".$this->data['url']."\r\n"; }
  148. if ($this->data['birthday']) { $this->card .= "BDAY:".$this->data['birthday']."\r\n"; }
  149. if ($this->data['role']) { $this->card .= "ROLE:".$this->data['role']."\r\n"; }
  150. if ($this->data['note']) { $this->card .= "NOTE:".$this->data['note']."\r\n"; }
  151. $this->card .= "TZ:".$this->data['timezone']."\r\n";
  152. $this->card .= "END:VCARD";
  153. }
  154. /*
  155. download() method streams the vcard to the browser client.
  156. */
  157. function download() {
  158. $this->log .= "vcard download() called<br />";
  159. if (!$this->card) { $this->build(); }
  160. if (!$this->filename) { $this->filename = trim($this->data['display_name']); }
  161. $this->filename = str_replace(" ", "_", $this->filename);
  162. header("Content-type: text/directory");
  163. header("Content-Disposition: attachment; filename=".$this->filename.".vcf");
  164. header("Pragma: public");
  165. echo $this->card;
  166. return true;
  167. }
  168. }