imap_search.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * imap_search.php
  4. *
  5. * Copyright (c) 1999-2002 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * IMAP search routines
  9. *
  10. * $Id$
  11. */
  12. require_once('../functions/imap.php');
  13. require_once('../functions/date.php');
  14. require_once('../functions/array.php');
  15. require_once('../functions/mailbox_display.php');
  16. require_once('../functions/mime.php');
  17. function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
  18. $color, $search_position = '', $search_all, $count_all) {
  19. global $message_highlight_list, $squirrelmail_language, $languages,
  20. $index_order, $pos, $allow_charset_search, $uid_support;
  21. $pos = $search_position;
  22. $urlMailbox = urlencode($mailbox);
  23. /* construct the search query, taking multiple search terms into account */
  24. $multi_search = array();
  25. $search_what = trim($search_what);
  26. $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
  27. $multi_search = explode(' ', $search_what);
  28. $search_string = '';
  29. foreach ($multi_search as $multi_search_part) {
  30. $search_string .= $search_where . ' {' . strlen($multi_search_part)
  31. . "}\r\n" . $multi_search_part . ' ';
  32. }
  33. $search_string = trim($search_string);
  34. /* now use $search_string in the imap search */
  35. if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
  36. $languages[$squirrelmail_language]['CHARSET']) {
  37. $ss = "SEARCH CHARSET "
  38. . strtoupper($languages[$squirrelmail_language]['CHARSET'])
  39. . " ALL $search_string";
  40. } else {
  41. $ss = "SEARCH ALL $search_string";
  42. }
  43. /* read data back from IMAP */
  44. $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
  45. /* try US-ASCII charset if search fails */
  46. if (isset($languages[$squirrelmail_language]['CHARSET'])
  47. && strtolower($result) == 'no') {
  48. $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
  49. $readin = sqimap_run_command ($imapConnection, $ss, true,
  50. $result, $message);
  51. }
  52. unset($messagelist);
  53. /* Keep going till we find the SEARCH response */
  54. foreach ($readin as $readin_part) {
  55. /* Check to see if a SEARCH response was received */
  56. if (substr($readin_part, 0, 9) == '* SEARCH ') {
  57. $messagelist = preg_split("/ /", substr($readin_part, 9));
  58. } else if (isset($errors)) {
  59. $errors = $errors.$readin_part;
  60. } else {
  61. $errors = $readin_part;
  62. }
  63. }
  64. /* If nothing is found * SEARCH should be the first error else echo errors */
  65. if (isset($errors)) {
  66. if (strstr($errors,'* SEARCH')) {
  67. return array();
  68. }
  69. echo "<!-- $errors -->";
  70. }
  71. global $sent_folder;
  72. $cnt = count($messagelist);
  73. for ($q = 0; $q < $cnt; $q++) {
  74. $id[$q] = trim($messagelist[$q]);
  75. }
  76. $issent = ($mailbox == $sent_folder);
  77. $msgs = fillMessageArray($imapConnection,$id,$cnt);
  78. return $msgs;
  79. }
  80. ?>