imap_search.php 5.6 KB

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