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