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