Aleksander Machniak
2015-02-20 c7b77b9179f1fcb52ed5f4d6b648daa54eeda7b4
commit | author | age
48e9c1 1 <?php
T 2 /**
3  * New user identity
4  *
5  * Populates a new user's default identity from LDAP on their first visit.
6  *
7  * This plugin requires that a working public_ldap directory be configured.
8  *
9  * @version @package_version@
10  * @author Kris Steinhoff
07c6c6 11  * @license GNU GPLv3+
48e9c1 12  */
T 13 class new_user_identity extends rcube_plugin
14 {
15     public $task = 'login';
16
1e89a6 17     private $rc;
48e9c1 18     private $ldap;
T 19
20     function init()
21     {
1e89a6 22         $this->rc = rcmail::get_instance();
193a0d 23
48e9c1 24         $this->add_hook('user_create', array($this, 'lookup_user_name'));
1e89a6 25         $this->add_hook('login_after', array($this, 'login_after'));
48e9c1 26     }
T 27
28     function lookup_user_name($args)
29     {
30         if ($this->init_ldap($args['host'])) {
d97118 31             $results = $this->ldap->search('*', $args['user'], true);
e2251d 32
48e9c1 33             if (count($results->records) == 1) {
d97118 34                 $user_name  = is_array($results->records[0]['name']) ? $results->records[0]['name'][0] : $results->records[0]['name'];
AM 35                 $user_email = is_array($results->records[0]['email']) ? $results->records[0]['email'][0] : $results->records[0]['email'];
36
e2251d 37                 $args['user_name']  = $user_name;
AM 38                 $args['email_list'] = array();
39
d97118 40                 if (!$args['user_email'] && strpos($user_email, '@')) {
61be82 41                     $args['user_email'] = rcube_utils::idn_to_ascii($user_email);
48e9c1 42                 }
6035e3 43
e2251d 44                 foreach (array_keys($results[0]) as $key) {
AM 45                     if (!preg_match('/^email($|:)/', $key)) {
6035e3 46                         continue;
BN 47                     }
48
e2251d 49                     foreach ((array) $results->records[0][$key] as $alias) {
AM 50                         if (strpos($alias, '@')) {
51                             $args['email_list'][] = rcube_utils::idn_to_ascii($alias);
6035e3 52                         }
BN 53                     }
54                 }
55
48e9c1 56             }
T 57         }
e2251d 58
48e9c1 59         return $args;
T 60     }
61
193a0d 62     function login_after($args)
BN 63     {
1e89a6 64         $this->load_config();
193a0d 65
385bb6 66         if ($this->ldap || !$this->rc->config->get('new_user_identity_onlogin')) {
1e89a6 67             return $args;
BN 68         }
193a0d 69
2f4678 70         $identities = $this->rc->user->list_emails();
8cbebc 71         $ldap_entry = $this->lookup_user_name(array(
AM 72                 'user' => $this->rc->user->data['username'],
73                 'host' => $this->rc->user->data['mail_host'],
74         ));
1e89a6 75
8cbebc 76         foreach ((array) $ldap_entry['email_list'] as $email) {
AM 77             foreach ($identities as $identity) {
2f4678 78                 if ($identity['email'] == $email) {
193a0d 79                     continue 2;
BN 80                 }
81             }
82
1e89a6 83             $plugin = $this->rc->plugins->exec_hook('identity_create', array(
8cbebc 84                 'login'  => true,
AM 85                 'record' => array(
86                     'user_id'  => $this->rc->user->ID,
87                     'standard' => 0,
88                     'email'    => $email,
89                     'name'     => $ldap_entry['user_name']
90                 ),
193a0d 91             ));
BN 92
93             if (!$plugin['abort'] && $plugin['record']['email']) {
1e89a6 94                 $this->rc->user->insert_identity($plugin['record']);
193a0d 95             }
BN 96         }
97         return $args;
98     }
99
48e9c1 100     private function init_ldap($host)
T 101     {
d97118 102         if ($this->ldap) {
48e9c1 103             return $this->ldap->ready;
d97118 104         }
48e9c1 105
385bb6 106         $this->load_config();
BN 107
1e89a6 108         $addressbook = $this->rc->config->get('new_user_identity_addressbook');
BN 109         $ldap_config = (array)$this->rc->config->get('ldap_public');
110         $match       = $this->rc->config->get('new_user_identity_match');
48e9c1 111
T 112         if (empty($addressbook) || empty($match) || empty($ldap_config[$addressbook])) {
113             return false;
114         }
115
116         $this->ldap = new new_user_identity_ldap_backend(
117             $ldap_config[$addressbook],
1e89a6 118             $this->rc->config->get('ldap_debug'),
BN 119             $this->rc->config->mail_domain($host),
48e9c1 120             $match);
T 121
122         return $this->ldap->ready;
123     }
124 }
125
126 class new_user_identity_ldap_backend extends rcube_ldap
127 {
128     function __construct($p, $debug, $mail_domain, $search)
129     {
130         parent::__construct($p, $debug, $mail_domain);
131         $this->prop['search_fields'] = (array)$search;
132     }
133 }