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