vcard.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. $company = new Societe($db);
  39. if ($contact->socid) {
  40. $result = $company->fetch($contact->socid);
  41. }
  42. // We create VCard
  43. $v = new vCard();
  44. $v->setProdId('Dolibarr '.DOL_VERSION);
  45. $v->setUid('DOLIBARR-CONTACTID-'.$contact->id);
  46. $v->setName($contact->lastname, $contact->firstname, "", $contact->civility, "");
  47. $v->setFormattedName($contact->getFullName($langs, 1));
  48. $v->setPhoneNumber($contact->phone_pro, "TYPE=WORK;VOICE");
  49. //$v->setPhoneNumber($contact->phone_perso,"TYPE=HOME;VOICE");
  50. $v->setPhoneNumber($contact->phone_mobile, "TYPE=CELL;VOICE");
  51. $v->setPhoneNumber($contact->fax, "TYPE=WORK;FAX");
  52. $country = $contact->country_code ? $contact->country : '';
  53. $v->setAddress("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK;POSTAL");
  54. $v->setLabel("", "", $contact->address, $contact->town, $contact->state, $contact->zip, $country, "TYPE=WORK");
  55. $v->setEmail($contact->email);
  56. $v->setNote($contact->note);
  57. $v->setTitle($contact->poste);
  58. // Data from linked company
  59. if ($company->id) {
  60. $v->setURL($company->url, "TYPE=WORK");
  61. if (!$contact->phone_pro) {
  62. $v->setPhoneNumber($company->phone, "TYPE=WORK;VOICE");
  63. }
  64. if (!$contact->fax) {
  65. $v->setPhoneNumber($company->fax, "TYPE=WORK;FAX");
  66. }
  67. if (!$contact->zip) {
  68. $v->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK;POSTAL");
  69. }
  70. // when company e-mail is empty, use only contact e-mail
  71. if (empty(trim($company->email))) {
  72. // was set before, don't set twice
  73. } elseif (empty(trim($contact->email))) {
  74. // when contact e-mail is empty, use only company e-mail
  75. $v->setEmail($company->email);
  76. } elseif (strtolower(end(explode("@", $contact->email))) == strtolower(end(explode("@", $company->email)))) {
  77. // when e-mail domain of contact and company are the same, use contact e-mail at first (and company e-mail at second)
  78. $v->setEmail($contact->email);
  79. // support by Microsoft Outlook (2019 and possible earlier)
  80. $v->setEmail($company->email, 'INTERNET');
  81. } else {
  82. // when e-mail of contact and company complete different use company e-mail at first (and contact e-mail at second)
  83. $v->setEmail($company->email);
  84. // support by Microsoft Outlook (2019 and possible earlier)
  85. $v->setEmail($contact->email, 'INTERNET');
  86. }
  87. // Si contact lie a un tiers non de type "particulier"
  88. if ($company->typent_code != 'TE_PRIVATE') {
  89. $v->setOrg($company->name);
  90. }
  91. }
  92. // Personal informations
  93. $v->setPhoneNumber($contact->phone_perso, "TYPE=HOME;VOICE");
  94. if ($contact->birthday) {
  95. $v->setBirthday($contact->birthday);
  96. }
  97. $db->close();
  98. // Renvoi la VCard au navigateur
  99. $output = $v->getVCard();
  100. $filename = trim(urldecode($v->getFileName())); // "Nom prenom.vcf"
  101. $filenameurlencoded = dol_sanitizeFileName(urlencode($filename));
  102. //$filename = dol_sanitizeFileName($filename);
  103. header("Content-Disposition: attachment; filename=\"".$filename."\"");
  104. header("Content-Length: ".dol_strlen($output));
  105. header("Connection: close");
  106. header("Content-Type: text/x-vcard; name=\"".$filename."\"");
  107. print $output;