imap_search.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * imap_search.php
  4. *
  5. * Copyright (c) 1999-2004 The SquirrelMail Project Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * IMAP search routines
  9. *
  10. * @version $Id$
  11. * @package squirrelmail
  12. * @subpackage imap
  13. * @deprecated This search interface has been largely replaced by asearch
  14. */
  15. /**
  16. * Load up a bunch of SM functions */
  17. require_once(SM_PATH . 'functions/imap.php');
  18. require_once(SM_PATH . 'functions/date.php');
  19. require_once(SM_PATH . 'functions/mailbox_display.php');
  20. require_once(SM_PATH . 'functions/mime.php');
  21. function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
  22. $color, $search_position = '', $search_all, $count_all) {
  23. global $message_highlight_list, $squirrelmail_language, $languages,
  24. $index_order, $pos, $allow_charset_search,
  25. $imap_server_type;
  26. $pos = $search_position;
  27. $urlMailbox = urlencode($mailbox);
  28. /* construct the search query, taking multiple search terms into account */
  29. $multi_search = array();
  30. $search_what = trim($search_what);
  31. $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
  32. $multi_search = explode(' ', $search_what);
  33. $search_string = '';
  34. /* it seems macosx does not support the prefered search
  35. syntax so we fall back to the older style. This IMAP
  36. server has a problem with multiple search terms. Instead
  37. of returning the messages that match all the terms it
  38. returns the messages that match each term. Could be fixed
  39. on the client side, but should be fixed on the server
  40. as per the RFC */
  41. if ($imap_server_type == 'macosx') {
  42. foreach ($multi_search as $multi_search_part) {
  43. if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
  44. $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
  45. }
  46. $search_string .= $search_where . ' ' .$multi_search_part . ' ';
  47. }
  48. }
  49. else {
  50. foreach ($multi_search as $multi_search_part) {
  51. if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
  52. $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
  53. }
  54. $search_string .= $search_where . ' {' . strlen($multi_search_part)
  55. . "}\r\n" . $multi_search_part . ' ';
  56. }
  57. }
  58. $search_string = trim($search_string);
  59. /* now use $search_string in the imap search */
  60. if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
  61. $languages[$squirrelmail_language]['CHARSET']) {
  62. $ss = "SEARCH CHARSET "
  63. . strtoupper($languages[$squirrelmail_language]['CHARSET'])
  64. . " ALL $search_string";
  65. } else {
  66. $ss = "SEARCH ALL $search_string";
  67. }
  68. /* read data back from IMAP */
  69. $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE);
  70. /* try US-ASCII charset if search fails */
  71. if (isset($languages[$squirrelmail_language]['CHARSET'])
  72. && strtolower($result) == 'no') {
  73. $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
  74. $readin = sqimap_run_command ($imapConnection, $ss, true,
  75. $result, $message);
  76. }
  77. unset($messagelist);
  78. /* Keep going till we find the SEARCH response */
  79. foreach ($readin as $readin_part) {
  80. /* Check to see if a SEARCH response was received */
  81. if (substr($readin_part, 0, 9) == '* SEARCH ') {
  82. $messagelist = preg_split("/ /", substr($readin_part, 9));
  83. } else if (isset($errors)) {
  84. $errors = $errors.$readin_part;
  85. } else {
  86. $errors = $readin_part;
  87. }
  88. }
  89. /* If nothing is found * SEARCH should be the first error else echo errors */
  90. if (isset($errors)) {
  91. if (strstr($errors,'* SEARCH')) {
  92. return array();
  93. }
  94. echo '<!-- '.htmlspecialchars($errors) .' -->';
  95. }
  96. global $sent_folder;
  97. $cnt = count($messagelist);
  98. for ($q = 0; $q < $cnt; $q++) {
  99. $id[$q] = trim($messagelist[$q]);
  100. }
  101. $issent = ($mailbox == $sent_folder);
  102. $msgs = fillMessageArray($imapConnection,$id,$cnt);
  103. return $msgs;
  104. }
  105. ?>