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