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