]> git.donarmstrong.com Git - roundcube.git/blob - plugins/vcard_attachments/vcard_attachments.php
Merge commit 'debian/0.3.1-1' into debian
[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 1.0
7  * @author Thomas Bruederli
8  */
9 class vcard_attachments extends rcube_plugin
10 {
11   public $task = 'mail';
12   
13   private $message;
14   private $vcard_part;
15
16   function init()
17   {
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'));
22     }
23     
24     $this->register_action('plugin.savevcard', array($this, 'save_vcard'));
25   }
26   
27   /**
28    * Check message attachments for vcards
29    */
30   function message_load($p)
31   {
32     $this->message = $p['object'];
33     
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;
37     }
38
39     if ($this->vcard_part)
40       $this->add_texts('localization');
41   }
42   
43   /**
44    * This callback function adds a box below the message content
45    * if there is a vcard attachment available
46    */
47   function html_output($p)
48   {
49     if ($this->vcard_part) {
50       $vcard = new rcube_vcard($this->message->get_part_content($this->vcard_part));
51       
52       // successfully parsed vcard
53       if ($vcard->displayname) {
54         $display = $vcard->displayname;
55         if ($vcard->email[0])
56           $display .= ' <'.$vcard->email[0].'>';
57         
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;"),
60           html::a(array(
61               'href' => "#",
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)));
66         
67         $this->include_script('vcardattach.js');
68       }
69     }
70     
71     return $p;
72   }
73   
74   /**
75    * Handler for request action
76    */
77   function save_vcard()
78   {
79           $this->add_texts('localization', true);
80
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);
84     
85     $rcmail = rcmail::get_instance();
86     $part = $uid && $mime_id ? $rcmail->imap->get_message_part($uid, $mime_id) : null;
87     
88     $error_msg = $this->gettext('vcardsavefailed');
89     
90     if ($part && ($vcard = new rcube_vcard($part)) && $vcard->displayname && $vcard->email) {
91       $contacts = $rcmail->get_address_book(null, true);
92       
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');
97       }
98       else {
99         // add contact
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(),
106         ));
107         
108         if ($success)
109           $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
110         else
111           $rcmail->output->command('display_message', $error_msg, 'error');
112       }
113     }
114     else
115       $rcmail->output->command('display_message', $error_msg, 'error');
116     
117     $rcmail->output->send();
118   }
119 }