imap_search.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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,$color, $search_position = '', $search_all, $count_all) {
  18. global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
  19. global $pos;
  20. $pos = $search_position;
  21. $urlMailbox = urlencode($mailbox);
  22. /* Construct the Search QuERY */
  23. # account for multiple search terms
  24. $multi_search = array ();
  25. $search_what = ereg_replace("[ ]{2,}", ' ', $search_what);
  26. $multi_search = split (' ', $search_what);
  27. if (count($multi_search)==1) {
  28. $search_string = $search_where . ' ' . '"' . $multi_search[0] . '"';
  29. }
  30. else {
  31. $search_string = '';
  32. $count = count($multi_search);
  33. for ($x=0;$x<$count;$x++) {
  34. $search_string = $search_string . ' ' . $search_where . " " . '"' . $multi_search[$x] . '" ';
  35. }
  36. }
  37. $search_string = trim($search_string);
  38. # now use $search_string in the imap search
  39. if (isset($languages[$squirrelmail_language]['CHARSET']) &&
  40. $languages[$squirrelmail_language]['CHARSET']) {
  41. $ss = "SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_string";
  42. } else {
  43. $ss .= "SEARCH ALL $search_string\"";
  44. }
  45. /* Read Data Back From IMAP */
  46. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  47. if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
  48. // $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"";
  49. $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
  50. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  51. }
  52. unset($messagelist); $msgs=""; $c = 0;
  53. /* Keep going till we find the SEARCH responce */
  54. while ($c < count( $readin )) {
  55. /* Check to see if a SEARCH Responce was recived */
  56. if (substr($readin[$c],0,9) == "* SEARCH ")
  57. $messagelist = explode(" ",substr($readin[$c],9));
  58. else if (isset($errors))
  59. $errors = $errors.$readin[$c];
  60. else
  61. $errors = $readin[$c];
  62. $c++;
  63. }
  64. /* If nothing is found * SEARCH should be the first error else echo errors */
  65. if (isset($errors) && strstr($errors,"* SEARCH")) {
  66. if ($search_all != "all") {
  67. echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
  68. return;
  69. }
  70. else {
  71. return;
  72. }
  73. }
  74. // else if ($search_all == 'all') {
  75. // return;
  76. // }
  77. else if (isset($errors)) {
  78. echo "<!-- ".$errors." -->";
  79. }
  80. /*
  81. HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  82. exsitising code with a search true/false varible.
  83. */
  84. global $sent_folder;
  85. for ($q = 0; $q < count($messagelist); $q++) {
  86. $id[$q] = trim($messagelist[$q]);
  87. }
  88. $issent = ($mailbox == $sent_folder);
  89. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  90. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  91. foreach ($hdr_list as $hdr) {
  92. $from[] = $hdr->from;
  93. $date[] = $hdr->date;
  94. $subject[] = $hdr->subject;
  95. $to[] = $hdr->to;
  96. $priority[] = $hdr->priority;
  97. $cc[] = $hdr->cc;
  98. $size[] = $hdr->size;
  99. $type[] = $hdr->type0;
  100. }
  101. $j = 0;
  102. while ($j < count($messagelist)) {
  103. $date[$j] = str_replace(' ', ' ', $date[$j]);
  104. $tmpdate = explode(" ", trim($date[$j]));
  105. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  106. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  107. $messages[$j]["ID"] = $id[$j];
  108. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  109. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  110. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  111. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  112. $messages[$j]["TO"] = decodeHeader($to[$j]);
  113. $messages[$j]["PRIORITY"] = $priority[$j];
  114. $messages[$j]["CC"] = $cc[$j];
  115. $messages[$j]["SIZE"] = $size[$j];
  116. $messages[$j]["TYPE0"] = $type[$j];
  117. $num = 0;
  118. while ($num < count($flags[$j])) {
  119. if ($flags[$j][$num] == 'Deleted') {
  120. $messages[$j]['FLAG_DELETED'] = true;
  121. } else if ($flags[$j][$num] == 'Answered') {
  122. $messages[$j]['FLAG_ANSWERED'] = true;
  123. } else if ($flags[$j][$num] == 'Seen') {
  124. $messages[$j]['FLAG_SEEN'] = true;
  125. } else if ($flags[$j][$num] == 'Flagged') {
  126. $messages[$j]['FLAG_FLAGGED'] = true;
  127. }
  128. $num++;
  129. }
  130. $j++;
  131. }
  132. /* Find and remove the ones that are deleted */
  133. $i = 0;
  134. $j = 0;
  135. while ($j < count($messagelist)) {
  136. if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
  137. $j++;
  138. continue;
  139. }
  140. $msgs[$i] = $messages[$j];
  141. $i++;
  142. $j++;
  143. }
  144. $numMessages = $i;
  145. /* There's gotta be messages in the array for it to sort them. */
  146. if (count($messagelist) > 0) {
  147. $j=0;
  148. if (!isset ($msg)) {
  149. $msg = '';
  150. }
  151. mail_message_listing_beginning( $imapConnection,
  152. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  153. $mailbox,
  154. -1,
  155. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>');
  156. #get_selectall_link($start_msg, $sort) );
  157. # echo '<table width=100%>';
  158. echo "<b><big><center>$mailbox</center></big></b>";
  159. while ($j < count($msgs)) {
  160. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
  161. $j++;
  162. echo '</td></tr>';
  163. }
  164. echo '</table></td></tr></table></form>';
  165. $count_all += count($msgs);
  166. }
  167. return $count_all;
  168. }
  169. ?>