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