imap_search.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. require_once('../functions/imap.php');
  10. require_once('../functions/date.php');
  11. require_once('../functions/array.php');
  12. require_once('../functions/mailbox_display.php');
  13. require_once('../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. $ss = sqimap_session_id();
  19. if (isset($languages[$squirrelmail_language]["CHARSET"]) && $languages[$squirrelmail_language]["CHARSET"]) {
  20. $ss = " SEARCH CHARSET ".$languages[$squirrelmail_language]["CHARSET"]." ALL $search_where \"$search_what\"\r\n";
  21. } else {
  22. $ss = " SEARCH ALL $search_where \"$search_what\"\r\n";
  23. }
  24. fputs($imapConnection,$ss);
  25. # Read Data Back From IMAP
  26. $readin = sqimap_read_data ($imapConnection, sqimap_session_id(), false, $result, $message);
  27. if (isset($languages[$squirrelmail_language]["CHARSET"]) && strtolower($result) == "no") {
  28. $ss = sqimap_session_id() . " SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
  29. fputs ($imapConnection, $ss);
  30. $readin = sqimap_read_data ($imapConnection, sqimap_session_id(), true, $result, $message);
  31. }
  32. unset($messagelist); $msgs=""; $c = 0;
  33. #Keep going till we find the SEARCH responce
  34. while ($c < count($readin)) {
  35. #Check to see if a SEARCH Responce was recived
  36. if (substr($readin[$c],0,9) == "* SEARCH ")
  37. $messagelist = explode(" ",substr($readin[$c],9));
  38. else if (isset($errors))
  39. $errors = $errors.$readin[$c];
  40. else
  41. $errors = $readin[$c];
  42. $c++;
  43. }
  44. #If nothing is found * SEARCH should be the first error else echo errors
  45. if (isset($errors) && strstr($errors,"* SEARCH")) {
  46. echo "<br><CENTER>No Messages Found</CENTER>";
  47. return;
  48. } else if (isset($errors)) {
  49. echo "<!-- ".$errors." -->";
  50. }
  51. # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  52. # exsitising code with a search true/false varible.
  53. global $sent_folder;
  54. for ($q = 0; $q < count($messagelist); $q++) {
  55. $id[$q] = trim($messagelist[$q]);
  56. }
  57. $issent = ($mailbox == $sent_folder);
  58. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  59. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  60. foreach ($hdr_list as $hdr) {
  61. $from[] = $hdr->from;
  62. $date[] = $hdr->date;
  63. $subject[] = $hdr->subject;
  64. $to[] = $hdr->to;
  65. $priority[] = $hdr->priority;
  66. $cc[] = $hdr->cc;
  67. $size[] = $hdr->size;
  68. $type[] = $hdr->type0;
  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. ?>