imap_search.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 = '', $search_all, $count_all) {
  18. global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
  19. global $pos;
  20. $pos = $search_position;
  21. $urlMailbox = urlencode($mailbox);
  22. /*
  23. Construct the Search QuERY
  24. account for multiple search terms
  25. */
  26. $multi_search = array ();
  27. $search_what = ereg_replace("[ ]{2,}", ' ', $search_what);
  28. $multi_search = split (' ', $search_what);
  29. if (count($multi_search)==1) {
  30. $search_string = $search_where . ' ' . '"' . $multi_search[0] . '"';
  31. }
  32. else {
  33. $search_string = '';
  34. $count = count($multi_search);
  35. for ($x=0;$x<$count;$x++) {
  36. $search_string = $search_string . ' ' . $search_where . ' "' . $multi_search[$x] . '" ';
  37. }
  38. }
  39. $search_string = trim($search_string);
  40. /* now use $search_string in the imap search */
  41. if (isset($languages[$squirrelmail_language]['CHARSET']) &&
  42. $languages[$squirrelmail_language]['CHARSET']) {
  43. $ss = "SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_string";
  44. } else {
  45. $ss .= "SEARCH ALL $search_string\"";
  46. }
  47. /* Read Data Back From IMAP */
  48. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  49. if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
  50. // $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"";
  51. $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
  52. $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message);
  53. }
  54. unset($messagelist);
  55. $msgs = '';
  56. $c = 0;
  57. /* Keep going till we find the SEARCH responce */
  58. while ($c < count( $readin )) {
  59. /* Check to see if a SEARCH Responce was recived */
  60. if (substr($readin[$c],0,9) == "* SEARCH ")
  61. $messagelist = explode(" ",substr($readin[$c],9));
  62. else if (isset($errors))
  63. $errors = $errors.$readin[$c];
  64. else
  65. $errors = $readin[$c];
  66. $c++;
  67. }
  68. /* If nothing is found * SEARCH should be the first error else echo errors */
  69. if (isset($errors) && strstr($errors,"* SEARCH")) {
  70. if ($search_all != "all") {
  71. echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
  72. return;
  73. }
  74. else {
  75. return;
  76. }
  77. }
  78. else if (isset($errors)) {
  79. echo "<!-- ".$errors." -->";
  80. }
  81. /*
  82. HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  83. exsitising code with a search true/false varible.
  84. */
  85. global $sent_folder;
  86. for ($q = 0; $q < count($messagelist); $q++) {
  87. $id[$q] = trim($messagelist[$q]);
  88. }
  89. $issent = ($mailbox == $sent_folder);
  90. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  91. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  92. foreach ($hdr_list as $hdr) {
  93. $from[] = $hdr->from;
  94. $date[] = $hdr->date;
  95. $subject[] = $hdr->subject;
  96. $to[] = $hdr->to;
  97. $priority[] = $hdr->priority;
  98. $cc[] = $hdr->cc;
  99. $size[] = $hdr->size;
  100. $type[] = $hdr->type0;
  101. }
  102. $j = 0;
  103. while ($j < count($messagelist)) {
  104. $date[$j] = str_replace(' ', ' ', $date[$j]);
  105. $tmpdate = explode(" ", trim($date[$j]));
  106. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  107. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  108. $messages[$j]["ID"] = $id[$j];
  109. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  110. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  111. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  112. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  113. $messages[$j]["TO"] = decodeHeader($to[$j]);
  114. $messages[$j]["PRIORITY"] = $priority[$j];
  115. $messages[$j]["CC"] = $cc[$j];
  116. $messages[$j]["SIZE"] = $size[$j];
  117. $messages[$j]["TYPE0"] = $type[$j];
  118. $num = 0;
  119. while ($num < count($flags[$j])) {
  120. if ($flags[$j][$num] == 'Deleted') {
  121. $messages[$j]['FLAG_DELETED'] = true;
  122. } else if ($flags[$j][$num] == 'Answered') {
  123. $messages[$j]['FLAG_ANSWERED'] = true;
  124. } else if ($flags[$j][$num] == 'Seen') {
  125. $messages[$j]['FLAG_SEEN'] = true;
  126. } else if ($flags[$j][$num] == 'Flagged') {
  127. $messages[$j]['FLAG_FLAGGED'] = true;
  128. }
  129. $num++;
  130. }
  131. $j++;
  132. }
  133. /* Find and remove the ones that are deleted */
  134. $i = 0;
  135. $j = 0;
  136. while ($j < count($messagelist)) {
  137. if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
  138. $j++;
  139. continue;
  140. }
  141. $msgs[$i] = $messages[$j];
  142. $i++;
  143. $j++;
  144. }
  145. $numMessages = $i;
  146. /* There's gotta be messages in the array for it to sort them. */
  147. if (count($messagelist) > 0) {
  148. $j=0;
  149. if (!isset ($msg)) {
  150. $msg = '';
  151. }
  152. if ($search_all != 'all') {
  153. if ( !isset( $start_msg ) ) {
  154. $start_msg =0;
  155. }
  156. if ( !isset( $sort ) ) {
  157. $sort = 0;
  158. }
  159. mail_message_listing_beginning( $imapConnection,
  160. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  161. $mailbox,
  162. -1,
  163. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
  164. get_selectall_link($start_msg, $sort));
  165. }
  166. else {
  167. mail_message_listing_beginning( $imapConnection,
  168. "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
  169. $mailbox,
  170. -1,
  171. '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
  172. }
  173. if ( $mailbox == 'INBOX' ) {
  174. $showbox = _("INBOX");
  175. } else {
  176. $showbox = $mailbox;
  177. }
  178. echo '<b><big>' . _("Folder:") . " $showbox</big></b>";
  179. while ($j < count($msgs)) {
  180. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
  181. $j++;
  182. echo '</td></tr>';
  183. }
  184. echo '</table></td></tr></table></form>';
  185. $count_all = count($msgs);
  186. }
  187. return $count_all;
  188. }
  189. ?>