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