phpdoc updates
[squirrelmail.git] / functions / imap_search.php
CommitLineData
6e189ce2 1<?php
2ba13803 2
35586184 3/**
15e6162e 4 * imap_search.php
35586184 5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
15e6162e 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$
d6c32258 12 * @package squirrelmail
13 * @deprecated This search interface has been largely replaced by asearch
35586184 14 */
15
d6c32258 16/**
17 * Load up a bunch of SM functions */
b68edc75 18require_once(SM_PATH . 'functions/imap.php');
19require_once(SM_PATH . 'functions/date.php');
b68edc75 20require_once(SM_PATH . 'functions/mailbox_display.php');
21require_once(SM_PATH . 'functions/mime.php');
2ba13803 22
b1e29794 23function sqimap_search($imapConnection, $search_where, $search_what, $mailbox,
24 $color, $search_position = '', $search_all, $count_all) {
2ba13803 25
dba269b1 26 global $message_highlight_list, $squirrelmail_language, $languages,
6201339c 27 $index_order, $pos, $allow_charset_search,
2b3d9db0 28 $imap_server_type;
dba269b1 29
53828ec5 30 $pos = $search_position;
2ba13803 31
29eb5486 32 $urlMailbox = urlencode($mailbox);
2ba13803 33
b1e29794 34 /* construct the search query, taking multiple search terms into account */
35 $multi_search = array();
36 $search_what = trim($search_what);
37 $search_what = ereg_replace('[ ]{2,}', ' ', $search_what);
38 $multi_search = explode(' ', $search_what);
39 $search_string = '';
2b3d9db0 40
41 /* it seems macosx does not support the prefered search
42 syntax so we fall back to the older style. This IMAP
43 server has a problem with multiple search terms. Instead
44 of returning the messages that match all the terms it
45 returns the messages that match each term. Could be fixed
46 on the client side, but should be fixed on the server
47 as per the RFC */
48
49 if ($imap_server_type == 'macosx') {
50 foreach ($multi_search as $multi_search_part) {
83be314a 51 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
52 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
53 }
2b3d9db0 54 $search_string .= $search_where . ' ' .$multi_search_part . ' ';
55 }
56 }
57 else {
58 foreach ($multi_search as $multi_search_part) {
83be314a 59 if (strtoupper($languages[$squirrelmail_language]['CHARSET']) == 'ISO-2022-JP') {
60 $multi_search_part = mb_convert_encoding($multi_search_part, 'JIS', 'auto');
61 }
2b3d9db0 62 $search_string .= $search_where . ' {' . strlen($multi_search_part)
63 . "}\r\n" . $multi_search_part . ' ';
64 }
5c9a316d 65 }
dba269b1 66
70c4fd84 67 $search_string = trim($search_string);
c92ef720 68
b1e29794 69 /* now use $search_string in the imap search */
65ffb3ce 70 if ($allow_charset_search && isset($languages[$squirrelmail_language]['CHARSET']) &&
29eb5486 71 $languages[$squirrelmail_language]['CHARSET']) {
8ceffac2 72 $ss = "SEARCH CHARSET "
73 . strtoupper($languages[$squirrelmail_language]['CHARSET'])
74 . " ALL $search_string";
29eb5486 75 } else {
b1e29794 76 $ss = "SEARCH ALL $search_string";
29eb5486 77 }
b1e29794 78
79 /* read data back from IMAP */
6201339c 80 $readin = sqimap_run_command($imapConnection, $ss, false, $result, $message, TRUE);
b1e29794 81
82 /* try US-ASCII charset if search fails */
8ceffac2 83 if (isset($languages[$squirrelmail_language]['CHARSET'])
84 && strtolower($result) == 'no') {
fee5ad96 85 $ss = "SEARCH CHARSET \"US-ASCII\" ALL $search_string";
8ceffac2 86 $readin = sqimap_run_command ($imapConnection, $ss, true,
87 $result, $message);
29eb5486 88 }
2ba13803 89
70c4fd84 90 unset($messagelist);
b1e29794 91
92 /* Keep going till we find the SEARCH response */
93 foreach ($readin as $readin_part) {
94 /* Check to see if a SEARCH response was received */
95 if (substr($readin_part, 0, 9) == '* SEARCH ') {
dba269b1 96 $messagelist = preg_split("/ /", substr($readin_part, 9));
b1e29794 97 } else if (isset($errors)) {
98 $errors = $errors.$readin_part;
99 } else {
100 $errors = $readin_part;
101 }
29eb5486 102 }
2ba13803 103
29eb5486 104 /* If nothing is found * SEARCH should be the first error else echo errors */
b1e29794 105 if (isset($errors)) {
106 if (strstr($errors,'* SEARCH')) {
5fac2758 107 return array();
70c4fd84 108 }
9b761dbd 109 echo '<!-- '.htmlspecialchars($errors) .' -->';
29eb5486 110 }
2ba13803 111
112
29eb5486 113 global $sent_folder;
dba269b1 114
115 $cnt = count($messagelist);
116 for ($q = 0; $q < $cnt; $q++) {
29eb5486 117 $id[$q] = trim($messagelist[$q]);
118 }
119 $issent = ($mailbox == $sent_folder);
f6b58812 120
20473d1e 121 $msgs = fillMessageArray($imapConnection,$id,$cnt);
2ba13803 122
dba269b1 123 return $msgs;
124}
2ba13803 125
2ba13803 126
fc05541b 127
1c72b151 128?>