Rewrite of internal message caching
[squirrelmail.git] / functions / imap_asearch.php
CommitLineData
cd33ec11 1<?php
2
3/**
4 * imap_search.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
cd33ec11 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * IMAP asearch routines
40fbe929 10 *
eb19bc67 11 * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas!
12 *
13 * @version $Id$
d6c32258 14 * @package squirrelmail
eb19bc67 15 * @subpackage imap
00b05f03 16 * @see search.php
2c300e0b 17 * @link http://www.ietf.org/rfc/rfc3501.txt
40fbe929 18 * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr
cd33ec11 19 */
20
0e218c3b 21/** This functionality requires the IMAP and date functions
22 */
ff6f916c 23require_once(SM_PATH . 'functions/imap_general.php');
cd33ec11 24require_once(SM_PATH . 'functions/date.php');
25
00b05f03 26/** Set to TRUE to dump the imap dialogue
27 * @global bool $imap_asearch_debug_dump
28 */
cd33ec11 29$imap_asearch_debug_dump = FALSE;
30
40fbe929 31/** Imap SEARCH keys
00b05f03 32 * @global array $imap_asearch_opcodes
33 */
cd33ec11 34$imap_asearch_opcodes = array(
2c300e0b 35/* <sequence-set> => 'asequence', */ // Special handling, @see sqimap_asearch_build_criteria()
cd33ec11 36/*'ALL' is binary operator */
37 'ANSWERED' => '',
38 'BCC' => 'astring',
39 'BEFORE' => 'adate',
40 'BODY' => 'astring',
41 'CC' => 'astring',
42 'DELETED' => '',
43 'DRAFT' => '',
44 'FLAGGED' => '',
45 'FROM' => 'astring',
2c300e0b 46 'HEADER' => 'afield', // Special syntax for this one, @see sqimap_asearch_build_criteria()
cd33ec11 47 'KEYWORD' => 'akeyword',
48 'LARGER' => 'anum',
49 'NEW' => '',
50/*'NOT' is unary operator */
51 'OLD' => '',
52 'ON' => 'adate',
53/*'OR' is binary operator */
54 'RECENT' => '',
55 'SEEN' => '',
56 'SENTBEFORE' => 'adate',
57 'SENTON' => 'adate',
58 'SENTSINCE' => 'adate',
59 'SINCE' => 'adate',
60 'SMALLER' => 'anum',
61 'SUBJECT' => 'astring',
62 'TEXT' => 'astring',
63 'TO' => 'astring',
64 'UID' => 'asequence',
65 'UNANSWERED' => '',
66 'UNDELETED' => '',
67 'UNDRAFT' => '',
68 'UNFLAGGED' => '',
69 'UNKEYWORD' => 'akeyword',
70 'UNSEEN' => ''
71);
72
00b05f03 73/** Imap SEARCH month names encoding
74 * @global array $imap_asearch_months
75 */
cd33ec11 76$imap_asearch_months = array(
77 '01' => 'jan',
78 '02' => 'feb',
79 '03' => 'mar',
80 '04' => 'apr',
81 '05' => 'may',
82 '06' => 'jun',
83 '07' => 'jul',
84 '08' => 'aug',
85 '09' => 'sep',
86 '10' => 'oct',
87 '11' => 'nov',
88 '12' => 'dec'
89);
90
00b05f03 91/** Error message titles according to imap server returned code
92 * @global array $imap_error_titles
93 */
ff6f916c 94$imap_error_titles = array(
95 'OK' => '',
96 'NO' => _("ERROR : Could not complete request."),
97 'BAD' => _("ERROR : Bad or malformed request."),
40fbe929 98 'BYE' => _("ERROR : Imap server closed the connection."),
99 '' => _("ERROR : Connection dropped by imap-server.")
ff6f916c 100);
101
00b05f03 102/**
103 * Function to display an error related to an IMAP-query.
104 * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD)
105 * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code
106 * @global array imap_error_titles
107 * @param string $response the imap server response code
108 * @param string $query the failed query
40fbe929 109 * @param string $message an optional error message
110 * @param string $link an optional link to try again
00b05f03 111 */
112//@global array color sm colors array
40fbe929 113function sqimap_asearch_error_box($response, $query, $message, $link = '')
ff6f916c 114{
115 global $imap_error_titles;
116
abd74f7d 117 if (!array_key_exists($response, $imap_error_titles))
ff6f916c 118 $title = _("ERROR : Unknown imap response.");
119 else
120 $title = $imap_error_titles[$response];
40fbe929 121 if ($link == '')
122 $message_title = _("Reason Given: ");
123 else
124 $message_title = _("Possible reason : ");
b28bec15 125 if (function_exists('sqimap_error_box'))
40fbe929 126 sqimap_error_box($title, $query, $message_title, $message, $link);
b28bec15 127 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
797784f9 128 global $color;
b28bec15 129 require_once(SM_PATH . 'functions/display_messages.php');
130 $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n";
131 if ($query != '')
132 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>';
133 if ($message_title != '')
134 $string .= $message_title;
135 if ($message != '')
136 $string .= htmlspecialchars($message);
40fbe929 137 if ($link != '')
138 $string .= $link;
b28bec15 139 $string .= "</font><br>\n";
140 error_box($string,$color);
141 }
ff6f916c 142}
143
48af4b64 144/**
2c300e0b 145 * This is a convenient way to avoid spreading if (isset(... all over the code
d2f031ed 146 * @param mixed $var any variable (reference)
0e218c3b 147 * @param mixed $def default value to return if unset (default is zls (''), pass 0 or array() when appropriate)
2c300e0b 148 * @return mixed $def if $var is unset, otherwise $var
48af4b64 149 */
2c300e0b 150function asearch_nz(&$var, $def = '')
cd33ec11 151{
152 if (isset($var))
153 return $var;
2c300e0b 154 return $def;
cd33ec11 155}
156
48af4b64 157/**
158 * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
159 * except it doesn't handle hex constructs
00b05f03 160 * @param string $string string to unhtmlentity()
161 * @return string decoded string
48af4b64 162 */
cd33ec11 163function asearch_unhtmlentities($string) {
164 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
165 for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
166 $trans_tbl['&#' . $i . ';'] = chr($i);
167 return strtr($string, $trans_tbl);
168/* I think the one above is quicker, though it should be benchmarked
169 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
170 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
171*/
172}
173
00b05f03 174/**
175 * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
0e218c3b 176 * @global bool imap_asearch_debug_dump
00b05f03 177 * @param string $var_name
178 * @param string $var_var
179 */
ff6f916c 180function s_debug_dump($var_name, $var_var)
cd33ec11 181{
182 global $imap_asearch_debug_dump;
f9fb0d38 183 if ($imap_asearch_debug_dump) {
b28bec15 184 if (function_exists('sm_print_r')) //Only exists since 1.4.2
f9fb0d38 185 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
186 else {
187 echo '<pre>';
188 echo htmlentities($var_name);
189 print_r($var_var);
190 echo '</pre>';
191 }
192 }
cd33ec11 193}
194
00b05f03 195/** Encode a string to quoted or literal as defined in rfc 3501
196 *
324ac3c5 197 * - 4.3 String:
00b05f03 198 * A quoted string is a sequence of zero or more 7-bit characters,
199 * excluding CR and LF, with double quote (<">) characters at each end.
324ac3c5 200 * - 9. Formal Syntax:
00b05f03 201 * quoted-specials = DQUOTE / "\"
202 * @param string $what string to encode
203 * @param string $charset search charset used
204 * @return string encoded string
205 */
f945228f 206function sqimap_asearch_encode_string($what, $charset)
cd33ec11 207{
f945228f 208 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
cd33ec11 209 $what = mb_convert_encoding($what, 'JIS', 'auto');
ff6f916c 210 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
797784f9 211 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
212 return '"' . $what . '"'; // 4.3 quoted string form
cd33ec11 213}
214
48af4b64 215/**
00b05f03 216 * Parses a user date string into an rfc 3501 date string
48af4b64 217 * Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
0e218c3b 218 * @global array imap_asearch_months
00b05f03 219 * @param string user date
220 * @return array a preg_match-style array:
221 * - [0] = fully formatted rfc 3501 date string (<day number>-<US month TLA>-<4 digit year>)
222 * - [1] = day
223 * - [2] = month
224 * - [3] = year
48af4b64 225 */
cd33ec11 226function sqimap_asearch_parse_date($what)
227{
228 global $imap_asearch_months;
229
230 $what = trim($what);
375b552d 231 $what = ereg_replace('[ /\\.,]+', '-', $what);
cd33ec11 232 if ($what) {
375b552d 233 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
cd33ec11 234 if (count($what_parts) == 4) {
235 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
236/* if (!in_array($what_month, $imap_asearch_months)) {*/
237 foreach ($imap_asearch_months as $month_number => $month_code) {
238 if (($what_month == $month_number)
239 || ($what_month == $month_code)
240 || ($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
241 || ($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
242 ) {
243 $what_parts[2] = $month_number;
244 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
245 break;
246 }
247 }
248/* }*/
249 }
250 }
251 else
252 $what_parts = array();
253 return $what_parts;
254}
255
00b05f03 256/**
257 * Build one criteria sequence
258 * @global array imap_asearch_opcodes
259 * @param string $opcode search opcode
260 * @param string $what opcode argument
261 * @param string $charset search charset
262 * @return string one full criteria sequence
263 */
f945228f 264function sqimap_asearch_build_criteria($opcode, $what, $charset)
cd33ec11 265{
266 global $imap_asearch_opcodes;
267
375b552d 268 $criteria = '';
cd33ec11 269 switch ($imap_asearch_opcodes[$opcode]) {
270 default:
271 case 'anum':
2c300e0b 272 $what = str_replace(' ', '', $what);
273 $what = ereg_replace('[^0-9]+[^KMG]$', '', strtoupper($what));
274 if ($what != '') {
275 switch (substr($what, -1)) {
276 case 'G':
1a4dffc7 277 $what = substr($what, 0, -1) << 30;
278 break;
2c300e0b 279 case 'M':
1a4dffc7 280 $what = substr($what, 0, -1) << 20;
281 break;
2c300e0b 282 case 'K':
1a4dffc7 283 $what = substr($what, 0, -1) << 10;
2c300e0b 284 break;
285 }
cd33ec11 286 $criteria = $opcode . ' ' . $what . ' ';
2c300e0b 287 }
cd33ec11 288 break;
00b05f03 289 case '': //aflag
cd33ec11 290 $criteria = $opcode . ' ';
291 break;
292 case 'afield': /* HEADER field-name: field-body */
293 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
294 if (count($what_parts) == 3)
324ac3c5 295 $criteria = $opcode . ' ' .
f945228f 296 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
297 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
cd33ec11 298 break;
299 case 'adate':
300 $what_parts = sqimap_asearch_parse_date($what);
375b552d 301 if (isset($what_parts[0]))
cd33ec11 302 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
303 break;
304 case 'akeyword':
305 case 'astring':
f945228f 306 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
cd33ec11 307 break;
308 case 'asequence':
309 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
310 if ($what != '')
311 $criteria = $opcode . ' ' . $what . ' ';
312 break;
313 }
314 return $criteria;
315}
316
00b05f03 317/**
318 * Another way to do array_values(array_unique(array_merge($to, $from)));
d2f031ed 319 * @param array $to to array (reference)
00b05f03 320 * @param array $from from array
321 * @return array uniquely merged array
322 */
d2f031ed 323function sqimap_array_merge_unique(&$to, $from)
75d24fd2 324{
325 if (empty($to))
326 return $from;
327 $count = count($from);
328 for ($i = 0; $i < $count; $i++) {
329 if (!in_array($from[$i], $to))
330 $to[] = $from[$i];
331 }
332 return $to;
333}
334
00b05f03 335/**
336 * Run the imap SEARCH command as defined in rfc 3501
2c300e0b 337 * @link http://www.ietf.org/rfc/rfc3501.txt
00b05f03 338 * @param resource $imapConnection the current imap stream
339 * @param string $search_string the full search expression eg "ALL RECENT"
340 * @param string $search_charset charset to use or zls ('')
341 * @return array an IDs or UIDs array of matching messages or an empty array
342 */
cd33ec11 343function sqimap_run_search($imapConnection, $search_string, $search_charset)
344{
0e218c3b 345 //For some reason, this seems to happen and forbids searching servers not allowing OPTIONAL [CHARSET]
346 if (strtoupper($search_charset) == 'US-ASCII')
347 $search_charset = '';
cd33ec11 348 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
f945228f 349 if ($search_charset != '')
324ac3c5 350 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ' . $search_string;
cd33ec11 351 else
324ac3c5 352 $query = 'SEARCH ' . $search_string;
ff6f916c 353 s_debug_dump('C:', $query);
6201339c 354 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE);
cd33ec11 355
f945228f 356 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
357 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
324ac3c5 358 $query = 'SEARCH CHARSET US-ASCII ' . $search_string;
ff6f916c 359 s_debug_dump('C:', $query);
6201339c 360 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE);
ff6f916c 361 }
362 if (strtoupper($response) != 'OK') {
363 sqimap_asearch_error_box($response, $query, $message);
364 return array();
cd33ec11 365 }
324ac3c5 366 $messagelist = parseUidList($readin,'SEARCH');
cd33ec11 367
ff6f916c 368 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
3f075f6c 369 return array();
370
cd33ec11 371 $cnt = count($messagelist);
372 for ($q = 0; $q < $cnt; $q++)
373 $id[$q] = trim($messagelist[$q]);
374 return $id;
375}
376
00b05f03 377/**
378 * @global bool allow_charset_search user setting
379 * @global array languages sm languages array
380 * @global string squirrelmail_language user language setting
381 * @return string the user defined charset if $allow_charset_search is TRUE else zls ('')
382 */
f945228f 383function sqimap_asearch_get_charset()
384{
385 global $allow_charset_search, $languages, $squirrelmail_language;
386
387 if ($allow_charset_search)
388 return $languages[$squirrelmail_language]['CHARSET'];
389 return '';
390}
391
00b05f03 392/**
393 * Convert sm internal sort to imap sort taking care of:
394 * - user defined date sorting (ARRIVAL vs DATE)
395 * - if the searched mailbox is the sent folder then TO is being used instead of FROM
396 * - reverse order by using REVERSE
397 * @param string $mailbox mailbox name to sort
40fbe929 398 * @param integer $sort_by sm sort criteria index
2d2f8bb8 399 * @global bool internal_date_sort sort by arrival date instead of message date
400 * @global string sent_folder sent folder name
00b05f03 401 * @return string imap sort criteria
402 */
c2d47d51 403function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
404{
405 global $internal_date_sort, $sent_folder;
406
d2f031ed 407 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT', 'SIZE');
c2d47d51 408 if ($internal_date_sort == true)
409 $sort_opcodes[0] = 'ARRIVAL';
410// if (handleAsSent($mailbox))
411// if (isSentFolder($mailbox))
412 if ($mailbox == $sent_folder)
413 $sort_opcodes[1] = 'TO';
d2f031ed 414 return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[($sort_by >> 1) & 3];
c2d47d51 415}
416
40fbe929 417/**
418 * @param string $cur_mailbox unformatted mailbox name
419 * @param array $boxes_unformatted selectable mailbox unformatted names array (reference)
420 * @return array sub mailboxes unformatted names
421 */
0e218c3b 422function sqimap_asearch_get_sub_mailboxes($cur_mailbox, &$mboxes_array)
40fbe929 423{
424 $sub_mboxes_array = array();
425 $boxcount = count($mboxes_array);
426 for ($boxnum=0; $boxnum < $boxcount; $boxnum++) {
427 if (isBoxBelow($mboxes_array[$boxnum], $cur_mailbox))
428 $sub_mboxes_array[] = $mboxes_array[$boxnum];
429 }
430 return $sub_mboxes_array;
431}
432
00b05f03 433/**
324ac3c5 434 * Create the search query strings for all given criteria and merge results for every mailbox
40fbe929 435 * @param resource $imapConnection
0e218c3b 436 * @param array $mailbox_array (reference)
437 * @param array $biop_array (reference)
438 * @param array $unop_array (reference)
439 * @param array $where_array (reference)
440 * @param array $what_array (reference)
441 * @param array $exclude_array (reference)
442 * @param array $sub_array (reference)
443 * @param array $mboxes_array selectable unformatted mailboxes names (reference)
0e218c3b 444 * @return array array(mailbox => array(UIDs))
00b05f03 445 */
0e218c3b 446function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array, &$mboxes_array)
cd33ec11 447{
c2d47d51 448
f945228f 449 $search_charset = sqimap_asearch_get_charset();
cd33ec11 450 $mbox_msgs = array();
324ac3c5 451 $mbox_search = array();
cd33ec11 452 $search_string = '';
453 $cur_mailbox = $mailbox_array[0];
454 $cur_biop = ''; /* Start with ALL */
455 /* We loop one more time than the real array count, so the last search gets fired */
324ac3c5 456 for ($cur_crit=0,$iCnt=count($where_array); $cur_crit <= $iCnt; ++$cur_crit) {
cd33ec11 457 if (empty($exclude_array[$cur_crit])) {
458 $next_mailbox = $mailbox_array[$cur_crit];
459 if ($next_mailbox != $cur_mailbox) {
460 $search_string = trim($search_string); /* Trim out last space */
40fbe929 461 if ($cur_mailbox == 'All Folders')
462 $search_mboxes = $mboxes_array;
463 else if ((!empty($sub_array[$cur_crit - 1])) || (!in_array($cur_mailbox, $mboxes_array)))
464 $search_mboxes = sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array);
cd33ec11 465 else
466 $search_mboxes = array($cur_mailbox);
467 foreach ($search_mboxes as $cur_mailbox) {
324ac3c5 468 if (isset($mbox_search[$cur_mailbox])) {
469 $mbox_search[$cur_mailbox]['search'] .= ' ' . $search_string;
470 } else {
471 $mbox_search[$cur_mailbox]['search'] = $search_string;
472 }
473 $mbox_search[$cur_mailbox]['charset'] = $search_charset;
474 }
475 $cur_mailbox = $next_mailbox;
cd33ec11 476 $search_string = '';
324ac3c5 477
cd33ec11 478 }
324ac3c5 479 if (isset($where_array[$cur_crit]) && empty($exclude_array[$cur_crit])) {
cd33ec11 480 $criteria = sqimap_asearch_build_criteria($where_array[$cur_crit], $what_array[$cur_crit], $search_charset);
481 if (!empty($criteria)) {
324ac3c5 482 //$criteria = 'ALL '. $criteria;
cd33ec11 483 $unop = $unop_array[$cur_crit];
324ac3c5 484 if (!empty($unop)) {
cd33ec11 485 $criteria = $unop . ' ' . $criteria;
324ac3c5 486 } else {
487 $criteria = 'ALL ' . $criteria;
488 }
cd33ec11 489 /* We need to infix the next non-excluded criteria's biop if it's the same mailbox */
490 $next_biop = '';
491 for ($next_crit = $cur_crit+1; $next_crit <= count($where_array); $next_crit++) {
492 if (empty($exclude_array[$next_crit])) {
324ac3c5 493 if (asearch_nz($mailbox_array[$next_crit]) == $cur_mailbox) {
5ab3c3fe 494 $next_biop = asearch_nz($biop_array[$next_crit]);
324ac3c5 495 if ($next_biop == 'OR' || $next_biop == 'ALL') {
496 $next_criterium = sqimap_asearch_build_criteria($where_array[$next_crit], $what_array[$next_crit], $search_charset);
497 // unset something
498 $exclude_array[$next_crit] = true;
499 $criteria .= $next_biop . ' '. $next_criterium;
500 }
501 }
cd33ec11 502 }
503 }
324ac3c5 504 //if ($next_biop == 'OR')
505 // $criteria = $next_biop . ' ' . $criteria;
cd33ec11 506 $search_string .= $criteria;
324ac3c5 507 //$cur_biop = asearch_nz($biop_array[$cur_crit]);
cd33ec11 508 }
509 }
324ac3c5 510
cd33ec11 511 }
512 }
324ac3c5 513 return ($mbox_search);
cd33ec11 514}
515
d6c32258 516?>