]> git.donarmstrong.com Git - roundcube.git/blob - plugins/vcard_attachments/vcard_attachments.php
New upstream release
[roundcube.git] / plugins / vcard_attachments / vcard_attachments.php
1 <?php
2
3 /**
4  * Detect VCard attachments and show a button to add them to address book
5  *
6  * @version @package_version@
7  * @author Thomas Bruederli, Aleksander Machniak
8  */
9 class vcard_attachments extends rcube_plugin
10 {
11     public $task = 'mail';
12
13     private $message;
14     private $vcard_parts = array();
15     private $vcard_bodies = array();
16
17     function init()
18     {
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'));
23         }
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');
28         }
29
30         $this->register_action('plugin.savevcard', array($this, 'save_vcard'));
31     }
32
33     /**
34      * Check message bodies and attachments for vcards
35      */
36     function message_load($p)
37     {
38         $this->message = $p['object'];
39
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;
44             }
45         }
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;
51             }
52         }
53
54         if ($this->vcard_parts)
55             $this->add_texts('localization');
56     }
57
58     /**
59      * This callback function adds a box below the message content
60      * if there is a vcard attachment available
61      */
62     function html_output($p)
63     {
64         $attach_script = false;
65         $icon = 'plugins/vcard_attachments/' .$this->local_skin_path(). '/vcard_add_contact.png';
66
67         foreach ($this->vcard_parts as $part) {
68             $vcards = rcube_vcard::import($this->message->get_part_content($part));
69
70             // successfully parsed vcards?
71             if (empty($vcards))
72                 continue;
73
74             // remove part's body
75             if (in_array($part, $this->vcard_bodies))
76                 $p['content'] = '';
77
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';
80
81             foreach ($vcards as $idx => $vcard) {
82                 $display = $vcard->displayname;
83                 if ($vcard->email[0])
84                     $display .= ' <'.$vcard->email[0].'>';
85
86                 // add box below messsage body
87                 $p['content'] .= html::p(array('style' => $style),
88                     html::a(array(
89                         'href' => "#",
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)));
94             }
95
96             $attach_script = true;
97         }
98
99         if ($attach_script)
100             $this->include_script('vcardattach.js');
101
102         return $p;
103     }
104
105     /**
106      * Handler for request action
107      */
108     function save_vcard()
109     {
110             $this->add_texts('localization', true);
111
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);
115
116         $rcmail = rcmail::get_instance();
117
118         if ($uid && $mime_id) {
119             list($mime_id, $index) = explode(':', $mime_id);
120             $part = $rcmail->imap->get_message_part($uid, $mime_id);
121         }
122
123         $error_msg = $this->gettext('vcardsavefailed');
124
125         if ($part && ($vcards = rcube_vcard::import($part))
126             && ($vcard = $vcards[$index]) && $vcard->displayname && $vcard->email) {
127
128             $contacts = $rcmail->get_address_book(null, true);
129
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');
134             }
135             else {
136                 // add contact
137                 $contact = array(
138                     'name'      => $vcard->displayname,
139                     'firstname' => $vcard->firstname,
140                     'surname'   => $vcard->surname,
141                     'email'     => $vcard->email[0],
142                     'vcard'     => $vcard->export(),
143                 );
144
145                 $plugin = $rcmail->plugins->exec_hook('contact_create', array('record' => $contact, 'source' => null));
146                 $contact = $plugin['record'];
147
148                 if (!$plugin['abort'] && ($done = $contacts->insert($contact)))
149                     $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
150                 else
151                     $rcmail->output->command('display_message', $error_msg, 'error');
152             }
153         }
154         else
155             $rcmail->output->command('display_message', $error_msg, 'error');
156
157         $rcmail->output->send();
158     }
159
160     /**
161      * Checks if specified message part is a vcard data
162      *
163      * @param rcube_message_part Part object
164      *
165      * @return boolean True if part is of type vcard
166      */
167     function is_vcard($part)
168     {
169         return (
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))
180                 )
181             )
182         );
183     }
184 }