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