imap_search.php 7.4 KB

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