imap_search.php 5.7 KB

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