6 * Copyright (c) 1999-2005 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 $squirrelmail_language, $languages, $pos, $allow_charset_search,
30 $pos = $search_position;
32 /* construct the search query, taking multiple search terms into account */
33 $multi_search = array();
34 $search_what = trim($search_what);
35 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
36 $multi_search = explode(' ', $search_what);
39 /* it seems macosx and hmailserver do not support the prefered search
40 syntax so we fall back to the older style. This IMAP
41 server has a problem with multiple search terms. Instead
42 of returning the messages that match all the terms it
43 returns the messages that match each term. Could be fixed
44 on the client side, but should be fixed on the server
47 if ($imap_server_type == 'macosx' ||
$imap_server_type == 'hmailserver') {
48 foreach ($multi_search as $multi_search_part) {
49 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
50 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
52 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
56 foreach ($multi_search as $multi_search_part) {
57 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
58 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
60 $search_string .= $search_where . ' {' . strlen($multi_search_part)
61 . "}\r\n" . $multi_search_part . ' ';
65 $search_string = trim($search_string);
67 /* now use $search_string in the imap search */
68 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
69 $languages[$squirrelmail_language]['CHARSET']) {
70 $ss = "SEARCH CHARSET "
71 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
72 . " ALL $search_string";
74 $ss = "SEARCH ALL $search_string";
77 /* read data back from IMAP */
78 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE);
80 /* try US-ASCII charset if search fails */
81 if (isset($languages[$squirrelmail_language]['CHARSET'])
82 && strtolower($result) == 'no') {
83 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
84 $readin = sqimap_run_command ($imapConnection, $ss, true,
90 /* Keep going till we find the SEARCH response */
91 foreach ($readin as $readin_part) {
92 /* Check to see if a SEARCH response was received */
93 if (substr($readin_part, 0, 9) == '* SEARCH ') {
94 $messagelist = preg_split("/ /", substr($readin_part, 9));
95 } else if (isset($errors)) {
96 $errors = $errors.$readin_part;
98 $errors = $readin_part;
102 /* If nothing is found * SEARCH should be the first error else echo errors */
103 if (isset($errors)) {
104 if (strstr($errors,'* SEARCH')) {
107 echo '<!-- '.htmlspecialchars($errors) .' -->';
113 $cnt = count($messagelist);
114 for ($q = 0; $q < $cnt; $q++
) {
115 $id[$q] = trim($messagelist[$q]);
118 $msgs = fillMessageArray($imapConnection,$id,$cnt);