6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Originally by Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
12 /* Path for SquirrelMail required files. */
13 define('SM_PATH','../');
15 /* SquirrelMail required files. */
16 require_once(SM_PATH
. 'include/validate.php');
17 require_once(SM_PATH
. 'functions/strings.php');
18 require_once(SM_PATH
. 'functions/imap_asearch.php');
19 require_once(SM_PATH
. 'functions/imap_mailbox.php');
20 require_once(SM_PATH
. 'functions/imap_messages.php');
21 require_once(SM_PATH
. 'functions/mailbox_display.php'); //getButton()...
23 function asearch_unhtml_strcoll($a, $b)
25 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
28 function imap_get_mailbox_display($mailbox)
30 if (strtoupper($mailbox) == 'INBOX')
32 return imap_utf7_decode_local($mailbox);
35 function asearch_get_mailbox_display($mailbox)
37 if ($mailbox == 'All Folders')
38 return _("All Folders");
39 return imap_get_mailbox_display($mailbox);
42 function asearch_get_title_display($color, $txt)
44 return '<b><big>' . $txt . '</big></b>';
47 function asearch_get_error_display($color, $txt)
49 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
50 /*return '<b><big>' . $txt . '</big></b>';*/
53 function asearch_serialize($input_array)
55 /*return $input_array[0];*/
56 return serialize($input_array);
59 function asearch_unserialize($input_string)
61 /*return array($input_string);*/
62 return unserialize($input_string);
65 function asearch_getPref($data_dir, $username, $key, $index, $default = '')
67 return getPref($data_dir, $username, $key . $index, $default);
70 function asearch_setPref($data_dir, $username, $key, $index, $value)
72 return setPref($data_dir, $username, $key . $index, $value);
75 function asearch_removePref($data_dir, $username, $key, $index)
77 return removePref($data_dir, $username, $key . $index);
80 /* sanity checks, done before running the imap command and before push_recent */
81 function asearch_check_query($where_array, $what_array, $exclude_array)
83 global $imap_asearch_opcodes;
85 if (empty($where_array))
86 return _("Please enter something to search for");
87 if (count($exclude_array) == count($where_array))
88 return _("There must be at least one criteria to search for");
89 for ($crit_num = 0; $crit_num < count($where_array); $crit_num++
) {
90 $where = $where_array[$crit_num];
91 $what = $what_array[$crit_num];
92 if (!(($what == '') ^
($imap_asearch_opcodes[$where] != '')))
93 return _("Error in criteria argument");
98 /* read the recent searches */
99 function asearch_read_recent($data_dir, $username)
101 global $recent_prefkeys;
103 $recent_array = array();
104 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
105 for ($recent_num = 0; $recent_num < $recent_max; $recent_num++
) {
106 foreach ($recent_prefkeys as $prefkey) {
107 $pref = asearch_getPref($data_dir, $username, $prefkey, $recent_num);
108 /* if (!empty($pref))*/
109 $recent_array[$prefkey][$recent_num] = $pref;
111 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
112 foreach ($recent_prefkeys as $key) {
113 array_pop($recent_array[$key]);
118 return $recent_array;
121 /* get the saved searches */
122 function asearch_read_saved($data_dir, $username)
124 global $saved_prefkeys;
126 $saved_array = array();
127 $saved_key = $saved_prefkeys[0];
128 for ($saved_count = 0; ; $saved_count++
) {
129 $pref = asearch_getPref($data_dir, $username, $saved_key, $saved_count);
133 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++
) {
134 foreach ($saved_prefkeys as $key) {
135 $saved_array[$key][$saved_num] = asearch_getPref($data_dir, $username, $key, $saved_num);
141 /* save a recent search */
142 function asearch_save_recent($data_dir, $username, $recent_index)
144 global $recent_prefkeys, $saved_prefkeys;
146 $saved_array = asearch_read_saved($data_dir, $username);
147 $saved_index = count($saved_array[$saved_prefkeys[0]]);
148 $recent_array = asearch_read_recent($data_dir, $username);
150 foreach ($recent_prefkeys as $key) {
151 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
152 if (!empty($recent_slice[0]))
153 asearch_setPref($data_dir, $username, $saved_prefkeys[$n], $saved_index, $recent_slice[0]);
155 asearch_removePref($data_dir, $username, $saved_prefkeys[$n], $saved_index);
160 function asearch_write_recent($data_dir, $username, $recent_array)
162 global $recent_prefkeys;
164 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
165 $recent_count = min($recent_max, count($recent_array[$recent_prefkeys[0]]));
166 for ($recent_num=0; $recent_num < $recent_count; $recent_num++
) {
167 foreach ($recent_prefkeys as $key) {
168 asearch_setPref($data_dir, $username, $key, $recent_num, $recent_array[$key][$recent_num]);
171 for (; $recent_num < $recent_max; $recent_num++
) {
172 foreach ($recent_prefkeys as $key) {
173 asearch_removePref($data_dir, $username, $key, $recent_num);
178 /* forget a recent search */
179 function asearch_forget_recent($data_dir, $username, $forget_index)
181 global $recent_prefkeys;
183 $recent_array = asearch_read_recent($data_dir, $username);
184 foreach ($recent_prefkeys as $key) {
185 array_splice($recent_array[$key], $forget_index, 1);
187 asearch_write_recent($data_dir, $username, $recent_array);
190 function asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
192 global $recent_prefkeys;
194 $mailbox_string = asearch_serialize($mailbox_array);
195 $biop_string = asearch_serialize($biop_array);
196 $unop_string = asearch_serialize($unop_array);
197 $where_string = asearch_serialize($where_array);
198 $what_string = asearch_serialize($what_array);
199 $exclude_string = asearch_serialize($exclude_array);
200 $recent_count = count($recent_array[$recent_prefkeys[0]]);
201 for ($recent_num=0; $recent_num<$recent_count; $recent_num++
) {
202 if (isset($recent_array[$recent_prefkeys[0]][$recent_num])) {
204 $mailbox_string == $recent_array['asearch_recent_mailbox'][$recent_num] &&
205 $biop_string == $recent_array['asearch_recent_biop'][$recent_num] &&
206 $unop_string == $recent_array['asearch_recent_unop'][$recent_num] &&
207 $where_string == $recent_array['asearch_recent_where'][$recent_num] &&
208 $what_string == $recent_array['asearch_recent_what'][$recent_num] &&
209 $exclude_string == $recent_array['asearch_recent_exclude'][$recent_num]
217 /* push a recent search */
218 function asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
220 global $recent_prefkeys;
222 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
223 if ($recent_max > 0) {
224 $recent_array = asearch_read_recent($data_dir, $username);
225 if (!asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)) {
226 $input = array($where_array, $mailbox_array, $what_array, $biop_array, $unop_array, $exclude_array);
228 foreach ($recent_prefkeys as $key) {
229 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
232 asearch_write_recent($data_dir, $username, $recent_array);
237 /* edit a recent search */
238 function asearch_edit_recent($data_dir, $username, $index)
240 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
242 $mailbox_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_mailbox', $index));
243 $biop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_biop', $index));
244 $unop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_unop', $index));
245 $where_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_where', $index));
246 $what_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_what', $index));
247 $exclude_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_exclude', $index));
250 /* edit the last recent search if the prefs permit it */
251 function asearch_edit_last($data_dir, $username)
253 if (getPref($data_dir, $username, 'search_memory', 0) > 0)
254 asearch_edit_recent($data_dir, $username, 0);
257 /* edit a saved search */
258 function asearch_edit_saved($data_dir, $username, $index)
260 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
262 $mailbox_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_mailbox', $index));
263 $biop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_biop', $index));
264 $unop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_unop', $index));
265 $where_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_where', $index));
266 $what_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_what', $index));
267 $exclude_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_exclude', $index));
270 function asearch_write_saved($data_dir, $username, $saved_array)
272 global $saved_prefkeys;
274 $saved_count = count($saved_array[$saved_prefkeys[0]]);
275 for ($saved_num=0; $saved_num < $saved_count; $saved_num++
) {
276 foreach ($saved_prefkeys as $key) {
277 asearch_setPref($data_dir, $username, $key, $saved_num, $saved_array[$key][$saved_num]);
280 foreach ($saved_prefkeys as $key) {
281 asearch_removePref($data_dir, $username, $key, $saved_count);
285 /* delete a saved search */
286 function asearch_delete_saved($data_dir, $username, $saved_index)
288 global $saved_prefkeys;
290 $saved_array = asearch_read_saved($data_dir, $username);
291 $asearch_keys = $saved_prefkeys;
292 foreach ($asearch_keys as $key) {
293 array_splice($saved_array[$key], $saved_index, 1);
295 asearch_write_saved($data_dir, $username, $saved_array);
298 /* translate the input date to imap date to local date display, so the user can know if the date is wrong or illegal */
299 function asearch_get_date_display($what)
301 $what_parts = sqimap_asearch_parse_date($what);
302 if (count($what_parts) == 4) {
303 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3])) {
304 $what_display = date_intl(_("M j, Y"),mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
305 /*$what_display = $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];*/
308 $what_display = _("(Illegal date)");
311 $what_display = _("(Wrong date)");
312 return $what_display;
315 /* translate the query to rough natural display */
316 function asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
318 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
319 global $imap_asearch_opcodes;
321 $last_mailbox = $mailbox_array[0];
322 if (empty($last_mailbox))
323 $last_mailbox = 'INBOX';
325 for ($crit_num=0; $crit_num < count($where_array); $crit_num++
) {
326 if ((!isset($exclude_array[$crit_num])) ||
(!$exclude_array[$crit_num])) {
327 $cur_mailbox = $mailbox_array[$crit_num];
328 if (empty($cur_mailbox))
329 $cur_mailbox = 'INBOX';
330 $biop = asearch_nz($biop_array[$crit_num]);
331 if (($query_display == '') ||
($cur_mailbox != $last_mailbox)) {
332 $mailbox_display = ' <B>' . asearch_get_mailbox_display($cur_mailbox) . '</B>';
333 if ($query_display == '')
334 $biop_display = _("In");
336 $biop_display = $imap_asearch_biops_in[$biop];
337 $last_mailbox = $cur_mailbox;
340 $mailbox_display = '';
341 $biop_display = $imap_asearch_biops[$biop];
343 $biop_display = ' <U><I>' . $biop_display . '</I></U>';
344 $unop = $unop_array[$crit_num];
345 $unop_display = $imap_asearch_unops[$unop];
346 $where = $where_array[$crit_num];
347 $where_display = $imap_asearch_options[$where];
348 if ($unop_display != '')
349 $where_display = ' <U><I>' . $unop_display . ' ' . $where_display . '</I></U>';
351 $where_display = ' <U><I>' . $where_display . '</I></U>';
352 $what_type = $imap_asearch_opcodes[$where];
353 $what = $what_array[$crit_num];
354 if ($what_type) { /* Check opcode parameter */
356 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
358 if ($what_type == 'adate')
359 $what_display = asearch_get_date_display($what);
361 $what_display = htmlentities($what);
362 $what_display = ' <B>' . $what_display . '</B>';
367 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
371 $query_display .= ' ' . $biop_display . $mailbox_display . $where_display . $what_display;
374 return $query_display;
377 /* Handle the alternate row colors */
378 function asearch_get_row_color($color, $row_num)
380 /*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
381 $color_string = $color[4];
382 if ($GLOBALS['alt_index_colors']) {
383 if (($row_num %
2) == 0) {
384 if (!isset($color[12]))
385 $color[12] = '#EAEAEA';
386 $color_string = $color[12];
389 return $color_string;
392 /* Print a whole query array, recent or saved */
393 function asearch_print_query_array($query_array, $query_keys, $action_array, $title)
398 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
399 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=5'));
400 $main_key = $query_keys[0];
401 $query_count = count($query_array[$main_key]);
402 for ($query_num=0, $row_num=0; $query_num<$query_count; $query_num++
) {
403 if (!empty($query_array[$main_key][$query_num])) {
404 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
406 unset($search_array);
407 foreach ($query_keys as $query_key) {
408 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
410 $mailbox_array = $search_array[1];
411 $biop_array = $search_array[3];
412 $unop_array = $search_array[4];
413 $where_array = $search_array[0];
414 $what_array = $search_array[2];
415 $exclude_array = $search_array[5];
416 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
418 echo html_tag('td', $query_num+
1, 'right');
419 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
420 foreach ($action_array as $action => $action_display) {
421 echo html_tag('td', '<a href=search.php?submit=' . $action . '&rownum=' . $query_num . '>' . $action_display . '</a>', 'center');
428 echo '</table>' . "\n";
431 /* print the saved array */
432 function asearch_print_saved($data_dir, $username)
434 global $saved_prefkeys;
436 $saved_array = asearch_read_saved($data_dir, $username);
437 if (isset($saved_array[$saved_prefkeys[0]])) {
438 $saved_count = count($saved_array[$saved_prefkeys[0]]);
439 if ($saved_count > 0) {
440 $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
441 asearch_print_query_array($saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"));
446 /* print the recent array */
447 function asearch_print_recent($data_dir, $username)
449 global $recent_prefkeys;
451 $recent_array = asearch_read_recent($data_dir, $username);
452 if (isset($recent_array[$recent_prefkeys[0]])) {
453 $recent_count = count($recent_array[$recent_prefkeys[0]]);
454 $recent_max = min($recent_count, getPref($data_dir, $username, 'search_memory', 0));
455 if ($recent_max > 0) {
456 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
457 asearch_print_query_array($recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"));
462 /* build an <option> statement */
463 function asearch_opt($val, $sel, $tit)
465 return '<option value="' . $val . '"' . ($sel == $val ?
' selected' : '') . '>' . $tit . '</option>' . "\n";
468 /* build a <select> statement from an array */
469 function asearch_opt_array($var_name, $opt_array, $cur_val)
471 $output = '<select name="' . $var_name . '">' . "\n";
472 foreach($opt_array as $val => $display)
473 $output .= asearch_opt($val, $cur_val, $display);
474 $output .= '</select>' . "\n";
478 function asearch_mailbox_exists($mailbox, $boxes)
480 foreach ($boxes as $box) {
481 if ($box['unformatted'] == $mailbox)
487 /* print one form row */
488 function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num)
490 global $imap_asearch_biops_in, $imap_asearch_unops, $imap_asearch_options;
493 echo html_tag('tr', '', '', $color[4]);
495 echo html_tag('td', '', 'center');
496 /* Binary operator */
498 echo asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop);
500 echo /*'<input type="hidden" name="biop[0]" value="">' .*/ '<b>' . _("In") . '</b>';
503 echo html_tag('td', '', 'center');
505 echo '<select name="mailbox[' . $row_num . ']">';
506 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes)))
507 echo asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
508 echo asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']');
509 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes);
510 echo '</select></td>' . "\n";
512 /* Unary operator and Search location */
516 asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
517 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
521 /* This is the original stuff. Except it doesn't work (eg commas are lost), why so much trouble?
522 $what_disp = str_replace(',', ' ', $what);
523 $what_disp = str_replace('\\\\', '\\', $what_disp);
524 $what_disp = str_replace('\\"', '"', $what_disp);
525 $what_disp = str_replace('"', '"', $what_disp);*/
526 $what_disp = htmlspecialchars($what);
527 echo html_tag('td', '<input type="text" size="35" name="what[' . $row_num . ']" value="' . $what_disp . '">', 'center') . "\n";
529 /* Exclude criteria */
531 _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ?
' CHECKED' : '') . '>', 'center', '') . "\n";
536 /* print the search form */
537 function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $add_criteria)
539 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
544 echo '<form action="search.php" name="form_asearch">' . "\n";
546 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
547 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
548 $row_count = count($where_array) +
$add_criteria;
550 for ($row_num = 0; $row_num < $row_count; $row_num++
) {
551 /* Keep the last non-empty mailbox as default choice */
552 if (strip_tags(asearch_nz($mailbox_array[$row_num])) != '')
553 $mailbox = strip_tags($mailbox_array[$row_num]);
555 $mailbox = $boxes[0]['unformatted'];
556 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
557 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
558 $where = asearch_nz($where_array[$row_num]);
559 $what = asearch_nz($what_array[$row_num]);
560 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
561 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num);
563 echo '</table>' . "\n";
566 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
568 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
569 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
570 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
571 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
573 echo '</table>' . "\n";
574 echo '</form>' . "\n";
577 /* print the $msgs messages from $mailbox mailbox */
578 /* this is almost the original code */
579 function asearch_print_mailbox_msgs($msgs, $mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false)
581 global $sort, $color;
584 $msort = calc_msort($msgs, $sort);
585 $showbox = asearch_get_mailbox_display($mailbox);
586 echo html_tag('div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
588 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
589 $toggle_all = get_selectall_link(1, $sort);
591 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
594 mail_message_listing_beginning($imapConnection, $mailbox, $sort, $msg_cnt_str, $toggle_all, 1);
597 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
600 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
603 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
605 printHeader($mailbox, 6, $color, false);
606 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $sort, $color, $cnt, $where, $what);
611 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
618 /* ------------------------ main ------------------------ */
619 global $allow_thread_sort;
621 /* get globals we may need */
622 sqgetGlobalVar('username', $username, SQ_SESSION
);
623 sqgetGlobalVar('key', $key, SQ_COOKIE
);
624 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
); /* we really need this? */
625 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
); /* do we really need this? */
627 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude');
628 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude');
629 /*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude');*/
631 $search_button_html = _("Search");
632 $search_button_text = asearch_unhtmlentities($search_button_html);
633 $add_criteria_button_html = _("Add New Criteria");
634 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
635 $del_excluded_button_html = _("Remove Excluded Criteria");
636 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
637 $del_all_button_html = _("Remove All Criteria");
638 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
640 $imap_asearch_options = array(
642 /*'ALL' is binary operator */
643 'ANSWERED' => _("Answered"),
645 'BEFORE' => _("Before"),
646 'BODY' => _("Message Body"),
648 'DELETED' => _("Deleted"),
649 'DRAFT' => _("Draft"),
650 'FLAGGED' => _("Flagged"),
651 'FROM' => _("Sent By"),
652 'HEADER' => _("Header Field"),
653 'KEYWORD' => _("Keyword"),
654 'LARGER' => _("Larger Than"),
656 /*'NOT' is unary operator */
659 /*'OR' is binary operator */
660 'RECENT' => _("Recent"),
662 'SENTBEFORE' => _("Sent Before"),
663 'SENTON' => _("Sent On"),
664 'SENTSINCE' => _("Sent Since"),
665 'SINCE' => _("Since"),
666 'SMALLER' => _("Smaller Than"),
667 'SUBJECT' => _("Subject Contains"),
668 'TEXT' => _("Header and Body"),
669 'TO' => _("Sent To"),
671 /*'UNANSWERED' => '',
675 'UNKEYWORD' => _("Unkeyword"),
676 'UNSEEN' => _("Unseen"),*/
678 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
680 $imap_asearch_unops = array(
685 $imap_asearch_biops_in = array(
686 'ALL' => _("And In"),
690 $imap_asearch_biops = array(
696 unset : Enter key, or called from outside (eg read_body)
697 $search_button_text : Search button
698 'Search_no_update' : Search but don't update recent
699 'Search_last' : Same as no_update but reload and search last
700 'Search_silent' : Same as no_update but only display results
701 $add_criteria_button_text : Add New Criteria button
702 $del_excluded_button_text : Remove Excluded Criteria button
703 $del_all_button_text : Remove All Criteria button
711 if (isset($_GET['submit']))
712 $submit = strip_tags($_GET['submit']);
715 if (isset($_GET['mailbox'])) {
716 $mailbox_array = $_GET['mailbox'];
717 if (!is_array($mailbox_array))
718 $mailbox_array = array($mailbox_array);
721 $mailbox_array = array();
723 if (isset($_GET['biop'])) {
724 $biop_array = $_GET['biop'];
725 if (!is_array($biop_array))
726 $biop_array = array($biop_array);
729 $biop_array = array();
731 if (isset($_GET['unop'])) {
732 $unop_array = $_GET['unop'];
733 if (!is_array($unop_array))
734 $unop_array = array($unop_array);
737 $unop_array = array();
739 if (isset($_GET['where'])) {
740 $where_array = $_GET['where'];
741 if (!is_array($where_array))
742 $where_array = array($where_array);
745 $where_array = array();
747 if (isset($_GET['what'])) {
748 $what_array = $_GET['what'];
749 if (!is_array($what_array))
750 $what_array = array($what_array);
753 $what_array = array();
755 if (isset($_GET['exclude']))
756 $exclude_array = $_GET['exclude'];
758 $exclude_array = array();
760 /* Used by recent and saved stuff */
761 if (isset($_GET['rownum'])) {
762 $submit_rownum = strip_tags($_GET['rownum']);
765 /* end of get globals */
767 $search_silent = FALSE; /* Default is normal behaviour */
769 /* See how the page was called and fire off correct function */
770 if ((!isset($submit) ||
empty($submit)) && !empty($where_array)) {
771 /* This happens when the Enter key is used or called from outside */
772 $submit = $search_button_text;
773 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
774 asearch_edit_last($data_dir, $username);
777 if (!isset($submit)) {
782 case $search_button_text:
783 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
784 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
786 case 'Search_silent':
787 $search_silent = TRUE;
789 case 'Search_no_update':
790 $submit = $search_button_text;
792 case $del_excluded_button_text:
793 $delarray = array_keys($exclude_array);
794 while (!empty($delarray)) {
795 $delrow = array_pop($delarray);
796 array_splice($mailbox_array, $delrow, 1);
797 array_splice($biop_array, $delrow, 1);
798 array_splice($unop_array, $delrow, 1);
799 array_splice($where_array, $delrow, 1);
800 array_splice($what_array, $delrow, 1);
801 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
803 $exclude_array = array();
805 case $del_all_button_text:
806 $mailbox_array = array();
807 $biop_array = array();
808 $unop_array = array();
809 $where_array = array();
810 $what_array = array();
811 $exclude_array = array();
814 asearch_save_recent($data_dir, $username, $submit_rownum);
816 case 'search_recent':
817 $submit = $search_button_text;
819 case 'edit_recent': /* no link to do this, yet */
820 asearch_edit_recent($data_dir, $username, $submit_rownum);
822 case 'forget_recent':
823 asearch_forget_recent($data_dir, $username, $submit_rownum);
826 $submit = $search_button_text;
829 asearch_edit_saved($data_dir, $username, $submit_rownum);
832 asearch_delete_saved($data_dir, $username, $submit_rownum);
837 /* open IMAP connection */
838 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
839 /* get mailbox names once here */
840 $boxes = sqimap_mailbox_list($imapConnection);
841 /* ensure we have a valid default mailbox name */
842 $mailbox = strip_tags(asearch_nz($mailbox_array[0]));
843 if (($mailbox == '') ||
($mailbox == 'None')) { //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
844 $mailbox = $boxes[0]['unformatted'];
845 $mailbox_array[0] = $mailbox;
848 if (isset($composenew) && $composenew) {
849 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
850 "&session=$composesession&attachedmessages=true&";
851 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
854 displayPageHeader($color, $mailbox);
856 do_hook('search_before_form');
858 if (!$search_silent) {
859 echo html_tag('table',
860 html_tag('tr', "\n" .
861 html_tag('td', asearch_get_title_display($color, _("Search")), 'center', $color[0])
863 '', '', 'width="100%"') . "\n";
864 asearch_print_saved($data_dir, $username);
865 asearch_print_recent($data_dir, $username);
866 if (($submit == $add_criteria_button_text) ||
(empty($where_array)))
870 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $new_row);
873 /* This deserves a comment, at least. What is it for exactly? */
874 if (isset($newsort)) {
876 sqsession_register($sort, 'sort');
879 /*********************************************************************
880 * Check to see if we can use cache or not. Currently the only time *
881 * when you will not use it is when a link on the left hand frame is *
882 * used. Also check to make sure we actually have the array in the *
883 * registered session data. :) *
884 *********************************************************************/
885 if (! isset($use_mailbox_cache)) {
886 $use_mailbox_cache = 0;
889 /* There is a problem with registered vars in 4.1 */
891 if(substr(phpversion(), 0, 3) == '4.1') {
892 $use_mailbox_cache = FALSE;
896 do_hook('search_after_form');
898 if ($submit == $search_button_text) {
899 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
900 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
901 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]));
902 echo '</table><br>' . "\n";
904 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
905 if ($query_error != '')
906 echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
908 $old_allow_thread_sort = 0;
909 if ($allow_thread_sort == TRUE) {
910 $old_allow_thread_sort = $allow_thread_sort;
911 $allow_thread_sort = FALSE;
914 $boxcount = count($boxes);
915 for ($boxnum=0; $boxnum<$boxcount; $boxnum++
) {
916 if (!in_array('noselect', $boxes[$boxnum]['flags']))
917 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
920 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array);
921 if (empty($mboxes_msgs))
922 echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
924 foreach($mboxes_msgs as $mailbox => $msgs) {
925 sqimap_mailbox_select($imapConnection, $mailbox);
926 $msgs = fillMessageArray($imapConnection, $msgs, count($msgs));
927 /* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
928 /* $where = asearch_serialize($where_array);
929 $what = asearch_serialize($what_array);*/
930 $where = $where_array[0];
931 $what = $what_array[0];
932 asearch_print_mailbox_msgs($msgs, $mailbox, count($msgs), $imapConnection, urlencode($where), urlencode($what), false, false);
936 $allow_thread_sort = $old_allow_thread_sort;
941 do_hook('search_bottom');
942 sqimap_logout($imapConnection);
943 echo '</body></html>';