vcard.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2020 Tobias Sekan <tobias.sekan@startmail.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/contact/vcard.php
  22. * \ingroup societe
  23. * \brief Onglet vcard d'un contact
  24. */
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/vcard.class.php';
  29. $contact = new Contact($db);
  30. $id = GETPOST('id', 'int');
  31. // Security check
  32. $result = restrictedArea($user, 'contact', $id, 'socpeople&societe');
  33. $result = $contact->fetch($id);
  34. if ($result <= 0) {
  35. dol_print_error($contact->error);
  36. exit;
  37. }
  38. $physicalperson = 1;
  39. $company = new Societe($db);
  40. if ($contact->socid) {
  41. $result = $company->fetch($contact->socid);
  42. }
  43. // We create VCard
  44. $v = new vCard();
  45. $v->setProdId('Dolibarr '.DOL_VERSION);
  46. $v->setUid('DOLIBARR-CONTACTID-'.$contact->id);
  47. $v->setName($contact->lastname, $contact->firstname, "", $contact->civility, "");
  48. $v->setFormattedName($contact->getFullName($langs, 1));
  49. $v->setPhoneNumber($contact->phone_pro, "TYPE=WORK;VOICE");
  50. //$v->setPhoneNumber($contact->phone_perso,"TYPE=HOME;VOICE");
  51. $v->setPhoneNumber($contact->phone_mobile, "TYPE=CELL;VOICE");
  52. $v->setPhoneNumber($contact->fax, "TYPE=WORK;FAX");
  53. $country = $contact->country_code ? $contact->country : '';
  54. $v->setAddress("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK;POSTAL");
  55. $v->setLabel("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK");
  56. $v->setEmail($contact->email);
  57. $v->setNote($contact->note);
  58. $v->setTitle($contact->poste);
  59. // Data from linked company
  60. if ($company->id) {
  61. $v->setURL($company->url, "TYPE=WORK");
  62. if (!$contact->phone_pro) {
  63. $v->setPhoneNumber($company->phone, "TYPE=WORK;VOICE");
  64. }
  65. if (!$contact->fax) {
  66. $v->setPhoneNumber($company->fax, "TYPE=WORK;FAX");
  67. }
  68. if (!$contact->zip) {
  69. $v->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK;POSTAL");
  70. }
  71. // when company e-mail is empty, use only contact e-mail
  72. if (empty(trim($company->email))) {
  73. // was set before, don't set twice
  74. } elseif (empty(trim($contact->email))) {
  75. // when contact e-mail is empty, use only company e-mail
  76. $v->setEmail($company->email);
  77. } elseif (strtolower(end(explode("@", $contact->email))) == strtolower(end(explode("@", $company->email)))) {
  78. // when e-mail domain of contact and company are the same, use contact e-mail at first (and company e-mail at second)
  79. $v->setEmail($contact->email);
  80. // support by Microsoft Outlook (2019 and possible earlier)
  81. $v->setEmail($company->email, 'INTERNET');
  82. } else {
  83. // when e-mail of contact and company complete different use company e-mail at first (and contact e-mail at second)
  84. $v->setEmail($company->email);
  85. // support by Microsoft Outlook (2019 and possible earlier)
  86. $v->setEmail($contact->email, 'INTERNET');
  87. }
  88. // Si contact lie a un tiers non de type "particulier"
  89. if ($contact->typent_code != 'TE_PRIVATE') {
  90. $v->setOrg($company->name);
  91. }
  92. }
  93. // Personal informations
  94. $v->setPhoneNumber($contact->phone_perso, "TYPE=HOME;VOICE");
  95. if ($contact->birthday) {
  96. $v->setBirthday($contact->birthday);
  97. }
  98. $db->close();
  99. // Renvoi la VCard au navigateur
  100. $output = $v->getVCard();
  101. $filename = trim(urldecode($v->getFileName())); // "Nom prenom.vcf"
  102. $filenameurlencoded = dol_sanitizeFileName(urlencode($filename));
  103. //$filename = dol_sanitizeFileName($filename);
  104. header("Content-Disposition: attachment; filename=\"".$filename."\"");
  105. header("Content-Length: ".dol_strlen($output));
  106. header("Connection: close");
  107. header("Content-Type: text/x-vcard; name=\"".$filename."\"");
  108. print $output;