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