diff options
author | Hugues Hiegel <root@paranoid> | 2015-04-21 12:49:44 +0200 |
---|---|---|
committer | Hugues Hiegel <root@paranoid> | 2015-04-21 12:49:44 +0200 |
commit | 733f8e8d0ce6217d906d06dc4fb08e36d48ed794 (patch) | |
tree | cff28366ff63ea6596f8026e1698090bd0b9405c /plugins/virtuser_file/virtuser_file.php | |
parent | ef2e7b3f9d264ec146d4dae257b1e295ab3b462a (diff) | |
parent | a4ba3df54834ee90fb2c9930669f1229dc80261a (diff) |
Conflicts:
composer.json-dist
config/defaults.inc.php
plugins
plugins/acl/acl.js
plugins/acl/acl.php
plugins/acl/skins/classic/templates/table.html
plugins/acl/skins/larry/templates/table.html
plugins/enigma/README
plugins/enigma/config.inc.php.dist
plugins/enigma/enigma.js
plugins/enigma/enigma.php
plugins/enigma/lib/enigma_driver.php
plugins/enigma/lib/enigma_driver_gnupg.php
plugins/enigma/lib/enigma_driver_phpssl.php
plugins/enigma/lib/enigma_engine.php
plugins/enigma/lib/enigma_error.php
plugins/enigma/lib/enigma_key.php
plugins/enigma/lib/enigma_signature.php
plugins/enigma/lib/enigma_subkey.php
plugins/enigma/lib/enigma_ui.php
plugins/enigma/lib/enigma_userid.php
plugins/enigma/localization/en_US.inc
plugins/enigma/localization/ja_JP.inc
plugins/enigma/localization/ru_RU.inc
plugins/enigma/skins/classic/enigma.css
plugins/enigma/skins/classic/templates/keys.html
plugins/help/config.inc.php.dist
plugins/help/help.php
plugins/help/localization/en_US.inc
plugins/jqueryui/jqueryui.php
plugins/managesieve/Changelog
plugins/managesieve/composer.json
plugins/managesieve/config.inc.php.dist
plugins/managesieve/lib/Roundcube/rcube_sieve.php
plugins/managesieve/lib/Roundcube/rcube_sieve_engine.php
plugins/managesieve/lib/Roundcube/rcube_sieve_vacation.php
plugins/managesieve/localization/en_US.inc
plugins/managesieve/managesieve.js
plugins/managesieve/skins/classic/managesieve.css
plugins/managesieve/skins/larry/managesieve.css
plugins/password/README
plugins/password/config.inc.php.dist
plugins/password/drivers/ldap.php
plugins/password/drivers/poppassd.php
plugins/password/drivers/vpopmaild.php
plugins/vcard_attachments/vcardattach.js
plugins/zipdownload/zipdownload.php
Diffstat (limited to 'plugins/virtuser_file/virtuser_file.php')
-rw-r--r-- | plugins/virtuser_file/virtuser_file.php | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/plugins/virtuser_file/virtuser_file.php b/plugins/virtuser_file/virtuser_file.php new file mode 100644 index 000000000..f2b357aaf --- /dev/null +++ b/plugins/virtuser_file/virtuser_file.php @@ -0,0 +1,105 @@ +<?php + +/** + * File based User-to-Email and Email-to-User lookup + * + * Add it to the plugins list in config.inc.php and set + * path to a virtuser table file to resolve user names and e-mail + * addresses + * $rcmail['virtuser_file'] = ''; + * + * @version @package_version@ + * @license GNU GPLv3+ + * @author Aleksander Machniak + */ +class virtuser_file extends rcube_plugin +{ + private $file; + private $app; + + function init() + { + $this->app = rcmail::get_instance(); + $this->file = $this->app->config->get('virtuser_file'); + + if ($this->file) { + $this->add_hook('user2email', array($this, 'user2email')); + $this->add_hook('email2user', array($this, 'email2user')); + } + } + + /** + * User > Email + */ + function user2email($p) + { + $r = $this->findinvirtual('/\s' . preg_quote($p['user'], '/') . '\s*$/'); + $result = array(); + + for ($i=0; $i<count($r); $i++) { + $arr = preg_split('/\s+/', $r[$i]); + + if (count($arr) > 0 && strpos($arr[0], '@')) { + $result[] = rcube_utils::idn_to_ascii(trim(str_replace('\\@', '@', $arr[0]))); + + if ($p['first']) { + $p['email'] = $result[0]; + break; + } + } + } + + $p['email'] = empty($result) ? NULL : $result; + + return $p; + } + + /** + * Email > User + */ + function email2user($p) + { + $r = $this->findinvirtual('/^' . preg_quote($p['email'], '/') . '\s/'); + + for ($i=0; $i<count($r); $i++) { + $arr = preg_split('/\s+/', trim($r[$i])); + + if (count($arr) > 0) { + $p['user'] = trim($arr[count($arr)-1]); + break; + } + } + + return $p; + } + + /** + * Find matches of the given pattern in virtuser file + * + * @param string Regular expression to search for + * @return array Matching entries + */ + private function findinvirtual($pattern) + { + $result = array(); + $virtual = null; + + if ($this->file) + $virtual = file($this->file); + + if (empty($virtual)) + return $result; + + // check each line for matches + foreach ($virtual as $line) { + $line = trim($line); + if (empty($line) || $line[0]=='#') + continue; + + if (preg_match($pattern, $line)) + $result[] = $line; + } + + return $result; + } +} |