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