]> git.donarmstrong.com Git - roundcube.git/blob - plugins/virtuser_query/virtuser_query.php
Imported Upstream version 0.5.2+dfsg
[roundcube.git] / plugins / virtuser_query / virtuser_query.php
1 <?php
2
3 /**
4  * DB based User-to-Email and Email-to-User lookup
5  *
6  * Add it to the plugins list in config/main.inc.php and set
7  * SQL queries to resolve usernames, e-mail addresses and hostnames from the database
8  * %u will be replaced with the current username for login.
9  * %m will be replaced with the current e-mail address for login.
10  *
11  * Queries should select the user's e-mail address, username or the imap hostname as first column
12  * The email query could optionally select identity data columns in specified order:
13  *    name, organization, reply-to, bcc, signature, html_signature
14  *
15  * $rcmail_config['virtuser_query'] = array('email' => '', 'user' => '', 'host' => '');
16  *
17  * @version 1.1
18  * @author Aleksander Machniak
19  * @author Steffen Vogel
20  */
21 class virtuser_query extends rcube_plugin
22 {
23     private $config;
24     private $app;
25
26     function init()
27     {
28             $this->app = rcmail::get_instance();
29             $this->config = $this->app->config->get('virtuser_query');
30
31         if (!empty($this->config)) {
32             if (is_string($this->config)) {
33                 $this->config = array('email' => $this->config);
34             }
35
36             if ($this->config['email']) {
37                 $this->add_hook('user2email', array($this, 'user2email'));
38             }
39             if ($this->config['user']) {
40                 $this->add_hook('email2user', array($this, 'email2user'));
41             }
42             if ($this->config['host']) {
43                 $this->add_hook('authenticate', array($this, 'user2host'));
44             }
45         }
46     }
47
48     /**
49      * User > Email
50      */
51     function user2email($p)
52     {
53             $dbh = $this->app->get_dbh();
54
55             $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escapeSimple($p['user']), $this->config['email']));
56
57             while ($sql_arr = $dbh->fetch_array($sql_result)) {
58                 if (strpos($sql_arr[0], '@')) {
59                         if ($p['extended'] && count($sql_arr) > 1) {
60                             $result[] = array(
61                                     'email'         => rcube_idn_to_ascii($sql_arr[0]),
62                                 'name'              => $sql_arr[1],
63                                     'organization'  => $sql_arr[2],
64                                 'reply-to'          => rcube_idn_to_ascii($sql_arr[3]),
65                                     'bcc'                   => rcube_idn_to_ascii($sql_arr[4]),
66                                     'signature'         => $sql_arr[5],
67                                 'html_signature' => (int)$sql_arr[6],
68                         );
69                         }
70                         else {
71                             $result[] = $sql_arr[0];
72                         }
73
74                         if ($p['first'])
75                             break;
76                 }
77             }
78
79             $p['email'] = $result;
80
81             return $p;
82     }
83
84     /**
85      * EMail > User
86      */
87     function email2user($p)
88     {
89         $dbh = $this->app->get_dbh();
90
91         $sql_result = $dbh->query(preg_replace('/%m/', $dbh->escapeSimple($p['email']), $this->config['user']));
92
93         if ($sql_arr = $dbh->fetch_array($sql_result)) {
94             $p['user'] = $sql_arr[0];
95         }
96
97         return $p;
98     }
99
100     /**
101      * User > Host
102      */
103     function user2host($p)
104     {
105         $dbh = $this->app->get_dbh();
106
107         $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escapeSimple($p['user']), $this->config['host']));
108
109         if ($sql_arr = $dbh->fetch_array($sql_result)) {
110             $p['host'] = $sql_arr[0];
111         }
112
113         return $p;
114     }
115
116 }
117