vcard_attachments.php
14.1 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
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
<?php
/**
* Detects VCard attachments and show a button to add them to address book
* Adds possibility to attach a contact vcard to mail messages
*
* @license GNU GPLv3+
* @author Thomas Bruederli, Aleksander Machniak
*/
class vcard_attachments extends rcube_plugin
{
public $task = 'mail|addressbook';
private $abook;
private $message;
private $vcard_parts = [];
private $vcard_bodies = [];
/**
* Plugin initialization
*/
function init()
{
$rcmail = rcmail::get_instance();
if ($rcmail->task == 'addressbook') {
$skin_path = $this->local_skin_path();
$this->add_texts('localization', !$rcmail->output->ajax_call);
$this->include_stylesheet($skin_path . '/style.css');
$this->include_script('vcardattach.js');
$this->add_button([
'type' => 'link-menuitem',
'label' => 'vcard_attachments.forwardvcard',
'command' => 'attach-vcard',
'class' => 'icon vcard',
'classact' => 'icon vcard active',
'innerclass' => 'icon vcard',
],
'contactmenu'
);
}
else {
if ($rcmail->action == 'show' || $rcmail->action == 'preview') {
$this->add_hook('message_load', [$this, 'message_load']);
$this->add_hook('message_objects', [$this, 'message_objects']);
$this->add_hook('template_object_messagebody', [$this, 'html_output']);
}
else if ($rcmail->action == 'upload') {
$this->add_hook('attachment_from_uri', [$this, 'attach_vcard']);
}
else if ($rcmail->action == 'compose' && !$rcmail->output->framed) {
$skin_path = $this->local_skin_path();
$btn_class = strpos($skin_path, 'classic') ? 'button' : 'listbutton';
$this->add_texts('localization', true);
$this->include_stylesheet($skin_path . '/style.css');
$this->include_script('vcardattach.js');
$this->add_button([
'type' => 'link',
'label' => 'vcard_attachments.vcard',
'command' => 'attach-vcard',
'class' => $btn_class . ' vcard disabled',
'classact' => $btn_class . ' vcard',
'title' => 'vcard_attachments.attachvcard',
'innerclass' => 'inner',
],
'compose-contacts-toolbar'
);
$this->add_hook('message_compose', [$this, 'message_compose']);
}
else if (!$rcmail->output->framed && (!$rcmail->action || $rcmail->action == 'list')) {
$skin_path = $this->local_skin_path();
$this->include_stylesheet($skin_path . '/style.css');
$this->include_script('vcardattach.js');
}
}
$this->register_action('plugin.savevcard', [$this, 'save_vcard']);
}
/**
* Check message bodies and attachments for vcards
*/
function message_load($p)
{
$this->message = $p['object'];
// handle attachments vcard attachments
foreach ((array)$this->message->attachments as $attachment) {
if ($this->is_vcard($attachment)) {
$this->vcard_parts[] = $attachment->mime_id;
}
}
// the same with message bodies
foreach ((array)$this->message->parts as $part) {
if ($this->is_vcard($part)) {
$this->vcard_parts[] = $part->mime_id;
$this->vcard_bodies[] = $part->mime_id;
}
}
if ($this->vcard_parts) {
$this->add_texts('localization');
}
}
/**
* This callback function adds a box above the message content
* if there is a vcard attachment available
*/
function message_objects($p)
{
$rcmail = rcmail::get_instance();
$contacts = [];
foreach ($this->vcard_parts as $part) {
$vcards = rcube_vcard::import($this->message->get_part_content($part, null, true));
foreach ($vcards as $idx => $vcard) {
// skip invalid vCards
if (empty($vcard->email) || empty($vcard->email[0])) {
continue;
}
$contacts["$part:$idx"] = "{$vcard->displayname} <{$vcard->email[0]}>";
}
}
if (!empty($contacts)) {
$attr = [
'title' => $this->gettext('addvcardmsg'),
'class' => 'import btn-sm',
];
if (count($contacts) == 1) {
$display = array_first($contacts);
$attr['onclick'] = "return plugin_vcard_import('" . rcube::JQ(key($contacts)) . "')";
}
else {
$display = $this->gettext(['name' => 'contactsattached', 'vars' => ['num' => count($contacts)]]);
$attr['onclick'] = "return plugin_vcard_import()";
$rcmail->output->set_env('vcards', $contacts);
$rcmail->output->add_label('vcard_attachments.addvcardmsg', 'import');
}
// add box below the message body
$p['content'][] = html::p(
['class' => 'vcardattachment aligned-buttons boxinformation'],
html::span(null, rcube::Q($display)) . html::tag('button', $attr, rcube::Q($rcmail->gettext('import')))
);
$this->include_script('vcardattach.js');
$this->include_stylesheet($this->local_skin_path() . '/style.css');
}
return $p;
}
/**
* This callback function adds a vCard to the message when attached from the Address book
*/
function message_compose($p)
{
if (
rcube_utils::get_input_value('_attach_vcard', rcube_utils::INPUT_GET) == '1'
&& ($uri = rcube_utils::get_input_value('_uri', rcube_utils::INPUT_GET))
) {
if ($attachment = $this->attach_vcard(['compose_id' => $p['id'], 'uri' => $uri])) {
$p['attachments'][] = $attachment;
};
}
return $p;
}
/**
* This callback function removes message part's content
* for parts that are vcards
*/
function html_output($p)
{
foreach ($this->vcard_parts as $part) {
// remove part's body
if (in_array($part, $this->vcard_bodies)) {
$p['content'] = '';
}
}
return $p;
}
/**
* Handler for request action
*/
function save_vcard()
{
$this->add_texts('localization');
$uid = rcube_utils::get_input_value('_uid', rcube_utils::INPUT_POST);
$mbox = rcube_utils::get_input_value('_mbox', rcube_utils::INPUT_POST);
$mime_id = rcube_utils::get_input_value('_part', rcube_utils::INPUT_POST);
$rcmail = rcmail::get_instance();
$message = new rcube_message($uid, $mbox);
$vcards = [];
$errors = 0;
if (!empty($message->headers) && $uid && $mime_id) {
$index = [];
foreach (explode(',', $mime_id) as $id) {
list($part_id, $card_id) = rcube_utils::explode(':', $id);
if (!isset($index[$part_id])) {
$index[$part_id] = [];
}
$index[$part_id][] = $card_id;
}
foreach ($index as $part_id => $mime_ids) {
$part = $message->get_part_content($part_id, null, true);
if (!empty($part) && ($part_vcards = rcube_vcard::import($part))) {
foreach ($mime_ids as $id) {
if (!empty($part_vcards[$id])
&& ($vcard = $part_vcards[$id])
&& !empty($vcard->email)
&& !empty($vcard->email[0])
) {
$vcards[] = $vcard;
}
}
}
}
}
$CONTACTS = $this->get_address_book();
foreach ($vcards as $vcard) {
$email = $vcard->email[0];
$contact = $vcard->get_assoc();
$valid = true;
// skip entries without an e-mail address or invalid
if (empty($email) || !$CONTACTS->validate($contact, true)) {
$valid = false;
}
else {
// We're using UTF8 internally
$email = rcube_utils::idn_to_utf8($email);
// compare e-mail address
$existing = $CONTACTS->search('email', $email, 1, false);
// compare display name
if (!$existing->count && !empty($vcard->displayname)) {
$existing = $CONTACTS->search('name', $vcard->displayname, 1, false);
}
if ($existing->count) {
// $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
$valid = false;
}
}
if ($valid) {
$plugin = $rcmail->plugins->exec_hook('contact_create', ['record' => $contact, 'source' => null]);
$contact = $plugin['record'];
if (!$plugin['abort'] && $CONTACTS->insert($contact)) {
// do nothing
}
else {
$errors++;
}
}
}
if ($errors || empty($vcards)) {
$rcmail->output->command('display_message', $this->gettext('vcardsavefailed'), 'error');
}
else {
$rcmail->output->command('display_message', $this->gettext('importedsuccessfully'), 'confirmation');
}
$rcmail->output->send();
}
/**
* Checks if specified message part is a vcard data
*
* @param rcube_message_part Part object
*
* @return bool True if part is of type vcard
*/
private static function is_vcard($part)
{
return (
// Content-Type: text/vcard;
$part->mimetype == 'text/vcard' ||
// Content-Type: text/x-vcard;
$part->mimetype == 'text/x-vcard' ||
// Content-Type: text/directory; profile=vCard;
($part->mimetype == 'text/directory' && (
(!empty($part->ctype_parameters['profile']) && strtolower($part->ctype_parameters['profile']) == 'vcard')
// Content-Type: text/directory; (with filename=*.vcf)
|| (!empty($part->filename) && preg_match('/\.vcf$/i', $part->filename))
)
)
);
}
/**
* Getter for default (writable) addressbook
*/
private function get_address_book()
{
if (!empty($this->abook)) {
return $this->abook;
}
$rcmail = rcmail::get_instance();
// Get configured addressbook
$CONTACTS = $rcmail->get_address_book(rcube_addressbook::TYPE_DEFAULT, true);
// Get first writeable addressbook if the configured doesn't exist
// This can happen when user deleted the addressbook (e.g. Kolab folder)
if (!is_object($CONTACTS)) {
$source = reset($rcmail->get_address_sources(true));
$CONTACTS = $rcmail->get_address_book($source['id'], true);
}
return $this->abook = $CONTACTS;
}
/**
* Attaches a contact vcard to composed mail
*/
public function attach_vcard($args)
{
if (preg_match('|^vcard://(.+)$|', $args['uri'], $m)) {
list($source, $cid, $email) = explode('-', $m[1]);
$vcard = $this->get_contact_vcard($source, $cid, $filename);
if ($vcard) {
$params = [
'filename' => $filename,
'mimetype' => 'text/vcard',
];
$args['attachment'] = rcmail_action_mail_compose::save_attachment($vcard, null, $args['compose_id'], $params);
}
}
return $args;
}
/**
* Get vcard data for specified contact
*/
private function get_contact_vcard($source, $cid, &$filename = null)
{
$rcmail = rcmail::get_instance();
$source = $rcmail->get_address_book($source);
$contact = $source->get_record($cid, true);
if ($contact) {
$fieldmap = $source ? $source->vcard_map : null;
if (empty($contact['vcard'])) {
$vcard = new rcube_vcard('', RCUBE_CHARSET, false, $fieldmap);
$vcard->reset();
foreach ($contact as $key => $values) {
list($field, $section) = rcube_utils::explode(':', $key);
// avoid unwanted casting of DateTime objects to an array
// (same as in rcube_contacts::convert_save_data())
if (is_object($values) && is_a($values, 'DateTime')) {
$values = [$values];
}
foreach ((array) $values as $value) {
if (is_array($value) || is_a($value, 'DateTime') || @strlen($value)) {
$vcard->set($field, $value, strtoupper($section));
}
}
}
$contact['vcard'] = $vcard->export();
}
$name = rcube_addressbook::compose_list_name($contact);
$filename = (self::parse_filename($name) ?: 'contact') . '.vcf';
// fix folding and end-of-line chars
$vcard = preg_replace('/\r|\n\s+/', '', $contact['vcard']);
$vcard = preg_replace('/\n/', rcube_vcard::$eol, $vcard);
return rcube_vcard::rfc2425_fold($vcard) . rcube_vcard::$eol;
}
}
/**
* Helper function to convert contact name into filename
*/
static private function parse_filename($str)
{
$str = preg_replace('/[\t\n\r\0\x0B:\/]+\s*/', ' ', $str);
return trim($str, " ./_");
}
}