-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathvCard.class.php
More file actions
246 lines (230 loc) · 8.01 KB
/
vCard.class.php
File metadata and controls
246 lines (230 loc) · 8.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
/**
* @file
* A class to generate vCards for contact data.
*/
class vCard
{
// An array of this vcard's contact data.
protected $data;
// Filename for download file naming.
protected $filename;
// vCard class: PUBLIC, PRIVATE, CONFIDENTIAL.
protected $class;
// vCard revision date.
protected $revision_date;
// The vCard gnerated.
protected $card;
/**
* The constructor.
*/
public function __construct()
{
$this->data = array(
'display_name' => NULL,
'first_name' => NULL,
'last_name' => NULL,
'additional_name' => NULL,
'name_prefix' => NULL,
'name_suffix' => NULL,
'nickname' => NULL,
'title' => NULL,
'role' => NULL,
'department' => NULL,
'company' => NULL,
'work_po_box' => NULL,
'work_extended_address' => NULL,
'work_address' => NULL,
'work_city' => NULL,
'work_state' => NULL,
'work_postal_code' => NULL,
'work_country' => NULL,
'home_po_box' => NULL,
'home_extended_address' => NULL,
'home_address' => NULL,
'home_city' => NULL,
'home_state' => NULL,
'home_postal_code' => NULL,
'home_country' => NULL,
'office_tel' => NULL,
'home_tel' => NULL,
'cell_tel' => NULL,
'fax_tel' => NULL,
'pager_tel' => NULL,
'email1' => NULL,
'email2' => NULL,
'url' => NULL,
'photo' => NULL,
'birthday' => NULL,
'timezone' => NULL,
'sort_string' => NULL,
'note' => NULL,
);
return true;
}
/**
* Global setter.
*
* @param string $key
* Name of the property.
* @param mixed $value
* Value to set.
*
* @return vCard
* Return itself.
*/
public function set($key, $value)
{
// Check if the specified property is defined.
if (property_exists($this, $key) && $key != 'data') {
$this->{$key} = trim($value);
return $this;
} elseif (property_exists($this, $key) && $key == 'data') {
foreach ($value as $v_key => $v_value) {
$this->{$key}[$v_key] = trim($v_value);
}
return $this;
} else {
return FALSE;
}
}
/**
* Checks all the values, builds appropriate defaults for
* missing values and generates the vcard data string.
*/
function build()
{
if (!$this->class) {
$this->class = 'PUBLIC';
}
if (!$this->data['display_name']) {
$this->data['display_name'] = $this->data['first_name'] . ' ' . $this->data['last_name'];
}
if (!$this->data['sort_string']) {
$this->data['sort_string'] = $this->data['last_name'];
}
if (!$this->data['sort_string']) {
$this->data['sort_string'] = $this->data['company'];
}
if (!$this->data['timezone']) {
$this->data['timezone'] = date("O");
}
if (!$this->revision_date) {
$this->revision_date = date('Y-m-d H:i:s');
}
$this->card = "BEGIN:VCARD\r\n";
$this->card .= "VERSION:3.0\r\n";
$this->card .= "CLASS:" . $this->class . "\r\n";
$this->card .= "PRODID:-//class_vCard from WhatsAPI//NONSGML Version 1//EN\r\n";
$this->card .= "REV:" . $this->revision_date . "\r\n";
$this->card .= "FN:" . $this->data['display_name'] . "\r\n";
$this->card .= "N:"
. $this->data['last_name'] . ";"
. $this->data['first_name'] . ";"
. $this->data['additional_name'] . ";"
. $this->data['name_prefix'] . ";"
. $this->data['name_suffix'] . "\r\n";
if ($this->data['nickname']) {
$this->card .= "NICKNAME:" . $this->data['nickname'] . "\r\n";
}
if ($this->data['title']) {
$this->card .= "TITLE:" . $this->data['title'] . "\r\n";
}
if ($this->data['company']) {
$this->card .= "ORG:" . $this->data['company'];
}
if ($this->data['department']) {
$this->card .= ";".$this->data['department'];
}
$this->card .= "\r\n";
if ($this->data['work_po_box'] || $this->data['work_extended_address']
|| $this->data['work_address'] || $this->data['work_city']
|| $this->data['work_state'] || $this->data['work_postal_code']
|| $this->data['work_country']) {
$this->card .= "ADR;type=WORK:"
. $this->data['work_po_box'] . ";"
. $this->data['work_extended_address'] . ";"
. $this->data['work_address'] . ";"
. $this->data['work_city'] . ";"
. $this->data['work_state'] . ";"
. $this->data['work_postal_code'] . ";"
. $this->data['work_country'] . "\r\n";
}
if ($this->data['home_po_box'] || $this->data['home_extended_address']
|| $this->data['home_address'] || $this->data['home_city']
|| $this->data['home_state'] || $this->data['home_postal_code']
|| $this->data['home_country']) {
$this->card .= "ADR;type=HOME:"
. $this->data['home_po_box'] . ";"
. $this->data['home_extended_address'] . ";"
. $this->data['home_address'] . ";"
. $this->data['home_city'] . ";"
. $this->data['home_state'] . ";"
. $this->data['home_postal_code'] . ";"
. $this->data['home_country'] . "\r\n";
}
if ($this->data['email1']) {
$this->card .= "EMAIL;type=INTERNET,pref:" . $this->data['email1'] . "\r\n";
}
if ($this->data['email2']) {
$this->card .= "EMAIL;type=INTERNET:" . $this->data['email2'] . "\r\n";
}
if ($this->data['office_tel']) {
$this->card .= "TEL;type=WORK,voice:" . $this->data['office_tel'] . "\r\n";
}
if ($this->data['home_tel']) {
$this->card .= "TEL;type=HOME,voice:" . $this->data['home_tel'] . "\r\n";
}
if ($this->data['cell_tel']) {
$this->card .= "TEL;type=CELL,voice:" . $this->data['cell_tel'] . "\r\n";
}
if ($this->data['fax_tel']) {
$this->card .= "TEL;type=WORK,fax:" . $this->data['fax_tel'] . "\r\n";
}
if ($this->data['pager_tel']) {
$this->card .= "TEL;type=WORK,pager:" . $this->data['pager_tel'] . "\r\n";
}
if ($this->data['url']) {
$this->card .= "URL;type=WORK:" . $this->data['url'] . "\r\n";
}
if ($this->data['birthday']) {
$this->card .= "BDAY:" . $this->data['birthday'] . "\r\n";
}
if ($this->data['role']) {
$this->card .= "ROLE:" . $this->data['role'] . "\r\n";
}
if ($this->data['note']) {
$this->card .= "NOTE:" . $this->data['note'] . "\r\n";
}
$this->card .= "TZ:" . $this->data['timezone'] . "\r\n";
$this->card .= "END:VCARD\r\n";
}
/**
* Streams the vcard to the browser client.
*/
function download()
{
if (!$this->card) {
$this->build();
}
if (!$this->filename) {
$this->filename = $this->data['display_name'];
}
$this->filename = str_replace(' ', '_', $this->filename);
header("Content-type: text/directory");
header("Content-Disposition: attachment; filename=" . $this->filename . ".vcf");
header("Pragma: public");
echo $this->card;
return TRUE;
}
/**
* Show the vcard.
*/
function show()
{
if (!$this->card) {
$this->build();
}
return $this->card;
}
}