String fix
[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 * @todo if(function_exists('addCheckBox')) ?
731 */
732 function asearch_get_form_sub($sub, $row_num = 0)
733 {
734 return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub)
735 : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />';
736 }
737
738 /** Build the 2 unop and where selects
739 */
740 function asearch_get_form_location($unop, $where, $row_num = 0)
741 {
742 global $imap_asearch_unops, $imap_asearch_options;
743
744 return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
745 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
746 }
747
748 /** Build the what text input
749 */
750 function asearch_get_form_what($what, $row_num = 0)
751 {
752 return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35')
753 : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />';
754 }
755
756 /** Build the Exclude criteria checkbox
757 * @todo if(function_exists('addCheckBox')) ?
758 */
759 function asearch_get_form_exclude($exclude, $row_num = 0)
760 {
761 return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude)
762 : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />';
763 }
764
765 /** Print one advanced form row
766 */
767 function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
768 {
769 global $imap_asearch_biops_in;
770 global $color;
771
772 echo html_tag('tr', '', '', $color[4]);
773
774 //Binary operator
775 echo html_tag('td', $row_num ?
776 asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
777 : '<b>' . _("In") . '</b>', 'center') . "\n";
778
779 //Mailbox list and Include Subfolders
780 echo html_tag('td',
781 asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
782 . _("and&nbsp;subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
783
784 //Unary operator and Search location
785 echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
786
787 //Text input
788 echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
789
790 //Exclude criteria
791 echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
792
793 echo "</tr>\n";
794 }
795
796 /** Print the advanced search form
797 */
798 function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
799 {
800 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
801 global $color;
802
803 //Search Form
804 echo "<br />\n";
805 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
806
807 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
808 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
809 $row_count = count($where_array);
810 for ($row_num = 0; $row_num < $row_count; $row_num++) {
811 $mailbox = asearch_nz($mailbox_array[$row_num]);
812 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
813 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
814 $where = strip_tags(asearch_nz($where_array[$row_num]));
815 $what = asearch_nz($what_array[$row_num]);
816 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
817 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
818 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
819 }
820 echo '</table>' . "\n";
821
822 //Submit buttons
823 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
824 echo html_tag('tr',
825 html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n"
826 . html_tag('td', getButton('submit', 'submit', $add_criteria_button_html), 'center') . "\n"
827 . html_tag('td', getButton('submit', 'submit', $del_all_button_html), 'center') . "\n"
828 . html_tag('td', getButton('submit', 'submit', $del_excluded_button_html), 'center') . "\n"
829 );
830 echo '</table>' . "\n";
831 echo '</form>' . "\n";
832 }
833
834 /** Print one basic form row
835 */
836 function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
837 {
838 global $search_button_html;
839 global $color;
840
841 echo html_tag('tr', '', '', $color[4]);
842
843 //Mailbox list
844 echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
845
846 //Unary operator and Search location
847 echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
848
849 //Text input
850 echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
851
852 //Submit button
853 echo html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n";
854
855 echo "</tr>\n";
856 }
857
858 /** Print the basic search form
859 */
860 function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
861 {
862 global $color;
863
864 //Search Form
865 echo "<br />\n";
866 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
867
868 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
869 //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4'));
870 $row_count = count($where_array);
871 for ($row_num = 0; $row_num < $row_count; $row_num++) {
872 $mailbox = asearch_nz($mailbox_array[$row_num]);
873 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
874 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
875 $where = strip_tags(asearch_nz($where_array[$row_num]));
876 $what = asearch_nz($what_array[$row_num]);
877 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
878 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
879 asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
880 }
881 echo '</table>' . "\n";
882 echo '</form>' . "\n";
883 }
884
885
886 /**
887 * @param array $boxes mailboxes array (reference)
888 * @return array selectable unformatted mailboxes names
889 */
890 function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
891 {
892 $mboxes_array = array();
893 $boxcount = count($boxes);
894 for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) {
895 if (!in_array('noselect', $boxes[$boxnum]['flags']))
896 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
897 }
898 return $mboxes_array;
899 }
900
901 /* ------------------------ main ------------------------ */
902 /* get globals we will need */
903 sqgetGlobalVar('username', $username, SQ_SESSION);
904 sqgetGlobalVar('key', $key, SQ_COOKIE);
905 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
906 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
907
908 if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) {
909 $checkall = (int) $temp;
910 }
911
912 /**
913 * Retrieve the mailbox cache from the session.
914 */
915 sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
916
917
918 $search_button_html = _("Search");
919 $search_button_text = asearch_unhtmlentities($search_button_html);
920 $add_criteria_button_html = _("Add New Criteria");
921 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
922 $del_excluded_button_html = _("Remove Excluded Criteria");
923 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
924 $del_all_button_html = _("Remove All Criteria");
925 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
926
927 /** Maximum number of recent searches to handle
928 * Default 0
929 * @global integer $search_memory
930 */
931 $search_memory = getPref($data_dir, $username, 'search_memory', 0);
932
933 /** Advanced search control
934 * - 0 => allow basic interface only
935 * - 1 => allow advanced interface only
936 * - 2 => allow both
937 * Default 2
938 */
939 $allow_advanced_search = asearch_nz($allow_advanced_search, 2);
940
941 /**
942 * Toggle advanced/basic search
943 */
944 if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET)) {
945 setPref($data_dir, $username, 'search_advanced', $search_advanced & 1);
946 }
947 /** If 1, show advanced search interface
948 * Default from allow_advanced_search pref
949 * @global integer $search_advanced
950 */
951 if ($allow_advanced_search > 1) {
952 $search_advanced = getPref($data_dir, $username, 'search_advanced', 0);
953 } else {
954 $search_advanced = $allow_advanced_search;
955 }
956 if ($search_advanced) {
957 /** Set recent prefkeys according to $search_advanced
958 * @global array $recent_prefkeys
959 */
960 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_unop', 'asearch_recent_biop', 'asearch_recent_exclude', 'asearch_recent_sub');
961
962 /** Set saved prefkeys according to $search_advanced
963 * @global array $saved_prefkeys
964 */
965 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_unop', 'asearch_saved_biop', 'asearch_saved_exclude', 'asearch_saved_sub');
966
967 /*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
968 } else {
969 $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop');
970 $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop');
971 }
972
973 /** How we did enter the form
974 * - unset : Enter key, or called from outside (eg read_body)
975 * - $search_button_text : Search button
976 * - 'Search_no_update' : Search but don't update recent
977 * - 'Search_last' : Same as no_update but reload and search last
978 * - 'Search_silent' : Same as no_update but only display results
979 * - $add_criteria_button_text : Add New Criteria button
980 * - $del_excluded_button_text : Remove Excluded Criteria button
981 * - $del_all_button_text : Remove All Criteria button
982 * - 'save_recent'
983 * - 'search_recent'
984 * - 'forget_recent'
985 * - 'edit_saved'
986 * - 'search_saved'
987 * - 'delete_saved'
988 * @global string $submit
989 */
990 $searchpressed = false;
991 if (isset($_GET['submit'])) {
992 $submit = strip_tags($_GET['submit']);
993 }
994
995 /** Searched mailboxes
996 * @global array $mailbox_array
997 */
998 if (isset($_GET['mailbox'])) {
999 $mailbox_array = $_GET['mailbox'];
1000 $targetmailbox = $_GET['mailbox'];
1001 if (!is_array($mailbox_array)) {
1002 $mailbox_array = array($mailbox_array);
1003 }
1004 } else {
1005 $mailbox_array = array();
1006 $targetmailbox = array();
1007 }
1008 $aMailboxGlobalPref = array(
1009 MBX_PREF_SORT => 0,
1010 MBX_PREF_LIMIT => (int) $show_num,
1011 MBX_PREF_AUTO_EXPUNGE => (bool) $auto_expunge,
1012 MBX_PREF_INTERNALDATE => (bool) getPref($data_dir, $username, 'internal_date_sort')
1013 // MBX_PREF_FUTURE => (var) $future
1014 );
1015
1016 /**
1017 * system wide admin settings and incoming vars.
1018 */
1019 $aConfig = array(
1020 'allow_thread_sort' => $allow_thread_sort,
1021 'allow_server_sort' => $allow_server_sort,
1022 'user' => $username,
1023 'setindex' => 1
1024 );
1025
1026 /** Binary operators
1027 * @global array $biop_array
1028 */
1029 if (isset($_GET['biop'])) {
1030 $biop_array = $_GET['biop'];
1031 if (!is_array($biop_array))
1032 $biop_array = array($biop_array);
1033 } else {
1034 $biop_array = array();
1035 }
1036 /** Unary operators
1037 * @global array $unop_array
1038 */
1039 if (isset($_GET['unop'])) {
1040 $unop_array = $_GET['unop'];
1041 if (!is_array($unop_array))
1042 $unop_array = array($unop_array);
1043 } else {
1044 $unop_array = array();
1045 }
1046 /** Where to search
1047 * @global array $where_array
1048 */
1049 if (isset($_GET['where'])) {
1050 $where_array = $_GET['where'];
1051 if (!is_array($where_array)) {
1052 $where_array = array($where_array);
1053 }
1054 } else {
1055 $where_array = array();
1056 }
1057 /** What to search
1058 * @global array $what_array
1059 */
1060 if (isset($_GET['what'])) {
1061 $what_array = $_GET['what'];
1062 if (!is_array($what_array)) {
1063 $what_array = array($what_array);
1064 }
1065 } else {
1066 $what_array = array();
1067 }
1068 /** Whether to exclude this criteria from search
1069 * @global array $exclude_array
1070 */
1071 if (isset($_GET['exclude'])) {
1072 $exclude_array = $_GET['exclude'];
1073 } else {
1074 $exclude_array = array();
1075 }
1076 /** Search within subfolders
1077 * @global array $sub_array
1078 */
1079 if (isset($_GET['sub'])) {
1080 $sub_array = $_GET['sub'];
1081 } else {
1082 $sub_array = array();
1083 }
1084 /** Row number used by recent and saved stuff
1085 */
1086 if (isset($_GET['rownum'])) {
1087 $submit_rownum = strip_tags($_GET['rownum']);
1088 }
1089 /** Change global sort
1090 */
1091 if (sqgetGlobalVar('srt', $temp, SQ_GET)) {
1092 $srt = (int) $temp;
1093 asearch_edit_last(1);
1094 // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1095 }
1096 if (sqgetGlobalVar('startMessage', $temp, SQ_GET)) {
1097 $startMessage = (int) $temp;
1098 asearch_edit_last(1);
1099 // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1100 }
1101
1102 if ( sqgetGlobalVar('showall', $temp, SQ_GET) ) {
1103 $showall = (int) $temp;
1104 asearch_edit_last(1);
1105 // asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1106 }
1107
1108 if ( sqgetGlobalVar('account', $temp, SQ_GET) ) {
1109 $iAccount = (int) $temp;
1110 } else {
1111 $iAccount = 0;
1112 }
1113
1114 /**
1115 * Which templatedir are we using. TODO, add make a config var of this and make it possible to switch templates
1116 */
1117 $sTplDir = SM_PATH . 'templates/default/';
1118
1119
1120 /**
1121 * Incoming submit buttons from the message list with search results
1122 */
1123 if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST) ||
1124 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST) ||
1125 sqgetGlobalVar('delete', $markDelete, SQ_POST) ||
1126 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST) ||
1127 sqgetGlobalVar('markRead', $markRead, SQ_POST) ||
1128 sqgetGlobalVar('markUnread', $markUnread, SQ_POST) ||
1129 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST) ||
1130 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST) ||
1131 sqgetGlobalVar('attache', $attache, SQ_POST)) {
1132 asearch_edit_last(1);
1133 $submit = '';
1134 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1135 }
1136
1137
1138
1139 /** Toggle show/hide saved searches
1140 */
1141 if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) {
1142 setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
1143 }
1144 /** Toggle show/hide recent searches
1145 */
1146 if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) {
1147 setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
1148 }
1149 // end of get globals
1150
1151 /** If TRUE, do not show search interface
1152 * Default FALSE
1153 * @global bool $search_silent
1154 */
1155 $search_silent = FALSE;
1156
1157 /* See how the page was called and fire off correct function */
1158 if ((empty($submit)) && (!empty($where_array))) {
1159 /* This happens when the Enter key is used or called from outside */
1160 $submit = $search_button_text;
1161 /* Hack needed to handle coming back from read_body et als */
1162 if (count($where_array) != count($unop_array)) {
1163 /**
1164 * Hack to use already existen where and what vars.
1165 * where now contains the initiator page of the messagelist
1166 * and in this case 'search'. what contains an index to access
1167 * the search history
1168 */
1169
1170 sqgetGlobalVar('what',$what,SQ_GET);
1171 asearch_edit_last($what);
1172 }
1173 }
1174
1175 if (!isset($submit)) {
1176 $submit = '';
1177 } else {
1178 switch ($submit) {
1179 case $search_button_text:
1180 if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
1181 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1182 }
1183 break;
1184 case 'Search_silent':
1185 $search_silent = TRUE;
1186 /*nobreak;*/
1187 case 'Search_no_update':
1188 $submit = $search_button_text;
1189 break;
1190 case $del_excluded_button_text:
1191 $delarray = array_keys($exclude_array);
1192 while (!empty($delarray)) {
1193 $delrow = array_pop($delarray);
1194 array_splice($mailbox_array, $delrow, 1);
1195 array_splice($biop_array, $delrow, 1);
1196 array_splice($unop_array, $delrow, 1);
1197 array_splice($where_array, $delrow, 1);
1198 array_splice($what_array, $delrow, 1);
1199 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
1200 array_splice($sub_array, $delrow, 1);
1201 }
1202 $exclude_array = array();
1203 break;
1204 case $del_all_button_text:
1205 $mailbox_array = array();
1206 $biop_array = array();
1207 $unop_array = array();
1208 $where_array = array();
1209 $what_array = array();
1210 $exclude_array = array();
1211 $sub_array = array();
1212 break;
1213 case 'save_recent':
1214 asearch_save_recent($submit_rownum);
1215 break;
1216 case 'search_recent':
1217 $submit = $search_button_text;
1218 asearch_edit_recent($submit_rownum);
1219 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1220 break;
1221 case 'edit_recent': /* no link to do this, yet */
1222 asearch_edit_recent($submit_rownum);
1223 break;
1224 case 'forget_recent':
1225 asearch_forget_recent($submit_rownum);
1226 break;
1227 case 'search_saved':
1228 $submit = $search_button_text;
1229 asearch_edit_saved($submit_rownum);
1230 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1231 break;
1232 case 'edit_saved':
1233 asearch_edit_saved($submit_rownum);
1234 break;
1235 case 'delete_saved':
1236 asearch_delete_saved($submit_rownum);
1237 break;
1238 }
1239 }
1240
1241 //Texts in both basic and advanced form
1242 $imap_asearch_unops = array(
1243 '' => '',
1244 'NOT' => _("Not")
1245 );
1246
1247 if ($search_advanced) {
1248 //Texts in advanced form only
1249 $imap_asearch_options = array(
1250 //<message set>,
1251 //'ALL' is binary operator
1252 'ANSWERED' => _("Answered"),
1253 'BCC' => _("Bcc"),
1254 'BEFORE' => _("Before"),
1255 'BODY' => _("Message Body"),
1256 'CC' => _("Cc"),
1257 'DELETED' => _("Deleted"),
1258 'DRAFT' => _("Draft"),
1259 'FLAGGED' => _("Flagged"),
1260 'FROM' => _("Sent By"),
1261 'HEADER' => _("Header Field"),
1262 'KEYWORD' => _("Keyword"),
1263 'LARGER' => _("Larger Than"),
1264 'NEW' => _("New"),
1265 //'NOT' is unary operator
1266 'OLD' => _("Old"),
1267 'ON' => _("On"),
1268 //'OR' is binary operator
1269 'RECENT' => _("Recent"),
1270 'SEEN' => _("Seen"),
1271 'SENTBEFORE' => _("Sent Before"),
1272 'SENTON' => _("Sent On"),
1273 'SENTSINCE' => _("Sent Since"),
1274 'SINCE' => _("Since"),
1275 'SMALLER' => _("Smaller Than"),
1276 'SUBJECT' => _("Subject Contains"),
1277 'TEXT' => _("Header and Body"),
1278 'TO' => _("Sent To"),
1279 //'UID' => 'anum',
1280 /* 'UNANSWERED' => '',
1281 'UNDELETED' => '',
1282 'UNDRAFT' => '',
1283 'UNFLAGGED' => '',
1284 'UNKEYWORD' => _("Unkeyword"),
1285 'UNSEEN' => _("Unseen"),*/
1286 );
1287
1288 $imap_asearch_biops_in = array(
1289 'ALL' => _("And In"),
1290 'OR' => _("Or In")
1291 );
1292
1293 $imap_asearch_biops = array(
1294 'ALL' => _("And"),
1295 'OR' => _("Or")
1296 );
1297 } else {
1298 //Texts in basic form only
1299 $imap_asearch_options = array(
1300 'BCC' => _("Bcc"),
1301 'BODY' => _("Body"),
1302 'CC' => _("Cc"),
1303 'FROM' => _("From"),
1304 'SUBJECT' => _("Subject"),
1305 'TEXT' => _("Everywhere"),
1306 'TO' => _("To"),
1307 );
1308 }
1309
1310 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
1311
1312 /* open IMAP connection */
1313 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
1314
1315
1316 /* get mailboxes once here */
1317 $boxes = sqimap_mailbox_list($imapConnection);
1318 /* ensure we have a valid default mailbox name */
1319 $mailbox = asearch_nz($mailbox_array[0]);
1320 if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
1321 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
1322
1323
1324 /**
1325 * Handle form actions like flag / unflag, seen / unseen, delete
1326 */
1327 if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST)) {
1328 if ($postMailbox) {
1329 /**
1330 * system wide admin settings and incoming vars.
1331 */
1332 $aConfig = array(
1333 'user' => $username,
1334 );
1335 $aConfig['setindex'] = 1; // $what $where = 'search'
1336 /**
1337 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1338 * when searching all mailboxes
1339 */
1340 $aConfig['max_cache_size'] = count($boxes) +1;
1341
1342 $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $postMailbox,$aConfig,array());
1343 $sError = handleMessageListForm($imapConnection,$aMailbox);
1344 /* add the mailbox to the cache */
1345 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1346
1347 if ($sError) {
1348 $note = $sError;
1349 }
1350 }
1351 }
1352
1353 if (isset($aMailbox['FORWARD_SESSION'])) {
1354 /* add the mailbox to the cache */
1355 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1356 sqsession_register($mailbox_cache,'mailbox_cache');
1357
1358 if ($compose_new_win) {
1359 // write the session in order to make sure that the compose window has
1360 // access to the composemessages array which is stored in the session
1361 session_write_close();
1362 // restart the session. Do not use sqsession_is_active because the session_id
1363 // isn't empty after a session_write_close
1364 session_start();
1365
1366 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
1367 $compose_width = '640';
1368 }
1369 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
1370 $compose_height = '550';
1371 }
1372 // do not use &amp;, it will break the query string and $session will not be detected!!!
1373 $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
1374 '&session='.$aMailbox['FORWARD_SESSION'];
1375 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", false);
1376 } else {
1377 // save mailboxstate
1378 sqsession_register($aMailbox,'aLastSelectedMailbox');
1379 session_write_close();
1380 // we have to redirect to the compose page
1381 $location = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
1382 '&amp;session='.$aMailbox['FORWARD_SESSION'];
1383 header("Location: $location");
1384 exit;
1385 }
1386 } else {
1387 displayPageHeader($color, $mailbox);
1388 // $compose_uri = $base_uri.'src/compose.php?newmessage=1';
1389 }
1390
1391 if (isset($note)) {
1392 echo html_tag( 'div', '<b>' . htmlspecialchars($note) .'</b>', 'center' ) . "<br />\n";
1393 }
1394
1395
1396 do_hook('search_before_form');
1397
1398 if (!$search_silent) {
1399 //Add a link to the other search mode if allowed
1400 if ($allow_advanced_search > 1)
1401 $toggle_link = ' - <small>['
1402 . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
1403 . ']</small>';
1404 else
1405 $toggle_link = '';
1406
1407 echo html_tag('table',
1408 html_tag('tr', "\n"
1409 . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
1410 ) ,
1411 '', '', 'width="100%"') . "\n";
1412 asearch_print_saved($boxes);
1413 asearch_print_recent($boxes);
1414 if (empty($where_array)) {
1415 global $sent_folder;
1416
1417 $mailbox_array[0] = $mailbox;
1418 $biop_array[0] = '';
1419 $unop_array[0] = '';
1420 if ($mailbox == $sent_folder) {
1421 $where_array[0] = 'TO';
1422 } else {
1423 $where_array[0] = 'FROM';
1424 }
1425 $what_array[0] = '';
1426 $exclude_array[0] = '';
1427 $sub_array[0] = '';
1428 }
1429 //Display advanced or basic form
1430 if ($search_advanced) {
1431 if ($submit == $add_criteria_button_text) {
1432 $last_index = max(count($where_array) - 1, 0);
1433 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1434 $biop_array[] = asearch_nz($biop_array[$last_index]);
1435 $unop_array[] = asearch_nz($unop_array[$last_index]);
1436 $where_array[] = asearch_nz($where_array[$last_index]);
1437 $what_array[] = asearch_nz($what_array[$last_index]);
1438 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1439 $sub_array[] = asearch_nz($sub_array[$last_index]);
1440 }
1441 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1442 } else {
1443 asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1444 }
1445 }
1446
1447 do_hook('search_after_form');
1448
1449 if ($submit == $search_button_text) {
1450 $msgsfound = false;
1451 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1452 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1453 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]));
1454 echo '</table>' . "\n";
1455
1456 flush();
1457 $iMsgCnt = 0;
1458 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
1459 if ($query_error != '') {
1460 echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
1461 } else {
1462 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
1463 /**
1464 * Retrieve the search queries
1465 */
1466 $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1467 foreach($mboxes_mailbox as $mbx => $search) {
1468
1469 /**
1470 * until there is no per mailbox option screen to set prefs we override
1471 * the mailboxprefs by the default ones
1472 */
1473
1474 $aMailboxPrefSer=getPref($data_dir, $username,'pref_'.$iAccount.'_'.$mbx);
1475 if ($aMailboxPrefSer) {
1476 $aMailboxPref = unserialize($aMailboxPrefSer);
1477 $aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
1478 } else {
1479 setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($default_mailbox_pref));
1480 $aMailboxPref = $default_mailbox_pref;
1481 }
1482 if (isset($srt) && $targetmailbox == $mbx) {
1483 $aMailboxPref[MBX_PREF_SORT] = (int) $srt;
1484 }
1485
1486 $trash_folder = (isset($trash_folder)) ? $trash_folder : false;
1487 $sent_folder = (isset($sent_folder)) ? $sent_folder : false;
1488 $draft_folder = (isset($draft_folder)) ? $draft_folder : false;
1489
1490
1491 /**
1492 * until there is no per mailbox option screen to set prefs we override
1493 * the mailboxprefs by the default ones
1494 */
1495 $aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num;
1496 $aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge;
1497 $aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort');
1498 $aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
1499
1500 /**
1501 * Replace From => To in case it concerns a draft or sent folder
1502 */
1503 if (($mbx == $sent_folder || $mbx == $draft_folder) &&
1504 !in_array(SQM_COL_TO,$aMailboxPref[MBX_PREF_COLUMNS])) {
1505 $aNewOrder = array(); // nice var name ;)
1506 foreach($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
1507 if ($iCol == SQM_COL_FROM) {
1508 $iCol = SQM_COL_TO;
1509 }
1510 $aNewOrder[] = $iCol;
1511 }
1512 $aMailboxPref[MBX_PREF_COLUMNS] = $aNewOrder;
1513 setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($aMailboxPref));
1514 }
1515
1516 $aConfig['search'] = $search['search'];
1517 $aConfig['charset'] = $search['charset'];
1518
1519 /**
1520 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1521 * when searching all mailboxes
1522 */
1523 $aConfig['max_cache_size'] = count($mboxes_mailbox) +1;
1524 if (isset($startMessage) && $targetmailbox == $mbx) {
1525 $aConfig['offset'] = $startMessage;
1526 } else {
1527 $aConfig['offset'] = 0;
1528 }
1529 if (isset($showall) && $targetmailbox == $mbx) {
1530 $aConfig['showall'] = $showall;
1531 } else {
1532 if (isset($aConfig['showall'])) {
1533 unset($aConfig['showall']);
1534 }
1535 $showall = false;
1536 }
1537
1538 /**
1539 * Set the config options for the messages list
1540 */
1541 $aColumns = array();
1542 foreach ($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
1543 $aColumns[$iCol] = array();
1544 switch ($iCol) {
1545 case SQM_COL_SUBJ:
1546 if ($truncate_subject) {
1547 $aColumns[$iCol]['truncate'] = $truncate_subject;
1548 }
1549 break;
1550 case SQM_COL_FROM:
1551 case SQM_COL_TO:
1552 case SQM_COL_CC:
1553 case SQM_COL_BCC:
1554 if ($truncate_sender) {
1555 $aColumns[$iCol]['truncate'] = $truncate_sender;
1556 }
1557 break;
1558 }
1559 }
1560
1561
1562 $aProps = array(
1563 'columns' => $aColumns,
1564 'config' => array('alt_index_colors' => $alt_index_colors,
1565 'highlight_list' => $message_highlight_list,
1566 'fancy_index_highlite' => $fancy_index_highlite,
1567 'show_flag_buttons' => (isset($show_flag_buttons)) ? $show_flag_buttons : true,
1568 'lastTargetMailbox' => (isset($lastTargetMailbox)) ? $lastTargetMailbox : '',
1569 'trash_folder' => $trash_folder,
1570 'sent_folder' => $sent_folder,
1571 'draft_folder' => $draft_folder,
1572 'enablesort' => true,
1573 'color' => $color
1574 ),
1575 'mailbox' => $mbx,
1576 'account' => (isset($iAccount)) ? $iAccount : 0,
1577 'module' => 'read_body',
1578 'email' => false);
1579
1580
1581 $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mbx,$aConfig,$aMailboxPref);
1582
1583 $iError = 0;
1584 $aTemplate = showMessagesForMailbox($imapConnection, $aMailbox,$aProps, $iError);
1585
1586 // in th future we can make use of multiple message sets, now set it to 1 for search.
1587 $iSetIndex = 1;
1588 if (isset($aMailbox['UIDSET'][$iSetIndex])) {
1589 $iMsgCnt += count($aMailbox['UIDSET'][$iSetIndex]);
1590 }
1591 if ($iError) {
1592 // error handling
1593 } else {
1594 /**
1595 * In the future, move this the the initialisation area
1596 */
1597 sqgetGlobalVar('align',$align,SQ_SESSION);
1598
1599 $oTemplate = new Template($sTplDir);
1600
1601 if ($aMailbox['EXISTS'] > 0) {
1602 if ($iError) {
1603 // TODO
1604 echo "ERROR occured, errorhandler will be implemented very soon";
1605 } else {
1606 foreach ($aTemplate as $k => $v) {
1607 $oTemplate->assign($k, $v);
1608 }
1609 $oTemplate->assign('page_selector', $page_selector);
1610 $oTemplate->assign('page_selector_max', $page_selector_max);
1611 $oTemplate->assign('compact_paginator', $compact_paginator);
1612 $oTemplate->assign('javascript_on', $javascript_on);
1613 $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false);
1614 // Aaaaaahhhhhhh FIX ME DO NOT USE the string "none" for a var when you mean the boolean false or null
1615 $oTemplate->assign('icon_theme', (isset($icon_theme) && $icon_theme !== 'none') ? $icon_theme : false);
1616 $oTemplate->assign('use_icons', (isset($use_icons)) ? $use_icons : false);
1617 $oTemplate->assign('aOrder', array_keys($aColumns));
1618 $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false);
1619 $oTemplate->assign('color', $color);
1620 $oTemplate->assign('align', $align);
1621
1622 $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
1623 if (strtoupper($mbx) == 'INBOX') {
1624 $mailbox_display = _("INBOX");
1625 } else {
1626 $mailbox_display = imap_utf7_decode_local($mbx);
1627 }
1628
1629 echo '<br /><b><big>' . _("Folder:") . ' '. $mailbox_display . '&nbsp;</big></b>';
1630
1631 $oTemplate->display('message_list.tpl');
1632 }
1633 }
1634 }
1635
1636 /* add the mailbox to the cache */
1637 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1638
1639 }
1640 }
1641 if(!$iMsgCnt) {
1642 echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1643 }
1644 }
1645
1646 do_hook('search_bottom');
1647 sqimap_logout($imapConnection);
1648 echo '</body></html>';
1649 sqsession_register($mailbox_cache,'mailbox_cache');
1650
1651 ?>