Mercurial > hg > rc1
comparison plugins/virtuser_query/virtuser_query.php @ 0:1e000243b222
vanilla 1.3.3 distro, I hope
| author | Charlie Root |
|---|---|
| date | Thu, 04 Jan 2018 15:50:29 -0500 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:1e000243b222 |
|---|---|
| 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.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 * $config['virtuser_query'] = array('email' => '', 'user' => '', 'host' => '', 'alias' => ''); | |
| 16 * | |
| 17 * The email query can return more than one record to create more identities. | |
| 18 * This requires identities_level option to be set to value less than 2. | |
| 19 * | |
| 20 * By default Roundcube database is used. To use different database (or host) | |
| 21 * you can specify DSN string in $config['virtuser_query_dsn'] option. | |
| 22 * | |
| 23 * @author Aleksander Machniak <alec@alec.pl> | |
| 24 * @author Steffen Vogel | |
| 25 * @author Tim Gerundt | |
| 26 * @license GNU GPLv3+ | |
| 27 */ | |
| 28 class virtuser_query extends rcube_plugin | |
| 29 { | |
| 30 private $config; | |
| 31 private $app; | |
| 32 private $db; | |
| 33 | |
| 34 function init() | |
| 35 { | |
| 36 $this->app = rcmail::get_instance(); | |
| 37 $this->config = $this->app->config->get('virtuser_query'); | |
| 38 | |
| 39 if (!empty($this->config)) { | |
| 40 if (is_string($this->config)) { | |
| 41 $this->config = array('email' => $this->config); | |
| 42 } | |
| 43 | |
| 44 if ($this->config['email']) { | |
| 45 $this->add_hook('user2email', array($this, 'user2email')); | |
| 46 } | |
| 47 if ($this->config['user']) { | |
| 48 $this->add_hook('email2user', array($this, 'email2user')); | |
| 49 } | |
| 50 if ($this->config['host']) { | |
| 51 $this->add_hook('authenticate', array($this, 'user2host')); | |
| 52 } | |
| 53 if ($this->config['alias']) { | |
| 54 $this->add_hook('authenticate', array($this, 'alias2user')); | |
| 55 } | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 /** | |
| 60 * User > Email | |
| 61 */ | |
| 62 function user2email($p) | |
| 63 { | |
| 64 $dbh = $this->get_dbh(); | |
| 65 | |
| 66 $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['email'])); | |
| 67 | |
| 68 while ($sql_arr = $dbh->fetch_array($sql_result)) { | |
| 69 if (strpos($sql_arr[0], '@')) { | |
| 70 if ($p['extended'] && count($sql_arr) > 1) { | |
| 71 $result[] = array( | |
| 72 'email' => rcube_utils::idn_to_ascii($sql_arr[0]), | |
| 73 'name' => (string) $sql_arr[1], | |
| 74 'organization' => (string) $sql_arr[2], | |
| 75 'reply-to' => (string) rcube_utils::idn_to_ascii($sql_arr[3]), | |
| 76 'bcc' => (string) rcube_utils::idn_to_ascii($sql_arr[4]), | |
| 77 'signature' => (string) $sql_arr[5], | |
| 78 'html_signature' => (int) $sql_arr[6], | |
| 79 ); | |
| 80 } | |
| 81 else { | |
| 82 $result[] = $sql_arr[0]; | |
| 83 } | |
| 84 | |
| 85 if ($p['first']) { | |
| 86 break; | |
| 87 } | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 $p['email'] = $result; | |
| 92 | |
| 93 return $p; | |
| 94 } | |
| 95 | |
| 96 /** | |
| 97 * EMail > User | |
| 98 */ | |
| 99 function email2user($p) | |
| 100 { | |
| 101 $dbh = $this->get_dbh(); | |
| 102 | |
| 103 $sql_result = $dbh->query(preg_replace('/%m/', $dbh->escape($p['email']), $this->config['user'])); | |
| 104 | |
| 105 if ($sql_arr = $dbh->fetch_array($sql_result)) { | |
| 106 $p['user'] = $sql_arr[0]; | |
| 107 } | |
| 108 | |
| 109 return $p; | |
| 110 } | |
| 111 | |
| 112 /** | |
| 113 * User > Host | |
| 114 */ | |
| 115 function user2host($p) | |
| 116 { | |
| 117 $dbh = $this->get_dbh(); | |
| 118 | |
| 119 $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['host'])); | |
| 120 | |
| 121 if ($sql_arr = $dbh->fetch_array($sql_result)) { | |
| 122 $p['host'] = $sql_arr[0]; | |
| 123 } | |
| 124 | |
| 125 return $p; | |
| 126 } | |
| 127 | |
| 128 /** | |
| 129 * Alias > User | |
| 130 */ | |
| 131 function alias2user($p) | |
| 132 { | |
| 133 $dbh = $this->get_dbh(); | |
| 134 | |
| 135 $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['alias'])); | |
| 136 | |
| 137 if ($sql_arr = $dbh->fetch_array($sql_result)) { | |
| 138 $p['user'] = $sql_arr[0]; | |
| 139 } | |
| 140 | |
| 141 return $p; | |
| 142 } | |
| 143 | |
| 144 /** | |
| 145 * Initialize database handler | |
| 146 */ | |
| 147 function get_dbh() | |
| 148 { | |
| 149 if (!$this->db) { | |
| 150 if ($dsn = $this->app->config->get('virtuser_query_dsn')) { | |
| 151 // connect to the virtuser database | |
| 152 $this->db = rcube_db::factory($dsn); | |
| 153 $this->db->set_debug((bool)$this->app->config->get('sql_debug')); | |
| 154 $this->db->db_connect('r'); // connect in read mode | |
| 155 } | |
| 156 else { | |
| 157 $this->db = $this->app->get_dbh(); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 return $this->db; | |
| 162 } | |
| 163 | |
| 164 } |
