Fix configtest to use correct PostgreSQL connection function. (#1166228).
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2005 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP search page
10 *
11 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
12 *
13 * @version $Id$
14 * @package squirrelmail
15 * @link http://www.ietf.org/rfc/rfc3501.txt
16 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
17 */
18
19 /**
20 * Path for SquirrelMail required files.
21 * @ignore
22 */
23 define('SM_PATH','../');
24
25 /** SquirrelMail required files.
26 */
27 require_once(SM_PATH . 'include/validate.php');
28 require_once(SM_PATH . 'functions/strings.php');
29 require_once(SM_PATH . 'functions/imap_asearch.php');
30 require_once(SM_PATH . 'functions/imap_mailbox.php');
31 require_once(SM_PATH . 'functions/imap_messages.php');
32 require_once(SM_PATH . 'functions/mime.php');
33 require_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()...
34
35 /** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys
36 */
37 define('ASEARCH_WHERE', 0);
38 define('ASEARCH_MAILBOX', 1);
39 define('ASEARCH_WHAT', 2);
40 define('ASEARCH_UNOP', 3);
41 define('ASEARCH_BIOP', 4);
42 define('ASEARCH_EXCLUDE', 5);
43 define('ASEARCH_SUB', 6);
44 define('ASEARCH_MAX', 7);
45
46 /** Name of session var
47 */
48 define('ASEARCH_CRITERIA', 'criteria');
49
50 /** Builds a href with params
51 * @param string $params optional parameters to GET
52 */
53 function asearch_get_href($params = '')
54 {
55 $href = 'search.php';
56 if ($params != '')
57 $href .= '?' . $params;
58 return $href;
59 }
60
61 /** Builds a [link]
62 * @param string $href (reference)
63 * @param string $text
64 * @param string $title
65 */
66 function asearch_get_link(&$href, $text, $title = '')
67 {
68 if ($title != '')
69 $title = ' title="' . $title . '"';
70 return '<a href="' . $href . '"' . $title . '>' . $text . '</a>';
71 }
72
73 /** Builds a toggle [link]
74 * @param integer $value
75 * @param string $action
76 * @param array $text_array
77 * @param array $title_array
78 */
79 function asearch_get_toggle_link($value, $action, $text_array, $title_array = array())
80 {
81 return asearch_get_link(asearch_get_href($action . '=' . (int)$value), $text_array[$value], asearch_nz($title_array[$value]));
82 }
83
84 /**
85 * @param string $a
86 * @param string $b
87 * @return bool strcoll()-like result
88 */
89 function asearch_unhtml_strcoll($a, $b)
90 {
91 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
92 }
93
94 /**
95 * @param string $mailbox mailbox name utf7 encoded inc. special case INBOX
96 * @return string mailbox name ready to display (utf7 decoded or localized INBOX)
97 */
98 function imap_get_mailbox_display($mailbox)
99 {
100 if (strtoupper($mailbox) == 'INBOX')
101 return _("INBOX");
102 return imap_utf7_decode_local($mailbox);
103 }
104
105 /**
106 * @param string $mailbox mailbox name or special case 'All Folders'
107 * @return string mailbox name ready to display (utf7 decoded or localized 'All Folders')
108 */
109 function asearch_get_mailbox_display($mailbox)
110 {
111 if ($mailbox == 'All Folders')
112 return _("All Folders");
113 return imap_get_mailbox_display($mailbox);
114 }
115
116 /**
117 * @param array $color color array
118 * @param string $txt text to display
119 * @return string title ready to display
120 */
121 function asearch_get_title_display(&$color, $txt)
122 {
123 return '<b><big>' . $txt . '</big></b>';
124 }
125
126 /**
127 * @param array $color color array
128 * @param string $txt text to display
129 * @return string error text ready to display
130 */
131 function asearch_get_error_display(&$color, $txt)
132 {
133 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
134 }
135
136 /**
137 * @param array $input_array array to serialize
138 * @return string a string containing a byte-stream representation of value that can be stored anywhere
139 */
140 function asearch_serialize(&$input_array)
141 {
142 global $search_advanced;
143 if ($search_advanced)
144 return serialize($input_array);
145 return $input_array[0];
146 }
147
148 /**
149 * @param string $input_string string to unserialize
150 * @return array
151 */
152 function asearch_unserialize($input_string)
153 {
154 global $search_advanced;
155 if ($search_advanced)
156 return unserialize($input_string);
157 return array($input_string);
158 }
159
160 /**
161 * @param string $key the pref key
162 * @param integer $index the pref key index
163 * @param string $default default value
164 * @return string pref value
165 */
166 function asearch_getPref(&$key, $index, $default = '')
167 {
168 global $data_dir, $username, $search_advanced;
169 return getPref($data_dir, $username, $key . ($index + !$search_advanced), $default);
170 }
171
172 /**
173 * @param string $key the pref key
174 * @param integer $index the pref key index
175 * @param string $value pref value to set
176 * @return bool status
177 */
178 function asearch_setPref(&$key, $index, $value)
179 {
180 global $data_dir, $username, $search_advanced;
181 return setPref($data_dir, $username, $key . ($index + !$search_advanced), $value);
182 }
183
184 /**
185 * @param string $key the pref key
186 * @param integer $index the pref key index
187 * @return bool status
188 */
189 function asearch_removePref(&$key, $index)
190 {
191 global $data_dir, $username, $search_advanced;
192 return removePref($data_dir, $username, $key . ($index + !$search_advanced));
193 }
194
195 /** Sanity checks, done before running the imap command and before calling push_recent
196 */
197 function asearch_check_query(&$where_array, &$what_array, &$exclude_array)
198 {
199 global $imap_asearch_opcodes;
200
201 if (empty($where_array))
202 return _("Please enter something to search for");
203 if (count($exclude_array) == count($where_array))
204 return _("There must be at least one criteria to search for");
205 for ($crit_num = 0; $crit_num < count($where_array); $crit_num++) {
206 $where = $where_array[$crit_num];
207 $what = $what_array[$crit_num];
208 if (!(($what == '') ^ ($imap_asearch_opcodes[$where] != '')))
209 return _("Error in criteria argument");
210 }
211 return '';
212 }
213
214 /** Read the recent searches from the prefs
215 */
216 function asearch_read_recent()
217 {
218 global $recent_prefkeys, $search_memory;
219
220 $recent_array = array();
221 $recent_num = 0;
222 for ($pref_num = 0; $pref_num < $search_memory; $pref_num++) {
223 foreach ($recent_prefkeys as $prefkey) {
224 $pref = asearch_getPref($prefkey, $pref_num);
225 /* if (!empty($pref))*/
226 $recent_array[$prefkey][$recent_num] = $pref;
227 }
228 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
229 foreach ($recent_prefkeys as $key) {
230 array_pop($recent_array[$key]);
231 }
232 // break; //Disabled to support old search code broken prefs
233 }
234 else
235 $recent_num++;
236 }
237 return $recent_array;
238 }
239
240 /** Read the saved searches from the prefs
241 */
242 function asearch_read_saved()
243 {
244 global $saved_prefkeys;
245
246 $saved_array = array();
247 $saved_key = $saved_prefkeys[0];
248 for ($saved_count = 0; ; $saved_count++) {
249 $pref = asearch_getPref($saved_key, $saved_count);
250 if (empty($pref))
251 break;
252 }
253 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) {
254 foreach ($saved_prefkeys as $key) {
255 $saved_array[$key][$saved_num] = asearch_getPref($key, $saved_num);
256 }
257 }
258 return $saved_array;
259 }
260
261 /** Save a recent search to the prefs
262 */
263 function asearch_save_recent($recent_index)
264 {
265 global $recent_prefkeys, $saved_prefkeys;
266
267 $saved_array = asearch_read_saved();
268 $saved_index = count($saved_array[$saved_prefkeys[0]]);
269 $recent_array = asearch_read_recent();
270 $n = 0;
271 foreach ($recent_prefkeys as $key) {
272 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
273 if (!empty($recent_slice[0]))
274 asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]);
275 else
276 asearch_removePref($saved_prefkeys[$n], $saved_index);
277 $n++;
278 }
279 }
280
281 /** Write a recent search to prefs
282 */
283 function asearch_write_recent(&$recent_array)
284 {
285 global $recent_prefkeys, $search_memory;
286
287 $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]]));
288 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
289 foreach ($recent_prefkeys as $key) {
290 asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]);
291 }
292 }
293 for (; $recent_num < $search_memory; $recent_num++) {
294 foreach ($recent_prefkeys as $key) {
295 asearch_removePref($key, $recent_num);
296 }
297 }
298 }
299
300 /** Remove a recent search from prefs
301 */
302 function asearch_forget_recent($forget_index)
303 {
304 global $recent_prefkeys;
305
306 $recent_array = asearch_read_recent();
307 foreach ($recent_prefkeys as $key) {
308 array_splice($recent_array[$key], $forget_index, 1);
309 }
310 asearch_write_recent($recent_array);
311 }
312
313 /** Find a recent search in the prefs (used to avoid saving duplicates)
314 */
315 function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
316 {
317 global $recent_prefkeys, $search_advanced;
318
319 $where_string = asearch_serialize($where_array);
320 $mailbox_string = asearch_serialize($mailbox_array);
321 $what_string = asearch_serialize($what_array);
322 $unop_string = asearch_serialize($unop_array);
323 if ($search_advanced) {
324 $biop_string = asearch_serialize($biop_array);
325 $exclude_string = asearch_serialize($exclude_array);
326 $sub_string = asearch_serialize($sub_array);
327 }
328 $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE]]);
329 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
330 if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num])) {
331 if (
332 $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num] &&
333 $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX]][$recent_num] &&
334 $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT]][$recent_num] &&
335 $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP]][$recent_num] &&
336 ((!$search_advanced) ||
337 ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP]][$recent_num] &&
338 $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE]][$recent_num] &&
339 $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB]][$recent_num]))
340 )
341 return $recent_num;
342 }
343 }
344 return -1;
345 }
346
347 /** Push a recent search into the prefs
348 */
349 function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
350 {
351 global $recent_prefkeys, $search_memory;
352 //global $what; // Hack to access issued search from read_body.php
353 $what = 1;
354 /**
355 * Update search history and store it in the session so we can retrieve the
356 * issued search when returning from an external page like read_body.php
357 */
358 $criteria[$what] = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
359 sqsession_register($criteria, ASEARCH_CRITERIA);
360 if ($search_memory > 0) {
361 $recent_array = asearch_read_recent();
362 $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
363 if ($recent_found >= 0) { // Remove identical recent
364 foreach ($recent_prefkeys as $key) {
365 array_splice($recent_array[$key], $recent_found, 1);
366 }
367 }
368 $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array);
369 $i = 0;
370 foreach ($recent_prefkeys as $key) {
371 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
372 $i++;
373 }
374 asearch_write_recent($recent_array);
375 }
376 }
377
378 /** Edit a recent search
379 * @global array mailbox_array searched mailboxes
380 */
381 function asearch_edit_recent($index)
382 {
383 global $recent_prefkeys, $search_advanced;
384 global $where_array, $mailbox_array, $what_array, $unop_array;
385 global $biop_array, $exclude_array, $sub_array;
386
387 $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE], $index));
388 $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX], $index));
389 $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT], $index));
390 $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP], $index));
391 if ($search_advanced) {
392 $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP], $index));
393 $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE], $index));
394 $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB], $index));
395 }
396 }
397
398 /** Get last search criteria from session or prefs
399 * FIX ME, try to avoid globals
400 */
401 function asearch_edit_last($index) {
402 if (sqGetGlobalVar(ASEARCH_CRITERIA, $criteria, SQ_SESSION)) {
403 global $where_array, $mailbox_array, $what_array, $unop_array;
404 global $biop_array, $exclude_array, $sub_array;
405 $mailbox_array = $criteria[$index][0];
406 $biop_array = $criteria[$index][1];
407 $unop_array = $criteria[$index][2];
408 $where_array = $criteria[$index][3];
409 $what_array = $criteria[$index][4];
410 $exclude_array = $criteria[$index][5];
411 $sub_array = $criteria[$index][6];
412 unset($criteria[$index]);
413 //sqsession_unregister(ASEARCH_CRITERIA);
414 } else {
415 global $search_memory;
416 if ($search_memory > 0) {
417 asearch_edit_recent(0);
418 }
419 }
420 }
421
422 /** Edit a saved search
423 */
424 function asearch_edit_saved($index)
425 {
426 global $saved_prefkeys, $search_advanced;
427 global $where_array, $mailbox_array, $what_array, $unop_array;
428 global $biop_array, $exclude_array, $sub_array;
429
430 $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE], $index));
431 $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX], $index));
432 $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT], $index));
433 $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP], $index));
434 if ($search_advanced) {
435 $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP], $index));
436 $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE], $index));
437 $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB], $index));
438 }
439 }
440
441 /** Write a saved search to the prefs
442 */
443 function asearch_write_saved(&$saved_array)
444 {
445 global $saved_prefkeys;
446
447 $saved_count = count($saved_array[$saved_prefkeys[0]]);
448 for ($saved_num=0; $saved_num < $saved_count; $saved_num++) {
449 foreach ($saved_prefkeys as $key) {
450 asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]);
451 }
452 }
453 foreach ($saved_prefkeys as $key) {
454 asearch_removePref($key, $saved_count);
455 }
456 }
457
458 /** Delete a saved search from the prefs
459 */
460 function asearch_delete_saved($saved_index)
461 {
462 global $saved_prefkeys;
463
464 $saved_array = asearch_read_saved();
465 $asearch_keys = $saved_prefkeys;
466 foreach ($asearch_keys as $key) {
467 array_splice($saved_array[$key], $saved_index, 1);
468 }
469 asearch_write_saved($saved_array);
470 }
471
472 /** Translate the input date to imap date to local date display,
473 * so the user can know if the date is wrong or illegal
474 * @return string locally formatted date or error text
475 */
476 function asearch_get_date_display(&$what)
477 {
478 $what_parts = sqimap_asearch_parse_date($what);
479 if (count($what_parts) == 4) {
480 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3]))
481 return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
482 //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];
483 return _("(Illegal date)");
484 }
485 return _("(Wrong date)");
486 }
487
488 /** Translate the query to rough natural display
489 * @return string rough natural query ready to display
490 */
491 function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
492 {
493 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
494 global $imap_asearch_opcodes;
495
496 $last_mailbox = $mailbox_array[0];
497 if (empty($last_mailbox))
498 $last_mailbox = 'INBOX';
499 $query_display = '';
500 for ($crit_num=0; $crit_num < count($where_array); $crit_num++) {
501 if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) {
502 $cur_mailbox = $mailbox_array[$crit_num];
503 if (empty($cur_mailbox))
504 $cur_mailbox = 'INBOX';
505 $biop = asearch_nz($biop_array[$crit_num]);
506 if (($query_display == '') || ($cur_mailbox != $last_mailbox)) {
507 $mailbox_display = ' <b>' . asearch_get_mailbox_display($cur_mailbox) . '</b>';
508 if ($query_display == '')
509 $biop_display = _("In");
510 else
511 $biop_display = $imap_asearch_biops_in[$biop];
512 $last_mailbox = $cur_mailbox;
513 }
514 else {
515 $mailbox_display = '';
516 $biop_display = $imap_asearch_biops[$biop];
517 }
518 $unop = $unop_array[$crit_num];
519 $unop_display = $imap_asearch_unops[$unop];
520 if ($unop_display != '')
521 $unop_display .= ' ';
522 $where = $where_array[$crit_num];
523 $where_display = $unop_display . asearch_nz($imap_asearch_options[$where], $where);
524 $what_type = $imap_asearch_opcodes[$where];
525 $what = $what_array[$crit_num];
526 if ($what_type) { /* Check opcode parameter */
527 if ($what == '')
528 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
529 else {
530 if ($what_type == 'adate')
531 $what_display = asearch_get_date_display($what);
532 else
533 $what_display = htmlspecialchars($what);
534 $what_display = ' <b>' . $what_display . '</b>';
535 }
536 }
537 else {
538 if ($what)
539 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
540 else
541 $what_display = '';
542 }
543 if ($mailbox_display != '')
544 $query_display .= ' <u><i>' . $biop_display . '</i></u>' . $mailbox_display . ' <u><i>' . $where_display . '</i></u>' . $what_display;
545 else
546 $query_display .= ' <u><i>' . $biop_display . ' ' . $where_display . '</i></u>' . $what_display;
547 }
548 }
549 return $query_display;
550 }
551
552 /** Handle the alternate row colors
553 * @return string color value
554 */
555 function asearch_get_row_color(&$color, $row_num)
556 {
557 /*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
558 $color_string = $color[4];
559 if ($GLOBALS['alt_index_colors']) {
560 if (($row_num % 2) == 0) {
561 if (!isset($color[12]))
562 $color[12] = '#EAEAEA';
563 $color_string = $color[12];
564 }
565 }
566 return $color_string;
567 }
568
569 /** Print a whole query array, recent or saved
570 */
571 function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref)
572 {
573 global $color;
574 global $data_dir, $username;
575 global $use_icons, $icon_theme;
576
577 $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1;
578 $use_icons_flag = ($use_icons) && ($icon_theme != 'none');
579 if ($use_icons_flag)
580 $text_array = array('<img src="' . SM_PATH . 'images/minus.png" border="0" height="7" width="7" />',
581 '<img src="' . SM_PATH . 'images/plus.png" border="0" height="7" width="7" />');
582 else
583 $text_array = array('-', '+');
584 $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, $text_array, array(_("Fold"), _("Unfold")));
585 if (!$use_icons_flag)
586 $toggle_link = '<small>[' . $toggle_link . ']</small>';
587
588 echo "<br />\n";
589 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
590 echo html_tag('tr',
591 html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"')
592 . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4'));
593 if ($show_flag) {
594 $main_key = $query_keys[ASEARCH_WHERE];
595 $query_count = count($query_array[$main_key]);
596 for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++) {
597 if (!empty($query_array[$main_key][$query_num])) {
598 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
599
600 unset($search_array);
601 foreach ($query_keys as $query_key) {
602 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
603 }
604 $where_array = $search_array[ASEARCH_WHERE];
605 $mailbox_array = $search_array[ASEARCH_MAILBOX];
606 $what_array = $search_array[ASEARCH_WHAT];
607 $unop_array = $search_array[ASEARCH_UNOP];
608 $biop_array = asearch_nz($search_array[ASEARCH_BIOP], array());
609 $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE], array());
610 $sub_array = asearch_nz($search_array[ASEARCH_SUB], array());
611 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
612
613 echo html_tag('td', $query_num + 1, 'right');
614 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
615 foreach ($action_array as $action => $action_display) {
616 echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&amp;rownum=' . $query_num) . '">' . $action_display . '</a>', 'center');
617 }
618
619 echo '</tr>' . "\n";
620 $row_num++;
621 }
622 }
623 }
624 echo '</table>' . "\n";
625 }
626
627 /** Print the saved array
628 */
629 function asearch_print_saved(&$boxes)
630 {
631 global $saved_prefkeys;
632
633 $saved_array = asearch_read_saved();
634 if (isset($saved_array[$saved_prefkeys[0]])) {
635 $saved_count = count($saved_array[$saved_prefkeys[0]]);
636 if ($saved_count > 0) {
637 $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
638 asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved');
639 }
640 }
641 }
642
643 /**
644 * Print the recent array
645 */
646 function asearch_print_recent(&$boxes)
647 {
648 global $recent_prefkeys, $search_memory;
649
650 $recent_array = asearch_read_recent();
651 if (isset($recent_array[$recent_prefkeys[0]])) {
652 $recent_count = count($recent_array[$recent_prefkeys[0]]);
653 if (min($recent_count, $search_memory) > 0) {
654 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
655 asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent');
656 }
657 }
658 }
659
660 /** Build an <option> statement
661 */
662 function asearch_opt($val, $sel, $tit)
663 {
664 return '<option value="' . $val . '"' . ($sel == $val ? ' selected="selected"' : '') . '>' . $tit . '</option>' . "\n";
665 }
666
667 /** Build a <select> statement from an array
668 */
669 function asearch_opt_array($var_name, $opt_array, $cur_val)
670 {
671 $output = '<select name="' . $var_name . '">' . "\n";
672 foreach($opt_array as $val => $display)
673 $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val));
674 $output .= '</select>' . "\n";
675 return $output;
676 }
677
678 /** Verify that a mailbox exists
679 * @return bool mailbox exists
680 */
681 function asearch_mailbox_exists($mailbox, &$boxes)
682 {
683 foreach ($boxes as $box) {
684 if ($box['unformatted'] == $mailbox)
685 return TRUE;
686 }
687 return FALSE;
688 }
689
690 /** Build the mailbox select
691 */
692 function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
693 {
694 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
695 $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
696 } else {
697 $missing = '';
698 }
699 return '<select name="mailbox[' . $row_num . ']">'
700 . $missing
701 . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']')
702 . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL)
703 . '</select>';
704 }
705
706 /** Build the Include subfolders checkbox
707 */
708 function asearch_get_form_sub($sub, $row_num = 0)
709 {
710 return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub)
711 : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />';
712 }
713
714 /** Build the 2 unop and where selects
715 */
716 function asearch_get_form_location($unop, $where, $row_num = 0)
717 {
718 global $imap_asearch_unops, $imap_asearch_options;
719
720 return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
721 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
722 }
723
724 /** Build the what text input
725 */
726 function asearch_get_form_what($what, $row_num = 0)
727 {
728 return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35')
729 : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />';
730 }
731
732 /** Build the Exclude criteria checkbox
733 */
734 function asearch_get_form_exclude($exclude, $row_num = 0)
735 {
736 return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude)
737 : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />';
738 }
739
740 /** Print one advanced form row
741 */
742 function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
743 {
744 global $imap_asearch_biops_in;
745 global $color;
746
747 echo html_tag('tr', '', '', $color[4]);
748
749 //Binary operator
750 echo html_tag('td', $row_num ?
751 asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
752 : '<b>' . _("In") . '</b>', 'center') . "\n";
753
754 //Mailbox list and Include Subfolders
755 echo html_tag('td',
756 asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
757 . _("and&nbsp;subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
758
759 //Unary operator and Search location
760 echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
761
762 //Text input
763 echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
764
765 //Exclude criteria
766 echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
767
768 echo "</tr>\n";
769 }
770
771 /** Print the advanced search form
772 */
773 function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
774 {
775 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
776 global $color;
777
778 //Search Form
779 echo "<br />\n";
780 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
781
782 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
783 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
784 $row_count = count($where_array);
785 for ($row_num = 0; $row_num < $row_count; $row_num++) {
786 $mailbox = asearch_nz($mailbox_array[$row_num]);
787 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
788 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
789 $where = strip_tags(asearch_nz($where_array[$row_num]));
790 $what = asearch_nz($what_array[$row_num]);
791 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
792 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
793 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
794 }
795 echo '</table>' . "\n";
796
797 //Submit buttons
798 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
799 echo html_tag('tr',
800 html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n"
801 . html_tag('td', getButton('submit', 'submit', $add_criteria_button_html), 'center') . "\n"
802 . html_tag('td', getButton('submit', 'submit', $del_all_button_html), 'center') . "\n"
803 . html_tag('td', getButton('submit', 'submit', $del_excluded_button_html), 'center') . "\n"
804 );
805 echo '</table>' . "\n";
806 echo '</form>' . "\n";
807 }
808
809 /** Print one basic form row
810 */
811 function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
812 {
813 global $search_button_html;
814 global $color;
815
816 echo html_tag('tr', '', '', $color[4]);
817
818 //Mailbox list
819 echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
820
821 //Unary operator and Search location
822 echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
823
824 //Text input
825 echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
826
827 //Submit button
828 echo html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n";
829
830 echo "</tr>\n";
831 }
832
833 /** Print the basic search form
834 */
835 function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
836 {
837 global $color;
838
839 //Search Form
840 echo "<br />\n";
841 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
842
843 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
844 //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4'));
845 $row_count = count($where_array);
846 for ($row_num = 0; $row_num < $row_count; $row_num++) {
847 $mailbox = asearch_nz($mailbox_array[$row_num]);
848 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
849 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
850 $where = strip_tags(asearch_nz($where_array[$row_num]));
851 $what = asearch_nz($what_array[$row_num]);
852 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
853 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
854 asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
855 }
856 echo '</table>' . "\n";
857 echo '</form>' . "\n";
858 }
859
860 /** Print the $msgs messages from $mailbox mailbox
861 */
862
863 function asearch_print_mailbox_msgs($imapConnection, &$aMailbox, $color) {
864 global $javascript_on, $compact_paginator;
865 /**
866 * A mailbox can contain different sets with uid's. Default, for normal
867 * message list view we use '0' as setindex and for search a different
868 * setindex.
869 */
870 $iSetIndx = $aMailbox['SETINDEX'];
871
872 $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
873 $mailbox_title = '<b><big>' . _("Folder:") . ' '. $mailbox_display . '&nbsp;</big></b>';
874
875 /**
876 * UIDSET contains the array with uid's returned by a search
877 */
878 $cnt = count($aMailbox['UIDSET'][$iSetIndx]);
879
880 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $cnt : $aMailbox['LIMIT'];
881 $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $cnt) ?
882 $aMailbox['PAGEOFFSET'] + ($iLimit - 1) : $cnt;
883
884 $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'],
885 $cnt, $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
886
887 if ($javascript_on && $compact_paginator) {
888 echo "\n<!-- start of compact paginator javascript -->\n"
889 . "<script language=\"JavaScript\">\n"
890 . "function SubmitOnSelect(select, URL)\n"
891 . "{\n"
892 . " URL += select.options[select.selectedIndex].value;\n"
893 . " window.location.href = URL;\n"
894 . "}\n"
895 . "</script>\n"
896 . "<!-- end of compact paginator javascript -->\n";
897 }
898
899 $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$cnt);
900
901 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
902
903 echo '<tr><td>';
904 mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $mailbox_title . " $paginator_str");
905 echo '</td></tr>';
906
907 echo '<tr><td height="5" bgcolor="'.$color[4].'"></td></tr>';
908
909 echo '<tr><td>';
910 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
911 echo ' <tr><td>';
912
913 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
914 echo ' <tr><td>';
915 printHeader($aMailbox);
916 displayMessageArray($imapConnection, $aMailbox);
917 echo ' </td></tr>';
918 echo ' </table>';
919 echo ' </td></tr>';
920 echo ' </table>';
921 mail_message_listing_end($cnt, '', $msg_cnt_str);
922 echo '</td></tr>';
923
924 echo '</table></form>';
925 }
926
927 /**
928 * @param array $boxes mailboxes array (reference)
929 * @return array selectable unformatted mailboxes names
930 */
931 function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
932 {
933 $mboxes_array = array();
934 $boxcount = count($boxes);
935 for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) {
936 if (!in_array('noselect', $boxes[$boxnum]['flags']))
937 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
938 }
939 return $mboxes_array;
940 }
941
942 /* ------------------------ main ------------------------ */
943 /* get globals we will need */
944 sqgetGlobalVar('username', $username, SQ_SESSION);
945 sqgetGlobalVar('key', $key, SQ_COOKIE);
946 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
947 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
948
949 if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) {
950 $checkall = (int) $temp;
951 }
952
953 /**
954 * Retrieve the mailbox cache from the session.
955 */
956 sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
957
958
959 $search_button_html = _("Search");
960 $search_button_text = asearch_unhtmlentities($search_button_html);
961 $add_criteria_button_html = _("Add New Criteria");
962 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
963 $del_excluded_button_html = _("Remove Excluded Criteria");
964 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
965 $del_all_button_html = _("Remove All Criteria");
966 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
967
968 /** Maximum number of recent searches to handle
969 * Default 0
970 * @global integer $search_memory
971 */
972 $search_memory = getPref($data_dir, $username, 'search_memory', 0);
973
974 /** Advanced search control
975 * - 0 => allow basic interface only
976 * - 1 => allow advanced interface only
977 * - 2 => allow both
978 * Default 2
979 */
980 $allow_advanced_search = asearch_nz($allow_advanced_search, 2);
981
982 /**
983 * Toggle advanced/basic search
984 */
985 if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET)) {
986 setPref($data_dir, $username, 'search_advanced', $search_advanced & 1);
987 }
988 /** If 1, show advanced search interface
989 * Default from allow_advanced_search pref
990 * @global integer $search_advanced
991 */
992 if ($allow_advanced_search > 1) {
993 $search_advanced = getPref($data_dir, $username, 'search_advanced', 0);
994 } else {
995 $search_advanced = $allow_advanced_search;
996 }
997 if ($search_advanced) {
998 /** Set recent prefkeys according to $search_advanced
999 * @global array $recent_prefkeys
1000 */
1001 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_unop', 'asearch_recent_biop', 'asearch_recent_exclude', 'asearch_recent_sub');
1002
1003 /** Set saved prefkeys according to $search_advanced
1004 * @global array $saved_prefkeys
1005 */
1006 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_unop', 'asearch_saved_biop', 'asearch_saved_exclude', 'asearch_saved_sub');
1007
1008 /*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
1009 } else {
1010 $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop');
1011 $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop');
1012 }
1013
1014 /** How we did enter the form
1015 * - unset : Enter key, or called from outside (eg read_body)
1016 * - $search_button_text : Search button
1017 * - 'Search_no_update' : Search but don't update recent
1018 * - 'Search_last' : Same as no_update but reload and search last
1019 * - 'Search_silent' : Same as no_update but only display results
1020 * - $add_criteria_button_text : Add New Criteria button
1021 * - $del_excluded_button_text : Remove Excluded Criteria button
1022 * - $del_all_button_text : Remove All Criteria button
1023 * - 'save_recent'
1024 * - 'search_recent'
1025 * - 'forget_recent'
1026 * - 'edit_saved'
1027 * - 'search_saved'
1028 * - 'delete_saved'
1029 * @global string $submit
1030 */
1031 if (isset($_GET['submit'])) {
1032 $submit = strip_tags($_GET['submit']);
1033 }
1034 /** Searched mailboxes
1035 * @global array $mailbox_array
1036 */
1037 if (isset($_GET['mailbox'])) {
1038 $mailbox_array = $_GET['mailbox'];
1039 $targetmailbox = $_GET['mailbox'];
1040 if (!is_array($mailbox_array)) {
1041 $mailbox_array = array($mailbox_array);
1042 }
1043 } else {
1044 $mailbox_array = array();
1045 $targetmailbox = array();
1046 }
1047 $aMailboxGlobalPref = array(
1048 MBX_PREF_SORT => 0,
1049 MBX_PREF_LIMIT => (int) $show_num,
1050 MBX_PREF_AUTO_EXPUNGE => (bool) $auto_expunge,
1051 MBX_PREF_INTERNALDATE => (bool) getPref($data_dir, $username, 'internal_date_sort')
1052 // MBX_PREF_FUTURE => (var) $future
1053 );
1054
1055 /**
1056 * system wide admin settings and incoming vars.
1057 */
1058 $aConfig = array(
1059 'allow_thread_sort' => $allow_thread_sort,
1060 'allow_server_sort' => $allow_server_sort,
1061 'user' => $username,
1062 'max_cache_size'
1063 );
1064
1065 /** Binary operators
1066 * @global array $biop_array
1067 */
1068 if (isset($_GET['biop'])) {
1069 $biop_array = $_GET['biop'];
1070 if (!is_array($biop_array))
1071 $biop_array = array($biop_array);
1072 } else {
1073 $biop_array = array();
1074 }
1075 /** Unary operators
1076 * @global array $unop_array
1077 */
1078 if (isset($_GET['unop'])) {
1079 $unop_array = $_GET['unop'];
1080 if (!is_array($unop_array))
1081 $unop_array = array($unop_array);
1082 } else {
1083 $unop_array = array();
1084 }
1085 /** Where to search
1086 * @global array $where_array
1087 */
1088 if (isset($_GET['where'])) {
1089 $where_array = $_GET['where'];
1090 if (!is_array($where_array)) {
1091 $where_array = array($where_array);
1092 }
1093 } else {
1094 $where_array = array();
1095 }
1096 /** What to search
1097 * @global array $what_array
1098 */
1099 if (isset($_GET['what'])) {
1100 $what_array = $_GET['what'];
1101 if (!is_array($what_array)) {
1102 $what_array = array($what_array);
1103 }
1104 } else {
1105 $what_array = array();
1106 }
1107 /** Whether to exclude this criteria from search
1108 * @global array $exclude_array
1109 */
1110 if (isset($_GET['exclude'])) {
1111 $exclude_array = $_GET['exclude'];
1112 } else {
1113 $exclude_array = array();
1114 }
1115 /** Search within subfolders
1116 * @global array $sub_array
1117 */
1118 if (isset($_GET['sub'])) {
1119 $sub_array = $_GET['sub'];
1120 } else {
1121 $sub_array = array();
1122 }
1123 /** Row number used by recent and saved stuff
1124 */
1125 if (isset($_GET['rownum'])) {
1126 $submit_rownum = strip_tags($_GET['rownum']);
1127 }
1128 /** Change global sort
1129 */
1130 if (sqgetGlobalVar('srt', $temp, SQ_GET)) {
1131 $srt = (int) $temp;
1132 asearch_edit_last(1);
1133 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1134 }
1135 if (sqgetGlobalVar('startMessage', $temp, SQ_GET)) {
1136 $startMessage = (int) $temp;
1137 asearch_edit_last(1);
1138 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1139 }
1140
1141 if ( sqgetGlobalVar('showall', $temp, SQ_GET) ) {
1142 $showall = (int) $temp;
1143 asearch_edit_last(1);
1144 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1145 }
1146 /**
1147 * Incoming submit buttons from the message list with search results
1148 */
1149 if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST) ||
1150 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST) ||
1151 sqgetGlobalVar('delete', $markDelete, SQ_POST) ||
1152 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST) ||
1153 sqgetGlobalVar('markRead', $markRead, SQ_POST) ||
1154 sqgetGlobalVar('markUnread', $markUnread, SQ_POST) ||
1155 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST) ||
1156 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST) ||
1157 sqgetGlobalVar('attache', $attache, SQ_POST)) {
1158 asearch_edit_last(1);
1159 $submit = '';
1160 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1161 }
1162
1163
1164
1165 /** Toggle show/hide saved searches
1166 */
1167 if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) {
1168 setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
1169 }
1170 /** Toggle show/hide recent searches
1171 */
1172 if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) {
1173 setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
1174 }
1175 // end of get globals
1176
1177 /** If TRUE, do not show search interface
1178 * Default FALSE
1179 * @global bool $search_silent
1180 */
1181 $search_silent = FALSE;
1182
1183 /* See how the page was called and fire off correct function */
1184 if ((empty($submit)) && (!empty($where_array))) {
1185 /* This happens when the Enter key is used or called from outside */
1186 $submit = $search_button_text;
1187 /* Hack needed to handle coming back from read_body et als */
1188 if (count($where_array) != count($unop_array)) {
1189 /**
1190 * Hack to use already existen where and what vars.
1191 * where now contains the initiator page of the messagelist
1192 * and in this case 'search'. what contains an index to access
1193 * the search history
1194 */
1195
1196 sqgetGlobalVar('what',$what,SQ_GET);
1197 asearch_edit_last($what);
1198 }
1199 }
1200
1201 if (!isset($submit)) {
1202 $submit = '';
1203 } else {
1204 switch ($submit) {
1205 case $search_button_text:
1206 if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
1207 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1208 }
1209 break;
1210 case 'Search_silent':
1211 $search_silent = TRUE;
1212 /*nobreak;*/
1213 case 'Search_no_update':
1214 $submit = $search_button_text;
1215 break;
1216 case $del_excluded_button_text:
1217 $delarray = array_keys($exclude_array);
1218 while (!empty($delarray)) {
1219 $delrow = array_pop($delarray);
1220 array_splice($mailbox_array, $delrow, 1);
1221 array_splice($biop_array, $delrow, 1);
1222 array_splice($unop_array, $delrow, 1);
1223 array_splice($where_array, $delrow, 1);
1224 array_splice($what_array, $delrow, 1);
1225 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
1226 array_splice($sub_array, $delrow, 1);
1227 }
1228 $exclude_array = array();
1229 break;
1230 case $del_all_button_text:
1231 $mailbox_array = array();
1232 $biop_array = array();
1233 $unop_array = array();
1234 $where_array = array();
1235 $what_array = array();
1236 $exclude_array = array();
1237 $sub_array = array();
1238 break;
1239 case 'save_recent':
1240 asearch_save_recent($submit_rownum);
1241 break;
1242 case 'search_recent':
1243 $submit = $search_button_text;
1244 asearch_edit_recent($submit_rownum);
1245 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1246 break;
1247 case 'edit_recent': /* no link to do this, yet */
1248 asearch_edit_recent($submit_rownum);
1249 break;
1250 case 'forget_recent':
1251 asearch_forget_recent($submit_rownum);
1252 break;
1253 case 'search_saved':
1254 $submit = $search_button_text;
1255 asearch_edit_saved($submit_rownum);
1256 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1257 break;
1258 case 'edit_saved':
1259 asearch_edit_saved($submit_rownum);
1260 break;
1261 case 'delete_saved':
1262 asearch_delete_saved($submit_rownum);
1263 break;
1264 }
1265 }
1266
1267 //Texts in both basic and advanced form
1268 $imap_asearch_unops = array(
1269 '' => '',
1270 'NOT' => _("Not")
1271 );
1272
1273 if ($search_advanced) {
1274 //Texts in advanced form only
1275 $imap_asearch_options = array(
1276 //<message set>,
1277 //'ALL' is binary operator
1278 'ANSWERED' => _("Answered"),
1279 'BCC' => _("Bcc"),
1280 'BEFORE' => _("Before"),
1281 'BODY' => _("Message Body"),
1282 'CC' => _("Cc"),
1283 'DELETED' => _("Deleted"),
1284 'DRAFT' => _("Draft"),
1285 'FLAGGED' => _("Flagged"),
1286 'FROM' => _("Sent By"),
1287 'HEADER' => _("Header Field"),
1288 'KEYWORD' => _("Keyword"),
1289 'LARGER' => _("Larger Than"),
1290 'NEW' => _("New"),
1291 //'NOT' is unary operator
1292 'OLD' => _("Old"),
1293 'ON' => _("On"),
1294 //'OR' is binary operator
1295 'RECENT' => _("Recent"),
1296 'SEEN' => _("Seen"),
1297 'SENTBEFORE' => _("Sent Before"),
1298 'SENTON' => _("Sent On"),
1299 'SENTSINCE' => _("Sent Since"),
1300 'SINCE' => _("Since"),
1301 'SMALLER' => _("Smaller Than"),
1302 'SUBJECT' => _("Subject Contains"),
1303 'TEXT' => _("Header and Body"),
1304 'TO' => _("Sent To"),
1305 //'UID' => 'anum',
1306 /* 'UNANSWERED' => '',
1307 'UNDELETED' => '',
1308 'UNDRAFT' => '',
1309 'UNFLAGGED' => '',
1310 'UNKEYWORD' => _("Unkeyword"),
1311 'UNSEEN' => _("Unseen"),*/
1312 );
1313
1314 $imap_asearch_biops_in = array(
1315 'ALL' => _("And In"),
1316 'OR' => _("Or In")
1317 );
1318
1319 $imap_asearch_biops = array(
1320 'ALL' => _("And"),
1321 'OR' => _("Or")
1322 );
1323 } else {
1324 //Texts in basic form only
1325 $imap_asearch_options = array(
1326 'BCC' => _("Bcc"),
1327 'BODY' => _("Body"),
1328 'CC' => _("Cc"),
1329 'FROM' => _("From"),
1330 'SUBJECT' => _("Subject"),
1331 'TEXT' => _("Everywhere"),
1332 'TO' => _("To"),
1333 );
1334 }
1335
1336 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
1337
1338 /* open IMAP connection */
1339 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
1340
1341
1342 /* get mailboxes once here */
1343 $boxes = sqimap_mailbox_list($imapConnection);
1344 /* ensure we have a valid default mailbox name */
1345 $mailbox = asearch_nz($mailbox_array[0]);
1346 if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
1347 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
1348
1349 if (isset($composenew) && $composenew) {
1350 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox)
1351 . "&amp;session=$composesession&amp;attachedmessages=true&amp";
1352 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
1353 } else {
1354 displayPageHeader($color, $mailbox);
1355 }
1356 do_hook('search_before_form');
1357
1358 if (!$search_silent) {
1359 //Add a link to the other search mode if allowed
1360 if ($allow_advanced_search > 1)
1361 $toggle_link = ' - <small>['
1362 . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
1363 . ']</small>';
1364 else
1365 $toggle_link = '';
1366
1367 echo html_tag('table',
1368 html_tag('tr', "\n"
1369 . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
1370 ) ,
1371 '', '', 'width="100%"') . "\n";
1372 asearch_print_saved($boxes);
1373 asearch_print_recent($boxes);
1374 if (empty($where_array)) {
1375 global $sent_folder;
1376
1377 $mailbox_array[0] = $mailbox;
1378 $biop_array[0] = '';
1379 $unop_array[0] = '';
1380 if ($mailbox == $sent_folder) {
1381 $where_array[0] = 'TO';
1382 } else {
1383 $where_array[0] = 'FROM';
1384 }
1385 $what_array[0] = '';
1386 $exclude_array[0] = '';
1387 $sub_array[0] = '';
1388 }
1389 //Display advanced or basic form
1390 if ($search_advanced) {
1391 if ($submit == $add_criteria_button_text) {
1392 $last_index = max(count($where_array) - 1, 0);
1393 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1394 $biop_array[] = asearch_nz($biop_array[$last_index]);
1395 $unop_array[] = asearch_nz($unop_array[$last_index]);
1396 $where_array[] = asearch_nz($where_array[$last_index]);
1397 $what_array[] = asearch_nz($what_array[$last_index]);
1398 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1399 $sub_array[] = asearch_nz($sub_array[$last_index]);
1400 }
1401 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1402 } else {
1403 asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1404 }
1405 }
1406
1407 do_hook('search_after_form');
1408
1409 if ($submit == $search_button_text) {
1410 $msgsfound = false;
1411 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1412 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1413 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]));
1414 echo '</table>' . "\n";
1415
1416 flush();
1417
1418 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
1419 if ($query_error != '') {
1420 echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
1421 } else {
1422 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
1423 /**
1424 * Retrieve the search queries
1425 */
1426 $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1427 foreach($mboxes_mailbox as $mbx => $search) {
1428 $aMailboxPrefSer=getPref($data_dir, $username, "pref_$mbx");
1429
1430 if ($aMailboxPrefSer) {
1431 $aMailboxPref = unserialize($aMailboxPrefSer);
1432 } else {
1433 setUserPref($username,"pref_$mbx",serialize($aMailboxGlobalPref));
1434 $aMailboxPref = $aMailboxGlobalPref;
1435 }
1436 if (isset($srt) && $targetmailbox == $mbx) {
1437 $aMailboxPref[MBX_PREF_SORT] = (int) $srt;
1438 }
1439 if (isset($startMessage) && $targetmailbox == $mbx) {
1440 $aConfig['offset'] = $startMessage;
1441 } else if (isset($aConfig['offset'])) {
1442 unset($aConfig['offset']);
1443 }
1444 if (isset($showall) && $targetmailbox == $mbx) {
1445 $aConfig['showall'] = $showall;
1446 } else if (isset($aConfig['showall'])) {
1447 unset($aConfig['showall']);
1448 }
1449 /**
1450 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1451 * when searching all mailboxes
1452 */
1453 $aConfig['max_cache_size'] = count($mboxes_mailbox) +1;
1454
1455 /**
1456 * until there is no per mailbox option screen to set prefs we override
1457 * the mailboxprefs by the default ones
1458 */
1459 $aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num;
1460 $aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge;
1461 $aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort');
1462
1463 $aConfig['search'] = $search['search'];
1464 $aConfig['charset'] = $search['charset'];
1465 $aConfig['setindex'] = 1; // $what $where = 'search'
1466
1467 $aMailbox = sqm_api_mailbox_select($imapConnection,$mbx,$aConfig,$aMailboxPref);
1468 /**
1469 * Handle form actions like flag / unflag, seen / unseen, delete
1470 */
1471 if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST)) {
1472 if ($postMailbox === $mbx) {
1473 handleMessageListForm($imapConnection,$aMailbox);
1474 }
1475 }
1476 if (fetchMessageHeaders($imapConnection, $aMailbox)) {
1477 $msgsfound = true;
1478 echo '<br />';
1479 asearch_print_mailbox_msgs($imapConnection, $aMailbox, $color);
1480 flush();
1481 }
1482 /* add the mailbox to the cache */
1483 $mailbox_cache[$aMailbox['NAME']] = $aMailbox;
1484
1485 }
1486 }
1487 if(!$msgsfound) {
1488 echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1489 }
1490 }
1491
1492 do_hook('search_bottom');
1493 sqimap_logout($imapConnection);
1494 echo '</body></html>';
1495 sqsession_register($mailbox_cache,'mailbox_cache');
1496 ?>