| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Detect VCard attachments and show a button to add them to address book |
|---|
| 5 | * |
|---|
| 6 | * @version @package_version@ |
|---|
| 7 | * @license GNU GPLv3+ |
|---|
| 8 | * @author Thomas Bruederli, Aleksander Machniak |
|---|
| 9 | */ |
|---|
| 10 | class vcard_attachments extends rcube_plugin |
|---|
| 11 | { |
|---|
| 12 | public $task = 'mail'; |
|---|
| 13 | |
|---|
| 14 | private $message; |
|---|
| 15 | private $vcard_parts = array(); |
|---|
| 16 | private $vcard_bodies = array(); |
|---|
| 17 | |
|---|
| 18 | function init() |
|---|
| 19 | { |
|---|
| 20 | $rcmail = rcmail::get_instance(); |
|---|
| 21 | if ($rcmail->action == 'show' || $rcmail->action == 'preview') { |
|---|
| 22 | $this->add_hook('message_load', array($this, 'message_load')); |
|---|
| 23 | $this->add_hook('template_object_messagebody', array($this, 'html_output')); |
|---|
| 24 | } |
|---|
| 25 | else if (!$rcmail->output->framed && (!$rcmail->action || $rcmail->action == 'list')) { |
|---|
| 26 | $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard.png'; |
|---|
| 27 | $rcmail->output->set_env('vcard_icon', $icon); |
|---|
| 28 | $this->include_script('vcardattach.js'); |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | $this->register_action('plugin.savevcard', array($this, 'save_vcard')); |
|---|
| 32 | } |
|---|
| 33 | |
|---|
| 34 | /** |
|---|
| 35 | * Check message bodies and attachments for vcards |
|---|
| 36 | */ |
|---|
| 37 | function message_load($p) |
|---|
| 38 | { |
|---|
| 39 | $this->message = $p['object']; |
|---|
| 40 | |
|---|
| 41 | // handle attachments vcard attachments |
|---|
| 42 | foreach ((array)$this->message->attachments as $attachment) { |
|---|
| 43 | if ($this->is_vcard($attachment)) { |
|---|
| 44 | $this->vcard_parts[] = $attachment->mime_id; |
|---|
| 45 | } |
|---|
| 46 | } |
|---|
| 47 | // the same with message bodies |
|---|
| 48 | foreach ((array)$this->message->parts as $idx => $part) { |
|---|
| 49 | if ($this->is_vcard($part)) { |
|---|
| 50 | $this->vcard_parts[] = $part->mime_id; |
|---|
| 51 | $this->vcard_bodies[] = $part->mime_id; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | if ($this->vcard_parts) |
|---|
| 56 | $this->add_texts('localization'); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /** |
|---|
| 60 | * This callback function adds a box below the message content |
|---|
| 61 | * if there is a vcard attachment available |
|---|
| 62 | */ |
|---|
| 63 | function html_output($p) |
|---|
| 64 | { |
|---|
| 65 | $attach_script = false; |
|---|
| 66 | $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard_add_contact.png'; |
|---|
| 67 | |
|---|
| 68 | foreach ($this->vcard_parts as $part) { |
|---|
| 69 | $vcards = rcube_vcard::import($this->message->get_part_content($part)); |
|---|
| 70 | |
|---|
| 71 | // successfully parsed vcards? |
|---|
| 72 | if (empty($vcards)) |
|---|
| 73 | continue; |
|---|
| 74 | |
|---|
| 75 | // remove part's body |
|---|
| 76 | if (in_array($part, $this->vcard_bodies)) |
|---|
| 77 | $p['content'] = ''; |
|---|
| 78 | |
|---|
| 79 | foreach ($vcards as $idx => $vcard) { |
|---|
| 80 | $display = $vcard->displayname; |
|---|
| 81 | if ($vcard->email[0]) |
|---|
| 82 | $display .= ' <'.$vcard->email[0].'>'; |
|---|
| 83 | |
|---|
| 84 | // add box below messsage body |
|---|
| 85 | $p['content'] .= html::p(array('class' => 'vcardattachment'), |
|---|
| 86 | html::a(array( |
|---|
| 87 | 'href' => "#", |
|---|
| 88 | 'onclick' => "return plugin_vcard_save_contact('" . JQ($part.':'.$idx) . "')", |
|---|
| 89 | 'title' => $this->gettext('addvcardmsg'), |
|---|
| 90 | ), |
|---|
| 91 | html::span(null, Q($display))) |
|---|
| 92 | ); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | $attach_script = true; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | if ($attach_script) { |
|---|
| 99 | $this->include_script('vcardattach.js'); |
|---|
| 100 | $this->include_stylesheet($this->local_skin_path() . '/style.css'); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | return $p; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | /** |
|---|
| 107 | * Handler for request action |
|---|
| 108 | */ |
|---|
| 109 | function save_vcard() |
|---|
| 110 | { |
|---|
| 111 | $this->add_texts('localization', true); |
|---|
| 112 | |
|---|
| 113 | $uid = get_input_value('_uid', RCUBE_INPUT_POST); |
|---|
| 114 | $mbox = get_input_value('_mbox', RCUBE_INPUT_POST); |
|---|
| 115 | $mime_id = get_input_value('_part', RCUBE_INPUT_POST); |
|---|
| 116 | |
|---|
| 117 | $rcmail = rcmail::get_instance(); |
|---|
| 118 | |
|---|
| 119 | if ($uid && $mime_id) { |
|---|
| 120 | list($mime_id, $index) = explode(':', $mime_id); |
|---|
| 121 | $part = $rcmail->storage->get_message_part($uid, $mime_id); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | $error_msg = $this->gettext('vcardsavefailed'); |
|---|
| 125 | |
|---|
| 126 | if ($part && ($vcards = rcube_vcard::import($part)) |
|---|
| 127 | && ($vcard = $vcards[$index]) && $vcard->displayname && $vcard->email) { |
|---|
| 128 | |
|---|
| 129 | $contacts = $rcmail->get_address_book(null, true); |
|---|
| 130 | |
|---|
| 131 | // check for existing contacts |
|---|
| 132 | $existing = $contacts->search('email', $vcard->email[0], true, false); |
|---|
| 133 | if ($existing->count) { |
|---|
| 134 | $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning'); |
|---|
| 135 | } |
|---|
| 136 | else { |
|---|
| 137 | // add contact |
|---|
| 138 | $contact = array( |
|---|
| 139 | 'name' => $vcard->displayname, |
|---|
| 140 | 'firstname' => $vcard->firstname, |
|---|
| 141 | 'surname' => $vcard->surname, |
|---|
| 142 | 'email' => $vcard->email[0], |
|---|
| 143 | 'vcard' => $vcard->export(), |
|---|
| 144 | ); |
|---|
| 145 | |
|---|
| 146 | $plugin = $rcmail->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null)); |
|---|
| 147 | $contact = $plugin['record']; |
|---|
| 148 | |
|---|
| 149 | if (!$plugin['abort'] && ($done = $contacts->insert($contact))) |
|---|
| 150 | $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation'); |
|---|
| 151 | else |
|---|
| 152 | $rcmail->output->command('display_message', $error_msg, 'error'); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | else |
|---|
| 156 | $rcmail->output->command('display_message', $error_msg, 'error'); |
|---|
| 157 | |
|---|
| 158 | $rcmail->output->send(); |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | /** |
|---|
| 162 | * Checks if specified message part is a vcard data |
|---|
| 163 | * |
|---|
| 164 | * @param rcube_message_part Part object |
|---|
| 165 | * |
|---|
| 166 | * @return boolean True if part is of type vcard |
|---|
| 167 | */ |
|---|
| 168 | function is_vcard($part) |
|---|
| 169 | { |
|---|
| 170 | return ( |
|---|
| 171 | // Content-Type: text/vcard; |
|---|
| 172 | $part->mimetype == 'text/vcard' || |
|---|
| 173 | // Content-Type: text/x-vcard; |
|---|
| 174 | $part->mimetype == 'text/x-vcard' || |
|---|
| 175 | // Content-Type: text/directory; profile=vCard; |
|---|
| 176 | ($part->mimetype == 'text/directory' && ( |
|---|
| 177 | ($part->ctype_parameters['profile'] && |
|---|
| 178 | strtolower($part->ctype_parameters['profile']) == 'vcard') |
|---|
| 179 | // Content-Type: text/directory; (with filename=*.vcf) |
|---|
| 180 | || ($part->filename && preg_match('/\.vcf$/i', $part->filename)) |
|---|
| 181 | ) |
|---|
| 182 | ) |
|---|
| 183 | ); |
|---|
| 184 | } |
|---|
| 185 | } |
|---|