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