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