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