moving functions to separate file, adding configuration files
[squirrelmail.git] / functions / imap_asearch.php
CommitLineData
cd33ec11 1<?php
2
3/**
f7027a32 4* imap_search.php
5*
6* Copyright (c) 1999-2004 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*/
cd33ec11 20
0e218c3b 21/** This functionality requires the IMAP and date functions
f7027a32 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
f7027a32 27* @global bool $imap_asearch_debug_dump
28*/
cd33ec11 29$imap_asearch_debug_dump = FALSE;
30
40fbe929 31/** Imap SEARCH keys
f7027a32 32* @global array $imap_asearch_opcodes
33*/
17a7913a 34global $imap_asearch_opcodes;
cd33ec11 35$imap_asearch_opcodes = array(
2c300e0b 36/* <sequence-set> => 'asequence', */ // Special handling, @see sqimap_asearch_build_criteria()
cd33ec11 37/*'ALL' is binary operator */
f7027a32 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' => '',
cd33ec11 51/*'NOT' is unary operator */
f7027a32 52 'OLD' => '',
53 'ON' => 'adate',
cd33ec11 54/*'OR' is binary operator */
f7027a32 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' => ''
cd33ec11 72);
73
00b05f03 74/** Imap SEARCH month names encoding
f7027a32 75* @global array $imap_asearch_months
76*/
cd33ec11 77$imap_asearch_months = array(
f7027a32 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'
cd33ec11 90);
91
00b05f03 92/**
f7027a32 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*/
00b05f03 102//@global array color sm colors array
40fbe929 103function sqimap_asearch_error_box($response, $query, $message, $link = '')
ff6f916c 104{
0daff8c9 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
f7027a32 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 if (function_exists('sqimap_error_box'))
125 sqimap_error_box($title, $query, $message_title, $message, $link);
126 else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time
127 global $color;
b28bec15 128 require_once(SM_PATH . 'functions/display_messages.php');
6fd95361 129 $string = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n";
b28bec15 130 if ($query != '')
6fd95361 131 $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br />';
b28bec15 132 if ($message_title != '')
133 $string .= $message_title;
134 if ($message != '')
135 $string .= htmlspecialchars($message);
40fbe929 136 if ($link != '')
137 $string .= $link;
6fd95361 138 $string .= "</font><br />\n";
b28bec15 139 error_box($string,$color);
f7027a32 140 }
ff6f916c 141}
142
48af4b64 143/**
f7027a32 144* This is a convenient way to avoid spreading if (isset(... all over the code
145* @param mixed $var any variable (reference)
146* @param mixed $def default value to return if unset (default is zls (''), pass 0 or array() when appropriate)
147* @return mixed $def if $var is unset, otherwise $var
148*/
2c300e0b 149function asearch_nz(&$var, $def = '')
cd33ec11 150{
f7027a32 151 if (isset($var))
152 return $var;
153 return $def;
cd33ec11 154}
155
48af4b64 156/**
f7027a32 157* This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(),
158* except it doesn't handle hex constructs
159* @param string $string string to unhtmlentity()
160* @return string decoded string
161*/
cd33ec11 162function asearch_unhtmlentities($string) {
f7027a32 163 $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES));
164 for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */
165 $trans_tbl['&#' . $i . ';'] = chr($i);
166 return strtr($string, $trans_tbl);
cd33ec11 167/* I think the one above is quicker, though it should be benchmarked
f7027a32 168 $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES)));
169 return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string);
cd33ec11 170*/
171}
172
00b05f03 173/**
f7027a32 174* Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE
175* @global bool imap_asearch_debug_dump
176* @param string $var_name
177* @param string $var_var
178*/
ff6f916c 179function s_debug_dump($var_name, $var_var)
cd33ec11 180{
f7027a32 181 global $imap_asearch_debug_dump;
182 if ($imap_asearch_debug_dump) {
183 if (function_exists('sm_print_r')) //Only exists since 1.4.2
184 sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;)
185 else {
186 echo '<pre>';
187 echo htmlentities($var_name);
188 print_r($var_var);
189 echo '</pre>';
190 }
191 }
cd33ec11 192}
193
00b05f03 194/** Encode a string to quoted or literal as defined in rfc 3501
f7027a32 195*
196* - 4.3 String:
197* A quoted string is a sequence of zero or more 7-bit characters,
198* excluding CR and LF, with double quote (<">) characters at each end.
199* - 9. Formal Syntax:
200* quoted-specials = DQUOTE / "\"
201* @param string $what string to encode
202* @param string $charset search charset used
203* @return string encoded string
204*/
f945228f 205function sqimap_asearch_encode_string($what, $charset)
cd33ec11 206{
f7027a32 207 if (strtoupper($charset) == 'ISO-2022-JP') // This should be now handled in imap_utf7_local?
208 $what = mb_convert_encoding($what, 'JIS', 'auto');
209 if (preg_match('/["\\\\\r\n\x80-\xff]/', $what))
210 return '{' . strlen($what) . "}\r\n" . $what; // 4.3 literal form
211 return '"' . $what . '"'; // 4.3 quoted string form
cd33ec11 212}
213
48af4b64 214/**
f7027a32 215* Parses a user date string into an rfc 3501 date string
216* Handles space, slash, backslash, dot and comma as separators (and dash of course ;=)
217* @global array imap_asearch_months
218* @param string user date
219* @return array a preg_match-style array:
220* - [0] = fully formatted rfc 3501 date string (<day number>-<US month TLA>-<4 digit year>)
221* - [1] = day
222* - [2] = month
223* - [3] = year
224*/
cd33ec11 225function sqimap_asearch_parse_date($what)
226{
f7027a32 227 global $imap_asearch_months;
228
229 $what = trim($what);
230 $what = ereg_replace('[ /\\.,]+', '-', $what);
231 if ($what) {
232 preg_match('/^([0-9]+)-+([^\-]+)-+([0-9]+)$/', $what, $what_parts);
233 if (count($what_parts) == 4) {
234 $what_month = strtolower(asearch_unhtmlentities($what_parts[2]));
cd33ec11 235/* if (!in_array($what_month, $imap_asearch_months)) {*/
f7027a32 236 foreach ($imap_asearch_months as $month_number => $month_code) {
237 if (($what_month == $month_number)
238 || ($what_month == $month_code)
239 || ($what_month == strtolower(asearch_unhtmlentities(getMonthName($month_number))))
240 || ($what_month == strtolower(asearch_unhtmlentities(getMonthAbrv($month_number))))
241 ) {
242 $what_parts[2] = $month_number;
243 $what_parts[0] = $what_parts[1] . '-' . $month_code . '-' . $what_parts[3];
244 break;
245 }
246 }
cd33ec11 247/* }*/
f7027a32 248 }
249 }
250 else
251 $what_parts = array();
252 return $what_parts;
cd33ec11 253}
254
00b05f03 255/**
f7027a32 256* Build one criteria sequence
257* @global array imap_asearch_opcodes
258* @param string $opcode search opcode
259* @param string $what opcode argument
260* @param string $charset search charset
261* @return string one full criteria sequence
262*/
f945228f 263function sqimap_asearch_build_criteria($opcode, $what, $charset)
cd33ec11 264{
f7027a32 265 global $imap_asearch_opcodes;
266
267 $criteria = '';
268 switch ($imap_asearch_opcodes[$opcode]) {
269 default:
270 case 'anum':
271 $what = str_replace(' ', '', $what);
272 $what = ereg_replace('[^0-9]+[^KMG]$', '', strtoupper($what));
273 if ($what != '') {
274 switch (substr($what, -1)) {
275 case 'G':
276 $what = substr($what, 0, -1) << 30;
277 break;
278 case 'M':
279 $what = substr($what, 0, -1) << 20;
280 break;
281 case 'K':
282 $what = substr($what, 0, -1) << 10;
283 break;
284 }
285 $criteria = $opcode . ' ' . $what . ' ';
286 }
287 break;
288 case '': //aflag
289 $criteria = $opcode . ' ';
290 break;
291 case 'afield': /* HEADER field-name: field-body */
292 preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
293 if (count($what_parts) == 3)
294 $criteria = $opcode . ' ' .
295 sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
296 sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
297 break;
298 case 'adate':
299 $what_parts = sqimap_asearch_parse_date($what);
300 if (isset($what_parts[0]))
301 $criteria = $opcode . ' ' . $what_parts[0] . ' ';
302 break;
303 case 'akeyword':
304 case 'astring':
305 $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
306 break;
307 case 'asequence':
308 $what = ereg_replace('[^0-9:\(\)]+', '', $what);
309 if ($what != '')
310 $criteria = $opcode . ' ' . $what . ' ';
311 break;
312 }
313 return $criteria;
cd33ec11 314}
315
00b05f03 316/**
f7027a32 317* Another way to do array_values(array_unique(array_merge($to, $from)));
318* @param array $to to array (reference)
319* @param array $from from array
320* @return array uniquely merged array
321*/
d2f031ed 322function sqimap_array_merge_unique(&$to, $from)
75d24fd2 323{
f7027a32 324 if (empty($to))
325 return $from;
326 $count = count($from);
327 for ($i = 0; $i < $count; $i++) {
328 if (!in_array($from[$i], $to))
329 $to[] = $from[$i];
330 }
331 return $to;
75d24fd2 332}
333
00b05f03 334/**
f7027a32 335* Run the imap SEARCH command as defined in rfc 3501
336* @link http://www.ietf.org/rfc/rfc3501.txt
337* @param resource $imapConnection the current imap stream
338* @param string $search_string the full search expression eg "ALL RECENT"
339* @param string $search_charset charset to use or zls ('')
340* @return array an IDs or UIDs array of matching messages or an empty array
341*/
cd33ec11 342function sqimap_run_search($imapConnection, $search_string, $search_charset)
343{
f7027a32 344 //For some reason, this seems to happen and forbids searching servers not allowing OPTIONAL [CHARSET]
345 if (strtoupper($search_charset) == 'US-ASCII')
346 $search_charset = '';
347 /* 6.4.4 try OPTIONAL [CHARSET] specification first */
348 if ($search_charset != '')
349 $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ' . $search_string;
350 else
351 $query = 'SEARCH ' . $search_string;
352 s_debug_dump('C:', $query);
353 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE);
354
355 /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
356 if (($search_charset != '') && (strtoupper($response) == 'NO')) {
357 $query = 'SEARCH CHARSET US-ASCII ' . $search_string;
358 s_debug_dump('C:', $query);
359 $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, TRUE);
360 }
361 if (strtoupper($response) != 'OK') {
362 sqimap_asearch_error_box($response, $query, $message);
363 return array();
364 }
324ac3c5 365 $messagelist = parseUidList($readin,'SEARCH');
cd33ec11 366
f7027a32 367 if (empty($messagelist)) //Empty search response, ie '* SEARCH'
368 return array();
3f075f6c 369
f7027a32 370 $cnt = count($messagelist);
371 for ($q = 0; $q < $cnt; $q++)
372 $id[$q] = trim($messagelist[$q]);
373 return $id;
cd33ec11 374}
375
00b05f03 376/**
f7027a32 377* @global bool allow_charset_search user setting
378* @global array languages sm languages array
379* @global string squirrelmail_language user language setting
380* @return string the user defined charset if $allow_charset_search is TRUE else zls ('')
381*/
f945228f 382function sqimap_asearch_get_charset()
383{
f7027a32 384 global $allow_charset_search, $languages, $squirrelmail_language;
f945228f 385
f7027a32 386 if ($allow_charset_search)
387 return $languages[$squirrelmail_language]['CHARSET'];
388 return '';
f945228f 389}
390
00b05f03 391/**
f7027a32 392* Convert sm internal sort to imap sort taking care of:
393* - user defined date sorting (ARRIVAL vs DATE)
394* - if the searched mailbox is the sent folder then TO is being used instead of FROM
395* - reverse order by using REVERSE
396* @param string $mailbox mailbox name to sort
397* @param integer $sort_by sm sort criteria index
398* @global bool internal_date_sort sort by arrival date instead of message date
399* @global string sent_folder sent folder name
400* @return string imap sort criteria
401*/
c2d47d51 402function sqimap_asearch_get_sort_criteria($mailbox, $sort_by)
403{
f7027a32 404 global $internal_date_sort, $sent_folder;
c2d47d51 405
f7027a32 406 $sort_opcodes = array ('DATE', 'FROM', 'SUBJECT', 'SIZE');
407 if ($internal_date_sort == true)
408 $sort_opcodes[0] = 'ARRIVAL';
c2d47d51 409// if (handleAsSent($mailbox))
410// if (isSentFolder($mailbox))
f7027a32 411 if ($mailbox == $sent_folder)
412 $sort_opcodes[1] = 'TO';
413 return (($sort_by % 2) ? '' : 'REVERSE ') . $sort_opcodes[($sort_by >> 1) & 3];
c2d47d51 414}
415
40fbe929 416/**
f7027a32 417* @param string $cur_mailbox unformatted mailbox name
418* @param array $boxes_unformatted selectable mailbox unformatted names array (reference)
419* @return array sub mailboxes unformatted names
420*/
0e218c3b 421function sqimap_asearch_get_sub_mailboxes($cur_mailbox, &$mboxes_array)
40fbe929 422{
f7027a32 423 $sub_mboxes_array = array();
424 $boxcount = count($mboxes_array);
425 for ($boxnum=0; $boxnum < $boxcount; $boxnum++) {
426 if (isBoxBelow($mboxes_array[$boxnum], $cur_mailbox))
427 $sub_mboxes_array[] = $mboxes_array[$boxnum];
428 }
429 return $sub_mboxes_array;
40fbe929 430}
431
00b05f03 432/**
f7027a32 433* Create the search query strings for all given criteria and merge results for every mailbox
434* @param resource $imapConnection
435* @param array $mailbox_array (reference)
436* @param array $biop_array (reference)
437* @param array $unop_array (reference)
438* @param array $where_array (reference)
439* @param array $what_array (reference)
440* @param array $exclude_array (reference)
441* @param array $sub_array (reference)
442* @param array $mboxes_array selectable unformatted mailboxes names (reference)
443* @return array array(mailbox => array(UIDs))
444*/
0e218c3b 445function sqimap_asearch($imapConnection, &$mailbox_array, &$biop_array, &$unop_array, &$where_array, &$what_array, &$exclude_array, &$sub_array, &$mboxes_array)
cd33ec11 446{
c2d47d51 447
f7027a32 448 $search_charset = sqimap_asearch_get_charset();
449 $mbox_msgs = array();
324ac3c5 450 $mbox_search = array();
f7027a32 451 $search_string = '';
452 $cur_mailbox = $mailbox_array[0];
453 $cur_biop = ''; /* Start with ALL */
454 /* We loop one more time than the real array count, so the last search gets fired */
455 for ($cur_crit=0,$iCnt=count($where_array); $cur_crit <= $iCnt; ++$cur_crit) {
456 if (empty($exclude_array[$cur_crit])) {
457 $next_mailbox = (isset($mailbox_array[$cur_crit])) ? $mailbox_array[$cur_crit] : false;
458 if ($next_mailbox != $cur_mailbox) {
459 $search_string = trim($search_string); /* Trim out last space */
460 if ($cur_mailbox == 'All Folders')
696155b5 461 $search_mboxes = $mboxes_array;
f7027a32 462 else if ((!empty($sub_array[$cur_crit - 1])) || (!in_array($cur_mailbox, $mboxes_array)))
463 $search_mboxes = sqimap_asearch_get_sub_mailboxes($cur_mailbox, $mboxes_array);
464 else
465 $search_mboxes = array($cur_mailbox);
466 foreach ($search_mboxes as $cur_mailbox) {
324ac3c5 467 if (isset($mbox_search[$cur_mailbox])) {
468 $mbox_search[$cur_mailbox]['search'] .= ' ' . $search_string;
469 } else {
470 $mbox_search[$cur_mailbox]['search'] = $search_string;
471 }
472 $mbox_search[$cur_mailbox]['charset'] = $search_charset;
473 }
f7027a32 474 $cur_mailbox = $next_mailbox;
475 $search_string = '';
f7027a32 476 }
477 if (isset($where_array[$cur_crit]) && empty($exclude_array[$cur_crit])) {
478 for ($crit = $cur_crit; $crit < count($where_array); $crit++) {
479 $criteria = trim(sqimap_asearch_build_criteria($where_array[$crit], $what_array[$crit], $search_charset));
480 if (!empty($criteria) && empty($exclude_array[$crit])) {
481 if (asearch_nz($mailbox_array[$crit]) == $cur_mailbox) {
482 $unop = $unop_array[$crit];
483 if (!empty($unop)) {
484 $criteria = $unop . ' ' . $criteria;
485 }
486 $aCriteria[] = array($biop_array[$crit], $criteria);
487 }
324ac3c5 488 }
f7027a32 489 // unset something
490 $exclude_array[$crit] = true;
491 }
492 $aSearch = array();
493 for($i=0,$iCnt=count($aCriteria);$i<$iCnt;++$i) {
494 $cur_biop = $aCriteria[$i][0];
495 $next_biop = (isset($aCriteria[$i+1][0])) ? $aCriteria[$i+1][0] : false;
496 if ($next_biop != $cur_biop && $next_biop == 'OR') {
497 $aSearch[] = 'OR '.$aCriteria[$i][1];
498 } else if ($cur_biop != 'OR') {
499 $aSearch[] = 'ALL '.$aCriteria[$i][1];
696155b5 500 } else { // OR only supports 2 search keys so we need to create a parenthesized list
f7027a32 501 $prev_biop = (isset($aCriteria[$i-1][0])) ? $aCriteria[$i-1][0] : false;
502 if ($prev_biop == $cur_biop) {
503 $last = $aSearch[$i-1];
504 if (!substr($last,-1) == ')') {
505 $aSearch[$i-1] = "(OR $last";
506 $aSearch[] = $aCriteria[$i][1].')';
507 } else {
508 $sEnd = '';
509 while ($last && substr($last,-1) == ')') {
696155b5 510 $last = substr($last,0,-1);
511 $sEnd .= ')';
324ac3c5 512 }
f7027a32 513 $aSearch[$i-1] = "(OR $last";
514 $aSearch[] = $aCriteria[$i][1].$sEnd.')';
515 }
516 } else {
517 $aSearch[] = $aCriteria[$i][1];
518 }
519 }
f7027a32 520 }
521 $search_string .= implode(' ',$aSearch);
522 }
f7027a32 523 }
524 }
324ac3c5 525 return ($mbox_search);
cd33ec11 526}
527
6fd95361 528?>