Minor cleanups
[squirrelmail.git] / functions / imap_search.php
CommitLineData
6e189ce2 1<?php
2ba13803 2
35586184 3/**
15e6162e 4 * imap_search.php
35586184 5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
15e6162e 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
35586184 8 *
15e6162e 9 * IMAP search routines
35586184 10 *
eb19bc67 11 * @version $Id$
d6c32258 12 * @package squirrelmail
eb19bc67 13 * @subpackage imap
d6c32258 14 * @deprecated This search interface has been largely replaced by asearch
35586184 15 */
16
d6c32258 17/**
18 * Load up a bunch of SM functions */
b68edc75 19require_once(SM_PATH . 'functions/imap.php');
20require_once(SM_PATH . 'functions/date.php');
b68edc75 21require_once(SM_PATH . 'functions/mailbox_display.php');
22require_once(SM_PATH . 'functions/mime.php');
2ba13803 23
b1e29794 24function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
25 $color, $search_position = '', $search_all, $count_all) {
2ba13803 26
dba269b1 27 global $message_highlight_list, $squirrelmail_language, $languages,
62f7daa5 28 $index_order, $pos, $allow_charset_search,
2b3d9db0 29 $imap_server_type;
dba269b1 30
53828ec5 31 $pos = $search_position;
2ba13803 32
29eb5486 33 $urlMailbox = urlencode($mailbox);
2ba13803 34
b1e29794 35 /* construct the search query, taking multiple search terms into account */
36 $multi_search = array();
37 $search_what = trim($search_what);
38 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
39 $multi_search = explode(' ', $search_what);
40 $search_string = '';
2b3d9db0 41
62f7daa5 42 /* it seems macosx does not support the prefered search
2b3d9db0 43 syntax so we fall back to the older style. This IMAP
44 server has a problem with multiple search terms. Instead
45 of returning the messages that match all the terms it
46 returns the messages that match each term. Could be fixed
47 on the client side, but should be fixed on the server
48 as per the RFC */
49
50 if ($imap_server_type == 'macosx') {
51 foreach ($multi_search as $multi_search_part) {
83be314a 52 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
53 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
54 }
2b3d9db0 55 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
56 }
57 }
58 else {
59 foreach ($multi_search as $multi_search_part) {
83be314a 60 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
61 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
62 }
2b3d9db0 63 $search_string .= $search_where . ' {' . strlen($multi_search_part)
64 . "}\r\n" . $multi_search_part . ' ';
65 }
5c9a316d 66 }
dba269b1 67
70c4fd84 68 $search_string = trim($search_string);
c92ef720 69
b1e29794 70 /* now use $search_string in the imap search */
65ffb3ce 71 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
29eb5486 72 $languages[$squirrelmail_language]['CHARSET']) {
8ceffac2 73 $ss = "SEARCH CHARSET "
62f7daa5 74 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
8ceffac2 75 . " ALL $search_string";
29eb5486 76 } else {
b1e29794 77 $ss = "SEARCH ALL $search_string";
29eb5486 78 }
b1e29794 79
80 /* read data back from IMAP */
6201339c 81 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE);
b1e29794 82
83 /* try US-ASCII charset if search fails */
62f7daa5 84 if (isset($languages[$squirrelmail_language]['CHARSET'])
8ceffac2 85 && strtolower($result) == 'no') {
fee5ad96 86 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
62f7daa5 87 $readin = sqimap_run_command ($imapConnection, $ss, true,
8ceffac2 88 $result, $message);
29eb5486 89 }
2ba13803 90
70c4fd84 91 unset($messagelist);
b1e29794 92
93 /* Keep going till we find the SEARCH response */
94 foreach ($readin as $readin_part) {
95 /* Check to see if a SEARCH response was received */
96 if (substr($readin_part, 0, 9) == '* SEARCH ') {
dba269b1 97 $messagelist = preg_split("/ /", substr($readin_part, 9));
b1e29794 98 } else if (isset($errors)) {
99 $errors = $errors.$readin_part;
100 } else {
101 $errors = $readin_part;
102 }
29eb5486 103 }
2ba13803 104
29eb5486 105 /* If nothing is found * SEARCH should be the first error else echo errors */
b1e29794 106 if (isset($errors)) {
107 if (strstr($errors,'* SEARCH')) {
5fac2758 108 return array();
70c4fd84 109 }
9b761dbd 110 echo '<!-- '.htmlspecialchars($errors) .' -->';
29eb5486 111 }
2ba13803 112
113
29eb5486 114 global $sent_folder;
dba269b1 115
116 $cnt = count($messagelist);
117 for ($q = 0; $q < $cnt; $q++) {
29eb5486 118 $id[$q] = trim($messagelist[$q]);
119 }
120 $issent = ($mailbox == $sent_folder);
f6b58812 121
20473d1e 122 $msgs = fillMessageArray($imapConnection,$id,$cnt);
2ba13803 123
dba269b1 124 return $msgs;
125}
2ba13803 126
2ba13803 127
fc05541b 128
62f7daa5 129?>