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