imap_search.php 7.5 KB

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