imap_search.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. $isid = sqimap_session_id();
  18. /* Construct the Search QuERY */
  19. $ss = $isid;
  20. if (isset($languages[$squirrelmail_language]['CHARSET']) &&
  21. $languages[$squirrelmail_language]['CHARSET']) {
  22. $ss .= " SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_where \"$search_what\"\r\n";
  23. } else {
  24. $ss .= " 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, $isid, false, $result, $message);
  29. if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
  30. $ss = $isid . " SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
  31. fputs ($imapConnection, $ss);
  32. $readin = sqimap_read_data ($imapConnection, $isid, 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. $id[$q] = trim($messagelist[$q]);
  58. }
  59. $issent = ($mailbox == $sent_folder);
  60. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  61. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  62. foreach ($hdr_list as $hdr) {
  63. $from[] = $hdr->from;
  64. $date[] = $hdr->date;
  65. $subject[] = $hdr->subject;
  66. $to[] = $hdr->to;
  67. $priority[] = $hdr->priority;
  68. $cc[] = $hdr->cc;
  69. $size[] = $hdr->size;
  70. $type[] = $hdr->type0;
  71. }
  72. $j = 0;
  73. while ($j < count($messagelist)) {
  74. $date[$j] = ereg_replace(' ', ' ', $date[$j]);
  75. $tmpdate = explode(" ", trim($date[$j]));
  76. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  77. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  78. $messages[$j]["ID"] = $id[$j];
  79. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  80. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  81. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  82. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  83. $messages[$j]["TO"] = decodeHeader($to[$j]);
  84. $messages[$j]["PRIORITY"] = $priority[$j];
  85. $messages[$j]["CC"] = $cc[$j];
  86. $messages[$j]["SIZE"] = $size[$j];
  87. $messages[$j]["TYPE0"] = $type[$j];
  88. $num = 0;
  89. while ($num < count($flags[$j])) {
  90. if ($flags[$j][$num] == "Deleted") {
  91. $messages[$j]["FLAG_DELETED"] = true;
  92. }
  93. else if ($flags[$j][$num] == "Answered") {
  94. $messages[$j]["FLAG_ANSWERED"] = true;
  95. }
  96. else if ($flags[$j][$num] == "Seen") {
  97. $messages[$j]["FLAG_SEEN"] = true;
  98. }
  99. else if ($flags[$j][$num] == "Flagged") {
  100. $messages[$j]["FLAG_FLAGGED"] = true;
  101. }
  102. $num++;
  103. }
  104. $j++;
  105. }
  106. /** Find and remove the ones that are deleted */
  107. $i = 0;
  108. $j = 0;
  109. while ($j < count($messagelist)) {
  110. if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
  111. $j++;
  112. continue;
  113. }
  114. $msgs[$i] = $messages[$j];
  115. $i++;
  116. $j++;
  117. }
  118. $numMessages = $i;
  119. // There's gotta be messages in the array for it to sort them.
  120. if (count($messagelist) > 0) {
  121. $j=0;
  122. if (!isset ($msg)) { $msg = ""; }
  123. mail_message_listing_beginning($imapConnection,
  124. "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
  125. '', -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
  126. '&nbsp;');
  127. while ($j < count($msgs)) {
  128. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
  129. $j++;
  130. }
  131. echo "</table>";
  132. echo "</tr></td></table>";
  133. }
  134. }
  135. ?>