imap_search.php 7.6 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, $allow_charset_search;
  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, 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. $flag_deleted[] = $hdr->flag_deleted;
  96. $flag_answered[] = $hdr->flag_answered;
  97. $flag_seen[] = $hdr->flag_seen;
  98. $flag_flagged[] = $hdr->flag_flagged;
  99. }
  100. $j = 0;
  101. while ($j < count($messagelist)) {
  102. $date[$j] = str_replace(' ', ' ', $date[$j]);
  103. $tmpdate = explode(' ', trim($date[$j]));
  104. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  105. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  106. $messages[$j]["ID"] = $id[$j];
  107. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  108. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  109. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  110. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  111. $messages[$j]["TO"] = decodeHeader($to[$j]);
  112. $messages[$j]["PRIORITY"] = $priority[$j];
  113. $messages[$j]["CC"] = $cc[$j];
  114. $messages[$j]["SIZE"] = $size[$j];
  115. $messages[$j]["TYPE0"] = $type[$j];
  116. $messages[$j]['FLAG_DELETED'] = $flag_deleted[$j];
  117. $messages[$j]['FLAG_ANSWERED'] = $flag_answered[$j];
  118. $messages[$j]['FLAG_SEEN'] = $flag_seen[$j];
  119. $messages[$j]['FLAG_FLAGGED'] = $flag_flagged[$j];
  120. /*
  121. $num = 0;
  122. while ($num < count($flags[$j])) {
  123. if ($flags[$j][$num] == 'Deleted') {
  124. $messages[$j]['FLAG_DELETED'] = true;
  125. } else if ($flags[$j][$num] == 'Answered') {
  126. $messages[$j]['FLAG_ANSWERED'] = true;
  127. } else if ($flags[$j][$num] == 'Seen') {
  128. $messages[$j]['FLAG_SEEN'] = true;
  129. } else if ($flags[$j][$num] == 'Flagged') {
  130. $messages[$j]['FLAG_FLAGGED'] = true;
  131. }
  132. $num++;
  133. }
  134. */
  135. $j++;
  136. }
  137. /* Find and remove the ones that are deleted */
  138. $i = 0;
  139. $j = 0;
  140. while ($j < count($messagelist)) {
  141. if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
  142. $j++;
  143. continue;
  144. }
  145. $msgs[$i] = $messages[$j];
  146. $i++;
  147. $j++;
  148. }
  149. $numMessages = $i;
  150. /* There's gotta be messages in the array for it to sort them. */
  151. if (count($messagelist) > 0) {
  152. $j=0;
  153. if (!isset ($msg)) {
  154. $msg = '';
  155. }
  156. if ($search_all != 'all') {
  157. if ( !isset( $start_msg ) ) {
  158. $start_msg =0;
  159. }
  160. if ( !isset( $sort ) ) {
  161. $sort = 0;
  162. }
  163. mail_message_listing_beginning( $imapConnection,
  164. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  165. $mailbox,
  166. -1,
  167. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
  168. get_selectall_link($start_msg, $sort));
  169. }
  170. else {
  171. mail_message_listing_beginning( $imapConnection,
  172. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  173. $mailbox,
  174. -1,
  175. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
  176. }
  177. if ( $mailbox == 'INBOX' ) {
  178. $showbox = _("INBOX");
  179. } else {
  180. $showbox = $mailbox;
  181. }
  182. echo '<b><big>' . _("Folder:") . " $showbox</big></b>";
  183. while ($j < count($msgs)) {
  184. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
  185. $j++;
  186. echo '</td></tr>';
  187. }
  188. echo '</table></td></tr></table></form>';
  189. $count_all = count($msgs);
  190. }
  191. return $count_all;
  192. }
  193. ?>