patch #551871 for feature request #545070 from Stefan Tietke (stfn) regarding ut
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * $Id$
10 */
11
12 require_once('../src/validate.php');
13 require_once('../functions/imap.php');
14 require_once('../functions/imap_search.php');
15 require_once('../functions/imap_utf7_decode_local.php');
16 require_once('../functions/array.php');
17 require_once('../functions/strings.php');
18
19 global $allow_thread_sort;
20
21 /* here are some functions, could go in imap_search.php
22
23 this was here, pretty handy */
24 function s_opt( $val, $sel, $tit ) {
25 echo " <option value=\"$val\"";
26 if ( $sel == $val ) {
27 echo ' selected';
28 }
29 echo ">$tit</option>\n";
30 }
31
32 /* function to get the recent searches and put them in the attributes array */
33 function get_recent($username, $data_dir) {
34 $attributes = array();
35 $types = array('search_what', 'search_where', 'search_folder');
36 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
37 for ($x=1;$x<=$recent_count;$x++) {
38 reset($types);
39 foreach ($types as $key) {
40 $attributes[$key][$x] = getPref($data_dir, $username, $key.$x, "");
41 }
42 }
43 return $attributes;
44 }
45
46 /* function to get the saved searches and put them in the saved_attributes array */
47 function get_saved($username, $data_dir) {
48 $saved_attributes = array();
49 $types = array('saved_what', 'saved_where', 'saved_folder');
50 foreach ($types as $key) {
51 for ($x=1;;$x++) {
52 $saved_attributes[$key][$x] = getPref($data_dir, $username, $key."$x", "");
53 if ($saved_attributes[$key][$x] == "") {
54 array_pop($saved_attributes[$key]);
55 break;
56 }
57 }
58 }
59 return $saved_attributes;
60 }
61
62 /* function to update recent pref arrays */
63 function update_recent($what, $where, $mailbox, $username, $data_dir) {
64 $attributes = array();
65 $types = array('search_what', 'search_where', 'search_folder');
66 $input = array($what, $where, $mailbox);
67 $attributes = get_recent( $username, $data_dir);
68 reset($types);
69 $dupe = 'no';
70 for ($i=1;$i<=count($attributes['search_what']);$i++) {
71 if (isset($attributes['search_what'][$i])) {
72 if ($what == $attributes['search_what'][$i] &&
73 $where == $attributes['search_where'][$i] &&
74 $mailbox == $attributes['search_folder'][$i]) {
75 $dupe = 'yes';
76 }
77 }
78 }
79 if ($dupe == 'no') {
80 $i = 0;
81 foreach ($types as $key) {
82 array_push ($attributes[$key], $input[$i]);
83 array_shift ($attributes[$key]);
84 $i++;
85 }
86 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
87 $n=0;
88 for ($i=1;$i<=$recent_count;$i++) {
89 reset($types);
90 foreach ($types as $key) {
91 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
92 }
93 $n++;
94 }
95 }
96 }
97
98 /* function to forget a recent search */
99 function forget_recent($forget_index, $username, $data_dir) {
100 $attributes = array();
101 $types = array('search_what', 'search_where', 'search_folder');
102 $attributes = get_recent( $username, $data_dir);
103 reset($types);
104 foreach ($types as $key) {
105 array_splice($attributes[$key], $forget_index, 1);
106 array_unshift($attributes[$key], '');
107 }
108 reset($types);
109 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
110 $n=0;
111 for ($i=1;$i<=$recent_count;$i++) {
112 reset($types);
113 foreach ($types as $key) {
114 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
115 }
116 $n++;
117 }
118 }
119
120 /* function to delete a saved search */
121 function delete_saved($delete_index, $username, $data_dir) {
122 $types = array('saved_what', 'saved_where', 'saved_folder');
123 $attributes = get_saved($username, $data_dir);
124 foreach ($types as $key) {
125 array_splice($attributes[$key], $delete_index, 1);
126 }
127 reset($types);
128 $n=0;
129 $saved_count = count($attributes['saved_what']);
130 $last_element = $saved_count + 1;
131 for ($i=1;$i<=$saved_count;$i++) {
132 reset($types);
133 foreach ($types as $key) {
134 setPref($data_dir, $username, $key.$i, $attributes[$key][$n]);
135 }
136 $n++;
137 }
138 reset($types);
139 foreach($types as $key) {
140 removePref($data_dir, $username, $key.$last_element);
141 }
142 }
143
144 /* function to save a search from recent to saved */
145 function save_recent($save_index, $username, $data_dir) {
146 $attributes = array();
147 $types = array('search_what', 'search_where', 'search_folder');
148 $saved_types = array(0 => 'saved_what', 1 => 'saved_where', 2 => 'saved_folder');
149 $saved_array = get_saved($username, $data_dir);
150 $save_index = $save_index -1;
151 $saved_count = (count($saved_array['saved_what']) + 1);
152 $attributes = get_recent ($username, $data_dir);
153 $n = 0;
154 foreach ($types as $key) {
155 $slice = array_slice($attributes[$key], $save_index, 1);
156 $name = $saved_types[$n];
157 setPref($data_dir, $username, $name.$saved_count, $slice[0]);
158 $n++;
159 }
160 }
161
162 /* ------------------------ main ------------------------ */
163
164 /* reset these arrays on each page load just in case */
165 $attributes = array ();
166 $saved_attributes = array ();
167 $search_all = 'none';
168 $perbox_count = array ();
169 $recent_count = getPref($data_dir, $username, 'search_memory', 0);
170
171 /* get mailbox names */
172 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
173 $boxes = sqimap_mailbox_list($imapConnection);
174
175 /* set current mailbox to INBOX if none was selected or if page
176 was called to search all folders. */
177 if ( !isset($mailbox) || $mailbox == 'None' || $mailbox == '' ) {
178 $mailbox = $boxes[0]['unformatted'];
179 }
180 if ($mailbox == 'All Folders') {
181 $search_all = 'all';
182 }
183
184 displayPageHeader($color, $mailbox);
185
186 /* See how the page was called and fire off correct function */
187 if ((!isset($submit) || empty($submit)) && !empty($what)) {
188 $submit = _("Search");
189 }
190 if ( !isset( $submit ) ) {
191 $submit = '';
192 } else if ($submit == _("Search") && !empty($what)) {
193 if ($recent_count > 0) {
194 update_recent($what, $where, $mailbox, $username, $data_dir);
195 }
196 }
197 elseif ($submit == 'forget') {
198 forget_recent($count, $username, $data_dir);
199 }
200 elseif ($submit == 'save') {
201 save_recent($count, $username, $data_dir);
202 }
203 elseif ($submit == 'delete') {
204 delete_saved($count, $username, $data_dir);
205 }
206
207 do_hook('search_before_form');
208
209 echo "<BR>\n".
210 "<table width=\"100%\">\n".
211 "<TR><td bgcolor=\"$color[0]\">\n".
212 "<CENTER><B>" . _("Search") . "</B></CENTER>\n".
213 "</TD></TR>\n".
214 "</TABLE>\n";
215
216 /* update the recent and saved searches from the pref files */
217 $attributes = get_recent($username, $data_dir);
218 $saved_attributes = get_saved($username, $data_dir);
219 $saved_count = count($saved_attributes['saved_what']);
220 $count_all = 0;
221
222 /* Saved Search Table */
223 if ($saved_count > 0) {
224 echo "<BR>\n"
225 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>"
226 . '<TR><TD align=center><B>Saved Searches</B></TD></TR><TR><TD>'
227 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
228 for ($i=0; $i < $saved_count; ++$i) {
229 if ($i % 2) {
230 echo "<TR BGCOLOR=\"$color[0]\">";
231 } else {
232 echo "<TR BGCOLOR=\"$color[4]\">";
233 }
234 echo "<TD WIDTH=\"35%\">".$saved_attributes['saved_folder'][$i]."</TD>"
235 . "<TD ALIGN=LEFT>".$saved_attributes['saved_what'][$i]."</TD>"
236 . "<TD ALIGN=CENTER>".$saved_attributes['saved_where'][$i]."</TD>"
237 . '<TD ALIGN=RIGHT>'
238 . '<A HREF=search.php'
239 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
240 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
241 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
242 . '>' . _("edit") . '</A>'
243 . '&nbsp;|&nbsp;'
244 . '<A HREF=search.php'
245 . '?mailbox=' . urlencode($saved_attributes['saved_folder'][$i])
246 . '&amp;what=' . urlencode($saved_attributes['saved_what'][$i])
247 . '&amp;where=' . urlencode($saved_attributes['saved_where'][$i])
248 . '&amp;submit=Search_no_update'
249 . '>' . _("search") . '</A>'
250 . '&nbsp;|&nbsp;'
251 . "<A HREF=search.php?count=$i&amp;submit=delete>"
252 . _("delete")
253 . '</A>'
254 . '</TD></TR>';
255 }
256 echo "</TABLE></TD></TR></TABLE>\n";
257 }
258
259 /* Recent Search Table */
260 if ($recent_count > 0) {
261 echo "<BR>\n"
262 . "<TABLE WIDTH=\"95%\" BGCOLOR=\"$color[9]\" ALIGN=\"CENTER\" CELLPADDING=1 CELLSPACING=1>\n"
263 . '<TR><TD ALIGN=CENTER><B>' . _("Recent Searches") . '</B></TD></TR><TR><TD>'
264 . '<TABLE WIDTH="100%" ALIGN="CENTER" CELLPADDING=0 CELLSPACING=0>';
265 for ($i=1; $i <= $recent_count; ++$i) {
266 if (isset($attributes['search_folder'][$i])) {
267 if ($attributes['search_folder'][$i] == "") {
268 $attributes['search_folder'][$i] = "INBOX";
269 }
270 }
271 if ($i % 2) {
272 echo "<TR BGCOLOR=\"$color[0]\">";
273 } else {
274 echo "<TR BGCOLOR=\"$color[4]\">";
275 }
276 if (isset($attributes['search_what'][$i]) &&
277 !empty($attributes['search_what'][$i])) {
278 echo "<TD WIDTH=35%>".$attributes['search_folder'][$i]."</TD>"
279 . "<TD ALIGN=LEFT>".$attributes['search_what'][$i]."</TD>"
280 . "<TD ALIGN=CENTER>".$attributes['search_where'][$i]."</TD>"
281 . '<TD ALIGN=RIGHT>'
282 . "<A HREF=search.php?count=$i&amp;submit=save>"
283 . _("save")
284 . '</A>'
285 . '&nbsp;|&nbsp;'
286 . '<A HREF=search.php'
287 . '?mailbox=' . urlencode($attributes['search_folder'][$i])
288 . '&amp;what=' . urlencode($attributes['search_what'][$i])
289 . '&amp;where=' . urlencode($attributes['search_where'][$i])
290 . '&amp;submit=Search_no_update'
291 . '>' . _("search") . '</A>'
292 . '&nbsp;|&nbsp;'
293 . "<A HREF=search.php?count=$i&amp;submit=forget>"
294 . _("forget")
295 . '</A>'
296 . '</TD></TR>';
297 }
298 }
299 echo '</TABLE></TD></TR></TABLE><BR>';
300 }
301
302 /* Search Form */
303 echo '<B>' . _("Current Search") . '</B>'
304 . '<FORM ACTION="search.php" NAME=s>'
305 . ' <TABLE WIDTH="95%" CELLPADDING=0 CELLSPACING=0>'
306 . ' <TR>'
307 . ' <TD><SELECT NAME="mailbox">';
308 for ($i = 0; $i < count($boxes); $i++) {
309 if (!in_array('noselect', $boxes[$i]['flags'])) {
310 $box = $boxes[$i]['unformatted'];
311 $box2 = imap_utf7_decode_local(
312 str_replace(' ', '&nbsp;', $boxes[$i]['unformatted-disp']));
313 if( $box2 == 'INBOX' ) {
314 $box2 = _("INBOX");
315 }
316 if ($mailbox == $box) {
317 echo " <OPTION VALUE=\"$box\" SELECTED>$box2</OPTION>\n";
318 }
319 else {
320 echo " <OPTION VALUE=\"$box\">$box2</OPTION>\n";
321 }
322 }
323 }
324 echo "<OPTION VALUE=\"All Folders\"";
325 if ($mailbox == "All Folders") {
326 echo "SELECTED";
327 }
328 echo ">All folders</OPTION>\n";
329 echo ' </SELECT>'.
330 " </TD>\n".
331 " <TD ALIGN=\"CENTER\">\n";
332 if ( !isset( $what ) ) {
333 $what = '';
334 }
335 if ( !isset( $where ) ) {
336 $where = '';
337 }
338
339
340 $what_disp = str_replace(',', ' ', $what);
341 $what_disp = str_replace('\\\\', '\\', $what_disp);
342 $what_disp = str_replace('\\"', '"', $what_disp);
343 $what_disp = str_replace('"', '&quot;', $what_disp);
344 echo " <INPUT TYPE=\"TEXT\" SIZE=\"35\" NAME=\"what\" VALUE=\"$what_disp\">\n".
345 " </TD>\n".
346 "<TD ALIGN=\"RIGHT\">\n".
347 "<SELECT NAME=\"where\">";
348 s_opt( 'BODY', $where, _("Body") );
349 s_opt( 'TEXT', $where, _("Everywhere") );
350 s_opt( 'SUBJECT', $where, _("Subject") );
351 s_opt( 'FROM', $where, _("From") );
352 s_opt( 'CC', $where, _("Cc") );
353 s_opt( 'TO', $where, _("To") );
354 echo " </SELECT>\n" .
355 " </TD>\n".
356 " <TD COLSPAN=\"3\" ALIGN=\"CENTER\">\n".
357 " <INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"" . _("Search") . "\">\n".
358 " </TD>\n".
359 " </TR>\n".
360 "</FORM>\n".
361 " </TABLE>\n".
362 "</TD></TR></TABLE>\n";
363
364
365 do_hook('search_after_form');
366
367 /*
368 search all folders option still in the works. returns a table for each
369 folder it finds a match in.
370 */
371
372 $old_value = 0;
373 if ($allow_thread_sort == TRUE) {
374 $old_value = $allow_thread_sort;
375 $allow_thread_sort = FALSE;
376 }
377
378 if ($search_all == 'all') {
379 $mailbox == '';
380 $boxcount = count($boxes);
381 echo '<BR><CENTER><B>' .
382 _("Search Results") .
383 "</B><CENTER><BR>\n";
384 for ($x=0;$x<$boxcount;$x++) {
385 if (!in_array('noselect', $boxes[$x]['flags'])) {
386 $mailbox = $boxes[$x]['unformatted'];
387 }
388 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
389 sqimap_mailbox_select($imapConnection, $mailbox);
390 $count_all = sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
391 array_push($perbox_count, $count_all);
392 }
393 }
394 for ($i=0;$i<count($perbox_count);$i++) {
395 if ($perbox_count[$i] != "") {
396 $count_all = "found";
397 break;
398 }
399 }
400 if ($count_all != "found") {
401 echo '<br><b>' .
402 _("No Messages found") .
403 '</b><br>';
404 }
405 }
406
407 /* search one folder option */
408 else {
409 if (($submit == _("Search") || $submit == 'Search_no_update') && !empty($what)) {
410 echo '<BR><CENTER><B>' .
411 _("Search Results") .
412 "</B></CENTER>\n";
413 sqimap_mailbox_select($imapConnection, $mailbox);
414 sqimap_search($imapConnection, $where, $what, $mailbox, $color, 0, $search_all, $count_all);
415 }
416 }
417
418 /* must have search terms to search */
419 if ($submit == _("Search") && empty($what)) {
420 echo "<BR><CENTER><B>Please enter something to search for</B></CENTER>\n";
421 }
422
423 $allow_thread_sort = $old_value;
424
425 do_hook('search_bottom');
426 sqimap_logout ($imapConnection);
427 echo '</body></html>';
428
429 ?>