Fixed login crash
[squirrelmail.git] / functions / imap_general.php
CommitLineData
59177427 1<?php
bccadd02 2
052e0c26 3 /**
7350889b 4 * imap.php
5 *
6 * Copyright (c) 1999-2001 The Squirrelmail Development Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This implements all functions that do general imap functions.
10 *
11 * $Id$
12 */
13
14 /**
15 * Unique SessionId
16 *
17 * Sets an unique session id in order to avoid simultanous sessions crash.
18 *
19 * @return string a 4 chars unique string
20 */
21 function sqimap_session_id() {
22
cb48c245 23 $IMAPSessionID = substr(session_id(), -4);
24 if( $IMAPSessionID == '' ) {
25 $IMAPSessionID = GenerateRandomString(4, '', 7);
26 }
7350889b 27
cb48c245 28 return( $IMAPSessionID );
180aa6d7 29 }
30
31
32 /******************************************************************************
052e0c26 33 ** Reads the output from the IMAP stream. If handle_errors is set to true,
34 ** this will also handle all errors that are received. If it is not set,
35 ** the errors will be sent back through $response and $message
36 ******************************************************************************/
a3432f47 37
91f68e94 38 function sqimap_read_data_list ($imap_stream, $pre, $handle_errors,
f1e6f580 39 &$response, &$message) {
40 global $color, $squirrelmail_language;
245a6892 41
f1e6f580 42 $read = '';
91f68e94 43 $resultlist = array();
7350889b 44
91f68e94 45 $more_msgs = true;
46 while ($more_msgs) {
47 $data = array();
48 $total_size = 0;
d9ca5b40 49 while (strpos($read, "\n") === false) {
50 $read .= fgets($imap_stream, 9096);
51 }
4f5c1bcb 52
91f68e94 53 if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) {
54 $size = $regs[1];
55 } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) {
56 // Sizeless response, probably single-line
55412053 57 $size = -1;
91f68e94 58 $data[] = $read;
59 $read = fgets($imap_stream, 9096);
d9ca5b40 60 } else {
55412053 61 $size = -1;
91f68e94 62 }
63 while (1) {
64 while (strpos($read, "\n") === false) {
65 $read .= fgets($imap_stream, 9096);
66 }
91f68e94 67 // If we know the size, no need to look at the end parameters
68 if ($size > 0) {
69 if ($total_size == $size) {
70 // We've reached the end of this 'message', switch to the next one.
71 $data[] = $read;
37e24380 72 break;
91f68e94 73 } else if ($total_size > $size) {
74 $difference = $total_size - $size;
75 $total_size = $total_size - strlen($read);
76 $data[] = substr ($read, 0, strlen($read)-$difference);
77 $read = substr ($read, strlen($read)-$difference, strlen($read));
78 break;
79 } else {
80 $data[] = $read;
81 $read = fgets($imap_stream, 9096);
f1e6f580 82 while (strpos($read, "\n") === false) {
83 $read .= fgets($imap_stream, 9096);
84 }
91f68e94 85 }
86 $total_size += strlen($read);
4f5c1bcb 87 } else {
91f68e94 88 if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) ||
55412053 89 (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) {
91f68e94 90 break;
91 } else {
92 $data[] = $read;
93 $read = fgets ($imap_stream, 9096);
94 }
4f5c1bcb 95 }
d9ca5b40 96 }
a3db804c 97
91f68e94 98 while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
99 !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
100 $read = fgets($imap_stream, 9096);
101 }
102 $resultlist[] = $data;
103 }
3d2504a1 104 $response = $regs[1];
105 $message = trim($regs[2]);
106
f7b1b3b1 107 if ($handle_errors == false) { return $resultlist; }
d9ca5b40 108
74424a43 109 if ($response == 'NO') {
f358f099 110 // ignore this error from m$ exchange, it is not fatal (aka bug)
8e8ebd27 111 if (strstr($message, 'command resulted in') === false) {
441f2d33 112 set_up_language($squirrelmail_language);
04632dbc 113 echo "<br><b><font color=$color[2]>\n";
f358f099 114 echo _("ERROR : Could not complete request.");
04632dbc 115 echo "</b><br>\n";
f358f099 116 echo _("Reason Given: ");
3d2504a1 117 echo $message . "</font><br>\n";
052e0c26 118 exit;
119 }
74424a43 120 } else if ($response == 'BAD') {
f358f099 121 set_up_language($squirrelmail_language);
122 echo "<br><b><font color=$color[2]>\n";
123 echo _("ERROR : Bad or malformed request.");
124 echo "</b><br>\n";
125 echo _("Server responded: ");
126 echo $message . "</font><br>\n";
127 exit;
052e0c26 128 }
91f68e94 129 return $resultlist;
130 }
131
132 function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) {
f1e6f580 133 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message);
134 return $res[0];
052e0c26 135 }
f1e6f580 136
052e0c26 137 /******************************************************************************
138 ** Logs the user into the imap server. If $hide is set, no error messages
139 ** will be displayed. This function returns the imap connection handle.
140 ******************************************************************************/
141 function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
cb48c245 142
bc104ef3 143 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
1b187352 144
cb48c245 145 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
146 $error_number, $error_string, 15);
052e0c26 147 $server_info = fgets ($imap_stream, 1024);
cb48c245 148
52eefafc 149 // Decrypt the password
bc104ef3 150 $password = OneTimePadDecrypt($password, $onetimepad);
52eefafc 151
052e0c26 152 /** Do some error correction **/
153 if (!$imap_stream) {
154 if (!$hide) {
441f2d33 155 set_up_language($squirrelmail_language, true);
1b187352 156 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 157 echo "$error_number : $error_string<br>\r\n";
158 }
159 exit;
160 }
161
f1e6f580 162 fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) .
704db5b2 163 '" "' . quoteIMAP($password) . "\"\r\n");
180aa6d7 164 $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message);
052e0c26 165
166 /** If the connection was not successful, lets see why **/
3d2504a1 167 if ($response != "OK") {
052e0c26 168 if (!$hide) {
74424a43 169 if ($response != 'NO') {
3d2504a1 170 // "BAD" and anything else gets reported here.
441f2d33 171 set_up_language($squirrelmail_language, true);
74424a43 172 if ($response == 'BAD')
3d2504a1 173 printf (_("Bad request: %s")."<br>\r\n", $message);
174 else
175 printf (_("Unknown error: %s") . "<br>\n", $message);
74424a43 176 echo '<br>';
3d2504a1 177 echo _("Read data:") . "<br>\n";
f1e6f580 178 if (is_array($read))
179 {
180 foreach ($read as $line)
181 {
182 echo htmlspecialchars($line) . "<br>\n";
183 }
3d2504a1 184 }
052e0c26 185 exit;
165e24a7 186 } else {
1b187352 187 // If the user does not log in with the correct
188 // username and password it is not possible to get the
189 // correct locale from the user's preferences.
190 // Therefore, apply the same hack as on the login
191 // screen.
192
193 // $squirrelmail_language is set by a cookie when
194 // the user selects language and logs out
f1e6f580 195
441f2d33 196 set_up_language($squirrelmail_language, true);
f1e6f580 197
052e0c26 198 ?>
199 <html>
74424a43 200 <body bgcolor="ffffff">
052e0c26 201 <br>
202 <center>
74424a43 203 <table width="70%" noborder bgcolor="ffffff" align="center">
052e0c26 204 <tr>
74424a43 205 <td bgcolor="dcdcdc">
206 <font color="cc0000">
052e0c26 207 <center>
59177427 208 <?php echo _("ERROR") ?>
052e0c26 209 </center>
210 </font>
211 </td>
212 </tr>
213 <tr>
214 <td>
215 <center>
59177427 216 <?php echo _("Unknown user or password incorrect.") ?><br>
5630c7ec 217 <a href="login.php" target="_top"><?php echo _("Click here to try again") ?></a>
052e0c26 218 </center>
219 </td>
220 </tr>
221 </table>
222 </center>
223 </body>
224 </html>
59177427 225 <?php
052e0c26 226 session_destroy();
227 exit;
052e0c26 228 }
229 } else {
230 exit;
231 }
232 }
233
234 return $imap_stream;
235 }
236
237
238
239
240 /******************************************************************************
241 ** Simply logs out the imap session
242 ******************************************************************************/
243 function sqimap_logout ($imap_stream) {
180aa6d7 244 fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n");
052e0c26 245 }
246
f435778e 247 function sqimap_capability($imap_stream, $capability) {
f1e6f580 248 global $sqimap_capabilities;
f1e6f580 249
250 if (!is_array($sqimap_capabilities)) {
251 fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n");
252 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
253
254 $c = explode(' ', $read[0]);
255 for ($i=2; $i < count($c); $i++) {
256 $cap_list = explode('=', $c[$i]);
257 if (isset($cap_list[1]))
258 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
259 else
260 $sqimap_capabilities[$cap_list[0]] = TRUE;
261 }
262 }
263 if (! isset($sqimap_capabilities[$capability]))
264 return false;
265 return $sqimap_capabilities[$capability];
266 }
052e0c26 267
268 /******************************************************************************
269 ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
270 ******************************************************************************/
f435778e 271 function sqimap_get_delimiter ($imap_stream = false) {
7350889b 272
f435778e 273 global $sqimap_delimiter;
274 global $optional_delimiter;
f6941f00 275
f435778e 276 /* Use configured delimiter if set */
277 if((!empty($optional_delimiter)) && $optional_delimiter != "detect")
278 return $optional_delimiter;
a3db804c 279
f435778e 280 /* Do some caching here */
281 if (!$sqimap_delimiter) {
f1e6f580 282 if (sqimap_capability($imap_stream, "NAMESPACE")) {
283 /* According to something that I can't find, this is supposed to work on all systems
284 OS: This won't work in Courier IMAP.
285 OS: According to rfc2342 response from NAMESPACE command is:
286 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
287 OS: We want to lookup all personal NAMESPACES...
288 */
289 fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n");
290 $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b);
291 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
292 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2))
293 $pn = $data2[1];
294 $pna = explode(')(', $pn);
295 while (list($k, $v) = each($pna))
296 {
862ff2d3 297 $lst = explode('"', $v);
298 if (isset($lst[3])) {
299 $pn[$lst[1]] = $lst[3];
300 } else {
74424a43 301 $pn[$lst[1]] = '';
862ff2d3 302 }
f1e6f580 303 }
304 }
305 $sqimap_delimiter = $pn[0];
306 } else {
307 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
308 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
309 $quote_position = strpos ($read[0], '"');
310 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
311 }
312 }
313 return $sqimap_delimiter;
f435778e 314 }
052e0c26 315
316
317 /******************************************************************************
318 ** Gets the number of messages in the current mailbox.
319 ******************************************************************************/
320 function sqimap_get_num_messages ($imap_stream, $mailbox) {
180aa6d7 321 fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n");
322 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
052e0c26 323 for ($i = 0; $i < count($read_ary); $i++) {
441f2d33 324 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
f1e6f580 325 return $regs[1];
052e0c26 326 }
327 }
441f2d33 328 return "BUG! Couldn't get number of messages in $mailbox!";
052e0c26 329 }
330
331
332 /******************************************************************************
333 ** Returns a displayable email address
334 ******************************************************************************/
335 function sqimap_find_email ($string) {
336 /** Luke Ehresman <lehresma@css.tayloru.edu>
337 ** <lehresma@css.tayloru.edu>
338 ** lehresma@css.tayloru.edu
1d2ce1c3 339 **
340 ** What about
341 ** lehresma@css.tayloru.edu (Luke Ehresman)
052e0c26 342 **/
343
441f2d33 344 if (ereg("<([^>]+)>", $string, $regs)) {
345 $string = $regs[1];
052e0c26 346 }
347 return trim($string);
348 }
349
350
351 /******************************************************************************
352 ** Takes the From: field, and creates a displayable name.
353 ** Luke Ehresman <lkehresman@yahoo.com>
354 ** becomes: Luke Ehresman
355 ** <lkehresman@yahoo.com>
356 ** becomes: lkehresman@yahoo.com
357 ******************************************************************************/
358 function sqimap_find_displayable_name ($string) {
74424a43 359 $string = ' '.trim($string);
1d2ce1c3 360 $orig_string = $string;
74424a43 361 if (strpos($string, '<') && strpos($string, '>')) {
362 if (strpos($string, '<') == 1) {
1d2ce1c3 363 $string = sqimap_find_email($string);
364 } else {
365 $string = trim($string);
74424a43 366 $string = substr($string, 0, strpos($string, '<'));
367 $string = ereg_replace ('"', '', $string);
1d2ce1c3 368 }
369
74424a43 370 if (trim($string) == '') {
1d2ce1c3 371 $string = sqimap_find_email($orig_string);
372 }
373 }
374 return $string;
052e0c26 375 }
376
377
052e0c26 378 /******************************************************************************
379 ** Returns the number of unseen messages in this folder
380 ******************************************************************************/
4d2c01e3 381 function sqimap_unseen_messages ($imap_stream, $mailbox) {
180aa6d7 382 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
383 fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n");
384 $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message);
441f2d33 385 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
386 return $regs[1];
052e0c26 387 }
388
389
390 /******************************************************************************
391 ** Saves a message to a given folder -- used for saving sent messages
392 ******************************************************************************/
393 function sqimap_append ($imap_stream, $sent_folder, $length) {
180aa6d7 394 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
052e0c26 395 $tmp = fgets ($imap_stream, 1024);
396 }
397
398 function sqimap_append_done ($imap_stream) {
399 fputs ($imap_stream, "\r\n");
400 $tmp = fgets ($imap_stream, 1024);
401 }
402?>