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_file.php 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * File based User-to-Email and Email-to-User lookup
  4. *
  5. * Add it to the plugins list in config.inc.php and set
  6. * path to a virtuser table file to resolve user names and e-mail
  7. * addresses
  8. * $rcmail['virtuser_file'] = '';
  9. *
  10. * @license GNU GPLv3+
  11. * @author Aleksander Machniak
  12. */
  13. class virtuser_file extends rcube_plugin
  14. {
  15. private $file;
  16. private $app;
  17. function init()
  18. {
  19. $this->app = rcmail::get_instance();
  20. $this->file = $this->app->config->get('virtuser_file');
  21. if ($this->file) {
  22. $this->add_hook('user2email', array($this, 'user2email'));
  23. $this->add_hook('email2user', array($this, 'email2user'));
  24. }
  25. }
  26. /**
  27. * User > Email
  28. */
  29. function user2email($p)
  30. {
  31. $r = $this->findinvirtual('/\s' . preg_quote($p['user'], '/') . '\s*$/');
  32. $result = array();
  33. for ($i=0; $i<count($r); $i++) {
  34. $arr = preg_split('/\s+/', $r[$i]);
  35. if (count($arr) > 0 && strpos($arr[0], '@')) {
  36. $result[] = rcube_utils::idn_to_ascii(trim(str_replace('\\@', '@', $arr[0])));
  37. if ($p['first']) {
  38. $p['email'] = $result[0];
  39. break;
  40. }
  41. }
  42. }
  43. $p['email'] = empty($result) ? NULL : $result;
  44. return $p;
  45. }
  46. /**
  47. * Email > User
  48. */
  49. function email2user($p)
  50. {
  51. $r = $this->findinvirtual('/^' . preg_quote($p['email'], '/') . '\s/');
  52. for ($i=0; $i<count($r); $i++) {
  53. $arr = preg_split('/\s+/', trim($r[$i]));
  54. if (count($arr) > 0) {
  55. $p['user'] = trim($arr[count($arr)-1]);
  56. break;
  57. }
  58. }
  59. return $p;
  60. }
  61. /**
  62. * Find matches of the given pattern in virtuser file
  63. *
  64. * @param string Regular expression to search for
  65. * @return array Matching entries
  66. */
  67. private function findinvirtual($pattern)
  68. {
  69. $result = array();
  70. $virtual = null;
  71. if ($this->file)
  72. $virtual = file($this->file);
  73. if (empty($virtual))
  74. return $result;
  75. // check each line for matches
  76. foreach ($virtual as $line) {
  77. $line = trim($line);
  78. if (empty($line) || $line[0]=='#')
  79. continue;
  80. if (preg_match($pattern, $line))
  81. $result[] = $line;
  82. }
  83. return $result;
  84. }
  85. }