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