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