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