Fix bug #534795 concerning logo height broken
[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
b1e29794 23 global $msgs, $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos;
53828ec5 25
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 = '';
36 foreach ($multi_search as $multi_search_part) {
37 $search_string .= $search_where . ' "' . $multi_search_part . '" ';
38 }
39/*
70c4fd84 40 if (count($multi_search)==1) {
41 $search_string = $search_where . ' ' . '"' . $multi_search[0] . '"';
42 }
43 else {
44 $search_string = '';
b1e29794 45 $count = count($multi_search);
70c4fd84 46 for ($x=0;$x<$count;$x++) {
7e235a1a 47 trim($multi_search[$x]);
856b2ee2 48 $search_string = $search_string . ' ' . $search_where . ' "' . $multi_search[$x] . '"';
70c4fd84 49 }
50 }
b1e29794 51*/
70c4fd84 52 $search_string = trim($search_string);
c92ef720 53
b1e29794 54 /* now use $search_string in the imap search */
29eb5486 55 if (isset($languages[$squirrelmail_language]['CHARSET']) &&
56 $languages[$squirrelmail_language]['CHARSET']) {
8ceffac2 57 $ss = "SEARCH CHARSET "
58 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
59 . " ALL $search_string";
29eb5486 60 } else {
b1e29794 61 $ss = "SEARCH ALL $search_string";
29eb5486 62 }
b1e29794 63
64 /* read data back from IMAP */
65 $readin = sqimap_run_command($imapConnection, $ss, true, $result, $message);
66
67 /* try US-ASCII charset if search fails */
8ceffac2 68 if (isset($languages[$squirrelmail_language]['CHARSET'])
69 && strtolower($result) == 'no') {
fee5ad96 70 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
8ceffac2 71 $readin = sqimap_run_command ($imapConnection, $ss, true,
72 $result, $message);
29eb5486 73 }
2ba13803 74
70c4fd84 75 unset($messagelist);
76 $msgs = '';
b1e29794 77
78 /* Keep going till we find the SEARCH response */
79 foreach ($readin as $readin_part) {
80 /* Check to see if a SEARCH response was received */
81 if (substr($readin_part, 0, 9) == '* SEARCH ') {
82 $messagelist = explode(' ', substr($readin_part, 9));
83 } else if (isset($errors)) {
84 $errors = $errors.$readin_part;
85 } else {
86 $errors = $readin_part;
87 }
29eb5486 88 }
2ba13803 89
29eb5486 90 /* If nothing is found * SEARCH should be the first error else echo errors */
b1e29794 91 if (isset($errors)) {
92 if (strstr($errors,'* SEARCH')) {
93 if ($search_all != 'all') {
94 echo '<br><CENTER>' . _("No Messages Found") . '</CENTER>';
95 }
70c4fd84 96 return;
97 }
b1e29794 98 echo "<!-- $errors -->";
29eb5486 99 }
2ba13803 100
29eb5486 101 /*
b1e29794 102 * HACKED CODE FROM ANOTHER FUNCTION, could probably dump this and modify
103 * existing code with a search true/false variable.
104 */
2ba13803 105
29eb5486 106 global $sent_folder;
107 for ($q = 0; $q < count($messagelist); $q++) {
108 $id[$q] = trim($messagelist[$q]);
109 }
110 $issent = ($mailbox == $sent_folder);
111 $hdr_list = sqimap_get_small_header_list($imapConnection, $id, $issent);
112 $flags = sqimap_get_flags_list($imapConnection, $id, $issent);
113 foreach ($hdr_list as $hdr) {
114 $from[] = $hdr->from;
115 $date[] = $hdr->date;
116 $subject[] = $hdr->subject;
117 $to[] = $hdr->to;
118 $priority[] = $hdr->priority;
119 $cc[] = $hdr->cc;
120 $size[] = $hdr->size;
121 $type[] = $hdr->type0;
122 }
2ba13803 123
29eb5486 124 $j = 0;
125 while ($j < count($messagelist)) {
0f8a1ce9 126 $date[$j] = str_replace(' ', ' ', $date[$j]);
b1e29794 127 $tmpdate = explode(' ', trim($date[$j]));
29eb5486 128
129 $messages[$j]["TIME_STAMP"] = getTimeStamp($tmpdate);
130 $messages[$j]["DATE_STRING"] = getDateString($messages[$j]["TIME_STAMP"]);
131 $messages[$j]["ID"] = $id[$j];
132 $messages[$j]["FROM"] = decodeHeader($from[$j]);
133 $messages[$j]["FROM-SORT"] = strtolower(sqimap_find_displayable_name(decodeHeader($from[$j])));
134 $messages[$j]["SUBJECT"] = decodeHeader($subject[$j]);
135 $messages[$j]["SUBJECT-SORT"] = strtolower(decodeHeader($subject[$j]));
136 $messages[$j]["TO"] = decodeHeader($to[$j]);
137 $messages[$j]["PRIORITY"] = $priority[$j];
138 $messages[$j]["CC"] = $cc[$j];
139 $messages[$j]["SIZE"] = $size[$j];
140 $messages[$j]["TYPE0"] = $type[$j];
141
142 $num = 0;
143 while ($num < count($flags[$j])) {
144 if ($flags[$j][$num] == 'Deleted') {
145 $messages[$j]['FLAG_DELETED'] = true;
146 } else if ($flags[$j][$num] == 'Answered') {
147 $messages[$j]['FLAG_ANSWERED'] = true;
148 } else if ($flags[$j][$num] == 'Seen') {
149 $messages[$j]['FLAG_SEEN'] = true;
150 } else if ($flags[$j][$num] == 'Flagged') {
151 $messages[$j]['FLAG_FLAGGED'] = true;
2ba13803 152 }
29eb5486 153 $num++;
fc05541b 154 }
29eb5486 155 $j++;
156 }
2ba13803 157
29eb5486 158 /* Find and remove the ones that are deleted */
159 $i = 0;
160 $j = 0;
161 while ($j < count($messagelist)) {
70c4fd84 162 if (isset($messages[$j]['FLAG_DELETED']) && $messages[$j]['FLAG_DELETED']) {
1809bad8 163 $j++;
29eb5486 164 continue;
2ba13803 165 }
29eb5486 166 $msgs[$i] = $messages[$j];
2ba13803 167
29eb5486 168 $i++;
169 $j++;
170 }
171 $numMessages = $i;
2ba13803 172
29eb5486 173 /* There's gotta be messages in the array for it to sort them. */
2ba13803 174
29eb5486 175 if (count($messagelist) > 0) {
176 $j=0;
70c4fd84 177 if (!isset ($msg)) {
178 $msg = '';
53828ec5 179 }
8d93176b 180 if ($search_all != 'all') {
70c4fd84 181 if ( !isset( $start_msg ) ) {
182 $start_msg =0;
183 }
184 if ( !isset( $sort ) ) {
185 $sort = 0;
186 }
8d93176b 187 mail_message_listing_beginning( $imapConnection,
188 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
b1e29794 189 $mailbox,
8d93176b 190 -1,
191 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>'.
b1e29794 192 get_selectall_link($start_msg, $sort));
8d93176b 193 }
194 else {
195 mail_message_listing_beginning( $imapConnection,
196 "move_messages.php?msg=$msg&mailbox=$urlMailbox&pos=$pos&where=" . urlencode($search_where) . "&what=".urlencode($search_what),
b1e29794 197 $mailbox,
8d93176b 198 -1,
199 '<b>' . _("Found") . ' ' . count($messagelist) . ' ' . _("messages") . '</b></tr><tr>');
200 }
f8a2d9ee 201 if ( $mailbox == 'INBOX' ) {
202 $showbox = _("INBOX");
203 } else {
204 $showbox = $mailbox;
205 }
206 echo '<b><big>' . _("Folder:") . " $showbox</big></b>";
29eb5486 207 while ($j < count($msgs)) {
208 printMessageInfo($imapConnection, $msgs[$j]["ID"], 0, $j, $mailbox, '', 0, $search_where, $search_what);
209 $j++;
70c4fd84 210 echo '</td></tr>';
2ba13803 211 }
7c0060c9 212 echo '</table></td></tr></table></form>';
70c4fd84 213 $count_all = count($msgs);
2ba13803 214 }
70c4fd84 215 return $count_all;
29eb5486 216}
fc05541b 217
1c72b151 218?>