XXS fixes, as in stable
[squirrelmail.git] / functions / imap_search.php
1 <?php
2
3 /**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP search routines
10 *
11 * $Id$
12 */
13
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');
18
19 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
20 $color, $search_position = '', $search_all, $count_all) {
21
22 global $message_highlight_list, $squirrelmail_language, $languages,
23 $index_order, $pos, $allow_charset_search, $uid_support,
24 $imap_server_type;
25
26 $pos = $search_position;
27
28 $urlMailbox = urlencode($mailbox);
29
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);
35 $search_string = '';
36
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
43 as per the RFC */
44
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');
49 }
50 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
51 }
52 }
53 else {
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');
57 }
58 $search_string .= $search_where . ' {' . strlen($multi_search_part)
59 . "}\r\n" . $multi_search_part . ' ';
60 }
61 }
62
63 $search_string = trim($search_string);
64
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";
71 } else {
72 $ss = "SEARCH ALL $search_string";
73 }
74
75 /* read data back from IMAP */
76 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
77
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,
83 $result, $message);
84 }
85
86 unset($messagelist);
87
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;
95 } else {
96 $errors = $readin_part;
97 }
98 }
99
100 /* If nothing is found * SEARCH should be the first error else echo errors */
101 if (isset($errors)) {
102 if (strstr($errors,'* SEARCH')) {
103 return array();
104 }
105 echo '<!-- '.htmlspecialchars($errors) .' -->';
106 }
107
108
109 global $sent_folder;
110
111 $cnt = count($messagelist);
112 for ($q = 0; $q < $cnt; $q++) {
113 $id[$q] = trim($messagelist[$q]);
114 }
115 $issent = ($mailbox == $sent_folder);
116
117 $msgs = fillMessageArray($imapConnection,$id,$cnt);
118
119 return $msgs;
120 }
121
122
123
124 ?>