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