]> git.donarmstrong.com Git - roundcube.git/blob - plugins/vcard_attachments/vcard_attachments.php
Imported Upstream version 0.3
[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   
40   /**
41    * This callback function adds a box below the message content
42    * if there is a vcard attachment available
43    */
44   function html_output($p)
45   {
46     if ($this->vcard_part) {
47       $vcard = new rcube_vcard($this->message->get_part_content($this->vcard_part));
48       
49       // successfully parsed vcard
50       if ($vcard->displayname) {
51         $display = $vcard->displayname;
52         if ($vcard->email[0])
53           $display .= ' <'.$vcard->email[0].'>';
54         
55         // add box below messsage body
56         $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;"),
57           html::a(array(
58               'href' => "#",
59               'onclick' => "return plugin_vcard_save_contact('".JQ($this->vcard_part)."')",
60               'title' => "Save contact in local address book"),  // TODO: localize this title
61             html::img(array('src' => $this->url('vcard_add_contact.png'), 'align' => "middle")))
62             . ' ' . html::span(null, Q($display)));
63         
64         $this->include_script('vcardattach.js');
65       }
66     }
67     
68     return $p;
69   }
70   
71   /**
72    * Handler for request action
73    */
74   function save_vcard()
75   {
76     $uid = get_input_value('_uid', RCUBE_INPUT_POST);
77     $mbox = get_input_value('_mbox', RCUBE_INPUT_POST);
78     $mime_id = get_input_value('_part', RCUBE_INPUT_POST);
79     
80     $rcmail = rcmail::get_instance();
81     $part = $uid && $mime_id ? $rcmail->imap->get_message_part($uid, $mime_id) : null;
82     
83     $error_msg = 'Failed to saved vcard'; // TODO: localize this text
84     
85     if ($part && ($vcard = new rcube_vcard($part)) && $vcard->displayname && $vcard->email) {
86       $contacts = $rcmail->get_address_book(null, true);
87       
88       // check for existing contacts
89       $existing = $contacts->search('email', $vcard->email[0], true, false);
90       if ($done = $existing->count) {
91         $rcmail->output->command('display_message', $this->gettext('contactexists'), 'warning');
92       }
93       else {
94         // add contact
95         $success = $contacts->insert(array(
96           'name' => $vcard->displayname,
97           'firstname' => $vcard->firstname,
98           'surname' => $vcard->surname,
99           'email' => $vcard->email[0],
100           'vcard' => $vcard->export(),
101         ));
102         
103         if ($success)
104           $rcmail->output->command('display_message', $this->gettext('addedsuccessfully'), 'confirmation');
105         else
106           $rcmail->output->command('display_message', $error_msg, 'error');
107       }
108     }
109     else
110       $rcmail->output->command('display_message', $error_msg, 'error');
111     
112     $rcmail->output->send();
113   }
114   
115 }