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