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