6c0cc5a3119ff87afeedace0565582026af6d65a
[squirrelmail.git] / functions / imap_asearch.php
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 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
11 * See README file for infos.
12 * @package squirrelmail
13 *
14 */
15
16 /** This functionality requires the IMAP and date functions */
17 require_once(SM_PATH . 'functions/imap_general.php');
18 require_once(SM_PATH . 'functions/date.php');
19
20 /** Set to TRUE to dump the imap dialogue */
21 $imap_asearch_debug_dump = FALSE;
22
23 $imap_asearch_opcodes = array(
24 /* <message set> => 'asequence', */
25 /*'ALL' is binary operator */
26 'ANSWERED' => '',
27 'BCC' => 'astring',
28 'BEFORE' => 'adate',
29 'BODY' => 'astring',
30 'CC' => 'astring',
31 'DELETED' => '',
32 'DRAFT' => '',
33 'FLAGGED' => '',
34 'FROM' => 'astring',
35 'HEADER' => 'afield', /* Special syntax for this one, see below */
36 'KEYWORD' => 'akeyword',
37 'LARGER' => 'anum',
38 'NEW' => '',
39 /*'NOT' is unary operator */
40 'OLD' => '',
41 'ON' => 'adate',
42 /*'OR' is binary operator */
43 'RECENT' => '',
44 'SEEN' => '',
45 'SENTBEFORE' => 'adate',
46 'SENTON' => 'adate',
47 'SENTSINCE' => 'adate',
48 'SINCE' => 'adate',
49 'SMALLER' => 'anum',
50 'SUBJECT' => 'astring',
51 'TEXT' => 'astring',
52 'TO' => 'astring',
53 'UID' => 'asequence',
54 'UNANSWERED' => '',
55 'UNDELETED' => '',
56 'UNDRAFT' => '',
57 'UNFLAGGED' => '',
58 'UNKEYWORD' => 'akeyword',
59 'UNSEEN' => ''
60 );
61
62 $imap_asearch_months = array(
63 '01' => 'jan',
64 '02' => 'feb',
65 '03' => 'mar',
66 '04' => 'apr',
67 '05' => 'may',
68 '06' => 'jun',
69 '07' => 'jul',
70 '08' => 'aug',
71 '09' => 'sep',
72 '10' => 'oct',
73 '11' => 'nov',
74 '12' => 'dec'
75 );
76
77 $imap_error_titles = array(
78 'OK' => '',
79 'NO' => _("ERROR : Could not complete request."),
80 'BAD' => _("ERROR : Bad or malformed request."),
81 'BYE' => _("ERROR : Imap server closed the connection.")
82 );
83
84 // why can't this just use sqimap_error_box() ?
85 // It does, indeed I isolated sqimap_error_box() as a stand-alone function just for this purpose ;)
86 function sqimap_asearch_error_box($response, $query, $message)
87 {
88 global $imap_error_titles;
89
90 //if (!array_key_exists($response, $imap_error_titles)) //php 4.0.6 compatibility
91 if (!in_array($response, array_keys($imap_error_titles)))
92 $title = _("ERROR : Unknown imap response.");
93 else
94 $title = $imap_error_titles[$response];
95 $message_title = _("Reason Given: ");
96 if (function_exists('sqimap_error_box'))
97 sqimap_error_box($title, $query, $message_title, $message);
98 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
99 global $color;
100 require_once(SM_PATH . 'functions/display_messages.php');
101 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
102 if ($query != '')
103 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
104 if ($message_title != '')
105 $string .= $message_title;
106 if ($message != '')
107 $string .= htmlspecialchars($message);
108 $string .= "</font><br>\n";
109 error_box($string,$color);
110 }
111 }
112
113 /**
114 * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc!
115 */
116 function asearch_nz(&$var)
117 {
118 if (isset($var))
119 return $var;
120 return '';
121 }
122
123 /**
124 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
125 * except it doesn't handle hex constructs
126 */
127 function asearch_unhtmlentities($string) {
128 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
129 for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
130 $trans_tbl['&#' . $i . ';'] = chr($i);
131 return strtr($string, $trans_tbl);
132 /* I think the one above is quicker, though it should be benchmarked
133 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
134 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
135 */
136 }
137
138 function s_debug_dump($var_name, $var_var)
139 {
140 global $imap_asearch_debug_dump;
141 if ($imap_asearch_debug_dump) {
142 if (function_exists('sm_print_r')) //Only exists since 1.4.2
143 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
144 else {
145 echo '<pre>';
146 echo htmlentities($var_name);
147 print_r($var_var);
148 echo '</pre>';
149 }
150 }
151 }
152
153 /*
154 4.3 String:
155 A quoted string is a sequence of zero or more 7-bit characters,
156 excluding CR and LF, with double quote (<">) characters at each end.
157 9. Formal Syntax:
158 quoted-specials = DQUOTE / "\"
159 */
160 function sqimap_asearch_encode_string($what, $charset)
161 {
162 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
163 $what = mb_convert_encoding($what, 'JIS', 'auto');
164 //if (ereg("[\"\\\r\n\x80-\xff]", $what))
165 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
166 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
167 return '"' . $what . '"'; // 4.3 quoted string form
168 }
169
170 /**
171 * Parses a user date string into an rfc2060 date string
172 * (<day number>-<US month TLA>-<4 digit year>).
173 * Returns a preg_match-style array: [0]: fully formatted date,
174 * [1]: day, [2]: month, [3]: year
175 * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
176 */
177 function sqimap_asearch_parse_date($what)
178 {
179 global $imap_asearch_months;
180
181 $what = trim($what);
182 $what = ereg_replace('[ /\\.,]+', '-', $what);
183 if ($what) {
184 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
185 if (count($what_parts) == 4) {
186 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
187 /* if (!in_array($what_month, $imap_asearch_months)) {*/
188 foreach ($imap_asearch_months as $month_number => $month_code) {
189 if (($what_month == $month_number)
190 || ($what_month == $month_code)
191 || ($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
192 || ($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
193 ) {
194 $what_parts[2] = $month_number;
195 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
196 break;
197 }
198 }
199 /* }*/
200 }
201 }
202 else
203 $what_parts = array();
204 return $what_parts;
205 }
206
207 function sqimap_asearch_build_criteria($opcode, $what, $charset)
208 {
209 global $imap_asearch_opcodes;
210
211 $criteria = '';
212 switch ($imap_asearch_opcodes[$opcode]) {
213 default:
214 case 'anum':
215 /* $what = str_replace(' ', '', $what);*/
216 $what = ereg_replace('[^0-9]+', '', $what);
217 if ($what != '')
218 $criteria = $opcode . ' ' . $what . ' ';
219 break;
220 case '': /* aflag */
221 $criteria = $opcode . ' ';
222 break;
223 case 'afield': /* HEADER field-name: field-body */
224 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
225 if (count($what_parts) == 3)
226 $criteria = $opcode . ' ' .
227 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
228 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
229 break;
230 case 'adate':
231 $what_parts = sqimap_asearch_parse_date($what);
232 if (isset($what_parts[0]))
233 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
234 break;
235 case 'akeyword':
236 case 'astring':
237 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
238 break;
239 case 'asequence':
240 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
241 if ($what != '')
242 $criteria = $opcode . ' ' . $what . ' ';
243 break;
244 }
245 return $criteria;
246 }
247
248 // equivalent to : $to = array_values(array_unique(array_merge($to, $from)));
249 function sqimap_array_merge_unique($to, $from)
250 {
251 if (empty($to))
252 return $from;
253 $count = count($from);
254 for ($i = 0; $i < $count; $i++) {
255 if (!in_array($from[$i], $to))
256 $to[] = $from[$i];
257 }
258 return $to;
259 }
260
261 function sqimap_run_search($imapConnection, $search_string, $search_charset)
262 {
263 global $uid_support;
264
265 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
266 if ($search_charset != '')
267 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
268 else
269 $query = 'SEARCH ALL ' . $search_string;
270 s_debug_dump('C:', $query);
271 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
272
273 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
274 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
275 $query = 'SEARCH CHARSET US-ASCII ALL ' . $search_string;
276 s_debug_dump('C:', $query);
277 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
278 }
279 if (strtoupper($response) != 'OK') {
280 sqimap_asearch_error_box($response, $query, $message);
281 return array();
282 }
283
284 unset($messagelist);
285
286 // Keep going till we find the * SEARCH response
287 foreach ($readin as $readin_part) {
288 s_debug_dump('S:', $readin_part);
289 if (substr($readin_part, 0, 9) == '* SEARCH ') {
290 //workaround for an EIMS singularity (to say the least)
291 //$messagelist = preg_split("/ /", substr($readin_part, 9));
292 //break; //this should be the last anyway
293 $messagelist = sqimap_array_merge_unique($message_list, preg_split("/ /", substr($readin_part, 9)));
294 }
295 }
296
297 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
298 return array();
299
300 $cnt = count($messagelist);
301 for ($q = 0; $q < $cnt; $q++)
302 $id[$q] = trim($messagelist[$q]);
303 return $id;
304 }
305
306 function sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria)
307 {
308 global $uid_support;
309
310 if ($search_charset == '')
311 $search_charset = 'US-ASCII';
312 $query = 'SORT (' . $sort_criteria . ') ' . strtoupper($search_charset) . ' ALL ' . $search_string;
313 s_debug_dump('C:', $query);
314 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
315
316 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
317 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
318 $query = 'SORT (' . $sort_criteria . ') US-ASCII ALL ' . $search_string;
319 s_debug_dump('C:', $query);
320 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
321 }
322
323 if (strtoupper($response) != 'OK') {
324 // sqimap_asearch_error_box($response, $query, $message);
325 // return array();
326 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
327 }
328
329 /* Keep going till we find the * SORT response */
330 foreach ($readin as $readin_part) {
331 s_debug_dump('S:', $readin_part);
332 if (substr($readin_part, 0, 7) == '* SORT ') {
333 $messagelist = preg_split("/ /", substr($readin_part, 7));
334 break; // Should be the last anyway
335 }
336 }
337
338 if (empty($messagelist)) //Empty search response, ie '* SORT'
339 return array();
340
341 $cnt = count($messagelist);
342 for ($q = 0; $q < $cnt; $q++)
343 $id[$q] = trim($messagelist[$q]);
344 return $id;
345 }
346
347 function sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm)
348 {
349 global $thread_new, $server_sort_array;
350
351 if (sqsession_is_registered('thread_new'))
352 sqsession_unregister('thread_new');
353 if (sqsession_is_registered('server_sort_array'))
354 sqsession_unregister('server_sort_array');
355
356 $thread_new = array();
357 $thread_new[0] = "";
358
359 $server_sort_array = array();
360
361 global $uid_support;
362
363 if ($search_charset == '')
364 $search_charset = 'US-ASCII';
365 $query = 'THREAD ' . $thread_algorithm . ' ' . strtoupper($search_charset) . ' ALL ' . $search_string;
366 s_debug_dump('C:', $query);
367 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
368
369 /* 6.4 try US-ASCII charset if we received a tagged NO response (SHOULD be [BADCHARSET]) */
370 if (($search_charset != 'US-ASCII') && (strtoupper($response) == 'NO')) {
371 $query = 'THREAD ' . $thread_algorithm . ' US-ASCII ALL ' . $search_string;
372 s_debug_dump('C:', $query);
373 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
374 }
375
376 if (strtoupper($response) != 'OK') {
377 /* we should at this point:
378 - warn the user that the THREAD call has failed
379 - (offer him a way to) disconnect it permanently in the prefs
380 - perform the regular search instead or provide a way to do it in one click
381 */
382 // sqimap_asearch_error_box($response, $query, $message);
383 // return array();
384 return sqimap_run_search($imapConnection, $search_string, $search_charset); // Fell back to standard search
385 }
386
387 /* Keep going till we find the * THREAD response */
388 foreach ($readin as $readin_part) {
389 s_debug_dump('S:', $readin_part);
390 if (substr($readin_part, 0, 9) == '* THREAD ') {
391 $thread_temp = preg_split("//", substr($readin_part, 9), -1, PREG_SPLIT_NO_EMPTY);
392 break; // Should be the last anyway
393 }
394 }
395
396 if (empty($thread_temp)) //Empty search response, ie '* THREAD'
397 return array();
398
399 $char_count = count($thread_temp);
400 $counter = 0;
401 $k = 0;
402 for ($i=0;$i<$char_count;$i++) {
403 if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') {
404 $thread_new[$k] = $thread_new[$k] . $thread_temp[$i];
405 }
406 elseif ($thread_temp[$i] == '(') {
407 $thread_new[$k] .= $thread_temp[$i];
408 $counter++;
409 }
410 elseif ($thread_temp[$i] == ')') {
411 if ($counter > 1) {
412 $thread_new[$k] .= $thread_temp[$i];
413 $counter = $counter - 1;
414 }
415 else {
416 $thread_new[$k] .= $thread_temp[$i];
417 $k++;
418 $thread_new[$k] = "";
419 $counter = $counter - 1;
420 }
421 }
422 }
423 sqsession_register($thread_new, 'thread_new');
424 $thread_new = array_reverse($thread_new);
425 $thread_list = implode(" ", $thread_new);
426 $thread_list = str_replace("(", " ", $thread_list);
427 $thread_list = str_replace(")", " ", $thread_list);
428 $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
429 $server_sort_array = $thread_list;
430 sqsession_register($server_sort_array, 'server_sort_array');
431 return $thread_list;
432 }
433
434 function sqimap_asearch_get_charset()
435 {
436 global $allow_charset_search, $languages, $squirrelmail_language;
437
438 if ($allow_charset_search)
439 return $languages[$squirrelmail_language]['CHARSET'];
440 return '';
441 }
442
443 function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
444 {
445 global $internal_date_sort, $sent_folder;
446
447 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT');
448 if ($internal_date_sort == true)
449 $sort_opcodes[0] = 'ARRIVAL';
450 // if (handleAsSent($mailbox))
451 // if (isSentFolder($mailbox))
452 if ($mailbox == $sent_folder)
453 $sort_opcodes[1] = 'TO';
454 return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[$sort_by >> 1];
455 }
456
457 function sqimap_asearch($imapConnection, $mailbox_array, $biop_array, $unop_array, $where_array, $what_array, $exclude_array, $mboxes_array)
458 {
459 global $allow_server_sort, $sort, $allow_thread_sort, $thread_sort_messages;
460 global $data_dir, $username;
461
462 $search_charset = sqimap_asearch_get_charset();
463 $mbox_msgs = array();
464 $search_string = '';
465 $cur_mailbox = $mailbox_array[0];
466 $cur_biop = ''; /* Start with ALL */
467 /* We loop one more time than the real array count, so the last search gets fired */
468 for ($cur_crit = 0; $cur_crit <= count($where_array); $cur_crit++) {
469 if (empty($exclude_array[$cur_crit])) {
470 $next_mailbox = $mailbox_array[$cur_crit];
471 if ($next_mailbox != $cur_mailbox) {
472 $search_string = trim($search_string); /* Trim out last space */
473 if (($cur_mailbox == 'All Folders') && (!empty($mboxes_array)))
474 $search_mboxes = $mboxes_array;
475 else
476 $search_mboxes = array($cur_mailbox);
477 foreach ($search_mboxes as $cur_mailbox) {
478 s_debug_dump('C:SELECT:', $cur_mailbox);
479 sqimap_mailbox_select($imapConnection, $cur_mailbox);
480 $thread_sort_messages = $allow_thread_sort && getPref($data_dir, $username, 'thread_' . $cur_mailbox);
481 if ($thread_sort_messages) {
482 $thread_algorithm = 'REFERENCES';
483 $found_msgs = sqimap_run_thread($imapConnection, $search_string, $search_charset, $thread_algorithm);
484 }
485 else
486 if (($allow_server_sort) && ($sort < 6)) {
487 $sort_criteria = sqimap_asearch_get_sort_criteria($cur_mailbox, $sort);
488 $found_msgs = sqimap_run_sort($imapConnection, $search_string, $search_charset, $sort_criteria);
489 }
490 else
491 $found_msgs = sqimap_run_search($imapConnection, $search_string, $search_charset);
492 if (isset($mbox_msgs[$cur_mailbox])) {
493 if ($cur_biop == 'OR') /* Merge with previous results */
494 $mbox_msgs[$cur_mailbox] = sqimap_array_merge_unique($mbox_msgs[$cur_mailbox], $found_msgs);
495 else /* Intersect previous results */
496 $mbox_msgs[$cur_mailbox] = array_values(array_intersect($found_msgs, $mbox_msgs[$cur_mailbox]));
497 }
498 else /* No previous results */
499 $mbox_msgs[$cur_mailbox] = $found_msgs;
500 if (empty($mbox_msgs[$cur_mailbox])) /* Can happen with intersect, and we need at the end a contiguous array */
501 unset($mbox_msgs[$cur_mailbox]);
502 }
503 $cur_mailbox = $next_mailbox;
504 $search_string = '';
505 }
506 if (isset($where_array[$cur_crit])) {
507 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
508 if (!empty($criteria)) {
509 $unop = $unop_array[$cur_crit];
510 if (!empty($unop))
511 $criteria = $unop . ' ' . $criteria;
512 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
513 $next_biop = '';
514 for ($next_crit = $cur_crit+1; $next_crit <= count($where_array); $next_crit++) {
515 if (empty($exclude_array[$next_crit])) {
516 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox)
517 $next_biop = asearch_nz($biop_array[$next_crit]);
518 break;
519 }
520 }
521 if ($next_biop == 'OR')
522 $criteria = $next_biop . ' ' . $criteria;
523 $search_string .= $criteria;
524 $cur_biop = asearch_nz($biop_array[$cur_crit]);
525 }
526 }
527 }
528 }
529 return $mbox_msgs;
530 }
531
532 ?>