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