imap_search.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 = '') {
  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 = ereg_replace("^ ", "", $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. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  50. }
  51. unset($messagelist); $msgs=""; $c = 0;
  52. /* Keep going till we find the SEARCH responce */
  53. while ($c < count( $readin )) {
  54. /* Check to see if a SEARCH Responce was recived */
  55. if (substr($readin[$c],0,9) == "* SEARCH ")
  56. $messagelist = explode(" ",substr($readin[$c],9));
  57. else if (isset($errors))
  58. $errors = $errors.$readin[$c];
  59. else
  60. $errors = $readin[$c];
  61. $c++;
  62. }
  63. /* If nothing is found * SEARCH should be the first error else echo errors */
  64. if (isset($errors) && strstr($errors,"* SEARCH")) {
  65. echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
  66. return;
  67. } else if (isset($errors)) {
  68. echo "<!-- ".$errors." -->";
  69. }
  70. /*
  71. HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  72. exsitising code with a search true/false varible.
  73. */
  74. global $sent_folder;
  75. for ($q = 0; $q < count($messagelist); $q++) {
  76. $id[$q] = trim($messagelist[$q]);
  77. }
  78. $issent = ($mailbox == $sent_folder);
  79. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  80. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  81. foreach ($hdr_list as $hdr) {
  82. $from[] = $hdr->from;
  83. $date[] = $hdr->date;
  84. $subject[] = $hdr->subject;
  85. $to[] = $hdr->to;
  86. $priority[] = $hdr->priority;
  87. $cc[] = $hdr->cc;
  88. $size[] = $hdr->size;
  89. $type[] = $hdr->type0;
  90. }
  91. $j = 0;
  92. while ($j < count($messagelist)) {
  93. $date[$j] = ereg_replace(' ', ' ', $date[$j]);
  94. $tmpdate = explode(" ", trim($date[$j]));
  95. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  96. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  97. $messages[$j]["ID"] = $id[$j];
  98. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  99. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  100. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  101. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  102. $messages[$j]["TO"] = decodeHeader($to[$j]);
  103. $messages[$j]["PRIORITY"] = $priority[$j];
  104. $messages[$j]["CC"] = $cc[$j];
  105. $messages[$j]["SIZE"] = $size[$j];
  106. $messages[$j]["TYPE0"] = $type[$j];
  107. $num = 0;
  108. while ($num < count($flags[$j])) {
  109. if ($flags[$j][$num] == 'Deleted') {
  110. $messages[$j]['FLAG_DELETED'] = true;
  111. } else if ($flags[$j][$num] == 'Answered') {
  112. $messages[$j]['FLAG_ANSWERED'] = true;
  113. } else if ($flags[$j][$num] == 'Seen') {
  114. $messages[$j]['FLAG_SEEN'] = true;
  115. } else if ($flags[$j][$num] == 'Flagged') {
  116. $messages[$j]['FLAG_FLAGGED'] = true;
  117. }
  118. $num++;
  119. }
  120. $j++;
  121. }
  122. /* Find and remove the ones that are deleted */
  123. $i = 0;
  124. $j = 0;
  125. while ($j < count($messagelist)) {
  126. if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
  127. $j++;
  128. continue;
  129. }
  130. $msgs[$i] = $messages[$j];
  131. $i++;
  132. $j++;
  133. }
  134. $numMessages = $i;
  135. /* There's gotta be messages in the array for it to sort them. */
  136. if (count($messagelist) > 0) {
  137. $j=0;
  138. if (!isset ($msg)) {
  139. $msg = '';
  140. }
  141. mail_message_listing_beginning( $imapConnection,
  142. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  143. $mailbox,
  144. -1,
  145. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
  146. get_selectall_link($start_msg, $sort) );
  147. while ($j < count($msgs)) {
  148. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
  149. $j++;
  150. }
  151. echo '</table></td></tr></table></form>';
  152. }
  153. }
  154. ?>