/
Vcard.php
454 lines (400 loc) · 17.7 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
<?php
/**
* The Horde_Core_Mime_Viewer_Vcard class renders out vCards in HTML format.
*
* Copyright 2002-2014 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @author Jan Schneider <jan@horde.org>
* @category Horde
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Core
*/
class Horde_Core_Mime_Viewer_Vcard extends Horde_Mime_Viewer_Base
{
/**
* This driver's display capabilities.
*
* @var array
*/
protected $_capability = array(
'full' => true,
'info' => false,
'inline' => true,
'raw' => false
);
/**
* URL that can be used as a callback for displaying images.
*
* @var Horde_Url
*/
protected $_imageUrl;
/**
* Constructor.
*
* @param Horde_Mime_Part $part The object with the data to be rendered.
* @param array $conf Configuration:
* - browser: (Horde_Browser) Browser object.
* - notification: (Horde_Notification_Base) Notification object.
* - prefs: (Horde_Prefs) Prefs object.
* - registry: (Horde_Registry) Registry object.
*
* @throws InvalidArgumentException
*/
public function __construct(Horde_Mime_Part $part, array $conf = array())
{
$this->_required = array_merge($this->_required, array(
'browser',
'notification',
'prefs',
'registry'
));
parent::__construct($part, $conf);
}
/**
* Return the full rendered version of the Horde_Mime_Part object.
*
* @return array See parent::render().
*/
protected function _render()
{
global $page_output;
$ret = $this->_renderInline();
if (!empty($ret)) {
$page_output->sidebar = $page_output->topbar = false;
reset($ret);
Horde::startBuffer();
$page_output->header();
echo $ret[key($ret)]['data'];
$page_output->footer();
$ret[key($ret)]['data'] = Horde::endBuffer();
}
return $ret;
}
/**
* Return the rendered inline version of the Horde_Mime_Part object.
*
* @return array See parent::render().
*/
protected function _renderInline()
{
$browser = $this->getConfigParam('browser');
$notification = $this->getConfigParam('notification');
$prefs = $this->getConfigParam('prefs');
$registry = $this->getConfigParam('registry');
$data = $this->_mimepart->getContents();
$html = '';
$title = Horde_Core_Translation::t("vCard");
$iCal = new Horde_Icalendar();
if (!$iCal->parsevCalendar($data, 'VCALENDAR', $this->_mimepart->getCharset())) {
$notification->push(Horde_Core_Translation::t("There was an error reading the contact data."), 'horde.error');
}
if (Horde_Util::getFormData('import') &&
Horde_Util::getFormData('source') &&
$registry->hasMethod('contacts/import')) {
$source = Horde_Util::getFormData('source');
$count = 0;
foreach ($iCal->getComponents() as $c) {
if ($c->getType() == 'vcard') {
try {
$registry->call('contacts/import', array($c, null, $source));
++$count;
} catch (Horde_Exception $e) {
$notification->push(Horde_Core_Translation::t("There was an error importing the contact data:") . ' ' . $e->getMessage(), 'horde.error');
}
}
}
$notification->push(sprintf(Horde_Core_Translation::ngettext(
"%d contact was successfully added to your address book.",
"%d contacts were successfully added to your address book.",
$count),
$count),
'horde.success');
}
$html .= '<table class="horde-table" style="width:100%">';
foreach ($iCal->getComponents() as $i => $vc) {
if ($i > 0) {
$html .= '<tr><td colspan="2"> </td></tr>';
}
$addresses = $vc->getAllAttributes('EMAIL');
$html .= '<tr><td colspan="2" class="header">';
if (($fullname = $vc->getAttributeDefault('FN', false)) === false) {
$fullname = count($addresses)
? $addresses[0]['value']
: Horde_Core_Translation::t("[No Label]");
}
$html .= htmlspecialchars($fullname) . '</td></tr>';
$n = $vc->printableName();
if (!empty($n)) {
$html .= $this->_row(Horde_Core_Translation::t("Name"), $n);
}
try {
$html .= $this->_row(Horde_Core_Translation::t("Alias"), implode("\n", $vc->getAttributeValues('ALIAS')));
} catch (Horde_Icalendar_Exception $e) {}
try {
$birthdays = $vc->getAttributeValues('BDAY');
$birthday = new Horde_Date($birthdays[0]);
$html .= $this->_row(
Horde_Core_Translation::t("Birthday"),
$birthday->strftime($prefs->getValue('date_format')));
} catch (Horde_Icalendar_Exception $e) {}
$photos = $vc->getAllAttributes('PHOTO');
foreach ($photos as $p => $photo) {
if (isset($photo['params']['VALUE']) &&
Horde_String::upper($photo['params']['VALUE']) == 'URI') {
$html .= $this->_row(Horde_Core_Translation::t("Photo"),
'<img src="' . htmlspecialchars($photo['value']) . '" />',
false);
} elseif (isset($photo['params']['ENCODING']) &&
Horde_String::upper($photo['params']['ENCODING']) == 'B' &&
isset($photo['params']['TYPE'])) {
if ($browser->hasFeature('datauri') === true ||
$browser->hasFeature('datauri') >= strlen($photo['value'])) {
$html .= $this->_row(Horde_Core_Translation::t("Photo"),
'<img src="' . Horde_Url_Data::create($photo['params']['TYPE'], base64_decode($photo['value'])) . '" />',
false);
} elseif ($this->_imageUrl) {
$html .= $this->_row(Horde_Core_Translation::t("Photo"),
'<img src="' . $this->_imageUrl->add(array('c' => $i, 'p' => $p)) . '" />',
false);
}
}
}
$labels = $vc->getAllAttributes('LABEL');
foreach ($labels as $label) {
if (isset($label['params']['TYPE'])) {
if (!is_array($label['params']['TYPE'])) {
$label['params']['TYPE'] = array($label['params']['TYPE']);
}
} else {
$label['params']['TYPE'] = array_keys($label['params']);
}
$types = array();
foreach ($label['params']['TYPE'] as $type) {
switch(Horde_String::upper($type)) {
case 'HOME':
$types[] = Horde_Core_Translation::t("Home Address");
break;
case 'WORK':
$types[] = Horde_Core_Translation::t("Work Address");
break;
case 'DOM':
$types[] = Horde_Core_Translation::t("Domestic Address");
break;
case 'INTL':
$types[] = Horde_Core_Translation::t("International Address");
break;
case 'POSTAL':
$types[] = Horde_Core_Translation::t("Postal Address");
break;
case 'PARCEL':
$types[] = Horde_Core_Translation::t("Parcel Address");
break;
case 'PREF':
$types[] = Horde_Core_Translation::t("Preferred Address");
break;
}
}
if (!count($types)) {
$types = array(Horde_Core_Translation::t("Address"));
}
$html .= $this->_row(implode('/', $types), $label['value']);
}
$adrs = $vc->getAllAttributes('ADR');
foreach ($adrs as $item) {
if (isset($item['params']['TYPE'])) {
if (!is_array($item['params']['TYPE'])) {
$item['params']['TYPE'] = array($item['params']['TYPE']);
}
} else {
$item['params']['TYPE'] = array_keys($item['params']);
}
$address = $item['values'];
$a = array();
$a_list = array(
Horde_Icalendar_Vcard::ADR_STREET,
Horde_Icalendar_Vcard::ADR_LOCALITY,
Horde_Icalendar_Vcard::ADR_REGION,
Horde_Icalendar_Vcard::ADR_POSTCODE,
Horde_Icalendar_Vcard::ADR_COUNTRY
);
foreach ($a_list as $val) {
if (isset($address[$val])) {
$a[] = $address[$val];
}
}
$types = array();
foreach ($item['params']['TYPE'] as $type) {
switch(Horde_String::upper($type)) {
case 'HOME':
$types[] = Horde_Core_Translation::t("Home Address");
break;
case 'WORK':
$types[] = Horde_Core_Translation::t("Work Address");
break;
case 'DOM':
$types[] = Horde_Core_Translation::t("Domestic Address");
break;
case 'INTL':
$types[] = Horde_Core_Translation::t("International Address");
break;
case 'POSTAL':
$types[] = Horde_Core_Translation::t("Postal Address");
break;
case 'PARCEL':
$types[] = Horde_Core_Translation::t("Parcel Address");
break;
case 'PREF':
$types[] = Horde_Core_Translation::t("Preferred Address");
break;
}
}
if (!count($types)) {
$types = array(Horde_Core_Translation::t("Address"));
}
$html .= $this->_row(implode('/', $types), implode("\n", $a));
}
$numbers = $vc->getAllAttributes('TEL');
foreach ($numbers as $number) {
if (isset($number['params']['TYPE'])) {
if (!is_array($number['params']['TYPE'])) {
$number['params']['TYPE'] = array($number['params']['TYPE']);
}
foreach ($number['params']['TYPE'] as $type) {
$number['params'][Horde_String::upper($type)] = true;
}
}
if (isset($number['params']['FAX'])) {
$html .= $this->_row(Horde_Core_Translation::t("Fax"), $number['value']);
} else {
if (isset($number['params']['HOME'])) {
$html .= $this->_row(Horde_Core_Translation::t("Home Phone"),
$number['value']);
} elseif (isset($number['params']['WORK'])) {
$html .= $this->_row(Horde_Core_Translation::t("Work Phone"),
$number['value']);
} elseif (isset($number['params']['CELL'])) {
$html .= $this->_row(Horde_Core_Translation::t("Cell Phone"),
$number['value']);
} else {
$html .= $this->_row(Horde_Core_Translation::t("Phone"),
$number['value']);
}
}
}
$emails = array();
foreach ($addresses as $address) {
if (isset($address['params']['TYPE'])) {
if (!is_array($address['params']['TYPE'])) {
$address['params']['TYPE'] = array($address['params']['TYPE']);
}
foreach ($address['params']['TYPE'] as $type) {
$address['params'][Horde_String::upper($type)] = true;
}
}
$email = '<a href="';
if ($registry->hasMethod('mail/compose')) {
$email .= $registry->call(
'mail/compose',
array(array('to' => $address['value'])));
} else {
$email .= 'mailto:' . htmlspecialchars($address['value']);
}
$email .= '">' . htmlspecialchars($address['value']) . '</a>';
if (isset($address['params']['PREF'])) {
array_unshift($emails, $email);
} else {
$emails[] = $email;
}
}
if (count($emails)) {
$html .= $this->_row(Horde_Core_Translation::t("Email"), implode("\n", $emails), false);
}
try {
$title = $vc->getAttributeValues('TITLE');
$html .= $this->_row(Horde_Core_Translation::t("Title"), $title[0]);
} catch (Horde_Icalendar_Exception $e) {}
try {
$role = $vc->getAttributeValues('ROLE');
$html .= $this->_row(Horde_Core_Translation::t("Role"), $role[0]);
} catch (Horde_Icalendar_Exception $e) {}
try {
$org = $vc->getAttributeValues('ORG');
$html .= $this->_row(Horde_Core_Translation::t("Company"), $org[0]);
if (isset($org[1])) {
$html .= $this->_row(Horde_Core_Translation::t("Department"), $org[1]);
}
} catch (Horde_Icalendar_Exception $e) {}
try {
$notes = $vc->getAttributeValues('NOTE');
$html .= $this->_row(Horde_Core_Translation::t("Notes"), $notes[0]);
} catch (Horde_Icalendar_Exception $e) {}
try {
$url = $vc->getAttributeValues('URL');
$html .= $this->_row(
Horde_Core_Translation::t("URL"),
'<a href="' . htmlspecialchars($url[0])
. '" target="_blank">' . htmlspecialchars($url[0])
. '</a>',
false);
} catch (Horde_Icalendar_Exception $e) {}
}
$html .= '</table>';
if ($registry->hasMethod('contacts/import') &&
$registry->hasMethod('contacts/sources')) {
$html .= '<div class="horde-form-buttons"><form action="'
. Horde::selfUrl() . '" method="get" name="vcard_import" id="vcard_import">'
. Horde_Util::formInput();
foreach ($_GET as $key => $val) {
$html .= '<input type="hidden" name="' . htmlspecialchars($key)
. '" value="' . htmlspecialchars($val) . '" />';
}
$sources = $registry->call('contacts/sources', array(true));
if (count($sources) > 1) {
$html .=
'<input type="submit" class="horde-default" name="import" value="'
. Horde_Core_Translation::t("Add to address book:") . '" />'
. ' <label for="add_source" class="hidden">'
. Horde_Core_Translation::t("Address Book") . '</label>'
. '<select id="add_source" name="source">';
foreach ($sources as $key => $label) {
$selected = ($key == $prefs->getValue('add_source'))
? ' selected="selected"' : '';
$html .= '<option value="' . htmlspecialchars($key) . '"'
. $selected . '>' . htmlspecialchars($label)
. '</option>';
}
$html .= '</select>';
} else {
reset($sources);
$html .=
'<input type="submit" class="horde-default" name="import" value="'
. Horde_Core_Translation::t("Add to my address book") . '" />'
. '<input type="hidden" name="source" value="'
. htmlspecialchars(key($sources)) . '" />';
}
$html .= '</form></div>';
}
Horde::startBuffer();
$notification->notify(array('listeners' => 'status'));
return $this->_renderReturn(
Horde::endBuffer() . $html,
'text/html; charset=' . $this->getConfigParam('charset')
);
}
/**
* TODO
*/
protected function _row($label, $value, $encode = true)
{
if ($encode) {
$label = htmlspecialchars($label);
$value = htmlspecialchars($value);
}
return '<tr><td>' . $label .
'</td><td>' . nl2br($value) .
"</td></tr>\n";
}
}