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
13 * @deprecated This search interface has been largely replaced by asearch
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');
23 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
24 $color, $search_position = '', $search_all, $count_all) {
26 global $message_highlight_list, $squirrelmail_language, $languages,
27 $index_order, $pos, $allow_charset_search,
30 $pos = $search_position;
32 $urlMailbox = urlencode($mailbox);
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);
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
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');
54 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
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');
62 $search_string .= $search_where . ' {' . strlen($multi_search_part)
63 . "}\r\n" . $multi_search_part . ' ';
67 $search_string = trim($search_string);
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";
76 $ss = "SEARCH ALL $search_string";
79 /* read data back from IMAP */
80 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE);
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,
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;
100 $errors = $readin_part;
104 /* If nothing is found * SEARCH should be the first error else echo errors */
105 if (isset($errors)) {
106 if (strstr($errors,'* SEARCH')) {
109 echo '<!-- '.htmlspecialchars($errors) .' -->';
115 $cnt = count($messagelist);
116 for ($q = 0; $q < $cnt; $q++
) {
117 $id[$q] = trim($messagelist[$q]);
119 $issent = ($mailbox == $sent_folder);
121 $msgs = fillMessageArray($imapConnection,$id,$cnt);