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