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