4 * Detect VCard attachments and show a button to add them to address book
7 * @author Thomas Bruederli
9 class vcard_attachments extends rcube_plugin
11 public $task = 'mail';
18 $rcmail = rcmail::get_instance();
19 if ($rcmail->action == 'show' || $rcmail->action == 'preview') {
20 $this->add_hook('message_load', array($this, 'message_load'));
21 $this->add_hook('template_object_messagebody', array($this, 'html_output'));
24 $this->register_action('plugin.savevcard', array($this, 'save_vcard'));
28 * Check message attachments for vcards
30 function message_load($p)
32 $this->message = $p['object'];
34 foreach ((array)$this->message->attachments as $attachment) {
35 if (in_array($attachment->mimetype, array('text/vcard', 'text/x-vcard')))
36 $this->vcard_part = $attachment->mime_id;
41 * This callback function adds a box below the message content
42 * if there is a vcard attachment available
44 function html_output($p)
46 if ($this->vcard_part) {
47 $vcard = new rcube_vcard($this->message->get_part_content($this->vcard_part));
49 // successfully parsed vcard
50 if ($vcard->displayname) {
51 $display = $vcard->displayname;
53 $display .= ' <'.$vcard->email[0].'>';
55 // add box below messsage body
56 $p['content'] .= html::p(array('style' => "margin:1em; padding:0.5em; border:1px solid #999; border-radius:4px; -moz-border-radius:4px; -webkit-border-radius:4px; width: auto;"),
59 'onclick' => "return plugin_vcard_save_contact('".JQ($this->vcard_part)."')",
60 'title' => "Save contact in local address book"), // TODO: localize this title
61 html::img(array('src' => $this->url('vcard_add_contact.png'), 'align' => "middle")))
62 . ' ' . html::span(null, Q($display)));
64 $this->include_script('vcardattach.js');
72 * Handler for request action
76 $uid = get_input_value('_uid', RCUBE_INPUT_POST);
77 $mbox = get_input_value('_mbox', RCUBE_INPUT_POST);
78 $mime_id = get_input_value('_part', RCUBE_INPUT_POST);
80 $rcmail = rcmail::get_instance();
81 $part = $uid && $mime_id ? $rcmail->imap->get_message_part($uid, $mime_id) : null;
83 $error_msg = 'Failed to saved vcard'; // TODO: localize this text
85 if ($part && ($vcard = new rcube_vcard($part)) && $vcard->displayname && $vcard->email) {
86 $contacts = $rcmail->get_address_book(null, true);
88 // check for existing contacts
89 $existing = $contacts->search('email', $vcard->email[0], true, false);
90 if ($done = $existing->count) {
91 $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
95 $success = $contacts->insert(array(
96 'name' => $vcard->displayname,
97 'firstname' => $vcard->firstname,
98 'surname' => $vcard->surname,
99 'email' => $vcard->email[0],
100 'vcard' => $vcard->export(),
104 $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
106 $rcmail->output->command('display_message', $error_msg, 'error');
110 $rcmail->output->command('display_message', $error_msg, 'error');
112 $rcmail->output->send();