| 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-2011, Roundcube Dev Team | |
|---|
| 9 | | Copyright (C) 2011, 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 | $Id$ |
|---|
| 23 | |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | if ($RCMAIL->action == 'group-expand') { |
|---|
| 27 | $abook = $RCMAIL->get_address_book(get_input_value('_source', RCUBE_INPUT_GPC)); |
|---|
| 28 | if ($gid = get_input_value('_gid', RCUBE_INPUT_GPC)) { |
|---|
| 29 | $members = array(); |
|---|
| 30 | $abook->set_group($gid); |
|---|
| 31 | $abook->set_pagesize(1000); // TODO: limit number of group members by config |
|---|
| 32 | $result = $abook->list_records(array('email','name')); |
|---|
| 33 | while ($result && ($sql_arr = $result->iterate())) { |
|---|
| 34 | foreach ((array)$sql_arr['email'] as $email) { |
|---|
| 35 | $members[] = format_email_recipient($email, $sql_arr['name']); |
|---|
| 36 | break; // only expand one email per contact |
|---|
| 37 | } |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' '; |
|---|
| 41 | $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members))); |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | $OUTPUT->send(); |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15); |
|---|
| 49 | $mode = (int) $RCMAIL->config->get('addressbook_search_mode'); |
|---|
| 50 | $single = (bool) $RCMAIL->config->get('autocomplete_single'); |
|---|
| 51 | $search = get_input_value('_search', RCUBE_INPUT_GPC, true); |
|---|
| 52 | $source = get_input_value('_source', RCUBE_INPUT_GPC); |
|---|
| 53 | $sid = get_input_value('_id', RCUBE_INPUT_GPC); |
|---|
| 54 | |
|---|
| 55 | if (strlen($source)) |
|---|
| 56 | $book_types = array($source); |
|---|
| 57 | else |
|---|
| 58 | $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql'); |
|---|
| 59 | |
|---|
| 60 | if (!empty($book_types) && strlen($search)) { |
|---|
| 61 | $contacts = array(); |
|---|
| 62 | $sort_keys = array(); |
|---|
| 63 | $books_num = count($book_types); |
|---|
| 64 | $search_lc = mb_strtolower($search); |
|---|
| 65 | |
|---|
| 66 | foreach ($book_types as $id) { |
|---|
| 67 | $abook = $RCMAIL->get_address_book($id); |
|---|
| 68 | $abook->set_pagesize($MAXNUM); |
|---|
| 69 | |
|---|
| 70 | if ($result = $abook->search(array('email','name'), $search, $mode, true, true, 'email')) { |
|---|
| 71 | while ($sql_arr = $result->iterate()) { |
|---|
| 72 | // Contact can have more than one e-mail address |
|---|
| 73 | $email_arr = (array)$abook->get_col_values('email', $sql_arr, true); |
|---|
| 74 | $email_cnt = count($email_arr); |
|---|
| 75 | $idx = 0; |
|---|
| 76 | foreach ($email_arr as $email) { |
|---|
| 77 | if (empty($email)) { |
|---|
| 78 | continue; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | $contact = format_email_recipient($email, $sql_arr['name']); |
|---|
| 82 | |
|---|
| 83 | // skip entries that don't match |
|---|
| 84 | if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) { |
|---|
| 85 | continue; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // skip duplicates |
|---|
| 89 | if (!in_array($contact, $contacts)) { |
|---|
| 90 | $contacts[] = $contact; |
|---|
| 91 | $sort_keys[] = sprintf('%s %03d', $sql_arr['name'] , $idx++); |
|---|
| 92 | |
|---|
| 93 | if (count($contacts) >= $MAXNUM) |
|---|
| 94 | break 2; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | // skip redundant entries (show only first email address) |
|---|
| 98 | if ($single) { |
|---|
| 99 | break; |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // also list matching contact groups |
|---|
| 106 | if ($abook->groups && count($contacts) < $MAXNUM) { |
|---|
| 107 | foreach ($abook->list_groups($search) as $group) { |
|---|
| 108 | $abook->reset(); |
|---|
| 109 | $abook->set_group($group['ID']); |
|---|
| 110 | $group_prop = $abook->get_group($group['ID']); |
|---|
| 111 | |
|---|
| 112 | // group (distribution list) with email address(es) |
|---|
| 113 | if ($group_prop['email']) { |
|---|
| 114 | $idx = 0; |
|---|
| 115 | foreach ((array)$group_prop['email'] as $email) { |
|---|
| 116 | $contacts[] = format_email_recipient($email, $group['name']); |
|---|
| 117 | $sort_keys[] = sprintf('%s %03d', $group['name'] , $idx++); |
|---|
| 118 | |
|---|
| 119 | if (count($contacts) >= $MAXNUM) |
|---|
| 120 | break 2; |
|---|
| 121 | } |
|---|
| 122 | } |
|---|
| 123 | // show group with count |
|---|
| 124 | else if (($result = $abook->count()) && $result->count) { |
|---|
| 125 | $contacts[] = array('name' => $group['name'] . ' (' . intval($result->count) . ')', 'id' => $group['ID'], 'source' => $id); |
|---|
| 126 | $sort_keys[] = $group['name']; |
|---|
| 127 | |
|---|
| 128 | if (count($contacts) >= $MAXNUM) |
|---|
| 129 | break; |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | if (count($contacts)) { |
|---|
| 136 | // sort contacts index |
|---|
| 137 | asort($sort_keys, SORT_LOCALE_STRING); |
|---|
| 138 | // re-sort contacts according to index |
|---|
| 139 | foreach ($sort_keys as $idx => $val) { |
|---|
| 140 | $sort_keys[$idx] = $contacts[$idx]; |
|---|
| 141 | } |
|---|
| 142 | $contacts = array_values($sort_keys); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | $OUTPUT->command('ksearch_query_results', $contacts, $search, $sid); |
|---|
| 147 | $OUTPUT->send(); |
|---|