vcard.php 4.8 KB

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