934d13b04d2d94cb19be6c469a82c9a1964e8557
[squirrelmail.git] / src / search.php
1 <?php
2
3 /**
4 * search.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Originally by Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
10 */
11
12 /* Path for SquirrelMail required files. */
13 define('SM_PATH','../');
14
15 /* SquirrelMail required files. */
16 require_once(SM_PATH . 'include/validate.php');
17 require_once(SM_PATH . 'functions/imap.php');
18 require_once(SM_PATH . 'functions/imap_asearch.php');
19 require_once(SM_PATH . 'functions/imap_mailbox.php');
20 require_once(SM_PATH . 'functions/strings.php');
21
22 function asearch_unhtml_strcoll($a, $b)
23 {
24 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
25 }
26
27 function imap_get_mailbox_display($mailbox)
28 {
29 if ($mailbox == 'INBOX')
30 return _("INBOX");
31 return imap_utf7_decode_local($mailbox);
32 }
33
34 function asearch_get_mailbox_display($mailbox)
35 {
36 if ($mailbox == 'All Folders')
37 return _("All Folders");
38 return imap_get_mailbox_display($mailbox);
39 }
40
41 function asearch_title_display($color, $txt)
42 {
43 return '<b><big>' . $txt . '</big></b>';
44 }
45
46 function asearch_error_display($color, $txt)
47 {
48 /*return '<font color="' . $color[11] . '">' . '<b><big>' . $txt . '</big></b></font>';*/
49 return '<b><big>' . $txt . '</big></b>';
50 }
51
52 function asearch_serialize($input_array)
53 {
54 /*return $input_array[0];*/
55 return serialize($input_array);
56 }
57
58 function asearch_unserialize($input_string)
59 {
60 /*return array($input_string);*/
61 return unserialize($input_string);
62 }
63
64 function asearch_getPref($data_dir, $username, $key, $index, $default = '')
65 {
66 return getPref($data_dir, $username, $key . $index, $default);
67 }
68
69 function asearch_setPref($data_dir, $username, $key, $index, $value)
70 {
71 return setPref($data_dir, $username, $key . $index, $value);
72 }
73
74 function asearch_removePref($data_dir, $username, $key, $index)
75 {
76 return removePref($data_dir, $username, $key . $index);
77 }
78
79 /* sanity checks, done before running the imap command and before push_recent */
80 function asearch_check_query()
81 {
82 global $where_array, $exclude_array;
83 if (empty($where_array))
84 return _("Please enter something to search for");
85 if (count($exclude_array) == count($where_array))
86 return _("There must be at least one criteria to search for");
87 return '';
88 }
89
90 /* read the recent searches */
91 function asearch_read_recent($data_dir, $username)
92 {
93 global $recent_prefkeys;
94
95 $recent_array = array();
96 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
97 for ($recent_num = 0; $recent_num < $recent_max; $recent_num++) {
98 foreach ($recent_prefkeys as $prefkey) {
99 $pref = asearch_getPref($data_dir, $username, $prefkey, $recent_num);
100 /* if (!empty($pref))*/
101 $recent_array[$prefkey][$recent_num] = $pref;
102 }
103 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
104 foreach ($recent_prefkeys as $key) {
105 array_pop($recent_array[$key]);
106 }
107 break;
108 }
109 }
110 return $recent_array;
111 }
112
113 /* get the saved searches */
114 function asearch_read_saved($data_dir, $username)
115 {
116 global $saved_prefkeys;
117
118 $saved_array = array();
119 $saved_key = $saved_prefkeys[0];
120 for ($saved_count = 0; ; $saved_count++) {
121 $pref = asearch_getPref($data_dir, $username, $saved_key, $saved_count);
122 if (empty($pref))
123 break;
124 }
125 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) {
126 foreach ($saved_prefkeys as $key) {
127 $saved_array[$key][$saved_num] = asearch_getPref($data_dir, $username, $key, $saved_num);
128 }
129 }
130 return $saved_array;
131 }
132
133 /* save a recent search */
134 function asearch_save_recent($data_dir, $username, $recent_index)
135 {
136 global $recent_prefkeys, $saved_prefkeys;
137
138 $saved_array = asearch_read_saved($data_dir, $username);
139 $saved_index = count($saved_array[$saved_prefkeys[0]]);
140 $recent_array = asearch_read_recent($data_dir, $username);
141 $n = 0;
142 foreach ($recent_prefkeys as $key) {
143 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
144 if (!empty($recent_slice[0]))
145 asearch_setPref($data_dir, $username, $saved_prefkeys[$n], $saved_index, $recent_slice[0]);
146 else
147 asearch_removePref($data_dir, $username, $saved_prefkeys[$n], $saved_index);
148 $n++;
149 }
150 }
151
152 function asearch_write_recent($data_dir, $username, $recent_array)
153 {
154 global $recent_prefkeys;
155
156 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
157 $recent_count = min($recent_max, count($recent_array[$recent_prefkeys[0]]));
158 for ($recent_num=0; $recent_num < $recent_count; $recent_num++) {
159 foreach ($recent_prefkeys as $key) {
160 asearch_setPref($data_dir, $username, $key, $recent_num, $recent_array[$key][$recent_num]);
161 }
162 }
163 for (; $recent_num < $recent_max; $recent_num++) {
164 foreach ($recent_prefkeys as $key) {
165 asearch_removePref($data_dir, $username, $key, $recent_num);
166 }
167 }
168 }
169
170 /* forget a recent search */
171 function asearch_forget_recent($data_dir, $username, $forget_index)
172 {
173 global $recent_prefkeys;
174
175 $recent_array = asearch_read_recent($data_dir, $username);
176 foreach ($recent_prefkeys as $key) {
177 array_splice($recent_array[$key], $forget_index, 1);
178 }
179 asearch_write_recent($data_dir, $username, $recent_array);
180 }
181
182 function asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
183 {
184 global $recent_prefkeys;
185
186 $mailbox_string = asearch_serialize($mailbox_array);
187 $biop_string = asearch_serialize($biop_array);
188 $unop_string = asearch_serialize($unop_array);
189 $where_string = asearch_serialize($where_array);
190 $what_string = asearch_serialize($what_array);
191 $exclude_string = asearch_serialize($exclude_array);
192 $recent_count = count($recent_array[$recent_prefkeys[0]]);
193 for ($recent_num=0; $recent_num<$recent_count; $recent_num++) {
194 if (isset($recent_array[$recent_prefkeys[0]][$recent_num])) {
195 if (
196 $mailbox_string == $recent_array['asearch_recent_mailbox'][$recent_num] &&
197 $biop_string == $recent_array['asearch_recent_biop'][$recent_num] &&
198 $unop_string == $recent_array['asearch_recent_unop'][$recent_num] &&
199 $where_string == $recent_array['asearch_recent_where'][$recent_num] &&
200 $what_string == $recent_array['asearch_recent_what'][$recent_num] &&
201 $exclude_string == $recent_array['asearch_recent_exclude'][$recent_num]
202 )
203 return TRUE;
204 }
205 }
206 return FALSE;
207 }
208
209 /* push a recent search */
210 function asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
211 {
212 global $recent_prefkeys;
213
214 $recent_max = getPref($data_dir, $username, 'search_memory', 0);
215 if ($recent_max > 0) {
216 $recent_array = asearch_read_recent($data_dir, $username);
217 if (!asearch_recent_exists($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)) {
218 $input = array($where_array, $mailbox_array, $what_array, $biop_array, $unop_array, $exclude_array);
219 $i = 0;
220 foreach ($recent_prefkeys as $key) {
221 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
222 $i++;
223 }
224 asearch_write_recent($data_dir, $username, $recent_array);
225 }
226 }
227 }
228
229 /* edit a recent search */
230 function asearch_edit_recent($data_dir, $username, $index)
231 {
232 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
233
234 $mailbox_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_mailbox', $index));
235 $biop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_biop', $index));
236 $unop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_unop', $index));
237 $where_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_where', $index));
238 $what_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_what', $index));
239 $exclude_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_recent_exclude', $index));
240 }
241
242 /* edit the last recent search if the prefs permit it */
243 function asearch_edit_last($data_dir, $username)
244 {
245 if (getPref($data_dir, $username, 'search_memory', 0) > 0)
246 asearch_edit_recent($data_dir, $username, 0);
247 }
248
249 /* edit a saved search */
250 function asearch_edit_saved($data_dir, $username, $index)
251 {
252 global $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array;
253
254 $mailbox_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_mailbox', $index));
255 $biop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_biop', $index));
256 $unop_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_unop', $index));
257 $where_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_where', $index));
258 $what_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_what', $index));
259 $exclude_array = asearch_unserialize(asearch_getPref($data_dir, $username, 'asearch_saved_exclude', $index));
260 }
261
262 function asearch_write_saved($data_dir, $username, $saved_array)
263 {
264 global $saved_prefkeys;
265
266 $saved_count = count($saved_array[$saved_prefkeys[0]]);
267 for ($saved_num=0; $saved_num < $saved_count; $saved_num++) {
268 foreach ($saved_prefkeys as $key) {
269 asearch_setPref($data_dir, $username, $key, $saved_num, $saved_array[$key][$saved_num]);
270 }
271 }
272 foreach ($saved_prefkeys as $key) {
273 asearch_removePref($data_dir, $username, $key, $saved_count);
274 }
275 }
276
277 /* delete a saved search */
278 function asearch_delete_saved($data_dir, $username, $saved_index)
279 {
280 global $saved_prefkeys;
281
282 $saved_array = asearch_read_saved($data_dir, $username);
283 $asearch_keys = $saved_prefkeys;
284 foreach ($asearch_keys as $key) {
285 array_splice($saved_array[$key], $saved_index, 1);
286 }
287 asearch_write_saved($data_dir, $username, $saved_array);
288 }
289
290 /* translate the input date to imap date to local date display, so the user can know if the date is wrong or illegal */
291 function asearch_get_date_display($what)
292 {
293 $what_parts = sqimap_asearch_parse_date($what);
294 if (count($what_parts) == 4) {
295 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3])) {
296 $what_display = date_intl(_("M j, Y"),mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
297 /*$what_display = $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];*/
298 }
299 else
300 $what_display = _("(Illegal date)");
301 }
302 else
303 $what_display = _("(Wrong date)");
304 return $what_display;
305 }
306
307 /* translate the query to rough natural display */
308 function asearch_build_query_display($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array)
309 {
310 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
311 global $imap_asearch_opcodes;
312
313 $last_mailbox = $mailbox_array[0];
314 if (empty($last_mailbox))
315 $last_mailbox = 'INBOX';
316 $asearch_string = '';
317 for ($cur_crit=0; $cur_crit < count($where_array); $cur_crit++) {
318 if ((!isset($exclude_array[$cur_crit])) || (!$exclude_array[$cur_crit])) {
319 $cur_mailbox = $mailbox_array[$cur_crit];
320 if (empty($cur_mailbox))
321 $cur_mailbox = 'INBOX';
322 $biop = asearch_nz($biop_array[$cur_crit]);
323 if (($asearch_string == '') || ($cur_mailbox != $last_mailbox)) {
324 $mailbox_display = ' <B>' . asearch_get_mailbox_display($cur_mailbox) . '</B>';
325 if ($asearch_string == '')
326 $biop_display = _("In");
327 else
328 $biop_display = $imap_asearch_biops_in[$biop];
329 $last_mailbox = $cur_mailbox;
330 }
331 else {
332 $mailbox_display = '';
333 $biop_display = $imap_asearch_biops[$biop];
334 }
335 $biop_display = ' <U><I>' . $biop_display . '</I></U>';
336 $unop = $unop_array[$cur_crit];
337 $unop_display = $imap_asearch_unops[$unop];
338 $where = $where_array[$cur_crit];
339 $where_display = $imap_asearch_options[$where];
340 if ($unop_display != '')
341 $where_display = ' <U><I>' . $unop_display . ' ' . $where_display . '</I></U>';
342 else
343 $where_display = ' <U><I>' . $where_display . '</I></U>';
344 $what_type = $imap_asearch_opcodes[$where];
345 if ($what_type) { /* Check if this opcode needs a parameter */
346 $what = $what_array[$cur_crit];
347 if ($what_type == 'adate')
348 $what_display = asearch_get_date_display($what);
349 else {
350 if ($what == '')
351 $what_display = '<I>' . _("(Empty string)") . '</I>';
352 else
353 $what_display = htmlspecialchars($what);
354 }
355 $what_display = ' <B>' . $what_display . '</B>';
356 }
357 else
358 $what_display = '';
359 $asearch_string .= ' ' . $biop_display . $mailbox_display . $where_display . $what_display;
360 }
361 }
362 return $asearch_string;
363 }
364
365 /* Handle the alternate row colors */
366 function asearch_get_row_color($color, $row_num)
367 {
368 /*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
369 $color_string = $color[4];
370 if ($GLOBALS['alt_index_colors']) {
371 if (($row_num % 2) == 0) {
372 if (!isset($color[12]))
373 $color[12] = '#EAEAEA';
374 $color_string = $color[12];
375 }
376 }
377 return $color_string;
378 }
379
380 /* Print a whole query array, recent or saved */
381 function asearch_print_query_array($query_array, $query_keys, $action_array, $title)
382 {
383 global $color;
384
385 echo "<br>\n";
386 echo html_tag( 'table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"' );
387 echo html_tag( 'tr', html_tag( 'td', asearch_title_display($color, $title), 'center', $color[5], 'colspan=5'));
388 $main_key = $query_keys[0];
389 $query_count = count($query_array[$main_key]);
390 for ($query_num=0, $row_num=0; $query_num<$query_count; $query_num++) {
391 if (!empty($query_array[$main_key][$query_num])) {
392 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
393
394 unset($search_array);
395 foreach ($query_keys as $query_key) {
396 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
397 }
398 $mailbox_array = $search_array[1];
399 $biop_array = $search_array[3];
400 $unop_array = $search_array[4];
401 $where_array = $search_array[0];
402 $what_array = $search_array[2];
403 $exclude_array = $search_array[5];
404 $query_display = asearch_build_query_display($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
405
406 echo html_tag( 'td', $query_num+1, 'right');
407 echo html_tag( 'td', $query_display, 'center', '', 'width="80%"');
408 foreach ($action_array as $action => $action_display) {
409 echo html_tag('td', '<a href=search.php?submit=' . $action . '&amp;rownum=' . $query_num . '>' . $action_display . '</a>', 'center');
410 }
411
412 echo '</tr>' . "\n";
413 $row_num++;
414 }
415 }
416 echo '</table>' . "\n";
417 }
418
419 /* print the saved array */
420 function asearch_print_saved($data_dir, $username)
421 {
422 global $saved_prefkeys;
423
424 $saved_array = asearch_read_saved($data_dir, $username);
425 if (isset($saved_prefkeys)) {
426 if (isset($saved_array[$saved_prefkeys[0]])) {
427 $saved_count = count($saved_array[$saved_prefkeys[0]]);
428 } else { $saved_count = 0; }
429 } else {
430 $saved_count = 0;
431 }
432
433 if ($saved_count > 0) {
434 $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
435 asearch_print_query_array($saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"));
436 }
437 }
438
439 /* print the recent array */
440 function asearch_print_recent($data_dir, $username)
441 {
442 global $recent_prefkeys;
443
444 $recent_array = asearch_read_recent($data_dir, $username);
445 if (isset($recent_array[$recent_prefkeys[0]])) {
446 $recent_count = count($recent_array[$recent_prefkeys[0]]);
447 } else {
448 $recent_count = 0;
449 }
450 $recent_max = min($recent_count, getPref($data_dir, $username, 'search_memory', 0));
451 if ($recent_max > 0) {
452 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
453 asearch_print_query_array($recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"));
454 }
455 }
456
457 /* build an <option> stuff */
458 function asearch_opt( $val, $sel, $tit )
459 {
460 return '<option value="' . $val . '"' . ($sel == $val ? ' selected' : '' ) . '>' . $tit . '</option>' . "\n";
461 }
462
463 /* build a <select> stuff from an array */
464 function asearch_opt_array($var_name, $opt_array, $cur_val)
465 {
466 $output = '<select name="' . $var_name . '">' . "\n";
467 foreach($opt_array as $val => $display)
468 $output .= asearch_opt($val, $cur_val, $display);
469 $output .= '</select>' . "\n";
470 return $output;
471 }
472
473 /* print one form row */
474 function asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $rownum)
475 {
476 global $imap_asearch_biops_in, $imap_asearch_unops, $imap_asearch_options;
477 global $color;
478
479 echo html_tag( 'tr', '', '', /*($rownum%2 ? $color[0] :*/ $color[4]/*)*/ );
480
481 echo html_tag( 'td', '', 'center' );
482 /* Binary operator */
483 if ($rownum)
484 echo asearch_opt_array('biop[' . $rownum . ']', $imap_asearch_biops_in, $biop);
485 else
486 echo /*'<input type="hidden" name="biop[0]" value="">' .*/ '<b>' . _("In") . '</b>';
487 echo "</td>\n";
488
489 echo html_tag( 'td', '', 'center' );
490 /* Mailbox list */
491 echo '<select name="mailbox[' . $rownum . ']">' . '<option value="All Folders"';
492 if ($mailbox == 'All Folders')
493 echo ' selected';
494 echo '>[ ' . asearch_get_mailbox_display('All Folders') . " ]</option>\n";
495 echo sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes);
496 echo '</select></td>' . "\n";
497
498 /* Unary operator and Search location */
499 if (empty($where))
500 $where = 'FROM';
501 echo html_tag( 'td',
502 asearch_opt_array('unop[' . $rownum . ']', $imap_asearch_unops, $unop)
503 . asearch_opt_array('where[' . $rownum . ']', $imap_asearch_options, $where),
504 'center');
505
506 /* Text input */
507 /* This is the original stuff. Except it doesn't work (eg commas are lost), why so much trouble?
508 $what_disp = str_replace(',', ' ', $what);
509 $what_disp = str_replace('\\\\', '\\', $what_disp);
510 $what_disp = str_replace('\\"', '"', $what_disp);
511 $what_disp = str_replace('"', '&quot;', $what_disp);*/
512 $what_disp = htmlspecialchars($what, ENT_QUOTES);
513 echo html_tag( 'td', '<input type="text" size="35" name="what[' . $rownum . ']" value="' . $what_disp . '">', 'center' ) . "\n";
514
515 /* Exclude criteria */
516 echo html_tag( 'td',
517 _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $rownum .']"' . ($exclude ? ' CHECKED' : '') . '>', 'center', '') . "\n";
518
519 echo "</tr>\n";
520 }
521
522 /* print the search form */
523 function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $add_criteria)
524 {
525 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
526 global $color;
527
528 /* Search Form */
529 echo "<br>\n";
530 echo '<form action="search.php" name="form_asearch">' . "\n";
531
532 echo html_tag( 'table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"' );
533 echo html_tag( 'tr', html_tag( 'td', asearch_title_display($color, _("Search Criterias")), 'center', $color[5], 'colspan=5'));
534 $row_count = count($where_array) + $add_criteria;
535 $mailbox = '';
536 for ($row_num = 0; $row_num < $row_count; $row_num++) {
537 /* Keep the last non-empty mailbox as default choice */
538 if (strip_tags($mailbox_array[$row_num]) != '')
539 $mailbox = strip_tags($mailbox_array[$row_num]);
540 if ($mailbox == '')
541 $mailbox = $boxes[0]['unformatted'];
542 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
543 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
544 $where = strip_tags(asearch_nz($where_array[$row_num]));
545 $what = asearch_nz($what_array[$row_num]);
546 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
547 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num);
548 }
549 echo '</table>' . "\n";
550
551 /* Submit buttons */
552 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"' );
553 echo html_tag('tr',
554 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
555 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
556 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
557 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
558 );
559 echo '</table>' . "\n";
560 echo '</form>' . "\n";
561 }
562
563 /* print the $msgs messages from $mailbox mailbox */
564 /* this is almost the original code */
565 function asearch_print_mailbox_msgs($msgs, $mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false)
566 {
567 global $sort, $color;
568
569 if ($cnt > 0) {
570 $msort = calc_msort($msgs, $sort);
571 $showbox = asearch_get_mailbox_display($mailbox);
572 echo html_tag( 'div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
573
574 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
575 $toggle_all = get_selectall_link(1, $sort);
576
577 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
578
579 echo '<tr><td>';
580 mail_message_listing_beginning($imapConnection, $mailbox, $sort, $msg_cnt_str, $toggle_all, 1);
581 echo '</td></tr>';
582
583 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
584
585 echo '<tr><td>';
586 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
587 echo ' <tr><td>';
588
589 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
590 echo ' <tr><td>';
591 printHeader($mailbox, 6, $color, false);
592 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $sort, $color, $cnt, $where, $what);
593 echo ' </td></tr>';
594 echo ' </table>';
595 echo ' </td></tr>';
596 echo ' </table>';
597 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
598 echo '</td></tr>';
599
600 echo '</table>';
601 }
602 }
603
604 /* ------------------------ main ------------------------ */
605 global $allow_thread_sort;
606
607 /* get globals we may need */
608
609 sqgetGlobalVar('username', $username, SQ_SESSION);
610 sqgetGlobalVar('key', $key, SQ_COOKIE);
611 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
612 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
613
614 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude');
615 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude');
616 /*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude');*/
617
618 $search_button_html = _("Search");
619 $search_button_text = asearch_unhtmlentities($search_button_html);
620 $add_criteria_button_html = _("Add New Criteria");
621 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
622 $del_excluded_button_html = _("Remove Excluded Criteria(s)");
623 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
624 $del_all_button_html = _("Remove All Criteria(s)");
625 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
626
627 $imap_asearch_options = array(
628 /* <message set>, */
629 /*'ALL' is binary operator */
630 'ANSWERED' => _("Answered"),
631 'BCC' => _("Bcc"),
632 'BEFORE' => _("Before"),
633 'BODY' => _("Message Body"),
634 'CC' => _("CC"),
635 'DELETED' => _("Deleted"),
636 'DRAFT' => _("Draft"),
637 'FLAGGED' => _("Flagged"),
638 'FROM' => _("Sent By"),
639 'HEADER' => _("Header Field"),
640 'KEYWORD' => _("Keyword"),
641 'LARGER' => _("Larger Than"),
642 'NEW' => _("New"),
643 /*'NOT' is unary operator */
644 'OLD' => _("Old"),
645 'ON' => _("On"),
646 /*'OR' is binary operator */
647 'RECENT' => _("Recent"),
648 'SEEN' => _("Seen"),
649 'SENTBEFORE' => _("Sent Before"),
650 'SENTON' => _("Sent On"),
651 'SENTSINCE' => _("Sent Since"),
652 'SINCE' => _("Since"),
653 'SMALLER' => _("Smaller Than"),
654 'SUBJECT' => _("Subject Contains"),
655 'TEXT' => _("Header and Body"),
656 'TO' => _("Sent To"),
657 /*'UID' => 'anum',*/
658 /*'UNANSWERED' => '',
659 'UNDELETED' => '',
660 'UNDRAFT' => '',
661 'UNFLAGGED' => '',
662 'UNKEYWORD' => _("Unkeyword"),
663 'UNSEEN' => _("Unseen"),*/
664 );
665 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
666
667 $imap_asearch_unops = array(
668 '' => '',
669 'NOT' => _("Not")
670 );
671
672 $imap_asearch_biops_in = array(
673 'ALL' => _("And In"),
674 'OR' => _("Or In")
675 );
676
677 $imap_asearch_biops = array(
678 'ALL' => _("And"),
679 'OR' => _("Or")
680 );
681
682 /*
683 unset : Enter key, or called from outside (eg read_body)
684 $search_button_text : Search button
685 'Search_no_update' : Search but don't update recent
686 'Search_last' : Same as no_update but reload and search last
687 'Search_silent' : Same as no_update but only display results
688 $add_criteria_button_text : Add New Criteria button
689 $del_excluded_button_text : Remove Excluded Criteria(s) button
690 $del_all_button_text : Remove All Criteria(s) button
691 'save_recent'
692 'search_recent'
693 'forget_recent'
694 'edit_saved'
695 'search_saved'
696 'delete_saved'
697 */
698 if (isset($_GET['submit']))
699 $submit = strip_tags($_GET['submit']);
700
701 /* Used by search */
702 if (isset($_GET['mailbox'])) {
703 $mailbox_array = $_GET['mailbox'];
704 if (!is_array($mailbox_array))
705 $mailbox_array = array($mailbox_array);
706 }
707 else
708 $mailbox_array = array();
709
710 if (isset($_GET['biop'])) {
711 $biop_array = $_GET['biop'];
712 if (!is_array($biop_array))
713 $biop_array = array($biop_array);
714 }
715 else
716 $biop_array = array();
717
718 if (isset($_GET['unop'])) {
719 $unop_array = $_GET['unop'];
720 if (!is_array($unop_array))
721 $unop_array = array($unop_array);
722 }
723 else
724 $unop_array = array();
725
726 if (isset($_GET['where'])) {
727 $where_array = $_GET['where'];
728 if (!is_array($where_array))
729 $where_array = array($where_array);
730 }
731 else
732 $where_array = array();
733
734 if (isset($_GET['what'])) {
735 $what_array = $_GET['what'];
736 if (!is_array($what_array))
737 $what_array = array($what_array);
738 }
739 else
740 $what_array = array();
741
742 if (isset($_GET['exclude']))
743 $exclude_array = $_GET['exclude'];
744 else
745 $exclude_array = array();
746
747 /* Used by recent and saved stuff */
748 if (isset($_GET['rownum'])) {
749 $submit_rownum = strip_tags($_GET['rownum']);
750 }
751
752 /* end of get globals */
753
754 $search_silent = FALSE; /* Default is normal behaviour */
755
756 /* See how the page was called and fire off correct function */
757 if ((!isset($submit) || empty($submit)) && !empty($where_array)) {
758 /* This happens when the Enter key is used or called from outside */
759 $submit = $search_button_text;
760 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
761 asearch_edit_last($data_dir, $username);
762 }
763
764 if (!isset($submit)) {
765 $submit = '';
766 }
767 else {
768 switch ($submit) {
769 case $search_button_text:
770 if (asearch_check_query() == '')
771 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
772 break;
773 case 'Search_silent':
774 $search_silent = TRUE;
775 /*nobreak;*/
776 case 'Search_no_update':
777 $submit = $search_button_text;
778 break;
779 case $del_excluded_button_text:
780 $delarray = array_keys($exclude_array);
781 while (!empty($delarray)) {
782 $delrow = array_pop($delarray);
783 array_splice($mailbox_array, $delrow, 1);
784 array_splice($biop_array, $delrow, 1);
785 array_splice($unop_array, $delrow, 1);
786 array_splice($where_array, $delrow, 1);
787 array_splice($what_array, $delrow, 1);
788 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
789 }
790 $exclude_array = array();
791 break;
792 case $del_all_button_text:
793 $mailbox_array = array();
794 $biop_array = array();
795 $unop_array = array();
796 $where_array = array();
797 $what_array = array();
798 $exclude_array = array();
799 break;
800 case 'save_recent':
801 asearch_save_recent($data_dir, $username, $submit_rownum);
802 break;
803 case 'search_recent':
804 $submit = $search_button_text;
805 /*nobreak;*/
806 case 'edit_recent': /* no link to do this, yet */
807 asearch_edit_recent($data_dir, $username, $submit_rownum);
808 break;
809 case 'forget_recent':
810 asearch_forget_recent($data_dir, $username, $submit_rownum);
811 break;
812 case 'search_saved':
813 $submit = $search_button_text;
814 /*nobreak;*/
815 case 'edit_saved':
816 asearch_edit_saved($data_dir, $username, $submit_rownum);
817 break;
818 case 'delete_saved':
819 asearch_delete_saved($data_dir, $username, $submit_rownum);
820 break;
821 }
822 }
823
824 /* open IMAP connection */
825 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
826 /* get mailbox names once here */
827 $boxes = sqimap_mailbox_list($imapConnection);
828
829 if (isset($mailbox_array[0])) {
830 $mailbox = strip_tags($mailbox_array[0]);
831 }
832 if (empty($mailbox))
833 $mailbox = $boxes[0]['unformatted'];
834 if (isset($composenew) && $composenew) {
835 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
836 "&amp;session=$composesession&amp;attachedmessages=true&amp";
837 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
838 }
839 else
840 displayPageHeader($color, $mailbox);
841
842 do_hook('search_before_form');
843
844 if (!$search_silent) {
845 echo html_tag( 'table',
846 html_tag( 'tr', "\n" .
847 html_tag( 'td', asearch_title_display($color, _("Advanced Search")), 'center', $color[0] )
848 ) ,
849 '', '', 'width="100%"') . "\n";
850 asearch_print_saved($data_dir, $username);
851 asearch_print_recent($data_dir, $username);
852 if (($submit == $add_criteria_button_text) || (empty($where_array)))
853 $new_row = 1;
854 else
855 $new_row = 0;
856 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $new_row);
857 }
858
859 /* This deserves a comment, at least. What is it for exactly? */
860 if (isset($newsort)) {
861 $sort = $newsort;
862 sqsession_register($sort, 'sort');
863 }
864
865 /*********************************************************************
866 * Check to see if we can use cache or not. Currently the only time *
867 * when you will not use it is when a link on the left hand frame is *
868 * used. Also check to make sure we actually have the array in the *
869 * registered session data. :) *
870 *********************************************************************/
871 if (! isset($use_mailbox_cache)) {
872 $use_mailbox_cache = 0;
873 }
874
875 /* There is a problem with registered vars in 4.1 */
876 /*
877 if( substr( phpversion(), 0, 3 ) == '4.1' ) {
878 $use_mailbox_cache = FALSE;
879 }
880 */
881
882 do_hook('search_after_form');
883
884 if ($submit == $search_button_text) {
885 $query_error = asearch_check_query();
886 if ($query_error != '')
887 echo '<br>' . html_tag( 'div', asearch_error_display($color, $query_error), 'center' ) . "\n";
888 else {
889 echo html_tag( 'table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"' );
890 echo html_tag( 'tr', html_tag( 'td', asearch_title_display($color, _("Search Results")), 'center', $color[5]));
891 echo html_tag( 'tr', html_tag( 'td', asearch_build_query_display($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array), 'center', $color[4]));
892 echo '</table><br>' . "\n";
893
894 $old_allow_thread_sort = 0;
895 if ($allow_thread_sort == TRUE) {
896 $old_allow_thread_sort = $allow_thread_sort;
897 $allow_thread_sort = FALSE;
898 }
899
900 $boxcount = count($boxes);
901 for ($boxnum=0; $boxnum<$boxcount; $boxnum++) {
902 if (!in_array('noselect', $boxes[$boxnum]['flags']))
903 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
904 }
905
906 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array);
907 if (empty($mboxes_msgs))
908 echo '<br>' . html_tag( 'div', asearch_error_display($color, _("No Messages Found")), 'center' ) . "\n";
909 else {
910 foreach($mboxes_msgs as $mailbox => $msgs) {
911 sqimap_mailbox_select($imapConnection, $mailbox);
912 $msgs = fillMessageArray($imapConnection, $msgs, count($msgs));
913 /* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
914 /* $where = asearch_serialize($where_array);
915 $what = asearch_serialize($what_array);*/
916 $where = $where_array[0];
917 $what = $what_array[0];
918 asearch_print_mailbox_msgs($msgs, $mailbox, count($msgs), $imapConnection, urlencode($where), urlencode($what), false, false);
919 }
920 }
921
922 $allow_thread_sort = $old_allow_thread_sort;
923 }
924 }
925
926
927 do_hook('search_bottom');
928 sqimap_logout($imapConnection);
929 echo '</body></html>';
930
931 ?>