You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virtuser_query.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * DB based User-to-Email and Email-to-User lookup
  4. *
  5. * Add it to the plugins list in config.inc.php and set
  6. * SQL queries to resolve usernames, e-mail addresses and hostnames from the database
  7. * %u will be replaced with the current username for login.
  8. * %m will be replaced with the current e-mail address for login.
  9. *
  10. * Queries should select the user's e-mail address, username or the imap hostname as first column
  11. * The email query could optionally select identity data columns in specified order:
  12. * name, organization, reply-to, bcc, signature, html_signature
  13. *
  14. * $config['virtuser_query'] = array('email' => '', 'user' => '', 'host' => '', 'alias' => '');
  15. *
  16. * The email query can return more than one record to create more identities.
  17. * This requires identities_level option to be set to value less than 2.
  18. *
  19. * By default Roundcube database is used. To use different database (or host)
  20. * you can specify DSN string in $config['virtuser_query_dsn'] option.
  21. *
  22. * @version @package_version@
  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. function init()
  34. {
  35. $this->app = rcmail::get_instance();
  36. $this->config = $this->app->config->get('virtuser_query');
  37. if (!empty($this->config)) {
  38. if (is_string($this->config)) {
  39. $this->config = array('email' => $this->config);
  40. }
  41. if ($this->config['email']) {
  42. $this->add_hook('user2email', array($this, 'user2email'));
  43. }
  44. if ($this->config['user']) {
  45. $this->add_hook('email2user', array($this, 'email2user'));
  46. }
  47. if ($this->config['host']) {
  48. $this->add_hook('authenticate', array($this, 'user2host'));
  49. }
  50. if ($this->config['alias']) {
  51. $this->add_hook('authenticate', array($this, 'alias2user'));
  52. }
  53. }
  54. }
  55. /**
  56. * User > Email
  57. */
  58. function user2email($p)
  59. {
  60. $dbh = $this->get_dbh();
  61. $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['email']));
  62. while ($sql_arr = $dbh->fetch_array($sql_result)) {
  63. if (strpos($sql_arr[0], '@')) {
  64. if ($p['extended'] && count($sql_arr) > 1) {
  65. $result[] = array(
  66. 'email' => rcube_utils::idn_to_ascii($sql_arr[0]),
  67. 'name' => (string) $sql_arr[1],
  68. 'organization' => (string) $sql_arr[2],
  69. 'reply-to' => (string) rcube_utils::idn_to_ascii($sql_arr[3]),
  70. 'bcc' => (string) rcube_utils::idn_to_ascii($sql_arr[4]),
  71. 'signature' => (string) $sql_arr[5],
  72. 'html_signature' => (int) $sql_arr[6],
  73. );
  74. }
  75. else {
  76. $result[] = $sql_arr[0];
  77. }
  78. if ($p['first']) {
  79. break;
  80. }
  81. }
  82. }
  83. $p['email'] = $result;
  84. return $p;
  85. }
  86. /**
  87. * EMail > User
  88. */
  89. function email2user($p)
  90. {
  91. $dbh = $this->get_dbh();
  92. $sql_result = $dbh->query(preg_replace('/%m/', $dbh->escape($p['email']), $this->config['user']));
  93. if ($sql_arr = $dbh->fetch_array($sql_result)) {
  94. $p['user'] = $sql_arr[0];
  95. }
  96. return $p;
  97. }
  98. /**
  99. * User > Host
  100. */
  101. function user2host($p)
  102. {
  103. $dbh = $this->get_dbh();
  104. $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['host']));
  105. if ($sql_arr = $dbh->fetch_array($sql_result)) {
  106. $p['host'] = $sql_arr[0];
  107. }
  108. return $p;
  109. }
  110. /**
  111. * Alias > User
  112. */
  113. function alias2user($p)
  114. {
  115. $dbh = $this->get_dbh();
  116. $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['alias']));
  117. if ($sql_arr = $dbh->fetch_array($sql_result)) {
  118. $p['user'] = $sql_arr[0];
  119. }
  120. return $p;
  121. }
  122. /**
  123. * Initialize database handler
  124. */
  125. function get_dbh()
  126. {
  127. if (!$this->db) {
  128. if ($dsn = $this->app->config->get('virtuser_query_dsn')) {
  129. // connect to the virtuser database
  130. $this->db = rcube_db::factory($dsn);
  131. $this->db->set_debug((bool)$this->app->config->get('sql_debug'));
  132. $this->db->db_connect('r'); // connect in read mode
  133. }
  134. else {
  135. $this->db = $this->app->get_dbh();
  136. }
  137. }
  138. return $this->db;
  139. }
  140. }