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.5KB

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