dolibarr  16.0.5
vcard.php
Go to the documentation of this file.
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 
28 require '../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/vcard.class.php';
32 
33 $user2 = new User($db);
34 
35 
36 $id = GETPOST('id', 'int');
37 
38 // Security check
39 $socid = 0;
40 if ($user->socid > 0) {
41  $socid = $user->socid;
42 }
43 $feature2 = 'user';
44 $result = restrictedArea($user, 'user', $id, 'user', $feature2);
45 
46 
47 $result = $user2->fetch($id);
48 if ($result <= 0) {
49  dol_print_error($user2->error);
50  exit;
51 }
52 
53 $physicalperson = 1;
54 
55 $company = new Societe($db);
56 if ($user2->socid) {
57  $result = $company->fetch($user2->socid);
58 }
59 
60 // We create VCard
61 $v = new vCard();
62 $v->setProdId('Dolibarr '.DOL_VERSION);
63 
64 $v->setUid('DOLIBARR-USERID-'.$user2->id);
65 $v->setName($user2->lastname, $user2->firstname, "", $user2->civility_code, "");
66 $v->setFormattedName($user2->getFullName($langs, 1));
67 
68 $v->setPhoneNumber($user2->office_phone, "TYPE=WORK;VOICE");
69 $v->setPhoneNumber($user2->personal_mobile, "TYPE=HOME;VOICE");
70 $v->setPhoneNumber($user2->user_mobile, "TYPE=CELL;VOICE");
71 $v->setPhoneNumber($user2->office_fax, "TYPE=WORK;FAX");
72 
73 $country = $user2->country_code ? $user2->country : '';
74 
75 $v->setAddress("", "", $user2->address, $user2->town, $user2->state, $user2->zip, $country, "TYPE=WORK;POSTAL");
76 $v->setLabel("", "", $user2->address, $user2->town, $user2->state, $user2->zip, $country, "TYPE=WORK");
77 
78 $v->setEmail($user2->email, "TYPE=WORK");
79 $v->setNote($user2->note_public);
80 $v->setTitle($user2->job);
81 
82 // Data from linked company
83 if ($company->id) {
84  $v->setURL($company->url, "TYPE=WORK");
85  if (!$user2->office_phone) {
86  $v->setPhoneNumber($company->phone, "TYPE=WORK;VOICE");
87  }
88  if (!$user2->office_fax) {
89  $v->setPhoneNumber($company->fax, "TYPE=WORK;FAX");
90  }
91  if (!$user2->zip) {
92  $v->setAddress("", "", $company->address, $company->town, $company->state, $company->zip, $company->country, "TYPE=WORK;POSTAL");
93  }
94 
95  // when company e-mail is empty, use only user e-mail
96  if (empty(trim($company->email))) {
97  // was set before, don't set twice
98  } elseif (empty(trim($user2->email))) {
99  // when user e-mail is empty, use only company e-mail
100  $v->setEmail($company->email, "TYPE=WORK");
101  } elseif (strtolower(end(explode("@", $user2->email))) == strtolower(end(explode("@", $company->email)))) {
102  // when e-mail domain of user and company are the same, use user e-mail at first (and company e-mail at second)
103  $v->setEmail($user2->email, "TYPE=WORK");
104 
105  // support by Microsoft Outlook (2019 and possible earlier)
106  $v->setEmail($company->email, 'INTERNET');
107  } else {
108  // when e-mail of user and company complete different use company e-mail at first (and user e-mail at second)
109  $v->setEmail($company->email, "TYPE=WORK");
110 
111  // support by Microsoft Outlook (2019 and possible earlier)
112  $v->setEmail($user2->email, 'INTERNET');
113  }
114 
115  // Si user lie a un tiers non de type "particulier"
116  if ($company->typent_code != 'TE_PRIVATE') {
117  $v->setOrg($company->name);
118  }
119 }
120 
121 // Personal informations
122 $v->setPhoneNumber($user2->personal_mobile, "TYPE=HOME;VOICE");
123 if ($user2->birth) {
124  $v->setBirthday($user2->birth);
125 }
126 
127 $db->close();
128 
129 // Renvoi la VCard au navigateur
130 
131 $output = $v->getVCard();
132 
133 $filename = trim(urldecode($v->getFileName())); // "Nom prenom.vcf"
134 $filenameurlencoded = dol_sanitizeFileName(urlencode($filename));
135 //$filename = dol_sanitizeFileName($filename);
136 
137 
138 header("Content-Disposition: attachment; filename=\"".$filename."\"");
139 header("Content-Length: ".dol_strlen($output));
140 header("Connection: close");
141 header("Content-Type: text/x-vcard; name=\"".$filename."\"");
142 
143 print $output;
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
dol_sanitizeFileName
dol_sanitizeFileName($str, $newstr='_', $unaccent=1)
Clean a string to use it as a file name.
Definition: functions.lib.php:1226
restrictedArea
restrictedArea($user, $features, $objectid=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.
Definition: security.lib.php:234
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
vCard
Class to buld vCard files.
Definition: vcard.class.php:88
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
User
Class to manage Dolibarr users.
Definition: user.class.php:44