- global $imap_asearch_opcodes;
-
- $criteria = '';
- switch ($imap_asearch_opcodes[$opcode]) {
- default:
- case 'anum':
-/* $what = str_replace(' ', '', $what);*/
- $what = ereg_replace('[^0-9]+', '', $what);
- if ($what != '')
- $criteria = $opcode . ' ' . $what . ' ';
- break;
- case '': /* aflag */
- $criteria = $opcode . ' ';
- break;
- case 'afield': /* HEADER field-name: field-body */
- preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
- if (count($what_parts) == 3)
- $criteria = $opcode . ' ' .
- sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
- sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
- break;
- case 'adate':
- $what_parts = sqimap_asearch_parse_date($what);
- if (isset($what_parts[0]))
- $criteria = $opcode . ' ' . $what_parts[0] . ' ';
- break;
- case 'akeyword':
- case 'astring':
- $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
- break;
- case 'asequence':
- $what = ereg_replace('[^0-9:\(\)]+', '', $what);
- if ($what != '')
- $criteria = $opcode . ' ' . $what . ' ';
- break;
- }
- return $criteria;
-}
-
-function sqimap_run_search($imapConnection, $search_string, $search_charset)
-{
- global $uid_support;
-
- /* 6.4.4 try OPTIONAL [CHARSET] specification first */
- if ($search_charset != '')
- $query = 'SEARCH CHARSET "' . strtoupper($search_charset) . '" ALL ' . $search_string;
- else
- $query = 'SEARCH ALL ' . $search_string;
- s_debug_dump('C:', $query);
- $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
-
- /* 6.4.4 try US-ASCII charset if we tried an OPTIONAL [CHARSET] and received a tagged NO response (SHOULD be [BADCHARSET]) */
- if (($search_charset != '') && (strtoupper($response) == 'NO')) {
- $query = 'SEARCH CHARSET US-ASCII ALL ' . $search_string;
- s_debug_dump('C:', $query);
- $readin = sqimap_run_command($imapConnection, $query, false, $response, $message, $uid_support);
- }
- if (strtoupper($response) != 'OK') {
- sqimap_asearch_error_box($response, $query, $message);
- return array();
- }
-
- unset($messagelist);
-
- /* Keep going till we find the * SEARCH response */
- foreach ($readin as $readin_part) {
- s_debug_dump('S:', $readin_part);
- if (substr($readin_part, 0, 9) == '* SEARCH ') {
- $messagelist = preg_split("/ /", substr($readin_part, 9));
- break; // Should be the last anyway
- }
-/* else {
- if (isset($errors))
- $errors = $errors . $readin_part;
- else
- $errors = $readin_part;
- }*/
- }
-
- /* If nothing is found * SEARCH should be the first error else echo errors */
-/*if (isset($errors)) {
- if (strstr($errors,'* SEARCH'))
- return array();
- echo '<!-- ' . htmlspecialchars($errors) . ' -->';
- }*/
-
- if (empty($messagelist)) //Empty search response, ie '* SEARCH'
- return array();
-
- $cnt = count($messagelist);
- for ($q = 0; $q < $cnt; $q++)
- $id[$q] = trim($messagelist[$q]);
- return $id;
+ global $imap_asearch_opcodes;
+
+ $criteria = '';
+ switch ($imap_asearch_opcodes[$opcode]) {
+ default:
+ case 'anum':
+ $what = str_replace(' ', '', $what);
+ $what = ereg_replace('[^0-9]+[^KMG]$', '', strtoupper($what));
+ if ($what != '') {
+ switch (substr($what, -1)) {
+ case 'G':
+ $what = substr($what, 0, -1) << 30;
+ break;
+ case 'M':
+ $what = substr($what, 0, -1) << 20;
+ break;
+ case 'K':
+ $what = substr($what, 0, -1) << 10;
+ break;
+ }
+ $criteria = $opcode . ' ' . $what . ' ';
+ }
+ break;
+ case '': //aflag
+ $criteria = $opcode . ' ';
+ break;
+ case 'afield': /* HEADER field-name: field-body */
+ preg_match('/^([^:]+):(.*)$/', $what, $what_parts);
+ if (count($what_parts) == 3)
+ $criteria = $opcode . ' ' .
+ sqimap_asearch_encode_string($what_parts[1], $charset) . ' ' .
+ sqimap_asearch_encode_string($what_parts[2], $charset) . ' ';
+ break;
+ case 'adate':
+ $what_parts = sqimap_asearch_parse_date($what);
+ if (isset($what_parts[0]))
+ $criteria = $opcode . ' ' . $what_parts[0] . ' ';
+ break;
+ case 'akeyword':
+ case 'astring':
+ $criteria = $opcode . ' ' . sqimap_asearch_encode_string($what, $charset) . ' ';
+ break;
+ case 'asequence':
+ $what = ereg_replace('[^0-9:\(\)]+', '', $what);
+ if ($what != '')
+ $criteria = $opcode . ' ' . $what . ' ';
+ break;
+ }
+ return $criteria;