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