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