59177427 |
1 | <?php |
bccadd02 |
2 | |
35586184 |
3 | /** |
a6fd80f5 |
4 | * imap_general.php |
35586184 |
5 | * |
15e6162e |
6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
35586184 |
8 | * |
15e6162e |
9 | * This implements all functions that do general imap functions. |
35586184 |
10 | * |
15e6162e |
11 | * $Id$ |
35586184 |
12 | */ |
13 | |
9c737111 |
14 | require_once('../functions/page_header.php'); |
35586184 |
15 | |
41600f7d |
16 | global $sqimap_session_id; |
17 | $sqimap_session_id = 1; |
71257f8b |
18 | |
3411d4ec |
19 | /* Sets an unique session id in order to avoid simultanous sessions crash. */ |
487daa81 |
20 | function sqimap_session_id($unique_id = false) { |
41600f7d |
21 | global $data_dir, $username, $sqimap_session_id; |
487daa81 |
22 | if (!$unique_id) { |
23 | return( sprintf("A%03d", $sqimap_session_id++) ); |
24 | } else { |
25 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
26 | } |
9c737111 |
27 | } |
28 | |
3411d4ec |
29 | /* |
30 | * Both send a command and accept the result from the command. |
31 | * This is to allow proper session number handling. |
32 | */ |
487daa81 |
33 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
34 | $sid = sqimap_session_id($unique_id); |
41600f7d |
35 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
b3837b0f |
36 | $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query ); |
1c72b151 |
37 | return $read; |
38 | } |
39 | |
487daa81 |
40 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
41 | $sid = sqimap_session_id($unique_id); |
41600f7d |
42 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
b3837b0f |
43 | $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query); |
42a07ac1 |
44 | return $read; |
45 | } |
46 | |
9c737111 |
47 | |
b8c285ab |
48 | /* |
49 | * custom fgets function. gets a line from IMAP |
50 | * no matter how big it may be |
51 | */ |
52 | |
53 | function sqimap_fgets($imap_stream) { |
54 | $read = ''; |
55 | $buffer = 4096; |
56 | $results = ''; |
57 | while (strpos($read, "\n") === false) { |
58 | if (!($read = fgets($imap_stream, $buffer))) { |
59 | break; |
60 | } |
61 | $results .= $read; |
62 | } |
63 | return $results; |
64 | } |
65 | |
bee165ef |
66 | /* |
3411d4ec |
67 | * Reads the output from the IMAP stream. If handle_errors is set to true, |
68 | * this will also handle all errors that are received. If it is not set, |
69 | * the errors will be sent back through $response and $message |
bee165ef |
70 | */ |
b8c285ab |
71 | |
3411d4ec |
72 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
9c737111 |
73 | global $color, $squirrelmail_language; |
9c737111 |
74 | $read = ''; |
487daa81 |
75 | $pre_a = explode(' ',trim($pre)); |
76 | $pre = $pre_a[0]; |
b8c285ab |
77 | $resultlist = array(); |
78 | $data = array(); |
79 | $read = sqimap_fgets($imap_stream); |
80 | while (1) { |
81 | switch (true) { |
82 | case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs): |
83 | case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs): |
84 | $response = $regs[1]; |
85 | $message = trim($regs[2]); |
86 | break 2; |
87 | case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read): |
88 | $read = sqimap_fgets($imap_stream); |
89 | break 1; |
56afb33f |
90 | case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs): |
b8c285ab |
91 | $fetch_data = array(); |
92 | $fetch_data[] = $read; |
93 | $read = sqimap_fgets($imap_stream); |
56afb33f |
94 | $i = 0; |
b8c285ab |
95 | while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) && |
96 | !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) { |
97 | $fetch_data[] = $read; |
98 | $read = sqimap_fgets($imap_stream); |
9c737111 |
99 | } |
b8c285ab |
100 | $resultlist[] = $fetch_data; |
101 | break 1; |
102 | default: |
103 | $data[] = $read; |
104 | $read = sqimap_fgets($imap_stream); |
105 | break 1; |
9c737111 |
106 | } |
b8c285ab |
107 | } |
108 | if (!empty($data)) { |
9c737111 |
109 | $resultlist[] = $data; |
110 | } |
b8c285ab |
111 | elseif (empty($resultlist)) { |
112 | $resultlist[] = array(); |
113 | } |
bee165ef |
114 | if ($handle_errors == false) { |
115 | return( $resultlist ); |
b8c285ab |
116 | } |
117 | elseif ($response == 'NO') { |
118 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
9c737111 |
119 | if (strstr($message, 'command resulted in') === false) { |
441f2d33 |
120 | set_up_language($squirrelmail_language); |
9c737111 |
121 | echo "<br><b><font color=$color[2]>\n" . |
b8c285ab |
122 | _("ERROR : Could not complete request.") . |
123 | "</b><br>\n" . |
124 | _("Query:") . |
125 | $query . '<br>' . |
126 | _("Reason Given: ") . |
127 | $message . "</font><br>\n"; |
052e0c26 |
128 | exit; |
9c737111 |
129 | } |
b8c285ab |
130 | } |
131 | elseif ($response == 'BAD') { |
9c737111 |
132 | set_up_language($squirrelmail_language); |
133 | echo "<br><b><font color=$color[2]>\n" . |
b8c285ab |
134 | _("ERROR : Bad or malformed request.") . |
135 | "</b><br>\n" . |
136 | _("Query:") . |
137 | $query . '<br>' . |
138 | _("Server responded: ") . |
139 | $message . "</font><br>\n"; |
9c737111 |
140 | exit; |
b8c285ab |
141 | } |
142 | else { |
3411d4ec |
143 | return $resultlist; |
9c737111 |
144 | } |
9c737111 |
145 | } |
146 | |
b3837b0f |
147 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
56afb33f |
148 | global $color, $squirrelmail_language; |
149 | $read = ''; |
150 | $pre_a = explode(' ',trim($pre)); |
151 | $pre = $pre_a[0]; |
152 | $result = ''; |
153 | $data = array(); |
154 | $read = sqimap_fgets($imap_stream); |
155 | while (1) { |
156 | switch (true) { |
157 | case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs): |
158 | case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs): |
159 | $response = $regs[1]; |
160 | $message = trim($regs[2]); |
161 | break 2; |
162 | case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read): |
163 | $read = sqimap_fgets($imap_stream); |
164 | break 1; |
165 | case preg_match('/^\* [0-9]+ FETCH.*/', $read): |
166 | $fetch_data = array(); |
167 | $fetch_data[] = $read; |
168 | $read = sqimap_fgets($imap_stream); |
169 | while (!preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) { |
170 | $fetch_data[] = $read; |
171 | $read = sqimap_fgets($imap_stream); |
172 | } |
173 | $result = $fetch_data; |
174 | break 2; |
175 | default: |
176 | $data[] = $read; |
177 | $read = sqimap_fgets($imap_stream); |
178 | break 1; |
179 | } |
180 | } |
181 | if (!empty($data)) { |
182 | $result = $data; |
183 | } |
184 | if ($handle_errors == false) { |
185 | return( $result ); |
186 | } |
187 | elseif ($response == 'NO') { |
188 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
189 | if (strstr($message, 'command resulted in') === false) { |
190 | set_up_language($squirrelmail_language); |
191 | echo "<br><b><font color=$color[2]>\n" . |
192 | _("ERROR : Could not complete request.") . |
193 | "</b><br>\n" . |
194 | _("Query:") . |
195 | $query . '<br>' . |
196 | _("Reason Given: ") . |
197 | $message . "</font><br>\n"; |
198 | exit; |
199 | } |
200 | } |
201 | elseif ($response == 'BAD') { |
202 | set_up_language($squirrelmail_language); |
203 | echo "<br><b><font color=$color[2]>\n" . |
204 | _("ERROR : Bad or malformed request.") . |
205 | "</b><br>\n" . |
206 | _("Query:") . |
207 | $query . '<br>' . |
208 | _("Server responded: ") . |
209 | $message . "</font><br>\n"; |
210 | exit; |
211 | } |
212 | else { |
213 | return $result; |
214 | } |
215 | |
9c737111 |
216 | } |
217 | |
3411d4ec |
218 | /* |
219 | * Logs the user into the imap server. If $hide is set, no error messages |
220 | * will be displayed. This function returns the imap connection handle. |
221 | */ |
9c737111 |
222 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
9c737111 |
223 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
85fc999e |
224 | |
bd9829d7 |
225 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
226 | |
3411d4ec |
227 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15); |
9c737111 |
228 | if ( !$imap_stream ) { |
3411d4ec |
229 | return false; |
9c737111 |
230 | } |
231 | $server_info = fgets ($imap_stream, 1024); |
85fc999e |
232 | |
3411d4ec |
233 | /* Decrypt the password */ |
9c737111 |
234 | $password = OneTimePadDecrypt($password, $onetimepad); |
85fc999e |
235 | |
3411d4ec |
236 | /* Do some error correction */ |
9c737111 |
237 | if (!$imap_stream) { |
238 | if (!$hide) { |
441f2d33 |
239 | set_up_language($squirrelmail_language, true); |
1b187352 |
240 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
052e0c26 |
241 | echo "$error_number : $error_string<br>\r\n"; |
9c737111 |
242 | } |
243 | exit; |
244 | } |
052e0c26 |
245 | |
1c72b151 |
246 | $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"'; |
247 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
052e0c26 |
248 | |
3411d4ec |
249 | /* If the connection was not successful, lets see why */ |
9c737111 |
250 | if ($response != 'OK') { |
251 | if (!$hide) { |
74424a43 |
252 | if ($response != 'NO') { |
3411d4ec |
253 | /* "BAD" and anything else gets reported here. */ |
9c737111 |
254 | set_up_language($squirrelmail_language, true); |
255 | if ($response == 'BAD') { |
3d2504a1 |
256 | printf (_("Bad request: %s")."<br>\r\n", $message); |
9c737111 |
257 | } else { |
3d2504a1 |
258 | printf (_("Unknown error: %s") . "<br>\n", $message); |
9c737111 |
259 | } |
260 | echo '<br>' . _("Read data:") . "<br>\n"; |
261 | if (is_array($read)) { |
262 | foreach ($read as $line) { |
263 | echo htmlspecialchars($line) . "<br>\n"; |
264 | } |
265 | } |
266 | exit; |
165e24a7 |
267 | } else { |
3411d4ec |
268 | /* |
269 | * If the user does not log in with the correct |
1c72b151 |
270 | * username and password it is not possible to get the |
271 | * correct locale from the user's preferences. |
272 | * Therefore, apply the same hack as on the login |
273 | * screen. |
3411d4ec |
274 | * |
275 | * $squirrelmail_language is set by a cookie when |
1c72b151 |
276 | * the user selects language and logs out |
bee165ef |
277 | */ |
9be8198d |
278 | |
9c737111 |
279 | set_up_language($squirrelmail_language, true); |
9be8198d |
280 | include_once( '../functions/display_messages.php' ); |
281 | logout_error( _("Unknown user or password incorrect.") ); |
9c737111 |
282 | session_destroy(); |
283 | exit; |
052e0c26 |
284 | } |
9c737111 |
285 | } else { |
052e0c26 |
286 | exit; |
9c737111 |
287 | } |
288 | } |
9c737111 |
289 | return $imap_stream; |
290 | } |
f1e6f580 |
291 | |
3411d4ec |
292 | /* Simply logs out the IMAP session */ |
9c737111 |
293 | function sqimap_logout ($imap_stream) { |
1c72b151 |
294 | /* Logout is not valid until the server returns 'BYE' */ |
295 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
9c737111 |
296 | } |
297 | |
487daa81 |
298 | function sqimap_capability($imap_stream, $capability='') { |
9c737111 |
299 | global $sqimap_capabilities; |
9c737111 |
300 | if (!is_array($sqimap_capabilities)) { |
1c72b151 |
301 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
302 | |
9c737111 |
303 | $c = explode(' ', $read[0]); |
304 | for ($i=2; $i < count($c); $i++) { |
305 | $cap_list = explode('=', $c[$i]); |
3411d4ec |
306 | if (isset($cap_list[1])) { |
9c737111 |
307 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
3411d4ec |
308 | } else { |
9c737111 |
309 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
3411d4ec |
310 | } |
f1e6f580 |
311 | } |
9c737111 |
312 | } |
487daa81 |
313 | if ($capability) { |
314 | if (isset($sqimap_capabilities[$capability])) { |
315 | return $sqimap_capabilities[$capability]; |
316 | } else { |
317 | return false; |
318 | } |
f1e6f580 |
319 | } |
487daa81 |
320 | return $sqimap_capabilities; |
9c737111 |
321 | } |
322 | |
3411d4ec |
323 | /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */ |
9c737111 |
324 | function sqimap_get_delimiter ($imap_stream = false) { |
3411d4ec |
325 | global $sqimap_delimiter, $optional_delimiter; |
85fc999e |
326 | |
9c737111 |
327 | /* Use configured delimiter if set */ |
328 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
329 | return $optional_delimiter; |
330 | } |
85fc999e |
331 | |
9c737111 |
332 | /* Do some caching here */ |
333 | if (!$sqimap_delimiter) { |
334 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
3411d4ec |
335 | /* |
336 | * According to something that I can't find, this is supposed to work on all systems |
337 | * OS: This won't work in Courier IMAP. |
338 | * OS: According to rfc2342 response from NAMESPACE command is: |
339 | * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
340 | * OS: We want to lookup all personal NAMESPACES... |
341 | */ |
1c72b151 |
342 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
f1e6f580 |
343 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
9c737111 |
344 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
f1e6f580 |
345 | $pn = $data2[1]; |
9c737111 |
346 | } |
f1e6f580 |
347 | $pna = explode(')(', $pn); |
9c737111 |
348 | while (list($k, $v) = each($pna)) { |
862ff2d3 |
349 | $lst = explode('"', $v); |
350 | if (isset($lst[3])) { |
351 | $pn[$lst[1]] = $lst[3]; |
352 | } else { |
74424a43 |
353 | $pn[$lst[1]] = ''; |
862ff2d3 |
354 | } |
f1e6f580 |
355 | } |
356 | } |
357 | $sqimap_delimiter = $pn[0]; |
358 | } else { |
359 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
360 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
361 | $quote_position = strpos ($read[0], '"'); |
362 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
363 | } |
364 | } |
365 | return $sqimap_delimiter; |
9c737111 |
366 | } |
052e0c26 |
367 | |
368 | |
3411d4ec |
369 | /* Gets the number of messages in the current mailbox. */ |
9c737111 |
370 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
41600f7d |
371 | $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message); |
9c737111 |
372 | for ($i = 0; $i < count($read_ary); $i++) { |
85fc999e |
373 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
374 | return $regs[1]; |
375 | } |
9c737111 |
376 | } |
3411d4ec |
377 | return "BUG! Couldn't get number of messages in $mailbox!"; |
9c737111 |
378 | } |
379 | |
380 | |
3411d4ec |
381 | /* Returns a displayable email address. |
382 | * Luke Ehresman <lehresma@css.tayloru.edu> |
383 | * "Luke Ehresman" <lehresma@css.tayloru.edu> |
384 | * <lehresma@css.tayloru.edu> |
385 | * lehresma@css.tayloru.edu (Luke Ehresman) |
386 | * lehresma@css.tayloru.edu |
387 | * becomes: lehresma@css.tayloru.edu |
388 | */ |
9c737111 |
389 | function sqimap_find_email ($string) { |
9c737111 |
390 | if (ereg("<([^>]+)>", $string, $regs)) { |
391 | $string = $regs[1]; |
3411d4ec |
392 | } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) { |
393 | $string = $regs[1]; |
9c737111 |
394 | } |
395 | return trim($string); |
396 | } |
397 | |
398 | |
399 | /* |
3411d4ec |
400 | * Takes the From: field and creates a displayable name. |
401 | * Luke Ehresman <lkehresman@yahoo.com> |
402 | * "Luke Ehresman" <lkehresman@yahoo.com> |
403 | * lkehresman@yahoo.com (Luke Ehresman) |
404 | * becomes: Luke Ehresman |
405 | * <lkehresman@yahoo.com> |
406 | * becomes: lkehresman@yahoo.com |
6282af09 |
407 | */ |
9c737111 |
408 | function sqimap_find_displayable_name ($string) { |
6282af09 |
409 | $string = trim($string); |
2f7bda0a |
410 | |
7e3de682 |
411 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
092d4f2c |
412 | $orig_string = $string; |
2f7bda0a |
413 | $string = str_replace ('"', '', $regs[1] ); |
092d4f2c |
414 | if (trim($string) == '') { |
415 | $string = sqimap_find_email($orig_string); |
416 | } |
33565ec4 |
417 | if( $string == '' || $string == ' ' ){ |
418 | $string = ' '; |
419 | } |
7e3de682 |
420 | } |
421 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
9e9c63e4 |
422 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
423 | if ( ereg('^(.+) \(', $string, $regs) ) { |
424 | $string = ereg_replace( ' \(\)$', '', $string ); |
425 | } else { |
426 | $string = ' '; |
427 | } |
428 | } else { |
429 | $string = $regs[1]; |
430 | } |
7e3de682 |
431 | } |
432 | else { |
2f7bda0a |
433 | $string = str_replace ('"', '', sqimap_find_email($string)); |
9c737111 |
434 | } |
9c737111 |
435 | |
7e3de682 |
436 | return trim($string); |
437 | } |
85fc999e |
438 | |
9c737111 |
439 | /* |
3411d4ec |
440 | * Returns the number of unseen messages in this folder |
441 | */ |
9c737111 |
442 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
1a16af11 |
443 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message); |
ea7ff111 |
444 | $i = 0; |
445 | while (isset($read_ary[$i])) { |
446 | if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) { |
447 | break; |
448 | } |
449 | $i++; |
450 | } |
9c737111 |
451 | return $regs[1]; |
452 | } |
453 | |
454 | |
455 | /* |
3411d4ec |
456 | * Saves a message to a given folder -- used for saving sent messages |
457 | */ |
9c737111 |
458 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
459 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
85fc999e |
460 | $tmp = fgets ($imap_stream, 1024); |
9c737111 |
461 | } |
462 | |
463 | function sqimap_append_done ($imap_stream) { |
464 | fputs ($imap_stream, "\r\n"); |
465 | $tmp = fgets ($imap_stream, 1024); |
466 | } |
85fc999e |
467 | |
bd9829d7 |
468 | function sqimap_get_user_server ($imap_server, $username) { |
469 | |
470 | if (substr($imap_server, 0, 4) != "map:") { |
471 | return $imap_server; |
472 | } |
473 | |
474 | $function = substr($imap_server, 4); |
475 | return $function($username); |
476 | } |
477 | |
478 | /* This is an example that gets imapservers from yellowpages (NIS). |
479 | * you can simple put map:map_yp_alias in your $imap_server_address |
480 | * in config.php use your own function instead map_yp_alias to map your |
481 | * LDAP whatever way to find the users imapserver. */ |
482 | |
483 | function map_yp_alias($username) { |
484 | $yp = `ypmatch $username aliases`; |
485 | return chop(substr($yp, strlen($username)+1)); |
486 | } |
487 | |
15e6162e |
488 | ?> |