| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_search.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2004 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 | * @package squirrelmail |
| 13 | * @deprecated This search interface has been largely replaced by asearch |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * Load up a bunch of SM functions */ |
| 18 | require_once(SM_PATH . 'functions/imap.php'); |
| 19 | require_once(SM_PATH . 'functions/date.php'); |
| 20 | require_once(SM_PATH . 'functions/mailbox_display.php'); |
| 21 | require_once(SM_PATH . 'functions/mime.php'); |
| 22 | |
| 23 | function sqimap_search($imapConnection, $search_where, $search_what, $mailbox, |
| 24 | $color, $search_position = '', $search_all, $count_all) { |
| 25 | |
| 26 | global $message_highlight_list, $squirrelmail_language, $languages, |
| 27 | $index_order, $pos, $allow_charset_search, $uid_support, |
| 28 | $imap_server_type; |
| 29 | |
| 30 | $pos = $search_position; |
| 31 | |
| 32 | $urlMailbox = urlencode($mailbox); |
| 33 | |
| 34 | /* construct the search query, taking multiple search terms into account */ |
| 35 | $multi_search = array(); |
| 36 | $search_what = trim($search_what); |
| 37 | $search_what = ereg_replace('[ ]{2,}', ' ', $search_what); |
| 38 | $multi_search = explode(' ', $search_what); |
| 39 | $search_string = ''; |
| 40 | |
| 41 | /* it seems macosx does not support the prefered search |
| 42 | syntax so we fall back to the older style. This IMAP |
| 43 | server has a problem with multiple search terms. Instead |
| 44 | of returning the messages that match all the terms it |
| 45 | returns the messages that match each term. Could be fixed |
| 46 | on the client side, but should be fixed on the server |
| 47 | as per the RFC */ |
| 48 | |
| 49 | if ($imap_server_type == 'macosx') { |
| 50 | foreach ($multi_search as $multi_search_part) { |
| 51 | if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') { |
| 52 | $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto'); |
| 53 | } |
| 54 | $search_string .= $search_where . ' ' .$multi_search_part . ' '; |
| 55 | } |
| 56 | } |
| 57 | else { |
| 58 | foreach ($multi_search as $multi_search_part) { |
| 59 | if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') { |
| 60 | $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto'); |
| 61 | } |
| 62 | $search_string .= $search_where . ' {' . strlen($multi_search_part) |
| 63 | . "}\r\n" . $multi_search_part . ' '; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $search_string = trim($search_string); |
| 68 | |
| 69 | /* now use $search_string in the imap search */ |
| 70 | if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) && |
| 71 | $languages[$squirrelmail_language]['CHARSET']) { |
| 72 | $ss = "SEARCH CHARSET " |
| 73 | . strtoupper($languages[$squirrelmail_language]['CHARSET']) |
| 74 | . " ALL $search_string"; |
| 75 | } else { |
| 76 | $ss = "SEARCH ALL $search_string"; |
| 77 | } |
| 78 | |
| 79 | /* read data back from IMAP */ |
| 80 | $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support); |
| 81 | |
| 82 | /* try US-ASCII charset if search fails */ |
| 83 | if (isset($languages[$squirrelmail_language]['CHARSET']) |
| 84 | && strtolower($result) == 'no') { |
| 85 | $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string"; |
| 86 | $readin = sqimap_run_command ($imapConnection, $ss, true, |
| 87 | $result, $message); |
| 88 | } |
| 89 | |
| 90 | unset($messagelist); |
| 91 | |
| 92 | /* Keep going till we find the SEARCH response */ |
| 93 | foreach ($readin as $readin_part) { |
| 94 | /* Check to see if a SEARCH response was received */ |
| 95 | if (substr($readin_part, 0, 9) == '* SEARCH ') { |
| 96 | $messagelist = preg_split("/ /", substr($readin_part, 9)); |
| 97 | } else if (isset($errors)) { |
| 98 | $errors = $errors.$readin_part; |
| 99 | } else { |
| 100 | $errors = $readin_part; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | /* If nothing is found * SEARCH should be the first error else echo errors */ |
| 105 | if (isset($errors)) { |
| 106 | if (strstr($errors,'* SEARCH')) { |
| 107 | return array(); |
| 108 | } |
| 109 | echo '<!-- '.htmlspecialchars($errors) .' -->'; |
| 110 | } |
| 111 | |
| 112 | |
| 113 | global $sent_folder; |
| 114 | |
| 115 | $cnt = count($messagelist); |
| 116 | for ($q = 0; $q < $cnt; $q++) { |
| 117 | $id[$q] = trim($messagelist[$q]); |
| 118 | } |
| 119 | $issent = ($mailbox == $sent_folder); |
| 120 | |
| 121 | $msgs = fillMessageArray($imapConnection,$id,$cnt); |
| 122 | |
| 123 | return $msgs; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | |
| 128 | ?> |