imap_search.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 $msgs, $message_highlight_list, $squirrelmail_language, $languages,
  20. $index_order, $pos;
  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 (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, true, $result, $message);
  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. $msgs = '';
  54. /* Keep going till we find the SEARCH response */
  55. foreach ($readin as $readin_part) {
  56. /* Check to see if a SEARCH response was received */
  57. if (substr($readin_part, 0, 9) == '* SEARCH ') {
  58. $messagelist = explode(' ', substr($readin_part, 9));
  59. } else if (isset($errors)) {
  60. $errors = $errors.$readin_part;
  61. } else {
  62. $errors = $readin_part;
  63. }
  64. }
  65. /* If nothing is found * SEARCH should be the first error else echo errors */
  66. if (isset($errors)) {
  67. if (strstr($errors,'* SEARCH')) {
  68. if ($search_all != 'all') {
  69. echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
  70. }
  71. return;
  72. }
  73. echo "<!-- $errors -->";
  74. }
  75. /*
  76. * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify
  77. * existing code with a search true/false variable.
  78. */
  79. global $sent_folder;
  80. for ($q = 0; $q < count($messagelist); $q++) {
  81. $id[$q] = trim($messagelist[$q]);
  82. }
  83. $issent = ($mailbox == $sent_folder);
  84. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  85. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  86. foreach ($hdr_list as $hdr) {
  87. $from[] = $hdr->from;
  88. $date[] = $hdr->date;
  89. $subject[] = $hdr->subject;
  90. $to[] = $hdr->to;
  91. $priority[] = $hdr->priority;
  92. $cc[] = $hdr->cc;
  93. $size[] = $hdr->size;
  94. $type[] = $hdr->type0;
  95. }
  96. $j = 0;
  97. while ($j < count($messagelist)) {
  98. $date[$j] = str_replace(' ', ' ', $date[$j]);
  99. $tmpdate = explode(' ', trim($date[$j]));
  100. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  101. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  102. $messages[$j]["ID"] = $id[$j];
  103. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  104. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  105. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  106. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  107. $messages[$j]["TO"] = decodeHeader($to[$j]);
  108. $messages[$j]["PRIORITY"] = $priority[$j];
  109. $messages[$j]["CC"] = $cc[$j];
  110. $messages[$j]["SIZE"] = $size[$j];
  111. $messages[$j]["TYPE0"] = $type[$j];
  112. $num = 0;
  113. while ($num < count($flags[$j])) {
  114. if ($flags[$j][$num] == 'Deleted') {
  115. $messages[$j]['FLAG_DELETED'] = true;
  116. } else if ($flags[$j][$num] == 'Answered') {
  117. $messages[$j]['FLAG_ANSWERED'] = true;
  118. } else if ($flags[$j][$num] == 'Seen') {
  119. $messages[$j]['FLAG_SEEN'] = true;
  120. } else if ($flags[$j][$num] == 'Flagged') {
  121. $messages[$j]['FLAG_FLAGGED'] = true;
  122. }
  123. $num++;
  124. }
  125. $j++;
  126. }
  127. /* Find and remove the ones that are deleted */
  128. $i = 0;
  129. $j = 0;
  130. while ($j < count($messagelist)) {
  131. if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
  132. $j++;
  133. continue;
  134. }
  135. $msgs[$i] = $messages[$j];
  136. $i++;
  137. $j++;
  138. }
  139. $numMessages = $i;
  140. /* There's gotta be messages in the array for it to sort them. */
  141. if (count($messagelist) > 0) {
  142. $j=0;
  143. if (!isset ($msg)) {
  144. $msg = '';
  145. }
  146. if ($search_all != 'all') {
  147. if ( !isset( $start_msg ) ) {
  148. $start_msg =0;
  149. }
  150. if ( !isset( $sort ) ) {
  151. $sort = 0;
  152. }
  153. mail_message_listing_beginning( $imapConnection,
  154. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  155. $mailbox,
  156. -1,
  157. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
  158. get_selectall_link($start_msg, $sort));
  159. }
  160. else {
  161. mail_message_listing_beginning( $imapConnection,
  162. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  163. $mailbox,
  164. -1,
  165. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
  166. }
  167. if ( $mailbox == 'INBOX' ) {
  168. $showbox = _("INBOX");
  169. } else {
  170. $showbox = $mailbox;
  171. }
  172. echo '<b><big>' . _("Folder:") . " $showbox</big></b>";
  173. while ($j < count($msgs)) {
  174. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
  175. $j++;
  176. echo '</td></tr>';
  177. }
  178. echo '</table></td></tr></table></form>';
  179. $count_all = count($msgs);
  180. }
  181. return $count_all;
  182. }
  183. ?>