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