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