imap_asearch.php 20 KB

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