8 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
10 * @author Alex Lemaresquier - Brainstorm <alex at brainstorm.fr>
11 * @copyright © 1999-2006 The SquirrelMail Project Team
12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 * @package squirrelmail
16 * @link http://www.ietf.org/rfc/rfc3501.txt
17 * @todo explain why references are used in function calls
21 * Include the SquirrelMail initialization file.
23 require('../include/init.php');
25 /** SquirrelMail required files.
27 require_once(SM_PATH
. 'functions/imap_asearch.php');
28 require_once(SM_PATH
. 'functions/imap_messages.php');
29 require_once(SM_PATH
. 'functions/imap_general.php');
30 require_once(SM_PATH
. 'functions/mime.php');
31 require_once(SM_PATH
. 'functions/mailbox_display.php'); //getButton()
32 require_once(SM_PATH
. 'functions/forms.php');
33 require_once(SM_PATH
. 'functions/date.php');
35 /** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys
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);
46 /** Name of session var
48 define('ASEARCH_CRITERIA', 'criteria');
50 /** Builds a href with params
51 * @param string $params optional parameters to GET
55 function asearch_get_href($params = '')
59 $href .= '?' . $params;
64 * @param string $href (reference) URL
65 * @param string $text link name
66 * @param string $title link title attribute value
70 function asearch_get_link(&$href, $text, $title = '')
73 $title = ' title="' . $title . '"';
74 return '<a href="' . $href . '"' . $title . '>' . $text . '</a>';
77 /** Builds a toggle [link]
78 * @param integer $value
79 * @param string $action
80 * @param array $text_array
81 * @param array $title_array
85 function asearch_get_toggle_link($value, $action, $text_array, $title_array = array())
87 $asearch_nz=asearch_nz($title_array[$value]);
88 $asearch_get_href=asearch_get_href($action . '=' . (int)$value);
89 return asearch_get_link($asearch_get_href, $text_array[$value], $asearch_nz);
93 * Array sort callback used to sort $imap_asearch_options
96 * @return bool strcoll()-like result
100 function asearch_unhtml_strcoll($a, $b)
102 // FIXME: Translation policy says "no html entities in translations"
103 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
108 * @param string $mailbox mailbox name utf7 encoded inc. special case INBOX
109 * @return string mailbox name ready to display (utf7 decoded or localized INBOX)
113 function imap_get_mailbox_display($mailbox)
115 if (strtoupper($mailbox) == 'INBOX')
117 return imap_utf7_decode_local($mailbox);
121 * @param string $mailbox mailbox name or special case 'All Folders'
122 * @return string mailbox name ready to display (utf7 decoded or localized 'All Folders')
126 function asearch_get_mailbox_display($mailbox)
128 if ($mailbox == 'All Folders')
129 return _("All Folders");
130 return imap_get_mailbox_display($mailbox);
134 * @param array $color color array (unused)
135 * @param string $txt text to display
136 * @return string title ready to display
140 function asearch_get_title_display($color, $txt)
142 return '<b><big>' . $txt . '</big></b>';
145 * @param array $color color array
146 * @param string $txt text to display
147 * @return string error text ready to display
151 function asearch_get_error_display($color, $txt)
153 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
157 * @param array $input_array array to serialize
158 * @return string a string containing a byte-stream representation of value that can be stored anywhere
162 function asearch_serialize(&$input_array)
164 global $search_advanced;
165 if ($search_advanced)
166 return serialize($input_array);
167 return $input_array[0];
171 * @param string $input_string string to unserialize
176 function asearch_unserialize($input_string)
178 global $search_advanced;
179 if ($search_advanced)
180 return unserialize($input_string);
181 return array($input_string);
185 * Gets user's advanced search preferences
187 * Arguments are different in 1.5.0.
188 * @param string $key the pref key
189 * @param integer $index the pref key index
190 * @param string $default default value
191 * @return string pref value
195 function asearch_getPref(&$key, $index, $default = '')
197 global $data_dir, $username, $search_advanced;
198 return getPref($data_dir, $username, $key . ($index +
!$search_advanced), $default);
202 * Sets user's advanced search preferences
204 * Arguments are different in 1.5.0.
205 * @param string $key the pref key
206 * @param integer $index the pref key index
207 * @param string $value pref value to set
208 * @return bool status
212 function asearch_setPref(&$key, $index, $value)
214 global $data_dir, $username, $search_advanced;
215 return setPref($data_dir, $username, $key . ($index +
!$search_advanced), $value);
219 * Deletes user's advanced search preferences
221 * Arguments are different in 1.5.0.
222 * @param string $key the pref key
223 * @param integer $index the pref key index
224 * @return bool status
228 function asearch_removePref(&$key, $index)
230 global $data_dir, $username, $search_advanced;
231 return removePref($data_dir, $username, $key . ($index +
!$search_advanced));
235 * Sanity checks, done before running the imap command and before calling push_recent
236 * @param array $where_array search location data
237 * @param array $what_array search criteria data
238 * @param array $exclude_array excluded criteria data
239 * @return string error message or empty string
243 function asearch_check_query(&$where_array, &$what_array, &$exclude_array)
245 global $imap_asearch_opcodes;
247 if (empty($where_array))
248 return _("Please enter something to search for");
249 if (count($exclude_array) == count($where_array))
250 return _("There must be at least one criteria to search for");
251 for ($crit_num = 0; $crit_num < count($where_array); $crit_num++
) {
252 $where = $where_array[$crit_num];
253 $what = $what_array[$crit_num];
254 if (!(($what == '') ^
($imap_asearch_opcodes[$where] != '')))
255 return _("Error in criteria argument");
261 * Read the recent searches from the prefs
263 * Function arguments are different in 1.5.0
264 * @return array recent searches
268 function asearch_read_recent()
270 global $recent_prefkeys, $search_memory;
272 $recent_array = array();
274 for ($pref_num = 0; $pref_num < $search_memory; $pref_num++
) {
275 foreach ($recent_prefkeys as $prefkey) {
276 $pref = asearch_getPref($prefkey, $pref_num);
277 /* if (!empty($pref))*/
278 $recent_array[$prefkey][$recent_num] = $pref;
280 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
281 foreach ($recent_prefkeys as $key) {
282 array_pop($recent_array[$key]);
284 // break; //Disabled to support old search code broken prefs
289 return $recent_array;
293 * Read the saved searches from the prefs
295 * Function arguments are different in 1.5.0
296 * @return array saved searches
300 function asearch_read_saved()
302 global $saved_prefkeys;
304 $saved_array = array();
305 $saved_key = $saved_prefkeys[0];
306 for ($saved_count = 0; ; $saved_count++
) {
307 $pref = asearch_getPref($saved_key, $saved_count);
311 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++
) {
312 foreach ($saved_prefkeys as $key) {
313 $saved_array[$key][$saved_num] = asearch_getPref($key, $saved_num);
320 * Save a recent search to the prefs
322 * Function arguments are different in 1.5.0
323 * @param integer $recent_index
327 function asearch_save_recent($recent_index)
329 global $recent_prefkeys, $saved_prefkeys;
331 $saved_array = asearch_read_saved();
332 if (isset($saved_array[$saved_prefkeys[0]])) {
333 $saved_index = count($saved_array[$saved_prefkeys[0]]);
337 $recent_array = asearch_read_recent();
339 foreach ($recent_prefkeys as $key) {
340 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
341 if (!empty($recent_slice[0]))
342 asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]);
344 asearch_removePref($saved_prefkeys[$n], $saved_index);
350 * Write a recent search to prefs
352 * Function arguments are different in 1.5.0
353 * @param array $recent_array
357 function asearch_write_recent(&$recent_array)
359 global $recent_prefkeys, $search_memory;
361 $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]]));
362 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++
) {
363 foreach ($recent_prefkeys as $key) {
364 asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]);
367 for (; $recent_num < $search_memory; $recent_num++
) {
368 foreach ($recent_prefkeys as $key) {
369 asearch_removePref($key, $recent_num);
375 * Remove a recent search from prefs
377 * Function arguments are different in 1.5.0
378 * @param integer $forget_index removed search number
382 function asearch_forget_recent($forget_index)
384 global $recent_prefkeys;
386 $recent_array = asearch_read_recent();
387 foreach ($recent_prefkeys as $key) {
388 array_splice($recent_array[$key], $forget_index, 1);
390 asearch_write_recent($recent_array);
394 * Find a recent search in the prefs (used to avoid saving duplicates)
395 * @param array $recent_array
396 * @param array $mailbox_array
397 * @param array $biop_array
398 * @param array $unop_array
399 * @param array $where_array
400 * @param array $what_array
401 * @param array $exclude_array
402 * @param array $sub_array
407 function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
409 global $recent_prefkeys, $search_advanced;
411 $where_string = asearch_serialize($where_array);
412 $mailbox_string = asearch_serialize($mailbox_array);
413 $what_string = asearch_serialize($what_array);
414 $unop_string = asearch_serialize($unop_array);
415 if ($search_advanced) {
416 $biop_string = asearch_serialize($biop_array);
417 $exclude_string = asearch_serialize($exclude_array);
418 $sub_string = asearch_serialize($sub_array);
420 $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE
]]);
421 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++
) {
422 if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE
]][$recent_num])) {
424 $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE
]][$recent_num] &&
425 $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX
]][$recent_num] &&
426 $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT
]][$recent_num] &&
427 $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP
]][$recent_num] &&
428 ((!$search_advanced) ||
429 ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP
]][$recent_num] &&
430 $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE
]][$recent_num] &&
431 $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB
]][$recent_num]))
440 * Push a recent search into the prefs
441 * @param array $recent_array
442 * @param array $mailbox_array
443 * @param array $biop_array
444 * @param array $unop_array
445 * @param array $where_array
446 * @param array $what_array
447 * @param array $exclude_array
448 * @param array $sub_array
452 function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
454 global $recent_prefkeys, $search_memory;
455 //global $what; // Hack to access issued search from read_body.php
458 * Update search history and store it in the session so we can retrieve the
459 * issued search when returning from an external page like read_body.php
461 $criteria[$what] = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
462 sqsession_register($criteria, ASEARCH_CRITERIA
);
463 if ($search_memory > 0) {
464 $recent_array = asearch_read_recent();
465 $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
466 if ($recent_found >= 0) { // Remove identical recent
467 foreach ($recent_prefkeys as $key) {
468 array_splice($recent_array[$key], $recent_found, 1);
471 $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array);
473 foreach ($recent_prefkeys as $key) {
474 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
477 asearch_write_recent($recent_array);
482 * Edit a recent search
484 * Function arguments are different in 1.5.0
485 * @global array mailbox_array searched mailboxes
486 * @param mixed $index
490 function asearch_edit_recent($index)
492 global $recent_prefkeys, $search_advanced;
493 global $where_array, $mailbox_array, $what_array, $unop_array;
494 global $biop_array, $exclude_array, $sub_array;
496 $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE
], $index));
497 $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX
], $index));
498 $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT
], $index));
499 $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP
], $index));
500 if ($search_advanced) {
501 $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP
], $index));
502 $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE
], $index));
503 $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB
], $index));
508 * Get last search criteria from session or prefs
510 * Function arguments are different in 1.5.0
511 * FIXME, try to avoid globals
512 * @param mixed $index
516 function asearch_edit_last($index) {
517 if (sqGetGlobalVar(ASEARCH_CRITERIA
, $criteria, SQ_SESSION
)) {
518 global $where_array, $mailbox_array, $what_array, $unop_array;
519 global $biop_array, $exclude_array, $sub_array;
520 $mailbox_array = $criteria[$index][0];
521 $biop_array = $criteria[$index][1];
522 $unop_array = $criteria[$index][2];
523 $where_array = $criteria[$index][3];
524 $what_array = $criteria[$index][4];
525 $exclude_array = $criteria[$index][5];
526 $sub_array = $criteria[$index][6];
527 unset($criteria[$index]);
528 //sqsession_unregister(ASEARCH_CRITERIA);
530 global $search_memory;
531 if ($search_memory > 0) {
532 asearch_edit_recent(0);
538 * Edit a saved search
540 * Function arguments are different in 1.5.0
541 * @param mixed $index
545 function asearch_edit_saved($index)
547 global $saved_prefkeys, $search_advanced;
548 global $where_array, $mailbox_array, $what_array, $unop_array;
549 global $biop_array, $exclude_array, $sub_array;
551 $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE
], $index));
552 $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX
], $index));
553 $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT
], $index));
554 $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP
], $index));
555 if ($search_advanced) {
556 $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP
], $index));
557 $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE
], $index));
558 $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB
], $index));
563 * Write a saved search to the prefs
565 * Function arguments are different in 1.5.0
566 * @param array $saved_array
570 function asearch_write_saved(&$saved_array)
572 global $saved_prefkeys;
574 $saved_count = count($saved_array[$saved_prefkeys[0]]);
575 for ($saved_num=0; $saved_num < $saved_count; $saved_num++
) {
576 foreach ($saved_prefkeys as $key) {
577 asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]);
580 foreach ($saved_prefkeys as $key) {
581 asearch_removePref($key, $saved_count);
586 * Delete a saved search from the prefs
588 * Function arguments are different in 1.5.0
589 * @param integer $saved_index
593 function asearch_delete_saved($saved_index)
595 global $saved_prefkeys;
597 $saved_array = asearch_read_saved();
598 $asearch_keys = $saved_prefkeys;
599 foreach ($asearch_keys as $key) {
600 array_splice($saved_array[$key], $saved_index, 1);
602 asearch_write_saved($saved_array);
605 /** Translate the input date to imap date to local date display,
606 * so the user can know if the date is wrong or illegal
607 * @param string $what date string
608 * @return string locally formatted date or error text
612 function asearch_get_date_display(&$what)
614 $what_parts = sqimap_asearch_parse_date($what);
615 if (count($what_parts) == 4) {
616 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3]))
617 return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
618 //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];
619 return _("(Illegal date)");
621 return _("(Wrong date)");
625 * Translate the query to rough natural display
626 * @param array $color
627 * @param array $mailbox_array
628 * @param array $biop_array
629 * @param array $unop_array
630 * @param array $where_array
631 * @param array $what_array
632 * @param array $exclude_array
633 * @param array $sub_array
634 * @return string rough natural query ready to display
638 function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
640 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
641 global $imap_asearch_opcodes;
643 $last_mailbox = $mailbox_array[0];
644 if (empty($last_mailbox))
645 $last_mailbox = 'INBOX';
647 for ($crit_num=0; $crit_num < count($where_array); $crit_num++
) {
648 if ((!isset($exclude_array[$crit_num])) ||
(!$exclude_array[$crit_num])) {
649 $cur_mailbox = $mailbox_array[$crit_num];
650 if (empty($cur_mailbox))
651 $cur_mailbox = 'INBOX';
652 $biop = asearch_nz($biop_array[$crit_num]);
653 if (($query_display == '') ||
($cur_mailbox != $last_mailbox)) {
654 $mailbox_display = ' <b>' . htmlspecialchars(asearch_get_mailbox_display($cur_mailbox)) . '</b>';
655 if ($query_display == '')
656 $biop_display = _("In");
658 $biop_display = $imap_asearch_biops_in[$biop];
659 $last_mailbox = $cur_mailbox;
662 $mailbox_display = '';
663 $biop_display = $imap_asearch_biops[$biop];
665 $unop = $unop_array[$crit_num];
666 $unop_display = $imap_asearch_unops[$unop];
667 if ($unop_display != '')
668 $unop_display .= ' ';
669 $where = $where_array[$crit_num];
670 $where_display = $unop_display . asearch_nz($imap_asearch_options[$where], $where);
671 $what_type = $imap_asearch_opcodes[$where];
672 $what = $what_array[$crit_num];
673 if ($what_type) { /* Check opcode parameter */
675 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
677 if ($what_type == 'adate')
678 $what_display = asearch_get_date_display($what);
680 $what_display = htmlspecialchars($what);
681 $what_display = ' <b>' . $what_display . '</b>';
686 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
690 if ($mailbox_display != '')
691 $query_display .= ' <u><i>' . $biop_display . '</i></u>' . $mailbox_display . ' <u><i>' . $where_display . '</i></u>' . $what_display;
693 $query_display .= ' <u><i>' . $biop_display . ' ' . $where_display . '</i></u>' . $what_display;
696 return $query_display;
702 * @deprecated see form functions available in 1.5.1 and 1.4.3.
703 * @param string $type
704 * @param string $name
705 * @param string $value
707 * @param bool $enabled
709 function getButton($type, $name, $value, $js = '', $enabled = TRUE) {
710 $disabled = ( $enabled ?
'' : 'disabled ' );
711 $js = ( $js ?
$js.' ' : '' );
712 return '<input '.$disabled.$js.
716 '" style="padding: 0px; margin: 0px" />';
720 /** Handle the alternate row colors
721 * @param array $color color theme array
722 * @param integer $row_num
723 * @return string color value
727 function asearch_get_row_color(&$color, $row_num)
729 /*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
730 $color_string = $color[4];
731 if ($GLOBALS['alt_index_colors']) {
732 if (($row_num %
2) == 0) {
733 if (!isset($color[12]))
734 $color[12] = '#EAEAEA';
735 $color_string = $color[12];
738 return $color_string;
742 * Print a whole query array, recent or saved
744 * Function arguments are different in 1.5.0
745 * @param array $boxes (unused)
746 * @param array $query_array
747 * @param mixed $query_keys
748 * @param array $action_array
749 * @param mixed $title
750 * @param string $show_pref
754 function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref)
757 global $data_dir, $username;
758 global $use_icons, $icon_theme;
760 $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1;
761 $use_icons_flag = ($use_icons) && ($icon_theme != 'none');
763 $text_array = array('<img src="' . SM_PATH
. 'images/minus.png" border="0" height="7" width="7" />',
764 '<img src="' . SM_PATH
. 'images/plus.png" border="0" height="7" width="7" />');
766 $text_array = array('-', '+');
767 $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, $text_array, array(_("Fold"), _("Unfold")));
768 if (!$use_icons_flag)
769 $toggle_link = '<small>[' . $toggle_link . ']</small>';
772 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
774 html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"')
775 . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4'));
777 $main_key = $query_keys[ASEARCH_WHERE
];
778 $query_count = count($query_array[$main_key]);
779 for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++
) {
780 if (!empty($query_array[$main_key][$query_num])) {
781 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
783 unset($search_array);
784 foreach ($query_keys as $query_key) {
785 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
787 $where_array = $search_array[ASEARCH_WHERE
];
788 $mailbox_array = $search_array[ASEARCH_MAILBOX
];
789 $what_array = $search_array[ASEARCH_WHAT
];
790 $unop_array = $search_array[ASEARCH_UNOP
];
791 $biop_array = asearch_nz($search_array[ASEARCH_BIOP
], array());
792 $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE
], array());
793 $sub_array = asearch_nz($search_array[ASEARCH_SUB
], array());
794 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
796 echo html_tag('td', $query_num +
1, 'right');
797 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
798 foreach ($action_array as $action => $action_display) {
799 echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&rownum=' . $query_num) . '">' . $action_display . '</a>', 'center');
807 echo '</table>' . "\n";
810 /** Print the saved array
812 * Function arguments are different in 1.5.0
813 * @param array $boxes (unused, see asearch_print_query_array())
817 function asearch_print_saved(&$boxes)
819 global $saved_prefkeys;
821 $saved_array = asearch_read_saved();
822 if (isset($saved_array[$saved_prefkeys[0]])) {
823 $saved_count = count($saved_array[$saved_prefkeys[0]]);
824 if ($saved_count > 0) {
825 $saved_actions = array('edit_saved' => _("Edit"), 'search_saved' => _("Search"), 'delete_saved' => _("Delete"));
826 asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved');
832 * Print the recent array
834 * Function arguments are different in 1.5.0
835 * @param array $boxes (unused, see asearch_print_query_array())
839 function asearch_print_recent(&$boxes)
841 global $recent_prefkeys, $search_memory;
843 $recent_array = asearch_read_recent();
844 if (isset($recent_array[$recent_prefkeys[0]])) {
845 $recent_count = count($recent_array[$recent_prefkeys[0]]);
846 if (min($recent_count, $search_memory) > 0) {
847 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
848 asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent');
854 * Build an <option> statement
855 * @param string $var option value
856 * @param string $set default option value
857 * @param string $tit displayed option name
858 * @return string formated html option tag
862 function asearch_opt($val, $sel, $tit)
864 return '<option value="' . $val . '"' . ($sel == $val ?
' selected="selected"' : '') . '>' . $tit . '</option>' . "\n";
867 /** Build a <select> statement from an array
868 * @param string $var_name select option name
869 * @param array $opt_array select option values
870 * @param string $cur_val default value
871 * @return string html formated select tag
872 * @deprecated FIXME use standard form functions or
873 * explain why they are not used.
877 function asearch_opt_array($var_name, $opt_array, $cur_val)
879 $output = '<select name="' . $var_name . '">' . "\n";
880 foreach($opt_array as $val => $display)
881 $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val));
882 $output .= '</select>' . "\n";
886 /** Verify that a mailbox exists
887 * @param string $mailbox
888 * @param array $boxes
889 * @return bool mailbox exists
890 * @deprecated FIXME use standard functions
894 function asearch_mailbox_exists($mailbox, &$boxes)
896 foreach ($boxes as $box) {
897 if ($box['unformatted'] == $mailbox)
904 * Build the mailbox select
905 * @param stream $imapConnection
906 * @param array $boxes
907 * @param string $mailbox
908 * @param integer $row_num
912 function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
914 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
915 $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . htmlspecialchars(asearch_get_mailbox_display($mailbox)));
919 return '<select name="mailbox[' . $row_num . ']">'
921 . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']')
922 . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL)
927 * Build the Include subfolders checkbox
929 * @param integer $row_num
934 function asearch_get_form_sub($sub, $row_num = 0)
936 return addCheckBox('sub[' . $row_num .']', $sub);
939 /** Build the 2 unop and where selects
940 * @param string $unop default value
941 * @param string $where default value
942 * @param integer $row_num
947 function asearch_get_form_location($unop, $where, $row_num = 0)
949 global $imap_asearch_unops, $imap_asearch_options;
951 return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
952 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
955 /** Build the what text input
956 * @param string $what
957 * @param integer $row_num
962 function asearch_get_form_what($what, $row_num = 0)
964 return addInput('what[' . $row_num . ']', $what, '35');
967 /** Build the Exclude criteria checkbox
968 * @param boolean $exclude
969 * @param integer $row_num
974 function asearch_get_form_exclude($exclude, $row_num = 0)
976 return addCheckBox('exclude['.$row_num.']', $exclude);
979 /** Print one advanced form row
980 * @param stream $imapConnection
981 * @param array $boxes
982 * @param string $mailbox
984 * @param string $unop
985 * @param string $where
986 * @param string $what
987 * @param boolean $exclude
989 * @param integer $row_num
993 function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
995 global $imap_asearch_biops_in;
998 echo html_tag('tr', '', '', $color[4]);
1001 echo html_tag('td', $row_num ?
1002 asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
1003 : '<b>' . _("In") . '</b>', 'center') . "\n";
1005 //Mailbox list and Include Subfolders
1007 asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
1008 . _("and subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
1010 //Unary operator and Search location
1011 echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
1014 echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
1017 echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
1022 /** Print the advanced search form
1023 * @param stream $imapConnection
1024 * @param array $boxes
1025 * @param array $mailbox_array
1026 * @param array $biop_array
1027 * @param array $unop_array
1028 * @param array $where_array
1029 * @param array $what_array
1030 * @param array $exclude_array
1031 * @param array $sub_array
1035 function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
1037 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
1042 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
1044 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
1045 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
1046 $row_count = count($where_array);
1047 for ($row_num = 0; $row_num < $row_count; $row_num++
) {
1048 $mailbox = asearch_nz($mailbox_array[$row_num]);
1049 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
1050 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
1051 $where = strip_tags(asearch_nz($where_array[$row_num]));
1052 $what = asearch_nz($what_array[$row_num]);
1053 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
1054 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
1055 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
1057 echo '</table>' . "\n";
1060 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1062 html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n"
1063 . html_tag('td', getButton('submit', 'submit', $add_criteria_button_html), 'center') . "\n"
1064 . html_tag('td', getButton('submit', 'submit', $del_all_button_html), 'center') . "\n"
1065 . html_tag('td', getButton('submit', 'submit', $del_excluded_button_html), 'center') . "\n"
1067 echo '</table>' . "\n";
1068 echo '</form>' . "\n";
1071 /** Print one basic form row
1072 * @param stream $imapConnection
1073 * @param array $boxes
1074 * @param string $mailbox
1075 * @param mixed $biop
1076 * @param mixed $unop
1077 * @param string $where
1078 * @param string $what
1079 * @param boolean $exclude
1081 * @param integer $row_num
1085 function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
1087 global $search_button_html;
1090 echo html_tag('tr', '', '', $color[4]);
1093 echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
1095 //Unary operator and Search location
1096 echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
1099 echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
1102 echo html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n";
1107 /** Print the basic search form
1108 * @param stream $imapConnection
1109 * @param array $boxes
1110 * @param array $mailbox_array
1111 * @param array $biop_array
1112 * @param array $unop_array
1113 * @param array $where_array
1114 * @param array $what_array
1115 * @param array $exclude_array
1116 * @param array $sub_array
1120 function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
1126 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
1128 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
1129 //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4'));
1130 $row_count = count($where_array);
1131 for ($row_num = 0; $row_num < $row_count; $row_num++
) {
1132 $mailbox = asearch_nz($mailbox_array[$row_num]);
1133 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
1134 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
1135 $where = strip_tags(asearch_nz($where_array[$row_num]));
1136 $what = asearch_nz($what_array[$row_num]);
1137 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
1138 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
1139 asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
1141 echo '</table>' . "\n";
1142 echo '</form>' . "\n";
1147 * @param array $boxes mailboxes array (reference)
1148 * @return array selectable unformatted mailboxes names
1152 function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
1154 $mboxes_array = array();
1155 $boxcount = count($boxes);
1156 for ($boxnum = 0; $boxnum < $boxcount; $boxnum++
) {
1157 if (!in_array('noselect', $boxes[$boxnum]['flags']))
1158 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
1160 return $mboxes_array;
1163 /* ------------------------ main ------------------------ */
1164 /* get globals we will need */
1165 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
1167 if ( sqgetGlobalVar('checkall', $temp, SQ_GET
) ) {
1168 $checkall = (int) $temp;
1172 * Retrieve the mailbox cache from the session.
1174 sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION
);
1177 $search_button_html = _("Search");
1178 $search_button_text = asearch_unhtmlentities($search_button_html);
1179 $add_criteria_button_html = _("Add New Criteria");
1180 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
1181 $del_excluded_button_html = _("Remove Excluded Criteria");
1182 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
1183 $del_all_button_html = _("Remove All Criteria");
1184 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
1186 /** Maximum number of recent searches to handle
1188 * @global integer $search_memory
1190 $search_memory = getPref($data_dir, $username, 'search_memory', 0);
1192 /** Advanced search control
1193 * - 0 => allow basic interface only
1194 * - 1 => allow advanced interface only
1198 $allow_advanced_search = asearch_nz($allow_advanced_search, 2);
1201 * Toggle advanced/basic search
1203 if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET
)) {
1204 setPref($data_dir, $username, 'search_advanced', $search_advanced & 1);
1206 /** If 1, show advanced search interface
1207 * Default from allow_advanced_search pref
1208 * @global integer $search_advanced
1210 if ($allow_advanced_search > 1) {
1211 $search_advanced = getPref($data_dir, $username, 'search_advanced', 0);
1213 $search_advanced = $allow_advanced_search;
1215 if ($search_advanced) {
1216 /** Set recent prefkeys according to $search_advanced
1217 * @global array $recent_prefkeys
1219 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_unop', 'asearch_recent_biop', 'asearch_recent_exclude', 'asearch_recent_sub');
1221 /** Set saved prefkeys according to $search_advanced
1222 * @global array $saved_prefkeys
1224 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_unop', 'asearch_saved_biop', 'asearch_saved_exclude', 'asearch_saved_sub');
1226 /*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
1228 $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop');
1229 $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop');
1232 /** How we did enter the form
1233 * - unset : Enter key, or called from outside (eg read_body)
1234 * - $search_button_text : Search button
1235 * - 'Search_no_update' : Search but don't update recent
1236 * - 'Search_last' : Same as no_update but reload and search last
1237 * - 'Search_silent' : Same as no_update but only display results
1238 * - $add_criteria_button_text : Add New Criteria button
1239 * - $del_excluded_button_text : Remove Excluded Criteria button
1240 * - $del_all_button_text : Remove All Criteria button
1247 * @global string $submit
1249 $searchpressed = false;
1250 if (isset($_GET['submit'])) {
1251 $submit = strip_tags($_GET['submit']);
1254 /** Searched mailboxes
1255 * @global array $mailbox_array
1257 if (isset($_GET['mailbox'])) {
1258 $mailbox_array = $_GET['mailbox'];
1259 $targetmailbox = $_GET['mailbox'];
1260 if (!is_array($mailbox_array)) {
1261 $mailbox_array = array($mailbox_array);
1264 $mailbox_array = array();
1265 $targetmailbox = array();
1267 $aMailboxGlobalPref = array(
1269 MBX_PREF_LIMIT
=> (int) $show_num,
1270 MBX_PREF_AUTO_EXPUNGE
=> (bool) $auto_expunge,
1271 MBX_PREF_INTERNALDATE
=> (bool) getPref($data_dir, $username, 'internal_date_sort')
1272 // MBX_PREF_FUTURE => (var) $future
1276 * system wide admin settings and incoming vars.
1279 // 'allow_thread_sort' => $allow_thread_sort,
1280 // 'allow_server_sort' => $allow_server_sort,
1281 'user' => $username,
1285 /** Binary operators
1286 * @global array $biop_array
1288 if (isset($_GET['biop'])) {
1289 $biop_array = $_GET['biop'];
1290 if (!is_array($biop_array))
1291 $biop_array = array($biop_array);
1293 $biop_array = array();
1296 * @global array $unop_array
1298 if (isset($_GET['unop'])) {
1299 $unop_array = $_GET['unop'];
1300 if (!is_array($unop_array))
1301 $unop_array = array($unop_array);
1303 $unop_array = array();
1306 * @global array $where_array
1308 if (isset($_GET['where'])) {
1309 $where_array = $_GET['where'];
1310 if (!is_array($where_array)) {
1311 $where_array = array($where_array);
1314 $where_array = array();
1317 * @global array $what_array
1319 if (isset($_GET['what'])) {
1320 $what_array = $_GET['what'];
1321 if (!is_array($what_array)) {
1322 $what_array = array($what_array);
1325 $what_array = array();
1327 /** Whether to exclude this criteria from search
1328 * @global array $exclude_array
1330 if (isset($_GET['exclude'])) {
1331 $exclude_array = $_GET['exclude'];
1333 $exclude_array = array();
1335 /** Search within subfolders
1336 * @global array $sub_array
1338 if (isset($_GET['sub'])) {
1339 $sub_array = $_GET['sub'];
1341 $sub_array = array();
1343 /** Row number used by recent and saved stuff
1345 if (isset($_GET['rownum'])) {
1346 $submit_rownum = strip_tags($_GET['rownum']);
1348 /** Change global sort
1350 if (sqgetGlobalVar('srt', $temp, SQ_GET
)) {
1352 asearch_edit_last(1);
1353 // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1355 if (sqgetGlobalVar('startMessage', $temp, SQ_GET
)) {
1356 $startMessage = (int) $temp;
1357 asearch_edit_last(1);
1358 // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1361 if ( sqgetGlobalVar('showall', $temp, SQ_GET
) ) {
1362 $showall = (int) $temp;
1363 asearch_edit_last(1);
1364 // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1367 if ( sqgetGlobalVar('account', $temp, SQ_GET
) ) {
1368 $iAccount = (int) $temp;
1374 * Which templatedir are we using. TODO, add make a config var of this and make it possible to switch templates
1376 $sTplDir = SM_PATH
. 'templates/default/';
1380 * Incoming submit buttons from the message list with search results
1382 if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST
) ||
1383 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
) ||
1384 sqgetGlobalVar('delete', $markDelete, SQ_POST
) ||
1385 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
) ||
1386 sqgetGlobalVar('markRead', $markRead, SQ_POST
) ||
1387 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
) ||
1388 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST
) ||
1389 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST
) ||
1390 sqgetGlobalVar('attache', $attache, SQ_POST
)) {
1391 asearch_edit_last(1);
1393 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1398 /** Toggle show/hide saved searches
1400 if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET
)) {
1401 setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
1403 /** Toggle show/hide recent searches
1405 if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET
)) {
1406 setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
1408 // end of get globals
1410 /** If TRUE, do not show search interface
1412 * @global bool $search_silent
1414 $search_silent = FALSE;
1416 /* See how the page was called and fire off correct function */
1417 if ((empty($submit)) && (!empty($where_array))) {
1418 /* This happens when the Enter key is used or called from outside */
1419 $submit = $search_button_text;
1420 /* Hack needed to handle coming back from read_body et als */
1421 if (count($where_array) != count($unop_array)) {
1423 * Hack to use already existen where and what vars.
1424 * where now contains the initiator page of the messagelist
1425 * and in this case 'search'. what contains an index to access
1426 * the search history
1429 sqgetGlobalVar('what',$what,SQ_GET
);
1430 asearch_edit_last($what);
1434 if (!isset($submit)) {
1438 case $search_button_text:
1439 if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
1440 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1443 case 'Search_silent':
1444 $search_silent = TRUE;
1446 case 'Search_no_update':
1447 $submit = $search_button_text;
1449 case $del_excluded_button_text:
1450 $delarray = array_keys($exclude_array);
1451 while (!empty($delarray)) {
1452 $delrow = array_pop($delarray);
1453 array_splice($mailbox_array, $delrow, 1);
1454 array_splice($biop_array, $delrow, 1);
1455 array_splice($unop_array, $delrow, 1);
1456 array_splice($where_array, $delrow, 1);
1457 array_splice($what_array, $delrow, 1);
1458 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
1459 array_splice($sub_array, $delrow, 1);
1461 $exclude_array = array();
1463 case $del_all_button_text:
1464 $mailbox_array = array();
1465 $biop_array = array();
1466 $unop_array = array();
1467 $where_array = array();
1468 $what_array = array();
1469 $exclude_array = array();
1470 $sub_array = array();
1473 asearch_save_recent($submit_rownum);
1475 case 'search_recent':
1476 $submit = $search_button_text;
1477 asearch_edit_recent($submit_rownum);
1478 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1480 case 'edit_recent': /* no link to do this, yet */
1481 asearch_edit_recent($submit_rownum);
1483 case 'forget_recent':
1484 asearch_forget_recent($submit_rownum);
1486 case 'search_saved':
1487 $submit = $search_button_text;
1488 asearch_edit_saved($submit_rownum);
1489 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1492 asearch_edit_saved($submit_rownum);
1494 case 'delete_saved':
1495 asearch_delete_saved($submit_rownum);
1500 //Texts in both basic and advanced form
1501 $imap_asearch_unops = array(
1506 if ($search_advanced) {
1507 //Texts in advanced form only
1508 $imap_asearch_options = array(
1510 //'ALL' is binary operator
1511 'ANSWERED' => _("Answered"),
1513 'BEFORE' => _("Before"),
1514 'BODY' => _("Message Body"),
1516 'DELETED' => _("Deleted"),
1517 'DRAFT' => _("Draft"),
1518 'FLAGGED' => _("Flagged"),
1519 'FROM' => _("Sent By"),
1520 'HEADER' => _("Header Field"),
1521 'KEYWORD' => _("Keyword"),
1522 'LARGER' => _("Larger Than"),
1524 //'NOT' is unary operator
1527 //'OR' is binary operator
1528 'RECENT' => _("Recent"),
1529 'SEEN' => _("Seen"),
1530 'SENTBEFORE' => _("Sent Before"),
1531 'SENTON' => _("Sent On"),
1532 'SENTSINCE' => _("Sent Since"),
1533 'SINCE' => _("Since"),
1534 'SMALLER' => _("Smaller Than"),
1535 'SUBJECT' => _("Subject Contains"),
1536 'TEXT' => _("Header and Body"),
1537 'TO' => _("Sent To"),
1539 /* 'UNANSWERED' => '',
1543 'UNKEYWORD' => _("Unkeyword"),
1544 'UNSEEN' => _("Unseen"),*/
1547 $imap_asearch_biops_in = array(
1548 'ALL' => _("And In"),
1552 $imap_asearch_biops = array(
1557 //Texts in basic form only
1558 $imap_asearch_options = array(
1560 'BODY' => _("Body"),
1562 'FROM' => _("From"),
1563 'SUBJECT' => _("Subject"),
1564 'TEXT' => _("Everywhere"),
1569 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
1571 /* open IMAP connection */
1572 $imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
1575 /* get mailboxes once here */
1576 $boxes = sqimap_mailbox_list($imapConnection);
1577 /* ensure we have a valid default mailbox name */
1578 $mailbox = asearch_nz($mailbox_array[0]);
1579 if (($mailbox == '') ||
($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
1580 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
1584 * Handle form actions like flag / unflag, seen / unseen, delete
1586 if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST
)) {
1589 * system wide admin settings and incoming vars.
1592 'user' => $username,
1594 $aConfig['setindex'] = 1; // $what $where = 'search'
1596 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1597 * when searching all mailboxes
1599 $aConfig['max_cache_size'] = count($boxes) +
1;
1601 $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $postMailbox,$aConfig,array());
1602 $sError = handleMessageListForm($imapConnection,$aMailbox);
1603 /* add the mailbox to the cache */
1604 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1612 if (isset($aMailbox['FORWARD_SESSION'])) {
1613 /* add the mailbox to the cache */
1614 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1615 sqsession_register($mailbox_cache,'mailbox_cache');
1617 if ($compose_new_win) {
1618 // write the session in order to make sure that the compose window has
1619 // access to the composemessages array which is stored in the session
1620 session_write_close();
1621 // restart the session. Do not use sqsession_is_active because the session_id
1622 // isn't empty after a session_write_close
1625 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
1626 $compose_width = '640';
1628 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
1629 $compose_height = '550';
1631 // do not use &, it will break the query string and $session will not be detected!!!
1632 $comp_uri = SM_PATH
. 'src/compose.php?mailbox='. urlencode($mailbox).
1633 '&session='.$aMailbox['FORWARD_SESSION'];
1634 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", false);
1636 // save mailboxstate
1637 sqsession_register($aMailbox,'aLastSelectedMailbox');
1638 session_write_close();
1639 // we have to redirect to the compose page
1640 $location = SM_PATH
. 'src/compose.php?mailbox='. urlencode($mailbox).
1641 '&session='.$aMailbox['FORWARD_SESSION'];
1642 header("Location: $location");
1646 displayPageHeader($color, $mailbox);
1647 // $compose_uri = $base_uri.'src/compose.php?newmessage=1';
1651 echo html_tag( 'div', '<b>' . htmlspecialchars($note) .'</b>', 'center' ) . "<br />\n";
1655 do_hook('search_before_form');
1657 if (!$search_silent) {
1658 //Add a link to the other search mode if allowed
1659 if ($allow_advanced_search > 1)
1660 $toggle_link = ' - <small>['
1661 . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
1666 echo html_tag('table',
1668 . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
1670 '', '', 'width="100%"') . "\n";
1671 asearch_print_saved($boxes);
1672 asearch_print_recent($boxes);
1673 if (empty($where_array)) {
1674 global $sent_folder;
1676 $mailbox_array[0] = $mailbox;
1677 $biop_array[0] = '';
1678 $unop_array[0] = '';
1679 if ($mailbox == $sent_folder) {
1680 $where_array[0] = 'TO';
1682 $where_array[0] = 'FROM';
1684 $what_array[0] = '';
1685 $exclude_array[0] = '';
1688 //Display advanced or basic form
1689 if ($search_advanced) {
1690 if ($submit == $add_criteria_button_text) {
1691 $last_index = max(count($where_array) - 1, 0);
1692 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1693 $biop_array[] = asearch_nz($biop_array[$last_index]);
1694 $unop_array[] = asearch_nz($unop_array[$last_index]);
1695 $where_array[] = asearch_nz($where_array[$last_index]);
1696 $what_array[] = asearch_nz($what_array[$last_index]);
1697 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1698 $sub_array[] = asearch_nz($sub_array[$last_index]);
1700 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1702 asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1706 do_hook('search_after_form');
1708 if ($submit == $search_button_text) {
1710 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1711 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1712 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]));
1713 echo '</table>' . "\n";
1717 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
1718 if ($query_error != '') {
1719 echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
1721 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
1723 * Retrieve the search queries
1725 $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1726 foreach($mboxes_mailbox as $mbx => $search) {
1729 * until there is no per mailbox option screen to set prefs we override
1730 * the mailboxprefs by the default ones
1733 $aMailboxPrefSer=getPref($data_dir, $username,'pref_'.$iAccount.'_'.$mbx);
1734 if ($aMailboxPrefSer) {
1735 $aMailboxPref = unserialize($aMailboxPrefSer);
1736 $aMailboxPref[MBX_PREF_COLUMNS
] = $index_order;
1738 setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($default_mailbox_pref));
1739 $aMailboxPref = $default_mailbox_pref;
1741 if (isset($srt) && $targetmailbox == $mbx) {
1742 $aMailboxPref[MBX_PREF_SORT
] = (int) $srt;
1745 $trash_folder = (isset($trash_folder)) ?
$trash_folder : false;
1746 $sent_folder = (isset($sent_folder)) ?
$sent_folder : false;
1747 $draft_folder = (isset($draft_folder)) ?
$draft_folder : false;
1751 * until there is no per mailbox option screen to set prefs we override
1752 * the mailboxprefs by the default ones
1754 $aMailboxPref[MBX_PREF_LIMIT
] = (int) $show_num;
1755 $aMailboxPref[MBX_PREF_AUTO_EXPUNGE
] = (bool) $auto_expunge;
1756 $aMailboxPref[MBX_PREF_INTERNALDATE
] = (bool) getPref($data_dir, $username, 'internal_date_sort');
1757 $aMailboxPref[MBX_PREF_COLUMNS
] = $index_order;
1760 * Replace From => To in case it concerns a draft or sent folder
1762 if (($mbx == $sent_folder ||
$mbx == $draft_folder) &&
1763 !in_array(SQM_COL_TO
,$aMailboxPref[MBX_PREF_COLUMNS
])) {
1764 $aNewOrder = array(); // nice var name ;)
1765 foreach($aMailboxPref[MBX_PREF_COLUMNS
] as $iCol) {
1766 if ($iCol == SQM_COL_FROM
) {
1769 $aNewOrder[] = $iCol;
1771 $aMailboxPref[MBX_PREF_COLUMNS
] = $aNewOrder;
1772 setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($aMailboxPref));
1775 $aConfig['search'] = $search['search'];
1776 $aConfig['charset'] = $search['charset'];
1779 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1780 * when searching all mailboxes
1782 $aConfig['max_cache_size'] = count($mboxes_mailbox) +
1;
1783 if (isset($startMessage) && $targetmailbox == $mbx) {
1784 $aConfig['offset'] = $startMessage;
1786 $aConfig['offset'] = 0;
1788 if (isset($showall) && $targetmailbox == $mbx) {
1789 $aConfig['showall'] = $showall;
1791 if (isset($aConfig['showall'])) {
1792 unset($aConfig['showall']);
1798 * Set the config options for the messages list
1800 $aColumns = array();
1801 foreach ($aMailboxPref[MBX_PREF_COLUMNS
] as $iCol) {
1802 $aColumns[$iCol] = array();
1805 if ($truncate_subject) {
1806 $aColumns[$iCol]['truncate'] = $truncate_subject;
1813 if ($truncate_sender) {
1814 $aColumns[$iCol]['truncate'] = $truncate_sender;
1822 'columns' => $aColumns,
1823 'config' => array('alt_index_colors' => $alt_index_colors,
1824 'highlight_list' => $message_highlight_list,
1825 'fancy_index_highlite' => $fancy_index_highlite,
1826 'show_flag_buttons' => (isset($show_flag_buttons)) ?
$show_flag_buttons : true,
1827 'lastTargetMailbox' => (isset($lastTargetMailbox)) ?
$lastTargetMailbox : '',
1828 'trash_folder' => $trash_folder,
1829 'sent_folder' => $sent_folder,
1830 'draft_folder' => $draft_folder,
1831 'enablesort' => true,
1835 'account' => (isset($iAccount)) ?
$iAccount : 0,
1836 'module' => 'read_body',
1840 $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mbx,$aConfig,$aMailboxPref);
1843 $aTemplate = showMessagesForMailbox($imapConnection, $aMailbox,$aProps, $iError);
1845 // in th future we can make use of multiple message sets, now set it to 1 for search.
1847 if (isset($aMailbox['UIDSET'][$iSetIndex])) {
1848 $iMsgCnt +
= count($aMailbox['UIDSET'][$iSetIndex]);
1854 * In the future, move this the the initialisation area
1856 sqgetGlobalVar('align',$align,SQ_SESSION
);
1858 $oTemplate = new Template($sTplDir);
1860 if ($aMailbox['EXISTS'] > 0) {
1863 echo "ERROR occured, errorhandler will be implemented very soon";
1865 foreach ($aTemplate as $k => $v) {
1866 $oTemplate->assign($k, $v);
1869 $oTemplate->assign('page_selector', $page_selector);
1870 $oTemplate->assign('page_selector_max', $page_selector_max);
1871 $oTemplate->assign('compact_paginator', $compact_paginator);
1872 $oTemplate->assign('javascript_on', $javascript_on);
1873 $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ?
$aProps['config']['enablesort'] : false);
1874 $oTemplate->assign('icon_theme_path', $icon_theme_path);
1875 $oTemplate->assign('use_icons', (isset($use_icons)) ?
$use_icons : false);
1876 $oTemplate->assign('aOrder', array_keys($aColumns));
1877 $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ?
$alt_index_colors: false);
1878 $oTemplate->assign('color', $color);
1879 $oTemplate->assign('align', $align);
1881 $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
1882 if (strtoupper($mbx) == 'INBOX') {
1883 $mailbox_display = _("INBOX");
1885 $mailbox_display = imap_utf7_decode_local($mbx);
1888 echo '<br /><b><big>' . _("Folder:") . ' '. htmlspecialchars($mailbox_display) . ' </big></b>';
1890 $oTemplate->display('message_list.tpl');
1895 /* add the mailbox to the cache */
1896 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1901 echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1905 do_hook('search_bottom');
1906 sqimap_logout($imapConnection);
1907 $oTemplate->display('footer.tpl');
1908 sqsession_register($mailbox_cache,'mailbox_cache');