Source for file imap_search.php

Documentation is available at imap_search.php

  1. <?php
  2.  
  3. /**
  4.  * imap_search.php
  5.  *
  6.  * IMAP search routines
  7.  *
  8.  * @copyright 1999-2020 The SquirrelMail Project Team
  9.  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  10.  * @version $Id: imap_search.php 14840 2020-01-07 07:42:38Z pdontthink $
  11.  * @package squirrelmail
  12.  * @subpackage imap
  13.  * @deprecated This search interface has been largely replaced by asearch
  14.  */
  15.  
  16. /**
  17.  * Load up a bunch of SM functions */
  18.  
  19. require_once(SM_PATH . 'functions/imap.php');
  20. require_once(SM_PATH . 'functions/date.php');
  21. require_once(SM_PATH . 'functions/mailbox_display.php');
  22. require_once(SM_PATH . 'functions/mime.php');
  23.  
  24. /**
  25.   * @param string $search_where The location to search (see RFC3501 section 6.4.4)
  26.   *                              If this string contains underscores, they are
  27.   *                              interpreted as an OR condition, thus "TO_CC" will
  28.   *                              result in a search of the TO *or* CC headers
  29.   */
  30. function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
  31.                        $color, $search_position = '', $search_all, $count_all) {
  32.  
  33.     global $message_highlight_list, $squirrelmail_language, $languages,
  34.            $index_order, $pos, $allow_charset_search, $uid_support,
  35.        $imap_server_type;
  36.  
  37.     $pos = $search_position;
  38.  
  39.     $urlMailbox = urlencode($mailbox);
  40.  
  41.     /* construct the search query, taking multiple search terms into account */
  42.     $multi_search = array();
  43.     $search_what  = trim($search_what);
  44.     $search_what  = preg_replace('/[ ]{2,}/', ' ', $search_what);
  45.     $multi_search = explode(' ', $search_what);
  46.     $search_string = '';
  47.  
  48.     if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
  49.         foreach($multi_search as $idx=>$search_part) {
  50.             $multi_search[$idx] = mb_convert_encoding($search_part, 'JIS', 'auto');
  51.         }
  52.     }
  53.  
  54.     $and_search = TRUE;
  55.     while ($pos = strpos($search_where, '_')) {
  56.         $and_search = FALSE;
  57.         $search_where = 'OR ' . substr($search_where, 0, $pos) . ' %s ' . substr($search_where, $pos + 1);
  58.     }
  59.     $search_where .= ' %s ';
  60.     $search_parts = array_filter(explode(' %s ', $search_where));
  61.  
  62.     $search_literal = array('commands'=>array(), 'literal_args'=>array());
  63.     $use_search_literal = FALSE;
  64.     foreach ($multi_search as $string) {
  65.         //FIXME: why JIS?  shouldn't input be in EUC-JP?  this is copied from DEVEL
  66.         if (isset($languages[$squirrelmail_language]['CHARSET']) &&
  67.             strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP')
  68.             $string = mb_convert_encoding($string, 'JIS', 'auto');
  69.         if (preg_match('/["\\\\\r\n\x80-\xff]/', $string))
  70.             $use_search_literal = TRUE;
  71.         foreach ($search_parts as $chunk) {
  72.             $search_literal['commands'][] = $chunk;
  73.             $search_literal['literal_args'][] = $string;
  74.         }
  75.         $search_string .= str_replace('%s', 
  76.                          '"'
  77.                        . str_replace(array('\\', '"'), array('\\\\', '\\"'), $string)
  78.                        . '"', $search_where);
  79.     }
  80.  
  81.     $search_string = trim($search_string);
  82.     $original_search_literal = $search_literal;
  83.  
  84.     /* now use $search_string in the imap search */
  85.     if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
  86.         $languages[$squirrelmail_language]['CHARSET']) {
  87.         if ($use_search_literal) {
  88.             $search_literal['commands'][0] = 'SEARCH CHARSET '
  89.                 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
  90.                 . ' ALL ' . $search_literal['commands'][0];
  91.         } else {
  92.             $ss = "SEARCH CHARSET "
  93.                 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
  94.                 . ($and_search ? ' ALL' : '') . " $search_string";
  95.         }
  96.     } else {
  97.         if ($use_search_literal) {
  98.             $search_literal['commands'][0] = 'SEARCH ALL ' . $search_literal['commands'][0];
  99.         } else {
  100.             $ss = "SEARCH ALL $search_string";
  101.         }
  102.     }
  103.  
  104.     /* read data back from IMAP */
  105.     if ($use_search_literal) {
  106.         $readin = sqimap_run_literal_command($imapConnection, $search_literal, false, $result, $message, $uid_support);
  107.     } else {
  108.         $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
  109.     }
  110.  
  111.     /* try US-ASCII charset if search fails */
  112.     if (isset($languages[$squirrelmail_language]['CHARSET'])
  113.         && strtolower($result) == 'no') {
  114.         if ($use_search_literal) {
  115.             $original_search_literal['commands'][0] = 'SEARCH CHARSET "US-ASCII" ALL '
  116.                                                     . $original_search_literal['commands'][0];
  117.         } else {
  118.             $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
  119.         }
  120.         if ($use_search_literal) {
  121.             $readin = sqimap_run_literal_command($imapConnection, $search_literal, false, $result, $message, $uid_support);
  122.         } else {
  123.             $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
  124.         }
  125.     }
  126.  
  127.     unset($messagelist);
  128.  
  129.     /* Keep going till we find the SEARCH response */
  130.     foreach ($readin as $readin_part) {
  131.         /* Check to see if a SEARCH response was received */
  132.         if (substr($readin_part, 0, 9) == '* SEARCH ') {
  133.             $messagelist = preg_split("/ /", substr($readin_part, 9));
  134.         } else if (isset($errors)) {
  135.             $errors = $errors.$readin_part;
  136.         } else {
  137.             $errors = $readin_part;
  138.         }
  139.     }
  140.  
  141.     /* If nothing is found * SEARCH should be the first error else echo errors */
  142.     if (isset($errors)) {
  143.         if (strstr($errors,'* SEARCH')) {
  144.             return array();
  145.         }
  146.         echo '<!-- '.sm_encode_html_special_chars($errors) .' -->';
  147.     }
  148.  
  149.  
  150.     global $sent_folder;
  151.  
  152.     $cnt = count($messagelist);
  153.     for ($q = 0; $q < $cnt; $q++) {
  154.         $id[$q] = trim($messagelist[$q]);
  155.     }
  156.     $issent = ($mailbox == $sent_folder);
  157.  
  158.     $msgs = fillMessageArray($imapConnection,$id,$cnt);
  159.  
  160.     return $msgs;
  161. }

Documentation generated on Mon, 13 Jan 2020 04:24:48 +0100 by phpDocumentor 1.4.3