Aleksander Machniak
2013-06-14 461a30d771edd8bc6606f2c92dfde363514b93b1
commit | author | age
48e9c1 1 <?php
T 2
3 /**
4  * DB based User-to-Email and Email-to-User lookup
5  *
461a30 6  * Add it to the plugins list in config.inc.php and set
48e9c1 7  * SQL queries to resolve usernames, e-mail addresses and hostnames from the database
T 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  *
221448 17  * The email query can return more than one record to create more identities.
AM 18  * This requires identities_level option to be set to value less than 2.
19  *
f5fcb9 20  * By default Roundcube database is used. To use different database (or host)
AM 21  * you can specify DSN string in $rcmail_config['virtuser_query_dsn'] option.
22  *
48e9c1 23  * @version @package_version@
221448 24  * @author Aleksander Machniak <alec@alec.pl>
48e9c1 25  * @author Steffen Vogel
T 26  */
27 class virtuser_query extends rcube_plugin
28 {
29     private $config;
30     private $app;
f5fcb9 31     private $db;
48e9c1 32
T 33     function init()
34     {
61be82 35         $this->app    = rcmail::get_instance();
AM 36         $this->config = $this->app->config->get('virtuser_query');
48e9c1 37
T 38         if (!empty($this->config)) {
39             if (is_string($this->config)) {
40                 $this->config = array('email' => $this->config);
41             }
42
43             if ($this->config['email']) {
44                 $this->add_hook('user2email', array($this, 'user2email'));
45             }
46             if ($this->config['user']) {
47                 $this->add_hook('email2user', array($this, 'email2user'));
48             }
49             if ($this->config['host']) {
50                 $this->add_hook('authenticate', array($this, 'user2host'));
51             }
52         }
53     }
54
55     /**
56      * User > Email
57      */
58     function user2email($p)
59     {
f5fcb9 60         $dbh = $this->get_dbh();
48e9c1 61
282dff 62         $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['email']));
48e9c1 63
61be82 64         while ($sql_arr = $dbh->fetch_array($sql_result)) {
AM 65             if (strpos($sql_arr[0], '@')) {
66                 if ($p['extended'] && count($sql_arr) > 1) {
67                     $result[] = array(
68                         'email'         => rcube_utils::idn_to_ascii($sql_arr[0]),
69                         'name'          => $sql_arr[1],
70                         'organization'  => $sql_arr[2],
71                         'reply-to'      => rcube_utils::idn_to_ascii($sql_arr[3]),
72                         'bcc'           => rcube_utils::idn_to_ascii($sql_arr[4]),
73                         'signature'     => $sql_arr[5],
74                         'html_signature' => (int)$sql_arr[6],
75                     );
76                 }
77                 else {
78                     $result[] = $sql_arr[0];
79                 }
48e9c1 80
39a034 81                 if ($p['first']) {
61be82 82                     break;
39a034 83                 }
61be82 84             }
AM 85         }
48e9c1 86
61be82 87         $p['email'] = $result;
48e9c1 88
61be82 89         return $p;
48e9c1 90     }
T 91
92     /**
93      * EMail > User
94      */
95     function email2user($p)
96     {
f5fcb9 97         $dbh = $this->get_dbh();
48e9c1 98
282dff 99         $sql_result = $dbh->query(preg_replace('/%m/', $dbh->escape($p['email']), $this->config['user']));
48e9c1 100
T 101         if ($sql_arr = $dbh->fetch_array($sql_result)) {
102             $p['user'] = $sql_arr[0];
103         }
104
105         return $p;
106     }
107
108     /**
109      * User > Host
110      */
111     function user2host($p)
112     {
f5fcb9 113         $dbh = $this->get_dbh();
48e9c1 114
282dff 115         $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['host']));
48e9c1 116
T 117         if ($sql_arr = $dbh->fetch_array($sql_result)) {
118             $p['host'] = $sql_arr[0];
119         }
120
121         return $p;
122     }
123
f5fcb9 124     /**
AM 125      * Initialize database handler
126      */
127     function get_dbh()
128     {
129         if (!$this->db) {
130             if ($dsn = $this->app->config->get('virtuser_query_dsn')) {
131                 // connect to the virtuser database
132                 $this->db = rcube_db::factory($dsn);
133                 $this->db->set_debug((bool)$this->app->config->get('sql_debug'));
134                 $this->db->db_connect('r'); // connect in read mode
135             }
136             else {
137                 $this->db = $this->app->get_dbh();
138             }
139         }
140
141         return $this->db;
142     }
143
48e9c1 144 }
T 145