Masato
[squirrelmail.git] / functions / imap_search.php
CommitLineData
6e189ce2 1<?php
2ba13803 2
35586184 3/**
15e6162e 4 * imap_search.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
35586184 8 *
15e6162e 9 * IMAP search routines
35586184 10 *
15e6162e 11 * $Id$
35586184 12 */
13
35586184 14require_once('../functions/imap.php');
15require_once('../functions/date.php');
16require_once('../functions/array.php');
17require_once('../functions/mailbox_display.php');
18require_once('../functions/mime.php');
2ba13803 19
b1e29794 20function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
21 $color, $search_position = '', $search_all, $count_all) {
2ba13803 22
dba269b1 23 global $message_highlight_list, $squirrelmail_language, $languages,
2b3d9db0 24 $index_order, $pos, $allow_charset_search, $uid_support,
25 $imap_server_type;
dba269b1 26
53828ec5 27 $pos = $search_position;
2ba13803 28
29eb5486 29 $urlMailbox = urlencode($mailbox);
2ba13803 30
b1e29794 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 = '';
2b3d9db0 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) {
83be314a 48 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
49 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
50 }
2b3d9db0 51 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
52 }
53 }
54 else {
55 foreach ($multi_search as $multi_search_part) {
83be314a 56 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
57 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
58 }
2b3d9db0 59 $search_string .= $search_where . ' {' . strlen($multi_search_part)
60 . "}\r\n" . $multi_search_part . ' ';
61 }
5c9a316d 62 }
dba269b1 63
70c4fd84 64 $search_string = trim($search_string);
c92ef720 65
b1e29794 66 /* now use $search_string in the imap search */
65ffb3ce 67 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
29eb5486 68 $languages[$squirrelmail_language]['CHARSET']) {
8ceffac2 69 $ss = "SEARCH CHARSET "
70 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
71 . " ALL $search_string";
29eb5486 72 } else {
b1e29794 73 $ss = "SEARCH ALL $search_string";
29eb5486 74 }
b1e29794 75
76 /* read data back from IMAP */
dba269b1 77 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
b1e29794 78
79 /* try US-ASCII charset if search fails */
8ceffac2 80 if (isset($languages[$squirrelmail_language]['CHARSET'])
81 && strtolower($result) == 'no') {
fee5ad96 82 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
8ceffac2 83 $readin = sqimap_run_command ($imapConnection, $ss, true,
84 $result, $message);
29eb5486 85 }
2ba13803 86
70c4fd84 87 unset($messagelist);
b1e29794 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 ') {
dba269b1 93 $messagelist = preg_split("/ /", substr($readin_part, 9));
b1e29794 94 } else if (isset($errors)) {
95 $errors = $errors.$readin_part;
96 } else {
97 $errors = $readin_part;
98 }
29eb5486 99 }
2ba13803 100
29eb5486 101 /* If nothing is found * SEARCH should be the first error else echo errors */
b1e29794 102 if (isset($errors)) {
103 if (strstr($errors,'* SEARCH')) {
5fac2758 104 return array();
70c4fd84 105 }
b1e29794 106 echo "<!-- $errors -->";
29eb5486 107 }
2ba13803 108
109
29eb5486 110 global $sent_folder;
dba269b1 111
112 $cnt = count($messagelist);
113 for ($q = 0; $q < $cnt; $q++) {
29eb5486 114 $id[$q] = trim($messagelist[$q]);
115 }
116 $issent = ($mailbox == $sent_folder);
f6b58812 117
20473d1e 118 $msgs = fillMessageArray($imapConnection,$id,$cnt);
2ba13803 119
dba269b1 120 return $msgs;
121}
2ba13803 122
2ba13803 123
fc05541b 124
1c72b151 125?>