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