source: github/plugins/virtuser_file/virtuser_file.php @ 48e9c14e

Revision 48e9c14e, 2.3 KB checked in by thomasb, 7 weeks ago (diff)

Move plugins repository into roundcubemail root folder; svn:externals are not defined anymore

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