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