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/mime.php');
22 require_once(SM_PATH
. 'functions/mailbox_display.php'); //getButton()...
24 function asearch_unhtml_strcoll($a, $b)
26 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
29 function imap_get_mailbox_display($mailbox)
31 if (strtoupper($mailbox) == 'INBOX')
33 return imap_utf7_decode_local($mailbox);
36 function asearch_get_mailbox_display($mailbox)
38 if ($mailbox == 'All Folders')
39 return _("All Folders");
40 return imap_get_mailbox_display($mailbox);
43 function asearch_get_title_display($color, $txt)
45 return '<b><big>' . $txt . '</big></b>';
48 function asearch_get_error_display($color, $txt)
50 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
51 /*return '<b><big>' . $txt . '</big></b>';*/
54 function asearch_serialize($input_array)
56 /*return $input_array[0];*/
57 return serialize($input_array);
60 function asearch_unserialize($input_string)
62 /*return array($input_string);*/
63 return unserialize($input_string);
66 function asearch_getPref($data_dir, $username, $key, $index, $default = '')
68 return getPref($data_dir, $username, $key . $index, $default);
71 function asearch_setPref($data_dir, $username, $key, $index, $value)
73 return setPref($data_dir, $username, $key . $index, $value);
76 function asearch_removePref($data_dir, $username, $key, $index)
78 return removePref($data_dir, $username, $key . $index);
81 /* sanity checks, done before running the imap command and before push_recent */
82 function asearch_check_query($where_array, $what_array, $exclude_array)
84 global $imap_asearch_opcodes;
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");
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");
99 /* read the recent searches */
100 function asearch_read_recent($data_dir, $username)
102 global $recent_prefkeys;
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;
112 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
113 foreach ($recent_prefkeys as $key) {
114 array_pop($recent_array[$key]);
119 return $recent_array;
122 /* get the saved searches */
123 function asearch_read_saved($data_dir, $username)
125 global $saved_prefkeys;
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);
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);
142 /* save a recent search */
143 function asearch_save_recent($data_dir, $username, $recent_index)
145 global $recent_prefkeys, $saved_prefkeys;
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);
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]);
156 asearch_removePref($data_dir, $username, $saved_prefkeys[$n], $saved_index);
161 function asearch_write_recent($data_dir, $username, $recent_array)
163 global $recent_prefkeys;
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]);
172 for (; $recent_num < $recent_max; $recent_num++
) {
173 foreach ($recent_prefkeys as $key) {
174 asearch_removePref($data_dir, $username, $key, $recent_num);
179 /* forget a recent search */
180 function asearch_forget_recent($data_dir, $username, $forget_index)
182 global $recent_prefkeys;
184 $recent_array = asearch_read_recent($data_dir, $username);
185 foreach ($recent_prefkeys as $key) {
186 array_splice($recent_array[$key], $forget_index, 1);
188 asearch_write_recent($data_dir, $username, $recent_array);
191 function asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
193 global $recent_prefkeys;
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])) {
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]
218 /* push a recent search */
219 function asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
221 global $recent_prefkeys;
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);
229 foreach ($recent_prefkeys as $key) {
230 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
233 asearch_write_recent($data_dir, $username, $recent_array);
238 /* edit a recent search */
239 function asearch_edit_recent($data_dir, $username, $index)
241 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
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));
251 /* edit the last recent search if the prefs permit it */
252 function asearch_edit_last($data_dir, $username)
254 if (getPref($data_dir, $username, 'search_memory', 0) > 0)
255 asearch_edit_recent($data_dir, $username, 0);
258 /* edit a saved search */
259 function asearch_edit_saved($data_dir, $username, $index)
261 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
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));
271 function asearch_write_saved($data_dir, $username, $saved_array)
273 global $saved_prefkeys;
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]);
281 foreach ($saved_prefkeys as $key) {
282 asearch_removePref($data_dir, $username, $key, $saved_count);
286 /* delete a saved search */
287 function asearch_delete_saved($data_dir, $username, $saved_index)
289 global $saved_prefkeys;
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);
296 asearch_write_saved($data_dir, $username, $saved_array);
299 /* translate the input date to imap date to local date display, so the user can know if the date is wrong or illegal */
300 function asearch_get_date_display($what)
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];*/
309 $what_display = _("(Illegal date)");
312 $what_display = _("(Wrong date)");
313 return $what_display;
316 /* translate the query to rough natural display */
317 function asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
319 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
320 global $imap_asearch_opcodes;
322 $last_mailbox = $mailbox_array[0];
323 if (empty($last_mailbox))
324 $last_mailbox = 'INBOX';
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];
329 if (empty($cur_mailbox))
330 $cur_mailbox = 'INBOX';
331 $biop = asearch_nz($biop_array[$crit_num]);
332 if (($query_display == '') ||
($cur_mailbox != $last_mailbox)) {
333 $mailbox_display = ' <B>' . asearch_get_mailbox_display($cur_mailbox) . '</B>';
334 if ($query_display == '')
335 $biop_display = _("In");
337 $biop_display = $imap_asearch_biops_in[$biop];
338 $last_mailbox = $cur_mailbox;
341 $mailbox_display = '';
342 $biop_display = $imap_asearch_biops[$biop];
344 $biop_display = ' <U><I>' . $biop_display . '</I></U>';
345 $unop = $unop_array[$crit_num];
346 $unop_display = $imap_asearch_unops[$unop];
347 $where = $where_array[$crit_num];
348 $where_display = $imap_asearch_options[$where];
349 if ($unop_display != '')
350 $where_display = ' <U><I>' . $unop_display . ' ' . $where_display . '</I></U>';
352 $where_display = ' <U><I>' . $where_display . '</I></U>';
353 $what_type = $imap_asearch_opcodes[$where];
354 $what = $what_array[$crit_num];
355 if ($what_type) { /* Check opcode parameter */
357 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
359 if ($what_type == 'adate')
360 $what_display = asearch_get_date_display($what);
362 $what_display = htmlspecialchars($what);
363 $what_display = ' <B>' . $what_display . '</B>';
368 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
372 $query_display .= ' ' . $biop_display . $mailbox_display . $where_display . $what_display;
375 return $query_display;
378 /* Handle the alternate row colors */
379 function asearch_get_row_color($color, $row_num)
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];
390 return $color_string;
393 /* Print a whole query array, recent or saved */
394 function asearch_print_query_array($query_array, $query_keys, $action_array, $title)
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'));
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));
407 unset($search_array);
408 foreach ($query_keys as $query_key) {
409 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
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];
417 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
419 echo html_tag('td', $query_num+
1, 'right');
420 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
421 foreach ($action_array as $action => $action_display) {
422 echo html_tag('td', '<a href=search.php?submit=' . $action . '&rownum=' . $query_num . '>' . $action_display . '</a>', 'center');
429 echo '</table>' . "\n";
432 /* print the saved array */
433 function asearch_print_saved($data_dir, $username)
435 global $saved_prefkeys;
437 $saved_array = asearch_read_saved($data_dir, $username);
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"));
447 /* print the recent array */
448 function asearch_print_recent($data_dir, $username)
450 global $recent_prefkeys;
452 $recent_array = asearch_read_recent($data_dir, $username);
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"));
463 /* build an <option> statement */
464 function asearch_opt($val, $sel, $tit)
466 return '<option value="' . $val . '"' . ($sel == $val ?
' selected' : '') . '>' . $tit . '</option>' . "\n";
469 /* build a <select> statement from an array */
470 function asearch_opt_array($var_name, $opt_array, $cur_val)
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";
479 function asearch_mailbox_exists($mailbox, $boxes)
481 foreach ($boxes as $box) {
482 if ($box['unformatted'] == $mailbox)
488 /* print one form row */
489 function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num)
491 global $imap_asearch_biops_in, $imap_asearch_unops, $imap_asearch_options;
494 echo html_tag('tr', '', '', $color[4]);
496 echo html_tag('td', '', 'center');
497 /* Binary operator */
499 echo asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop);
501 echo /*'<input type="hidden" name="biop[0]" value="">' .*/ '<b>' . _("In") . '</b>';
504 echo html_tag('td', '', 'center');
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') . ']');
510 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes);
511 echo '</select></td>' . "\n";
513 /* Unary operator and Search location */
517 asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
518 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
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('"', '"', $what_disp);*/
527 $what_disp = htmlspecialchars($what);
528 echo html_tag('td', '<input type="text" size="35" name="what[' . $row_num . ']" value="' . $what_disp . '">', 'center') . "\n";
530 /* Exclude criteria */
532 _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ?
' CHECKED' : '') . '>', 'center', '') . "\n";
537 /* print the search form */
538 function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $add_criteria)
540 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
545 echo '<form action="search.php" name="form_asearch">' . "\n";
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'));
549 $row_count = count($where_array) +
$add_criteria;
551 for ($row_num = 0; $row_num < $row_count; $row_num++
) {
552 /* Keep the last non-empty mailbox as default choice */
553 if (strip_tags(asearch_nz($mailbox_array[$row_num])) != '')
554 $mailbox = strip_tags($mailbox_array[$row_num]);
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]));
559 $where = asearch_nz($where_array[$row_num]);
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);
564 echo '</table>' . "\n";
567 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
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"
574 echo '</table>' . "\n";
575 echo '</form>' . "\n";
578 /* print the $msgs messages from $mailbox mailbox */
579 /* this is almost the original code */
580 function asearch_print_mailbox_msgs($msgs, $mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false)
582 global $sort, $color;
585 $msort = calc_msort($msgs, $sort);
586 $showbox = asearch_get_mailbox_display($mailbox);
587 echo html_tag('div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
589 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
590 $toggle_all = get_selectall_link(1, $sort);
592 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
595 mail_message_listing_beginning($imapConnection, $mailbox, $sort, $msg_cnt_str, $toggle_all, 1);
598 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
601 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
604 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
606 printHeader($mailbox, 6, $color, false);
607 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $sort, $color, $cnt, $where, $what);
612 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
619 /* ------------------------ main ------------------------ */
620 global $allow_thread_sort;
622 /* get globals we may need */
623 sqgetGlobalVar('username', $username, SQ_SESSION
);
624 sqgetGlobalVar('key', $key, SQ_COOKIE
);
625 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
); /* we really need this? */
626 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION
); /* do we really need this? */
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');*/
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);
636 $del_excluded_button_html = _("Remove Excluded Criteria");
637 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
638 $del_all_button_html = _("Remove All Criteria");
639 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
641 $imap_asearch_options = array(
643 /*'ALL' is binary operator */
644 'ANSWERED' => _("Answered"),
646 'BEFORE' => _("Before"),
647 'BODY' => _("Message Body"),
649 'DELETED' => _("Deleted"),
650 'DRAFT' => _("Draft"),
651 'FLAGGED' => _("Flagged"),
652 'FROM' => _("Sent By"),
653 'HEADER' => _("Header Field"),
654 'KEYWORD' => _("Keyword"),
655 'LARGER' => _("Larger Than"),
657 /*'NOT' is unary operator */
660 /*'OR' is binary operator */
661 'RECENT' => _("Recent"),
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"),
672 /*'UNANSWERED' => '',
676 'UNKEYWORD' => _("Unkeyword"),
677 'UNSEEN' => _("Unseen"),*/
679 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
681 $imap_asearch_unops = array(
686 $imap_asearch_biops_in = array(
687 'ALL' => _("And In"),
691 $imap_asearch_biops = array(
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
703 $del_excluded_button_text : Remove Excluded Criteria button
704 $del_all_button_text : Remove All Criteria button
712 if (isset($_GET['submit']))
713 $submit = strip_tags($_GET['submit']);
716 if (isset($_GET['mailbox'])) {
717 $mailbox_array = $_GET['mailbox'];
718 if (!is_array($mailbox_array))
719 $mailbox_array = array($mailbox_array);
722 $mailbox_array = array();
724 if (isset($_GET['biop'])) {
725 $biop_array = $_GET['biop'];
726 if (!is_array($biop_array))
727 $biop_array = array($biop_array);
730 $biop_array = array();
732 if (isset($_GET['unop'])) {
733 $unop_array = $_GET['unop'];
734 if (!is_array($unop_array))
735 $unop_array = array($unop_array);
738 $unop_array = array();
740 if (isset($_GET['where'])) {
741 $where_array = $_GET['where'];
742 if (!is_array($where_array))
743 $where_array = array($where_array);
746 $where_array = array();
748 if (isset($_GET['what'])) {
749 $what_array = $_GET['what'];
750 if (!is_array($what_array))
751 $what_array = array($what_array);
754 $what_array = array();
756 if (isset($_GET['exclude']))
757 $exclude_array = $_GET['exclude'];
759 $exclude_array = array();
761 /* Used by recent and saved stuff */
762 if (isset($_GET['rownum'])) {
763 $submit_rownum = strip_tags($_GET['rownum']);
766 /* end of get globals */
768 $search_silent = FALSE; /* Default is normal behaviour */
770 /* See how the page was called and fire off correct function */
771 if ((!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);
778 if (!isset($submit)) {
783 case $search_button_text:
784 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
785 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
787 case 'Search_silent':
788 $search_silent = TRUE;
790 case 'Search_no_update':
791 $submit = $search_button_text;
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 */
804 $exclude_array = array();
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();
815 asearch_save_recent($data_dir, $username, $submit_rownum);
817 case 'search_recent':
818 $submit = $search_button_text;
820 case 'edit_recent': /* no link to do this, yet */
821 asearch_edit_recent($data_dir, $username, $submit_rownum);
823 case 'forget_recent':
824 asearch_forget_recent($data_dir, $username, $submit_rownum);
827 $submit = $search_button_text;
830 asearch_edit_saved($data_dir, $username, $submit_rownum);
833 asearch_delete_saved($data_dir, $username, $submit_rownum);
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);
842 /* ensure we have a valid default mailbox name */
843 $mailbox = strip_tags(asearch_nz($mailbox_array[0]));
844 if (($mailbox == '') ||
($mailbox == 'None')) { //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
845 $mailbox = $boxes[0]['unformatted'];
846 $mailbox_array[0] = $mailbox;
849 if (isset($composenew) && $composenew) {
850 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
851 "&session=$composesession&attachedmessages=true&";
852 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
855 displayPageHeader($color, $mailbox);
857 do_hook('search_before_form');
859 if (!$search_silent) {
860 echo html_tag('table',
861 html_tag('tr', "\n" .
862 html_tag('td', asearch_get_title_display($color, _("Search")), 'center', $color[0])
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)))
871 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $new_row);
874 /* This deserves a comment, at least. What is it for exactly? */
875 if (isset($newsort)) {
877 sqsession_register($sort, 'sort');
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 *********************************************************************/
886 if (! isset($use_mailbox_cache)) {
887 $use_mailbox_cache = 0;
890 /* There is a problem with registered vars in 4.1 */
892 if(substr(phpversion(), 0, 3) == '4.1') {
893 $use_mailbox_cache = FALSE;
897 do_hook('search_after_form');
899 if ($submit == $search_button_text) {
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]));
903 echo '</table><br>' . "\n";
905 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
906 if ($query_error != '')
907 echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
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;
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'];
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))
923 echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
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);
937 $allow_thread_sort = $old_allow_thread_sort;
942 do_hook('search_bottom');
943 sqimap_logout($imapConnection);
944 echo '</body></html>';