6e189ce2 |
1 | <?php |
2ba13803 |
2 | |
35586184 |
3 | /** |
15e6162e |
4 | * imap_search.php |
35586184 |
5 | * |
6c84ba1e |
6 | * Copyright (c) 1999-2005 The SquirrelMail Project Team |
15e6162e |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
35586184 |
8 | * |
15e6162e |
9 | * IMAP search routines |
35586184 |
10 | * |
eb19bc67 |
11 | * @version $Id$ |
d6c32258 |
12 | * @package squirrelmail |
eb19bc67 |
13 | * @subpackage imap |
d6c32258 |
14 | * @deprecated This search interface has been largely replaced by asearch |
35586184 |
15 | */ |
16 | |
d6c32258 |
17 | /** |
18 | * Load up a bunch of SM functions */ |
b68edc75 |
19 | require_once(SM_PATH . 'functions/imap.php'); |
20 | require_once(SM_PATH . 'functions/date.php'); |
b68edc75 |
21 | require_once(SM_PATH . 'functions/mailbox_display.php'); |
22 | require_once(SM_PATH . 'functions/mime.php'); |
2ba13803 |
23 | |
b1e29794 |
24 | function sqimap_search($imapConnection, $search_where, $search_what, $mailbox, |
25 | $color, $search_position = '', $search_all, $count_all) { |
2ba13803 |
26 | |
ce68b76b |
27 | global $squirrelmail_language, $languages, $pos, $allow_charset_search, |
91e0dccc |
28 | $imap_server_type; |
dba269b1 |
29 | |
53828ec5 |
30 | $pos = $search_position; |
2ba13803 |
31 | |
b1e29794 |
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); |
37 | $search_string = ''; |
2b3d9db0 |
38 | |
b39825f0 |
39 | /* it seems macosx and hmailserver do not support the prefered search |
2b3d9db0 |
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 |
45 | as per the RFC */ |
46 | |
b39825f0 |
47 | if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') { |
2b3d9db0 |
48 | foreach ($multi_search as $multi_search_part) { |
83be314a |
49 | if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') { |
50 | $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto'); |
51 | } |
2b3d9db0 |
52 | $search_string .= $search_where . ' ' .$multi_search_part . ' '; |
53 | } |
54 | } |
55 | else { |
56 | foreach ($multi_search as $multi_search_part) { |
83be314a |
57 | if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') { |
58 | $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto'); |
59 | } |
2b3d9db0 |
60 | $search_string .= $search_where . ' {' . strlen($multi_search_part) |
61 | . "}\r\n" . $multi_search_part . ' '; |
62 | } |
5c9a316d |
63 | } |
dba269b1 |
64 | |
70c4fd84 |
65 | $search_string = trim($search_string); |
c92ef720 |
66 | |
b1e29794 |
67 | /* now use $search_string in the imap search */ |
65ffb3ce |
68 | if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) && |
29eb5486 |
69 | $languages[$squirrelmail_language]['CHARSET']) { |
8ceffac2 |
70 | $ss = "SEARCH CHARSET " |
62f7daa5 |
71 | . strtoupper($languages[$squirrelmail_language]['CHARSET']) |
8ceffac2 |
72 | . " ALL $search_string"; |
29eb5486 |
73 | } else { |
b1e29794 |
74 | $ss = "SEARCH ALL $search_string"; |
29eb5486 |
75 | } |
b1e29794 |
76 | |
77 | /* read data back from IMAP */ |
6201339c |
78 | $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE); |
b1e29794 |
79 | |
80 | /* try US-ASCII charset if search fails */ |
62f7daa5 |
81 | if (isset($languages[$squirrelmail_language]['CHARSET']) |
8ceffac2 |
82 | && strtolower($result) == 'no') { |
fee5ad96 |
83 | $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string"; |
62f7daa5 |
84 | $readin = sqimap_run_command ($imapConnection, $ss, true, |
8ceffac2 |
85 | $result, $message); |
29eb5486 |
86 | } |
2ba13803 |
87 | |
70c4fd84 |
88 | unset($messagelist); |
b1e29794 |
89 | |
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 ') { |
dba269b1 |
94 | $messagelist = preg_split("/ /", substr($readin_part, 9)); |
b1e29794 |
95 | } else if (isset($errors)) { |
96 | $errors = $errors.$readin_part; |
97 | } else { |
98 | $errors = $readin_part; |
99 | } |
29eb5486 |
100 | } |
2ba13803 |
101 | |
29eb5486 |
102 | /* If nothing is found * SEARCH should be the first error else echo errors */ |
b1e29794 |
103 | if (isset($errors)) { |
104 | if (strstr($errors,'* SEARCH')) { |
5fac2758 |
105 | return array(); |
70c4fd84 |
106 | } |
9b761dbd |
107 | echo '<!-- '.htmlspecialchars($errors) .' -->'; |
29eb5486 |
108 | } |
2ba13803 |
109 | |
110 | |
29eb5486 |
111 | global $sent_folder; |
dba269b1 |
112 | |
113 | $cnt = count($messagelist); |
114 | for ($q = 0; $q < $cnt; $q++) { |
29eb5486 |
115 | $id[$q] = trim($messagelist[$q]); |
116 | } |
f6b58812 |
117 | |
20473d1e |
118 | $msgs = fillMessageArray($imapConnection,$id,$cnt); |
2ba13803 |
119 | |
dba269b1 |
120 | return $msgs; |
121 | } |
2ba13803 |
122 | |
e50f5ac2 |
123 | ?> |