imap_search.php 6.2 KB

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