Replace all session_start() calls with sqsession_is_active(). The latter
[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 if (empty($where))
515 $where = 'FROM';
516 echo html_tag('td',
517 asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
518 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where),
519 'center');
520
521 /* Text input */
522 /* This is the original stuff. Except it doesn't work (eg commas are lost), why so much trouble?
523 $what_disp = str_replace(',', ' ', $what);
524 $what_disp = str_replace('\\\\', '\\', $what_disp);
525 $what_disp = str_replace('\\"', '"', $what_disp);
526 $what_disp = str_replace('"', '&quot;', $what_disp);*/
527 $what_disp = htmlspecialchars($what);
528 echo html_tag('td', '<input type="text" size="35" name="what[' . $row_num . ']" value="' . $what_disp . '">', 'center') . "\n";
529
530 /* Exclude criteria */
531 echo html_tag('td',
532 _("Exclude Criteria:") . '<input type=checkbox name="exclude[' . $row_num .']"' . ($exclude ? ' CHECKED' : '') . '>', 'center', '') . "\n";
533
534 echo "</tr>\n";
535 }
536
537 /* print the search form */
538 function asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $add_criteria)
539 {
540 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
541 global $color;
542
543 /* Search Form */
544 echo "<br>\n";
545 echo '<form action="search.php" name="form_asearch">' . "\n";
546
547 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
548 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
549 $row_count = count($where_array) + $add_criteria;
550 $mailbox = '';
551 for ($row_num = 0; $row_num < $row_count; $row_num++) {
552 /* Keep the last non-empty mailbox as default choice */
553 if (strip_tags(asearch_nz($mailbox_array[$row_num])) != '')
554 $mailbox = strip_tags($mailbox_array[$row_num]);
555 if ($mailbox == '')
556 $mailbox = $boxes[0]['unformatted'];
557 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
558 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
559 $where = asearch_nz($where_array[$row_num]);
560 $what = asearch_nz($what_array[$row_num]);
561 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
562 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $row_num);
563 }
564 echo '</table>' . "\n";
565
566 /* Submit buttons */
567 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
568 echo html_tag('tr',
569 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
570 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
571 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
572 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
573 );
574 echo '</table>' . "\n";
575 echo '</form>' . "\n";
576 }
577
578 /* print the $msgs messages from $mailbox mailbox */
579 /* this is almost the original code */
580 function asearch_print_mailbox_msgs($msgs, $mailbox, $cnt, $imapConnection, $where, $what, $usecache = false, $newsort = false)
581 {
582 global $sort, $color;
583
584 if ($cnt > 0) {
585 $msort = calc_msort($msgs, $sort);
586 $showbox = asearch_get_mailbox_display($mailbox);
587 echo html_tag('div', '<b><big>' . _("Folder:") . ' '. $showbox.'</big></b>','center') . "\n";
588
589 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
590 $toggle_all = get_selectall_link(1, $sort);
591
592 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
593
594 echo '<tr><td>';
595 mail_message_listing_beginning($imapConnection, $mailbox, $sort, $msg_cnt_str, $toggle_all, 1);
596 echo '</td></tr>';
597
598 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
599
600 echo '<tr><td>';
601 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
602 echo ' <tr><td>';
603
604 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
605 echo ' <tr><td>';
606 printHeader($mailbox, 6, $color, false);
607 displayMessageArray($imapConnection, $cnt, 1, $msort, $mailbox, $sort, $color, $cnt, $where, $what);
608 echo ' </td></tr>';
609 echo ' </table>';
610 echo ' </td></tr>';
611 echo ' </table>';
612 mail_message_listing_end($cnt, '', $msg_cnt_str, $color);
613 echo '</td></tr>';
614
615 echo '</table>';
616 }
617 }
618
619 /* ------------------------ main ------------------------ */
620 global $allow_thread_sort;
621
622 /* get globals we may need */
623 sqgetGlobalVar('username', $username, SQ_SESSION);
624 sqgetGlobalVar('key', $key, SQ_COOKIE);
625 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); /* we really need this? */
626 sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION); /* do we really need this? */
627
628 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_biop', 'asearch_recent_unop', 'asearch_recent_exclude');
629 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_biop', 'asearch_saved_unop', 'asearch_saved_exclude');
630 /*$asearch_keys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude');*/
631
632 $search_button_html = _("Search");
633 $search_button_text = asearch_unhtmlentities($search_button_html);
634 $add_criteria_button_html = _("Add New Criteria");
635 $add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
636 $del_excluded_button_html = _("Remove Excluded Criteria");
637 $del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
638 $del_all_button_html = _("Remove All Criteria");
639 $del_all_button_text = asearch_unhtmlentities($del_all_button_html);
640
641 $imap_asearch_options = array(
642 /* <message set>, */
643 /*'ALL' is binary operator */
644 'ANSWERED' => _("Answered"),
645 'BCC' => _("Bcc"),
646 'BEFORE' => _("Before"),
647 'BODY' => _("Message Body"),
648 'CC' => _("CC"),
649 'DELETED' => _("Deleted"),
650 'DRAFT' => _("Draft"),
651 'FLAGGED' => _("Flagged"),
652 'FROM' => _("Sent By"),
653 'HEADER' => _("Header Field"),
654 'KEYWORD' => _("Keyword"),
655 'LARGER' => _("Larger Than"),
656 'NEW' => _("New"),
657 /*'NOT' is unary operator */
658 'OLD' => _("Old"),
659 'ON' => _("On"),
660 /*'OR' is binary operator */
661 'RECENT' => _("Recent"),
662 'SEEN' => _("Seen"),
663 'SENTBEFORE' => _("Sent Before"),
664 'SENTON' => _("Sent On"),
665 'SENTSINCE' => _("Sent Since"),
666 'SINCE' => _("Since"),
667 'SMALLER' => _("Smaller Than"),
668 'SUBJECT' => _("Subject Contains"),
669 'TEXT' => _("Header and Body"),
670 'TO' => _("Sent To"),
671 /*'UID' => 'anum',*/
672 /*'UNANSWERED' => '',
673 'UNDELETED' => '',
674 'UNDRAFT' => '',
675 'UNFLAGGED' => '',
676 'UNKEYWORD' => _("Unkeyword"),
677 'UNSEEN' => _("Unseen"),*/
678 );
679 uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
680
681 $imap_asearch_unops = array(
682 '' => '',
683 'NOT' => _("Not")
684 );
685
686 $imap_asearch_biops_in = array(
687 'ALL' => _("And In"),
688 'OR' => _("Or In")
689 );
690
691 $imap_asearch_biops = array(
692 'ALL' => _("And"),
693 'OR' => _("Or")
694 );
695
696 /*
697 unset : Enter key, or called from outside (eg read_body)
698 $search_button_text : Search button
699 'Search_no_update' : Search but don't update recent
700 'Search_last' : Same as no_update but reload and search last
701 'Search_silent' : Same as no_update but only display results
702 $add_criteria_button_text : Add New Criteria button
703 $del_excluded_button_text : Remove Excluded Criteria button
704 $del_all_button_text : Remove All Criteria button
705 'save_recent'
706 'search_recent'
707 'forget_recent'
708 'edit_saved'
709 'search_saved'
710 'delete_saved'
711 */
712 if (isset($_GET['submit']))
713 $submit = strip_tags($_GET['submit']);
714
715 /* Used by search */
716 if (isset($_GET['mailbox'])) {
717 $mailbox_array = $_GET['mailbox'];
718 if (!is_array($mailbox_array))
719 $mailbox_array = array($mailbox_array);
720 }
721 else
722 $mailbox_array = array();
723
724 if (isset($_GET['biop'])) {
725 $biop_array = $_GET['biop'];
726 if (!is_array($biop_array))
727 $biop_array = array($biop_array);
728 }
729 else
730 $biop_array = array();
731
732 if (isset($_GET['unop'])) {
733 $unop_array = $_GET['unop'];
734 if (!is_array($unop_array))
735 $unop_array = array($unop_array);
736 }
737 else
738 $unop_array = array();
739
740 if (isset($_GET['where'])) {
741 $where_array = $_GET['where'];
742 if (!is_array($where_array))
743 $where_array = array($where_array);
744 }
745 else
746 $where_array = array();
747
748 if (isset($_GET['what'])) {
749 $what_array = $_GET['what'];
750 if (!is_array($what_array))
751 $what_array = array($what_array);
752 }
753 else
754 $what_array = array();
755
756 if (isset($_GET['exclude']))
757 $exclude_array = $_GET['exclude'];
758 else
759 $exclude_array = array();
760
761 /* Used by recent and saved stuff */
762 if (isset($_GET['rownum'])) {
763 $submit_rownum = strip_tags($_GET['rownum']);
764 }
765
766 /* end of get globals */
767
768 $search_silent = FALSE; /* Default is normal behaviour */
769
770 /* See how the page was called and fire off correct function */
771 if ((!isset($submit) || empty($submit)) && !empty($where_array)) {
772 /* This happens when the Enter key is used or called from outside */
773 $submit = $search_button_text;
774 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
775 asearch_edit_last($data_dir, $username);
776 }
777
778 if (!isset($submit)) {
779 $submit = '';
780 }
781 else {
782 switch ($submit) {
783 case $search_button_text:
784 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
785 asearch_push_recent($data_dir, $username, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array);
786 break;
787 case 'Search_silent':
788 $search_silent = TRUE;
789 /*nobreak;*/
790 case 'Search_no_update':
791 $submit = $search_button_text;
792 break;
793 case $del_excluded_button_text:
794 $delarray = array_keys($exclude_array);
795 while (!empty($delarray)) {
796 $delrow = array_pop($delarray);
797 array_splice($mailbox_array, $delrow, 1);
798 array_splice($biop_array, $delrow, 1);
799 array_splice($unop_array, $delrow, 1);
800 array_splice($where_array, $delrow, 1);
801 array_splice($what_array, $delrow, 1);
802 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
803 }
804 $exclude_array = array();
805 break;
806 case $del_all_button_text:
807 $mailbox_array = array();
808 $biop_array = array();
809 $unop_array = array();
810 $where_array = array();
811 $what_array = array();
812 $exclude_array = array();
813 break;
814 case 'save_recent':
815 asearch_save_recent($data_dir, $username, $submit_rownum);
816 break;
817 case 'search_recent':
818 $submit = $search_button_text;
819 /*nobreak;*/
820 case 'edit_recent': /* no link to do this, yet */
821 asearch_edit_recent($data_dir, $username, $submit_rownum);
822 break;
823 case 'forget_recent':
824 asearch_forget_recent($data_dir, $username, $submit_rownum);
825 break;
826 case 'search_saved':
827 $submit = $search_button_text;
828 /*nobreak;*/
829 case 'edit_saved':
830 asearch_edit_saved($data_dir, $username, $submit_rownum);
831 break;
832 case 'delete_saved':
833 asearch_delete_saved($data_dir, $username, $submit_rownum);
834 break;
835 }
836 }
837
838 /* open IMAP connection */
839 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
840 /* get mailbox names once here */
841 $boxes = sqimap_mailbox_list($imapConnection);
842 /* ensure we have a valid default mailbox name */
843 $mailbox = strip_tags(asearch_nz($mailbox_array[0]));
844 if (($mailbox == '') || ($mailbox == 'None')) { //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
845 $mailbox = $boxes[0]['unformatted'];
846 $mailbox_array[0] = $mailbox;
847 }
848
849 if (isset($composenew) && $composenew) {
850 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox) .
851 "&amp;session=$composesession&amp;attachedmessages=true&amp";
852 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
853 }
854 else
855 displayPageHeader($color, $mailbox);
856
857 do_hook('search_before_form');
858
859 if (!$search_silent) {
860 echo html_tag('table',
861 html_tag('tr', "\n" .
862 html_tag('td', asearch_get_title_display($color, _("Search")), 'center', $color[0])
863 ) ,
864 '', '', 'width="100%"') . "\n";
865 asearch_print_saved($data_dir, $username);
866 asearch_print_recent($data_dir, $username);
867 if (($submit == $add_criteria_button_text) || (empty($where_array)))
868 $new_row = 1;
869 else
870 $new_row = 0;
871 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $new_row);
872 }
873
874 /* This deserves a comment, at least. What is it for exactly? */
875 if (isset($newsort)) {
876 $sort = $newsort;
877 sqsession_register($sort, 'sort');
878 }
879
880 /*********************************************************************
881 * Check to see if we can use cache or not. Currently the only time *
882 * when you will not use it is when a link on the left hand frame is *
883 * used. Also check to make sure we actually have the array in the *
884 * registered session data. :) *
885 *********************************************************************/
886 if (! isset($use_mailbox_cache)) {
887 $use_mailbox_cache = 0;
888 }
889
890 /* There is a problem with registered vars in 4.1 */
891 /*
892 if(substr(phpversion(), 0, 3) == '4.1') {
893 $use_mailbox_cache = FALSE;
894 }
895 */
896
897 do_hook('search_after_form');
898
899 if ($submit == $search_button_text) {
900 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
901 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
902 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]));
903 echo '</table><br>' . "\n";
904
905 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
906 if ($query_error != '')
907 echo '<br>' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
908 else {
909 $old_allow_thread_sort = 0;
910 if ($allow_thread_sort == TRUE) {
911 $old_allow_thread_sort = $allow_thread_sort;
912 $allow_thread_sort = FALSE;
913 }
914
915 $boxcount = count($boxes);
916 for ($boxnum=0; $boxnum<$boxcount; $boxnum++) {
917 if (!in_array('noselect', $boxes[$boxnum]['flags']))
918 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
919 }
920
921 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array);
922 if (empty($mboxes_msgs))
923 echo '<br>' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
924 else {
925 foreach($mboxes_msgs as $mailbox => $msgs) {
926 sqimap_mailbox_select($imapConnection, $mailbox);
927 $msgs = fillMessageArray($imapConnection, $msgs, count($msgs));
928 /* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
929 /* $where = asearch_serialize($where_array);
930 $what = asearch_serialize($what_array);*/
931 $where = $where_array[0];
932 $what = $what_array[0];
933 asearch_print_mailbox_msgs($msgs, $mailbox, count($msgs), $imapConnection, urlencode($where), urlencode($what), false, false);
934 }
935 }
936
937 $allow_thread_sort = $old_allow_thread_sort;
938 }
939 }
940
941
942 do_hook('search_bottom');
943 sqimap_logout($imapConnection);
944 echo '</body></html>';
945
946 ?>