6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
14 require_once(SM_PATH
. 'functions/imap.php');
15 require_once(SM_PATH
. 'functions/date.php');
16 require_once(SM_PATH
. 'functions/mailbox_display.php');
17 require_once(SM_PATH
. 'functions/mime.php');
19 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
20 $color, $search_position = '', $search_all, $count_all) {
22 global $message_highlight_list, $squirrelmail_language, $languages,
23 $index_order, $pos, $allow_charset_search, $uid_support,
26 $pos = $search_position;
28 $urlMailbox = urlencode($mailbox);
30 /* construct the search query, taking multiple search terms into account */
31 $multi_search = array();
32 $search_what = trim($search_what);
33 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
34 $multi_search = explode(' ', $search_what);
37 /* it seems macosx does not support the prefered search
38 syntax so we fall back to the older style. This IMAP
39 server has a problem with multiple search terms. Instead
40 of returning the messages that match all the terms it
41 returns the messages that match each term. Could be fixed
42 on the client side, but should be fixed on the server
45 if ($imap_server_type == 'macosx') {
46 foreach ($multi_search as $multi_search_part) {
47 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
48 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
50 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
54 foreach ($multi_search as $multi_search_part) {
55 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
56 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
58 $search_string .= $search_where . ' {' . strlen($multi_search_part)
59 . "}\r\n" . $multi_search_part . ' ';
63 $search_string = trim($search_string);
65 /* now use $search_string in the imap search */
66 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
67 $languages[$squirrelmail_language]['CHARSET']) {
68 $ss = "SEARCH CHARSET "
69 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
70 . " ALL $search_string";
72 $ss = "SEARCH ALL $search_string";
75 /* read data back from IMAP */
76 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
78 /* try US-ASCII charset if search fails */
79 if (isset($languages[$squirrelmail_language]['CHARSET'])
80 && strtolower($result) == 'no') {
81 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
82 $readin = sqimap_run_command ($imapConnection, $ss, true,
88 /* Keep going till we find the SEARCH response */
89 foreach ($readin as $readin_part) {
90 /* Check to see if a SEARCH response was received */
91 if (substr($readin_part, 0, 9) == '* SEARCH ') {
92 $messagelist = preg_split("/ /", substr($readin_part, 9));
93 } else if (isset($errors)) {
94 $errors = $errors.$readin_part;
96 $errors = $readin_part;
100 /* If nothing is found * SEARCH should be the first error else echo errors */
101 if (isset($errors)) {
102 if (strstr($errors,'* SEARCH')) {
105 echo '<!-- '.htmlspecialchars($errors) .' -->';
111 $cnt = count($messagelist);
112 for ($q = 0; $q < $cnt; $q++
) {
113 $id[$q] = trim($messagelist[$q]);
115 $issent = ($mailbox == $sent_folder);
117 $msgs = fillMessageArray($imapConnection,$id,$cnt);