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