imap_search.php 5.6 KB

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