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