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