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