imap_search.php 4.9 KB

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