Added documentation and fixed a minor issue
[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;
324ac3c5 352 //global $what; // Hack to access issued search from read_body.php
353 $what = 1;
354 /**
355 * Update search history and store it in the session so we can retrieve the
356 * issued search when returning from an external page like read_body.php
357 */
358 $criteria[$what] = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
8cc8ec79 359 sqsession_register($criteria, ASEARCH_CRITERIA);
360 if ($search_memory > 0) {
361 $recent_array = asearch_read_recent();
362 $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
363 if ($recent_found >= 0) { // Remove identical recent
364 foreach ($recent_prefkeys as $key) {
365 array_splice($recent_array[$key], $recent_found, 1);
366 }
367 }
368 $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array);
369 $i = 0;
370 foreach ($recent_prefkeys as $key) {
371 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
372 $i++;
373 }
374 asearch_write_recent($recent_array);
375 }
6b37252b 376}
377
7d60bda5 378/** Edit a recent search
8cc8ec79 379* @global array mailbox_array searched mailboxes
380*/
7d60bda5 381function asearch_edit_recent($index)
6b37252b 382{
8cc8ec79 383 global $recent_prefkeys, $search_advanced;
384 global $where_array, $mailbox_array, $what_array, $unop_array;
385 global $biop_array, $exclude_array, $sub_array;
7d60bda5 386
8cc8ec79 387 $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE], $index));
388 $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX], $index));
389 $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT], $index));
390 $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP], $index));
391 if ($search_advanced) {
392 $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP], $index));
393 $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE], $index));
394 $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB], $index));
395 }
6b37252b 396}
397
958becc9 398/** Get last search criteria from session or prefs
324ac3c5 399* FIX ME, try to avoid globals
8cc8ec79 400*/
324ac3c5 401function asearch_edit_last($index) {
8cc8ec79 402 if (sqGetGlobalVar(ASEARCH_CRITERIA, $criteria, SQ_SESSION)) {
403 global $where_array, $mailbox_array, $what_array, $unop_array;
404 global $biop_array, $exclude_array, $sub_array;
324ac3c5 405 $mailbox_array = $criteria[$index][0];
406 $biop_array = $criteria[$index][1];
407 $unop_array = $criteria[$index][2];
408 $where_array = $criteria[$index][3];
409 $what_array = $criteria[$index][4];
410 $exclude_array = $criteria[$index][5];
411 $sub_array = $criteria[$index][6];
412 unset($criteria[$index]);
413 //sqsession_unregister(ASEARCH_CRITERIA);
414 } else {
8cc8ec79 415 global $search_memory;
324ac3c5 416 if ($search_memory > 0) {
8cc8ec79 417 asearch_edit_recent(0);
324ac3c5 418 }
8cc8ec79 419 }
6b37252b 420}
421
7d60bda5 422/** Edit a saved search
8cc8ec79 423*/
7d60bda5 424function asearch_edit_saved($index)
6b37252b 425{
8cc8ec79 426 global $saved_prefkeys, $search_advanced;
427 global $where_array, $mailbox_array, $what_array, $unop_array;
428 global $biop_array, $exclude_array, $sub_array;
7d60bda5 429
8cc8ec79 430 $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE], $index));
431 $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX], $index));
432 $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT], $index));
433 $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP], $index));
434 if ($search_advanced) {
435 $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP], $index));
436 $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE], $index));
437 $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB], $index));
438 }
6b37252b 439}
440
7d60bda5 441/** Write a saved search to the prefs
8cc8ec79 442*/
7d60bda5 443function asearch_write_saved(&$saved_array)
6b37252b 444{
8cc8ec79 445 global $saved_prefkeys;
6b37252b 446
8cc8ec79 447 $saved_count = count($saved_array[$saved_prefkeys[0]]);
448 for ($saved_num=0; $saved_num < $saved_count; $saved_num++) {
449 foreach ($saved_prefkeys as $key) {
450 asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]);
451 }
452 }
453 foreach ($saved_prefkeys as $key) {
454 asearch_removePref($key, $saved_count);
455 }
6b37252b 456}
457
7d60bda5 458/** Delete a saved search from the prefs
8cc8ec79 459*/
7d60bda5 460function asearch_delete_saved($saved_index)
6b37252b 461{
8cc8ec79 462 global $saved_prefkeys;
6b37252b 463
8cc8ec79 464 $saved_array = asearch_read_saved();
465 $asearch_keys = $saved_prefkeys;
466 foreach ($asearch_keys as $key) {
467 array_splice($saved_array[$key], $saved_index, 1);
468 }
469 asearch_write_saved($saved_array);
6b37252b 470}
471
7d60bda5 472/** Translate the input date to imap date to local date display,
8cc8ec79 473* so the user can know if the date is wrong or illegal
474* @return string locally formatted date or error text
475*/
7d60bda5 476function asearch_get_date_display(&$what)
6b37252b 477{
8cc8ec79 478 $what_parts = sqimap_asearch_parse_date($what);
479 if (count($what_parts) == 4) {
480 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3]))
481 return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
482 //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];
483 return _("(Illegal date)");
484 }
485 return _("(Wrong date)");
6b37252b 486}
487
7d60bda5 488/** Translate the query to rough natural display
8cc8ec79 489* @return string rough natural query ready to display
490*/
7d60bda5 491function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 492{
8cc8ec79 493 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
494 global $imap_asearch_opcodes;
495
496 $last_mailbox = $mailbox_array[0];
497 if (empty($last_mailbox))
498 $last_mailbox = 'INBOX';
499 $query_display = '';
500 for ($crit_num=0; $crit_num < count($where_array); $crit_num++) {
501 if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) {
502 $cur_mailbox = $mailbox_array[$crit_num];
503 if (empty($cur_mailbox))
504 $cur_mailbox = 'INBOX';
505 $biop = asearch_nz($biop_array[$crit_num]);
506 if (($query_display == '') || ($cur_mailbox != $last_mailbox)) {
507 $mailbox_display = ' <b>' . asearch_get_mailbox_display($cur_mailbox) . '</b>';
508 if ($query_display == '')
509 $biop_display = _("In");
510 else
511 $biop_display = $imap_asearch_biops_in[$biop];
512 $last_mailbox = $cur_mailbox;
513 }
514 else {
515 $mailbox_display = '';
516 $biop_display = $imap_asearch_biops[$biop];
517 }
518 $unop = $unop_array[$crit_num];
519 $unop_display = $imap_asearch_unops[$unop];
520 if ($unop_display != '')
521 $unop_display .= ' ';
522 $where = $where_array[$crit_num];
523 $where_display = $unop_display . asearch_nz($imap_asearch_options[$where], $where);
524 $what_type = $imap_asearch_opcodes[$where];
525 $what = $what_array[$crit_num];
526 if ($what_type) { /* Check opcode parameter */
527 if ($what == '')
528 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
529 else {
530 if ($what_type == 'adate')
531 $what_display = asearch_get_date_display($what);
532 else
533 $what_display = htmlspecialchars($what);
534 $what_display = ' <b>' . $what_display . '</b>';
535 }
536 }
537 else {
538 if ($what)
539 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
540 else
541 $what_display = '';
542 }
543 if ($mailbox_display != '')
544 $query_display .= ' <u><i>' . $biop_display . '</i></u>' . $mailbox_display . ' <u><i>' . $where_display . '</i></u>' . $what_display;
545 else
546 $query_display .= ' <u><i>' . $biop_display . ' ' . $where_display . '</i></u>' . $what_display;
547 }
548 }
549 return $query_display;
6b37252b 550}
551
7d60bda5 552/** Handle the alternate row colors
8cc8ec79 553* @return string color value
554*/
7d60bda5 555function asearch_get_row_color(&$color, $row_num)
6b37252b 556{
557/*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
8cc8ec79 558 $color_string = $color[4];
559 if ($GLOBALS['alt_index_colors']) {
560 if (($row_num % 2) == 0) {
561 if (!isset($color[12]))
562 $color[12] = '#EAEAEA';
563 $color_string = $color[12];
564 }
565 }
566 return $color_string;
6b37252b 567}
568
7d60bda5 569/** Print a whole query array, recent or saved
8cc8ec79 570*/
7d60bda5 571function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref)
6b37252b 572{
8cc8ec79 573 global $color;
574 global $data_dir, $username;
575 global $use_icons, $icon_theme;
576
577 $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1;
578 $use_icons_flag = ($use_icons) && ($icon_theme != 'none');
579 if ($use_icons_flag)
580 $text_array = array('<img src="' . SM_PATH . 'images/minus.png" border="0" height="7" width="7" />',
581 '<img src="' . SM_PATH . 'images/plus.png" border="0" height="7" width="7" />');
582 else
583 $text_array = array('-', '+');
584 $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, $text_array, array(_("Fold"), _("Unfold")));
585 if (!$use_icons_flag)
586 $toggle_link = '<small>[' . $toggle_link . ']</small>';
587
588 echo "<br />\n";
589 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
590 echo html_tag('tr',
591 html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"')
592 . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4'));
593 if ($show_flag) {
594 $main_key = $query_keys[ASEARCH_WHERE];
595 $query_count = count($query_array[$main_key]);
596 for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++) {
597 if (!empty($query_array[$main_key][$query_num])) {
598 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
599
600 unset($search_array);
601 foreach ($query_keys as $query_key) {
602 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
603 }
604 $where_array = $search_array[ASEARCH_WHERE];
605 $mailbox_array = $search_array[ASEARCH_MAILBOX];
606 $what_array = $search_array[ASEARCH_WHAT];
607 $unop_array = $search_array[ASEARCH_UNOP];
608 $biop_array = asearch_nz($search_array[ASEARCH_BIOP], array());
609 $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE], array());
610 $sub_array = asearch_nz($search_array[ASEARCH_SUB], array());
611 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
612
613 echo html_tag('td', $query_num + 1, 'right');
614 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
615 foreach ($action_array as $action => $action_display) {
616 echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&amp;rownum=' . $query_num) . '">' . $action_display . '</a>', 'center');
617 }
618
619 echo '</tr>' . "\n";
620 $row_num++;
621 }
622 }
623 }
624 echo '</table>' . "\n";
6b37252b 625}
626
7d60bda5 627/** Print the saved array
8cc8ec79 628*/
7d60bda5 629function asearch_print_saved(&$boxes)
6b37252b 630{
8cc8ec79 631 global $saved_prefkeys;
6b37252b 632
8cc8ec79 633 $saved_array = asearch_read_saved();
634 if (isset($saved_array[$saved_prefkeys[0]])) {
635 $saved_count = count($saved_array[$saved_prefkeys[0]]);
636 if ($saved_count > 0) {
637 $saved_actions = array('edit_saved' => _("edit"), 'search_saved' => _("search"), 'delete_saved' => _("delete"));
638 asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved');
639 }
640 }
70c4fd84 641}
6c8388a9 642
7d60bda5 643/**
8cc8ec79 644* Print the recent array
645*/
7d60bda5 646function asearch_print_recent(&$boxes)
6b37252b 647{
8cc8ec79 648 global $recent_prefkeys, $search_memory;
6b37252b 649
8cc8ec79 650 $recent_array = asearch_read_recent();
651 if (isset($recent_array[$recent_prefkeys[0]])) {
652 $recent_count = count($recent_array[$recent_prefkeys[0]]);
653 if (min($recent_count, $search_memory) > 0) {
654 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
655 asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent');
656 }
657 }
56e0b3b7 658}
659
7d60bda5 660/** Build an <option> statement
8cc8ec79 661*/
920ec5ec 662function asearch_opt($val, $sel, $tit)
6b37252b 663{
8cc8ec79 664 return '<option value="' . $val . '"' . ($sel == $val ? ' selected' : '') . '>' . $tit . '</option>' . "\n";
6b37252b 665}
666
7d60bda5 667/** Build a <select> statement from an array
8cc8ec79 668*/
6b37252b 669function asearch_opt_array($var_name, $opt_array, $cur_val)
670{
8cc8ec79 671 $output = '<select name="' . $var_name . '">' . "\n";
672 foreach($opt_array as $val => $display)
673 $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val));
674 $output .= '</select>' . "\n";
675 return $output;
6b37252b 676}
677
7d60bda5 678/** Verify that a mailbox exists
8cc8ec79 679* @return bool mailbox exists
680*/
7d60bda5 681function asearch_mailbox_exists($mailbox, &$boxes)
920ec5ec 682{
8cc8ec79 683 foreach ($boxes as $box) {
684 if ($box['unformatted'] == $mailbox)
685 return TRUE;
686 }
687 return FALSE;
920ec5ec 688}
689
5b6abe97 690/** Build the mailbox select
8cc8ec79 691*/
7d60bda5 692function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
6b37252b 693{
324ac3c5 694 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
8cc8ec79 695 $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . asearch_get_mailbox_display($mailbox));
324ac3c5 696 } else {
8cc8ec79 697 $missing = '';
324ac3c5 698 }
8cc8ec79 699 return '<select name="mailbox[' . $row_num . ']">'
700 . $missing
701 . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']')
702 . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL)
703 . '</select>';
7d60bda5 704}
705
5b6abe97 706/** Build the Include subfolders checkbox
8cc8ec79 707*/
7d60bda5 708function asearch_get_form_sub($sub, $row_num = 0)
709{
8cc8ec79 710 return function_exists('addCheckBox') ? addCheckBox('sub[' . $row_num .']', $sub)
711 : '<input type="checkbox" name="sub[' . $row_num .']"' . ($sub ? ' checked="checked"' : '') . ' />';
7d60bda5 712}
713
5b6abe97 714/** Build the 2 unop and where selects
8cc8ec79 715*/
7d60bda5 716function asearch_get_form_location($unop, $where, $row_num = 0)
717{
8cc8ec79 718 global $imap_asearch_unops, $imap_asearch_options;
7d60bda5 719
8cc8ec79 720 return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
721 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
7d60bda5 722}
723
5b6abe97 724/** Build the what text input
8cc8ec79 725*/
7d60bda5 726function asearch_get_form_what($what, $row_num = 0)
727{
8cc8ec79 728 return function_exists('addInput') ? addInput('what[' . $row_num . ']', $what, '35')
729 : '<input type="text" size="35" name="what[' . $row_num . ']" value="' . htmlspecialchars($what) . '" />';
7d60bda5 730}
731
5b6abe97 732/** Build the Exclude criteria checkbox
8cc8ec79 733*/
7d60bda5 734function asearch_get_form_exclude($exclude, $row_num = 0)
735{
8cc8ec79 736 return function_exists('addCheckBox') ? addCheckBox('exclude['.$row_num.']', $exclude)
737 : '<input type="checkbox" name="exclude[' . $row_num .']"' . ($exclude ? ' checked="checked"' : '') . ' />';
7d60bda5 738}
739
740/** Print one advanced form row
8cc8ec79 741*/
7d60bda5 742function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
743{
8cc8ec79 744 global $imap_asearch_biops_in;
745 global $color;
6b37252b 746
8cc8ec79 747 echo html_tag('tr', '', '', $color[4]);
6b37252b 748
7d60bda5 749//Binary operator
8cc8ec79 750 echo html_tag('td', $row_num ?
751 asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
752 : '<b>' . _("In") . '</b>', 'center') . "\n";
7d60bda5 753
754//Mailbox list and Include Subfolders
8cc8ec79 755 echo html_tag('td',
756 asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
757 . _("and&nbsp;subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
6b37252b 758
7d60bda5 759//Unary operator and Search location
8cc8ec79 760 echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
6b37252b 761
7d60bda5 762//Text input
8cc8ec79 763 echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
7d60bda5 764
765//Exclude criteria
8cc8ec79 766 echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
6b37252b 767
8cc8ec79 768 echo "</tr>\n";
6b37252b 769}
770
7d60bda5 771/** Print the advanced search form
8cc8ec79 772*/
7d60bda5 773function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
6b37252b 774{
8cc8ec79 775 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
776 global $color;
6b37252b 777
7d60bda5 778//Search Form
8cc8ec79 779 echo "<br />\n";
780 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
781
782 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
783 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
784 $row_count = count($where_array);
785 for ($row_num = 0; $row_num < $row_count; $row_num++) {
786 $mailbox = asearch_nz($mailbox_array[$row_num]);
787 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
788 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
789 $where = strip_tags(asearch_nz($where_array[$row_num]));
790 $what = asearch_nz($what_array[$row_num]);
791 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
792 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
793 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
794 }
795 echo '</table>' . "\n";
6b37252b 796
7d60bda5 797//Submit buttons
8cc8ec79 798 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
799 echo html_tag('tr',
800 html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n"
801 . html_tag('td', getButton('SUBMIT', 'submit', $add_criteria_button_html), 'center') . "\n"
802 . html_tag('td', getButton('SUBMIT', 'submit', $del_all_button_html), 'center') . "\n"
803 . html_tag('td', getButton('SUBMIT', 'submit', $del_excluded_button_html), 'center') . "\n"
804 );
805 echo '</table>' . "\n";
806 echo '</form>' . "\n";
6b37252b 807}
808
7d60bda5 809/** Print one basic form row
8cc8ec79 810*/
7d60bda5 811function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
812{
8cc8ec79 813 global $search_button_html;
814 global $color;
7d60bda5 815
8cc8ec79 816 echo html_tag('tr', '', '', $color[4]);
7d60bda5 817
818//Mailbox list
8cc8ec79 819 echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
7d60bda5 820
821//Unary operator and Search location
8cc8ec79 822 echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
7d60bda5 823
824//Text input
8cc8ec79 825 echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
7d60bda5 826
827//Submit button
8cc8ec79 828 echo html_tag('td', getButton('SUBMIT', 'submit', $search_button_html), 'center') . "\n";
7d60bda5 829
8cc8ec79 830 echo "</tr>\n";
7d60bda5 831}
832
833/** Print the basic search form
8cc8ec79 834*/
7d60bda5 835function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
836{
8cc8ec79 837 global $color;
7d60bda5 838
839//Search Form
8cc8ec79 840 echo "<br />\n";
841 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
842
843 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
844 //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4'));
845 $row_count = count($where_array);
846 for ($row_num = 0; $row_num < $row_count; $row_num++) {
847 $mailbox = asearch_nz($mailbox_array[$row_num]);
848 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
849 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
850 $where = strip_tags(asearch_nz($where_array[$row_num]));
851 $what = asearch_nz($what_array[$row_num]);
852 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
853 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
854 asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
855 }
856 echo '</table>' . "\n";
857 echo '</form>' . "\n";
7d60bda5 858}
859
860/** Print the $msgs messages from $mailbox mailbox
8cc8ec79 861*/
8cc8ec79 862
324ac3c5 863function asearch_print_mailbox_msgs($imapConnection, &$aMailbox, $color)
864{
865 if (fetchMessageHeaders($imapConnection, $aMailbox)) {
866 /**
867 * A mailbox can contain different sets with uid's. Default, for normal
868 * message list view we use '0' as setindex and for search a different
869 * setindex.
870 */
871 $iSetIndx = $aMailbox['SETINDEX'];
872
873 $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
8cc8ec79 874 $mailbox_title = '<b><big>' . _("Folder:") . ' '. $mailbox_display . '&nbsp;</big></b>';
324ac3c5 875
876 /**
877 * UIDSET contains the array with uid's returned by a search
878 */
879 $cnt = count($aMailbox['UIDSET'][$iSetIndx]);
880
881 $iLimit = ($aMailbox['SHOWALL'][$iSetIndx]) ? $cnt : $aMailbox['LIMIT'];
882 $iEnd = ($aMailbox['PAGEOFFSET'] + ($iLimit - 1) < $aMailbox['EXISTS']) ?
883 $aMailbox['PAGEOFFSET'] + $iLimit - 1 : $cnt;
884
885 $paginator_str = get_paginator_str($aMailbox['NAME'], $aMailbox['PAGEOFFSET'],
886 $cnt, $aMailbox['LIMIT'], $aMailbox['SHOWALL'][$iSetIndx]);
887
888 $msg_cnt_str = get_msgcnt_str($aMailbox['PAGEOFFSET'], $iEnd,$cnt);
8cc8ec79 889
890 echo '<table border="0" width="100%" cellpadding="0" cellspacing="0">';
891
892 echo '<tr><td>';
324ac3c5 893 mail_message_listing_beginning($imapConnection, $aMailbox, $msg_cnt_str, $mailbox_title . " $paginator_str");
8cc8ec79 894 echo '</td></tr>';
895
896 echo '<tr><td HEIGHT="5" BGCOLOR="'.$color[4].'"></td></tr>';
897
898 echo '<tr><td>';
899 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center"'.' border="0" bgcolor="'.$color[9].'">';
900 echo ' <tr><td>';
901
902 echo ' <table width="100%" cellpadding="1" cellspacing="0" align="center" border="0" bgcolor="'.$color[5].'">';
903 echo ' <tr><td>';
904 printHeader($aMailbox);
324ac3c5 905 displayMessageArray($imapConnection, $aMailbox);
8cc8ec79 906 echo ' </td></tr>';
907 echo ' </table>';
908 echo ' </td></tr>';
909 echo ' </table>';
910 mail_message_listing_end($cnt, '', $msg_cnt_str);
911 echo '</td></tr>';
912
913 echo '</table>';
324ac3c5 914 } else {
915 echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
8cc8ec79 916 }
139b3991 917}
59a623e6 918
40fbe929 919/**
8cc8ec79 920* @param array $boxes mailboxes array (reference)
921* @return array selectable unformatted mailboxes names
922*/
40fbe929 923function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
924{
8cc8ec79 925 $mboxes_array = array();
926 $boxcount = count($boxes);
927 for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) {
928 if (!in_array('noselect', $boxes[$boxnum]['flags']))
929 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
930 }
931 return $mboxes_array;
40fbe929 932}
933
0d672ac0 934/* ------------------------ main ------------------------ */
7d60bda5 935/* get globals we will need */
6b37252b 936sqgetGlobalVar('username', $username, SQ_SESSION);
937sqgetGlobalVar('key', $key, SQ_COOKIE);
7d60bda5 938sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
939sqgetGlobalVar('onetimepad', $onetimepad, SQ_SESSION);
6b37252b 940
ad2212db 941if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) {
139b3991 942 $checkall = (int) $temp;
ad2212db 943}
944
324ac3c5 945/**
946 * Retrieve the mailbox cache from the session.
947 */
948sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
949
139b3991 950
6b37252b 951$search_button_html = _("Search");
952$search_button_text = asearch_unhtmlentities($search_button_html);
953$add_criteria_button_html = _("Add New Criteria");
954$add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
34a85c56 955$del_excluded_button_html = _("Remove Excluded Criteria");
6b37252b 956$del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
34a85c56 957$del_all_button_html = _("Remove All Criteria");
6b37252b 958$del_all_button_text = asearch_unhtmlentities($del_all_button_html);
959
7d60bda5 960/** Maximum number of recent searches to handle
8cc8ec79 961* Default 0
962* @global integer $search_memory
963*/
7d60bda5 964$search_memory = getPref($data_dir, $username, 'search_memory', 0);
965
966/** Advanced search control
8cc8ec79 967* - 0 => allow basic interface only
968* - 1 => allow advanced interface only
969* - 2 => allow both
970* Default 2
971* @global integer $allow_advanced_search
972*/
5b6abe97 973$allow_advanced_search = asearch_nz($allow_advanced_search, 2);
6b37252b 974
7d60bda5 975/**
8cc8ec79 976* Toggle advanced/basic search
977*/
324ac3c5 978if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET)) {
8cc8ec79 979 setPref($data_dir, $username, 'search_advanced', $search_advanced & 1);
324ac3c5 980}
7d60bda5 981/** If 1, show advanced search interface
8cc8ec79 982* Default from allow_advanced_search pref
983* @global integer $search_advanced
984*/
324ac3c5 985if ($allow_advanced_search > 1) {
8cc8ec79 986 $search_advanced = getPref($data_dir, $username, 'search_advanced', 0);
324ac3c5 987} else {
8cc8ec79 988 $search_advanced = $allow_advanced_search;
324ac3c5 989}
7d60bda5 990if ($search_advanced) {
991/** Set recent prefkeys according to $search_advanced
8cc8ec79 992* @global array $recent_prefkeys
993*/
994 $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 995
996/** Set saved prefkeys according to $search_advanced
8cc8ec79 997* @global array $saved_prefkeys
998*/
999 $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 1000
1001/*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
324ac3c5 1002} else {
8cc8ec79 1003 $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop');
1004 $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop');
7d60bda5 1005}
6b37252b 1006
2d2f8bb8 1007/** How we did enter the form
8cc8ec79 1008* - unset : Enter key, or called from outside (eg read_body)
1009* - $search_button_text : Search button
1010* - 'Search_no_update' : Search but don't update recent
1011* - 'Search_last' : Same as no_update but reload and search last
1012* - 'Search_silent' : Same as no_update but only display results
1013* - $add_criteria_button_text : Add New Criteria button
1014* - $del_excluded_button_text : Remove Excluded Criteria button
1015* - $del_all_button_text : Remove All Criteria button
1016* - 'save_recent'
1017* - 'search_recent'
1018* - 'forget_recent'
1019* - 'edit_saved'
1020* - 'search_saved'
1021* - 'delete_saved'
1022* @global string $submit
1023*/
324ac3c5 1024if (isset($_GET['submit'])) {
8cc8ec79 1025 $submit = strip_tags($_GET['submit']);
324ac3c5 1026}
2d2f8bb8 1027/** Searched mailboxes
8cc8ec79 1028* @global array $mailbox_array
1029*/
6b37252b 1030if (isset($_GET['mailbox'])) {
8cc8ec79 1031 $mailbox_array = $_GET['mailbox'];
324ac3c5 1032 $targetmailbox = $_GET['mailbox'];
1033 if (!is_array($mailbox_array)) {
8cc8ec79 1034 $mailbox_array = array($mailbox_array);
324ac3c5 1035 }
1036} else {
8cc8ec79 1037 $mailbox_array = array();
324ac3c5 1038}
1039$aMailboxGlobalPref = array(
1040 MBX_PREF_SORT => 0,
1041 MBX_PREF_LIMIT => (int) $show_num,
1042 MBX_PREF_AUTO_EXPUNGE => (bool) $auto_expunge,
1043 MBX_PREF_INTERNALDATE => (bool) getPref($data_dir, $username, 'internal_date_sort')
1044 // MBX_PREF_FUTURE => (var) $future
1045 );
1046
1047/**
1048 * system wide admin settings and incoming vars.
1049 */
1050$aConfig = array(
1051 'allow_thread_sort' => $allow_thread_sort,
1052 'allow_server_sort' => $allow_server_sort,
1053 'user' => $username,
1054 'max_cache_size'
1055 );
6b37252b 1056
2d2f8bb8 1057/** Binary operators
8cc8ec79 1058* @global array $biop_array
1059*/
6b37252b 1060if (isset($_GET['biop'])) {
8cc8ec79 1061 $biop_array = $_GET['biop'];
1062 if (!is_array($biop_array))
1063 $biop_array = array($biop_array);
324ac3c5 1064} else {
8cc8ec79 1065 $biop_array = array();
324ac3c5 1066}
2d2f8bb8 1067/** Unary operators
8cc8ec79 1068* @global array $unop_array
1069*/
6b37252b 1070if (isset($_GET['unop'])) {
8cc8ec79 1071 $unop_array = $_GET['unop'];
1072 if (!is_array($unop_array))
1073 $unop_array = array($unop_array);
324ac3c5 1074} else {
8cc8ec79 1075 $unop_array = array();
324ac3c5 1076}
2d2f8bb8 1077/** Where to search
8cc8ec79 1078* @global array $where_array
1079*/
6b37252b 1080if (isset($_GET['where'])) {
8cc8ec79 1081 $where_array = $_GET['where'];
324ac3c5 1082 if (!is_array($where_array)) {
8cc8ec79 1083 $where_array = array($where_array);
324ac3c5 1084 }
1085} else {
8cc8ec79 1086 $where_array = array();
324ac3c5 1087}
2d2f8bb8 1088/** What to search
8cc8ec79 1089* @global array $what_array
1090*/
6b37252b 1091if (isset($_GET['what'])) {
8cc8ec79 1092 $what_array = $_GET['what'];
324ac3c5 1093 if (!is_array($what_array)) {
8cc8ec79 1094 $what_array = array($what_array);
324ac3c5 1095 }
1096} else {
8cc8ec79 1097 $what_array = array();
324ac3c5 1098}
2d2f8bb8 1099/** Whether to exclude this criteria from search
8cc8ec79 1100* @global array $exclude_array
1101*/
324ac3c5 1102if (isset($_GET['exclude'])) {
8cc8ec79 1103 $exclude_array = $_GET['exclude'];
324ac3c5 1104} else {
8cc8ec79 1105 $exclude_array = array();
324ac3c5 1106}
2d2f8bb8 1107/** Search within subfolders
8cc8ec79 1108* @global array $sub_array
1109*/
324ac3c5 1110if (isset($_GET['sub'])) {
8cc8ec79 1111 $sub_array = $_GET['sub'];
324ac3c5 1112} else {
8cc8ec79 1113 $sub_array = array();
324ac3c5 1114}
2d2f8bb8 1115/** Row number used by recent and saved stuff
8cc8ec79 1116*/
324ac3c5 1117if (isset($_GET['rownum'])) {
6b37252b 1118 $submit_rownum = strip_tags($_GET['rownum']);
324ac3c5 1119}
2d2f8bb8 1120/** Change global sort
8cc8ec79 1121*/
324ac3c5 1122if (sqgetGlobalVar('srt', $temp, SQ_GET)) {
1123 $srt = (int) $temp;
1124 asearch_edit_last(1);
1125 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1126}
1127if (sqgetGlobalVar('startMessage', $temp, SQ_GET)) {
1128 $startMessage = (int) $temp;
1129 asearch_edit_last(1);
1130 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
c2d47d51 1131}
1132
324ac3c5 1133if ( sqgetGlobalVar('showall', $temp, SQ_GET) ) {
1134 $showall = (int) $temp;
1135 asearch_edit_last(1);
1136 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
56e0b3b7 1137}
324ac3c5 1138/**
1139 * Incoming submit buttons from the message list with search results
1140 */
1141if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST) ||
1142 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST) ||
1143 sqgetGlobalVar('delete', $markDelete, SQ_POST) ||
1144 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST) ||
1145 sqgetGlobalVar('markRead', $markRead, SQ_POST) ||
1146 sqgetGlobalVar('markUnread', $markUnread, SQ_POST) ||
1147 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST) ||
1148 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST) ||
1149 sqgetGlobalVar('attache', $attache, SQ_POST)) {
1150 asearch_edit_last(1);
1151 $submit = '';
1152 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1153}
1154
1155
6b37252b 1156
7d60bda5 1157/** Toggle show/hide saved searches
8cc8ec79 1158*/
324ac3c5 1159if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) {
8cc8ec79 1160 setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
324ac3c5 1161}
7d60bda5 1162/** Toggle show/hide recent searches
8cc8ec79 1163*/
324ac3c5 1164if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) {
8cc8ec79 1165 setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
324ac3c5 1166}
7d60bda5 1167// end of get globals
6b37252b 1168
40fbe929 1169/** If TRUE, do not show search interface
8cc8ec79 1170* Default FALSE
1171* @global bool $search_silent
1172*/
7d60bda5 1173$search_silent = FALSE;
6b37252b 1174
1175/* See how the page was called and fire off correct function */
7d60bda5 1176if ((empty($submit)) && (!empty($where_array))) {
8cc8ec79 1177 /* This happens when the Enter key is used or called from outside */
1178 $submit = $search_button_text;
324ac3c5 1179 /* Hack needed to handle coming back from read_body et als */
1180 if (count($where_array) != count($unop_array)) {
1181 /**
1182 * Hack to use already existen where and what vars.
1183 * where now contains the initiator page of the messagelist
1184 * and in this case 'search'. what contains an index to access
1185 * the search history
1186 */
1187
1188 sqgetGlobalVar('what',$what,SQ_GET);
1189 asearch_edit_last($what);
1190 }
56e0b3b7 1191}
6b37252b 1192
1193if (!isset($submit)) {
8cc8ec79 1194 $submit = '';
324ac3c5 1195} else {
8cc8ec79 1196 switch ($submit) {
324ac3c5 1197 case $search_button_text:
1198 if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
1199 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1200 }
8cc8ec79 1201 break;
324ac3c5 1202 case 'Search_silent':
1203 $search_silent = TRUE;
8cc8ec79 1204 /*nobreak;*/
324ac3c5 1205 case 'Search_no_update':
1206 $submit = $search_button_text;
8cc8ec79 1207 break;
324ac3c5 1208 case $del_excluded_button_text:
1209 $delarray = array_keys($exclude_array);
1210 while (!empty($delarray)) {
1211 $delrow = array_pop($delarray);
1212 array_splice($mailbox_array, $delrow, 1);
1213 array_splice($biop_array, $delrow, 1);
1214 array_splice($unop_array, $delrow, 1);
1215 array_splice($where_array, $delrow, 1);
1216 array_splice($what_array, $delrow, 1);
1217 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
1218 array_splice($sub_array, $delrow, 1);
1219 }
1220 $exclude_array = array();
8cc8ec79 1221 break;
324ac3c5 1222 case $del_all_button_text:
1223 $mailbox_array = array();
1224 $biop_array = array();
1225 $unop_array = array();
1226 $where_array = array();
1227 $what_array = array();
1228 $exclude_array = array();
1229 $sub_array = array();
8cc8ec79 1230 break;
324ac3c5 1231 case 'save_recent':
1232 asearch_save_recent($submit_rownum);
8cc8ec79 1233 break;
324ac3c5 1234 case 'search_recent':
1235 $submit = $search_button_text;
1236 asearch_edit_recent($submit_rownum);
1237 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
8cc8ec79 1238 break;
324ac3c5 1239 case 'edit_recent': /* no link to do this, yet */
1240 asearch_edit_recent($submit_rownum);
8cc8ec79 1241 break;
324ac3c5 1242 case 'forget_recent':
1243 asearch_forget_recent($submit_rownum);
8cc8ec79 1244 break;
324ac3c5 1245 case 'search_saved':
1246 $submit = $search_button_text;
1247 asearch_edit_saved($submit_rownum);
1248 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
8cc8ec79 1249 break;
324ac3c5 1250 case 'edit_saved':
1251 asearch_edit_saved($submit_rownum);
8cc8ec79 1252 break;
324ac3c5 1253 case 'delete_saved':
1254 asearch_delete_saved($submit_rownum);
8cc8ec79 1255 break;
1256 }
56e0b3b7 1257}
23a9084b 1258
7d60bda5 1259//Texts in both basic and advanced form
1260$imap_asearch_unops = array(
8cc8ec79 1261 '' => '',
1262 'NOT' => _("Not")
7d60bda5 1263);
1264
1265if ($search_advanced) {
8cc8ec79 1266 //Texts in advanced form only
1267 $imap_asearch_options = array(
1268 //<message set>,
1269 //'ALL' is binary operator
1270 'ANSWERED' => _("Answered"),
1271 'BCC' => _("Bcc"),
1272 'BEFORE' => _("Before"),
1273 'BODY' => _("Message Body"),
1274 'CC' => _("CC"),
1275 'DELETED' => _("Deleted"),
1276 'DRAFT' => _("Draft"),
1277 'FLAGGED' => _("Flagged"),
1278 'FROM' => _("Sent By"),
1279 'HEADER' => _("Header Field"),
1280 'KEYWORD' => _("Keyword"),
1281 'LARGER' => _("Larger Than"),
1282 'NEW' => _("New"),
1283 //'NOT' is unary operator
1284 'OLD' => _("Old"),
1285 'ON' => _("On"),
1286 //'OR' is binary operator
1287 'RECENT' => _("Recent"),
1288 'SEEN' => _("Seen"),
1289 'SENTBEFORE' => _("Sent Before"),
1290 'SENTON' => _("Sent On"),
1291 'SENTSINCE' => _("Sent Since"),
1292 'SINCE' => _("Since"),
1293 'SMALLER' => _("Smaller Than"),
1294 'SUBJECT' => _("Subject Contains"),
1295 'TEXT' => _("Header and Body"),
1296 'TO' => _("Sent To"),
1297 //'UID' => 'anum',
7d60bda5 1298/* 'UNANSWERED' => '',
8cc8ec79 1299 'UNDELETED' => '',
1300 'UNDRAFT' => '',
1301 'UNFLAGGED' => '',
1302 'UNKEYWORD' => _("Unkeyword"),
1303 'UNSEEN' => _("Unseen"),*/
1304 );
1305
1306 $imap_asearch_biops_in = array(
1307 'ALL' => _("And In"),
1308 'OR' => _("Or In")
1309 );
1310
1311 $imap_asearch_biops = array(
1312 'ALL' => _("And"),
1313 'OR' => _("Or")
1314 );
324ac3c5 1315} else {
8cc8ec79 1316 //Texts in basic form only
1317 $imap_asearch_options = array(
1318 'BCC' => _("Bcc"),
1319 'BODY' => _("Body"),
1320 'CC' => _("Cc"),
1321 'FROM' => _("From"),
1322 'SUBJECT' => _("Subject"),
1323 'TEXT' => _("Everywhere"),
1324 'TO' => _("To"),
1325 );
7d60bda5 1326}
1327
1328uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
1329
6b37252b 1330/* open IMAP connection */
1331$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
324ac3c5 1332
1333
5b6abe97 1334/* get mailboxes once here */
6b37252b 1335$boxes = sqimap_mailbox_list($imapConnection);
99f3175e 1336/* ensure we have a valid default mailbox name */
edda50ae 1337$mailbox = asearch_nz($mailbox_array[0]);
1338if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
8cc8ec79 1339 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
99f3175e 1340
6b37252b 1341if (isset($composenew) && $composenew) {
8cc8ec79 1342 $comp_uri = "../src/compose.php?mailbox=" . urlencode($mailbox)
1343 . "&amp;session=$composesession&amp;attachedmessages=true&amp";
1344 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri');", false);
324ac3c5 1345} else {
8cc8ec79 1346 displayPageHeader($color, $mailbox);
324ac3c5 1347}
6b37252b 1348do_hook('search_before_form');
d81e351b 1349
6b37252b 1350if (!$search_silent) {
8cc8ec79 1351 //Add a link to the other search mode if allowed
1352 if ($allow_advanced_search > 1)
1353 $toggle_link = ' - <small>['
1354 . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
1355 . ']</small>';
1356 else
1357 $toggle_link = '';
1358
1359 echo html_tag('table',
1360 html_tag('tr', "\n"
1361 . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
1362 ) ,
1363 '', '', 'width="100%"') . "\n";
1364 asearch_print_saved($boxes);
1365 asearch_print_recent($boxes);
1366 if (empty($where_array)) {
1367 global $sent_folder;
1368
1369 $mailbox_array[0] = $mailbox;
1370 $biop_array[0] = '';
1371 $unop_array[0] = '';
324ac3c5 1372 if ($mailbox == $sent_folder) {
8cc8ec79 1373 $where_array[0] = 'TO';
324ac3c5 1374 } else {
8cc8ec79 1375 $where_array[0] = 'FROM';
324ac3c5 1376 }
8cc8ec79 1377 $what_array[0] = '';
1378 $exclude_array[0] = '';
1379 $sub_array[0] = '';
1380 }
1381 //Display advanced or basic form
1382 if ($search_advanced) {
1383 if ($submit == $add_criteria_button_text) {
1384 $last_index = max(count($where_array) - 1, 0);
1385 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1386 $biop_array[] = asearch_nz($biop_array[$last_index]);
1387 $unop_array[] = asearch_nz($unop_array[$last_index]);
1388 $where_array[] = asearch_nz($where_array[$last_index]);
1389 $what_array[] = asearch_nz($what_array[$last_index]);
1390 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1391 $sub_array[] = asearch_nz($sub_array[$last_index]);
1392 }
1393 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
324ac3c5 1394 } else {
8cc8ec79 1395 asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
324ac3c5 1396 }
6b37252b 1397}
1398
88cb1b4d 1399do_hook('search_after_form');
56e0b3b7 1400
6b37252b 1401if ($submit == $search_button_text) {
8cc8ec79 1402 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1403 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1404 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]));
1405 echo '</table>' . "\n";
1406
1407 flush();
1408
1409 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
324ac3c5 1410 if ($query_error != '') {
8cc8ec79 1411 echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
324ac3c5 1412 } else {
8cc8ec79 1413 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
324ac3c5 1414 /**
1415 * Retrieve the search queries
1416 */
1417 $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1418 foreach($mboxes_mailbox as $mbx => $search) {
1419 $aMailboxPrefSer=getPref($data_dir, $username, "pref_$mbx");
1420
1421 if ($aMailboxPrefSer) {
1422 $aMailboxPref = unserialize($aMailboxPrefSer);
1423 } else {
1424 setUserPref($username,"pref_$mbx",serialize($aMailboxGlobalPref));
1425 $aMailboxPref = $aMailboxGlobalPref;
8cc8ec79 1426 }
324ac3c5 1427 if (isset($srt) && $targetmailbox == $mbx) {
1428 $aMailboxPref[MBX_PREF_SORT] = (int) $srt;
1429 }
1430 if (isset($startMessage) && $targetmailbox == $mbx) {
1431 $aConfig['offset'] = $startMessage;
1432 } else if (isset($aConfig['offset'])) {
1433 unset($aConfig['offset']);
1434 }
1435 if (isset($showall) && $targetmailbox == $mbx) {
1436 $aConfig['showall'] = $showall;
1437 } else if (isset($aConfig['showall'])) {
1438 unset($aConfig['showall']);
1439 }
1440 /**
1441 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1442 * when searching all mailboxes
1443 */
1444 $aConfig['max_cache_size'] = count($mboxes_mailbox) +1;
1445
1446 /**
1447 * until there is no per mailbox option screen to set prefs we override
1448 * the mailboxprefs by the default ones
1449 */
1450 $aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num;
1451 $aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge;
1452 $aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort');
1453
1454 echo '<br />';
1455 $aConfig['search'] = $search['search'];
1456 $aConfig['charset'] = $search['charset'];
1457 $aConfig['setindex'] = 1; // $what $where = 'search'
1458
1459 $aMailbox = sqm_api_mailbox_select($imapConnection,$mbx,$aConfig,$aMailboxPref);
1460 /**
1461 * Handle form actions like flag / unflag, seen / unseen, delete
1462 */
1463 if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST)) {
1464 if ($postMailbox === $mbx) {
1465 handleMessageListForm($imapConnection,$aMailbox);
1466 }
1467 }
1468 asearch_print_mailbox_msgs($imapConnection, $aMailbox, $color);
1469 /* add the mailbox to the cache */
1470 $mailbox_cache[$aMailbox['NAME']] = $aMailbox;
8cc8ec79 1471
324ac3c5 1472 }
8cc8ec79 1473 }
56e0b3b7 1474}
1475
70c4fd84 1476do_hook('search_bottom');
6b37252b 1477sqimap_logout($imapConnection);
dcc1cc82 1478echo '</body></html>';
324ac3c5 1479sqsession_register($mailbox_cache,'mailbox_cache');
139b3991 1480?>