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