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