accidently removed macosx search syntax. Thnx Jasper Kalkman.
[squirrelmail.git] / functions / imap_search.php
1 <?php
2
3 /**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP search routines
10 *
11 * $Id$
12 */
13
14 require_once('../functions/imap.php');
15 require_once('../functions/date.php');
16 require_once('../functions/array.php');
17 require_once('../functions/mailbox_display.php');
18 require_once('../functions/mime.php');
19
20 function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
21 $color, $search_position = '', $search_all, $count_all) {
22
23 global $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos, $allow_charset_search, $uid_support,
25 $imap_server_type;
26
27 $pos = $search_position;
28
29 $urlMailbox = urlencode($mailbox);
30
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 = '';
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 }
56 }
57
58 $search_string = trim($search_string);
59
60 /* now use $search_string in the imap search */
61 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
62 $languages[$squirrelmail_language]['CHARSET']) {
63 $ss = "SEARCH CHARSET "
64 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
65 . " ALL $search_string";
66 } else {
67 $ss = "SEARCH ALL $search_string";
68 }
69
70 /* read data back from IMAP */
71 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
72
73 /* try US-ASCII charset if search fails */
74 if (isset($languages[$squirrelmail_language]['CHARSET'])
75 && strtolower($result) == 'no') {
76 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
77 $readin = sqimap_run_command ($imapConnection, $ss, true,
78 $result, $message);
79 }
80
81 unset($messagelist);
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 ') {
87 $messagelist = preg_split("/ /", substr($readin_part, 9));
88 } else if (isset($errors)) {
89 $errors = $errors.$readin_part;
90 } else {
91 $errors = $readin_part;
92 }
93 }
94
95 /* If nothing is found * SEARCH should be the first error else echo errors */
96 if (isset($errors)) {
97 if (strstr($errors,'* SEARCH')) {
98 return array();
99 }
100 echo "<!-- $errors -->";
101 }
102
103
104 global $sent_folder;
105
106 $cnt = count($messagelist);
107 for ($q = 0; $q < $cnt; $q++) {
108 $id[$q] = trim($messagelist[$q]);
109 }
110 $issent = ($mailbox == $sent_folder);
111
112 $msgs = fillMessageArray($imapConnection,$id,$cnt);
113
114 return $msgs;
115 }
116
117
118
119 ?>