/libs/functions/imap_search.php

https://github.com/dslfaithdev/soEmail · PHP · 127 lines · 75 code · 24 blank · 28 comment · 24 complexity · 9498bb242b50f4d073dfc1e9819a4ae1 MD5 · raw file

  1. <?php
  2. /**
  3. * imap_search.php
  4. *
  5. * IMAP search routines
  6. *
  7. * @copyright &copy; 1999-2009 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id: imap_search.php 13549 2009-04-15 22:00:49Z jervfors $
  10. * @package squirrelmail
  11. * @subpackage imap
  12. * @deprecated This search interface has been largely replaced by asearch
  13. */
  14. /**
  15. * Load up a bunch of SM functions */
  16. require_once(SM_PATH . 'functions/imap.php');
  17. require_once(SM_PATH . 'functions/date.php');
  18. require_once(SM_PATH . 'functions/mailbox_display.php');
  19. require_once(SM_PATH . 'functions/mime.php');
  20. function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
  21. $color, $search_position = '', $search_all, $count_all) {
  22. global $message_highlight_list, $squirrelmail_language, $languages,
  23. $index_order, $pos, $allow_charset_search, $uid_support,
  24. $imap_server_type;
  25. $pos = $search_position;
  26. $urlMailbox = urlencode($mailbox);
  27. /* construct the search query, taking multiple search terms into account */
  28. $multi_search = array();
  29. $search_what = trim($search_what);
  30. $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
  31. $multi_search = explode(' ', $search_what);
  32. $search_string = '';
  33. /* it seems macosx and hmailserver do not support the prefered search
  34. syntax so we fall back to the older style. This IMAP
  35. server has a problem with multiple search terms. Instead
  36. of returning the messages that match all the terms it
  37. returns the messages that match each term. Could be fixed
  38. on the client side, but should be fixed on the server
  39. as per the RFC */
  40. if ($imap_server_type == 'macosx' || $imap_server_type == 'hmailserver') {
  41. foreach ($multi_search as $multi_search_part) {
  42. if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
  43. $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
  44. }
  45. $search_string .= $search_where . ' ' .$multi_search_part . ' ';
  46. }
  47. }
  48. else {
  49. foreach ($multi_search as $multi_search_part) {
  50. if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
  51. $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
  52. }
  53. $search_string .= $search_where . ' {' . strlen($multi_search_part)
  54. . "}\r\n" . $multi_search_part . ' ';
  55. }
  56. }
  57. $search_string = trim($search_string);
  58. /* now use $search_string in the imap search */
  59. if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
  60. $languages[$squirrelmail_language]['CHARSET']) {
  61. $ss = "SEARCH CHARSET "
  62. . strtoupper($languages[$squirrelmail_language]['CHARSET'])
  63. . " ALL $search_string";
  64. } else {
  65. $ss = "SEARCH ALL $search_string";
  66. }
  67. /* read data back from IMAP */
  68. $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
  69. /* try US-ASCII charset if search fails */
  70. if (isset($languages[$squirrelmail_language]['CHARSET'])
  71. && strtolower($result) == 'no') {
  72. $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
  73. $readin = sqimap_run_command ($imapConnection, $ss, true,
  74. $result, $message, $uid_support);
  75. }
  76. unset($messagelist);
  77. /* Keep going till we find the SEARCH response */
  78. foreach ($readin as $readin_part) {
  79. /* Check to see if a SEARCH response was received */
  80. if (substr($readin_part, 0, 9) == '* SEARCH ') {
  81. $messagelist = preg_split("/ /", substr($readin_part, 9));
  82. } else if (isset($errors)) {
  83. $errors = $errors.$readin_part;
  84. } else {
  85. $errors = $readin_part;
  86. }
  87. }
  88. /* If nothing is found * SEARCH should be the first error else echo errors */
  89. if (isset($errors)) {
  90. if (strstr($errors,'* SEARCH')) {
  91. return array();
  92. }
  93. echo '<!-- '.htmlspecialchars($errors) .' -->';
  94. }
  95. global $sent_folder;
  96. $cnt = count($messagelist);
  97. for ($q = 0; $q < $cnt; $q++) {
  98. $id[$q] = trim($messagelist[$q]);
  99. }
  100. $issent = ($mailbox == $sent_folder);
  101. $msgs = fillMessageArray($imapConnection,$id,$cnt);
  102. return $msgs;
  103. }