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