imap_search.php 5.2 KB

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