Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

virtuser_query.php 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. * @author Aleksander Machniak <alec@alec.pl>
  23. * @author Steffen Vogel
  24. * @author Tim Gerundt
  25. * @license GNU GPLv3+
  26. */
  27. class virtuser_query extends rcube_plugin
  28. {
  29. private $config;
  30. private $app;
  31. private $db;
  32. function init()
  33. {
  34. $this->app = rcmail::get_instance();
  35. $this->config = $this->app->config->get('virtuser_query');
  36. if (!empty($this->config)) {
  37. if (is_string($this->config)) {
  38. $this->config = array('email' => $this->config);
  39. }
  40. if ($this->config['email']) {
  41. $this->add_hook('user2email', array($this, 'user2email'));
  42. }
  43. if ($this->config['user']) {
  44. $this->add_hook('email2user', array($this, 'email2user'));
  45. }
  46. if ($this->config['host']) {
  47. $this->add_hook('authenticate', array($this, 'user2host'));
  48. }
  49. if ($this->config['alias']) {
  50. $this->add_hook('authenticate', array($this, 'alias2user'));
  51. }
  52. }
  53. }
  54. /**
  55. * User > Email
  56. */
  57. function user2email($p)
  58. {
  59. $dbh = $this->get_dbh();
  60. $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['email']));
  61. while ($sql_arr = $dbh->fetch_array($sql_result)) {
  62. if (strpos($sql_arr[0], '@')) {
  63. if ($p['extended'] && count($sql_arr) > 1) {
  64. $result[] = array(
  65. 'email' => rcube_utils::idn_to_ascii($sql_arr[0]),
  66. 'name' => (string) $sql_arr[1],
  67. 'organization' => (string) $sql_arr[2],
  68. 'reply-to' => (string) rcube_utils::idn_to_ascii($sql_arr[3]),
  69. 'bcc' => (string) rcube_utils::idn_to_ascii($sql_arr[4]),
  70. 'signature' => (string) $sql_arr[5],
  71. 'html_signature' => (int) $sql_arr[6],
  72. );
  73. }
  74. else {
  75. $result[] = $sql_arr[0];
  76. }
  77. if ($p['first']) {
  78. break;
  79. }
  80. }
  81. }
  82. $p['email'] = $result;
  83. return $p;
  84. }
  85. /**
  86. * EMail > User
  87. */
  88. function email2user($p)
  89. {
  90. $dbh = $this->get_dbh();
  91. $sql_result = $dbh->query(preg_replace('/%m/', $dbh->escape($p['email']), $this->config['user']));
  92. if ($sql_arr = $dbh->fetch_array($sql_result)) {
  93. $p['user'] = $sql_arr[0];
  94. }
  95. return $p;
  96. }
  97. /**
  98. * User > Host
  99. */
  100. function user2host($p)
  101. {
  102. $dbh = $this->get_dbh();
  103. $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['host']));
  104. if ($sql_arr = $dbh->fetch_array($sql_result)) {
  105. $p['host'] = $sql_arr[0];
  106. }
  107. return $p;
  108. }
  109. /**
  110. * Alias > User
  111. */
  112. function alias2user($p)
  113. {
  114. $dbh = $this->get_dbh();
  115. $sql_result = $dbh->query(preg_replace('/%u/', $dbh->escape($p['user']), $this->config['alias']));
  116. if ($sql_arr = $dbh->fetch_array($sql_result)) {
  117. $p['user'] = $sql_arr[0];
  118. }
  119. return $p;
  120. }
  121. /**
  122. * Initialize database handler
  123. */
  124. function get_dbh()
  125. {
  126. if (!$this->db) {
  127. if ($dsn = $this->app->config->get('virtuser_query_dsn')) {
  128. // connect to the virtuser database
  129. $this->db = rcube_db::factory($dsn);
  130. $this->db->set_debug((bool)$this->app->config->get('sql_debug'));
  131. $this->db->db_connect('r'); // connect in read mode
  132. }
  133. else {
  134. $this->db = $this->app->get_dbh();
  135. }
  136. }
  137. return $this->db;
  138. }
  139. }