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