Aleksander Machniak
2016-05-06 acf633c73bc8df9a5036bc52d7568f4213ab73c7
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  * Example configuration:
14  *
15  *  // The id of the address book to use to automatically set a new
16  *  // user's full name in their new identity. (This should be an
bcedf0 17  *  // string, which refers to the $config['ldap_public'] array.)
AM 18  *  $config['new_user_identity_addressbook'] = 'People';
48e9c1 19  *
T 20  *  // When automatically setting a new users's full name in their
21  *  // new identity, match the user's login name against this field.
bcedf0 22  *  $config['new_user_identity_match'] = 'uid';
48e9c1 23  */
T 24 class new_user_identity extends rcube_plugin
25 {
26     public $task = 'login';
27
28     private $ldap;
29
30     function init()
31     {
32         $this->add_hook('user_create', array($this, 'lookup_user_name'));
33     }
34
35     function lookup_user_name($args)
36     {
37         if ($this->init_ldap($args['host'])) {
d97118 38             $results = $this->ldap->search('*', $args['user'], true);
48e9c1 39             if (count($results->records) == 1) {
d97118 40                 $user_name  = is_array($results->records[0]['name']) ? $results->records[0]['name'][0] : $results->records[0]['name'];
AM 41                 $user_email = is_array($results->records[0]['email']) ? $results->records[0]['email'][0] : $results->records[0]['email'];
42
43                 $args['user_name'] = $user_name;
44                 if (!$args['user_email'] && strpos($user_email, '@')) {
61be82 45                     $args['user_email'] = rcube_utils::idn_to_ascii($user_email);
48e9c1 46                 }
T 47             }
48         }
49         return $args;
50     }
51
52     private function init_ldap($host)
53     {
d97118 54         if ($this->ldap) {
48e9c1 55             return $this->ldap->ready;
d97118 56         }
48e9c1 57
T 58         $rcmail = rcmail::get_instance();
59
60         $addressbook = $rcmail->config->get('new_user_identity_addressbook');
61         $ldap_config = (array)$rcmail->config->get('ldap_public');
62         $match       = $rcmail->config->get('new_user_identity_match');
63
64         if (empty($addressbook) || empty($match) || empty($ldap_config[$addressbook])) {
65             return false;
66         }
67
68         $this->ldap = new new_user_identity_ldap_backend(
69             $ldap_config[$addressbook],
70             $rcmail->config->get('ldap_debug'),
71             $rcmail->config->mail_domain($host),
72             $match);
73
74         return $this->ldap->ready;
75     }
76 }
77
78 class new_user_identity_ldap_backend extends rcube_ldap
79 {
80     function __construct($p, $debug, $mail_domain, $search)
81     {
82         parent::__construct($p, $debug, $mail_domain);
83         $this->prop['search_fields'] = (array)$search;
84     }
85 }