New minus image
[squirrelmail.git] / src / search.php
CommitLineData
c61bb006 1<?php
245a6892 2
35586184 3/**
134e4174 4 * search.php
5 *
134e4174 6 * IMAP search page
7 *
8 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
9 *
4b4abf93 10 * @author Alex Lemaresquier - Brainstorm <alex at brainstorm.fr>
47ccfad4 11 * @copyright &copy; 1999-2006 The SquirrelMail Project Team
4b4abf93 12 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
134e4174 13 * @version $Id$
14 * @package squirrelmail
3f294f60 15 * @subpackage search
134e4174 16 * @link http://www.ietf.org/rfc/rfc3501.txt
b4425d7c 17 * @todo explain why references are used in function calls
134e4174 18 */
2d367c68 19
30967a1e 20/**
202bcbcc 21 * Include the SquirrelMail initialization file.
134e4174 22 */
202bcbcc 23require('../include/init.php');
86725763 24
7d60bda5 25/** SquirrelMail required files.
134e4174 26 */
202bcbcc 27require_once(SM_PATH . 'functions/imap_asearch.php');
28require_once(SM_PATH . 'functions/imap_messages.php');
29require_once(SM_PATH . 'functions/imap_general.php');
30require_once(SM_PATH . 'functions/mime.php');
31require_once(SM_PATH . 'functions/mailbox_display.php'); //getButton()
32require_once(SM_PATH . 'functions/forms.php');
33require_once(SM_PATH . 'functions/date.php');
c61bb006 34
7d60bda5 35/** Prefs array ordinals. Must match $recent_prefkeys and $saved_prefkeys
134e4174 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
134e4174 47 */
5b6abe97 48define('ASEARCH_CRITERIA', 'criteria');
7d60bda5 49
50/** Builds a href with params
134e4174 51 * @param string $params optional parameters to GET
b4425d7c 52 * @since 1.5.1
53 * @private
134e4174 54 */
7d60bda5 55function asearch_get_href($params = '')
56{
8cc8ec79 57 $href = 'search.php';
58 if ($params != '')
59 $href .= '?' . $params;
60 return $href;
7d60bda5 61}
62
63/** Builds a [link]
b4425d7c 64 * @param string $href (reference) URL
65 * @param string $text link name
66 * @param string $title link title attribute value
67 * @since 1.5.1
68 * @private
134e4174 69 */
94375458 70function asearch_get_link(&$href, $text, $title = '')
7d60bda5 71{
8cc8ec79 72 if ($title != '')
73 $title = ' title="' . $title . '"';
74 return '<a href="' . $href . '"' . $title . '>' . $text . '</a>';
7d60bda5 75}
76
77/** Builds a toggle [link]
134e4174 78 * @param integer $value
79 * @param string $action
80 * @param array $text_array
81 * @param array $title_array
b4425d7c 82 * @since 1.5.1
83 * @private
134e4174 84 */
94375458 85function asearch_get_toggle_link($value, $action, $text_array, $title_array = array())
7d60bda5 86{
aef9ca1e 87 $asearch_nz=asearch_nz($title_array[$value]);
88 $asearch_get_href=asearch_get_href($action . '=' . (int)$value);
89 return asearch_get_link($asearch_get_href, $text_array[$value], $asearch_nz);
7d60bda5 90}
91
40fbe929 92/**
b4425d7c 93 * Array sort callback used to sort $imap_asearch_options
134e4174 94 * @param string $a
95 * @param string $b
96 * @return bool strcoll()-like result
b4425d7c 97 * @since 1.5.0
98 * @private
134e4174 99 */
6b37252b 100function asearch_unhtml_strcoll($a, $b)
101{
b4425d7c 102 // FIXME: Translation policy says "no html entities in translations"
8cc8ec79 103 return strcoll(asearch_unhtmlentities($a), asearch_unhtmlentities($b));
6b37252b 104}
40fbe929 105
91c27aee 106
40fbe929 107/**
134e4174 108 * @param string $mailbox mailbox name utf7 encoded inc. special case INBOX
109 * @return string mailbox name ready to display (utf7 decoded or localized INBOX)
b4425d7c 110 * @since 1.5.0
111 * @private
134e4174 112 */
6b37252b 113function imap_get_mailbox_display($mailbox)
114{
8cc8ec79 115 if (strtoupper($mailbox) == 'INBOX')
116 return _("INBOX");
117 return imap_utf7_decode_local($mailbox);
6b37252b 118}
56e0b3b7 119
40fbe929 120/**
134e4174 121 * @param string $mailbox mailbox name or special case 'All Folders'
122 * @return string mailbox name ready to display (utf7 decoded or localized 'All Folders')
b4425d7c 123 * @since 1.5.0
124 * @private
134e4174 125 */
6b37252b 126function asearch_get_mailbox_display($mailbox)
127{
8cc8ec79 128 if ($mailbox == 'All Folders')
129 return _("All Folders");
130 return imap_get_mailbox_display($mailbox);
6b37252b 131}
0b97a708 132
40fbe929 133/**
b4425d7c 134 * @param array $color color array (unused)
134e4174 135 * @param string $txt text to display
136 * @return string title ready to display
b4425d7c 137 * @since 1.5.0
138 * @private
134e4174 139 */
b4425d7c 140function asearch_get_title_display($color, $txt)
6b37252b 141{
8cc8ec79 142 return '<b><big>' . $txt . '</big></b>';
6b37252b 143}
40fbe929 144/**
134e4174 145 * @param array $color color array
146 * @param string $txt text to display
147 * @return string error text ready to display
b4425d7c 148 * @since 1.5.0
149 * @private
134e4174 150 */
b4425d7c 151function asearch_get_error_display($color, $txt)
6b37252b 152{
8cc8ec79 153 return '<font color="' . $color[2] . '">' . '<b><big>' . $txt . '</big></b></font>';
0b97a708 154}
6b37252b 155
40fbe929 156/**
134e4174 157 * @param array $input_array array to serialize
158 * @return string a string containing a byte-stream representation of value that can be stored anywhere
b4425d7c 159 * @since 1.5.0
160 * @private
134e4174 161 */
7d60bda5 162function asearch_serialize(&$input_array)
6b37252b 163{
8cc8ec79 164 global $search_advanced;
165 if ($search_advanced)
166 return serialize($input_array);
167 return $input_array[0];
0b97a708 168}
6b37252b 169
40fbe929 170/**
134e4174 171 * @param string $input_string string to unserialize
172 * @return array
b4425d7c 173 * @since 1.5.0
174 * @private
134e4174 175 */
6b37252b 176function asearch_unserialize($input_string)
177{
8cc8ec79 178 global $search_advanced;
179 if ($search_advanced)
180 return unserialize($input_string);
181 return array($input_string);
0b97a708 182}
6b37252b 183
40fbe929 184/**
b4425d7c 185 * Gets user's advanced search preferences
186 *
187 * Arguments are different in 1.5.0.
134e4174 188 * @param string $key the pref key
189 * @param integer $index the pref key index
190 * @param string $default default value
191 * @return string pref value
b4425d7c 192 * @since 1.5.0
193 * @private
134e4174 194 */
7d60bda5 195function asearch_getPref(&$key, $index, $default = '')
6b37252b 196{
8cc8ec79 197 global $data_dir, $username, $search_advanced;
198 return getPref($data_dir, $username, $key . ($index + !$search_advanced), $default);
0b97a708 199}
6b37252b 200
40fbe929 201/**
b4425d7c 202 * Sets user's advanced search preferences
203 *
204 * Arguments are different in 1.5.0.
134e4174 205 * @param string $key the pref key
206 * @param integer $index the pref key index
207 * @param string $value pref value to set
208 * @return bool status
b4425d7c 209 * @since 1.5.0
210 * @private
134e4174 211 */
7d60bda5 212function asearch_setPref(&$key, $index, $value)
6b37252b 213{
8cc8ec79 214 global $data_dir, $username, $search_advanced;
215 return setPref($data_dir, $username, $key . ($index + !$search_advanced), $value);
0b97a708 216}
6b37252b 217
40fbe929 218/**
b4425d7c 219 * Deletes user's advanced search preferences
220 *
221 * Arguments are different in 1.5.0.
134e4174 222 * @param string $key the pref key
223 * @param integer $index the pref key index
224 * @return bool status
b4425d7c 225 * @since 1.5.0
226 * @private
134e4174 227 */
7d60bda5 228function asearch_removePref(&$key, $index)
6b37252b 229{
8cc8ec79 230 global $data_dir, $username, $search_advanced;
231 return removePref($data_dir, $username, $key . ($index + !$search_advanced));
0b97a708 232}
0b97a708 233
b4425d7c 234/**
235 * Sanity checks, done before running the imap command and before calling push_recent
236 * @param array $where_array search location data
237 * @param array $what_array search criteria data
238 * @param array $exclude_array excluded criteria data
239 * @return string error message or empty string
240 * @since 1.5.0
241 * @private
134e4174 242 */
7d60bda5 243function asearch_check_query(&$where_array, &$what_array, &$exclude_array)
6b37252b 244{
8cc8ec79 245 global $imap_asearch_opcodes;
05f5b952 246
8cc8ec79 247 if (empty($where_array))
248 return _("Please enter something to search for");
249 if (count($exclude_array) == count($where_array))
250 return _("There must be at least one criteria to search for");
251 for ($crit_num = 0; $crit_num < count($where_array); $crit_num++) {
252 $where = $where_array[$crit_num];
253 $what = $what_array[$crit_num];
254 if (!(($what == '') ^ ($imap_asearch_opcodes[$where] != '')))
255 return _("Error in criteria argument");
256 }
257 return '';
56e0b3b7 258}
259
b4425d7c 260/**
261 * Read the recent searches from the prefs
262 *
263 * Function arguments are different in 1.5.0
264 * @return array recent searches
265 * @since 1.5.0
266 * @private
134e4174 267 */
7d60bda5 268function asearch_read_recent()
6b37252b 269{
8cc8ec79 270 global $recent_prefkeys, $search_memory;
6b37252b 271
8cc8ec79 272 $recent_array = array();
273 $recent_num = 0;
274 for ($pref_num = 0; $pref_num < $search_memory; $pref_num++) {
275 foreach ($recent_prefkeys as $prefkey) {
276 $pref = asearch_getPref($prefkey, $pref_num);
134e4174 277/* if (!empty($pref))*/
8cc8ec79 278 $recent_array[$prefkey][$recent_num] = $pref;
279 }
280 if (empty($recent_array[$recent_prefkeys[0]][$recent_num])) {
281 foreach ($recent_prefkeys as $key) {
282 array_pop($recent_array[$key]);
283 }
134e4174 284// break; //Disabled to support old search code broken prefs
8cc8ec79 285 }
286 else
287 $recent_num++;
288 }
289 return $recent_array;
56e0b3b7 290}
23a9084b 291
b4425d7c 292/**
293 * Read the saved searches from the prefs
294 *
295 * Function arguments are different in 1.5.0
296 * @return array saved searches
297 * @since 1.5.0
298 * @private
134e4174 299 */
7d60bda5 300function asearch_read_saved()
6b37252b 301{
8cc8ec79 302 global $saved_prefkeys;
303
304 $saved_array = array();
305 $saved_key = $saved_prefkeys[0];
306 for ($saved_count = 0; ; $saved_count++) {
307 $pref = asearch_getPref($saved_key, $saved_count);
308 if (empty($pref))
309 break;
310 }
311 for ($saved_num = 0; $saved_num < $saved_count; $saved_num++) {
312 foreach ($saved_prefkeys as $key) {
313 $saved_array[$key][$saved_num] = asearch_getPref($key, $saved_num);
314 }
315 }
316 return $saved_array;
6b37252b 317}
318
b4425d7c 319/**
320 * Save a recent search to the prefs
321 *
322 * Function arguments are different in 1.5.0
323 * @param integer $recent_index
324 * @since 1.5.0
325 * @private
134e4174 326 */
7d60bda5 327function asearch_save_recent($recent_index)
6b37252b 328{
8cc8ec79 329 global $recent_prefkeys, $saved_prefkeys;
330
331 $saved_array = asearch_read_saved();
b4425d7c 332 if (isset($saved_array[$saved_prefkeys[0]])) {
333 $saved_index = count($saved_array[$saved_prefkeys[0]]);
334 } else {
335 $saved_index = 0;
336 }
8cc8ec79 337 $recent_array = asearch_read_recent();
338 $n = 0;
339 foreach ($recent_prefkeys as $key) {
340 $recent_slice = array_slice($recent_array[$key], $recent_index, 1);
341 if (!empty($recent_slice[0]))
342 asearch_setPref($saved_prefkeys[$n], $saved_index, $recent_slice[0]);
343 else
344 asearch_removePref($saved_prefkeys[$n], $saved_index);
345 $n++;
346 }
6b37252b 347}
348
b4425d7c 349/**
350 * Write a recent search to prefs
351 *
352 * Function arguments are different in 1.5.0
353 * @param array $recent_array
354 * @since 1.5.0
355 * @private
134e4174 356 */
7d60bda5 357function asearch_write_recent(&$recent_array)
6b37252b 358{
8cc8ec79 359 global $recent_prefkeys, $search_memory;
6b37252b 360
8cc8ec79 361 $recent_count = min($search_memory, count($recent_array[$recent_prefkeys[0]]));
362 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
363 foreach ($recent_prefkeys as $key) {
364 asearch_setPref($key, $recent_num, $recent_array[$key][$recent_num]);
365 }
366 }
367 for (; $recent_num < $search_memory; $recent_num++) {
368 foreach ($recent_prefkeys as $key) {
369 asearch_removePref($key, $recent_num);
370 }
371 }
6b37252b 372}
373
b4425d7c 374/**
375 * Remove a recent search from prefs
376 *
377 * Function arguments are different in 1.5.0
378 * @param integer $forget_index removed search number
379 * @since 1.5.0
380 * @private
134e4174 381 */
7d60bda5 382function asearch_forget_recent($forget_index)
6b37252b 383{
8cc8ec79 384 global $recent_prefkeys;
6b37252b 385
8cc8ec79 386 $recent_array = asearch_read_recent();
387 foreach ($recent_prefkeys as $key) {
388 array_splice($recent_array[$key], $forget_index, 1);
389 }
390 asearch_write_recent($recent_array);
6b37252b 391}
392
b4425d7c 393/**
394 * Find a recent search in the prefs (used to avoid saving duplicates)
395 * @param array $recent_array
396 * @param array $mailbox_array
397 * @param array $biop_array
398 * @param array $unop_array
399 * @param array $where_array
400 * @param array $what_array
401 * @param array $exclude_array
402 * @param array $sub_array
403 * @return integer
404 * @since 1.5.0
405 * @private
134e4174 406 */
7d60bda5 407function asearch_find_recent(&$recent_array, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 408{
8cc8ec79 409 global $recent_prefkeys, $search_advanced;
410
411 $where_string = asearch_serialize($where_array);
412 $mailbox_string = asearch_serialize($mailbox_array);
413 $what_string = asearch_serialize($what_array);
414 $unop_string = asearch_serialize($unop_array);
415 if ($search_advanced) {
416 $biop_string = asearch_serialize($biop_array);
417 $exclude_string = asearch_serialize($exclude_array);
418 $sub_string = asearch_serialize($sub_array);
419 }
420 $recent_count = count($recent_array[$recent_prefkeys[ASEARCH_WHERE]]);
421 for ($recent_num = 0; $recent_num < $recent_count; $recent_num++) {
422 if (isset($recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num])) {
423 if (
424 $where_string == $recent_array[$recent_prefkeys[ASEARCH_WHERE]][$recent_num] &&
425 $mailbox_string == $recent_array[$recent_prefkeys[ASEARCH_MAILBOX]][$recent_num] &&
426 $what_string == $recent_array[$recent_prefkeys[ASEARCH_WHAT]][$recent_num] &&
427 $unop_string == $recent_array[$recent_prefkeys[ASEARCH_UNOP]][$recent_num] &&
428 ((!$search_advanced) ||
429 ($biop_string == $recent_array[$recent_prefkeys[ASEARCH_BIOP]][$recent_num] &&
430 $exclude_string == $recent_array[$recent_prefkeys[ASEARCH_EXCLUDE]][$recent_num] &&
431 $sub_string == $recent_array[$recent_prefkeys[ASEARCH_SUB]][$recent_num]))
432 )
433 return $recent_num;
434 }
435 }
436 return -1;
6b37252b 437}
438
b4425d7c 439/**
440 * Push a recent search into the prefs
441 * @param array $recent_array
442 * @param array $mailbox_array
443 * @param array $biop_array
444 * @param array $unop_array
445 * @param array $where_array
446 * @param array $what_array
447 * @param array $exclude_array
448 * @param array $sub_array
449 * @since 1.5.0
450 * @private
134e4174 451 */
7d60bda5 452function asearch_push_recent(&$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 453{
8cc8ec79 454 global $recent_prefkeys, $search_memory;
324ac3c5 455 //global $what; // Hack to access issued search from read_body.php
456 $what = 1;
457 /**
458 * Update search history and store it in the session so we can retrieve the
459 * issued search when returning from an external page like read_body.php
460 */
461 $criteria[$what] = array($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
8cc8ec79 462 sqsession_register($criteria, ASEARCH_CRITERIA);
463 if ($search_memory > 0) {
464 $recent_array = asearch_read_recent();
465 $recent_found = asearch_find_recent($recent_array, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
134e4174 466 if ($recent_found >= 0) { // Remove identical recent
8cc8ec79 467 foreach ($recent_prefkeys as $key) {
468 array_splice($recent_array[$key], $recent_found, 1);
469 }
470 }
471 $input = array($where_array, $mailbox_array, $what_array, $unop_array, $biop_array, $exclude_array, $sub_array);
472 $i = 0;
473 foreach ($recent_prefkeys as $key) {
474 array_unshift($recent_array[$key], asearch_serialize($input[$i]));
475 $i++;
476 }
477 asearch_write_recent($recent_array);
478 }
6b37252b 479}
480
b4425d7c 481/**
482 * Edit a recent search
483 *
484 * Function arguments are different in 1.5.0
134e4174 485 * @global array mailbox_array searched mailboxes
b4425d7c 486 * @param mixed $index
487 * @since 1.5.0
488 * @private
134e4174 489 */
7d60bda5 490function asearch_edit_recent($index)
6b37252b 491{
8cc8ec79 492 global $recent_prefkeys, $search_advanced;
493 global $where_array, $mailbox_array, $what_array, $unop_array;
494 global $biop_array, $exclude_array, $sub_array;
7d60bda5 495
8cc8ec79 496 $where_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHERE], $index));
497 $mailbox_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_MAILBOX], $index));
498 $what_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_WHAT], $index));
499 $unop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_UNOP], $index));
500 if ($search_advanced) {
501 $biop_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_BIOP], $index));
502 $exclude_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_EXCLUDE], $index));
503 $sub_array = asearch_unserialize(asearch_getPref($recent_prefkeys[ASEARCH_SUB], $index));
504 }
6b37252b 505}
506
b4425d7c 507/**
508 * Get last search criteria from session or prefs
509 *
510 * Function arguments are different in 1.5.0
511 * FIXME, try to avoid globals
512 * @param mixed $index
513 * @since 1.5.0
514 * @private
134e4174 515 */
324ac3c5 516function asearch_edit_last($index) {
8cc8ec79 517 if (sqGetGlobalVar(ASEARCH_CRITERIA, $criteria, SQ_SESSION)) {
518 global $where_array, $mailbox_array, $what_array, $unop_array;
519 global $biop_array, $exclude_array, $sub_array;
324ac3c5 520 $mailbox_array = $criteria[$index][0];
521 $biop_array = $criteria[$index][1];
522 $unop_array = $criteria[$index][2];
523 $where_array = $criteria[$index][3];
524 $what_array = $criteria[$index][4];
525 $exclude_array = $criteria[$index][5];
526 $sub_array = $criteria[$index][6];
527 unset($criteria[$index]);
528 //sqsession_unregister(ASEARCH_CRITERIA);
529 } else {
8cc8ec79 530 global $search_memory;
324ac3c5 531 if ($search_memory > 0) {
8cc8ec79 532 asearch_edit_recent(0);
324ac3c5 533 }
8cc8ec79 534 }
6b37252b 535}
536
b4425d7c 537/**
538 * Edit a saved search
539 *
540 * Function arguments are different in 1.5.0
541 * @param mixed $index
542 * @since 1.5.0
543 * @private
134e4174 544 */
7d60bda5 545function asearch_edit_saved($index)
6b37252b 546{
8cc8ec79 547 global $saved_prefkeys, $search_advanced;
548 global $where_array, $mailbox_array, $what_array, $unop_array;
549 global $biop_array, $exclude_array, $sub_array;
7d60bda5 550
8cc8ec79 551 $where_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHERE], $index));
552 $mailbox_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_MAILBOX], $index));
553 $what_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_WHAT], $index));
554 $unop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_UNOP], $index));
555 if ($search_advanced) {
556 $biop_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_BIOP], $index));
557 $exclude_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_EXCLUDE], $index));
558 $sub_array = asearch_unserialize(asearch_getPref($saved_prefkeys[ASEARCH_SUB], $index));
559 }
6b37252b 560}
561
b4425d7c 562/**
563 * Write a saved search to the prefs
564 *
565 * Function arguments are different in 1.5.0
566 * @param array $saved_array
567 * @since 1.5.0
568 * @private
134e4174 569 */
7d60bda5 570function asearch_write_saved(&$saved_array)
6b37252b 571{
8cc8ec79 572 global $saved_prefkeys;
6b37252b 573
8cc8ec79 574 $saved_count = count($saved_array[$saved_prefkeys[0]]);
575 for ($saved_num=0; $saved_num < $saved_count; $saved_num++) {
576 foreach ($saved_prefkeys as $key) {
577 asearch_setPref($key, $saved_num, $saved_array[$key][$saved_num]);
578 }
579 }
580 foreach ($saved_prefkeys as $key) {
581 asearch_removePref($key, $saved_count);
582 }
6b37252b 583}
584
b4425d7c 585/**
586 * Delete a saved search from the prefs
587 *
588 * Function arguments are different in 1.5.0
589 * @param integer $saved_index
590 * @since 1.5.0
591 * @private
134e4174 592 */
7d60bda5 593function asearch_delete_saved($saved_index)
6b37252b 594{
8cc8ec79 595 global $saved_prefkeys;
6b37252b 596
8cc8ec79 597 $saved_array = asearch_read_saved();
598 $asearch_keys = $saved_prefkeys;
599 foreach ($asearch_keys as $key) {
600 array_splice($saved_array[$key], $saved_index, 1);
601 }
602 asearch_write_saved($saved_array);
6b37252b 603}
604
7d60bda5 605/** Translate the input date to imap date to local date display,
134e4174 606 * so the user can know if the date is wrong or illegal
b4425d7c 607 * @param string $what date string
134e4174 608 * @return string locally formatted date or error text
b4425d7c 609 * @since 1.5.0
610 * @private
134e4174 611 */
7d60bda5 612function asearch_get_date_display(&$what)
6b37252b 613{
8cc8ec79 614 $what_parts = sqimap_asearch_parse_date($what);
615 if (count($what_parts) == 4) {
616 if (checkdate($what_parts[2], $what_parts[1], $what_parts[3]))
617 return date_intl(_("M j, Y"), mktime(0,0,0,$what_parts[2],$what_parts[1],$what_parts[3]));
618 //return $what_parts[1] . ' ' . getMonthName($what_parts[2]) . ' ' . $what_parts[3];
619 return _("(Illegal date)");
620 }
621 return _("(Wrong date)");
6b37252b 622}
623
b4425d7c 624/**
625 * Translate the query to rough natural display
626 * @param array $color
627 * @param array $mailbox_array
628 * @param array $biop_array
629 * @param array $unop_array
630 * @param array $where_array
631 * @param array $what_array
632 * @param array $exclude_array
633 * @param array $sub_array
134e4174 634 * @return string rough natural query ready to display
b4425d7c 635 * @since 1.5.0
636 * @private
134e4174 637 */
7d60bda5 638function asearch_get_query_display(&$color, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array)
6b37252b 639{
8cc8ec79 640 global $imap_asearch_biops_in, $imap_asearch_biops, $imap_asearch_unops, $imap_asearch_options;
641 global $imap_asearch_opcodes;
642
643 $last_mailbox = $mailbox_array[0];
644 if (empty($last_mailbox))
645 $last_mailbox = 'INBOX';
646 $query_display = '';
647 for ($crit_num=0; $crit_num < count($where_array); $crit_num++) {
648 if ((!isset($exclude_array[$crit_num])) || (!$exclude_array[$crit_num])) {
649 $cur_mailbox = $mailbox_array[$crit_num];
650 if (empty($cur_mailbox))
651 $cur_mailbox = 'INBOX';
652 $biop = asearch_nz($biop_array[$crit_num]);
653 if (($query_display == '') || ($cur_mailbox != $last_mailbox)) {
fa62b054 654 $mailbox_display = ' <b>' . htmlspecialchars(asearch_get_mailbox_display($cur_mailbox)) . '</b>';
8cc8ec79 655 if ($query_display == '')
656 $biop_display = _("In");
657 else
658 $biop_display = $imap_asearch_biops_in[$biop];
659 $last_mailbox = $cur_mailbox;
660 }
661 else {
662 $mailbox_display = '';
663 $biop_display = $imap_asearch_biops[$biop];
664 }
665 $unop = $unop_array[$crit_num];
666 $unop_display = $imap_asearch_unops[$unop];
667 if ($unop_display != '')
668 $unop_display .= ' ';
669 $where = $where_array[$crit_num];
670 $where_display = $unop_display . asearch_nz($imap_asearch_options[$where], $where);
671 $what_type = $imap_asearch_opcodes[$where];
672 $what = $what_array[$crit_num];
134e4174 673 if ($what_type) { /* Check opcode parameter */
8cc8ec79 674 if ($what == '')
675 $what_display = ' ' . asearch_get_error_display($color, _("(Missing argument)"));
676 else {
677 if ($what_type == 'adate')
678 $what_display = asearch_get_date_display($what);
679 else
680 $what_display = htmlspecialchars($what);
681 $what_display = ' <b>' . $what_display . '</b>';
682 }
683 }
684 else {
685 if ($what)
686 $what_display = ' ' . asearch_get_error_display($color, _("(Spurious argument)"));
687 else
688 $what_display = '';
689 }
690 if ($mailbox_display != '')
691 $query_display .= ' <u><i>' . $biop_display . '</i></u>' . $mailbox_display . ' <u><i>' . $where_display . '</i></u>' . $what_display;
692 else
693 $query_display .= ' <u><i>' . $biop_display . ' ' . $where_display . '</i></u>' . $what_display;
694 }
695 }
696 return $query_display;
6b37252b 697}
698
91c27aee 699/**
700 * Creates button
701 *
702 * @deprecated see form functions available in 1.5.1 and 1.4.3.
703 * @param string $type
704 * @param string $name
705 * @param string $value
706 * @param string $js
707 * @param bool $enabled
708 */
709function getButton($type, $name, $value, $js = '', $enabled = TRUE) {
710 $disabled = ( $enabled ? '' : 'disabled ' );
711 $js = ( $js ? $js.' ' : '' );
712 return '<input '.$disabled.$js.
713 'type="'.$type.
714 '" name="'.$name.
715 '" value="'.$value .
716 '" style="padding: 0px; margin: 0px" />';
717}
718
719
7d60bda5 720/** Handle the alternate row colors
b4425d7c 721 * @param array $color color theme array
722 * @param integer $row_num
134e4174 723 * @return string color value
b4425d7c 724 * @since 1.5.0
725 * @private
134e4174 726 */
7d60bda5 727function asearch_get_row_color(&$color, $row_num)
6b37252b 728{
729/*$color_string = ($row_num%2 ? $color[0] : $color[4]);*/
8cc8ec79 730 $color_string = $color[4];
731 if ($GLOBALS['alt_index_colors']) {
732 if (($row_num % 2) == 0) {
733 if (!isset($color[12]))
734 $color[12] = '#EAEAEA';
735 $color_string = $color[12];
736 }
737 }
738 return $color_string;
6b37252b 739}
740
b4425d7c 741/**
742 * Print a whole query array, recent or saved
743 *
744 * Function arguments are different in 1.5.0
745 * @param array $boxes (unused)
746 * @param array $query_array
747 * @param mixed $query_keys
748 * @param array $action_array
749 * @param mixed $title
750 * @param string $show_pref
751 * @since 1.5.0
752 * @private
134e4174 753 */
7d60bda5 754function asearch_print_query_array(&$boxes, &$query_array, &$query_keys, &$action_array, $title, $show_pref)
6b37252b 755{
8cc8ec79 756 global $color;
757 global $data_dir, $username;
758 global $use_icons, $icon_theme;
759
760 $show_flag = getPref($data_dir, $username, $show_pref, 0) & 1;
761 $use_icons_flag = ($use_icons) && ($icon_theme != 'none');
762 if ($use_icons_flag)
763 $text_array = array('<img src="' . SM_PATH . 'images/minus.png" border="0" height="7" width="7" />',
764 '<img src="' . SM_PATH . 'images/plus.png" border="0" height="7" width="7" />');
765 else
766 $text_array = array('-', '+');
767 $toggle_link = asearch_get_toggle_link(!$show_flag, $show_pref, $text_array, array(_("Fold"), _("Unfold")));
768 if (!$use_icons_flag)
769 $toggle_link = '<small>[' . $toggle_link . ']</small>';
770
771 echo "<br />\n";
772 echo html_tag('table', '', 'center', $color[9], 'width="95%" cellpadding="1" cellspacing="1" border="0"');
773 echo html_tag('tr',
774 html_tag('td', $toggle_link, 'center', $color[5], 'width="5%"')
775 . html_tag('td', asearch_get_title_display($color, $title), 'center', $color[5], 'colspan=4'));
776 if ($show_flag) {
777 $main_key = $query_keys[ASEARCH_WHERE];
778 $query_count = count($query_array[$main_key]);
779 for ($query_num = 0, $row_num = 0; $query_num < $query_count; $query_num++) {
780 if (!empty($query_array[$main_key][$query_num])) {
781 echo html_tag('tr', '', '', asearch_get_row_color($color, $row_num));
782
783 unset($search_array);
784 foreach ($query_keys as $query_key) {
785 $search_array[] = asearch_unserialize($query_array[$query_key][$query_num]);
786 }
787 $where_array = $search_array[ASEARCH_WHERE];
788 $mailbox_array = $search_array[ASEARCH_MAILBOX];
789 $what_array = $search_array[ASEARCH_WHAT];
790 $unop_array = $search_array[ASEARCH_UNOP];
791 $biop_array = asearch_nz($search_array[ASEARCH_BIOP], array());
792 $exclude_array = asearch_nz($search_array[ASEARCH_EXCLUDE], array());
793 $sub_array = asearch_nz($search_array[ASEARCH_SUB], array());
794 $query_display = asearch_get_query_display($color, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
795
796 echo html_tag('td', $query_num + 1, 'right');
797 echo html_tag('td', $query_display, 'center', '', 'width="80%"');
798 foreach ($action_array as $action => $action_display) {
799 echo html_tag('td', '<a href="' . asearch_get_href('submit=' . $action . '&amp;rownum=' . $query_num) . '">' . $action_display . '</a>', 'center');
800 }
801
802 echo '</tr>' . "\n";
803 $row_num++;
804 }
805 }
806 }
807 echo '</table>' . "\n";
6b37252b 808}
809
7d60bda5 810/** Print the saved array
b4425d7c 811 *
812 * Function arguments are different in 1.5.0
813 * @param array $boxes (unused, see asearch_print_query_array())
814 * @since 1.5.0
815 * @private
134e4174 816 */
7d60bda5 817function asearch_print_saved(&$boxes)
6b37252b 818{
8cc8ec79 819 global $saved_prefkeys;
6b37252b 820
8cc8ec79 821 $saved_array = asearch_read_saved();
822 if (isset($saved_array[$saved_prefkeys[0]])) {
823 $saved_count = count($saved_array[$saved_prefkeys[0]]);
824 if ($saved_count > 0) {
674a2ca2 825 $saved_actions = array('edit_saved' => _("Edit"), 'search_saved' => _("Search"), 'delete_saved' => _("Delete"));
8cc8ec79 826 asearch_print_query_array($boxes, $saved_array, $saved_prefkeys, $saved_actions, _("Saved Searches"), 'search_show_saved');
827 }
828 }
70c4fd84 829}
6c8388a9 830
7d60bda5 831/**
134e4174 832 * Print the recent array
b4425d7c 833 *
834 * Function arguments are different in 1.5.0
835 * @param array $boxes (unused, see asearch_print_query_array())
836 * @since 1.5.0
837 * @private
134e4174 838 */
7d60bda5 839function asearch_print_recent(&$boxes)
6b37252b 840{
8cc8ec79 841 global $recent_prefkeys, $search_memory;
6b37252b 842
8cc8ec79 843 $recent_array = asearch_read_recent();
844 if (isset($recent_array[$recent_prefkeys[0]])) {
845 $recent_count = count($recent_array[$recent_prefkeys[0]]);
846 if (min($recent_count, $search_memory) > 0) {
847 $recent_actions = array('save_recent' => _("save"), 'search_recent' => _("search"), 'forget_recent' => _("forget"));
848 asearch_print_query_array($boxes, $recent_array, $recent_prefkeys, $recent_actions, _("Recent Searches"), 'search_show_recent');
849 }
850 }
56e0b3b7 851}
852
b4425d7c 853/**
854 * Build an <option> statement
855 * @param string $var option value
856 * @param string $set default option value
857 * @param string $tit displayed option name
858 * @return string formated html option tag
859 * @since 1.5.0
860 * @private
134e4174 861 */
920ec5ec 862function asearch_opt($val, $sel, $tit)
6b37252b 863{
e1728a7a 864 return '<option value="' . $val . '"' . ($sel == $val ? ' selected="selected"' : '') . '>' . $tit . '</option>' . "\n";
6b37252b 865}
866
7d60bda5 867/** Build a <select> statement from an array
b4425d7c 868 * @param string $var_name select option name
869 * @param array $opt_array select option values
870 * @param string $cur_val default value
871 * @return string html formated select tag
872 * @deprecated FIXME use standard form functions or
873 * explain why they are not used.
874 * @since 1.5.0
875 * @private
134e4174 876 */
6b37252b 877function asearch_opt_array($var_name, $opt_array, $cur_val)
878{
8cc8ec79 879 $output = '<select name="' . $var_name . '">' . "\n";
880 foreach($opt_array as $val => $display)
881 $output .= asearch_opt($val, $cur_val, asearch_nz($display, $val));
882 $output .= '</select>' . "\n";
883 return $output;
6b37252b 884}
885
7d60bda5 886/** Verify that a mailbox exists
b4425d7c 887 * @param string $mailbox
888 * @param array $boxes
134e4174 889 * @return bool mailbox exists
b4425d7c 890 * @deprecated FIXME use standard functions
891 * @since 1.5.0
892 * @private
134e4174 893 */
7d60bda5 894function asearch_mailbox_exists($mailbox, &$boxes)
920ec5ec 895{
8cc8ec79 896 foreach ($boxes as $box) {
897 if ($box['unformatted'] == $mailbox)
898 return TRUE;
899 }
900 return FALSE;
920ec5ec 901}
902
b4425d7c 903/**
904 * Build the mailbox select
905 * @param stream $imapConnection
906 * @param array $boxes
907 * @param string $mailbox
908 * @param integer $row_num
909 * @since 1.5.1
910 * @private
134e4174 911 */
7d60bda5 912function asearch_get_form_mailbox($imapConnection, &$boxes, $mailbox, $row_num = 0)
6b37252b 913{
324ac3c5 914 if (($mailbox != 'All Folders') && (!asearch_mailbox_exists($mailbox, $boxes))) {
fa62b054 915 $missing = asearch_opt($mailbox, $mailbox, '[' . _("Missing") . '] ' . htmlspecialchars(asearch_get_mailbox_display($mailbox)));
324ac3c5 916 } else {
8cc8ec79 917 $missing = '';
324ac3c5 918 }
8cc8ec79 919 return '<select name="mailbox[' . $row_num . ']">'
920 . $missing
921 . asearch_opt('All Folders', $mailbox, '[' . asearch_get_mailbox_display('All Folders') . ']')
922 . sqimap_mailbox_option_list($imapConnection, array(strtolower($mailbox)), 0, $boxes, NULL)
923 . '</select>';
7d60bda5 924}
925
b4425d7c 926/**
927 * Build the Include subfolders checkbox
928 * @param string $sub
929 * @param integer $row_num
930 * @return string
931 * @since 1.5.1
932 * @private
134e4174 933 */
7d60bda5 934function asearch_get_form_sub($sub, $row_num = 0)
935{
582d0621 936 return addCheckBox('sub[' . $row_num .']', $sub);
7d60bda5 937}
938
5b6abe97 939/** Build the 2 unop and where selects
b4425d7c 940 * @param string $unop default value
941 * @param string $where default value
942 * @param integer $row_num
943 * @return string
944 * @since 1.5.1
945 * @private
134e4174 946 */
7d60bda5 947function asearch_get_form_location($unop, $where, $row_num = 0)
948{
8cc8ec79 949 global $imap_asearch_unops, $imap_asearch_options;
7d60bda5 950
8cc8ec79 951 return asearch_opt_array('unop[' . $row_num . ']', $imap_asearch_unops, $unop)
952 . asearch_opt_array('where[' . $row_num . ']', $imap_asearch_options, $where);
7d60bda5 953}
954
5b6abe97 955/** Build the what text input
b4425d7c 956 * @param string $what
957 * @param integer $row_num
958 * @return string
959 * @since 1.5.1
960 * @private
134e4174 961 */
7d60bda5 962function asearch_get_form_what($what, $row_num = 0)
963{
582d0621 964 return addInput('what[' . $row_num . ']', $what, '35');
7d60bda5 965}
966
5b6abe97 967/** Build the Exclude criteria checkbox
b4425d7c 968 * @param boolean $exclude
969 * @param integer $row_num
970 * @return string
971 * @since 1.5.1
972 * @private
134e4174 973 */
7d60bda5 974function asearch_get_form_exclude($exclude, $row_num = 0)
975{
582d0621 976 return addCheckBox('exclude['.$row_num.']', $exclude);
7d60bda5 977}
978
979/** Print one advanced form row
b4425d7c 980 * @param stream $imapConnection
981 * @param array $boxes
982 * @param string $mailbox
983 * @param mixed $biop
984 * @param string $unop
985 * @param string $where
986 * @param string $what
987 * @param boolean $exclude
988 * @param mixed $sub
989 * @param integer $row_num
990 * @since 1.5.0
991 * @private
134e4174 992 */
7d60bda5 993function asearch_print_form_row($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
994{
8cc8ec79 995 global $imap_asearch_biops_in;
996 global $color;
6b37252b 997
8cc8ec79 998 echo html_tag('tr', '', '', $color[4]);
6b37252b 999
7d60bda5 1000//Binary operator
8cc8ec79 1001 echo html_tag('td', $row_num ?
1002 asearch_opt_array('biop[' . $row_num . ']', $imap_asearch_biops_in, $biop)
134e4174 1003 : '<b>' . _("In") . '</b>', 'center') . "\n";
7d60bda5 1004
1005//Mailbox list and Include Subfolders
8cc8ec79 1006 echo html_tag('td',
1007 asearch_get_form_mailbox($imapConnection, $boxes, $mailbox, $row_num)
1008 . _("and&nbsp;subfolders:") . asearch_get_form_sub($sub, $row_num), 'center') . "\n";
6b37252b 1009
7d60bda5 1010//Unary operator and Search location
8cc8ec79 1011 echo html_tag('td', asearch_get_form_location($unop, $where, $row_num), 'center') . "\n";
6b37252b 1012
7d60bda5 1013//Text input
8cc8ec79 1014 echo html_tag('td', asearch_get_form_what($what, $row_num), 'center') . "\n";
7d60bda5 1015
1016//Exclude criteria
8cc8ec79 1017 echo html_tag('td', _("Exclude Criteria:") . asearch_get_form_exclude($exclude, $row_num), 'center') . "\n";
6b37252b 1018
8cc8ec79 1019 echo "</tr>\n";
6b37252b 1020}
1021
7d60bda5 1022/** Print the advanced search form
b4425d7c 1023 * @param stream $imapConnection
1024 * @param array $boxes
1025 * @param array $mailbox_array
1026 * @param array $biop_array
1027 * @param array $unop_array
1028 * @param array $where_array
1029 * @param array $what_array
1030 * @param array $exclude_array
1031 * @param array $sub_array
1032 * @since 1.5.0
1033 * @private
134e4174 1034 */
7d60bda5 1035function asearch_print_form($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
6b37252b 1036{
8cc8ec79 1037 global $search_button_html, $add_criteria_button_html, $del_excluded_button_html, $del_all_button_html;
1038 global $color;
6b37252b 1039
7d60bda5 1040//Search Form
8cc8ec79 1041 echo "<br />\n";
1042 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
1043
1044 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
1045 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=5'));
1046 $row_count = count($where_array);
1047 for ($row_num = 0; $row_num < $row_count; $row_num++) {
1048 $mailbox = asearch_nz($mailbox_array[$row_num]);
1049 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
1050 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
1051 $where = strip_tags(asearch_nz($where_array[$row_num]));
1052 $what = asearch_nz($what_array[$row_num]);
1053 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
1054 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
1055 asearch_print_form_row($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
1056 }
1057 echo '</table>' . "\n";
6b37252b 1058
7d60bda5 1059//Submit buttons
8cc8ec79 1060 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1061 echo html_tag('tr',
e1728a7a 1062 html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n"
1063 . html_tag('td', getButton('submit', 'submit', $add_criteria_button_html), 'center') . "\n"
1064 . html_tag('td', getButton('submit', 'submit', $del_all_button_html), 'center') . "\n"
1065 . html_tag('td', getButton('submit', 'submit', $del_excluded_button_html), 'center') . "\n"
8cc8ec79 1066 );
1067 echo '</table>' . "\n";
1068 echo '</form>' . "\n";
6b37252b 1069}
1070
7d60bda5 1071/** Print one basic form row
b4425d7c 1072 * @param stream $imapConnection
1073 * @param array $boxes
1074 * @param string $mailbox
1075 * @param mixed $biop
1076 * @param mixed $unop
1077 * @param string $where
1078 * @param string $what
1079 * @param boolean $exclude
1080 * @param mixed $sub
1081 * @param integer $row_num
1082 * @since 1.5.1
1083 * @private
134e4174 1084 */
7d60bda5 1085function asearch_print_form_row_basic($imapConnection, &$boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num)
1086{
8cc8ec79 1087 global $search_button_html;
1088 global $color;
7d60bda5 1089
8cc8ec79 1090 echo html_tag('tr', '', '', $color[4]);
7d60bda5 1091
1092//Mailbox list
8cc8ec79 1093 echo html_tag('td', '<b>' . _("In") . '</b> ' . asearch_get_form_mailbox($imapConnection, $boxes, $mailbox), 'center') . "\n";
7d60bda5 1094
1095//Unary operator and Search location
8cc8ec79 1096 echo html_tag('td', asearch_get_form_location($unop, $where), 'center') . "\n";
7d60bda5 1097
1098//Text input
8cc8ec79 1099 echo html_tag('td', asearch_get_form_what($what), 'center') . "\n";
7d60bda5 1100
1101//Submit button
e1728a7a 1102 echo html_tag('td', getButton('submit', 'submit', $search_button_html), 'center') . "\n";
7d60bda5 1103
8cc8ec79 1104 echo "</tr>\n";
7d60bda5 1105}
1106
1107/** Print the basic search form
b4425d7c 1108 * @param stream $imapConnection
1109 * @param array $boxes
1110 * @param array $mailbox_array
1111 * @param array $biop_array
1112 * @param array $unop_array
1113 * @param array $where_array
1114 * @param array $what_array
1115 * @param array $exclude_array
1116 * @param array $sub_array
1117 * @since 1.5.1
1118 * @private
134e4174 1119 */
7d60bda5 1120function asearch_print_form_basic($imapConnection, &$boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array)
1121{
8cc8ec79 1122 global $color;
7d60bda5 1123
1124//Search Form
8cc8ec79 1125 echo "<br />\n";
1126 echo '<form action="' . asearch_get_href() . '" name="form_asearch">' . "\n";
1127
1128 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="1" border="0"');
1129 //echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Criteria")), 'center', $color[5], 'colspan=4'));
1130 $row_count = count($where_array);
1131 for ($row_num = 0; $row_num < $row_count; $row_num++) {
1132 $mailbox = asearch_nz($mailbox_array[$row_num]);
1133 $biop = strip_tags(asearch_nz($biop_array[$row_num]));
1134 $unop = strip_tags(asearch_nz($unop_array[$row_num]));
1135 $where = strip_tags(asearch_nz($where_array[$row_num]));
1136 $what = asearch_nz($what_array[$row_num]);
1137 $exclude = strip_tags(asearch_nz($exclude_array[$row_num]));
1138 $sub = strip_tags(asearch_nz($sub_array[$row_num]));
1139 asearch_print_form_row_basic($imapConnection, $boxes, $mailbox, $biop, $unop, $where, $what, $exclude, $sub, $row_num);
1140 }
1141 echo '</table>' . "\n";
1142 echo '</form>' . "\n";
7d60bda5 1143}
1144
59a623e6 1145
40fbe929 1146/**
134e4174 1147 * @param array $boxes mailboxes array (reference)
1148 * @return array selectable unformatted mailboxes names
b4425d7c 1149 * @since 1.5.0
1150 * @private
134e4174 1151 */
40fbe929 1152function sqimap_asearch_get_selectable_unformatted_mailboxes(&$boxes)
1153{
8cc8ec79 1154 $mboxes_array = array();
1155 $boxcount = count($boxes);
1156 for ($boxnum = 0; $boxnum < $boxcount; $boxnum++) {
1157 if (!in_array('noselect', $boxes[$boxnum]['flags']))
1158 $mboxes_array[] = $boxes[$boxnum]['unformatted'];
1159 }
1160 return $mboxes_array;
40fbe929 1161}
1162
0d672ac0 1163/* ------------------------ main ------------------------ */
7d60bda5 1164/* get globals we will need */
7d60bda5 1165sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
6b37252b 1166
ad2212db 1167if ( sqgetGlobalVar('checkall', $temp, SQ_GET) ) {
139b3991 1168 $checkall = (int) $temp;
ad2212db 1169}
1170
324ac3c5 1171/**
1172 * Retrieve the mailbox cache from the session.
1173 */
1174sqgetGlobalVar('mailbox_cache',$mailbox_cache,SQ_SESSION);
1175
139b3991 1176
6b37252b 1177$search_button_html = _("Search");
1178$search_button_text = asearch_unhtmlentities($search_button_html);
1179$add_criteria_button_html = _("Add New Criteria");
1180$add_criteria_button_text = asearch_unhtmlentities($add_criteria_button_html);
34a85c56 1181$del_excluded_button_html = _("Remove Excluded Criteria");
6b37252b 1182$del_excluded_button_text = asearch_unhtmlentities($del_excluded_button_html);
34a85c56 1183$del_all_button_html = _("Remove All Criteria");
6b37252b 1184$del_all_button_text = asearch_unhtmlentities($del_all_button_html);
1185
7d60bda5 1186/** Maximum number of recent searches to handle
134e4174 1187 * Default 0
1188 * @global integer $search_memory
1189 */
7d60bda5 1190$search_memory = getPref($data_dir, $username, 'search_memory', 0);
1191
1192/** Advanced search control
134e4174 1193 * - 0 => allow basic interface only
1194 * - 1 => allow advanced interface only
1195 * - 2 => allow both
1196 * Default 2
1197 */
5b6abe97 1198$allow_advanced_search = asearch_nz($allow_advanced_search, 2);
6b37252b 1199
7d60bda5 1200/**
134e4174 1201 * Toggle advanced/basic search
1202 */
324ac3c5 1203if (sqgetGlobalVar('advanced', $search_advanced, SQ_GET)) {
8cc8ec79 1204 setPref($data_dir, $username, 'search_advanced', $search_advanced & 1);
324ac3c5 1205}
7d60bda5 1206/** If 1, show advanced search interface
134e4174 1207 * Default from allow_advanced_search pref
1208 * @global integer $search_advanced
1209 */
324ac3c5 1210if ($allow_advanced_search > 1) {
8cc8ec79 1211 $search_advanced = getPref($data_dir, $username, 'search_advanced', 0);
324ac3c5 1212} else {
8cc8ec79 1213 $search_advanced = $allow_advanced_search;
324ac3c5 1214}
7d60bda5 1215if ($search_advanced) {
1216/** Set recent prefkeys according to $search_advanced
134e4174 1217 * @global array $recent_prefkeys
1218 */
8cc8ec79 1219 $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 1220
1221/** Set saved prefkeys according to $search_advanced
134e4174 1222 * @global array $saved_prefkeys
1223 */
8cc8ec79 1224 $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 1225
1226/*$asearch_prefkeys = array('where', 'mailbox', 'what', 'biop', 'unop', 'exclude', 'sub');*/
324ac3c5 1227} else {
8cc8ec79 1228 $recent_prefkeys = array('search_where', 'search_folder', 'search_what', 'search_unop');
1229 $saved_prefkeys = array('saved_where', 'saved_folder', 'saved_what', 'saved_unop');
7d60bda5 1230}
6b37252b 1231
2d2f8bb8 1232/** How we did enter the form
134e4174 1233 * - unset : Enter key, or called from outside (eg read_body)
1234 * - $search_button_text : Search button
1235 * - 'Search_no_update' : Search but don't update recent
1236 * - 'Search_last' : Same as no_update but reload and search last
1237 * - 'Search_silent' : Same as no_update but only display results
1238 * - $add_criteria_button_text : Add New Criteria button
1239 * - $del_excluded_button_text : Remove Excluded Criteria button
1240 * - $del_all_button_text : Remove All Criteria button
1241 * - 'save_recent'
1242 * - 'search_recent'
1243 * - 'forget_recent'
1244 * - 'edit_saved'
1245 * - 'search_saved'
1246 * - 'delete_saved'
1247 * @global string $submit
1248 */
e09e556f 1249$searchpressed = false;
324ac3c5 1250if (isset($_GET['submit'])) {
8cc8ec79 1251 $submit = strip_tags($_GET['submit']);
324ac3c5 1252}
e09e556f 1253
2d2f8bb8 1254/** Searched mailboxes
134e4174 1255 * @global array $mailbox_array
1256 */
6b37252b 1257if (isset($_GET['mailbox'])) {
8cc8ec79 1258 $mailbox_array = $_GET['mailbox'];
324ac3c5 1259 $targetmailbox = $_GET['mailbox'];
1260 if (!is_array($mailbox_array)) {
8cc8ec79 1261 $mailbox_array = array($mailbox_array);
324ac3c5 1262 }
1263} else {
8cc8ec79 1264 $mailbox_array = array();
d0dd72b5 1265 $targetmailbox = array();
324ac3c5 1266}
1267$aMailboxGlobalPref = array(
1268 MBX_PREF_SORT => 0,
1269 MBX_PREF_LIMIT => (int) $show_num,
1270 MBX_PREF_AUTO_EXPUNGE => (bool) $auto_expunge,
1271 MBX_PREF_INTERNALDATE => (bool) getPref($data_dir, $username, 'internal_date_sort')
1272 // MBX_PREF_FUTURE => (var) $future
1273 );
1274
1275/**
1276 * system wide admin settings and incoming vars.
1277 */
1278$aConfig = array(
f027a882 1279// 'allow_thread_sort' => $allow_thread_sort,
1280// 'allow_server_sort' => $allow_server_sort,
324ac3c5 1281 'user' => $username,
91c27aee 1282 'setindex' => 1
324ac3c5 1283 );
6b37252b 1284
2d2f8bb8 1285/** Binary operators
134e4174 1286 * @global array $biop_array
1287 */
6b37252b 1288if (isset($_GET['biop'])) {
8cc8ec79 1289 $biop_array = $_GET['biop'];
1290 if (!is_array($biop_array))
1291 $biop_array = array($biop_array);
324ac3c5 1292} else {
8cc8ec79 1293 $biop_array = array();
324ac3c5 1294}
2d2f8bb8 1295/** Unary operators
134e4174 1296 * @global array $unop_array
1297 */
6b37252b 1298if (isset($_GET['unop'])) {
8cc8ec79 1299 $unop_array = $_GET['unop'];
1300 if (!is_array($unop_array))
1301 $unop_array = array($unop_array);
324ac3c5 1302} else {
8cc8ec79 1303 $unop_array = array();
324ac3c5 1304}
2d2f8bb8 1305/** Where to search
134e4174 1306 * @global array $where_array
1307 */
6b37252b 1308if (isset($_GET['where'])) {
8cc8ec79 1309 $where_array = $_GET['where'];
324ac3c5 1310 if (!is_array($where_array)) {
8cc8ec79 1311 $where_array = array($where_array);
324ac3c5 1312 }
1313} else {
8cc8ec79 1314 $where_array = array();
324ac3c5 1315}
2d2f8bb8 1316/** What to search
134e4174 1317 * @global array $what_array
1318 */
6b37252b 1319if (isset($_GET['what'])) {
8cc8ec79 1320 $what_array = $_GET['what'];
324ac3c5 1321 if (!is_array($what_array)) {
8cc8ec79 1322 $what_array = array($what_array);
324ac3c5 1323 }
1324} else {
8cc8ec79 1325 $what_array = array();
324ac3c5 1326}
2d2f8bb8 1327/** Whether to exclude this criteria from search
134e4174 1328 * @global array $exclude_array
1329 */
324ac3c5 1330if (isset($_GET['exclude'])) {
8cc8ec79 1331 $exclude_array = $_GET['exclude'];
324ac3c5 1332} else {
8cc8ec79 1333 $exclude_array = array();
324ac3c5 1334}
2d2f8bb8 1335/** Search within subfolders
134e4174 1336 * @global array $sub_array
1337 */
324ac3c5 1338if (isset($_GET['sub'])) {
8cc8ec79 1339 $sub_array = $_GET['sub'];
324ac3c5 1340} else {
8cc8ec79 1341 $sub_array = array();
324ac3c5 1342}
2d2f8bb8 1343/** Row number used by recent and saved stuff
134e4174 1344 */
324ac3c5 1345if (isset($_GET['rownum'])) {
6b37252b 1346 $submit_rownum = strip_tags($_GET['rownum']);
324ac3c5 1347}
2d2f8bb8 1348/** Change global sort
134e4174 1349 */
324ac3c5 1350if (sqgetGlobalVar('srt', $temp, SQ_GET)) {
1351 $srt = (int) $temp;
1352 asearch_edit_last(1);
91c27aee 1353// asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
324ac3c5 1354}
1355if (sqgetGlobalVar('startMessage', $temp, SQ_GET)) {
1356 $startMessage = (int) $temp;
1357 asearch_edit_last(1);
91c27aee 1358// asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
c2d47d51 1359}
1360
324ac3c5 1361if ( sqgetGlobalVar('showall', $temp, SQ_GET) ) {
1362 $showall = (int) $temp;
1363 asearch_edit_last(1);
91c27aee 1364// asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
56e0b3b7 1365}
91c27aee 1366
1367if ( sqgetGlobalVar('account', $temp, SQ_GET) ) {
1368 $iAccount = (int) $temp;
1369} else {
1370 $iAccount = 0;
1371}
1372
1373/**
1374 * Which templatedir are we using. TODO, add make a config var of this and make it possible to switch templates
1375 */
1376$sTplDir = SM_PATH . 'templates/default/';
1377
1378
324ac3c5 1379/**
1380 * Incoming submit buttons from the message list with search results
1381 */
1382if (sqgetGlobalVar('moveButton', $moveButton, SQ_POST) ||
1383 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST) ||
1384 sqgetGlobalVar('delete', $markDelete, SQ_POST) ||
1385 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST) ||
1386 sqgetGlobalVar('markRead', $markRead, SQ_POST) ||
1387 sqgetGlobalVar('markUnread', $markUnread, SQ_POST) ||
1388 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST) ||
1389 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST) ||
1390 sqgetGlobalVar('attache', $attache, SQ_POST)) {
1391 asearch_edit_last(1);
1392 $submit = '';
1393 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1394}
1395
1396
6b37252b 1397
7d60bda5 1398/** Toggle show/hide saved searches
134e4174 1399 */
324ac3c5 1400if (sqgetGlobalVar('search_show_saved', $search_show_saved, SQ_GET)) {
8cc8ec79 1401 setPref($data_dir, $username, 'search_show_saved', $search_show_saved & 1);
324ac3c5 1402}
7d60bda5 1403/** Toggle show/hide recent searches
134e4174 1404 */
324ac3c5 1405if (sqgetGlobalVar('search_show_recent', $search_show_recent, SQ_GET)) {
8cc8ec79 1406 setPref($data_dir, $username, 'search_show_recent', $search_show_recent & 1);
324ac3c5 1407}
7d60bda5 1408// end of get globals
6b37252b 1409
40fbe929 1410/** If TRUE, do not show search interface
134e4174 1411 * Default FALSE
1412 * @global bool $search_silent
1413 */
7d60bda5 1414$search_silent = FALSE;
6b37252b 1415
1416/* See how the page was called and fire off correct function */
7d60bda5 1417if ((empty($submit)) && (!empty($where_array))) {
8cc8ec79 1418 /* This happens when the Enter key is used or called from outside */
1419 $submit = $search_button_text;
324ac3c5 1420 /* Hack needed to handle coming back from read_body et als */
1421 if (count($where_array) != count($unop_array)) {
1422 /**
1423 * Hack to use already existen where and what vars.
1424 * where now contains the initiator page of the messagelist
1425 * and in this case 'search'. what contains an index to access
1426 * the search history
1427 */
1428
1429 sqgetGlobalVar('what',$what,SQ_GET);
1430 asearch_edit_last($what);
1431 }
56e0b3b7 1432}
6b37252b 1433
1434if (!isset($submit)) {
8cc8ec79 1435 $submit = '';
324ac3c5 1436} else {
8cc8ec79 1437 switch ($submit) {
324ac3c5 1438 case $search_button_text:
1439 if (asearch_check_query($where_array, $what_array, $exclude_array) == '') {
1440 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
1441 }
8cc8ec79 1442 break;
324ac3c5 1443 case 'Search_silent':
1444 $search_silent = TRUE;
8cc8ec79 1445 /*nobreak;*/
324ac3c5 1446 case 'Search_no_update':
1447 $submit = $search_button_text;
8cc8ec79 1448 break;
324ac3c5 1449 case $del_excluded_button_text:
1450 $delarray = array_keys($exclude_array);
1451 while (!empty($delarray)) {
1452 $delrow = array_pop($delarray);
1453 array_splice($mailbox_array, $delrow, 1);
1454 array_splice($biop_array, $delrow, 1);
1455 array_splice($unop_array, $delrow, 1);
1456 array_splice($where_array, $delrow, 1);
1457 array_splice($what_array, $delrow, 1);
134e4174 1458 /* array_splice($exclude_array, $delrow, 1);*/ /* There is still some php magic that eludes me */
324ac3c5 1459 array_splice($sub_array, $delrow, 1);
1460 }
1461 $exclude_array = array();
8cc8ec79 1462 break;
324ac3c5 1463 case $del_all_button_text:
1464 $mailbox_array = array();
1465 $biop_array = array();
1466 $unop_array = array();
1467 $where_array = array();
1468 $what_array = array();
1469 $exclude_array = array();
1470 $sub_array = array();
8cc8ec79 1471 break;
324ac3c5 1472 case 'save_recent':
1473 asearch_save_recent($submit_rownum);
8cc8ec79 1474 break;
324ac3c5 1475 case 'search_recent':
1476 $submit = $search_button_text;
1477 asearch_edit_recent($submit_rownum);
1478 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
8cc8ec79 1479 break;
134e4174 1480 case 'edit_recent': /* no link to do this, yet */
324ac3c5 1481 asearch_edit_recent($submit_rownum);
8cc8ec79 1482 break;
324ac3c5 1483 case 'forget_recent':
1484 asearch_forget_recent($submit_rownum);
8cc8ec79 1485 break;
324ac3c5 1486 case 'search_saved':
1487 $submit = $search_button_text;
1488 asearch_edit_saved($submit_rownum);
1489 asearch_push_recent($mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
8cc8ec79 1490 break;
324ac3c5 1491 case 'edit_saved':
1492 asearch_edit_saved($submit_rownum);
8cc8ec79 1493 break;
324ac3c5 1494 case 'delete_saved':
1495 asearch_delete_saved($submit_rownum);
8cc8ec79 1496 break;
1497 }
56e0b3b7 1498}
23a9084b 1499
7d60bda5 1500//Texts in both basic and advanced form
1501$imap_asearch_unops = array(
8cc8ec79 1502 '' => '',
1503 'NOT' => _("Not")
7d60bda5 1504);
1505
1506if ($search_advanced) {
8cc8ec79 1507 //Texts in advanced form only
1508 $imap_asearch_options = array(
1509 //<message set>,
1510 //'ALL' is binary operator
1511 'ANSWERED' => _("Answered"),
1512 'BCC' => _("Bcc"),
1513 'BEFORE' => _("Before"),
1514 'BODY' => _("Message Body"),
48027e89 1515 'CC' => _("Cc"),
8cc8ec79 1516 'DELETED' => _("Deleted"),
1517 'DRAFT' => _("Draft"),
1518 'FLAGGED' => _("Flagged"),
1519 'FROM' => _("Sent By"),
1520 'HEADER' => _("Header Field"),
1521 'KEYWORD' => _("Keyword"),
1522 'LARGER' => _("Larger Than"),
1523 'NEW' => _("New"),
1524 //'NOT' is unary operator
1525 'OLD' => _("Old"),
1526 'ON' => _("On"),
1527 //'OR' is binary operator
1528 'RECENT' => _("Recent"),
1529 'SEEN' => _("Seen"),
1530 'SENTBEFORE' => _("Sent Before"),
1531 'SENTON' => _("Sent On"),
1532 'SENTSINCE' => _("Sent Since"),
1533 'SINCE' => _("Since"),
1534 'SMALLER' => _("Smaller Than"),
1535 'SUBJECT' => _("Subject Contains"),
1536 'TEXT' => _("Header and Body"),
1537 'TO' => _("Sent To"),
1538 //'UID' => 'anum',
134e4174 1539/* 'UNANSWERED' => '',
8cc8ec79 1540 'UNDELETED' => '',
1541 'UNDRAFT' => '',
1542 'UNFLAGGED' => '',
1543 'UNKEYWORD' => _("Unkeyword"),
1544 'UNSEEN' => _("Unseen"),*/
1545 );
1546
1547 $imap_asearch_biops_in = array(
1548 'ALL' => _("And In"),
1549 'OR' => _("Or In")
1550 );
1551
1552 $imap_asearch_biops = array(
1553 'ALL' => _("And"),
1554 'OR' => _("Or")
1555 );
324ac3c5 1556} else {
8cc8ec79 1557 //Texts in basic form only
1558 $imap_asearch_options = array(
1559 'BCC' => _("Bcc"),
1560 'BODY' => _("Body"),
1561 'CC' => _("Cc"),
1562 'FROM' => _("From"),
1563 'SUBJECT' => _("Subject"),
1564 'TEXT' => _("Everywhere"),
1565 'TO' => _("To"),
1566 );
7d60bda5 1567}
1568
1569uasort($imap_asearch_options, 'asearch_unhtml_strcoll');
1570
6b37252b 1571/* open IMAP connection */
906f7e9f 1572$imapConnection = sqimap_login($username, false, $imapServerAddress, $imapPort, 0);
324ac3c5 1573
1574
5b6abe97 1575/* get mailboxes once here */
6b37252b 1576$boxes = sqimap_mailbox_list($imapConnection);
99f3175e 1577/* ensure we have a valid default mailbox name */
edda50ae 1578$mailbox = asearch_nz($mailbox_array[0]);
134e4174 1579if (($mailbox == '') || ($mailbox == 'None')) //Workaround for sm quirk IMHO (what if I really have a mailbox called None?)
1580 $mailbox = $boxes[0]['unformatted']; //Usually INBOX ;)
99f3175e 1581
91c27aee 1582
1583/**
1584* Handle form actions like flag / unflag, seen / unseen, delete
1585*/
1586if (sqgetGlobalVar('mailbox',$postMailbox,SQ_POST)) {
1587 if ($postMailbox) {
1588 /**
1589 * system wide admin settings and incoming vars.
1590 */
1591 $aConfig = array(
1592 'user' => $username,
1593 );
1594 $aConfig['setindex'] = 1; // $what $where = 'search'
1595 /**
1596 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1597 * when searching all mailboxes
1598 */
1599 $aConfig['max_cache_size'] = count($boxes) +1;
1600
1601 $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $postMailbox,$aConfig,array());
1602 $sError = handleMessageListForm($imapConnection,$aMailbox);
1603 /* add the mailbox to the cache */
1604 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1605
1606 if ($sError) {
1607 $note = $sError;
1608 }
1609 }
1610}
1611
1612if (isset($aMailbox['FORWARD_SESSION'])) {
1613 /* add the mailbox to the cache */
1614 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
1615 sqsession_register($mailbox_cache,'mailbox_cache');
1616
1617 if ($compose_new_win) {
1618 // write the session in order to make sure that the compose window has
1619 // access to the composemessages array which is stored in the session
1620 session_write_close();
8517e764 1621 // restart the session. Do not use sqsession_is_active because the session_id
1622 // isn't empty after a session_write_close
3a1de9f1 1623 sqsession_start();
91c27aee 1624
1625 if (!preg_match("/^[0-9]{3,4}$/", $compose_width)) {
1626 $compose_width = '640';
1627 }
1628 if (!preg_match("/^[0-9]{3,4}$/", $compose_height)) {
1629 $compose_height = '550';
1630 }
1631 // do not use &amp;, it will break the query string and $session will not be detected!!!
1632 $comp_uri = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
1633 '&session='.$aMailbox['FORWARD_SESSION'];
1634 displayPageHeader($color, $mailbox, "comp_in_new('$comp_uri', $compose_width, $compose_height);", false);
1635 } else {
1636 // save mailboxstate
1637 sqsession_register($aMailbox,'aLastSelectedMailbox');
1638 session_write_close();
1639 // we have to redirect to the compose page
1640 $location = SM_PATH . 'src/compose.php?mailbox='. urlencode($mailbox).
1641 '&amp;session='.$aMailbox['FORWARD_SESSION'];
1642 header("Location: $location");
1643 exit;
1644 }
324ac3c5 1645} else {
8cc8ec79 1646 displayPageHeader($color, $mailbox);
91c27aee 1647// $compose_uri = $base_uri.'src/compose.php?newmessage=1';
1648}
1649
1650if (isset($note)) {
a6d3eff6 1651 echo html_tag( 'div', '<b>' . htmlspecialchars($note) .'</b>', 'center' ) . "<br />\n";
324ac3c5 1652}
91c27aee 1653
1654
6b37252b 1655do_hook('search_before_form');
d81e351b 1656
6b37252b 1657if (!$search_silent) {
8cc8ec79 1658 //Add a link to the other search mode if allowed
1659 if ($allow_advanced_search > 1)
1660 $toggle_link = ' - <small>['
1661 . asearch_get_toggle_link(!$search_advanced, 'advanced', array(_("Standard search"), _("Advanced search")))
1662 . ']</small>';
1663 else
1664 $toggle_link = '';
1665
1666 echo html_tag('table',
1667 html_tag('tr', "\n"
1668 . html_tag('td', asearch_get_title_display($color, _("Search")) . $toggle_link, 'center', $color[0])
1669 ) ,
1670 '', '', 'width="100%"') . "\n";
1671 asearch_print_saved($boxes);
1672 asearch_print_recent($boxes);
1673 if (empty($where_array)) {
1674 global $sent_folder;
1675
1676 $mailbox_array[0] = $mailbox;
1677 $biop_array[0] = '';
1678 $unop_array[0] = '';
324ac3c5 1679 if ($mailbox == $sent_folder) {
8cc8ec79 1680 $where_array[0] = 'TO';
324ac3c5 1681 } else {
8cc8ec79 1682 $where_array[0] = 'FROM';
324ac3c5 1683 }
8cc8ec79 1684 $what_array[0] = '';
1685 $exclude_array[0] = '';
1686 $sub_array[0] = '';
1687 }
1688 //Display advanced or basic form
1689 if ($search_advanced) {
1690 if ($submit == $add_criteria_button_text) {
1691 $last_index = max(count($where_array) - 1, 0);
1692 $mailbox_array[] = asearch_nz($mailbox_array[$last_index]);
1693 $biop_array[] = asearch_nz($biop_array[$last_index]);
1694 $unop_array[] = asearch_nz($unop_array[$last_index]);
1695 $where_array[] = asearch_nz($where_array[$last_index]);
1696 $what_array[] = asearch_nz($what_array[$last_index]);
1697 $exclude_array[] = asearch_nz($exclude_array[$last_index]);
1698 $sub_array[] = asearch_nz($sub_array[$last_index]);
1699 }
1700 asearch_print_form($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
324ac3c5 1701 } else {
8cc8ec79 1702 asearch_print_form_basic($imapConnection, $boxes, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array);
324ac3c5 1703 }
6b37252b 1704}
1705
88cb1b4d 1706do_hook('search_after_form');
56e0b3b7 1707
6b37252b 1708if ($submit == $search_button_text) {
a73ae1c2 1709 $msgsfound = false;
8cc8ec79 1710 echo html_tag('table', '', 'center', $color[9], 'width="100%" cellpadding="1" cellspacing="0" border="0"');
1711 echo html_tag('tr', html_tag('td', asearch_get_title_display($color, _("Search Results")), 'center', $color[5]));
1712 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]));
1713 echo '</table>' . "\n";
1714
1715 flush();
e09e556f 1716 $iMsgCnt = 0;
8cc8ec79 1717 $query_error = asearch_check_query($where_array, $what_array, $exclude_array);
324ac3c5 1718 if ($query_error != '') {
8cc8ec79 1719 echo '<br />' . html_tag('div', asearch_get_error_display($color, $query_error), 'center') . "\n";
324ac3c5 1720 } else {
8cc8ec79 1721 $mboxes_array = sqimap_asearch_get_selectable_unformatted_mailboxes($boxes);
324ac3c5 1722 /**
1723 * Retrieve the search queries
1724 */
1725 $mboxes_mailbox = sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $sub_array, $mboxes_array);
1726 foreach($mboxes_mailbox as $mbx => $search) {
324ac3c5 1727
91c27aee 1728 /**
1729 * until there is no per mailbox option screen to set prefs we override
1730 * the mailboxprefs by the default ones
1731 */
1732
1733 $aMailboxPrefSer=getPref($data_dir, $username,'pref_'.$iAccount.'_'.$mbx);
324ac3c5 1734 if ($aMailboxPrefSer) {
1735 $aMailboxPref = unserialize($aMailboxPrefSer);
91c27aee 1736 $aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
324ac3c5 1737 } else {
91c27aee 1738 setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($default_mailbox_pref));
1739 $aMailboxPref = $default_mailbox_pref;
8cc8ec79 1740 }
324ac3c5 1741 if (isset($srt) && $targetmailbox == $mbx) {
1742 $aMailboxPref[MBX_PREF_SORT] = (int) $srt;
1743 }
91c27aee 1744
1745 $trash_folder = (isset($trash_folder)) ? $trash_folder : false;
1746 $sent_folder = (isset($sent_folder)) ? $sent_folder : false;
1747 $draft_folder = (isset($draft_folder)) ? $draft_folder : false;
1748
324ac3c5 1749
1750 /**
1751 * until there is no per mailbox option screen to set prefs we override
1752 * the mailboxprefs by the default ones
1753 */
1754 $aMailboxPref[MBX_PREF_LIMIT] = (int) $show_num;
1755 $aMailboxPref[MBX_PREF_AUTO_EXPUNGE] = (bool) $auto_expunge;
1756 $aMailboxPref[MBX_PREF_INTERNALDATE] = (bool) getPref($data_dir, $username, 'internal_date_sort');
91c27aee 1757 $aMailboxPref[MBX_PREF_COLUMNS] = $index_order;
1758
1759 /**
1760 * Replace From => To in case it concerns a draft or sent folder
1761 */
1762 if (($mbx == $sent_folder || $mbx == $draft_folder) &&
1763 !in_array(SQM_COL_TO,$aMailboxPref[MBX_PREF_COLUMNS])) {
1764 $aNewOrder = array(); // nice var name ;)
1765 foreach($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
1766 if ($iCol == SQM_COL_FROM) {
1767 $iCol = SQM_COL_TO;
1768 }
1769 $aNewOrder[] = $iCol;
1770 }
1771 $aMailboxPref[MBX_PREF_COLUMNS] = $aNewOrder;
f16dc830 1772 setUserPref($username,'pref_'.$iAccount.'_'.$mbx,serialize($aMailboxPref));
91c27aee 1773 }
324ac3c5 1774
324ac3c5 1775 $aConfig['search'] = $search['search'];
1776 $aConfig['charset'] = $search['charset'];
324ac3c5 1777
324ac3c5 1778 /**
91c27aee 1779 * Set the max cache size to the number of mailboxes to avoid cache cleanups
1780 * when searching all mailboxes
1781 */
1782 $aConfig['max_cache_size'] = count($mboxes_mailbox) +1;
1783 if (isset($startMessage) && $targetmailbox == $mbx) {
1784 $aConfig['offset'] = $startMessage;
e09e556f 1785 } else {
1786 $aConfig['offset'] = 0;
91c27aee 1787 }
1788 if (isset($showall) && $targetmailbox == $mbx) {
1789 $aConfig['showall'] = $showall;
1790 } else {
1791 if (isset($aConfig['showall'])) {
1792 unset($aConfig['showall']);
1793 }
1794 $showall = false;
1795 }
1796
1797 /**
1798 * Set the config options for the messages list
324ac3c5 1799 */
91c27aee 1800 $aColumns = array();
1801 foreach ($aMailboxPref[MBX_PREF_COLUMNS] as $iCol) {
1802 $aColumns[$iCol] = array();
1803 switch ($iCol) {
1804 case SQM_COL_SUBJ:
1805 if ($truncate_subject) {
1806 $aColumns[$iCol]['truncate'] = $truncate_subject;
1807 }
1808 break;
1809 case SQM_COL_FROM:
1810 case SQM_COL_TO:
1811 case SQM_COL_CC:
1812 case SQM_COL_BCC:
1813 if ($truncate_sender) {
1814 $aColumns[$iCol]['truncate'] = $truncate_sender;
1815 }
1816 break;
324ac3c5 1817 }
1818 }
91c27aee 1819
1820
1821 $aProps = array(
1822 'columns' => $aColumns,
1823 'config' => array('alt_index_colors' => $alt_index_colors,
1824 'highlight_list' => $message_highlight_list,
1825 'fancy_index_highlite' => $fancy_index_highlite,
1826 'show_flag_buttons' => (isset($show_flag_buttons)) ? $show_flag_buttons : true,
1827 'lastTargetMailbox' => (isset($lastTargetMailbox)) ? $lastTargetMailbox : '',
1828 'trash_folder' => $trash_folder,
1829 'sent_folder' => $sent_folder,
1830 'draft_folder' => $draft_folder,
1831 'enablesort' => true,
1832 'color' => $color
1833 ),
1834 'mailbox' => $mbx,
1835 'account' => (isset($iAccount)) ? $iAccount : 0,
1836 'module' => 'read_body',
1837 'email' => false);
1838
1839
1840 $aMailbox = sqm_api_mailbox_select($imapConnection, $iAccount, $mbx,$aConfig,$aMailboxPref);
1841
1842 $iError = 0;
1843 $aTemplate = showMessagesForMailbox($imapConnection, $aMailbox,$aProps, $iError);
e09e556f 1844
1845 // in th future we can make use of multiple message sets, now set it to 1 for search.
1846 $iSetIndex = 1;
1847 if (isset($aMailbox['UIDSET'][$iSetIndex])) {
1848 $iMsgCnt += count($aMailbox['UIDSET'][$iSetIndex]);
1849 }
91c27aee 1850 if ($iError) {
1851 // error handling
1852 } else {
1853 /**
1854 * In the future, move this the the initialisation area
1855 */
1856 sqgetGlobalVar('align',$align,SQ_SESSION);
1857
1858 $oTemplate = new Template($sTplDir);
1859
1860 if ($aMailbox['EXISTS'] > 0) {
91c27aee 1861 if ($iError) {
e09e556f 1862 // TODO
1863 echo "ERROR occured, errorhandler will be implemented very soon";
91c27aee 1864 } else {
e09e556f 1865 foreach ($aTemplate as $k => $v) {
1866 $oTemplate->assign($k, $v);
1867 }
29997535 1868
e09e556f 1869 $oTemplate->assign('page_selector', $page_selector);
1870 $oTemplate->assign('page_selector_max', $page_selector_max);
1871 $oTemplate->assign('compact_paginator', $compact_paginator);
1872 $oTemplate->assign('javascript_on', $javascript_on);
1873 $oTemplate->assign('enablesort', (isset($aProps['config']['enablesort'])) ? $aProps['config']['enablesort'] : false);
29997535 1874 $oTemplate->assign('icon_theme_path', $icon_theme_path);
e09e556f 1875 $oTemplate->assign('use_icons', (isset($use_icons)) ? $use_icons : false);
1876 $oTemplate->assign('aOrder', array_keys($aColumns));
1877 $oTemplate->assign('alt_index_colors', isset($alt_index_colors) ? $alt_index_colors: false);
1878 $oTemplate->assign('color', $color);
1879 $oTemplate->assign('align', $align);
e09e556f 1880
1881 $mailbox_display = asearch_get_mailbox_display($aMailbox['NAME']);
1882 if (strtoupper($mbx) == 'INBOX') {
1883 $mailbox_display = _("INBOX");
1884 } else {
1885 $mailbox_display = imap_utf7_decode_local($mbx);
1886 }
91c27aee 1887
fa62b054 1888 echo '<br /><b><big>' . _("Folder:") . ' '. htmlspecialchars($mailbox_display) . '&nbsp;</big></b>';
91c27aee 1889
e09e556f 1890 $oTemplate->display('message_list.tpl');
1891 }
91c27aee 1892 }
a73ae1c2 1893 }
91c27aee 1894
324ac3c5 1895 /* add the mailbox to the cache */
91c27aee 1896 $mailbox_cache[$iAccount.'_'.$aMailbox['NAME']] = $aMailbox;
8cc8ec79 1897
324ac3c5 1898 }
8cc8ec79 1899 }
e09e556f 1900 if(!$iMsgCnt) {
a73ae1c2 1901 echo '<br />' . html_tag('div', asearch_get_error_display($color, _("No Messages Found")), 'center') . "\n";
1902 }
56e0b3b7 1903}
1904
70c4fd84 1905do_hook('search_bottom');
6b37252b 1906sqimap_logout($imapConnection);
5c4ff7bf 1907$oTemplate->display('footer.tpl');
324ac3c5 1908sqsession_register($mailbox_cache,'mailbox_cache');
a2b193bc 1909
fa62b054 1910?>