fixed bug 777849 (unnecessary   from subject)
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
a5ee5ac2 4 * search.php
35586184 5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
6b37252b 9 * Originally by Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
35586184 10 */
2d367c68 11
86725763 12/* Path for SquirrelMail required files. */
13define('SM_PATH','../');
14
15/* SquirrelMail required files. */
08185f2a 16require_once(SM_PATH . 'include/validate.php');
b2e7eb53 17require_once(SM_PATH . 'functions/strings.php');
6b37252b 18require_once(SM_PATH . 'functions/imap_asearch.php');
86725763 19require_once(SM_PATH . 'functions/imap_mailbox.php');
b65c5db0 20require_once(SM_PATH . 'functions/imap_messages.php');
0ca9a6ec 21require_once(SM_PATH . 'functions/mime.php');
b2e7eb53 22require_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()...
c61bb006 23
6b37252b 24function asearch_unhtml_strcoll($a, $b)
25{
26 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
27}
28
29function imap_get_mailbox_display($mailbox)
30{
3dffe83f 31 if (strtoupper($mailbox) == 'INBOX')
6b37252b 32 return _("INBOX");
33 return imap_utf7_decode_local($mailbox);
34}
56e0b3b7 35
6b37252b 36function asearch_get_mailbox_display($mailbox)
37{
38 if ($mailbox == 'All Folders')
39 return _("All Folders");
40 return imap_get_mailbox_display($mailbox);
41}
0b97a708 42
05f5b952 43function asearch_get_title_display($color, $txt)
6b37252b 44{
45 return '<b><big>' . $txt . '</big></b>';
46}
0b97a708 47
05f5b952 48function asearch_get_error_display($color, $txt)
6b37252b 49{
05f5b952 50 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
51/*return '<b><big>' . $txt . '</big></b>';*/
0b97a708 52}
6b37252b 53
54function asearch_serialize($input_array)
55{
56/*return $input_array[0];*/
57 return serialize($input_array);
0b97a708 58}
6b37252b 59
60function asearch_unserialize($input_string)
61{
62/*return array($input_string);*/
63 return unserialize($input_string);
0b97a708 64}
6b37252b 65
66function asearch_getPref($data_dir, $username, $key, $index, $default = '')
67{
68 return getPref($data_dir, $username, $key . $index, $default);
0b97a708 69}
6b37252b 70
71function asearch_setPref($data_dir, $username, $key, $index, $value)
72{
73 return setPref($data_dir, $username, $key . $index, $value);
0b97a708 74}
6b37252b 75
76function asearch_removePref($data_dir, $username, $key, $index)
77{
78 return removePref($data_dir, $username, $key . $index);
0b97a708 79}
0b97a708 80
6b37252b 81/* sanity checks, done before running the imap command and before push_recent */
05f5b952 82function asearch_check_query($where_array, $what_array, $exclude_array)
6b37252b 83{
05f5b952 84 global $imap_asearch_opcodes;
85
6b37252b 86 if (empty($where_array))
87 return _("Please enter something to search for");
88 if (count($exclude_array) == count($where_array))
89 return _("There must be at least one criteria to search for");
05f5b952 90 for ($crit_num = 0; $crit_num < count($where_array); $crit_num++) {
91 $where = $where_array[$crit_num];
92 $what = $what_array[$crit_num];
93 if (!(($what == '') ^ ($imap_asearch_opcodes[$where] != '')))
94 return _("Error in criteria argument");
95 }
6b37252b 96 return '';
56e0b3b7 97}
98
6b37252b 99/* read the recent searches */
100function asearch_read_recent($data_dir, $username)
101{
102 global $recent_prefkeys;
103
104 $recent_array = array();
105 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
106 for ($recent_num = 0; $recent_num < $recent_max; $recent_num++) {
107 foreach ($recent_prefkeys as $prefkey) {
108 $pref = asearch_getPref($data_dir, $username, $prefkey, $recent_num);
109/* if (!empty($pref))*/
110 $recent_array[$prefkey][$recent_num] = $pref;
111 }
112 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
113 foreach ($recent_prefkeys as $key) {
114 array_pop($recent_array[$key]);
115 }
116 break;
117 }
118 }
119 return $recent_array;
56e0b3b7 120}
23a9084b 121
6b37252b 122/* get the saved searches */
123function asearch_read_saved($data_dir, $username)
124{
125 global $saved_prefkeys;
126
127 $saved_array = array();
128 $saved_key = $saved_prefkeys[0];
129 for ($saved_count = 0; ; $saved_count++) {
130 $pref = asearch_getPref($data_dir, $username, $saved_key, $saved_count);
131 if (empty($pref))
132 break;
133 }
134 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) {
135 foreach ($saved_prefkeys as $key) {
136 $saved_array[$key][$saved_num] = asearch_getPref($data_dir, $username, $key, $saved_num);
137 }
138 }
139 return $saved_array;
140}
141
142/* save a recent search */
143function asearch_save_recent($data_dir, $username, $recent_index)
144{
145 global $recent_prefkeys, $saved_prefkeys;
146
147 $saved_array = asearch_read_saved($data_dir, $username);
148 $saved_index = count($saved_array[$saved_prefkeys[0]]);
149 $recent_array = asearch_read_recent($data_dir, $username);
150 $n = 0;
151 foreach ($recent_prefkeys as $key) {
152 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
153 if (!empty($recent_slice[0]))
154 asearch_setPref($data_dir, $username, $saved_prefkeys[$n], $saved_index, $recent_slice[0]);
155 else
156 asearch_removePref($data_dir, $username, $saved_prefkeys[$n], $saved_index);
157 $n++;
158 }
159}
160
161function asearch_write_recent($data_dir, $username, $recent_array)
162{
163 global $recent_prefkeys;
164
165 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
166 $recent_count = min($recent_max, count($recent_array[$recent_prefkeys[0]]));
167 for ($recent_num=0; $recent_num < $recent_count; $recent_num++) {
168 foreach ($recent_prefkeys as $key) {
169 asearch_setPref($data_dir, $username, $key, $recent_num, $recent_array[$key][$recent_num]);
170 }
171 }
172 for (; $recent_num < $recent_max; $recent_num++) {
173 foreach ($recent_prefkeys as $key) {
174 asearch_removePref($data_dir, $username, $key, $recent_num);
175 }
176 }
177}
178
179/* forget a recent search */
180function asearch_forget_recent($data_dir, $username, $forget_index)
181{
182 global $recent_prefkeys;
183
184 $recent_array = asearch_read_recent($data_dir, $username);
185 foreach ($recent_prefkeys as $key) {
186 array_splice($recent_array[$key], $forget_index, 1);
187 }
188 asearch_write_recent($data_dir, $username, $recent_array);
189}
190
191function asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
192{
193 global $recent_prefkeys;
194
195 $mailbox_string = asearch_serialize($mailbox_array);
196 $biop_string = asearch_serialize($biop_array);
197 $unop_string = asearch_serialize($unop_array);
198 $where_string = asearch_serialize($where_array);
199 $what_string = asearch_serialize($what_array);
200 $exclude_string = asearch_serialize($exclude_array);
201 $recent_count = count($recent_array[$recent_prefkeys[0]]);
202 for ($recent_num=0; $recent_num<$recent_count; $recent_num++) {
203 if (isset($recent_array[$recent_prefkeys[0]][$recent_num])) {
204 if (
205 $mailbox_string == $recent_array['asearch_recent_mailbox'][$recent_num] &&
206 $biop_string == $recent_array['asearch_recent_biop'][$recent_num] &&
207 $unop_string == $recent_array['asearch_recent_unop'][$recent_num] &&
208 $where_string == $recent_array['asearch_recent_where'][$recent_num] &&
209 $what_string == $recent_array['asearch_recent_what'][$recent_num] &&
210 $exclude_string == $recent_array['asearch_recent_exclude'][$recent_num]
211 )
212 return TRUE;
213 }
214 }
215 return FALSE;
216}
217
218/* push a recent search */
219function asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
220{
221 global $recent_prefkeys;
222
223 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
224 if ($recent_max > 0) {
225 $recent_array = asearch_read_recent($data_dir, $username);
226 if (!asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)) {
227 $input = array($where_array, $mailbox_array, $what_array, $biop_array, $unop_array, $exclude_array);
228 $i = 0;
229 foreach ($recent_prefkeys as $key) {
230 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
231 $i++;
232 }
233 asearch_write_recent($data_dir, $username, $recent_array);
234 }
235 }
236}
237
238/* edit a recent search */
239function asearch_edit_recent($data_dir, $username, $index)
240{
241 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
242
243 $mailbox_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_mailbox', $index));
244 $biop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_biop', $index));
245 $unop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_unop', $index));
246 $where_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_where', $index));
247 $what_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_what', $index));
248 $exclude_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_exclude', $index));
249}
250
251/* edit the last recent search if the prefs permit it */
252function asearch_edit_last($data_dir, $username)
253{
254 if (getPref($data_dir, $username, 'search_memory', 0) > 0)
255 asearch_edit_recent($data_dir, $username, 0);
256}
257
258/* edit a saved search */
259function asearch_edit_saved($data_dir, $username, $index)
260{
261 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
262
263 $mailbox_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_mailbox', $index));
264 $biop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_biop', $index));
265 $unop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_unop', $index));
266 $where_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_where', $index));
267 $what_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_what', $index));
268 $exclude_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_exclude', $index));
269}
270
271function asearch_write_saved($data_dir, $username, $saved_array)
272{
273 global $saved_prefkeys;
274
275 $saved_count = count($saved_array[$saved_prefkeys[0]]);
276 for ($saved_num=0; $saved_num < $saved_count; $saved_num++) {
277 foreach ($saved_prefkeys as $key) {
278 asearch_setPref($data_dir, $username, $key, $saved_num, $saved_array[$key][$saved_num]);
279 }
280 }
281 foreach ($saved_prefkeys as $key) {
282 asearch_removePref($data_dir, $username, $key, $saved_count);
283 }
284}
285
286/* delete a saved search */
287function asearch_delete_saved($data_dir, $username, $saved_index)
288{
289 global $saved_prefkeys;
290
291 $saved_array = asearch_read_saved($data_dir, $username);
292 $asearch_keys = $saved_prefkeys;
293 foreach ($asearch_keys as $key) {
294 array_splice($saved_array[$key], $saved_index, 1);
295 }
296 asearch_write_saved($data_dir, $username, $saved_array);
297}
298
299/* translate the input date to imap date to local date display, so the user can know if the date is wrong or illegal */
300function asearch_get_date_display($what)
301{
302 $what_parts = sqimap_asearch_parse_date($what);
303 if (count($what_parts) == 4) {
304 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3])) {
305 $what_display = date_intl(_("M j, Y"),mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
306 /*$what_display = $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];*/
307 }
308 else
309 $what_display = _("(Illegal date)");
310 }
311 else
312 $what_display = _("(Wrong date)");
313 return $what_display;
314}
315
316/* translate the query to rough natural display */
05f5b952 317function asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
6b37252b 318{
319 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
320 global $imap_asearch_opcodes;
321
322 $last_mailbox = $mailbox_array[0];
323 if (empty($last_mailbox))
324 $last_mailbox = 'INBOX';
05f5b952 325 $query_display = '';
326 for ($crit_num=0; $crit_num < count($where_array); $crit_num++) {
327 if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) {
328 $cur_mailbox = $mailbox_array[$crit_num];
6b37252b 329 if (empty($cur_mailbox))
330 $cur_mailbox = 'INBOX';
05f5b952 331 $biop = asearch_nz($biop_array[$crit_num]);
332 if (($query_display == '') || ($cur_mailbox != $last_mailbox)) {
6b37252b 333 $mailbox_display = ' <B>' . asearch_get_mailbox_display($cur_mailbox) . '</B>';
05f5b952 334 if ($query_display == '')
6b37252b 335 $biop_display = _("In");
336 else
337 $biop_display = $imap_asearch_biops_in[$biop];
338 $last_mailbox = $cur_mailbox;
339 }
340 else {
341 $mailbox_display = '';
342 $biop_display = $imap_asearch_biops[$biop];
343 }
344 $biop_display = ' <U><I>' . $biop_display . '</I></U>';
05f5b952 345 $unop = $unop_array[$crit_num];
6b37252b 346 $unop_display = $imap_asearch_unops[$unop];
05f5b952 347 $where = $where_array[$crit_num];
6b37252b 348 $where_display = $imap_asearch_options[$where];
349 if ($unop_display != '')
350 $where_display = ' <U><I>' . $unop_display . ' ' . $where_display . '</I></U>';
351 else
352 $where_display = ' <U><I>' . $where_display . '</I></U>';
353 $what_type = $imap_asearch_opcodes[$where];
05f5b952 354 $what = $what_array[$crit_num];
355 if ($what_type) { /* Check opcode parameter */
356 if ($what == '')
357 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
6b37252b 358 else {
05f5b952 359 if ($what_type == 'adate')
360 $what_display = asearch_get_date_display($what);
6b37252b 361 else
b2e7eb53 362 $what_display = htmlentities($what);
05f5b952 363 $what_display = ' <B>' . $what_display . '</B>';
6b37252b 364 }
6b37252b 365 }
05f5b952 366 else {
367 if ($what)
368 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
369 else
370 $what_display = '';
371 }
372 $query_display .= ' ' . $biop_display . $mailbox_display . $where_display . $what_display;
6b37252b 373 }
374 }
05f5b952 375 return $query_display;
6b37252b 376}
377
378/* Handle the alternate row colors */
379function asearch_get_row_color($color, $row_num)
380{
381/*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
382 $color_string = $color[4];
383 if ($GLOBALS['alt_index_colors']) {
384 if (($row_num % 2) == 0) {
385 if (!isset($color[12]))
386 $color[12] = '#EAEAEA';
387 $color_string = $color[12];
388 }
389 }
390 return $color_string;
391}
392
393/* Print a whole query array, recent or saved */
394function asearch_print_query_array($query_array, $query_keys, $action_array, $title)
395{
396 global $color;
397
398 echo "<br>\n";
920ec5ec 399 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
400 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=5'));
6b37252b 401 $main_key = $query_keys[0];
402 $query_count = count($query_array[$main_key]);
403 for ($query_num=0, $row_num=0; $query_num<$query_count; $query_num++) {
404 if (!empty($query_array[$main_key][$query_num])) {
405 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
406
407 unset($search_array);
408 foreach ($query_keys as $query_key) {
409 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
410 }
411 $mailbox_array = $search_array[1];
412 $biop_array = $search_array[3];
413 $unop_array = $search_array[4];
414 $where_array = $search_array[0];
415 $what_array = $search_array[2];
416 $exclude_array = $search_array[5];
05f5b952 417 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
6b37252b 418
920ec5ec 419 echo html_tag('td', $query_num+1, 'right');
420 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
6b37252b 421 foreach ($action_array as $action => $action_display) {
422 echo html_tag('td', '<a href=search.php?submit=' . $action . '&amp;rownum=' . $query_num . '>' . $action_display . '</a>', 'center');
423 }
424
425 echo '</tr>' . "\n";
426 $row_num++;
427 }
428 }
429 echo '</table>' . "\n";
430}
431
432/* print the saved array */
433function asearch_print_saved($data_dir, $username)
434{
435 global $saved_prefkeys;
436
437 $saved_array = asearch_read_saved($data_dir, $username);
05f5b952 438 if (isset($saved_array[$saved_prefkeys[0]])) {
439 $saved_count = count($saved_array[$saved_prefkeys[0]]);
440 if ($saved_count > 0) {
441 $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
442 asearch_print_query_array($saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"));
443 }
6b37252b 444 }
70c4fd84 445}
6c8388a9 446
6b37252b 447/* print the recent array */
448function asearch_print_recent($data_dir, $username)
449{
450 global $recent_prefkeys;
451
452 $recent_array = asearch_read_recent($data_dir, $username);
05f5b952 453 if (isset($recent_array[$recent_prefkeys[0]])) {
454 $recent_count = count($recent_array[$recent_prefkeys[0]]);
455 $recent_max = min($recent_count, getPref($data_dir, $username, 'search_memory', 0));
456 if ($recent_max > 0) {
457 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
458 asearch_print_query_array($recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"));
459 }
6b37252b 460 }
56e0b3b7 461}
462
05f5b952 463/* build an <option> statement */
920ec5ec 464function asearch_opt($val, $sel, $tit)
6b37252b 465{
920ec5ec 466 return '<option value="' . $val . '"' . ($sel == $val ? ' selected' : '') . '>' . $tit . '</option>' . "\n";
6b37252b 467}
468
05f5b952 469/* build a <select> statement from an array */
6b37252b 470function asearch_opt_array($var_name, $opt_array, $cur_val)
471{
472 $output = '<select name="' . $var_name . '">' . "\n";
473 foreach($opt_array as $val => $display)
474 $output .= asearch_opt($val, $cur_val, $display);
475 $output .= '</select>' . "\n";
476 return $output;
477}
478
920ec5ec 479function asearch_mailbox_exists($mailbox, $boxes)
480{
481 foreach ($boxes as $box) {
482 if ($box['unformatted'] == $mailbox)
483 return TRUE;
484 }
485 return FALSE;
486}
487
6b37252b 488/* print one form row */
05f5b952 489function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num)
6b37252b 490{
491 global $imap_asearch_biops_in, $imap_asearch_unops, $imap_asearch_options;
492 global $color;
493
920ec5ec 494 echo html_tag('tr', '', '', $color[4]);
6b37252b 495
920ec5ec 496 echo html_tag('td', '', 'center');
6b37252b 497/* Binary operator */
05f5b952 498 if ($row_num)
499 echo asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop);
6b37252b 500 else
501 echo /*'<input type="hidden" name="biop[0]" value="">' .*/ '<b>' . _("In") . '</b>';
502 echo "</td>\n";
503
920ec5ec 504 echo html_tag('td', '', 'center');
6b37252b 505/* Mailbox list */
920ec5ec 506 echo '<select name="mailbox[' . $row_num . ']">';
507 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes)))
508 echo asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
509 echo asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']');
6b37252b 510 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes);
511 echo '</select></td>' . "\n";
512
513/* Unary operator and Search location */
514 if (empty($where))
515 $where = 'FROM';
920ec5ec 516 echo html_tag('td',
05f5b952 517 asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
518 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
6b37252b 519 'center');
520
521/* Text input */
522/* This is the original stuff. Except it doesn't work (eg commas are lost), why so much trouble?
523 $what_disp = str_replace(',', ' ', $what);
524 $what_disp = str_replace('\\\\', '\\', $what_disp);
525 $what_disp = str_replace('\\"', '"', $what_disp);
526 $what_disp = str_replace('"', '&quot;', $what_disp);*/
b2e7eb53 527 $what_disp = htmlspecialchars($what);
920ec5ec 528 echo html_tag('td', '<input type="text" size="35" name="what[' . $row_num . ']" value="' . $what_disp . '">', 'center') . "\n";
6b37252b 529
530/* Exclude criteria */
920ec5ec 531 echo html_tag('td',
05f5b952 532 _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ? ' CHECKED' : '') . '>', 'center', '') . "\n";
6b37252b 533
534 echo "</tr>\n";
535}
536
537/* print the search form */
538function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $add_criteria)
539{
540 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
541 global $color;
542
543 /* Search Form */
5a383256 544 echo "<br>\n";
6b37252b 545 echo '<form action="search.php" name="form_asearch">' . "\n";
546
920ec5ec 547 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
548 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
6b37252b 549 $row_count = count($where_array) + $add_criteria;
550 $mailbox = '';
551 for ($row_num = 0; $row_num < $row_count; $row_num++) {
552 /* Keep the last non-empty mailbox as default choice */
852b59d2 553 if (strip_tags(asearch_nz($mailbox_array[$row_num])) != '')
6b37252b 554 $mailbox = strip_tags($mailbox_array[$row_num]);
555 if ($mailbox == '')
556 $mailbox = $boxes[0]['unformatted'];
557 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
558 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
b2e7eb53 559 $where = asearch_nz($where_array[$row_num]);
6b37252b 560 $what = asearch_nz($what_array[$row_num]);
561 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
562 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num);
563 }
564 echo '</table>' . "\n";
565
566/* Submit buttons */
920ec5ec 567 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
6b37252b 568 echo html_tag('tr',
569 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
570 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
571 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
572 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
573 );
574 echo '</table>' . "\n";
575 echo '</form>' . "\n";
576}
577
578/* print the $msgs messages from $mailbox mailbox */
579/* this is almost the original code */
580function asearch_print_mailbox_msgs($msgs, $mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false)
581{
582 global $sort, $color;
59a623e6 583
cf6cb8b1 584 if ($cnt > 0) {
6b37252b 585 $msort = calc_msort($msgs, $sort);
586 $showbox = asearch_get_mailbox_display($mailbox);
920ec5ec 587 echo html_tag('div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
70f1b6b5 588
6b37252b 589 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
590 $toggle_all = get_selectall_link(1, $sort);
70f1b6b5 591
6b37252b 592 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
0fd2f513 593
6b37252b 594 echo '<tr><td>';
595 mail_message_listing_beginning($imapConnection, $mailbox, $sort, $msg_cnt_str, $toggle_all, 1);
596 echo '</td></tr>';
70f1b6b5 597
6b37252b 598 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
70f1b6b5 599
6b37252b 600 echo '<tr><td>';
601 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
602 echo ' <tr><td>';
70f1b6b5 603
6b37252b 604 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
605 echo ' <tr><td>';
606 printHeader($mailbox, 6, $color, false);
607 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $sort, $color, $cnt, $where, $what);
608 echo ' </td></tr>';
609 echo ' </table>';
610 echo ' </td></tr>';
611 echo ' </table>';
612 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
613 echo '</td></tr>';
70f1b6b5 614
6b37252b 615 echo '</table>';
616 }
59a623e6 617}
618
0d672ac0 619/* ------------------------ main ------------------------ */
6b37252b 620global $allow_thread_sort;
0d672ac0 621
6b37252b 622/* get globals we may need */
6b37252b 623sqgetGlobalVar('username', $username, SQ_SESSION);
624sqgetGlobalVar('key', $key, SQ_COOKIE);
bfc5b989 625sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); /* we really need this? */
e5cbf2f9 626sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); /* do we really need this? */
c1cb7ba4 627
6b37252b 628$recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude');
629$saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude');
630/*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude');*/
631
632$search_button_html = _("Search");
633$search_button_text = asearch_unhtmlentities($search_button_html);
634$add_criteria_button_html = _("Add New Criteria");
635$add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
34a85c56 636$del_excluded_button_html = _("Remove Excluded Criteria");
6b37252b 637$del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
34a85c56 638$del_all_button_html = _("Remove All Criteria");
6b37252b 639$del_all_button_text = asearch_unhtmlentities($del_all_button_html);
640
641$imap_asearch_options = array(
642/* <message set>, */
643/*'ALL' is binary operator */
644 'ANSWERED' => _("Answered"),
645 'BCC' => _("Bcc"),
646 'BEFORE' => _("Before"),
647 'BODY' => _("Message Body"),
648 'CC' => _("CC"),
649 'DELETED' => _("Deleted"),
650 'DRAFT' => _("Draft"),
651 'FLAGGED' => _("Flagged"),
652 'FROM' => _("Sent By"),
653 'HEADER' => _("Header Field"),
654 'KEYWORD' => _("Keyword"),
655 'LARGER' => _("Larger Than"),
656 'NEW' => _("New"),
657/*'NOT' is unary operator */
658 'OLD' => _("Old"),
659 'ON' => _("On"),
660/*'OR' is binary operator */
661 'RECENT' => _("Recent"),
662 'SEEN' => _("Seen"),
663 'SENTBEFORE' => _("Sent Before"),
664 'SENTON' => _("Sent On"),
665 'SENTSINCE' => _("Sent Since"),
666 'SINCE' => _("Since"),
667 'SMALLER' => _("Smaller Than"),
668 'SUBJECT' => _("Subject Contains"),
669 'TEXT' => _("Header and Body"),
670 'TO' => _("Sent To"),
671/*'UID' => 'anum',*/
672/*'UNANSWERED' => '',
673 'UNDELETED' => '',
674 'UNDRAFT' => '',
675 'UNFLAGGED' => '',
676 'UNKEYWORD' => _("Unkeyword"),
677 'UNSEEN' => _("Unseen"),*/
678);
679uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
680
681$imap_asearch_unops = array(
682 '' => '',
683 'NOT' => _("Not")
684);
685
686$imap_asearch_biops_in = array(
687 'ALL' => _("And In"),
688 'OR' => _("Or In")
689);
690
691$imap_asearch_biops = array(
692 'ALL' => _("And"),
693 'OR' => _("Or")
694);
695
696/*
697 unset : Enter key, or called from outside (eg read_body)
698 $search_button_text : Search button
699 'Search_no_update' : Search but don't update recent
700 'Search_last' : Same as no_update but reload and search last
701 'Search_silent' : Same as no_update but only display results
702 $add_criteria_button_text : Add New Criteria button
34a85c56 703 $del_excluded_button_text : Remove Excluded Criteria button
704 $del_all_button_text : Remove All Criteria button
6b37252b 705 'save_recent'
706 'search_recent'
707 'forget_recent'
708 'edit_saved'
709 'search_saved'
710 'delete_saved'
711*/
712if (isset($_GET['submit']))
713 $submit = strip_tags($_GET['submit']);
714
715/* Used by search */
716if (isset($_GET['mailbox'])) {
717 $mailbox_array = $_GET['mailbox'];
718 if (!is_array($mailbox_array))
719 $mailbox_array = array($mailbox_array);
56e0b3b7 720}
6b37252b 721else
722 $mailbox_array = array();
723
724if (isset($_GET['biop'])) {
725 $biop_array = $_GET['biop'];
726 if (!is_array($biop_array))
727 $biop_array = array($biop_array);
c1cb7ba4 728}
6b37252b 729else
730 $biop_array = array();
c1cb7ba4 731
6b37252b 732if (isset($_GET['unop'])) {
733 $unop_array = $_GET['unop'];
734 if (!is_array($unop_array))
735 $unop_array = array($unop_array);
548a552a 736}
6b37252b 737else
738 $unop_array = array();
739
740if (isset($_GET['where'])) {
741 $where_array = $_GET['where'];
742 if (!is_array($where_array))
743 $where_array = array($where_array);
744}
745else
746 $where_array = array();
747
748if (isset($_GET['what'])) {
749 $what_array = $_GET['what'];
750 if (!is_array($what_array))
751 $what_array = array($what_array);
752}
753else
754 $what_array = array();
755
756if (isset($_GET['exclude']))
757 $exclude_array = $_GET['exclude'];
758else
759 $exclude_array = array();
760
761/* Used by recent and saved stuff */
762if (isset($_GET['rownum'])) {
763 $submit_rownum = strip_tags($_GET['rownum']);
56e0b3b7 764}
6b37252b 765
766/* end of get globals */
767
768$search_silent = FALSE; /* Default is normal behaviour */
769
770/* See how the page was called and fire off correct function */
771if ((!isset($submit) || empty($submit)) && !empty($where_array)) {
772 /* This happens when the Enter key is used or called from outside */
773 $submit = $search_button_text;
774 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
775 asearch_edit_last($data_dir, $username);
56e0b3b7 776}
6b37252b 777
778if (!isset($submit)) {
779 $submit = '';
56e0b3b7 780}
6b37252b 781else {
782 switch ($submit) {
783 case $search_button_text:
05f5b952 784 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
6b37252b 785 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
786 break;
787 case 'Search_silent':
788 $search_silent = TRUE;
789 /*nobreak;*/
790 case 'Search_no_update':
791 $submit = $search_button_text;
792 break;
793 case $del_excluded_button_text:
794 $delarray = array_keys($exclude_array);
795 while (!empty($delarray)) {
796 $delrow = array_pop($delarray);
797 array_splice($mailbox_array, $delrow, 1);
798 array_splice($biop_array, $delrow, 1);
799 array_splice($unop_array, $delrow, 1);
800 array_splice($where_array, $delrow, 1);
801 array_splice($what_array, $delrow, 1);
802/* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
803 }
804 $exclude_array = array();
805 break;
806 case $del_all_button_text:
807 $mailbox_array = array();
808 $biop_array = array();
809 $unop_array = array();
810 $where_array = array();
811 $what_array = array();
812 $exclude_array = array();
813 break;
814 case 'save_recent':
815 asearch_save_recent($data_dir, $username, $submit_rownum);
816 break;
817 case 'search_recent':
818 $submit = $search_button_text;
819 /*nobreak;*/
820 case 'edit_recent': /* no link to do this, yet */
821 asearch_edit_recent($data_dir, $username, $submit_rownum);
822 break;
823 case 'forget_recent':
824 asearch_forget_recent($data_dir, $username, $submit_rownum);
825 break;
826 case 'search_saved':
827 $submit = $search_button_text;
828 /*nobreak;*/
829 case 'edit_saved':
830 asearch_edit_saved($data_dir, $username, $submit_rownum);
831 break;
832 case 'delete_saved':
833 asearch_delete_saved($data_dir, $username, $submit_rownum);
834 break;
835 }
56e0b3b7 836}
23a9084b 837
6b37252b 838/* open IMAP connection */
839$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
840/* get mailbox names once here */
841$boxes = sqimap_mailbox_list($imapConnection);
99f3175e 842/* ensure we have a valid default mailbox name */
843$mailbox = strip_tags(asearch_nz($mailbox_array[0]));
844if (($mailbox == '') || ($mailbox == 'None')) { //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
6b37252b 845 $mailbox = $boxes[0]['unformatted'];
99f3175e 846 $mailbox_array[0] = $mailbox;
847}
848
6b37252b 849if (isset($composenew) && $composenew) {
850 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
851 "&amp;session=$composesession&amp;attachedmessages=true&amp";
852 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
56e0b3b7 853}
6b37252b 854else
855 displayPageHeader($color, $mailbox);
184ef883 856
6b37252b 857do_hook('search_before_form');
d81e351b 858
6b37252b 859if (!$search_silent) {
920ec5ec 860 echo html_tag('table',
861 html_tag('tr', "\n" .
862 html_tag('td', asearch_get_title_display($color, _("Search")), 'center', $color[0])
6b37252b 863 ) ,
864 '', '', 'width="100%"') . "\n";
865 asearch_print_saved($data_dir, $username);
866 asearch_print_recent($data_dir, $username);
867 if (($submit == $add_criteria_button_text) || (empty($where_array)))
868 $new_row = 1;
869 else
870 $new_row = 0;
871 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $new_row);
872}
873
874/* This deserves a comment, at least. What is it for exactly? */
d81e351b 875if (isset($newsort)) {
876 $sort = $newsort;
9837b0a5 877 sqsession_register($sort, 'sort');
d81e351b 878}
879
880/*********************************************************************
881 * Check to see if we can use cache or not. Currently the only time *
882 * when you will not use it is when a link on the left hand frame is *
883 * used. Also check to make sure we actually have the array in the *
884 * registered session data. :) *
885 *********************************************************************/
886if (! isset($use_mailbox_cache)) {
887 $use_mailbox_cache = 0;
888}
889
890/* There is a problem with registered vars in 4.1 */
891/*
920ec5ec 892if(substr(phpversion(), 0, 3) == '4.1') {
d81e351b 893 $use_mailbox_cache = FALSE;
894}
895*/
896
88cb1b4d 897do_hook('search_after_form');
56e0b3b7 898
6b37252b 899if ($submit == $search_button_text) {
920ec5ec 900 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
901 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
902 echo html_tag('tr', html_tag('td', asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array), 'center', $color[4]));
05f5b952 903 echo '</table><br>' . "\n";
904
905 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
6b37252b 906 if ($query_error != '')
920ec5ec 907 echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
6b37252b 908 else {
6b37252b 909 $old_allow_thread_sort = 0;
910 if ($allow_thread_sort == TRUE) {
911 $old_allow_thread_sort = $allow_thread_sort;
912 $allow_thread_sort = FALSE;
913 }
914
915 $boxcount = count($boxes);
916 for ($boxnum=0; $boxnum<$boxcount; $boxnum++) {
917 if (!in_array('noselect', $boxes[$boxnum]['flags']))
918 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
919 }
920
921 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array);
922 if (empty($mboxes_msgs))
920ec5ec 923 echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
6b37252b 924 else {
925 foreach($mboxes_msgs as $mailbox => $msgs) {
926 sqimap_mailbox_select($imapConnection, $mailbox);
927 $msgs = fillMessageArray($imapConnection, $msgs, count($msgs));
928/* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
929/* $where = asearch_serialize($where_array);
930 $what = asearch_serialize($what_array);*/
931 $where = $where_array[0];
932 $what = $what_array[0];
933 asearch_print_mailbox_msgs($msgs, $mailbox, count($msgs), $imapConnection, urlencode($where), urlencode($what), false, false);
934 }
935 }
936
937 $allow_thread_sort = $old_allow_thread_sort;
59a623e6 938 }
56e0b3b7 939}
940
d81e351b 941
70c4fd84 942do_hook('search_bottom');
6b37252b 943sqimap_logout($imapConnection);
29eb5486 944echo '</body></html>';
99d2a184 945
91954f9e 946?>