59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
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 | /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/ |
16 | /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/ |
17 | /*** + Base level indent should begin at left margin, as ***/ |
18 | /*** the function definition and comments start below. ***/ |
19 | /*** + All identation should consist of four space blocks ***/ |
20 | /*** + Tab characters are evil. ***/ |
21 | /*** + all comments should use "slash-star ... star-slash" ***/ |
22 | /*** style -- no pound characters, no slash-slash style ***/ |
23 | /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/ |
24 | /*** ALWAYS USE { AND } CHARACTERS!!! ***/ |
25 | /*** + Please use ' instead of ", when possible. Note " ***/ |
26 | /*** should always be used in _( ) function calls. ***/ |
27 | /*** Thank you for your help making the SM code more readable. ***/ |
28 | /*****************************************************************/ |
29 | |
30 | /** |
31 | * Unique SessionId |
32 | * |
33 | * Sets an unique session id in order to avoid simultanous sessions crash. |
34 | * |
35 | * @return string a 4 chars unique string |
36 | */ |
37 | function sqimap_session_id() { |
7350889b |
38 | |
71257f8b |
39 | global $data_dir, $username; |
40 | |
cb48c245 |
41 | $IMAPSessionID = substr(session_id(), -4); |
42 | if( $IMAPSessionID == '' ) { |
22edfb6f |
43 | $IMAPSessionID = 'A001'; |
cb48c245 |
44 | } |
7350889b |
45 | |
cb48c245 |
46 | return( $IMAPSessionID ); |
180aa6d7 |
47 | } |
48 | |
49 | |
052e0c26 |
50 | /****************************************************************************** |
51 | ** Reads the output from the IMAP stream. If handle_errors is set to true, |
52 | ** this will also handle all errors that are received. If it is not set, |
53 | ** the errors will be sent back through $response and $message |
54 | ******************************************************************************/ |
a3432f47 |
55 | |
91f68e94 |
56 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, |
f1e6f580 |
57 | &$response, &$message) { |
58 | global $color, $squirrelmail_language; |
245a6892 |
59 | |
f1e6f580 |
60 | $read = ''; |
91f68e94 |
61 | $resultlist = array(); |
7350889b |
62 | |
91f68e94 |
63 | $more_msgs = true; |
64 | while ($more_msgs) { |
65 | $data = array(); |
66 | $total_size = 0; |
d9ca5b40 |
67 | while (strpos($read, "\n") === false) { |
68 | $read .= fgets($imap_stream, 9096); |
69 | } |
4f5c1bcb |
70 | |
91f68e94 |
71 | if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { |
72 | $size = $regs[1]; |
73 | } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { |
74 | // Sizeless response, probably single-line |
55412053 |
75 | $size = -1; |
91f68e94 |
76 | $data[] = $read; |
77 | $read = fgets($imap_stream, 9096); |
d9ca5b40 |
78 | } else { |
55412053 |
79 | $size = -1; |
91f68e94 |
80 | } |
81 | while (1) { |
82 | while (strpos($read, "\n") === false) { |
83 | $read .= fgets($imap_stream, 9096); |
84 | } |
91f68e94 |
85 | // If we know the size, no need to look at the end parameters |
86 | if ($size > 0) { |
87 | if ($total_size == $size) { |
88 | // We've reached the end of this 'message', switch to the next one. |
89 | $data[] = $read; |
37e24380 |
90 | break; |
91f68e94 |
91 | } else if ($total_size > $size) { |
92 | $difference = $total_size - $size; |
93 | $total_size = $total_size - strlen($read); |
94 | $data[] = substr ($read, 0, strlen($read)-$difference); |
95 | $read = substr ($read, strlen($read)-$difference, strlen($read)); |
96 | break; |
97 | } else { |
98 | $data[] = $read; |
99 | $read = fgets($imap_stream, 9096); |
f1e6f580 |
100 | while (strpos($read, "\n") === false) { |
101 | $read .= fgets($imap_stream, 9096); |
102 | } |
91f68e94 |
103 | } |
104 | $total_size += strlen($read); |
4f5c1bcb |
105 | } else { |
91f68e94 |
106 | if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || |
55412053 |
107 | (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) { |
91f68e94 |
108 | break; |
109 | } else { |
110 | $data[] = $read; |
111 | $read = fgets ($imap_stream, 9096); |
112 | } |
4f5c1bcb |
113 | } |
d9ca5b40 |
114 | } |
a3db804c |
115 | |
91f68e94 |
116 | while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) && |
117 | !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) { |
118 | $read = fgets($imap_stream, 9096); |
119 | } |
120 | $resultlist[] = $data; |
121 | } |
3d2504a1 |
122 | $response = $regs[1]; |
123 | $message = trim($regs[2]); |
124 | |
f7b1b3b1 |
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) { |
cb48c245 |
160 | |
bc104ef3 |
161 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
1b187352 |
162 | |
cb48c245 |
163 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, |
164 | $error_number, $error_string, 15); |
052e0c26 |
165 | $server_info = fgets ($imap_stream, 1024); |
cb48c245 |
166 | |
52eefafc |
167 | // Decrypt the password |
bc104ef3 |
168 | $password = OneTimePadDecrypt($password, $onetimepad); |
52eefafc |
169 | |
052e0c26 |
170 | /** Do some error correction **/ |
171 | if (!$imap_stream) { |
172 | if (!$hide) { |
441f2d33 |
173 | set_up_language($squirrelmail_language, true); |
1b187352 |
174 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
052e0c26 |
175 | echo "$error_number : $error_string<br>\r\n"; |
176 | } |
177 | exit; |
178 | } |
179 | |
f1e6f580 |
180 | fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) . |
704db5b2 |
181 | '" "' . quoteIMAP($password) . "\"\r\n"); |
180aa6d7 |
182 | $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message); |
052e0c26 |
183 | |
184 | /** If the connection was not successful, lets see why **/ |
3d2504a1 |
185 | if ($response != "OK") { |
052e0c26 |
186 | if (!$hide) { |
74424a43 |
187 | if ($response != 'NO') { |
3d2504a1 |
188 | // "BAD" and anything else gets reported here. |
441f2d33 |
189 | set_up_language($squirrelmail_language, true); |
74424a43 |
190 | if ($response == 'BAD') |
3d2504a1 |
191 | printf (_("Bad request: %s")."<br>\r\n", $message); |
192 | else |
193 | printf (_("Unknown error: %s") . "<br>\n", $message); |
74424a43 |
194 | echo '<br>'; |
3d2504a1 |
195 | echo _("Read data:") . "<br>\n"; |
f1e6f580 |
196 | if (is_array($read)) |
197 | { |
198 | foreach ($read as $line) |
199 | { |
200 | echo htmlspecialchars($line) . "<br>\n"; |
201 | } |
3d2504a1 |
202 | } |
052e0c26 |
203 | exit; |
165e24a7 |
204 | } else { |
1b187352 |
205 | // If the user does not log in with the correct |
206 | // username and password it is not possible to get the |
207 | // correct locale from the user's preferences. |
208 | // Therefore, apply the same hack as on the login |
209 | // screen. |
210 | |
211 | // $squirrelmail_language is set by a cookie when |
212 | // the user selects language and logs out |
f1e6f580 |
213 | |
441f2d33 |
214 | set_up_language($squirrelmail_language, true); |
f1e6f580 |
215 | |
052e0c26 |
216 | ?> |
217 | <html> |
74424a43 |
218 | <body bgcolor="ffffff"> |
052e0c26 |
219 | <br> |
220 | <center> |
74424a43 |
221 | <table width="70%" noborder bgcolor="ffffff" align="center"> |
052e0c26 |
222 | <tr> |
74424a43 |
223 | <td bgcolor="dcdcdc"> |
224 | <font color="cc0000"> |
052e0c26 |
225 | <center> |
59177427 |
226 | <?php echo _("ERROR") ?> |
052e0c26 |
227 | </center> |
228 | </font> |
229 | </td> |
230 | </tr> |
231 | <tr> |
232 | <td> |
233 | <center> |
59177427 |
234 | <?php echo _("Unknown user or password incorrect.") ?><br> |
5630c7ec |
235 | <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a> |
052e0c26 |
236 | </center> |
237 | </td> |
238 | </tr> |
239 | </table> |
240 | </center> |
241 | </body> |
242 | </html> |
59177427 |
243 | <?php |
052e0c26 |
244 | session_destroy(); |
245 | exit; |
052e0c26 |
246 | } |
247 | } else { |
248 | exit; |
249 | } |
250 | } |
251 | |
252 | return $imap_stream; |
253 | } |
254 | |
255 | |
256 | |
257 | |
258 | /****************************************************************************** |
259 | ** Simply logs out the imap session |
260 | ******************************************************************************/ |
261 | function sqimap_logout ($imap_stream) { |
180aa6d7 |
262 | fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n"); |
052e0c26 |
263 | } |
264 | |
f435778e |
265 | function sqimap_capability($imap_stream, $capability) { |
f1e6f580 |
266 | global $sqimap_capabilities; |
f1e6f580 |
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) { |
7350889b |
290 | |
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 | ?> |