cd33ec11 |
1 | <?php |
2 | |
3 | /** |
4 | * imap_search.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 The SquirrelMail Project Team |
cd33ec11 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * IMAP asearch routines |
40fbe929 |
10 | * |
11 | * $Id$ |
d6c32258 |
12 | * @package squirrelmail |
00b05f03 |
13 | * @see search.php |
40fbe929 |
14 | * @link ftp://ftp.rfc-editor.org/in-notes/rfc3501.txt |
15 | * @author Alex Lemaresquier - Brainstorm - alex at brainstorm.fr |
cd33ec11 |
16 | * |
40fbe929 |
17 | * Subfolder search idea from Patch #806075 by Thomas Pohl xraven at users.sourceforge.net. Thanks Thomas! |
cd33ec11 |
18 | */ |
19 | |
d6c32258 |
20 | /** This functionality requires the IMAP and date functions */ |
ff6f916c |
21 | require_once(SM_PATH . 'functions/imap_general.php'); |
cd33ec11 |
22 | require_once(SM_PATH . 'functions/date.php'); |
23 | |
00b05f03 |
24 | /** Set to TRUE to dump the imap dialogue |
25 | * @global bool $imap_asearch_debug_dump |
26 | */ |
cd33ec11 |
27 | $imap_asearch_debug_dump = FALSE; |
28 | |
40fbe929 |
29 | /** Imap SEARCH keys |
00b05f03 |
30 | * @global array $imap_asearch_opcodes |
31 | */ |
cd33ec11 |
32 | $imap_asearch_opcodes = array( |
33 | /* <message set> => 'asequence', */ |
34 | /*'ALL' is binary operator */ |
35 | 'ANSWERED' => '', |
36 | 'BCC' => 'astring', |
37 | 'BEFORE' => 'adate', |
38 | 'BODY' => 'astring', |
39 | 'CC' => 'astring', |
40 | 'DELETED' => '', |
41 | 'DRAFT' => '', |
42 | 'FLAGGED' => '', |
43 | 'FROM' => 'astring', |
44 | 'HEADER' => 'afield', /* Special syntax for this one, see below */ |
45 | 'KEYWORD' => 'akeyword', |
46 | 'LARGER' => 'anum', |
47 | 'NEW' => '', |
48 | /*'NOT' is unary operator */ |
49 | 'OLD' => '', |
50 | 'ON' => 'adate', |
51 | /*'OR' is binary operator */ |
52 | 'RECENT' => '', |
53 | 'SEEN' => '', |
54 | 'SENTBEFORE' => 'adate', |
55 | 'SENTON' => 'adate', |
56 | 'SENTSINCE' => 'adate', |
57 | 'SINCE' => 'adate', |
58 | 'SMALLER' => 'anum', |
59 | 'SUBJECT' => 'astring', |
60 | 'TEXT' => 'astring', |
61 | 'TO' => 'astring', |
62 | 'UID' => 'asequence', |
63 | 'UNANSWERED' => '', |
64 | 'UNDELETED' => '', |
65 | 'UNDRAFT' => '', |
66 | 'UNFLAGGED' => '', |
67 | 'UNKEYWORD' => 'akeyword', |
68 | 'UNSEEN' => '' |
69 | ); |
70 | |
00b05f03 |
71 | /** Imap SEARCH month names encoding |
72 | * @global array $imap_asearch_months |
73 | */ |
cd33ec11 |
74 | $imap_asearch_months = array( |
75 | '01' => 'jan', |
76 | '02' => 'feb', |
77 | '03' => 'mar', |
78 | '04' => 'apr', |
79 | '05' => 'may', |
80 | '06' => 'jun', |
81 | '07' => 'jul', |
82 | '08' => 'aug', |
83 | '09' => 'sep', |
84 | '10' => 'oct', |
85 | '11' => 'nov', |
86 | '12' => 'dec' |
87 | ); |
88 | |
00b05f03 |
89 | /** Error message titles according to imap server returned code |
90 | * @global array $imap_error_titles |
91 | */ |
ff6f916c |
92 | $imap_error_titles = array( |
93 | 'OK' => '', |
94 | 'NO' => _("ERROR : Could not complete request."), |
95 | 'BAD' => _("ERROR : Bad or malformed request."), |
40fbe929 |
96 | 'BYE' => _("ERROR : Imap server closed the connection."), |
97 | '' => _("ERROR : Connection dropped by imap-server.") |
ff6f916c |
98 | ); |
99 | |
00b05f03 |
100 | /** |
101 | * Function to display an error related to an IMAP-query. |
102 | * We need to do our own error management since we may receive NO responses on purpose (even BAD with SORT or THREAD) |
103 | * so we call sqimap_error_box() if the function exists (sm >= 1.5) or use our own embedded code |
104 | * @global array imap_error_titles |
105 | * @param string $response the imap server response code |
106 | * @param string $query the failed query |
40fbe929 |
107 | * @param string $message an optional error message |
108 | * @param string $link an optional link to try again |
00b05f03 |
109 | */ |
110 | //@global array color sm colors array |
40fbe929 |
111 | function sqimap_asearch_error_box($response, $query, $message, $link = '') |
ff6f916c |
112 | { |
113 | global $imap_error_titles; |
114 | |
115 | //if (!array_key_exists($response, $imap_error_titles)) //php 4.0.6 compatibility |
116 | if (!in_array($response, array_keys($imap_error_titles))) |
117 | $title = _("ERROR : Unknown imap response."); |
118 | else |
119 | $title = $imap_error_titles[$response]; |
40fbe929 |
120 | if ($link == '') |
121 | $message_title = _("Reason Given: "); |
122 | else |
123 | $message_title = _("Possible reason : "); |
b28bec15 |
124 | if (function_exists('sqimap_error_box')) |
40fbe929 |
125 | sqimap_error_box($title, $query, $message_title, $message, $link); |
b28bec15 |
126 | else { //Straight copy of 1.5 imap_general.php:sqimap_error_box(). Can be removed at a later time |
797784f9 |
127 | global $color; |
b28bec15 |
128 | require_once(SM_PATH . 'functions/display_messages.php'); |
129 | $string = "<font color=$color[2]><b>\n" . $title . "</b><br>\n"; |
130 | if ($query != '') |
131 | $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br>'; |
132 | if ($message_title != '') |
133 | $string .= $message_title; |
134 | if ($message != '') |
135 | $string .= htmlspecialchars($message); |
40fbe929 |
136 | if ($link != '') |
137 | $string .= $link; |
b28bec15 |
138 | $string .= "</font><br>\n"; |
139 | error_box($string,$color); |
140 | } |
ff6f916c |
141 | } |
142 | |
48af4b64 |
143 | /** |
144 | * This is to avoid the E_NOTICE warnings signaled by marc AT squirrelmail.org. Thanks Marc! |
d2f031ed |
145 | * @param mixed $var any variable (reference) |
00b05f03 |
146 | * @return mixed zls ('') if $var is not defined, otherwise $var |
48af4b64 |
147 | */ |
cd33ec11 |
148 | function asearch_nz(&$var) |
149 | { |
150 | if (isset($var)) |
151 | return $var; |
152 | return ''; |
153 | } |
154 | |
48af4b64 |
155 | /** |
156 | * This should give the same results as PHP 4 >= 4.3.0's html_entity_decode(), |
157 | * except it doesn't handle hex constructs |
00b05f03 |
158 | * @param string $string string to unhtmlentity() |
159 | * @return string decoded string |
48af4b64 |
160 | */ |
cd33ec11 |
161 | function asearch_unhtmlentities($string) { |
162 | $trans_tbl = array_flip(get_html_translation_table(HTML_ENTITIES)); |
163 | for ($i=127; $i<255; $i++) /* Add &#<dec>; entities */ |
164 | $trans_tbl['&#' . $i . ';'] = chr($i); |
165 | return strtr($string, $trans_tbl); |
166 | /* I think the one above is quicker, though it should be benchmarked |
167 | $string = strtr($string, array_flip(get_html_translation_table(HTML_ENTITIES))); |
168 | return preg_replace("/&#([0-9]+);/E", "chr('\\1')", $string); |
169 | */ |
170 | } |
171 | |
00b05f03 |
172 | /** |
173 | * Provide an easy way to dump the imap dialogue if $imap_asearch_debug_dump is TRUE |
174 | * @global imap_asearch_debug_dump |
175 | * @param string $var_name |
176 | * @param string $var_var |
177 | */ |
ff6f916c |
178 | function s_debug_dump($var_name, $var_var) |
cd33ec11 |
179 | { |
180 | global $imap_asearch_debug_dump; |
f9fb0d38 |
181 | if ($imap_asearch_debug_dump) { |
b28bec15 |
182 | if (function_exists('sm_print_r')) //Only exists since 1.4.2 |
f9fb0d38 |
183 | sm_print_r($var_name, $var_var); //Better be the 'varargs' version ;) |
184 | else { |
185 | echo '<pre>'; |
186 | echo htmlentities($var_name); |
187 | print_r($var_var); |
188 | echo '</pre>'; |
189 | } |
190 | } |
cd33ec11 |
191 | } |
192 | |
00b05f03 |
193 | /** Encode a string to quoted or literal as defined in rfc 3501 |
194 | * |
195 |