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;
39 if ($this->vcard_part)
40 $this->add_texts('localization');
44 * This callback function adds a box below the message content
45 * if there is a vcard attachment available
47 function html_output($p)
49 if ($this->vcard_part) {
50 $vcard = new rcube_vcard($this->message->get_part_content($this->vcard_part));
52 // successfully parsed vcard
53 if ($vcard->displayname) {
54 $display = $vcard->displayname;
56 $display .= ' <'.$vcard->email[0].'>';
58 // add box below messsage body
59 $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;"),
62 'onclick' => "return plugin_vcard_save_contact('".JQ($this->vcard_part)."')",
63 'title' => $this->gettext('addvardmsg')),
64 html::img(array('src' => $this->url('vcard_add_contact.png'), 'align' => "middle")))
65 . ' ' . html::span(null, Q($display)));
67 $this->include_script('vcardattach.js');
75 * Handler for request action
79 $this->add_texts('localization', true);
81 $uid = get_input_value('_uid', RCUBE_INPUT_POST);
82 $mbox = get_input_value('_mbox', RCUBE_INPUT_POST);
83 $mime_id = get_input_value('_part', RCUBE_INPUT_POST);
85 $rcmail = rcmail::get_instance();
86 $part = $uid && $mime_id ? $rcmail->imap->get_message_part($uid, $mime_id) : null;
88 $error_msg = $this->gettext('vcardsavefailed');
90 if ($part && ($vcard = new rcube_vcard($part)) && $vcard->displayname && $vcard->email) {
91 $contacts = $rcmail->get_address_book(null, true);
93 // check for existing contacts
94 $existing = $contacts->search('email', $vcard->email[0], true, false);
95 if ($done = $existing->count) {
96 $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
100 $success = $contacts->insert(array(
101 'name' => $vcard->displayname,
102 'firstname' => $vcard->firstname,
103 'surname' => $vcard->surname,
104 'email' => $vcard->email[0],
105 'vcard' => $vcard->export(),
109 $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
111 $rcmail->output->command('display_message', $error_msg, 'error');
115 $rcmail->output->command('display_message', $error_msg, 'error');
117 $rcmail->output->send();