autocomplete.php
9.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
+-----------------------------------------------------------------------+
| This file is part of the Roundcube Webmail client |
| |
| Copyright (C) The Roundcube Dev Team |
| Copyright (C) Kolab Systems AG |
| |
| Licensed under the GNU General Public License version 3 or |
| any later version with exceptions for skins & plugins. |
| See the README file for a full license statement. |
| |
| PURPOSE: |
| Perform a search on configured address books for the email |
| address autocompletion |
+-----------------------------------------------------------------------+
| Author: Thomas Bruederli <roundcube@gmail.com> |
+-----------------------------------------------------------------------+
*/
class rcmail_action_mail_autocomplete extends rcmail_action
{
protected static $mode = self::MODE_AJAX;
/**
* Request handler.
*
* @param array $args Arguments from the previous step(s)
*/
public function run($args = [])
{
$rcmail = rcmail::get_instance();
$MAXNUM = (int) $rcmail->config->get('autocomplete_max', 15);
$mode = (int) $rcmail->config->get('addressbook_search_mode');
$single = (bool) $rcmail->config->get('autocomplete_single');
$search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
$reqid = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
$contacts = [];
if (strlen($search) && ($book_types = self::autocomplete_addressbooks())) {
$sort_keys = [];
$books_num = count($book_types);
$search_lc = mb_strtolower($search);
$mode |= rcube_addressbook::SEARCH_GROUPS;
$fields = $rcmail->config->get('contactlist_fields');
foreach ($book_types as $abook_id) {
$abook = $rcmail->get_address_book($abook_id);
$abook->set_pagesize($MAXNUM);
if ($result = $abook->search($fields, $search, $mode, true, true, 'email')) {
while ($record = $result->iterate()) {
// Contact can have more than one e-mail address
$email_arr = (array) $abook->get_col_values('email', $record, true);
$email_cnt = count($email_arr);
$idx = 0;
foreach ($email_arr as $email) {
if (empty($email)) {
continue;
}
$name = rcube_addressbook::compose_list_name($record);
$contact = format_email_recipient($email, $name);
// skip entries that don't match
if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
continue;
}
$index = $contact;
// skip duplicates
if (empty($contacts[$index])) {
$contact = [
'name' => $contact,
'type' => isset($record['_type']) ? $record['_type'] : null,
'id' => $record['ID'],
'source' => $abook_id,
];
$display = rcube_addressbook::compose_search_name($record, $email, $name);
if ($display && $display != $contact['name']) {
$contact['display'] = $display;
}
// groups with defined email address will not be expanded to its members' addresses
if ($contact['type'] == 'group') {
$contact['email'] = $email;
}
$name = !empty($contact['display']) ? $contact['display'] : $name;
$contacts[$index] = $contact;
$sort_keys[$index] = sprintf('%s %03d', $name, $idx++);
if (count($contacts) >= $MAXNUM) {
break 2;
}
}
// skip redundant entries (show only first email address)
if ($single) {
break;
}
}
}
}
// also list matching contact groups
if ($abook->groups && count($contacts) < $MAXNUM) {
foreach ($abook->list_groups($search, $mode) as $group) {
$abook->reset();
$abook->set_group($group['ID']);
$group_prop = $abook->get_group($group['ID']);
// group (distribution list) with email address(es)
if (!empty($group_prop['email'])) {
$idx = 0;
foreach ((array) $group_prop['email'] as $email) {
$index = format_email_recipient($email, $group['name']);
if (empty($contacts[$index])) {
$sort_keys[$index] = sprintf('%s %03d', $group['name'] , $idx++);
$contacts[$index] = [
'name' => $index,
'email' => $email,
'type' => 'group',
'id' => $group['ID'],
'source' => $abook_id,
];
if (count($contacts) >= $MAXNUM) {
break 3;
}
}
}
}
// show group with count
else if (($result = $abook->count()) && $result->count) {
if (empty($contacts[$group['name']])) {
$sort_keys[$group['name']] = $group['name'];
$contacts[$group['name']] = [
'name' => $group['name'] . ' (' . intval($result->count) . ')',
'type' => 'group',
'id' => $group['ID'],
'source' => $abook_id,
];
if (count($contacts) >= $MAXNUM) {
break 2;
}
}
}
}
}
}
if (count($contacts)) {
// sort contacts index
asort($sort_keys, SORT_LOCALE_STRING);
// re-sort contacts according to index
foreach ($sort_keys as $idx => $val) {
$sort_keys[$idx] = $contacts[$idx];
}
$contacts = array_values($sort_keys);
}
}
// Allow autocomplete result optimization via plugin
$plugin = $rcmail->plugins->exec_hook('contacts_autocomplete_after', [
'search' => $search,
// Provide already-found contacts to plugin if they are required
'contacts' => $contacts,
]);
$contacts = $plugin['contacts'];
$rcmail->output->command('ksearch_query_results', $contacts, $search, $reqid);
$rcmail->output->send();
}
/**
* Collect addressbook sources used for autocompletion
*/
public static function autocomplete_addressbooks()
{
$rcmail = rcmail::get_instance();
$source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
if (strlen($source)) {
$book_types = [$source];
}
else {
$book_types = (array) $rcmail->config->get('autocomplete_addressbooks', 'sql');
}
$collected_recipients = $rcmail->config->get('collected_recipients');
$collected_senders = $rcmail->config->get('collected_senders');
if (strlen($collected_recipients) && !in_array($collected_recipients, $book_types)) {
$book_types[] = $collected_recipients;
}
if (strlen($collected_senders) && !in_array($collected_senders, $book_types)) {
$book_types[] = $collected_senders;
}
return !empty($book_types) ? $book_types : null;
}
}