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