Finally fixed the IE/SSL download problem.
[squirrelmail.git] / functions / imap_general.php
1 <?php
2 /**
3 ** imap.php
4 **
5 ** This implements all functions that do general imap functions.
6 **/
7
8 $imap_general_debug = false;
9 //$imap_general_debug = false;
10
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 ******************************************************************************/
16 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
17 global $color, $squirrelmail_language, $imap_general_debug;
18
19 $counter = 0;
20 do {
21 $data[$counter] = $read = fgets ($imap_stream, 9096);
22 if ($imap_general_debug) { echo "<small><tt><font color=cc0000>$read</font></tt></small><br>"; flush(); }
23 $counter++;
24 } while (! ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs));
25
26 $response = $regs[1];
27 $message = trim($regs[2]);
28
29 if ($imap_general_debug) echo "--<br>";
30
31 if ($handle_errors == true) {
32 if ($response == "NO") {
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 }
43 } else if ($response == "BAD") {
44 set_up_language($squirrelmail_language);
45 echo "<br><b><font color=$color[2]>\n";
46 echo _("ERROR : Bad or malformed request.");
47 echo "</b><br>\n";
48 echo _("Server responded: ");
49 echo $message . "</font><br>\n";
50 exit;
51 }
52 }
53
54 return $data;
55 }
56
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) {
62 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad, $otp_pad;
63
64 $imap_stream = fsockopen ($imap_server_address, $imap_port,
65 &$error_number, &$error_string, 15);
66 $server_info = fgets ($imap_stream, 1024);
67
68 // Decrypt the password
69 $onetpad = OneTimePadDecrypt($onetimepad, $otp_pad);
70 $password = OneTimePadDecrypt($password, $onetpad);
71
72 /** Do some error correction **/
73 if (!$imap_stream) {
74 if (!$hide) {
75 set_up_language($squirrelmail_language, true);
76 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
77 echo "$error_number : $error_string<br>\r\n";
78 }
79 exit;
80 }
81
82 fputs ($imap_stream, "a001 LOGIN \"" . quotemeta($username) .
83 "\" \"" . quotemeta($password) . "\"\r\n");
84 $read = sqimap_read_data ($imap_stream, "a001", false, $response, $message);
85
86 /** If the connection was not successful, lets see why **/
87 if ($response != "OK") {
88 if (!$hide) {
89 if ($response != "NO") {
90 // "BAD" and anything else gets reported here.
91 set_up_language($squirrelmail_language, true);
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";
98 if (is_array($read))
99 {
100 foreach ($read as $line)
101 {
102 echo htmlspecialchars($line) . "<br>\n";
103 }
104 }
105 exit;
106 } else {
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
116 set_up_language($squirrelmail_language, true);
117
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>
128 <?php echo _("ERROR") ?>
129 </center>
130 </font>
131 </td>
132 </tr>
133 <tr>
134 <td>
135 <center>
136 <?php echo _("Unknown user or password incorrect.") ?><br>
137 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
138 </center>
139 </td>
140 </tr>
141 </table>
142 </center>
143 </body>
144 </html>
145 <?php
146 session_destroy();
147 exit;
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
167 function sqimap_capability($imap_stream, $capability) {
168 global $sqimap_capabilities;
169 global $imap_general_debug;
170
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 }
183
184 /******************************************************************************
185 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
186 ******************************************************************************/
187 function sqimap_get_delimiter ($imap_stream = false) {
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 }
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];
238 } else {
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);
243 }
244 }
245 return $sqimap_delimiter;
246 }
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++) {
256 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
257 return $regs[1];
258 }
259 }
260 return "BUG! Couldn't get number of messages in $mailbox!";
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
271 **
272 ** What about
273 ** lehresma@css.tayloru.edu (Luke Ehresman)
274 **/
275
276 if (ereg("<([^>]+)>", $string, $regs)) {
277 $string = $regs[1];
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) {
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;
307 }
308
309
310 /******************************************************************************
311 ** Returns the number of unseen messages in this folder
312 ******************************************************************************/
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");
316 $read_ary = sqimap_read_data ($imap_stream, "a001", true, $result, $message);
317 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
318 return $regs[1];
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 ?>