*** empty log message ***
[squirrelmail.git] / functions / imap_search.php
CommitLineData
6e189ce2 1<?php
2ba13803 2
35586184 3/**
15e6162e 4 * imap_search.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
35586184 8 *
15e6162e 9 * IMAP search routines
35586184 10 *
15e6162e 11 * $Id$
35586184 12 */
13
35586184 14require_once('../functions/imap.php');
15require_once('../functions/date.php');
16require_once('../functions/array.php');
17require_once('../functions/mailbox_display.php');
18require_once('../functions/mime.php');
2ba13803 19
b1e29794 20function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
21 $color, $search_position = '', $search_all, $count_all) {
2ba13803 22
dba269b1 23 global $message_highlight_list, $squirrelmail_language, $languages,
24 $index_order, $pos, $allow_charset_search, $uid_support;
25
53828ec5 26 $pos = $search_position;
2ba13803 27
29eb5486 28 $urlMailbox = urlencode($mailbox);
2ba13803 29
b1e29794 30 /* construct the search query, taking multiple search terms into account */
31 $multi_search = array();
32 $search_what = trim($search_what);
33 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
34 $multi_search = explode(' ', $search_what);
35 $search_string = '';
dba269b1 36 foreach ($multi_search as $multi_search_part) {
37 $search_string .= $search_where . ' {' . strlen($multi_search_part)
38 . "}\r\n" . $multi_search_part . ' ';
5c9a316d 39 }
dba269b1 40
70c4fd84 41 $search_string = trim($search_string);
c92ef720 42
b1e29794 43 /* now use $search_string in the imap search */
65ffb3ce 44 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
29eb5486 45 $languages[$squirrelmail_language]['CHARSET']) {
8ceffac2 46 $ss = "SEARCH CHARSET "
47 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
48 . " ALL $search_string";
29eb5486 49 } else {
b1e29794 50 $ss = "SEARCH ALL $search_string";
29eb5486 51 }
b1e29794 52
53 /* read data back from IMAP */
dba269b1 54 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, $uid_support);
b1e29794 55
56 /* try US-ASCII charset if search fails */
8ceffac2 57 if (isset($languages[$squirrelmail_language]['CHARSET'])
58 && strtolower($result) == 'no') {
fee5ad96 59 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
8ceffac2 60 $readin = sqimap_run_command ($imapConnection, $ss, true,
61 $result, $message);
29eb5486 62 }
2ba13803 63
70c4fd84 64 unset($messagelist);
b1e29794 65
66 /* Keep going till we find the SEARCH response */
67 foreach ($readin as $readin_part) {
68 /* Check to see if a SEARCH response was received */
69 if (substr($readin_part, 0, 9) == '* SEARCH ') {
dba269b1 70 $messagelist = preg_split("/ /", substr($readin_part, 9));
b1e29794 71 } else if (isset($errors)) {
72 $errors = $errors.$readin_part;
73 } else {
74 $errors = $readin_part;
75 }
29eb5486 76 }
2ba13803 77
29eb5486 78 /* If nothing is found * SEARCH should be the first error else echo errors */
b1e29794 79 if (isset($errors)) {
80 if (strstr($errors,'* SEARCH')) {
5fac2758 81 return array();
70c4fd84 82 }
b1e29794 83 echo "<!-- $errors -->";
29eb5486 84 }
2ba13803 85
86
29eb5486 87 global $sent_folder;
dba269b1 88
89 $cnt = count($messagelist);
90 for ($q = 0; $q < $cnt; $q++) {
29eb5486 91 $id[$q] = trim($messagelist[$q]);
92 }
93 $issent = ($mailbox == $sent_folder);
f6b58812 94
dba269b1 95 $msgs = fillMessageArray($imapConnection,$id,$issent,$cnt);
2ba13803 96
dba269b1 97 return $msgs;
98}
2ba13803 99
2ba13803 100
fc05541b 101
1c72b151 102?>