59177427 |
1 | <?php |
052e0c26 |
2 | /** |
3 | ** imap.php |
4 | ** |
5 | ** This implements all functions that do general imap functions. |
245a6892 |
6 | ** |
7 | ** $Id$ |
052e0c26 |
8 | **/ |
9 | |
f435778e |
10 | if (defined ('imap_general_php')) |
11 | return; |
12 | define ('imap_general_php', true); |
13 | |
14 | global $imap_general_debug; |
a3db804c |
15 | $imap_general_debug = false; |
a3db804c |
16 | |
052e0c26 |
17 | /****************************************************************************** |
18 | ** Reads the output from the IMAP stream. If handle_errors is set to true, |
19 | ** this will also handle all errors that are received. If it is not set, |
20 | ** the errors will be sent back through $response and $message |
21 | ******************************************************************************/ |
3d2504a1 |
22 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) { |
a3db804c |
23 | global $color, $squirrelmail_language, $imap_general_debug; |
a3432f47 |
24 | |
f358f099 |
25 | $read = fgets($imap_stream, 9096); |
245a6892 |
26 | |
4e7d6212 |
27 | if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { |
d9ca5b40 |
28 | $size = $regs[1]; |
245a6892 |
29 | } else { |
30 | $size = 0; |
d9ca5b40 |
31 | } |
32 | |
61d9e885 |
33 | $data = array(); |
4f5c1bcb |
34 | $total_size = 0; |
245a6892 |
35 | |
d9ca5b40 |
36 | $continue = true; |
37 | while ($continue) { |
38 | // Continue if needed for this single line |
39 | while (strpos($read, "\n") === false) { |
40 | $read .= fgets($imap_stream, 9096); |
41 | } |
4f5c1bcb |
42 | // For debugging purposes |
d9ca5b40 |
43 | if ($imap_general_debug) { |
44 | echo "<small><tt><font color=\"#CC0000\">$read</font></tt></small><br>\n"; |
45 | flush(); |
46 | } |
4f5c1bcb |
47 | |
48 | |
49 | // If we know the size, no need to look at the end parameters |
50 | if ($size > 0) { |
51 | if ($total_size == $size) { |
52 | $data[] = $read; |
53 | $read = fgets($imap_stream, 9096); |
61d9e885 |
54 | while (!ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) { |
55 | $read = fgets($imap_stream, 9096); |
56 | } |
4f5c1bcb |
57 | $continue = false; |
58 | } else if ($total_size > $size) { |
59 | $difference = $total_size - $size; |
60 | $total_size = $total_size - strlen($read); |
61 | $read = substr ($read, 0, strlen($read)-$difference); |
62 | $data[] = $read; |
63 | $junk = fgets($imap_stream, 9096); |
d9ca5b40 |
64 | $continue = false; |
4f5c1bcb |
65 | } else { |
66 | $data[] = $read; |
67 | $read = fgets($imap_stream, 9096); |
d9ca5b40 |
68 | } |
4f5c1bcb |
69 | $total_size += strlen($read); |
d9ca5b40 |
70 | } else { |
4f5c1bcb |
71 | if (ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) { |
72 | $continue = false; |
73 | } else { |
74 | $data[] = $read; |
75 | $read = fgets ($imap_stream, 9096); |
76 | } |
d9ca5b40 |
77 | } |
f358f099 |
78 | } |
a3db804c |
79 | |
3d2504a1 |
80 | $response = $regs[1]; |
81 | $message = trim($regs[2]); |
82 | |
74424a43 |
83 | if ($imap_general_debug) echo '--<br>'; |
052e0c26 |
84 | |
f358f099 |
85 | if ($handle_errors == false) |
86 | return $data; |
d9ca5b40 |
87 | |
74424a43 |
88 | if ($response == 'NO') { |
f358f099 |
89 | // ignore this error from m$ exchange, it is not fatal (aka bug) |
8e8ebd27 |
90 | if (strstr($message, 'command resulted in') === false) { |
441f2d33 |
91 | set_up_language($squirrelmail_language); |
04632dbc |
92 | echo "<br><b><font color=$color[2]>\n"; |
f358f099 |
93 | echo _("ERROR : Could not complete request."); |
04632dbc |
94 | echo "</b><br>\n"; |
f358f099 |
95 | echo _("Reason Given: "); |
3d2504a1 |
96 | echo $message . "</font><br>\n"; |
052e0c26 |
97 | exit; |
98 | } |
74424a43 |
99 | } else if ($response == 'BAD') { |
f358f099 |
100 | set_up_language($squirrelmail_language); |
101 | echo "<br><b><font color=$color[2]>\n"; |
102 | echo _("ERROR : Bad or malformed request."); |
103 | echo "</b><br>\n"; |
104 | echo _("Server responded: "); |
105 | echo $message . "</font><br>\n"; |
106 | exit; |
052e0c26 |
107 | } |
108 | |
109 | return $data; |
110 | } |
111 | |
052e0c26 |
112 | /****************************************************************************** |
113 | ** Logs the user into the imap server. If $hide is set, no error messages |
114 | ** will be displayed. This function returns the imap connection handle. |
115 | ******************************************************************************/ |
116 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
bc104ef3 |
117 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
1b187352 |
118 | |
342c46bd |
119 | $imap_stream = fsockopen ($imap_server_address, $imap_port, |
090595e1 |
120 | $error_number, $error_string, 15); |
052e0c26 |
121 | $server_info = fgets ($imap_stream, 1024); |
122 | |
52eefafc |
123 | // Decrypt the password |
bc104ef3 |
124 | $password = OneTimePadDecrypt($password, $onetimepad); |
52eefafc |
125 | |
052e0c26 |
126 | /** Do some error correction **/ |
127 | if (!$imap_stream) { |
128 | if (!$hide) { |
441f2d33 |
129 | set_up_language($squirrelmail_language, true); |
1b187352 |
130 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
052e0c26 |
131 | echo "$error_number : $error_string<br>\r\n"; |
132 | } |
133 | exit; |
134 | } |
135 | |
704db5b2 |
136 | fputs ($imap_stream, "a001 LOGIN \"" . quoteIMAP($username) . |
137 | '" "' . quoteIMAP($password) . "\"\r\n"); |
74424a43 |
138 | $read = sqimap_read_data ($imap_stream, 'a001', false, $response, $message); |
052e0c26 |
139 | |
140 | /** If the connection was not successful, lets see why **/ |
3d2504a1 |
141 | if ($response != "OK") { |
052e0c26 |
142 | if (!$hide) { |
74424a43 |
143 | if ($response != 'NO') { |
3d2504a1 |
144 | // "BAD" and anything else gets reported here. |
441f2d33 |
145 | set_up_language($squirrelmail_language, true); |
74424a43 |
146 | if ($response == 'BAD') |
3d2504a1 |
147 | printf (_("Bad request: %s")."<br>\r\n", $message); |
148 | else |
149 | printf (_("Unknown error: %s") . "<br>\n", $message); |
74424a43 |
150 | echo '<br>'; |
3d2504a1 |
151 | echo _("Read data:") . "<br>\n"; |
ec0b7dbd |
152 | if (is_array($read)) |
153 | { |
154 | foreach ($read as $line) |
155 | { |
156 | echo htmlspecialchars($line) . "<br>\n"; |
157 | } |
3d2504a1 |
158 | } |
052e0c26 |
159 | exit; |
165e24a7 |
160 | } else { |
1b187352 |
161 | // If the user does not log in with the correct |
162 | // username and password it is not possible to get the |
163 | // correct locale from the user's preferences. |
164 | // Therefore, apply the same hack as on the login |
165 | // screen. |
166 | |
167 | // $squirrelmail_language is set by a cookie when |
168 | // the user selects language and logs out |
169 | |
441f2d33 |
170 | set_up_language($squirrelmail_language, true); |
1b187352 |
171 | |
052e0c26 |
172 | ?> |
173 | <html> |
74424a43 |
174 | <body bgcolor="ffffff"> |
052e0c26 |
175 | <br> |
176 | <center> |
74424a43 |
177 | <table width="70%" noborder bgcolor="ffffff" align="center"> |
052e0c26 |
178 | <tr> |
74424a43 |
179 | <td bgcolor="dcdcdc"> |
180 | <font color="cc0000"> |
052e0c26 |
181 | <center> |
59177427 |
182 | <?php echo _("ERROR") ?> |
052e0c26 |
183 | </center> |
184 | </font> |
185 | </td> |
186 | </tr> |
187 | <tr> |
188 | <td> |
189 | <center> |
59177427 |
190 | <?php echo _("Unknown user or password incorrect.") ?><br> |
5630c7ec |
191 | <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a> |
052e0c26 |
192 | </center> |
193 | </td> |
194 | </tr> |
195 | </table> |
196 | </center> |
197 | </body> |
198 | </html> |
59177427 |
199 | <?php |
052e0c26 |
200 | session_destroy(); |
201 | exit; |
052e0c26 |
202 | } |
203 | } else { |
204 | exit; |
205 | } |
206 | } |
207 | |
208 | return $imap_stream; |
209 | } |
210 | |
211 | |
212 | |
213 | |
214 | /****************************************************************************** |
215 | ** Simply logs out the imap session |
216 | ******************************************************************************/ |
217 | function sqimap_logout ($imap_stream) { |
218 | fputs ($imap_stream, "a001 LOGOUT\r\n"); |
219 | } |
220 | |
f435778e |
221 | function sqimap_capability($imap_stream, $capability) { |
a3db804c |
222 | global $sqimap_capabilities; |
223 | global $imap_general_debug; |
052e0c26 |
224 | |
a3db804c |
225 | if (!is_array($sqimap_capabilities)) { |
226 | fputs ($imap_stream, "a001 CAPABILITY\r\n"); |
74424a43 |
227 | $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b); |
a3db804c |
228 | |
229 | $c = explode(' ', $read[0]); |
230 | for ($i=2; $i < count($c); $i++) { |
245a6892 |
231 | $cap_list = explode('=', $c[$i]); |
232 | if (isset($cap_list[1])) |
233 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
234 | else |
235 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
a3db804c |
236 | } |
237 | } |
238 | return $sqimap_capabilities[$capability]; |
239 | } |
052e0c26 |
240 | |
241 | /****************************************************************************** |
242 | ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test... |
243 | ******************************************************************************/ |
f435778e |
244 | function sqimap_get_delimiter ($imap_stream = false) { |
245 | global $imap_general_debug; |
246 | global $sqimap_delimiter; |
247 | global $optional_delimiter; |
f6941f00 |
248 | |
f435778e |
249 | /* Use configured delimiter if set */ |
250 | if((!empty($optional_delimiter)) && $optional_delimiter != "detect") |
251 | return $optional_delimiter; |
a3db804c |
252 | |
f435778e |
253 | /* Do some caching here */ |
254 | if (!$sqimap_delimiter) { |
a3db804c |
255 | if (sqimap_capability($imap_stream, "NAMESPACE")) { |
256 | /* According to something that I can't find, this is supposed to work on all systems |
257 | OS: This won't work in Courier IMAP. |
258 | OS: According to rfc2342 response from NAMESPACE command is: |
259 | OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
260 | OS: We want to lookup all personal NAMESPACES... |
261 | */ |
262 | fputs ($imap_stream, "a001 NAMESPACE\r\n"); |
74424a43 |
263 | $read = sqimap_read_data($imap_stream, 'a001', true, $a, $b); |
8e8ebd27 |
264 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
265 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) |
a3db804c |
266 | $pn = $data2[1]; |
267 | $pna = explode(')(', $pn); |
268 | while (list($k, $v) = each($pna)) |
269 | { |
862ff2d3 |
270 | $lst = explode('"', $v); |
271 | if (isset($lst[3])) { |
272 | $pn[$lst[1]] = $lst[3]; |
273 | } else { |
74424a43 |
274 | $pn[$lst[1]] = ''; |
862ff2d3 |
275 | } |
a3db804c |
276 | } |
a3db804c |
277 | } |
278 | $sqimap_delimiter = $pn[0]; |
40ba4d36 |
279 | } else { |
a3db804c |
280 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
74424a43 |
281 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
282 | $quote_position = strpos ($read[0], '"'); |
a3db804c |
283 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
40ba4d36 |
284 | } |
a3db804c |
285 | } |
286 | return $sqimap_delimiter; |
f435778e |
287 | } |
052e0c26 |
288 | |
289 | |
290 | /****************************************************************************** |
291 | ** Gets the number of messages in the current mailbox. |
292 | ******************************************************************************/ |
293 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
294 | fputs ($imap_stream, "a001 EXAMINE \"$mailbox\"\r\n"); |
74424a43 |
295 | $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message); |
052e0c26 |
296 | for ($i = 0; $i < count($read_ary); $i++) { |
441f2d33 |
297 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
298 | return $regs[1]; |
052e0c26 |
299 | } |
300 | } |
441f2d33 |
301 | return "BUG! Couldn't get number of messages in $mailbox!"; |
052e0c26 |
302 | } |
303 | |
304 | |
305 | /****************************************************************************** |
306 | ** Returns a displayable email address |
307 | ******************************************************************************/ |
308 | function sqimap_find_email ($string) { |
309 | /** Luke Ehresman <lehresma@css.tayloru.edu> |
310 | ** <lehresma@css.tayloru.edu> |
311 | ** lehresma@css.tayloru.edu |
1d2ce1c3 |
312 | ** |
313 | ** What about |
314 | ** lehresma@css.tayloru.edu (Luke Ehresman) |
052e0c26 |
315 | **/ |
316 | |
441f2d33 |
317 | if (ereg("<([^>]+)>", $string, $regs)) { |
318 | $string = $regs[1]; |
052e0c26 |
319 | } |
320 | return trim($string); |
321 | } |
322 | |
323 | |
324 | /****************************************************************************** |
325 | ** Takes the From: field, and creates a displayable name. |
326 | ** Luke Ehresman <lkehresman@yahoo.com> |
327 | ** becomes: Luke Ehresman |
328 | ** <lkehresman@yahoo.com> |
329 | ** becomes: lkehresman@yahoo.com |
330 | ******************************************************************************/ |
331 | function sqimap_find_displayable_name ($string) { |
74424a43 |
332 | $string = ' '.trim($string); |
1d2ce1c3 |
333 | $orig_string = $string; |
74424a43 |
334 | if (strpos($string, '<') && strpos($string, '>')) { |
335 | if (strpos($string, '<') == 1) { |
1d2ce1c3 |
336 | $string = sqimap_find_email($string); |
337 | } else { |
338 | $string = trim($string); |
74424a43 |
339 | $string = substr($string, 0, strpos($string, '<')); |
340 | $string = ereg_replace ('"', '', $string); |
1d2ce1c3 |
341 | } |
342 | |
74424a43 |
343 | if (trim($string) == '') { |
1d2ce1c3 |
344 | $string = sqimap_find_email($orig_string); |
345 | } |
346 | } |
347 | return $string; |
052e0c26 |
348 | } |
349 | |
350 | |
052e0c26 |
351 | /****************************************************************************** |
352 | ** Returns the number of unseen messages in this folder |
353 | ******************************************************************************/ |
4d2c01e3 |
354 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
c5eb2c03 |
355 | //fputs ($imap_stream, "a001 SEARCH UNSEEN NOT DELETED\r\n"); |
356 | fputs ($imap_stream, "a001 STATUS \"$mailbox\" (UNSEEN)\r\n"); |
74424a43 |
357 | $read_ary = sqimap_read_data ($imap_stream, 'a001', true, $result, $message); |
441f2d33 |
358 | ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs); |
359 | return $regs[1]; |
052e0c26 |
360 | } |
361 | |
362 | |
363 | /****************************************************************************** |
364 | ** Saves a message to a given folder -- used for saving sent messages |
365 | ******************************************************************************/ |
366 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
367 | fputs ($imap_stream, "a001 APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
368 | $tmp = fgets ($imap_stream, 1024); |
369 | } |
370 | |
371 | function sqimap_append_done ($imap_stream) { |
372 | fputs ($imap_stream, "\r\n"); |
373 | $tmp = fgets ($imap_stream, 1024); |
374 | } |
375 | ?> |