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