typo
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
8cc8ec79 4* search.php
5*
6* Copyright (c) 1999-2004 The SquirrelMail Project Team
7* Licensed under the GNU GPL. For full terms see the file COPYING.
8*
9* IMAP search page
10*
11* Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
12*
13* @version $Id$
14* @package squirrelmail
15* @link http://www.ietf.org/rfc/rfc3501.txt
16* @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
17*/
2d367c68 18
30967a1e 19/**
8cc8ec79 20* Path for SquirrelMail required files.
21* @ignore
22*/
86725763 23define('SM_PATH','../');
24
7d60bda5 25/** SquirrelMail required files.
8cc8ec79 26*/
08185f2a 27require_once(SM_PATH . 'include/validate.php');
b2e7eb53 28require_once(SM_PATH . 'functions/strings.php');
6b37252b 29require_once(SM_PATH . 'functions/imap_asearch.php');
86725763 30require_once(SM_PATH . 'functions/imap_mailbox.php');
b65c5db0 31require_once(SM_PATH . 'functions/imap_messages.php');
0ca9a6ec 32require_once(SM_PATH . 'functions/mime.php');
b2e7eb53 33require_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()...
c61bb006 34
7d60bda5 35/** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys
8cc8ec79 36*/
7d60bda5 37define('ASEARCH_WHERE', 0);
38define('ASEARCH_MAILBOX', 1);
39define('ASEARCH_WHAT', 2);
40define('ASEARCH_UNOP', 3);
41define('ASEARCH_BIOP', 4);
42define('ASEARCH_EXCLUDE', 5);
43define('ASEARCH_SUB', 6);
5b6abe97 44define('ASEARCH_MAX', 7);
45
46/** Name of session var
8cc8ec79 47*/
5b6abe97 48define('ASEARCH_CRITERIA', 'criteria');
7d60bda5 49
50/** Builds a href with params
8cc8ec79 51* @param string $params optional parameters to GET
52*/
7d60bda5 53function asearch_get_href($params = '')
54{
8cc8ec79 55 $href = 'search.php';
56 if ($params != '')
57 $href .= '?' . $params;
58 return $href;
7d60bda5 59}
60
61/** Builds a [link]
8cc8ec79 62* @param string $href (reference)
63* @param string $text
64* @param string $title
65*/
94375458 66function asearch_get_link(&$href, $text, $title = '')
7d60bda5 67{
8cc8ec79 68 if ($title != '')
69 $title = ' title="' . $title . '"';
70 return '<a href="' . $href . '"' . $title . '>' . $text . '</a>';
7d60bda5 71}
72
73/** Builds a toggle [link]
8cc8ec79 74* @param integer $value
75* @param string $action
76* @param array $text_array
77* @param array $title_array
78*/
94375458 79function asearch_get_toggle_link($value, $action, $text_array, $title_array = array())
7d60bda5 80{
8cc8ec79 81 return asearch_get_link(asearch_get_href($action . '=' . (int)$value), $text_array[$value], asearch_nz($title_array[$value]));
7d60bda5 82}
83
40fbe929 84/**
8cc8ec79 85* @param string $a
86* @param string $b
87* @return bool strcoll()-like result
88*/
6b37252b 89function asearch_unhtml_strcoll($a, $b)
90{
8cc8ec79 91 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
6b37252b 92}
40fbe929 93
94/**
8cc8ec79 95* @param string $mailbox mailbox name utf7 encoded inc. special case INBOX
96* @return string mailbox name ready to display (utf7 decoded or localized INBOX)
97*/
6b37252b 98function imap_get_mailbox_display($mailbox)
99{
8cc8ec79 100 if (strtoupper($mailbox) == 'INBOX')
101 return _("INBOX");
102 return imap_utf7_decode_local($mailbox);
6b37252b 103}
56e0b3b7 104
40fbe929 105/**
8cc8ec79 106* @param string $mailbox mailbox name or special case 'All Folders'
107* @return string mailbox name ready to display (utf7 decoded or localized 'All Folders')
108*/
6b37252b 109function asearch_get_mailbox_display($mailbox)
110{
8cc8ec79 111 if ($mailbox == 'All Folders')
112 return _("All Folders");
113 return imap_get_mailbox_display($mailbox);
6b37252b 114}
0b97a708 115
40fbe929 116/**
8cc8ec79 117* @param array $color color array
118* @param string $txt text to display
119* @return string title ready to display
120*/
7d60bda5 121function asearch_get_title_display(&$color, $txt)
6b37252b 122{
8cc8ec79 123 return '<b><big>' . $txt . '</big></b>';
6b37252b 124}
0b97a708 125
40fbe929 126/**
8cc8ec79 127* @param array $color color array
128* @param string $txt text to display
129* @return string error text ready to display
130*/
7d60bda5 131function asearch_get_error_display(&$color, $txt)
6b37252b 132{
8cc8ec79 133 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
0b97a708 134}
6b37252b 135
40fbe929 136/**
8cc8ec79 137* @param array $input_array array to serialize
138* @return string a string containing a byte-stream representation of value that can be stored anywhere
139*/
7d60bda5 140function asearch_serialize(&$input_array)
6b37252b 141{
8cc8ec79 142 global $search_advanced;
143 if ($search_advanced)
144 return serialize($input_array);
145 return $input_array[0];
0b97a708 146}
6b37252b 147
40fbe929 148/**
8cc8ec79 149* @param string $input_string string to unserialize
150* @return array
151*/
6b37252b 152function asearch_unserialize($input_string)
153{
8cc8ec79 154 global $search_advanced;
155 if ($search_advanced)
156 return unserialize($input_string);
157 return array($input_string);
0b97a708 158}
6b37252b 159
40fbe929 160/**
8cc8ec79 161* @param string $key the pref key
162* @param integer $index the pref key index
163* @param string $default default value
164* @return string pref value
165*/
7d60bda5 166function asearch_getPref(&$key, $index, $default = '')
6b37252b 167{
8cc8ec79 168 global $data_dir, $username, $search_advanced;
169 return getPref($data_dir, $username, $key . ($index + !$search_advanced), $default);
0b97a708 170}
6b37252b 171
40fbe929 172/**
8cc8ec79 173* @param string $key the pref key
174* @param integer $index the pref key index
175* @param string $value pref value to set
176* @return bool status
177*/
7d60bda5 178function asearch_setPref(&$key, $index, $value)
6b37252b 179{
8cc8ec79 180 global $data_dir, $username, $search_advanced;
181 return setPref($data_dir, $username, $key . ($index + !$search_advanced), $value);
0b97a708 182}
6b37252b 183
40fbe929 184/**
8cc8ec79 185* @param string $key the pref key
186* @param integer $index the pref key index
187* @return bool status
188*/
7d60bda5 189function asearch_removePref(&$key, $index)
6b37252b 190{
8cc8ec79 191 global $data_dir, $username, $search_advanced;
192 return removePref($data_dir, $username, $key . ($index + !$search_advanced));
0b97a708 193}
0b97a708 194
7d60bda5 195/** Sanity checks, done before running the imap command and before calling push_recent
8cc8ec79 196*/
7d60bda5 197function asearch_check_query(&$where_array, &$what_array, &$exclude_array)
6b37252b 198{
8cc8ec79 199 global $imap_asearch_opcodes;
05f5b952 200
8cc8ec79 201 if (empty($where_array))
202 return _("Please enter something to search for");
203 if (count($exclude_array) == count($where_array))
204 return _("There must be at least one criteria to search for");
205 for ($crit_num = 0; $crit_num < count($where_array); $crit_num++) {
206 $where = $where_array[$crit_num];
207 $what = $what_array[$crit_num];
208 if (!(($what == '') ^ ($imap_asearch_opcodes[$where] != '')))
209 return _("Error in criteria argument");
210 }
211 return '';
56e0b3b7 212}
213
7d60bda5 214/** Read the recent searches from the prefs
8cc8ec79 215*/
7d60bda5 216function asearch_read_recent()
6b37252b 217{
8cc8ec79 218 global $recent_prefkeys, $search_memory;
6b37252b 219
8cc8ec79 220 $recent_array = array();
221 $recent_num = 0;
222 for ($pref_num = 0; $pref_num < $search_memory; $pref_num++) {
223 foreach ($recent_prefkeys as $prefkey) {
224 $pref = asearch_getPref($prefkey, $pref_num);
6b37252b 225/* if (!empty($pref))*/
8cc8ec79 226 $recent_array[$prefkey][$recent_num] = $pref;
227 }
228 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
229 foreach ($recent_prefkeys as $key) {
230 array_pop($recent_array[$key]);
231 }
7d60bda5 232// break; //Disabled to support old search code broken prefs
8cc8ec79 233 }
234 else
235 $recent_num++;
236 }
237 return $recent_array;
56e0b3b7 238}
23a9084b 239
7d60bda5 240/** Read the saved searches from the prefs
8cc8ec79 241*/
7d60bda5 242function asearch_read_saved()
6b37252b 243{
8cc8ec79 244 global $saved_prefkeys;
245
246 $saved_array = array();
247 $saved_key = $saved_prefkeys[0];
248 for ($saved_count = 0; ; $saved_count++) {
249 $pref = asearch_getPref($saved_key, $saved_count);
250 if (empty($pref))
251 break;
252 }
253 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) {
254 foreach ($saved_prefkeys as $key) {
255 $saved_array[$key][$saved_num] = asearch_getPref($key, $saved_num);
256 }
257 }
258 return $saved_array;
6b37252b 259}
260
7d60bda5 261/** Save a recent search to the prefs
8cc8ec79 262*/
7d60bda5 263function asearch_save_recent($recent_index)
6b37252b 264{
8cc8ec79 265 global $recent_prefkeys, $saved_prefkeys;
266
267 $saved_array = asearch_read_saved();
268 $saved_index = count($saved_array[$saved_prefkeys[0]]);
269 $recent_array = asearch_read_recent();
270 $n = 0;
271 foreach ($recent_prefkeys as $key) {
272 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
273 if (!empty($recent_slice[0]))
274 asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]);
275 else
276 asearch_removePref($saved_prefkeys[$n], $saved_index);
277 $n++;
278 }
6b37252b 279}
280
7d60bda5 281/** Write a recent search to prefs
8cc8ec79 282*/
7d60bda5 283function asearch_write_recent(&$recent_array)
6b37252b 284{
8cc8ec79 285 global $recent_prefkeys, $search_memory;
6b37252b 286
8cc8ec79 287 $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]]));
288 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
289 foreach ($recent_prefkeys as $key) {
290 asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]);
291 }
292 }
293 for (; $recent_num < $search_memory; $recent_num++) {
294 foreach ($recent_prefkeys as $key) {
295 asearch_removePref($key, $recent_num);
296 }
297 }
6b37252b 298}
299
7d60bda5 300/** Remove a recent search from prefs
8cc8ec79 301*/
7d60bda5 302function asearch_forget_recent($forget_index)
6b37252b 303{
8cc8ec79 304 global $recent_prefkeys;
6b37252b 305
8cc8ec79 306 $recent_array = asearch_read_recent();
307 foreach ($recent_prefkeys as $key) {
308 array_splice($recent_array[$key], $forget_index, 1);
309 }
310 asearch_write_recent($recent_array);
6b37252b 311}
312
7d60bda5 313/** Find a recent search in the prefs (used to avoid saving duplicates)
8cc8ec79 314*/
7d60bda5 315function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 316{
8cc8ec79 317 global $recent_prefkeys, $search_advanced;
318
319 $where_string = asearch_serialize($where_array);
320 $mailbox_string = asearch_serialize($mailbox_array);
321 $what_string = asearch_serialize($what_array);
322 $unop_string = asearch_serialize($unop_array);
323 if ($search_advanced) {
324 $biop_string = asearch_serialize($biop_array);
325 $exclude_string = asearch_serialize($exclude_array);
326 $sub_string = asearch_serialize($sub_array);
327 }
328 $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE]]);
329 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
330 if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num])) {
331 if (
332 $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num] &&
333 $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX]][$recent_num] &&
334 $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT]][$recent_num] &&
335 $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP]][$recent_num] &&
336 ((!$search_advanced) ||
337 ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP]][$recent_num] &&
338 $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE]][$recent_num] &&
339 $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB]][$recent_num]))
340 )
341 return $recent_num;
342 }
343 }
344 return -1;
6b37252b 345}
346
7d60bda5 347/** Push a recent search into the prefs
8cc8ec79 348*/
7d60bda5 349function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 350{
8cc8ec79 351 global $recent_prefkeys, $search_memory;
352
353 $criteria = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
354 sqsession_register($criteria, ASEARCH_CRITERIA);
355 if ($search_memory > 0) {
356 $recent_array = asearch_read_recent();
357 $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
358 if ($recent_found >= 0) { // Remove identical recent
359 foreach ($recent_prefkeys as $key) {
360 array_splice($recent_array[$key], $recent_found, 1);
361 }
362 }
363 $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array);
364 $i = 0;
365 foreach ($recent_prefkeys as $key) {
366 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
367 $i++;
368 }
369 asearch_write_recent($recent_array);
370 }
6b37252b 371}
372
7d60bda5 373/** Edit a recent search
8cc8ec79 374* @global array mailbox_array searched mailboxes
375*/
7d60bda5 376function asearch_edit_recent($index)
6b37252b 377{
8cc8ec79 378 global $recent_prefkeys, $search_advanced;
379 global $where_array, $mailbox_array, $what_array, $unop_array;
380 global $biop_array, $exclude_array, $sub_array;
7d60bda5 381
8cc8ec79 382 $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE], $index));
383 $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX], $index));
384 $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT], $index));
385 $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP], $index));
386 if ($search_advanced) {
387 $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP], $index));
388 $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE], $index));
389 $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB], $index));
390 }
6b37252b 391}
392
958becc9 393/** Get last search criteria from session or prefs
8cc8ec79 394*/
7d60bda5 395function asearch_edit_last()
6b37252b 396{
8cc8ec79 397 if (sqGetGlobalVar(ASEARCH_CRITERIA, $criteria, SQ_SESSION)) {
398 global $where_array, $mailbox_array, $what_array, $unop_array;
399 global $biop_array, $exclude_array, $sub_array;
400 $mailbox_array = $criteria[0];
401 $biop_array = $criteria[1];
402 $unop_array = $criteria[2];
403 $where_array = $criteria[3];
404 $what_array = $criteria[4];
405 $exclude_array = $criteria[5];
406 $sub_array = $criteria[6];
407 sqsession_unregister(ASEARCH_CRITERIA);
408 }
409 else {
410 global $search_memory;
411 if ($search_memory > 0)
412 asearch_edit_recent(0);
413 }
6b37252b 414}
415
7d60bda5 416/** Edit a saved search
8cc8ec79 417*/
7d60bda5 418function asearch_edit_saved($index)
6b37252b 419{
8cc8ec79 420 global $saved_prefkeys, $search_advanced;
421 global $where_array, $mailbox_array, $what_array, $unop_array;
422 global $biop_array, $exclude_array, $sub_array;
7d60bda5 423
8cc8ec79 424 $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE], $index));
425 $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX], $index));
426 $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT], $index));
427 $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP], $index));
428 if ($search_advanced) {
429 $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP], $index));
430 $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE], $index));
431 $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB], $index));
432 }
6b37252b 433}
434
7d60bda5 435/** Write a saved search to the prefs
8cc8ec79 436*/
7d60bda5 437function asearch_write_saved(&$saved_array)
6b37252b 438{
8cc8ec79 439 global $saved_prefkeys;
6b37252b 440
8cc8ec79 441 $saved_count = count($saved_array[$saved_prefkeys[0]]);
442 for ($saved_num=0; $saved_num < $saved_count; $saved_num++) {
443 foreach ($saved_prefkeys as $key) {
444 asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]);
445 }
446 }
447 foreach ($saved_prefkeys as $key) {
448 asearch_removePref($key, $saved_count);
449 }
6b37252b 450}
451
7d60bda5 452/** Delete a saved search from the prefs
8cc8ec79 453*/
7d60bda5 454function asearch_delete_saved($saved_index)
6b37252b 455{
8cc8ec79 456 global $saved_prefkeys;
6b37252b 457
8cc8ec79 458 $saved_array = asearch_read_saved();
459 $asearch_keys = $saved_prefkeys;
460 foreach ($asearch_keys as $key) {
461 array_splice($saved_array[$key], $saved_index, 1);
462 }
463 asearch_write_saved($saved_array);
6b37252b 464}
465
7d60bda5 466/** Translate the input date to imap date to local date display,
8cc8ec79 467* so the user can know if the date is wrong or illegal
468* @return string locally formatted date or error text
469*/
7d60bda5 470function asearch_get_date_display(&$what)
6b37252b 471{
8cc8ec79 472 $what_parts = sqimap_asearch_parse_date($what);
473 if (count($what_parts) == 4) {
474 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3]))
475 return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
476 //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];
477 return _("(Illegal date)");
478 }
479 return _("(Wrong date)");
6b37252b 480}
481
7d60bda5 482/** Translate the query to rough natural display
8cc8ec79 483* @return string rough natural query ready to display
484*/
7d60bda5 485function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 486{
8cc8ec79 487 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
488 global $imap_asearch_opcodes;
489
490 $last_mailbox = $mailbox_array[0];
491 if (empty($last_mailbox))
492 $last_mailbox = 'INBOX';
493 $query_display = '';
494 for ($crit_num=0; $crit_num < count($where_array); $crit_num++) {
495 if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) {
496 $cur_mailbox = $mailbox_array[$crit_num];
497 if (empty($cur_mailbox))
498 $cur_mailbox = 'INBOX';
499 $biop = asearch_nz($biop_array[$crit_num]);
500 if (($query_display == '') || ($cur_mailbox != $last_mailbox)) {
501 $mailbox_display = ' <b>' . asearch_get_mailbox_display($cur_mailbox) . '</b>';
502 if ($query_display == '')
503 $biop_display = _("In");
504 else
505 $biop_display = $imap_asearch_biops_in[$biop];
506 $last_mailbox = $cur_mailbox;
507 }
508 else {
509 $mailbox_display = '';
510 $biop_display = $imap_asearch_biops[$biop];
511 }
512 $unop = $unop_array[$crit_num];
513 $unop_display = $imap_asearch_unops[$unop];
514 if ($unop_display != '')
515 $unop_display .= ' ';
516 $where = $where_array[$crit_num];
517 $where_display = $unop_display . asearch_nz($imap_asearch_options[$where], $where);
518 $what_type = $imap_asearch_opcodes[$where];
519 $what = $what_array[$crit_num];
520 if ($what_type) { /* Check opcode parameter */
521 if ($what == '')
522 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
523 else {
524 if ($what_type == 'adate')
525 $what_display = asearch_get_date_display($what);
526 else
527 $what_display = htmlspecialchars($what);
528 $what_display = ' <b>' . $what_display . '</b>';
529 }
530 }
531 else {
532 if ($what)
533 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
534 else
535 $what_display = '';
536 }
537 if ($mailbox_display != '')
538 $query_display .= ' <u><i>' . $biop_display . '</i></u>' . $mailbox_display . ' <u><i>' . $where_display . '</i></u>' . $what_display;
539 else
540 $query_display .= ' <u><i>' . $biop_display . ' ' . $where_display . '</i></u>' . $what_display;
541 }
542 }
543 return $query_display;
6b37252b 544}
545
7d60bda5 546/** Handle the alternate row colors
8cc8ec79 547* @return string color value
548*/
7d60bda5 549function asearch_get_row_color(&$color, $row_num)
6b37252b 550{
551/*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
8cc8ec79 552 $color_string = $color[4];
553 if ($GLOBALS['alt_index_colors']) {
554 if (($row_num % 2) == 0) {
555 if (!isset($color[12]))
556 $color[12] = '#EAEAEA';
557 $color_string = $color[12];
558 }
559 }
560 return $color_string;
6b37252b 561}
562
7d60bda5 563/** Print a whole query array, recent or saved
8cc8ec79 564*/
7d60bda5 565function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref)
6b37252b 566{
8cc8ec79 567 global $color;
568 global $data_dir, $username;
569 global $use_icons, $icon_theme;
570
571 $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1;
572 $use_icons_flag = ($use_icons) && ($icon_theme != 'none');
573 if ($use_icons_flag)
574 $text_array = array('<img src="' . SM_PATH . 'images/minus.png" border="0" height="7" width="7" />',
575 '<img src="' . SM_PATH . 'images/plus.png" border="0" height="7" width="7" />');
576 else
577 $text_array = array('-', '+');
578 $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, $text_array, array(_("Fold"), _("Unfold")));
579 if (!$use_icons_flag)
580 $toggle_link = '<small>[' . $toggle_link . ']</small>';
581
582 echo "<br />\n";
583 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
584 echo html_tag('tr',
585 html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"')
586 . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4'));
587 if ($show_flag) {
588 $main_key = $query_keys[ASEARCH_WHERE];
589 $query_count = count($query_array[$main_key]);
590 for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++) {
591 if (!empty($query_array[$main_key][$query_num])) {
592 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
593
594 unset($search_array);
595 foreach ($query_keys as $query_key) {
596 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
597 }
598 $where_array = $search_array[ASEARCH_WHERE];
599 $mailbox_array = $search_array[ASEARCH_MAILBOX];
600 $what_array = $search_array[ASEARCH_WHAT];
601 $unop_array = $search_array[ASEARCH_UNOP];
602 $biop_array = asearch_nz($search_array[ASEARCH_BIOP], array());
603 $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE], array());
604 $sub_array = asearch_nz($search_array[ASEARCH_SUB], array());
605 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
606
607 echo html_tag('td', $query_num + 1, 'right');
608 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
609 foreach ($action_array as $action => $action_display) {
610 echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&amp;rownum=' . $query_num) . '">' . $action_display . '</a>', 'center');
611 }
612
613 echo '</tr>' . "\n";
614 $row_num++;
615 }
616 }
617 }
618 echo '</table>' . "\n";
6b37252b 619}
620
7d60bda5 621/** Print the saved array
8cc8ec79 622*/
7d60bda5 623function asearch_print_saved(&$boxes)
6b37252b 624{
8cc8ec79 625 global $saved_prefkeys;
6b37252b 626
8cc8ec79 627 $saved_array = asearch_read_saved();
628 if (isset($saved_array[$saved_prefkeys[0]])) {
629 $saved_count = count($saved_array[$saved_prefkeys[0]]);
630 if ($saved_count > 0) {
631 $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
632 asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved');
633 }
634 }
70c4fd84 635}
6c8388a9 636
7d60bda5 637/**
8cc8ec79 638* Print the recent array
639*/
7d60bda5 640function asearch_print_recent(&$boxes)
6b37252b 641{
8cc8ec79 642 global $recent_prefkeys, $search_memory;
6b37252b 643
8cc8ec79 644 $recent_array = asearch_read_recent();
645 if (isset($recent_array[$recent_prefkeys[0]])) {
646 $recent_count = count($recent_array[$recent_prefkeys[0]]);
647 if (min($recent_count, $search_memory) > 0) {
648 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
649 asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent');
650 }
651 }
56e0b3b7 652}
653
7d60bda5 654/** Build an <option> statement
8cc8ec79 655*/
920ec5ec 656function asearch_opt($val, $sel, $tit)
6b37252b 657{
8cc8ec79 658 return '<option value="' . $val . '"' . ($sel == $val ? ' selected' : '') . '>' . $tit . '</option>' . "\n";
6b37252b 659}
660
7d60bda5 661/** Build a <select> statement from an array
8cc8ec79 662*/
6b37252b 663function asearch_opt_array($var_name, $opt_array, $cur_val)
664{
8cc8ec79 665 $output = '<select name="' . $var_name . '">' . "\n";
666 foreach($opt_array as $val => $display)
667 $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val));
668 $output .= '</select>' . "\n";
669 return $output;
6b37252b 670}
671
7d60bda5 672/** Verify that a mailbox exists
8cc8ec79 673* @return bool mailbox exists
674*/
7d60bda5 675function asearch_mailbox_exists($mailbox, &$boxes)
920ec5ec 676{
8cc8ec79 677 foreach ($boxes as $box) {
678 if ($box['unformatted'] == $mailbox)
679 return TRUE;
680 }
681 return FALSE;
920ec5ec 682}
683
5b6abe97 684/** Build the mailbox select
8cc8ec79 685*/
7d60bda5 686function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
6b37252b 687{
8cc8ec79 688 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes)))
689 $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
690 else
691 $missing = '';
692 return '<select name="mailbox[' . $row_num . ']">'
693 . $missing
694 . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']')
695 . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL)
696 . '</select>';
7d60bda5 697}
698
5b6abe97 699/** Build the Include subfolders checkbox
8cc8ec79 700*/
7d60bda5 701function asearch_get_form_sub($sub, $row_num = 0)
702{
8cc8ec79 703 return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub)
704 : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />';
7d60bda5 705}
706
5b6abe97 707/** Build the 2 unop and where selects
8cc8ec79 708*/
7d60bda5 709function asearch_get_form_location($unop, $where, $row_num = 0)
710{
8cc8ec79 711 global $imap_asearch_unops, $imap_asearch_options;
7d60bda5 712
8cc8ec79 713 return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
714 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
7d60bda5 715}
716
5b6abe97 717/** Build the what text input
8cc8ec79 718*/
7d60bda5 719function asearch_get_form_what($what, $row_num = 0)
720{
8cc8ec79 721 return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35')
722 : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />';
7d60bda5 723}
724
5b6abe97 725/** Build the Exclude criteria checkbox
8cc8ec79 726*/
7d60bda5 727function asearch_get_form_exclude($exclude, $row_num = 0)
728{
8cc8ec79 729 return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude)
730 : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />';
7d60bda5 731}
732
733/** Print one advanced form row
8cc8ec79 734*/
7d60bda5 735function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
736{
8cc8ec79 737 global $imap_asearch_biops_in;
738 global $color;
6b37252b 739
8cc8ec79 740 echo html_tag('tr', '', '', $color[4]);
6b37252b 741
7d60bda5 742//Binary operator
8cc8ec79 743 echo html_tag('td', $row_num ?
744 asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
745 : '<b>' . _("In") . '</b>', 'center') . "\n";
7d60bda5 746
747//Mailbox list and Include Subfolders
8cc8ec79 748 echo html_tag('td',
749 asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
750 . _("and&nbsp;subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
6b37252b 751
7d60bda5 752//Unary operator and Search location
8cc8ec79 753 echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
6b37252b 754
7d60bda5 755//Text input
8cc8ec79 756 echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
7d60bda5 757
758//Exclude criteria
8cc8ec79 759 echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
6b37252b 760
8cc8ec79 761 echo "</tr>\n";
6b37252b 762}
763
7d60bda5 764/** Print the advanced search form
8cc8ec79 765*/
7d60bda5 766function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
6b37252b 767{
8cc8ec79 768 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
769 global $color;
6b37252b 770
7d60bda5 771//Search Form
8cc8ec79 772 echo "<br />\n";
773 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
774
775 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
776 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
777 $row_count = count($where_array);
778 for ($row_num = 0; $row_num < $row_count; $row_num++) {
779 $mailbox = asearch_nz($mailbox_array[$row_num]);
780 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
781 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
782 $where = strip_tags(asearch_nz($where_array[$row_num]));
783 $what = asearch_nz($what_array[$row_num]);
784 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
785 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
786 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
787 }
788 echo '</table>' . "\n";
6b37252b 789
7d60bda5 790//Submit buttons
8cc8ec79 791 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
792 echo html_tag('tr',
793 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
794 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
795 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
796 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
797 );
798 echo '</table>' . "\n";
799 echo '</form>' . "\n";
6b37252b 800}
801
7d60bda5 802/** Print one basic form row
8cc8ec79 803*/
7d60bda5 804function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
805{
8cc8ec79 806 global $search_button_html;
807 global $color;
7d60bda5 808
8cc8ec79 809 echo html_tag('tr', '', '', $color[4]);
7d60bda5 810
811//Mailbox list
8cc8ec79 812 echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
7d60bda5 813
814//Unary operator and Search location
8cc8ec79 815 echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
7d60bda5 816
817//Text input
8cc8ec79 818 echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
7d60bda5 819
820//Submit button
8cc8ec79 821 echo html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n";
7d60bda5 822
8cc8ec79 823 echo "</tr>\n";
7d60bda5 824}
825
826/** Print the basic search form
8cc8ec79 827*/
7d60bda5 828function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
829{
8cc8ec79 830 global $color;
7d60bda5 831
832//Search Form
8cc8ec79 833 echo "<br />\n";
834 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
835
836 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
837 //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4'));
838 $row_count = count($where_array);
839 for ($row_num = 0; $row_num < $row_count; $row_num++) {
840 $mailbox = asearch_nz($mailbox_array[$row_num]);
841 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
842 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
843 $where = strip_tags(asearch_nz($where_array[$row_num]));
844 $what = asearch_nz($what_array[$row_num]);
845 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
846 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
847 asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
848 }
849 echo '</table>' . "\n";
850 echo '</form>' . "\n";
7d60bda5 851}
852
853/** Print the $msgs messages from $mailbox mailbox
8cc8ec79 854*/
139b3991 855function asearch_print_mailbox_msgs($imapConnection, $mbxresponse, $mailbox, $id, $cnt, $sort, $color, $where, $what)
6b37252b 856{
8cc8ec79 857 /* initialize */
858 $aMailbox['NAME'] = $mailbox;
859 $aMailbox['EXISTS'] = $cnt;
860 $aMailbox['SORT'] = $sort;
861 $aMailbox['UIDVALIDITY'] = $mbxresponse['UIDVALIDITY'];
862 $aMailbox['UIDNEXT'] = $mbxresponse['UIDNEXT'];
863 $aMailbox['PERMANENTFLAGS'] = $mbxresponse['PERMANENTFLAGS'];
864 $aMailbox['RIGHTS'] = $mbxresponse['RIGHTS'];
865 $aMailbox['NAME'] = $mailbox;
866 $aMailbox['UIDSET'] = $id;
867 $aMailbox['OFFSET'] = 0;
868 $aMailbox['PAGEOFFSET'] = 0;
869 $aMailbox['LIMIT'] = $cnt;
870 $aMailbox['SEARCH'] = array($where,$what);
871 $aMailbox['SORT_METHOD'] = 'SEARCH';
872
873 if ($cnt > 0) {
874 global $allow_server_sort, $allow_thread_sort, $thread_sort_messages, $auto_expunge;
875 $aMailbox['ALLOW_THREAD'] = $allow_thread_sort;
876 $aMailbox['AUTO_EXPUNGE'] = $auto_expunge;
139b3991 877 $msgs = sqimap_get_small_header_list ($imapConnection, $id, count($id));
8cc8ec79 878 $thread_sort_messages = 0;
879 if ($allow_thread_sort) {
880 global $data_dir, $username;
881 $thread_sort_messages = getPref($data_dir, $username, 'thread_' . $mailbox);
882 }
883
884 $mailbox_display = asearch_get_mailbox_display($mailbox);
885 $mailbox_title = '<b><big>' . _("Folder:") . ' '. $mailbox_display . '&nbsp;</big></b>';
886 $devel = check_sm_version(1, 5, 0);
887 if (!$devel) {
888 echo html_tag('div', $mailbox_title, 'center') . "\n";
889 $mailbox_title = get_selectall_link(1, $real_sort);
890 }
891 $msg_cnt_str = get_msgcnt_str(1, $cnt, $cnt);
892
893 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
894
895 echo '<tr><td>';
896 //if ($devel)
897 mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $mailbox_title);
898 //else
899 /// mail_message_listing_beginning($imapConnection, $aMailbox $msg_cnt_str, $mailbox_title);
900 echo '</td></tr>';
901
902 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
903
904 echo '<tr><td>';
905 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
906 echo ' <tr><td>';
907
908 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
909 echo ' <tr><td>';
910 printHeader($aMailbox);
911 $aMailbox['NAME'] = $mailbox;
912 $aMailbox['MSG_HEADERS'] = $msgs;
913 $aMailbox['UIDSET'] = $id;
914 $aMailbox['OFFSET'] = 0;
915 $aMailbox['PAGEOFFSET'] = 0;
916 $aMailbox['LIMIT'] = $cnt;
917 $aMailbox['SEARCH'] = array($where,$what);
918 displayMessageArray($imapConnection, $aMailbox);//$cnt, 1, $id, $msgs, $mailbox, $sort, $cnt, $where, $what);
919 echo ' </td></tr>';
920 echo ' </table>';
921 echo ' </td></tr>';
922 echo ' </table>';
923 mail_message_listing_end($cnt, '', $msg_cnt_str);
924 echo '</td></tr>';
925
926 echo '</table>';
927 }
139b3991 928}
59a623e6 929
40fbe929 930/**
8cc8ec79 931* @param array $boxes mailboxes array (reference)
932* @return array selectable unformatted mailboxes names
933*/
40fbe929 934function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
935{
8cc8ec79 936 $mboxes_array = array();
937 $boxcount = count($boxes);
938 for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) {
939 if (!in_array('noselect', $boxes[$boxnum]['flags']))
940 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
941 }
942 return $mboxes_array;
40fbe929 943}
944
0d672ac0 945/* ------------------------ main ------------------------ */
7d60bda5 946/* get globals we will need */
6b37252b 947sqgetGlobalVar('username', $username, SQ_SESSION);
948sqgetGlobalVar('key', $key, SQ_COOKIE);
7d60bda5 949sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
950sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
6b37252b 951
ad2212db 952if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) {
139b3991 953 $checkall = (int) $temp;
ad2212db 954}
955
139b3991 956
6b37252b 957$search_button_html = _("Search");
958$search_button_text = asearch_unhtmlentities($search_button_html);
959$add_criteria_button_html = _("Add New Criteria");
960$add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
34a85c56 961$del_excluded_button_html = _("Remove Excluded Criteria");
6b37252b 962$del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
34a85c56 963$del_all_button_html = _("Remove All Criteria");
6b37252b 964$del_all_button_text = asearch_unhtmlentities($del_all_button_html);
965
7d60bda5 966/** Maximum number of recent searches to handle
8cc8ec79 967* Default 0
968* @global integer $search_memory
969*/
7d60bda5 970$search_memory = getPref($data_dir, $username, 'search_memory', 0);
971
972/** Advanced search control
8cc8ec79 973* - 0 => allow basic interface only
974* - 1 => allow advanced interface only
975* - 2 => allow both
976* Default 2
977* @global integer $allow_advanced_search
978*/
5b6abe97 979$allow_advanced_search = asearch_nz($allow_advanced_search, 2);
6b37252b 980
7d60bda5 981/**
8cc8ec79 982* Toggle advanced/basic search
983*/
7d60bda5 984if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET))
8cc8ec79 985 setPref($data_dir, $username, 'search_advanced', $search_advanced & 1);
6b37252b 986
7d60bda5 987/** If 1, show advanced search interface
8cc8ec79 988* Default from allow_advanced_search pref
989* @global integer $search_advanced
990*/
7d60bda5 991if ($allow_advanced_search > 1)
8cc8ec79 992 $search_advanced = getPref($data_dir, $username, 'search_advanced', 0);
7d60bda5 993else
8cc8ec79 994 $search_advanced = $allow_advanced_search;
6b37252b 995
7d60bda5 996if ($search_advanced) {
997/** Set recent prefkeys according to $search_advanced
8cc8ec79 998* @global array $recent_prefkeys
999*/
1000 $recent_prefkeys = array('asearch_recent_where', 'asearch_recent_mailbox', 'asearch_recent_what', 'asearch_recent_unop', 'asearch_recent_biop', 'asearch_recent_exclude', 'asearch_recent_sub');
7d60bda5 1001
1002/** Set saved prefkeys according to $search_advanced
8cc8ec79 1003* @global array $saved_prefkeys
1004*/
1005 $saved_prefkeys = array('asearch_saved_where', 'asearch_saved_mailbox', 'asearch_saved_what', 'asearch_saved_unop', 'asearch_saved_biop', 'asearch_saved_exclude', 'asearch_saved_sub');
7d60bda5 1006
1007/*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
1008}
1009else {
8cc8ec79 1010 $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop');
1011 $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop');
7d60bda5 1012}
6b37252b 1013
2d2f8bb8 1014/** How we did enter the form
8cc8ec79 1015* - unset : Enter key, or called from outside (eg read_body)
1016* - $search_button_text : Search button
1017* - 'Search_no_update' : Search but don't update recent
1018* - 'Search_last' : Same as no_update but reload and search last
1019* - 'Search_silent' : Same as no_update but only display results
1020* - $add_criteria_button_text : Add New Criteria button
1021* - $del_excluded_button_text : Remove Excluded Criteria button
1022* - $del_all_button_text : Remove All Criteria button
1023* - 'save_recent'
1024* - 'search_recent'
1025* - 'forget_recent'
1026* - 'edit_saved'
1027* - 'search_saved'
1028* - 'delete_saved'
1029* @global string $submit
1030*/
6b37252b 1031if (isset($_GET['submit']))
8cc8ec79 1032 $submit = strip_tags($_GET['submit']);
6b37252b 1033
2d2f8bb8 1034/** Searched mailboxes
8cc8ec79 1035* @global array $mailbox_array
1036*/
6b37252b 1037if (isset($_GET['mailbox'])) {
8cc8ec79 1038 $mailbox_array = $_GET['mailbox'];
1039 if (!is_array($mailbox_array))
1040 $mailbox_array = array($mailbox_array);
56e0b3b7 1041}
6b37252b 1042else
8cc8ec79 1043 $mailbox_array = array();
6b37252b 1044
2d2f8bb8 1045/** Binary operators
8cc8ec79 1046* @global array $biop_array
1047*/
6b37252b 1048if (isset($_GET['biop'])) {
8cc8ec79 1049 $biop_array = $_GET['biop'];
1050 if (!is_array($biop_array))
1051 $biop_array = array($biop_array);
c1cb7ba4 1052}
6b37252b 1053else
8cc8ec79 1054 $biop_array = array();
c1cb7ba4 1055
2d2f8bb8 1056/** Unary operators
8cc8ec79 1057* @global array $unop_array
1058*/
6b37252b 1059if (isset($_GET['unop'])) {
8cc8ec79 1060 $unop_array = $_GET['unop'];
1061 if (!is_array($unop_array))
1062 $unop_array = array($unop_array);
548a552a 1063}
6b37252b 1064else
8cc8ec79 1065 $unop_array = array();
6b37252b 1066
2d2f8bb8 1067/** Where to search
8cc8ec79 1068* @global array $where_array
1069*/
6b37252b 1070if (isset($_GET['where'])) {
8cc8ec79 1071 $where_array = $_GET['where'];
1072 if (!is_array($where_array))
1073 $where_array = array($where_array);
6b37252b 1074}
1075else
8cc8ec79 1076 $where_array = array();
6b37252b 1077
2d2f8bb8 1078/** What to search
8cc8ec79 1079* @global array $what_array
1080*/
6b37252b 1081if (isset($_GET['what'])) {
8cc8ec79 1082 $what_array = $_GET['what'];
1083 if (!is_array($what_array))
1084 $what_array = array($what_array);
6b37252b 1085}
1086else
8cc8ec79 1087 $what_array = array();
6b37252b 1088
2d2f8bb8 1089/** Whether to exclude this criteria from search
8cc8ec79 1090* @global array $exclude_array
1091*/
6b37252b 1092if (isset($_GET['exclude']))
8cc8ec79 1093 $exclude_array = $_GET['exclude'];
6b37252b 1094else
8cc8ec79 1095 $exclude_array = array();
6b37252b 1096
2d2f8bb8 1097/** Search within subfolders
8cc8ec79 1098* @global array $sub_array
1099*/
40fbe929 1100if (isset($_GET['sub']))
8cc8ec79 1101 $sub_array = $_GET['sub'];
40fbe929 1102else
8cc8ec79 1103 $sub_array = array();
40fbe929 1104
2d2f8bb8 1105/** Row number used by recent and saved stuff
8cc8ec79 1106*/
c2d47d51 1107if (isset($_GET['rownum']))
6b37252b 1108 $submit_rownum = strip_tags($_GET['rownum']);
c2d47d51 1109
2d2f8bb8 1110/** Change global sort
8cc8ec79 1111*/
c2d47d51 1112if (sqgetGlobalVar('newsort', $newsort, SQ_GET)) {
8cc8ec79 1113 setPref($data_dir, $username, 'sort', $newsort);
1114 $sort = $newsort;
1115 sqsession_register($sort, 'sort');
1116 asearch_edit_last();
c2d47d51 1117}
1118
7d60bda5 1119/** Toggle mailbox threading
8cc8ec79 1120*/
c2d47d51 1121if (sqgetGlobalVar('set_thread', $set_thread, SQ_GET)) {
8cc8ec79 1122 setPref($data_dir, $username, 'thread_' . $mailbox_array[0], $set_thread & 1);
1123 asearch_edit_last();
56e0b3b7 1124}
6b37252b 1125
7d60bda5 1126/** Toggle show/hide saved searches
8cc8ec79 1127*/
7d60bda5 1128if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET))
8cc8ec79 1129 setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
7d60bda5 1130
1131/** Toggle show/hide recent searches
8cc8ec79 1132*/
7d60bda5 1133if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET))
8cc8ec79 1134 setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
7d60bda5 1135
1136// end of get globals
6b37252b 1137
40fbe929 1138/** If TRUE, do not show search interface
8cc8ec79 1139* Default FALSE
1140* @global bool $search_silent
1141*/
7d60bda5 1142$search_silent = FALSE;
6b37252b 1143
1144/* See how the page was called and fire off correct function */
7d60bda5 1145if ((empty($submit)) && (!empty($where_array))) {
8cc8ec79 1146 /* This happens when the Enter key is used or called from outside */
1147 $submit = $search_button_text;
1148 if (count($where_array) != count($unop_array)) /* Hack needed to handle coming back from read_body et als */
1149 asearch_edit_last();
56e0b3b7 1150}
6b37252b 1151
1152if (!isset($submit)) {
8cc8ec79 1153 $submit = '';
56e0b3b7 1154}
6b37252b 1155else {
8cc8ec79 1156 switch ($submit) {
1157 case $search_button_text:
1158 if (asearch_check_query($where_array, $what_array, $exclude_array) == '')
1159 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1160 break;
1161 case 'Search_silent':
1162 $search_silent = TRUE;
1163 /*nobreak;*/
1164 case 'Search_no_update':
1165 $submit = $search_button_text;
1166 break;
1167 case $del_excluded_button_text:
1168 $delarray = array_keys($exclude_array);
1169 while (!empty($delarray)) {
1170 $delrow = array_pop($delarray);
1171 array_splice($mailbox_array, $delrow, 1);
1172 array_splice($biop_array, $delrow, 1);
1173 array_splice($unop_array, $delrow, 1);
1174 array_splice($where_array, $delrow, 1);
1175 array_splice($what_array, $delrow, 1);
6b37252b 1176/* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
8cc8ec79 1177 array_splice($sub_array, $delrow, 1);
1178 }
1179 $exclude_array = array();
1180 break;
1181 case $del_all_button_text:
1182 $mailbox_array = array();
1183 $biop_array = array();
1184 $unop_array = array();
1185 $where_array = array();
1186 $what_array = array();
1187 $exclude_array = array();
1188 $sub_array = array();
1189 break;
1190 case 'save_recent':
1191 asearch_save_recent($submit_rownum);
1192 break;
1193 case 'search_recent':
1194 $submit = $search_button_text;
1195 asearch_edit_recent($submit_rownum);
1196 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1197 break;
1198 case 'edit_recent': /* no link to do this, yet */
1199 asearch_edit_recent($submit_rownum);
1200 break;
1201 case 'forget_recent':
1202 asearch_forget_recent($submit_rownum);
1203 break;
1204 case 'search_saved':
1205 $submit = $search_button_text;
1206 asearch_edit_saved($submit_rownum);
1207 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1208 break;
1209 case 'edit_saved':
1210 asearch_edit_saved($submit_rownum);
1211 break;
1212 case 'delete_saved':
1213 asearch_delete_saved($submit_rownum);
1214 break;
1215 }
56e0b3b7 1216}
23a9084b 1217
7d60bda5 1218//Texts in both basic and advanced form
1219$imap_asearch_unops = array(
8cc8ec79 1220 '' => '',
1221 'NOT' => _("Not")
7d60bda5 1222);
1223
1224if ($search_advanced) {
8cc8ec79 1225 //Texts in advanced form only
1226 $imap_asearch_options = array(
1227 //<message set>,
1228 //'ALL' is binary operator
1229 'ANSWERED' => _("Answered"),
1230 'BCC' => _("Bcc"),
1231 'BEFORE' => _("Before"),
1232 'BODY' => _("Message Body"),
1233 'CC' => _("CC"),
1234 'DELETED' => _("Deleted"),
1235 'DRAFT' => _("Draft"),
1236 'FLAGGED' => _("Flagged"),
1237 'FROM' => _("Sent By"),
1238 'HEADER' => _("Header Field"),
1239 'KEYWORD' => _("Keyword"),
1240 'LARGER' => _("Larger Than"),
1241 'NEW' => _("New"),
1242 //'NOT' is unary operator
1243 'OLD' => _("Old"),
1244 'ON' => _("On"),
1245 //'OR' is binary operator
1246 'RECENT' => _("Recent"),
1247 'SEEN' => _("Seen"),
1248 'SENTBEFORE' => _("Sent Before"),
1249 'SENTON' => _("Sent On"),
1250 'SENTSINCE' => _("Sent Since"),
1251 'SINCE' => _("Since"),
1252 'SMALLER' => _("Smaller Than"),
1253 'SUBJECT' => _("Subject Contains"),
1254 'TEXT' => _("Header and Body"),
1255 'TO' => _("Sent To"),
1256 //'UID' => 'anum',
7d60bda5 1257/* 'UNANSWERED' => '',
8cc8ec79 1258 'UNDELETED' => '',
1259 'UNDRAFT' => '',
1260 'UNFLAGGED' => '',
1261 'UNKEYWORD' => _("Unkeyword"),
1262 'UNSEEN' => _("Unseen"),*/
1263 );
1264
1265 $imap_asearch_biops_in = array(
1266 'ALL' => _("And In"),
1267 'OR' => _("Or In")
1268 );
1269
1270 $imap_asearch_biops = array(
1271 'ALL' => _("And"),
1272 'OR' => _("Or")
1273 );
7d60bda5 1274}
1275else {
8cc8ec79 1276 //Texts in basic form only
1277 $imap_asearch_options = array(
1278 'BCC' => _("Bcc"),
1279 'BODY' => _("Body"),
1280 'CC' => _("Cc"),
1281 'FROM' => _("From"),
1282 'SUBJECT' => _("Subject"),
1283 'TEXT' => _("Everywhere"),
1284 'TO' => _("To"),
1285 );
7d60bda5 1286}
1287
1288uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
1289
6b37252b 1290/* open IMAP connection */
1291$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
5b6abe97 1292/* get mailboxes once here */
6b37252b 1293$boxes = sqimap_mailbox_list($imapConnection);
99f3175e 1294/* ensure we have a valid default mailbox name */
edda50ae 1295$mailbox = asearch_nz($mailbox_array[0]);
1296if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
8cc8ec79 1297 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
99f3175e 1298
6b37252b 1299if (isset($composenew) && $composenew) {
8cc8ec79 1300 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox)
1301 . "&amp;session=$composesession&amp;attachedmessages=true&amp";
1302 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
56e0b3b7 1303}
6b37252b 1304else
8cc8ec79 1305 displayPageHeader($color, $mailbox);
184ef883 1306
6b37252b 1307do_hook('search_before_form');
d81e351b 1308
6b37252b 1309if (!$search_silent) {
8cc8ec79 1310 //Add a link to the other search mode if allowed
1311 if ($allow_advanced_search > 1)
1312 $toggle_link = ' - <small>['
1313 . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
1314 . ']</small>';
1315 else
1316 $toggle_link = '';
1317
1318 echo html_tag('table',
1319 html_tag('tr', "\n"
1320 . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
1321 ) ,
1322 '', '', 'width="100%"') . "\n";
1323 asearch_print_saved($boxes);
1324 asearch_print_recent($boxes);
1325 if (empty($where_array)) {
1326 global $sent_folder;
1327
1328 $mailbox_array[0] = $mailbox;
1329 $biop_array[0] = '';
1330 $unop_array[0] = '';
1331 if ($mailbox == $sent_folder)
1332 $where_array[0] = 'TO';
1333 else
1334 $where_array[0] = 'FROM';
1335 $what_array[0] = '';
1336 $exclude_array[0] = '';
1337 $sub_array[0] = '';
1338 }
1339 //Display advanced or basic form
1340 if ($search_advanced) {
1341 if ($submit == $add_criteria_button_text) {
1342 $last_index = max(count($where_array) - 1, 0);
1343 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1344 $biop_array[] = asearch_nz($biop_array[$last_index]);
1345 $unop_array[] = asearch_nz($unop_array[$last_index]);
1346 $where_array[] = asearch_nz($where_array[$last_index]);
1347 $what_array[] = asearch_nz($what_array[$last_index]);
1348 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1349 $sub_array[] = asearch_nz($sub_array[$last_index]);
1350 }
1351 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1352 }
1353 else
1354 asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
6b37252b 1355}
1356
d81e351b 1357/*********************************************************************
8cc8ec79 1358* Check to see if we can use cache or not. Currently the only time *
1359* when you will not use it is when a link on the left hand frame is *
1360* used. Also check to make sure we actually have the array in the *
1361* registered session data. :) *
1362*********************************************************************/
edda50ae 1363if (!isset($use_mailbox_cache))
d81e351b 1364 $use_mailbox_cache = 0;
d81e351b 1365
88cb1b4d 1366do_hook('search_after_form');
56e0b3b7 1367
6b37252b 1368if ($submit == $search_button_text) {
8cc8ec79 1369 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1370 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1371 echo html_tag('tr', html_tag('td', asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array), 'center', $color[4]));
1372 echo '</table>' . "\n";
1373
1374 flush();
1375
1376 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
1377 if ($query_error != '')
1378 echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
1379 else {
1380 // Disable thread sort for now if there is more than one mailbox or at least one 'All Folders'
1381 global $allow_thread_sort;
1382 $old_allow_thread_sort = $allow_thread_sort;
1383 $allow_thread_sort = ((count(array_unique($mailbox_array)) <= 1) && (!in_array('All Folders', $mailbox_array)));
1384
1385 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
1386 $mboxes_msgs = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1387 if (empty($mboxes_msgs))
1388 echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1389 else {
1390 foreach($mboxes_msgs as $mailbox => $msgs) {
1391 echo '<br />';
1392 $mbxresponse = sqimap_mailbox_select($imapConnection, $mailbox);
1393 //$msgs = sqimap_get_small_header_list ($imapConnection, $msgs, count($msgs));
6b37252b 1394/* For now just keep the first criteria to make the regular search happy if the user tries to come back to search */
7d60bda5 1395/* $where = asearch_serialize($where_array);
8cc8ec79 1396 $what = asearch_serialize($what_array);*/
1397 $where = $where_array[0];
1398 $what = $what_array[0];
1399 asearch_print_mailbox_msgs($imapConnection, $mbxresponse, $mailbox, $msgs, count($msgs), $sort, $color, urlencode($where), urlencode($what));
1400 }
1401 }
1402
1403 $allow_thread_sort = $old_allow_thread_sort; // Restore thread sort
1404 }
56e0b3b7 1405}
1406
70c4fd84 1407do_hook('search_bottom');
6b37252b 1408sqimap_logout($imapConnection);
dcc1cc82 1409echo '</body></html>';
99d2a184 1410
139b3991 1411?>