imap_search.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. /*****************************************************************/
  13. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  14. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  15. /*** + Base level indent should begin at left margin, as ***/
  16. /*** the require_once below. ***/
  17. /*** + All identation should consist of four space blocks ***/
  18. /*** + Tab characters are evil. ***/
  19. /*** + all comments should use "slash-star ... star-slash" ***/
  20. /*** style -- no pound characters, no slash-slash style ***/
  21. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  22. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  23. /*** + Please use ' instead of ", when possible. Note " ***/
  24. /*** should always be used in _( ) function calls. ***/
  25. /*** Thank you for your help making the SM code more readable. ***/
  26. /*****************************************************************/
  27. require_once('../functions/imap.php');
  28. require_once('../functions/date.php');
  29. require_once('../functions/array.php');
  30. require_once('../functions/mailbox_display.php');
  31. require_once('../functions/mime.php');
  32. function sqimap_search($imapConnection,$search_where,$search_what,$mailbox,$color) {
  33. global $msgs, $message_highlight_list, $squirrelmail_language, $languages, $index_order;
  34. $urlMailbox = urlencode($mailbox);
  35. $isid = sqimap_session_id();
  36. /* Construct the Search QuERY */
  37. $ss = $isid;
  38. if (isset($languages[$squirrelmail_language]['CHARSET']) &&
  39. $languages[$squirrelmail_language]['CHARSET']) {
  40. $ss .= " SEARCH CHARSET ".$languages[$squirrelmail_language]['CHARSET']." ALL $search_where \"$search_what\"\r\n";
  41. } else {
  42. $ss .= " SEARCH ALL $search_where \"$search_what\"\r\n";
  43. }
  44. fputs($imapConnection,$ss);
  45. # Read Data Back From IMAP
  46. $readin = sqimap_read_data ($imapConnection, $isid, false, $result, $message);
  47. if (isset($languages[$squirrelmail_language]['CHARSET']) && strtolower($result) == 'no') {
  48. $ss = $isid . " SEARCH CHARSET \"US-ASCII\" ALL $search_where \"$search_what\"\r\n";
  49. fputs ($imapConnection, $ss);
  50. $readin = sqimap_read_data ($imapConnection, $isid, true, $result, $message);
  51. }
  52. unset($messagelist); $msgs=""; $c = 0;
  53. /* Keep going till we find the SEARCH responce */
  54. while ($c < count( $readin )) {
  55. /* Check to see if a SEARCH Responce was recived */
  56. if (substr($readin[$c],0,9) == "* SEARCH ")
  57. $messagelist = explode(" ",substr($readin[$c],9));
  58. else if (isset($errors))
  59. $errors = $errors.$readin[$c];
  60. else
  61. $errors = $readin[$c];
  62. $c++;
  63. }
  64. #If nothing is found * SEARCH should be the first error else echo errors
  65. if (isset($errors) && strstr($errors,"* SEARCH")) {
  66. echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
  67. return;
  68. } else if (isset($errors)) {
  69. echo "<!-- ".$errors." -->";
  70. }
  71. # HACKED CODED FROM ANOTHER FUNCTION, Could Probably dump this and mondify
  72. # exsitising code with a search true/false varible.
  73. global $sent_folder;
  74. for ($q = 0; $q < count($messagelist); $q++) {
  75. $id[$q] = trim($messagelist[$q]);
  76. }
  77. $issent = ($mailbox == $sent_folder);
  78. $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
  79. $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
  80. foreach ($hdr_list as $hdr) {
  81. $from[] = $hdr->from;
  82. $date[] = $hdr->date;
  83. $subject[] = $hdr->subject;
  84. $to[] = $hdr->to;
  85. $priority[] = $hdr->priority;
  86. $cc[] = $hdr->cc;
  87. $size[] = $hdr->size;
  88. $type[] = $hdr->type0;
  89. }
  90. $j = 0;
  91. while ($j < count($messagelist)) {
  92. $date[$j] = ereg_replace(' ', ' ', $date[$j]);
  93. $tmpdate = explode(" ", trim($date[$j]));
  94. $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
  95. $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
  96. $messages[$j]["ID"] = $id[$j];
  97. $messages[$j]["FROM"] = decodeHeader($from[$j]);
  98. $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
  99. $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
  100. $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
  101. $messages[$j]["TO"] = decodeHeader($to[$j]);
  102. $messages[$j]["PRIORITY"] = $priority[$j];
  103. $messages[$j]["CC"] = $cc[$j];
  104. $messages[$j]["SIZE"] = $size[$j];
  105. $messages[$j]["TYPE0"] = $type[$j];
  106. $num = 0;
  107. while ($num < count($flags[$j])) {
  108. if ($flags[$j][$num] == 'Deleted') {
  109. $messages[$j]['FLAG_DELETED'] = true;
  110. } else if ($flags[$j][$num] == 'Answered') {
  111. $messages[$j]['FLAG_ANSWERED'] = true;
  112. } else if ($flags[$j][$num] == 'Seen') {
  113. $messages[$j]['FLAG_SEEN'] = true;
  114. } else if ($flags[$j][$num] == 'Flagged') {
  115. $messages[$j]['FLAG_FLAGGED'] = true;
  116. }
  117. $num++;
  118. }
  119. $j++;
  120. }
  121. /** Find and remove the ones that are deleted */
  122. $i = 0;
  123. $j = 0;
  124. while ($j < count($messagelist)) {
  125. if (isset($messages[$j]["FLAG_DELETED"]) && $messages[$j]["FLAG_DELETED"] == true) {
  126. $j++;
  127. continue;
  128. }
  129. $msgs[$i] = $messages[$j];
  130. $i++;
  131. $j++;
  132. }
  133. $numMessages = $i;
  134. // There's gotta be messages in the array for it to sort them.
  135. if (count($messagelist) > 0) {
  136. $j=0;
  137. if (!isset ($msg)) { $msg = ""; }
  138. mail_message_listing_beginning($imapConnection,
  139. "move_messages.php?msg=$msg&mailbox=$urlMailbox&where=".urlencode($search_where)."&what=".urlencode($search_what),
  140. $mailbox, -1, '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b>',
  141. '&nbsp;');
  142. while ($j < count($msgs)) {
  143. printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, "", 0, $search_where, $search_what);
  144. $j++;
  145. }
  146. echo '</table></tr></td></table>';
  147. }
  148. }
  149. ?>