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