4 * Detect VCard attachments and show a button to add them to address book
6 * @version @package_version@
7 * @author Thomas Bruederli, Aleksander Machniak
9 class vcard_attachments extends rcube_plugin
11 public $task = 'mail';
14 private $vcard_parts = array();
15 private $vcard_bodies = array();
19 $rcmail = rcmail::get_instance();
20 if ($rcmail->action == 'show' || $rcmail->action == 'preview') {
21 $this->add_hook('message_load', array($this, 'message_load'));
22 $this->add_hook('template_object_messagebody', array($this, 'html_output'));
24 else if (!$rcmail->output->framed && (!$rcmail->action || $rcmail->action == 'list')) {
25 $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard.png';
26 $rcmail->output->set_env('vcard_icon', $icon);
27 $this->include_script('vcardattach.js');
30 $this->register_action('plugin.savevcard', array($this, 'save_vcard'));
34 * Check message bodies and attachments for vcards
36 function message_load($p)
38 $this->message = $p['object'];
40 // handle attachments vcard attachments
41 foreach ((array)$this->message->attachments as $attachment) {
42 if ($this->is_vcard($attachment)) {
43 $this->vcard_parts[] = $attachment->mime_id;
46 // the same with message bodies
47 foreach ((array)$this->message->parts as $idx => $part) {
48 if ($this->is_vcard($part)) {
49 $this->vcard_parts[] = $part->mime_id;
50 $this->vcard_bodies[] = $part->mime_id;
54 if ($this->vcard_parts)
55 $this->add_texts('localization');
59 * This callback function adds a box below the message content
60 * if there is a vcard attachment available
62 function html_output($p)
64 $attach_script = false;
65 $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard_add_contact.png';
67 foreach ($this->vcard_parts as $part) {
68 $vcards = rcube_vcard::import($this->message->get_part_content($part));
70 // successfully parsed vcards?
75 if (in_array($part, $this->vcard_bodies))
78 $style = 'margin:0.5em 1em; padding:0.2em 0.5em; border:1px solid #999; '
79 .'border-radius:4px; -moz-border-radius:4px; -webkit-border-radius:4px; width: auto';
81 foreach ($vcards as $idx => $vcard) {
82 $display = $vcard->displayname;
84 $display .= ' <'.$vcard->email[0].'>';
86 // add box below messsage body
87 $p['content'] .= html::p(array('style' => $style),
90 'onclick' => "return plugin_vcard_save_contact('".JQ($part.':'.$idx)."')",
91 'title' => $this->gettext('addvcardmsg')),
92 html::img(array('src' => $icon, 'style' => "vertical-align:middle")))
93 . ' ' . html::span(null, Q($display)));
96 $attach_script = true;
100 $this->include_script('vcardattach.js');
106 * Handler for request action
108 function save_vcard()
110 $this->add_texts('localization', true);
112 $uid = get_input_value('_uid', RCUBE_INPUT_POST);
113 $mbox = get_input_value('_mbox', RCUBE_INPUT_POST);
114 $mime_id = get_input_value('_part', RCUBE_INPUT_POST);
116 $rcmail = rcmail::get_instance();
118 if ($uid && $mime_id) {
119 list($mime_id, $index) = explode(':', $mime_id);
120 $part = $rcmail->imap->get_message_part($uid, $mime_id);
123 $error_msg = $this->gettext('vcardsavefailed');
125 if ($part && ($vcards = rcube_vcard::import($part))
126 && ($vcard = $vcards[$index]) && $vcard->displayname && $vcard->email) {
128 $contacts = $rcmail->get_address_book(null, true);
130 // check for existing contacts
131 $existing = $contacts->search('email', $vcard->email[0], true, false);
132 if ($existing->count) {
133 $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
138 'name' => $vcard->displayname,
139 'firstname' => $vcard->firstname,
140 'surname' => $vcard->surname,
141 'email' => $vcard->email[0],
142 'vcard' => $vcard->export(),
145 $plugin = $rcmail->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
146 $contact = $plugin['record'];
148 if (!$plugin['abort'] && ($done = $contacts->insert($contact)))
149 $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
151 $rcmail->output->command('display_message', $error_msg, 'error');
155 $rcmail->output->command('display_message', $error_msg, 'error');
157 $rcmail->output->send();
161 * Checks if specified message part is a vcard data
163 * @param rcube_message_part Part object
165 * @return boolean True if part is of type vcard
167 function is_vcard($part)
170 // Content-Type: text/vcard;
171 $part->mimetype == 'text/vcard' ||
172 // Content-Type: text/x-vcard;
173 $part->mimetype == 'text/x-vcard' ||
174 // Content-Type: text/directory; profile=vCard;
175 ($part->mimetype == 'text/directory' && (
176 ($part->ctype_parameters['profile'] &&
177 strtolower($part->ctype_parameters['profile']) == 'vcard')
178 // Content-Type: text/directory; (with filename=*.vcf)
179 || ($part->filename && preg_match('/\.vcf$/i', $part->filename))