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; |
90 | case preg_match('/^\* [0-9]+ FETCH.*/', $read): |
91 | $fetch_data = array(); |
92 | $fetch_data[] = $read; |
93 | $read = sqimap_fgets($imap_stream); |
94 | while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) && |
95 | !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) { |
96 | $fetch_data[] = $read; |
97 | $read = sqimap_fgets($imap_stream); |
9c737111 |
98 | } |
b8c285ab |
99 | $resultlist[] = $fetch_data; |
100 | break 1; |
101 | default: |
102 | $data[] = $read; |
103 | $read = sqimap_fgets($imap_stream); |
104 | break 1; |
9c737111 |
105 | } |
b8c285ab |
106 | } |
107 | if (!empty($data)) { |
9c737111 |
108 | $resultlist[] = $data; |
109 | } |
b8c285ab |
110 | elseif (empty($resultlist)) { |
111 | $resultlist[] = array(); |
112 | } |
bee165ef |
113 | if ($handle_errors == false) { |
114 | return( $resultlist ); |
b8c285ab |
115 | } |
116 | elseif ($response == 'NO') { |
117 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
9c737111 |
118 | if (strstr($message, 'command resulted in') === false) { |
441f2d33 |
119 | set_up_language($squirrelmail_language); |
9c737111 |
120 | echo "<br><b><font color=$color[2]>\n" . |
b8c285ab |
121 | _("ERROR : Could not complete request.") . |
122 | "</b><br>\n" . |
123 | _("Query:") . |
124 | $query . '<br>' . |
125 | _("Reason Given: ") . |
126 | $message . "</font><br>\n"; |
052e0c26 |
127 | exit; |
9c737111 |
128 | } |
b8c285ab |
129 | } |
130 | elseif ($response == 'BAD') { |
9c737111 |
131 | set_up_language($squirrelmail_language); |
132 | echo "<br><b><font color=$color[2]>\n" . |
b8c285ab |
133 | _("ERROR : Bad or malformed request.") . |
134 | "</b><br>\n" . |
135 | _("Query:") . |
136 | $query . '<br>' . |
137 | _("Server responded: ") . |
138 | $message . "</font><br>\n"; |
9c737111 |
139 | exit; |
b8c285ab |
140 | } |
141 | else { |
3411d4ec |
142 | return $resultlist; |
9c737111 |
143 | } |
9c737111 |
144 | } |
145 | |
b3837b0f |
146 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
b3837b0f |
147 | $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query); |
9c737111 |
148 | return $res[0]; |
9c737111 |
149 | } |
150 | |
3411d4ec |
151 | /* |
152 | * Logs the user into the imap server. If $hide is set, no error messages |
153 | * will be displayed. This function returns the imap connection handle. |
154 | */ |
9c737111 |
155 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
9c737111 |
156 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
85fc999e |
157 | |
bd9829d7 |
158 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
159 | |
3411d4ec |
160 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15); |
9c737111 |
161 | if ( !$imap_stream ) { |
3411d4ec |
162 | return false; |
9c737111 |
163 | } |
164 | $server_info = fgets ($imap_stream, 1024); |
85fc999e |
165 | |
3411d4ec |
166 | /* Decrypt the password */ |
9c737111 |
167 | $password = OneTimePadDecrypt($password, $onetimepad); |
85fc999e |
168 | |
3411d4ec |
169 | /* Do some error correction */ |
9c737111 |
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"; |
9c737111 |
175 | } |
176 | exit; |
177 | } |
052e0c26 |
178 | |
1c72b151 |
179 | $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"'; |
180 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
052e0c26 |
181 | |
3411d4ec |
182 | /* If the connection was not successful, lets see why */ |
9c737111 |
183 | if ($response != 'OK') { |
184 | if (!$hide) { |
74424a43 |
185 | if ($response != 'NO') { |
3411d4ec |
186 | /* "BAD" and anything else gets reported here. */ |
9c737111 |
187 | set_up_language($squirrelmail_language, true); |
188 | if ($response == 'BAD') { |
3d2504a1 |
189 | printf (_("Bad request: %s")."<br>\r\n", $message); |
9c737111 |
190 | } else { |
3d2504a1 |
191 | printf (_("Unknown error: %s") . "<br>\n", $message); |
9c737111 |
192 | } |
193 | echo '<br>' . _("Read data:") . "<br>\n"; |
194 | if (is_array($read)) { |
195 | foreach ($read as $line) { |
196 | echo htmlspecialchars($line) . "<br>\n"; |
197 | } |
198 | } |
199 | exit; |
165e24a7 |
200 | } else { |
3411d4ec |
201 | /* |
202 | * If the user does not log in with the correct |
1c72b151 |
203 | * username and password it is not possible to get the |
204 | * correct locale from the user's preferences. |
205 | * Therefore, apply the same hack as on the login |
206 | * screen. |
3411d4ec |
207 | * |
208 | * $squirrelmail_language is set by a cookie when |
1c72b151 |
209 | * the user selects language and logs out |
bee165ef |
210 | */ |
9be8198d |
211 | |
9c737111 |
212 | set_up_language($squirrelmail_language, true); |
9be8198d |
213 | include_once( '../functions/display_messages.php' ); |
214 | logout_error( _("Unknown user or password incorrect.") ); |
9c737111 |
215 | session_destroy(); |
216 | exit; |
052e0c26 |
217 | } |
9c737111 |
218 | } else { |
052e0c26 |
219 | exit; |
9c737111 |
220 | } |
221 | } |
9c737111 |
222 | return $imap_stream; |
223 | } |
f1e6f580 |
224 | |
3411d4ec |
225 | /* Simply logs out the IMAP session */ |
9c737111 |
226 | function sqimap_logout ($imap_stream) { |
1c72b151 |
227 | /* Logout is not valid until the server returns 'BYE' */ |
228 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
9c737111 |
229 | } |
230 | |
487daa81 |
231 | function sqimap_capability($imap_stream, $capability='') { |
9c737111 |
232 | global $sqimap_capabilities; |
9c737111 |
233 | if (!is_array($sqimap_capabilities)) { |
1c72b151 |
234 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
235 | |
9c737111 |
236 | $c = explode(' ', $read[0]); |
237 | for ($i=2; $i < count($c); $i++) { |
238 | $cap_list = explode('=', $c[$i]); |
3411d4ec |
239 | if (isset($cap_list[1])) { |
9c737111 |
240 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
3411d4ec |
241 | } else { |
9c737111 |
242 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
3411d4ec |
243 | } |
f1e6f580 |
244 | } |
9c737111 |
245 | } |
487daa81 |
246 | if ($capability) { |
247 | if (isset($sqimap_capabilities[$capability])) { |
248 | return $sqimap_capabilities[$capability]; |
249 | } else { |
250 | return false; |
251 | } |
f1e6f580 |
252 | } |
487daa81 |
253 | return $sqimap_capabilities; |
9c737111 |
254 | } |
255 | |
3411d4ec |
256 | /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */ |
9c737111 |
257 | function sqimap_get_delimiter ($imap_stream = false) { |
3411d4ec |
258 | global $sqimap_delimiter, $optional_delimiter; |
85fc999e |
259 | |
9c737111 |
260 | /* Use configured delimiter if set */ |
261 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
262 | return $optional_delimiter; |
263 | } |
85fc999e |
264 | |
9c737111 |
265 | /* Do some caching here */ |
266 | if (!$sqimap_delimiter) { |
267 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
3411d4ec |
268 | /* |
269 | * According to something that I can't find, this is supposed to work on all systems |
270 | * OS: This won't work in Courier IMAP. |
271 | * OS: According to rfc2342 response from NAMESPACE command is: |
272 | * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
273 | * OS: We want to lookup all personal NAMESPACES... |
274 | */ |
1c72b151 |
275 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
f1e6f580 |
276 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
9c737111 |
277 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
f1e6f580 |
278 | $pn = $data2[1]; |
9c737111 |
279 | } |
f1e6f580 |
280 | $pna = explode(')(', $pn); |
9c737111 |
281 | while (list($k, $v) = each($pna)) { |
862ff2d3 |
282 | $lst = explode('"', $v); |
283 | if (isset($lst[3])) { |
284 | $pn[$lst[1]] = $lst[3]; |
285 | } else { |
74424a43 |
286 | $pn[$lst[1]] = ''; |
862ff2d3 |
287 | } |
f1e6f580 |
288 | } |
289 | } |
290 | $sqimap_delimiter = $pn[0]; |
291 | } else { |
292 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
293 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
294 | $quote_position = strpos ($read[0], '"'); |
295 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
296 | } |
297 | } |
298 | return $sqimap_delimiter; |
9c737111 |
299 | } |
052e0c26 |
300 | |
301 | |
3411d4ec |
302 | /* Gets the number of messages in the current mailbox. */ |
9c737111 |
303 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
41600f7d |
304 | $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message); |
9c737111 |
305 | for ($i = 0; $i < count($read_ary); $i++) { |
85fc999e |
306 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
307 | return $regs[1]; |
308 | } |
9c737111 |
309 | } |
3411d4ec |
310 | return "BUG! Couldn't get number of messages in $mailbox!"; |
9c737111 |
311 | } |
312 | |
313 | |
3411d4ec |
314 | /* Returns a displayable email address. |
315 | * Luke Ehresman <lehresma@css.tayloru.edu> |
316 | * "Luke Ehresman" <lehresma@css.tayloru.edu> |
317 | * <lehresma@css.tayloru.edu> |
318 | * lehresma@css.tayloru.edu (Luke Ehresman) |
319 | * lehresma@css.tayloru.edu |
320 | * becomes: lehresma@css.tayloru.edu |
321 | */ |
9c737111 |
322 | function sqimap_find_email ($string) { |
9c737111 |
323 | if (ereg("<([^>]+)>", $string, $regs)) { |
324 | $string = $regs[1]; |
3411d4ec |
325 | } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) { |
326 | $string = $regs[1]; |
9c737111 |
327 | } |
328 | return trim($string); |
329 | } |
330 | |
331 | |
332 | /* |
3411d4ec |
333 | * Takes the From: field and creates a displayable name. |
334 | * Luke Ehresman <lkehresman@yahoo.com> |
335 | * "Luke Ehresman" <lkehresman@yahoo.com> |
336 | * lkehresman@yahoo.com (Luke Ehresman) |
337 | * becomes: Luke Ehresman |
338 | * <lkehresman@yahoo.com> |
339 | * becomes: lkehresman@yahoo.com |
6282af09 |
340 | */ |
9c737111 |
341 | function sqimap_find_displayable_name ($string) { |
6282af09 |
342 | $string = trim($string); |
2f7bda0a |
343 | |
7e3de682 |
344 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
092d4f2c |
345 | $orig_string = $string; |
2f7bda0a |
346 | $string = str_replace ('"', '', $regs[1] ); |
092d4f2c |
347 | if (trim($string) == '') { |
348 | $string = sqimap_find_email($orig_string); |
349 | } |
33565ec4 |
350 | if( $string == '' || $string == ' ' ){ |
351 | $string = ' '; |
352 | } |
7e3de682 |
353 | } |
354 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
9e9c63e4 |
355 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
356 | if ( ereg('^(.+) \(', $string, $regs) ) { |
357 | $string = ereg_replace( ' \(\)$', '', $string ); |
358 | } else { |
359 | $string = ' '; |
360 | } |
361 | } else { |
362 | $string = $regs[1]; |
363 | } |
7e3de682 |
364 | } |
365 | else { |
2f7bda0a |
366 | $string = str_replace ('"', '', sqimap_find_email($string)); |
9c737111 |
367 | } |
9c737111 |
368 | |
7e3de682 |
369 | return trim($string); |
370 | } |
85fc999e |
371 | |
9c737111 |
372 | /* |
3411d4ec |
373 | * Returns the number of unseen messages in this folder |
374 | */ |
9c737111 |
375 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
1a16af11 |
376 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message); |
ea7ff111 |
377 | $i = 0; |
378 | while (isset($read_ary[$i])) { |
379 | if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) { |
380 | break; |
381 | } |
382 | $i++; |
383 | } |
9c737111 |
384 | return $regs[1]; |
385 | } |
386 | |
387 | |
388 | /* |
3411d4ec |
389 | * Saves a message to a given folder -- used for saving sent messages |
390 | */ |
9c737111 |
391 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
392 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
85fc999e |
393 | $tmp = fgets ($imap_stream, 1024); |
9c737111 |
394 | } |
395 | |
396 | function sqimap_append_done ($imap_stream) { |
397 | fputs ($imap_stream, "\r\n"); |
398 | $tmp = fgets ($imap_stream, 1024); |
399 | } |
85fc999e |
400 | |
bd9829d7 |
401 | function sqimap_get_user_server ($imap_server, $username) { |
402 | |
403 | if (substr($imap_server, 0, 4) != "map:") { |
404 | return $imap_server; |
405 | } |
406 | |
407 | $function = substr($imap_server, 4); |
408 | return $function($username); |
409 | } |
410 | |
411 | /* This is an example that gets imapservers from yellowpages (NIS). |
412 | * you can simple put map:map_yp_alias in your $imap_server_address |
413 | * in config.php use your own function instead map_yp_alias to map your |
414 | * LDAP whatever way to find the users imapserver. */ |
415 | |
416 | function map_yp_alias($username) { |
417 | $yp = `ypmatch $username aliases`; |
418 | return chop(substr($yp, strlen($username)+1)); |
419 | } |
420 | |
15e6162e |
421 | ?> |