imap_search.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. /* Construct the Search QuERY */
  23. if (isset($languages[$squirrelmail_language]['CHARSET']) &&
  24. $languages[$squirrelmail_language]['CHARSET']) {
  25. $ss = "SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_where \"$search_what\"";
  26. } else {
  27. $ss .= "SEARCH ALL $search_where \"$search_what\"";
  28. }
  29. /* Read Data Back From IMAP */
  30. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  31. if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
  32. $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"";
  33. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  34. }
  35. unset($messagelist); $msgs=""; $c = 0;
  36. /* Keep going till we find the SEARCH responce */
  37. while ($c < count( $readin )) {
  38. /* Check to see if a SEARCH Responce was recived */
  39. if (substr($readin[$c],0,9) == "* SEARCH ")
  40. $messagelist = explode(" ",substr($readin[$c],9));
  41. else if (isset($errors))
  42. $errors = $errors.$readin[$c];
  43. else
  44. $errors = $readin[$c];
  45. $c++;
  46. }
  47. /* If nothing is found * SEARCH should be the first error else echo errors */
  48. if (isset($errors) && strstr($errors,"* SEARCH")) {
  49. echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
  50. return;
  51. } else if (isset($errors)) {
  52. echo "<!-- ".$errors." -->";
  53. }
  54. /*
  55. HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  56. exsitising code with a search true/false varible.
  57. */
  58. global $sent_folder;
  59. for ($q = 0; $q < count($messagelist); $q++) {
  60. $id[$q] = trim($messagelist[$q]);
  61. }
  62. $issent = ($mailbox == $sent_folder);
  63. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  64. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  65. foreach ($hdr_list as $hdr) {
  66. $from[] = $hdr->from;
  67. $date[] = $hdr->date;
  68. $subject[] = $hdr->subject;
  69. $to[] = $hdr->to;
  70. $priority[] = $hdr->priority;
  71. $cc[] = $hdr->cc;
  72. $size[] = $hdr->size;
  73. $type[] = $hdr->type0;
  74. }
  75. $j = 0;
  76. while ($j < count($messagelist)) {
  77. $date[$j] = ereg_replace(' ', ' ', $date[$j]);
  78. $tmpdate = explode(" ", trim($date[$j]));
  79. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  80. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  81. $messages[$j]["ID"] = $id[$j];
  82. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  83. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  84. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  85. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  86. $messages[$j]["TO"] = decodeHeader($to[$j]);
  87. $messages[$j]["PRIORITY"] = $priority[$j];
  88. $messages[$j]["CC"] = $cc[$j];
  89. $messages[$j]["SIZE"] = $size[$j];
  90. $messages[$j]["TYPE0"] = $type[$j];
  91. $num = 0;
  92. while ($num < count($flags[$j])) {
  93. if ($flags[$j][$num] == 'Deleted') {
  94. $messages[$j]['FLAG_DELETED'] = true;
  95. } else if ($flags[$j][$num] == 'Answered') {
  96. $messages[$j]['FLAG_ANSWERED'] = true;
  97. } else if ($flags[$j][$num] == 'Seen') {
  98. $messages[$j]['FLAG_SEEN'] = true;
  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)) {
  123. $msg = '';
  124. }
  125. mail_message_listing_beginning( $imapConnection,
  126. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  127. $mailbox,
  128. -1,
  129. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
  130. get_selectall_link($start_msg, $sort) );
  131. while ($j < count($msgs)) {
  132. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
  133. $j++;
  134. }
  135. echo '</table></td></tr></table></form>';
  136. }
  137. }
  138. ?>