ChangeLog
[squirrelmail.git] / functions / imap_asearch.php
CommitLineData
cd33ec11 1<?php
2
3/**
4 * imap_search.php
5 *
6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP asearch routines
10 * Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
11 * See README file for infos.
12 *
13 */
14
15require_once(SM_PATH . 'functions/imap.php');
16require_once(SM_PATH . 'functions/date.php');
17
18/* Set to TRUE to dump the imap dialogue */
19$imap_asearch_debug_dump = FALSE;
20
21$imap_asearch_opcodes = array(
22/* <message set> => 'asequence', */
23/*'ALL' is binary operator */
24 'ANSWERED' => '',
25 'BCC' => 'astring',
26 'BEFORE' => 'adate',
27 'BODY' => 'astring',
28 'CC' => 'astring',
29 'DELETED' => '',
30 'DRAFT' => '',
31 'FLAGGED' => '',
32 'FROM' => 'astring',
33 'HEADER' => 'afield', /* Special syntax for this one, see below */
34 'KEYWORD' => 'akeyword',
35 'LARGER' => 'anum',
36 'NEW' => '',
37/*'NOT' is unary operator */
38 'OLD' => '',
39 'ON' => 'adate',
40/*'OR' is binary operator */
41 'RECENT' => '',
42 'SEEN' => '',
43 'SENTBEFORE' => 'adate',
44 'SENTON' => 'adate',
45 'SENTSINCE' => 'adate',
46 'SINCE' => 'adate',
47 'SMALLER' => 'anum',
48 'SUBJECT' => 'astring',
49 'TEXT' => 'astring',
50 'TO' => 'astring',
51 'UID' => 'asequence',
52 'UNANSWERED' => '',
53 'UNDELETED' => '',
54 'UNDRAFT' => '',
55 'UNFLAGGED' => '',
56 'UNKEYWORD' => 'akeyword',
57 'UNSEEN' => ''
58);
59
60$imap_asearch_months = array(
61 '01' => 'jan',
62 '02' => 'feb',
63 '03' => 'mar',
64 '04' => 'apr',
65 '05' => 'may',
66 '06' => 'jun',
67 '07' => 'jul',
68 '08' => 'aug',
69 '09' => 'sep',
70 '10' => 'oct',
71 '11' => 'nov',
72 '12' => 'dec'
73);
74
75/* This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! */
76function asearch_nz(&$var)
77{
78 if (isset($var))
79 return $var;
80 return '';
81}
82
83/* This should give the same results as PHP 4 >= 4.3.0's html_entity_decode() */
84function asearch_unhtmlentities($string) {
85 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
86 for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
87 $trans_tbl['&#' . $i . ';'] = chr($i);
88 return strtr($string, $trans_tbl);
89/* I think the one above is quicker, though it should be benchmarked
90 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
91 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
92*/
93}
94
95function s_dump($var_name, $var_var, $compact = FALSE)
96{
97 if (!$compact)
98 echo '<PRE>';
99 echo htmlentities($var_name) . '=';
100 print_r($var_var);
101 if ($compact)
102 echo "<BR>\n";
103 else
104 echo "</PRE>\n";
105}
106
107function s_debug_dump($var_name, $var_var, $compact = FALSE)
108{
109 global $imap_asearch_debug_dump;
110 if ($imap_asearch_debug_dump)
111 s_dump($var_name, $var_var, $compact);
112}
113
375b552d 114/*
1154.3 String:
116 A quoted string is a sequence of zero or more 7-bit characters,
117 excluding CR and LF, with double quote (<">) characters at each end.
1189. Formal Syntax:
119 quoted-specials = DQUOTE / "\"
120*/
cd33ec11 121function sqimap_asearch_encode_string($what, $search_charset)
122{
123 if (strtoupper($search_charset) == 'ISO-2022-JP')
124 $what = mb_convert_encoding($what, 'JIS', 'auto');
375b552d 125 if (ereg("[\"\\\r\n]", $what))
cd33ec11 126 return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */
127 return '"' . $what . '"'; /* 4.3 quoted string form */
128}
129
130/*
131 Parses a user date string into an rfc2060 date string (<day number>-<US month TLA>-<4 digit year>)
132 Returns a preg_match-style array: [0]: fully formatted date, [1]: day, [2]: month, [3]: year
375b552d 133 Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
cd33ec11 134*/
135function sqimap_asearch_parse_date($what)
136{
137 global $imap_asearch_months;
138
139 $what = trim($what);
375b552d 140 $what = ereg_replace('[ /\\.,]+', '-', $what);
cd33ec11 141 if ($what) {
375b552d 142 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
cd33ec11 143 if (count($what_parts) == 4) {
144 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
145/* if (!in_array($what_month, $imap_asearch_months)) {*/
146 foreach ($imap_asearch_months as $month_number => $month_code) {
147 if (($what_month == $month_number)
148 || ($what_month == $month_code)
149 || ($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
150 || ($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
151 ) {
152 $what_parts[2] = $month_number;
153 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
154 break;
155 }
156 }
157/* }*/
158 }
159 }
160 else
161 $what_parts = array();
162 return $what_parts;
163}
164
165function sqimap_asearch_build_criteria($opcode, $what, $search_charset)
166{
167 global $imap_asearch_opcodes;
168
375b552d 169 $criteria = '';
cd33ec11 170 switch ($imap_asearch_opcodes[$opcode]) {
171 default:
172 case 'anum':
173/* $what = str_replace(' ', '', $what);*/
174 $what = ereg_replace('[^0-9]+', '', $what);
175 if ($what != '')
176 $criteria = $opcode . ' ' . $what . ' ';
177 break;
178 case '': /* aflag */
179 $criteria = $opcode . ' ';
180 break;
181 case 'afield': /* HEADER field-name: field-body */
182 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
183 if (count($what_parts) == 3)
184 $criteria = $opcode . ' ' .
185 sqimap_asearch_encode_string($what_parts[1], $search_charset) . ' ' .
186 sqimap_asearch_encode_string($what_parts[2], $search_charset) . ' ';
187 break;
188 case 'adate':
189 $what_parts = sqimap_asearch_parse_date($what);
375b552d 190 if (isset($what_parts[0]))
cd33ec11 191 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
192 break;
193 case 'akeyword':
194 case 'astring':
195 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $search_charset) . ' ';
196 break;
197 case 'asequence':
198 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
199 if ($what != '')
200 $criteria = $opcode . ' ' . $what . ' ';
201 break;
202 }
203 return $criteria;
204}
205
206function sqimap_run_search($imapConnection, $search_string, $search_charset)
207{
208 global $allow_charset_search, $uid_support;
209
210 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
211 if ($allow_charset_search && (!empty($search_charset)))
212 $ss = 'SEARCH CHARSET ' . strtoupper($search_charset) . ' ALL ' . $search_string;
213 else
214 $ss = 'SEARCH ALL ' . $search_string;
215 s_debug_dump('O', $ss);
216
217 /* read data back from IMAP */
218 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
219
220 /* 6.4.4 try US-ASCII charset if we receive a tagged NO response */
221 if (!empty($charset) && strtolower($result) == 'no') {
222 $ss = 'SEARCH CHARSET "US-ASCII" ALL ' . $search_string;
223 s_debug_dump('O', $ss);
224 $readin = sqimap_run_command ($imapConnection, $ss, true, $result, $message); /* no $uid_support? */
225 }
226
227 unset($messagelist);
228
229 /* Keep going till we find the SEARCH response */
230 foreach ($readin as $readin_part) {
231 s_debug_dump('I', $readin_part);
232 /* Check to see if a SEARCH response was received */
233 if (substr($readin_part, 0, 9) == '* SEARCH ') {
234 $messagelist = preg_split("/ /", substr($readin_part, 9));
235 } else if (isset($errors)) {
236 $errors = $errors . $readin_part;
237 } else {
238 $errors = $readin_part;
239 }
240 }
241
242 /* If nothing is found * SEARCH should be the first error else echo errors */
243 if (isset($errors)) {
244 if (strstr($errors,'* SEARCH'))
245 return array();
246 echo '<!-- ' . htmlspecialchars($errors) . ' -->';
247 }
248
3f075f6c 249 if (empty($messagelist))
250 return array();
251
cd33ec11 252 $cnt = count($messagelist);
253 for ($q = 0; $q < $cnt; $q++)
254 $id[$q] = trim($messagelist[$q]);
255 return $id;
256}
257
258/* replaces $mbox_msgs[$search_mailbox] = array_values(array_unique(array_merge($mbox_msgs[$search_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset))));*/
259function sqimap_array_merge_unique($to, $from)
260{
261 if (empty($to))
262 return $from;
263 for ($i=0; $i<count($from); $i++) {
264 if (!in_array($from[$i], $to))
265 $to[] = $from[$i];
266 }
267 return $to;
268}
269
270function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
271{
272 global $languages, $squirrelmail_language;
273
274/* ??? what are those for ?? */
275/* $pos = $search_position;*/
276
277 $mbox_msgs = array();
278 $search_charset = $languages[$squirrelmail_language]['CHARSET'];
279 $search_string = '';
280 $cur_mailbox = $mailbox_array[0];
281 $cur_biop = ''; /* Start with ALL */
282 /* We loop one more time than the real array count, so the last search gets fired */
283 for ($cur_crit = 0; $cur_crit <= count($where_array); $cur_crit++) {
284 if (empty($exclude_array[$cur_crit])) {
285 $next_mailbox = $mailbox_array[$cur_crit];
286 if ($next_mailbox != $cur_mailbox) {
287 $search_string = trim($search_string); /* Trim out last space */
288 if (($cur_mailbox == 'All Folders') && (!empty($mboxes_array)))
289 $search_mboxes = $mboxes_array;
290 else
291 $search_mboxes = array($cur_mailbox);
292 foreach ($search_mboxes as $cur_mailbox) {
293 sqimap_mailbox_select($imapConnection, $cur_mailbox);
294 s_debug_dump('SELECT',$cur_mailbox);
5ab3c3fe 295 if (isset($mbox_msgs[$cur_mailbox])) {
296 if ($cur_biop == 'OR') /* Merge with previous results */
297 $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], sqimap_run_search($imapConnection, $search_string, $search_charset));
298 else /* Intersect previous results */
cd33ec11 299 $mbox_msgs[$cur_mailbox] = array_values(array_intersect(sqimap_run_search($imapConnection, $search_string, $search_charset), $mbox_msgs[$cur_mailbox]));
300 }
5ab3c3fe 301 else /* No previous results */
302 $mbox_msgs[$cur_mailbox] = sqimap_run_search($imapConnection, $search_string, $search_charset);
cd33ec11 303 if (empty($mbox_msgs[$cur_mailbox])) /* Can happen with intersect, and we need at the end a contiguous array */
304 unset($mbox_msgs[$cur_mailbox]);
305 }
306 $cur_mailbox = $next_mailbox;
307 $search_string = '';
308 }
309 if (isset($where_array[$cur_crit])) {
310 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
311 if (!empty($criteria)) {
312 $unop = $unop_array[$cur_crit];
313 if (!empty($unop))
314 $criteria = $unop . ' ' . $criteria;
315 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
316 $next_biop = '';
317 for ($next_crit = $cur_crit+1; $next_crit <= count($where_array); $next_crit++) {
318 if (empty($exclude_array[$next_crit])) {
319 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox)
5ab3c3fe 320 $next_biop = asearch_nz($biop_array[$next_crit]);
cd33ec11 321 break;
322 }
323 }
324 if ($next_biop == 'OR')
325 $criteria = $next_biop . ' ' . $criteria;
326 $search_string .= $criteria;
327 $cur_biop = asearch_nz($biop_array[$cur_crit]);
328 }
329 }
330 }
331 }
332 return $mbox_msgs;
333}
334
335?>