Masato
[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('../functions/imap.php');
15 require_once('../functions/date.php');
16 require_once('../functions/array.php');
17 require_once('../functions/mailbox_display.php');
18 require_once('../functions/mime.php');
19
20 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
21 $color, $search_position = '', $search_all, $count_all) {
22
23 global $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos, $allow_charset_search, $uid_support,
25 $imap_server_type;
26
27 $pos = $search_position;
28
29 $urlMailbox = urlencode($mailbox);
30
31 /* construct the search query, taking multiple search terms into account */
32 $multi_search = array();
33 $search_what = trim($search_what);
34 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
35 $multi_search = explode(' ', $search_what);
36 $search_string = '';
37
38 /* it seems macosx does not support the prefered search
39 syntax so we fall back to the older style. This IMAP
40 server has a problem with multiple search terms. Instead
41 of returning the messages that match all the terms it
42 returns the messages that match each term. Could be fixed
43 on the client side, but should be fixed on the server
44 as per the RFC */
45
46 if ($imap_server_type == 'macosx') {
47 foreach ($multi_search as $multi_search_part) {
48 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
49 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
50 }
51 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
52 }
53 }
54 else {
55 foreach ($multi_search as $multi_search_part) {
56 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
57 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
58 }
59 $search_string .= $search_where . ' {' . strlen($multi_search_part)
60 . "}\r\n" . $multi_search_part . ' ';
61 }
62 }
63
64 $search_string = trim($search_string);
65
66 /* now use $search_string in the imap search */
67 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
68 $languages[$squirrelmail_language]['CHARSET']) {
69 $ss = "SEARCH CHARSET "
70 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
71 . " ALL $search_string";
72 } else {
73 $ss = "SEARCH ALL $search_string";
74 }
75
76 /* read data back from IMAP */
77 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
78
79 /* try US-ASCII charset if search fails */
80 if (isset($languages[$squirrelmail_language]['CHARSET'])
81 && strtolower($result) == 'no') {
82 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
83 $readin = sqimap_run_command ($imapConnection, $ss, true,
84 $result, $message);
85 }
86
87 unset($messagelist);
88
89 /* Keep going till we find the SEARCH response */
90 foreach ($readin as $readin_part) {
91 /* Check to see if a SEARCH response was received */
92 if (substr($readin_part, 0, 9) == '* SEARCH ') {
93 $messagelist = preg_split("/ /", substr($readin_part, 9));
94 } else if (isset($errors)) {
95 $errors = $errors.$readin_part;
96 } else {
97 $errors = $readin_part;
98 }
99 }
100
101 /* If nothing is found * SEARCH should be the first error else echo errors */
102 if (isset($errors)) {
103 if (strstr($errors,'* SEARCH')) {
104 return array();
105 }
106 echo "<!-- $errors -->";
107 }
108
109
110 global $sent_folder;
111
112 $cnt = count($messagelist);
113 for ($q = 0; $q < $cnt; $q++) {
114 $id[$q] = trim($messagelist[$q]);
115 }
116 $issent = ($mailbox == $sent_folder);
117
118 $msgs = fillMessageArray($imapConnection,$id,$cnt);
119
120 return $msgs;
121 }
122
123
124
125 ?>