comparison program/steps/mail/autocomplete.inc @ 0:4681f974d28b

vanilla 1.3.3 distro, I hope
author Charlie Root
date Thu, 04 Jan 2018 15:52:31 -0500
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4681f974d28b
1 <?php
2
3 /**
4 +-----------------------------------------------------------------------+
5 | program/steps/mail/autocomplete.inc |
6 | |
7 | This file is part of the Roundcube Webmail client |
8 | Copyright (C) 2008-2013, Roundcube Dev Team |
9 | Copyright (C) 2011-2013, Kolab Systems AG |
10 | |
11 | Licensed under the GNU General Public License version 3 or |
12 | any later version with exceptions for skins & plugins. |
13 | See the README file for a full license statement. |
14 | |
15 | PURPOSE: |
16 | Perform a search on configured address books for the address |
17 | autocompletion of the message compose screen |
18 +-----------------------------------------------------------------------+
19 | Author: Thomas Bruederli <roundcube@gmail.com> |
20 +-----------------------------------------------------------------------+
21 */
22
23 if ($RCMAIL->action == 'group-expand') {
24 $abook = $RCMAIL->get_address_book(rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC));
25 if ($gid = rcube_utils::get_input_value('_gid', rcube_utils::INPUT_GET)) {
26 $abook->set_group($gid);
27 $abook->set_pagesize(9999); // TODO: limit number of group members by config?
28
29 $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
30 $result = $abook->list_records($RCMAIL->config->get('contactlist_fields'));
31 $members = array();
32
33 while ($result && ($record = $result->iterate())) {
34 $emails = (array) $abook->get_col_values('email', $record, true);
35 if (!empty($emails) && ($email = array_shift($emails))) {
36 $members[] = format_email_recipient($email, rcube_addressbook::compose_list_name($record));
37 }
38 }
39
40 $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
41 }
42
43 $OUTPUT->send();
44 }
45
46
47 $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
48 $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
49 $single = (bool) $RCMAIL->config->get('autocomplete_single');
50 $search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
51 $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
52 $reqid = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
53
54 if (strlen($source)) {
55 $book_types = array($source);
56 }
57 else {
58 $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
59 }
60
61 $contacts = array();
62 if (!empty($book_types) && strlen($search)) {
63 $sort_keys = array();
64 $books_num = count($book_types);
65 $search_lc = mb_strtolower($search);
66 $mode |= rcube_addressbook::SEARCH_GROUPS;
67
68 foreach ($book_types as $abook_id) {
69 $abook = $RCMAIL->get_address_book($abook_id);
70 $abook->set_pagesize($MAXNUM);
71
72 if ($result = $abook->search($RCMAIL->config->get('contactlist_fields'), $search, $mode, true, true, 'email')) {
73 while ($record = $result->iterate()) {
74 // Contact can have more than one e-mail address
75 $email_arr = (array)$abook->get_col_values('email', $record, true);
76 $email_cnt = count($email_arr);
77 $idx = 0;
78
79 foreach ($email_arr as $email) {
80 if (empty($email)) {
81 continue;
82 }
83
84 $name = rcube_addressbook::compose_list_name($record);
85 $contact = format_email_recipient($email, $name);
86
87 // skip entries that don't match
88 if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
89 continue;
90 }
91
92 $index = $contact;
93
94 // skip duplicates
95 if (empty($contacts[$index])) {
96 $contact = array(
97 'name' => $contact,
98 'type' => $record['_type'],
99 'id' => $record['ID'],
100 'source' => $abook_id,
101 );
102
103 if (($display = rcube_addressbook::compose_search_name($record, $email, $name)) && $display != $contact['name']) {
104 $contact['display'] = $display;
105 }
106
107 // groups with defined email address will not be expanded to its members' addresses
108 if ($record['_type'] == 'group') {
109 $contact['email'] = $email;
110 }
111
112 $contacts[$index] = $contact;
113 $sort_keys[$index] = sprintf('%s %03d', $contact['display'] ?: $name, $idx++);
114
115 if (count($contacts) >= $MAXNUM) {
116 break 2;
117 }
118 }
119
120 // skip redundant entries (show only first email address)
121 if ($single) {
122 break;
123 }
124 }
125 }
126 }
127
128 // also list matching contact groups
129 if ($abook->groups && count($contacts) < $MAXNUM) {
130 foreach ($abook->list_groups($search, $mode) as $group) {
131 $abook->reset();
132 $abook->set_group($group['ID']);
133
134 $group_prop = $abook->get_group($group['ID']);
135
136 // group (distribution list) with email address(es)
137 if ($group_prop['email']) {
138 $idx = 0;
139 foreach ((array)$group_prop['email'] as $email) {
140 $index = format_email_recipient($email, $group['name']);
141
142 if (empty($contacts[$index])) {
143 $sort_keys[$index] = sprintf('%s %03d', $group['name'] , $idx++);
144 $contacts[$index] = array(
145 'name' => $index,
146 'email' => $email,
147 'type' => 'group',
148 'id' => $group['ID'],
149 'source' => $abook_id,
150 );
151
152 if (count($contacts) >= $MAXNUM) {
153 break 2;
154 }
155 }
156 }
157 }
158 // show group with count
159 else if (($result = $abook->count()) && $result->count) {
160 if (empty($contacts[$group['name']])) {
161 $sort_keys[$group['name']] = $group['name'];
162 $contacts[$group['name']] = array(
163 'name' => $group['name'] . ' (' . intval($result->count) . ')',
164 'type' => 'group',
165 'id' => $group['ID'],
166 'source' => $abook_id,
167 );
168
169 if (count($contacts) >= $MAXNUM) {
170 break;
171 }
172 }
173 }
174 }
175 }
176 }
177
178 if (count($contacts)) {
179 // sort contacts index
180 asort($sort_keys, SORT_LOCALE_STRING);
181 // re-sort contacts according to index
182 foreach ($sort_keys as $idx => $val) {
183 $sort_keys[$idx] = $contacts[$idx];
184 }
185 $contacts = array_values($sort_keys);
186 }
187 }
188
189
190 // Allow autocomplete result optimization via plugin
191 $pluginResult = $RCMAIL->plugins->exec_hook('contacts_autocomplete_after', array(
192 'search' => $search,
193 'contacts' => $contacts, // Provide already-found contacts to plugin if they are required
194 ));
195 $contacts = $pluginResult['contacts'];
196
197
198 $OUTPUT->command('ksearch_query_results', $contacts, $search, $reqid);
199 $OUTPUT->send();