imap_search.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /******************************************************************
  3. ** IMAP SEARCH ROUTIES
  4. ** $Id$
  5. *****************************************************************/
  6. if (defined ('imap_search_php'))
  7. return;
  8. define ('imap_search_php', true);
  9. include("../functions/imap.php");
  10. include("../functions/date.php");
  11. include("../functions/array.php");
  12. include("../functions/mailbox_display.php");
  13. include("../functions/mime.php");
  14. function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
  15. global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
  16. $urlMailbox = urlencode($mailbox);
  17. # Construct the Search QuERY
  18. if (isset($languages[$squirrelmail_language]["CHARSET"]) && $languages[$squirrelmail_language]["CHARSET"]) {
  19. $ss = "a001 SEARCH CHARSET ".$languages[$squirrelmail_language]["CHARSET"]." ALL $search_where \"$search_what\"\r\n";
  20. } else {
  21. $ss = "a001 SEARCH ALL $search_where \"$search_what\"\r\n";
  22. }
  23. fputs($imapConnection,$ss);
  24. # Read Data Back From IMAP
  25. $readin = sqimap_read_data ($imapConnection, "a001", false, $result, $message);
  26. if (isset($languages[$squirrelmail_language]["CHARSET"]) && strtolower($result) == "no") {
  27. $ss = "a001 SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
  28. fputs ($imapConnection, $ss);
  29. $readin = sqimap_read_data ($imapConnection, "a001", true, $result, $message);
  30. }
  31. unset($messagelist); $msgs=""; $c = 0;
  32. #Keep going till we find the SEARCH responce
  33. while ($c < count($readin)) {
  34. #Check to see if a SEARCH Responce was recived
  35. if (substr($readin[$c],0,9) == "* SEARCH ")
  36. $messagelist = explode(" ",substr($readin[$c],9));
  37. else if (isset($errors))
  38. $errors = $errors.$readin[$c];
  39. else
  40. $errors = $readin[$c];
  41. $c++;
  42. }
  43. #If nothing is found * SEARCH should be the first error else echo errors
  44. if (isset($errors) && strstr($errors,"* SEARCH")) {
  45. echo "<br><CENTER>No Messages Found</CENTER>";
  46. return;
  47. } else if (isset($errors)) {
  48. echo "<!-- ".$errors." -->";
  49. }
  50. # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  51. # exsitising code with a search true/false varible.
  52. global $sent_folder;
  53. for ($q = 0; $q < count($messagelist); $q++) {
  54. $messagelist[$q] = trim($messagelist[$q]);
  55. if ($mailbox == $sent_folder)
  56. $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], true);
  57. else
  58. $hdr = sqimap_get_small_header ($imapConnection, $messagelist[$q], false);
  59. $from[$q] = $hdr->from;
  60. $date[$q] = $hdr->date;
  61. $subject[$q] = $hdr->subject;
  62. $to[$q] = $hdr->to;
  63. $priority[$q] = $hdr->priority;
  64. $cc[$q] = $hdr->cc;
  65. $size[$q] = $hdr->size;
  66. $type[$q] = $hdr->type0;
  67. $id[$q] = $messagelist[$q];
  68. $flags[$q] = sqimap_get_flags ($imapConnection, $messagelist[$q]);
  69. }
  70. $j = 0;
  71. while ($j < count($messagelist)) {
  72. $date[$j] = ereg_replace(' ', ' ', $date[$j]);
  73. $tmpdate = explode(" ", trim($date[$j]));
  74. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  75. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  76. $messages[$j]["ID"] = $id[$j];
  77. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  78. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  79. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  80. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  81. $messages[$j]["TO"] = decodeHeader($to[$j]);
  82. $messages[$j]["PRIORITY"] = $priority[$j];
  83. $messages[$j]["CC"] = $cc[$j];
  84. $messages[$j]["SIZE"] = $size[$j];
  85. $messages[$j]["TYPE0"] = $type[$j];
  86. $num = 0;
  87. while ($num < count($flags[$j])) {
  88. if ($flags[$j][$num] == "Deleted") {
  89. $messages[$j]["FLAG_DELETED"] = true;
  90. }
  91. else if ($flags[$j][$num] == "Answered") {
  92. $messages[$j]["FLAG_ANSWERED"] = true;
  93. }
  94. else if ($flags[$j][$num] == "Seen") {
  95. $messages[$j]["FLAG_SEEN"] = true;
  96. }
  97. else if ($flags[$j][$num] == "Flagged") {
  98. $messages[$j]["FLAG_FLAGGED"] = true;
  99. }
  100. $num++;
  101. }
  102. $j++;
  103. }
  104. /** Find and remove the ones that are deleted */
  105. $i = 0;
  106. $j = 0;
  107. while ($j < count($messagelist)) {
  108. if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
  109. $j++;
  110. continue;
  111. }
  112. $msgs[$i] = $messages[$j];
  113. $i++;
  114. $j++;
  115. }
  116. $numMessages = $i;
  117. // There's gotta be messages in the array for it to sort them.
  118. if (count($messagelist) > 0) {
  119. $j=0;
  120. if (!isset ($msg)) { $msg = ""; }
  121. mail_message_listing_beginning($imapConnection,
  122. "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
  123. '', -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
  124. '&nbsp;');
  125. while ($j < count($msgs)) {
  126. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
  127. $j++;
  128. }
  129. echo "</table>";
  130. echo "</tr></td></table>";
  131. }
  132. }
  133. ?>