Added support for using Squirrelmail without frames
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2003 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 ftp://ftp.rfc-editor.org/in-notes/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 echo html_tag('div', '<b><big>' . _("Folder:") . ' '. $mailbox_display . '</big></b>','center') . "\n";
667
668 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
669 $toggle_all = get_selectall_link(1, $real_sort);
670
671 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
672
673 echo '<tr><td>';
674 mail_message_listing_beginning($imapConnection, $mailbox, $real_sort, $msg_cnt_str, $toggle_all, 1);
675 echo '</td></tr>';
676
677 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
678
679 echo '<tr><td>';
680 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
681 echo ' <tr><td>';
682
683 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
684 echo ' <tr><td>';
685 printHeader($mailbox, $sort, $color, !$thread_sort_messages);
686 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $real_sort, $color, $cnt, $where, $what);
687 echo ' </td></tr>';
688 echo ' </table>';
689 echo ' </td></tr>';
690 echo ' </table>';
691 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
692 echo '</td></tr>';
693
694 echo '</table>';
695 }
696 }
697
698 /**
699 * @param array $boxes mailboxes array (reference)
700 * @return array selectable unformatted mailboxes names
701 */
702 function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
703 {
704 $mboxes_array = array();
705 $boxcount = count($boxes);
706 for ($boxnum=0; $boxnum<$boxcount; $boxnum++) {
707 if (!in_array('noselect', $boxes[$boxnum]['flags']))
708 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
709 }
710 return $mboxes_array;
711 }
712
713 /* ------------------------ main ------------------------ */
714 /* get globals we may need */
715 sqgetGlobalVar('username', $username, SQ_SESSION);
716 sqgetGlobalVar('key', $key, SQ_COOKIE);
717 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); /* we really need this? */
718 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); /* do we really need this? */
719
720 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude', 'asearch_recent_sub');
721 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude', 'asearch_saved_sub');
722 /*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
723
724 $search_button_html = _("Search");
725 $search_button_text = asearch_unhtmlentities($search_button_html);
726 $add_criteria_button_html = _("Add New Criteria");
727 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
728 $del_excluded_button_html = _("Remove Excluded Criteria");
729 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
730 $del_all_button_html = _("Remove All Criteria");
731 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
732
733 $imap_asearch_options = array(
734 /* <message set>, */
735 /*'ALL' is binary operator */
736 'ANSWERED' => _("Answered"),
737 'BCC' => _("Bcc"),
738 'BEFORE' => _("Before"),
739 'BODY' => _("Message Body"),
740 'CC' => _("CC"),
741 'DELETED' => _("Deleted"),
742 'DRAFT' => _("Draft"),
743 'FLAGGED' => _("Flagged"),
744 'FROM' => _("Sent By"),
745 'HEADER' => _("Header Field"),
746 'KEYWORD' => _("Keyword"),
747 'LARGER' => _("Larger Than"),
748 'NEW' => _("New"),
749 /*'NOT' is unary operator */
750 'OLD' => _("Old"),
751 'ON' => _("On"),
752 /*'OR' is binary operator */
753 'RECENT' => _("Recent"),
754 'SEEN' => _("Seen"),
755 'SENTBEFORE' => _("Sent Before"),
756 'SENTON' => _("Sent On"),
757 'SENTSINCE' => _("Sent Since"),
758 'SINCE' => _("Since"),
759 'SMALLER' => _("Smaller Than"),
760 'SUBJECT' => _("Subject Contains"),
761 'TEXT' => _("Header and Body"),
762 'TO' => _("Sent To"),
763 /*'UID' => 'anum',*/
764 /*'UNANSWERED' => '',
765 'UNDELETED' => '',
766 'UNDRAFT' => '',
767 'UNFLAGGED' => '',
768 'UNKEYWORD' => _("Unkeyword"),
769 'UNSEEN' => _("Unseen"),*/
770 );
771 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
772
773 $imap_asearch_unops = array(
774 '' => '',
775 'NOT' => _("Not")
776 );
777
778 $imap_asearch_biops_in = array(
779 'ALL' => _("And In"),
780 'OR' => _("Or In")
781 );
782
783 $imap_asearch_biops = array(
784 'ALL' => _("And"),
785 'OR' => _("Or")
786 );
787
788 /** How we did enter the form
789 * - unset : Enter key, or called from outside (eg read_body)
790 * - $search_button_text : Search button
791 * - 'Search_no_update' : Search but don't update recent
792 * - 'Search_last' : Same as no_update but reload and search last
793 * - 'Search_silent' : Same as no_update but only display results
794 * - $add_criteria_button_text : Add New Criteria button
795 * - $del_excluded_button_text : Remove Excluded Criteria button
796 * - $del_all_button_text : Remove All Criteria button
797 * - 'save_recent'
798 * - 'search_recent'
799 * - 'forget_recent'
800 * - 'edit_saved'
801 * - 'search_saved'
802 * - 'delete_saved'
803 * @global string $submit
804 */
805 if (isset($_GET['submit']))
806 $submit = strip_tags($_GET['submit']);
807
808 /** Searched mailboxes
809 * @global array $mailbox_array
810 */
811 if (isset($_GET['mailbox'])) {
812 $mailbox_array = $_GET['mailbox'];
813 if (!is_array($mailbox_array))
814 $mailbox_array = array($mailbox_array);
815 }
816 else
817 $mailbox_array = array();
818
819 /** Binary operators
820 * @global array $biop_array
821 */
822 if (isset($_GET['biop'])) {
823 $biop_array = $_GET['biop'];
824 if (!is_array($biop_array))
825 $biop_array = array($biop_array);
826 }
827 else
828 $biop_array = array();
829
830 /** Unary operators
831 * @global array $unop_array
832 */
833 if (isset($_GET['unop'])) {
834 $unop_array = $_GET['unop'];
835 if (!is_array($unop_array))
836 $unop_array = array($unop_array);
837 }
838 else
839 $unop_array = array();
840
841 /** Where to search
842 * @global array $where_array
843 */
844 if (isset($_GET['where'])) {
845 $where_array = $_GET['where'];
846 if (!is_array($where_array))
847 $where_array = array($where_array);
848 }
849 else
850 $where_array = array();
851
852 /** What to search
853 * @global array $what_array
854 */
855 if (isset($_GET['what'])) {
856 $what_array = $_GET['what'];
857 if (!is_array($what_array))
858 $what_array = array($what_array);
859 }
860 else
861 $what_array = array();
862
863 /** Whether to exclude this criteria from search
864 * @global array $exclude_array
865 */
866 if (isset($_GET['exclude']))
867 $exclude_array = $_GET['exclude'];
868 else
869 $exclude_array = array();
870
871 /** Search within subfolders
872 * @global array $sub_array
873 */
874 if (isset($_GET['sub']))
875 $sub_array = $_GET['sub'];
876 else
877 $sub_array = array();
878
879 /** Row number used by recent and saved stuff
880 */
881 if (isset($_GET['rownum']))
882 $submit_rownum = strip_tags($_GET['rownum']);
883
884 /** Change global sort
885 */
886 if (sqgetGlobalVar('newsort', $newsort, SQ_GET)) {
887 setPref($data_dir, $username, 'sort', $newsort);
888 $sort = $newsort;
889 sqsession_register($sort, 'sort');
890 asearch_edit_last($data_dir, $username);
891 }
892
893 /** Change mailbox threading
894 */
895 if (sqgetGlobalVar('set_thread', $set_thread, SQ_GET)) {
896 setPref($data_dir, $username, 'thread_' . $mailbox_array[0], ($set_thread == 1) ? 1 : 0 );
897 asearch_edit_last($data_dir, $username);
898 }
899
900 /* end of get globals */
901
902 /** If TRUE, do not show search interface
903 * @global bool $search_silent
904 */
905 $search_silent = FALSE; /* Default is normal behaviour */
906
907 /* See how the page was called and fire off correct function */
908 if ((!isset($submit) || empty($submit)) && !empty($where_array)) {
909 /* This happens when the Enter key is used or called from outside */
910 $submit = $search_button_text;
911 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
912 asearch_edit_last($data_dir, $username);
913 }
914
915 if (!isset($submit)) {
916 $submit = '';
917 }
918 else {
919 switch ($submit) {
920 case $search_button_text:
921 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
922 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
923 break;
924 case 'Search_silent':
925 $search_silent = TRUE;
926 /*nobreak;*/
927 case 'Search_no_update':
928 $submit = $search_button_text;
929 break;
930 case $del_excluded_button_text:
931 $delarray = array_keys($exclude_array);
932 while (!empty($delarray)) {
933 $delrow = array_pop($delarray);
934 array_splice($mailbox_array, $delrow, 1);
935 array_splice($biop_array, $delrow, 1);
936 array_splice($unop_array, $delrow, 1);
937 array_splice($where_array, $delrow, 1);
938 array_splice($what_array, $delrow, 1);
939 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
940 array_splice($sub_array, $delrow, 1);
941 }
942 $exclude_array = array();
943 break;
944 case $del_all_button_text:
945 $mailbox_array = array();
946 $biop_array = array();
947 $unop_array = array();
948 $where_array = array();
949 $what_array = array();
950 $exclude_array = array();
951 $sub_array = array();
952 break;
953 case 'save_recent':
954 asearch_save_recent($data_dir, $username, $submit_rownum);
955 break;
956 case 'search_recent':
957 $submit = $search_button_text;
958 asearch_edit_recent($data_dir, $username, $submit_rownum);
959 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
960 break;
961 case 'edit_recent': /* no link to do this, yet */
962 asearch_edit_recent($data_dir, $username, $submit_rownum);
963 break;
964 case 'forget_recent':
965 asearch_forget_recent($data_dir, $username, $submit_rownum);
966 break;
967 case 'search_saved':
968 $submit = $search_button_text;
969 asearch_edit_saved($data_dir, $username, $submit_rownum);
970 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
971 break;
972 case 'edit_saved':
973 asearch_edit_saved($data_dir, $username, $submit_rownum);
974 break;
975 case 'delete_saved':
976 asearch_delete_saved($data_dir, $username, $submit_rownum);
977 break;
978 }
979 }
980
981 /* open IMAP connection */
982 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
983 /* get mailbox names once here */
984 $boxes = sqimap_mailbox_list($imapConnection);
985 /* ensure we have a valid default mailbox name */
986 $mailbox = asearch_nz($mailbox_array[0]);
987 if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
988 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
989
990 if (isset($composenew) && $composenew) {
991 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
992 "&amp;session=$composesession&amp;attachedmessages=true&amp";
993 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
994 }
995 else
996 displayPageHeader($color, $mailbox);
997
998 do_hook('search_before_form');
999
1000 if (!$search_silent) {
1001 echo html_tag('table',
1002 html_tag('tr', "\n" .
1003 html_tag('td', asearch_get_title_display($color, _("Search")), 'center', $color[0])
1004 ) ,
1005 '', '', 'width="100%"') . "\n";
1006 asearch_print_saved($data_dir, $username);
1007 asearch_print_recent($data_dir, $username);
1008 if (empty($where_array)) {
1009 global $sent_folder;
1010
1011 $mailbox_array[0] = $mailbox;
1012 $biop_array[0] = '';
1013 $unop_array[0] = '';
1014 if ($mailbox == $sent_folder)
1015 $where_array[0] = 'TO';
1016 else
1017 $where_array[0] = 'FROM';
1018 $what_array[0] = '';
1019 $exclude_array[0] = '';
1020 $sub_array[0] = '';
1021 }
1022 if ($submit == $add_criteria_button_text) {
1023 $last_index = max(count($where_array) - 1, 0);
1024 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1025 $biop_array[] = asearch_nz($biop_array[$last_index]);
1026 $unop_array[] = asearch_nz($unop_array[$last_index]);
1027 $where_array[] = asearch_nz($where_array[$last_index]);
1028 $what_array[] = asearch_nz($what_array[$last_index]);
1029 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1030 $sub_array[] = asearch_nz($sub_array[$last_index]);
1031 }
1032 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1033 }
1034
1035 /*********************************************************************
1036 * Check to see if we can use cache or not. Currently the only time *
1037 * when you will not use it is when a link on the left hand frame is *
1038 * used. Also check to make sure we actually have the array in the *
1039 * registered session data. :) *
1040 *********************************************************************/
1041 if (!isset($use_mailbox_cache))
1042 $use_mailbox_cache = 0;
1043
1044 do_hook('search_after_form');
1045
1046 if ($submit == $search_button_text) {
1047 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1048 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1049 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]));
1050 echo '</table><br>' . "\n";
1051
1052 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
1053 if ($query_error != '')
1054 echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
1055 else {
1056
1057 // Disable thread sort for now if there is more than one mailbox or at least one 'All Folders'
1058 global $allow_thread_sort;
1059 $old_allow_thread_sort = $allow_thread_sort;
1060 $allow_thread_sort = ((count(array_unique($mailbox_array)) <= 1) && (!in_array('All Folders', $mailbox_array)));
1061
1062 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
1063 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1064 if (empty($mboxes_msgs))
1065 echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1066 else {
1067 foreach($mboxes_msgs as $mailbox => $msgs) {
1068 sqimap_mailbox_select($imapConnection, $mailbox);
1069 $msgs = fillMessageArray($imapConnection, $msgs, count($msgs));
1070 /* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
1071 /* $where = asearch_serialize($where_array);
1072 $what = asearch_serialize($what_array);*/
1073 $where = $where_array[0];
1074 $what = $what_array[0];
1075 asearch_print_mailbox_msgs($imapConnection, $mailbox, $msgs, count($msgs), $sort, $color, urlencode($where), urlencode($what));
1076 }
1077 }
1078
1079 $allow_thread_sort = $old_allow_thread_sort; // Restore thread sort
1080 }
1081 }
1082
1083 do_hook('search_bottom');
1084 sqimap_logout($imapConnection);
1085 noframes_bottom();
1086
1087 ?>