6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
12 * @package squirrelmail
14 * @deprecated This search interface has been largely replaced by asearch
18 * Load up a bunch of SM functions */
19 require_once(SM_PATH
. 'functions/imap.php');
20 require_once(SM_PATH
. 'functions/date.php');
21 require_once(SM_PATH
. 'functions/mailbox_display.php');
22 require_once(SM_PATH
. 'functions/mime.php');
24 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
25 $color, $search_position = '', $search_all, $count_all) {
27 global $message_highlight_list, $squirrelmail_language, $languages,
28 $index_order, $pos, $allow_charset_search,
31 $pos = $search_position;
33 $urlMailbox = urlencode($mailbox);
35 /* construct the search query, taking multiple search terms into account */
36 $multi_search = array();
37 $search_what = trim($search_what);
38 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
39 $multi_search = explode(' ', $search_what);
42 /* it seems macosx does not support the prefered search
43 syntax so we fall back to the older style. This IMAP
44 server has a problem with multiple search terms. Instead
45 of returning the messages that match all the terms it
46 returns the messages that match each term. Could be fixed
47 on the client side, but should be fixed on the server
50 if ($imap_server_type == 'macosx') {
51 foreach ($multi_search as $multi_search_part) {
52 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
53 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
55 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
59 foreach ($multi_search as $multi_search_part) {
60 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
61 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
63 $search_string .= $search_where . ' {' . strlen($multi_search_part)
64 . "}\r\n" . $multi_search_part . ' ';
68 $search_string = trim($search_string);
70 /* now use $search_string in the imap search */
71 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
72 $languages[$squirrelmail_language]['CHARSET']) {
73 $ss = "SEARCH CHARSET "
74 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
75 . " ALL $search_string";
77 $ss = "SEARCH ALL $search_string";
80 /* read data back from IMAP */
81 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE);
83 /* try US-ASCII charset if search fails */
84 if (isset($languages[$squirrelmail_language]['CHARSET'])
85 && strtolower($result) == 'no') {
86 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
87 $readin = sqimap_run_command ($imapConnection, $ss, true,
93 /* Keep going till we find the SEARCH response */
94 foreach ($readin as $readin_part) {
95 /* Check to see if a SEARCH response was received */
96 if (substr($readin_part, 0, 9) == '* SEARCH ') {
97 $messagelist = preg_split("/ /", substr($readin_part, 9));
98 } else if (isset($errors)) {
99 $errors = $errors.$readin_part;
101 $errors = $readin_part;
105 /* If nothing is found * SEARCH should be the first error else echo errors */
106 if (isset($errors)) {
107 if (strstr($errors,'* SEARCH')) {
110 echo '<!-- '.htmlspecialchars($errors) .' -->';
116 $cnt = count($messagelist);
117 for ($q = 0; $q < $cnt; $q++
) {
118 $id[$q] = trim($messagelist[$q]);
120 $issent = ($mailbox == $sent_folder);
122 $msgs = fillMessageArray($imapConnection,$id,$cnt);