-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathvcard.php
352 lines (306 loc) · 8.18 KB
/
vcard.php
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
/**
* VCard generator - can save to file or output as a download
*
* @author Jeroen Desloovere <[email protected]>
*/
class VCard
{
/**
* Filename
*
* @param string
*/
private $filename;
/**
* Properties
*
* @param array
*/
private $properties;
/**
* Add address
*
* @param string[optional] $name
* @param string[optional] $extended
* @param string[optional] $street
* @param string[optional] $city
* @param string[optional] $region
* @param string[optional] $zip
* @param string[optional] $country
* @param string[optional] $type $type may be DOM | INTL | POSTAL | PARCEL | HOME | WORK or any combination of these: e.g. "WORK;PARCEL;POSTAL"
*/
public function addAddress($name = '', $extended = '', $street = '', $city = '', $region = '', $zip = '', $country = '', $type = 'WORK;POSTAL')
{
// init value
$value = $name . ';' . $extended . ';' . $street . ';' . $city . ';' . $region . ';' . $zip . ';' . $country;
// set property
$this->setProperty('ADR' . (($type != '') ? ';' . $type : ''), $value);
}
/**
* Add birthday
*
* @param string $date Format is YYYY-MM-DD
*/
public function addBirthday($date)
{
$this->setProperty('BDAY', $date);
}
/**
* Add company
*
* @param string $company
*/
public function addCompany($company)
{
$this->setProperty('ORG', $company);
// if filename is empty, add to filename
if(empty($this->filename)) $this->setFilename($company);
}
/**
* Add email
*
* @param string $date Format is YYYY-MM-DD
*/
public function addEmail($address)
{
$this->setProperty('EMAIL;INTERNET', $address);
}
/**
* Add jobtitle
*
* @param string $jobtitle The jobtitle for the person.
*/
public function addJobtitle($jobtitle)
{
$this->setProperty('TITLE', $jobtitle);
}
/**
* Add name
*
* @param string[optional] $lastname
* @param string[optional] $firstname
* @param string[optional] $additional
* @param string[optional] $prefix
* @param string[optional] $suffix
*/
public function addName($lastname = '', $firstname = '', $additional = '', $prefix = '', $suffix = '')
{
// define filename
$this->setFilename(array($prefix, $firstname, $additional, $lastname, $suffix));
// set property
$this->setProperty('N', $lastname . ';' . $firstname . ';' . $additional . ';' . $prefix . ';' . $suffix);
// is property FN set?
if(!isset($this->properties['FN']) || $this->properties['FN'] == '')
{
// set property
$this->setProperty('FN', trim($prefix . ' ' . $firstname . ' ' . $additional . ' ' . $lastname . ' ' . $suffix));
}
}
/**
* Add note
*
* @param string $note
*/
public function addNote($note)
{
$this->setProperty('NOTE', $note);
}
/**
* Add phone number
*
* @param string $number
* @param string[optional] $type Type may be PREF | WORK | HOME | VOICE | FAX | MSG | CELL | PAGER | BBS | CAR | MODEM | ISDN | VIDEO or any senseful combination, e.g. "PREF;WORK;VOICE"
*/
public function addPhoneNumber($number, $type = '')
{
$this->setProperty('TEL' . (($type != '') ? ';' . $type : ''), $number);
}
/**
* Add URL
*
* @param string $url
* @param string[optional] $type Type may be WORK | HOME
*/
public function addURL($url, $type = '')
{
$this->setProperty('URL' . (($type != '') ? ';' . $type : ''), $url);
}
/**
* Build VCard (.vcf)
*
* @return string
*/
protected function buildVCard()
{
// init string
$string = "BEGIN:VCARD\r\n";
$string .= "VERSION:3.0\r\n";
$string .= "REV:" . date("Y-m-d") . "T" . date("H:i:s") . "Z\r\n";
// loop all properties
foreach($this->properties as $key => $value)
{
// add to string
$string .= $key . ':' . $value . "\r\n";
}
// add to string
$string .= "END:VCARD\r\n";
// return
return $string;
}
/**
* Build VCalender (.ics) - Safari (iOS) can not open .vcf files, so we build a workaround.
*
* @return string
*/
protected function buildVCalendar()
{
// init dates
$dtstart = date("Ymd")."T".date("Hi")."00";
$dtend = date("Ymd")."T".date("Hi")."01";
// init string
$string = "BEGIN:VCALENDAR\n";
$string .= "VERSION:2.0\n";
$string .= "BEGIN:VEVENT\n";
$string .= "DTSTART;TZID=Europe/London:" . $dtstart . "\n";
$string .= "DTEND;TZID=Europe/London:" . $dtend . "\n";
$string .= "SUMMARY:" . FL::msg('VCardSummaryHelp') . "\n"; // Click attached contact below to save to your contacts
$string .= "DTSTAMP:" . $dtstart . "Z\n";
$string .= "ATTACH;VALUE=BINARY;ENCODING=BASE64;FMTTYPE=text/directory;\n";
$string .= " X-APPLE-FILENAME=" . $this->filename . ".vcf:\n";
// base64 encode it so that it can be used as an attachemnt to the "dummy" calendar appointment
$b64vcard = base64_encode($this->buildVCard());
// chunk the single long line of b64 text in accordance with RFC2045
// (and the exact line length determined from the original .ics file exported from Apple calendar
$b64mline = chunk_split($b64vcard, 74, "\n");
// need to indent all the lines by 1 space for the iphone (yes really?!!)
$b64final = preg_replace('/(.+)/', ' $1', $b64mline);
$string .= $b64final;
// output the correctly formatted encoded text
$string .= "END:VEVENT\n";
$string .= "END:VCALENDAR\n";
// return
return $string;
}
/**
* Decode
*
* @param string $value
* @return string decoded
*/
private function decode($value)
{
return htmlspecialchars_decode((string) $value, ENT_QUOTES);
}
/**
* Download
*/
public function download()
{
// iOS devices
if($this->isIOS())
{
// define output
$output = $this->buildVCalendar();
# Send correct headers
header('Content-type: text/x-vcalendar; charset=utf-8');
// header('Content-type: application/octet-stream; charset=utf-8');
// Alternatively: application/octet-stream
// Depending on the desired browser behaviour
// Be sure to test thoroughly cross-browser
header('Content-Disposition: attachment; filename=' . $this->filename . '.ics;');
}
// non-iOS devices
else
{
// define output
$output = $this->buildVCard();
// output to file
header('Content-type:text/x-vcard; charset=UTF-8');
header('Content-Disposition: attachment; filename=' . $this->filename . '.vcf;');
}
header('Content-Length: ' . strlen($output));
header('Connection: close');
echo $output;
}
/**
* Get output as string
*/
public function get()
{
// return output for iOS devices or for non-iOS devices
return ($this->isIOS()) ? $this->buildVCalendar() : $this->buildVCard();
}
/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* Is iOS?
*
* @return string ios
*/
public function isIOS()
{
// get user agent
$browser = strtolower($_SERVER['HTTP_USER_AGENT']);
// return bool
return (strpos($browser,'iphone') || strpos($browser,'ipod') || strpos($browser,'ipad')) ? true : false;
}
/**
* Save
*/
public function save()
{
// iOS devices - save to file as .ics
if($this->isIOS()) file_put_contents($this->filename . '.ics', $this->buildVCalendar());
// non-iOS devices - save to file as .vcf
else file_put_contents($this->filename . '.vcf', $this->buildVCard());
}
/**
* Set filename
*
* @param mixed $value
* @param bool $overwrite
* @param string[optional] $separator
*/
public function setFilename($value, $overwrite = true, $separator = '_')
{
// recast to string if $value is array
if(is_array($value)) $value = implode($separator, $value);
// trim unneeded values
$value = trim($value, $separator);
// remove all spaces
$value = preg_replace('/\s+/', $separator, $value);
// if value is empty, stop here
if(empty($value)) return false;
// decode value + lowercase the string
$value = strtolower($this->decode($value));
// overwrite filename or add to filename using a prefix in between
$this->filename = ($overwrite) ? $value : $this->filename . $separator . $value;
}
/**
* Set property
*
* @param string $key
* @param string $value
*/
private function setProperty($key, $value)
{
// set decoded property
$this->properties[$key] = $this->decode($value);
}
}
/**
* VCard Exception class
*
* @author Jeroen Desloovere <[email protected]>
*/
class VCardException extends Exception
{
}