You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

autocomplete.inc 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | program/steps/mail/autocomplete.inc |
  5. | |
  6. | This file is part of the Roundcube Webmail client |
  7. | Copyright (C) 2008-2013, Roundcube Dev Team |
  8. | Copyright (C) 2011-2013, Kolab Systems AG |
  9. | |
  10. | Licensed under the GNU General Public License version 3 or |
  11. | any later version with exceptions for skins & plugins. |
  12. | See the README file for a full license statement. |
  13. | |
  14. | PURPOSE: |
  15. | Perform a search on configured address books for the address |
  16. | autocompletion of the message compose screen |
  17. +-----------------------------------------------------------------------+
  18. | Author: Thomas Bruederli <roundcube@gmail.com> |
  19. +-----------------------------------------------------------------------+
  20. */
  21. if ($RCMAIL->action == 'group-expand') {
  22. $abook = $RCMAIL->get_address_book(rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC));
  23. if ($gid = rcube_utils::get_input_value('_gid', rcube_utils::INPUT_GPC)) {
  24. $abook->set_group($gid);
  25. $abook->set_pagesize(1000); // TODO: limit number of group members by config
  26. $separator = trim($RCMAIL->config->get('recipients_separator', ',')) . ' ';
  27. $result = $abook->list_records($RCMAIL->config->get('contactlist_fields'));
  28. $members = array();
  29. while ($result && ($sql_arr = $result->iterate())) {
  30. $emails = (array) $abook->get_col_values('email', $sql_arr, true);
  31. if (!empty($emails) && ($email = array_shift($emails))) {
  32. $members[] = format_email_recipient($email, rcube_addressbook::compose_list_name($sql_arr));
  33. }
  34. }
  35. $OUTPUT->command('replace_group_recipients', $gid, join($separator, array_unique($members)));
  36. }
  37. $OUTPUT->send();
  38. }
  39. $MAXNUM = (int) $RCMAIL->config->get('autocomplete_max', 15);
  40. $mode = (int) $RCMAIL->config->get('addressbook_search_mode');
  41. $single = (bool) $RCMAIL->config->get('autocomplete_single');
  42. $search = rcube_utils::get_input_value('_search', rcube_utils::INPUT_GPC, true);
  43. $source = rcube_utils::get_input_value('_source', rcube_utils::INPUT_GPC);
  44. $reqid = rcube_utils::get_input_value('_reqid', rcube_utils::INPUT_GPC);
  45. if (strlen($source)) {
  46. $book_types = array($source);
  47. }
  48. else {
  49. $book_types = (array) $RCMAIL->config->get('autocomplete_addressbooks', 'sql');
  50. }
  51. if (!empty($book_types) && strlen($search)) {
  52. $contacts = array();
  53. $sort_keys = array();
  54. $books_num = count($book_types);
  55. $search_lc = mb_strtolower($search);
  56. foreach ($book_types as $id) {
  57. $abook = $RCMAIL->get_address_book($id);
  58. $abook->set_pagesize($MAXNUM);
  59. if ($result = $abook->search($RCMAIL->config->get('contactlist_fields'), $search, $mode, true, true, 'email')) {
  60. while ($sql_arr = $result->iterate()) {
  61. // Contact can have more than one e-mail address
  62. $email_arr = (array)$abook->get_col_values('email', $sql_arr, true);
  63. $email_cnt = count($email_arr);
  64. $idx = 0;
  65. foreach ($email_arr as $email) {
  66. if (empty($email)) {
  67. continue;
  68. }
  69. $name = rcube_addressbook::compose_list_name($sql_arr);
  70. $contact = format_email_recipient($email, $name);
  71. // skip entries that don't match
  72. if ($email_cnt > 1 && strpos(mb_strtolower($contact), $search_lc) === false) {
  73. continue;
  74. }
  75. $index = $contact;
  76. // skip duplicates
  77. if (empty($contacts[$index])) {
  78. $contact = array('name' => $contact, 'type' => $sql_arr['_type']);
  79. if (($display = rcube_addressbook::compose_search_name($sql_arr, $email, $name)) && $display != $contact['name']) {
  80. $contact['display'] = $display;
  81. }
  82. $contacts[$index] = $contact;
  83. $sort_keys[$index] = sprintf('%s %03d', $contact['display'] ?: $name, $idx++);
  84. if (count($contacts) >= $MAXNUM) {
  85. break 2;
  86. }
  87. }
  88. // skip redundant entries (show only first email address)
  89. if ($single) {
  90. break;
  91. }
  92. }
  93. }
  94. }
  95. // also list matching contact groups
  96. if ($abook->groups && count($contacts) < $MAXNUM) {
  97. foreach ($abook->list_groups($search, $mode) as $group) {
  98. $abook->reset();
  99. $abook->set_group($group['ID']);
  100. $group_prop = $abook->get_group($group['ID']);
  101. // group (distribution list) with email address(es)
  102. if ($group_prop['email']) {
  103. $idx = 0;
  104. foreach ((array)$group_prop['email'] as $email) {
  105. $index = format_email_recipient($email, $group['name']);
  106. if (empty($contacts[$index])) {
  107. $sort_keys[$index] = sprintf('%s %03d', $group['name'] , $idx++);
  108. $contacts[$index] = array(
  109. 'name' => $index,
  110. 'email' => $email,
  111. 'type' => 'group',
  112. 'id' => $group['ID'],
  113. 'source' => $id,
  114. );
  115. if (count($contacts) >= $MAXNUM) {
  116. break 2;
  117. }
  118. }
  119. }
  120. }
  121. // show group with count
  122. else if (($result = $abook->count()) && $result->count) {
  123. if (empty($contacts[$group['name']])) {
  124. $sort_keys[$group['name']] = $group['name'];
  125. $contacts[$group['name']] = array(
  126. 'name' => $group['name'] . ' (' . intval($result->count) . ')',
  127. 'type' => 'group',
  128. 'id' => $group['ID'],
  129. 'source' => $id
  130. );
  131. if (count($contacts) >= $MAXNUM) {
  132. break;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. if (count($contacts)) {
  140. // sort contacts index
  141. asort($sort_keys, SORT_LOCALE_STRING);
  142. // re-sort contacts according to index
  143. foreach ($sort_keys as $idx => $val) {
  144. $sort_keys[$idx] = $contacts[$idx];
  145. }
  146. $contacts = array_values($sort_keys);
  147. }
  148. }
  149. $OUTPUT->command('ksearch_query_results', $contacts, $search, $reqid);
  150. $OUTPUT->send();