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