vcard.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. * Called when there are no references to a particular object
  65. * unset the variables used in the class
  66. */
  67. public function __destruct() {
  68. foreach ($this as $key => $value) {
  69. unset($this->$key);
  70. }
  71. }
  72. /*
  73. build() method checks all the values, builds appropriate defaults for
  74. missing values, generates the vcard data string.
  75. */
  76. function build() {
  77. $this->log .= "vcard build() called<br />";
  78. /*
  79. For many of the values, if they are not passed in, we set defaults or
  80. build them based on other values.
  81. */
  82. if (!$this->class) { $this->class = "PUBLIC"; }
  83. if (!$this->data['display_name']) {
  84. $this->data['display_name'] = trim($this->data['first_name']." ".$this->data['last_name']);
  85. }
  86. if (!$this->data['sort_string']) { $this->data['sort_string'] = $this->data['last_name']; }
  87. if (!$this->data['sort_string']) { $this->data['sort_string'] = $this->data['company']; }
  88. if (!$this->data['timezone']) { $this->data['timezone'] = date("O"); }
  89. if (!$this->revision_date) { $this->revision_date = date('Y-m-d H:i:s'); }
  90. $this->card = "BEGIN:VCARD\r\n";
  91. $this->card .= "VERSION:3.0\r\n";
  92. //$this->card .= "CLASS:".$this->class."\r\n";
  93. //$this->card .= "PRODID:-//class_vcard from TroyWolf.com//NONSGML Version 1//EN\r\n";
  94. // $this->card .= "REV:".$this->revision_date."\r\n";
  95. $this->card .= "FN:".$this->data['display_name']."\r\n";
  96. $this->card .= "N:";
  97. $this->card .= $this->data['last_name'].";";
  98. $this->card .= $this->data['first_name'];
  99. if (strlen($this->data['additional_name']) > 0) {
  100. $this->card .= ";".$this->data['additional_name'];
  101. }
  102. if (strlen($this->data['name_prefix']) > 0) {
  103. $this->card .= ";".$this->data['name_prefix'];
  104. }
  105. if (strlen($this->data['name_suffix']) > 0) {
  106. $this->card .= ";".$this->data['name_suffix'];
  107. }
  108. $this->card .= "\r\n";
  109. if ($this->data['nickname']) { $this->card .= "NICKNAME:".$this->data['contact_nickname']."\r\n"; }
  110. if ($this->data['title']) { $this->card .= "TITLE:".$this->data['title']."\r\n"; }
  111. if ($this->data['company']) { $this->card .= "ORG:".$this->data['company']; }
  112. if ($this->data['department']) { $this->card .= ";".$this->data['department']; }
  113. $this->card .= "\r\n";
  114. $vcard_address_type_values = array('work','home','dom','intl','postal','parcel','pref');
  115. foreach ($vcard_address_type_values as $vcard_address_type_value) {
  116. if ($this->data[$vcard_address_type_value.'_po_box']
  117. || $this->data[$vcard_address_type_value.'_extended_address']
  118. || $this->data[$vcard_address_type_value.'_address']
  119. || $this->data[$vcard_address_type_value.'_city']
  120. || $this->data[$vcard_address_type_value.'_state']
  121. || $this->data[$vcard_address_type_value.'_postal_code']
  122. || $this->data[$vcard_address_type_value.'_country']) {
  123. $this->card .= "ADR;TYPE=".$vcard_address_type_value.":";
  124. if (strlen($this->data[$vcard_address_type_value.'_po_box']) > 0) {
  125. $this->card .= $this->data[$vcard_address_type_value.'_po_box'].";";
  126. }
  127. if (strlen($this->data[$vcard_address_type_value.'_extended_address']) > 0) {
  128. $this->card .= $this->data[$vcard_address_type_value.'_extended_address'].";";
  129. }
  130. if (strlen($this->data[$vcard_address_type_value.'_address']) > 0) {
  131. $this->card .= $this->data[$vcard_address_type_value.'_address'].";";
  132. }
  133. if (strlen($this->data[$vcard_address_type_value.'_city']) > 0) {
  134. $this->card .= $this->data[$vcard_address_type_value.'_city'].";";
  135. }
  136. if (strlen($this->data[$vcard_address_type_value.'_state']) > 0) {
  137. $this->card .= $this->data[$vcard_address_type_value.'_state'].";";
  138. }
  139. if (strlen($this->data[$vcard_address_type_value.'_postal_code']) > 0) {
  140. $this->card .= $this->data[$vcard_address_type_value.'_postal_code'].";";
  141. }
  142. if (strlen($this->data[$vcard_address_type_value.'_country']) > 0) {
  143. $this->card .= $this->data[$vcard_address_type_value.'_country']."";
  144. }
  145. $this->card .= "\r\n";
  146. }
  147. }
  148. if ($this->data['email1']) { $this->card .= "EMAIL;PREF=1:".$this->data['email1']."\r\n"; }
  149. if ($this->data['email2']) { $this->card .= "EMAIL;PREF=2:".$this->data['email2']."\r\n"; }
  150. if ($this->data['voice_tel']) { $this->card .= "TEL;TYPE=voice:".$this->data['voice_tel']."\r\n"; }
  151. if ($this->data['work_tel']) { $this->card .= "TEL;TYPE=work:".$this->data['work_tel']."\r\n"; }
  152. if ($this->data['home_tel']) { $this->card .= "TEL;TYPE=home:".$this->data['home_tel']."\r\n"; }
  153. if ($this->data['cell_tel']) { $this->card .= "TEL;TYPE=cell:".$this->data['cell_tel']."\r\n"; }
  154. if ($this->data['fax_tel']) { $this->card .= "TEL;TYPE=fax:".$this->data['fax_tel']."\r\n"; }
  155. if ($this->data['pager_tel']) { $this->card .= "TEL;TYPE=pager:".$this->data['pager_tel']."\r\n"; }
  156. if ($this->data['url']) { $this->card .= "URL:".$this->data['url']."\r\n"; }
  157. if ($this->data['birthday']) { $this->card .= "BDAY:".$this->data['birthday']."\r\n"; }
  158. if ($this->data['role']) { $this->card .= "ROLE:".$this->data['role']."\r\n"; }
  159. if ($this->data['note']) { $this->card .= "NOTE:".$this->data['note']."\r\n"; }
  160. $this->card .= "TZ:".$this->data['timezone']."\r\n";
  161. $this->card .= "END:VCARD";
  162. }
  163. /*
  164. download() method streams the vcard to the browser client.
  165. */
  166. function download() {
  167. $this->log .= "vcard download() called<br />";
  168. if (!$this->card) { $this->build(); }
  169. if (!$this->filename) { $this->filename = trim($this->data['display_name']); }
  170. $this->filename = str_replace(" ", "_", $this->filename);
  171. header("Content-type: text/directory");
  172. header("Content-Disposition: attachment; filename=".$this->filename.".vcf");
  173. header("Pragma: public");
  174. echo $this->card;
  175. return true;
  176. }
  177. }