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