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