bugfix
[squirrelmail.git] / functions / imap_general.php
CommitLineData
59177427 1<?php
bccadd02 2
35586184 3/**
a6fd80f5 4 * imap_general.php
35586184 5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
35586184 8 *
15e6162e 9 * This implements all functions that do general imap functions.
35586184 10 *
15e6162e 11 * $Id$
35586184 12 */
13
9c737111 14require_once('../functions/page_header.php');
35586184 15
41600f7d 16global $sqimap_session_id;
17$sqimap_session_id = 1;
71257f8b 18
3411d4ec 19/* Sets an unique session id in order to avoid simultanous sessions crash. */
20function sqimap_session_id() {
41600f7d 21 global $data_dir, $username, $sqimap_session_id;
41600f7d 22 return( sprintf("A%03d", $sqimap_session_id++) );
9c737111 23}
24
3411d4ec 25/*
26 * Both send a command and accept the result from the command.
27 * This is to allow proper session number handling.
28 */
1c72b151 29function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) {
41600f7d 30 $sid = sqimap_session_id();
31 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
b3837b0f 32 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
1c72b151 33 return $read;
34}
35
42a07ac1 36function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) {
41600f7d 37 $sid = sqimap_session_id();
38 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
b3837b0f 39 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
42a07ac1 40 return $read;
41}
42
9c737111 43
bee165ef 44/*
3411d4ec 45 * Reads the output from the IMAP stream. If handle_errors is set to true,
46 * this will also handle all errors that are received. If it is not set,
47 * the errors will be sent back through $response and $message
bee165ef 48 */
3411d4ec 49function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
9c737111 50 global $color, $squirrelmail_language;
bee165ef 51
9c737111 52 $read = '';
bee165ef 53 $bufsize = 9096;
9c737111 54 $resultlist = array();
bee165ef 55
9c737111 56 $more_msgs = true;
57 while ($more_msgs) {
58 $data = array();
59 $total_size = 0;
bee165ef 60
9c737111 61 while (strpos($read, "\n") === false) {
bee165ef 62 if(!($read .= fgets($imap_stream, $bufsize))) {
8084e2cb 63 break;
64 }
9c737111 65 }
bee165ef 66
3411d4ec 67 /* if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { */
bee165ef 68 if (preg_match('/^\* [0-9]+ FETCH.*\{([0-9]+)\}/', $read, $regs)) {
3411d4ec 69 $size = $regs[1];
70 /* } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { */
85fc999e 71 } else if (preg_match('/^\* [0-9]+ FETCH/', $read, $regs)) {
3411d4ec 72 /* Sizeless response, probably single-line */
55412053 73 $size = -1;
91f68e94 74 $data[] = $read;
bee165ef 75 $read = fgets($imap_stream, $bufsize);
9c737111 76 } else {
55412053 77 $size = -1;
9c737111 78 }
79 while (1) {
91f68e94 80 while (strpos($read, "\n") === false) {
bee165ef 81 if(!($read .= fgets($imap_stream, $bufsize))) {
8084e2cb 82 break;
83 }
91f68e94 84 }
3411d4ec 85 /* If we know the size, no need to look at the end parameters */
91f68e94 86 if ($size > 0) {
9c737111 87 if ($total_size == $size) {
3411d4ec 88 /* We've reached the end of this 'message', switch to the next one. */
9c737111 89 $data[] = $read;
90 break;
91 } else if ($total_size > $size) {
92 $difference = $total_size - $size;
93 $total_size = $total_size - strlen($read);
94 $data[] = substr ($read, 0, strlen($read)-$difference);
95 $read = substr ($read, strlen($read)-$difference, strlen($read));
96 break;
97 } else {
98 $data[] = $read;
bee165ef 99 $read = fgets($imap_stream, $bufsize);
9c737111 100 while (strpos($read, "\n") === false) {
bee165ef 101 $read .= fgets($imap_stream, $bufsize);
9c737111 102 }
103 }
104 $total_size += strlen($read);
4f5c1bcb 105 } else {
3411d4ec 106 /* if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || */
bee165ef 107 if (preg_match("/^$pre (OK|BAD|NO)(.*)/", $read, $regs) ||
3411d4ec 108 /* (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) { */
109 (($size == -1) && preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs))) {
9c737111 110 break;
85fc999e 111 } else if ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
3411d4ec 112 /*
113 * This block has been added in order to avoid the problem
114 * caused by the * OK [PARSE] Missing parameter answer
115 * Please, replace it with a better parsing if you know how.
116 * This block has been updated by
117 * Seth E. Randall <sethr@missoulafcu.org>. Once we see
118 * one OK [PARSE line, we just go through and keep
119 * tossing them out until we get something different.
120 */
85fc999e 121 while ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) {
122 $read = fgets($imap_stream, $bufsize);
123 }
bee165ef 124 $data[] = $read;
125 $read = fgets ($imap_stream, $bufsize);
85fc999e 126 } else if (preg_match('/^\* BYE \[ALERT\](.*)/', $read, $regs)) {
0dbccd34 127 /*
3411d4ec 128 * It seems that the IMAP server has coughed up a lung
129 * and hung up the connection. Print any info we have
130 * and abort.
0dbccd34 131 */
132 echo _("Please contact your system administrator and report the following error:") . "<br>\n";
3411d4ec 133 echo $regs[1];
134 exit;
9c737111 135 } else {
136 $data[] = $read;
bee165ef 137 $read = fgets ($imap_stream, $bufsize);
9c737111 138 }
4f5c1bcb 139 }
9c737111 140 }
bee165ef 141
3411d4ec 142 /*
143 * while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
144 * !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
145 */
bee165ef 146 while (($more_msgs = !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs)) &&
147 !preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs)) {
148 $read = fgets($imap_stream, $bufsize);
9c737111 149 }
150 $resultlist[] = $data;
151 }
bee165ef 152
9c737111 153 $response = $regs[1];
154 $message = trim($regs[2]);
bee165ef 155
156 if ($handle_errors == false) {
157 return( $resultlist );
158 } else if ($response == 'NO') {
3411d4ec 159 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
9c737111 160 if (strstr($message, 'command resulted in') === false) {
441f2d33 161 set_up_language($squirrelmail_language);
9c737111 162 echo "<br><b><font color=$color[2]>\n" .
163 _("ERROR : Could not complete request.") .
164 "</b><br>\n" .
b3837b0f 165 _("Query:") .
166 $query . '<br>' .
9c737111 167 _("Reason Given: ") .
168 $message . "</font><br>\n";
052e0c26 169 exit;
9c737111 170 }
171 } else if ($response == 'BAD') {
172 set_up_language($squirrelmail_language);
173 echo "<br><b><font color=$color[2]>\n" .
174 _("ERROR : Bad or malformed request.") .
175 "</b><br>\n" .
b3837b0f 176 _("Query:") .
177 $query . '<br>' .
9c737111 178 _("Server responded: ") .
179 $message . "</font><br>\n";
180 exit;
bee165ef 181 } else {
3411d4ec 182 return $resultlist;
9c737111 183 }
9c737111 184}
185
b3837b0f 186function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
b3837b0f 187 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
9c737111 188 return $res[0];
9c737111 189}
190
3411d4ec 191/*
192 * Logs the user into the imap server. If $hide is set, no error messages
193 * will be displayed. This function returns the imap connection handle.
194 */
9c737111 195function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
9c737111 196 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
85fc999e 197
3411d4ec 198 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
9c737111 199 if ( !$imap_stream ) {
3411d4ec 200 return false;
9c737111 201 }
202 $server_info = fgets ($imap_stream, 1024);
85fc999e 203
3411d4ec 204 /* Decrypt the password */
9c737111 205 $password = OneTimePadDecrypt($password, $onetimepad);
85fc999e 206
3411d4ec 207 /* Do some error correction */
9c737111 208 if (!$imap_stream) {
209 if (!$hide) {
441f2d33 210 set_up_language($squirrelmail_language, true);
1b187352 211 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 212 echo "$error_number : $error_string<br>\r\n";
9c737111 213 }
214 exit;
215 }
052e0c26 216
1c72b151 217 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
218 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
052e0c26 219
3411d4ec 220 /* If the connection was not successful, lets see why */
9c737111 221 if ($response != 'OK') {
222 if (!$hide) {
74424a43 223 if ($response != 'NO') {
3411d4ec 224 /* "BAD" and anything else gets reported here. */
9c737111 225 set_up_language($squirrelmail_language, true);
226 if ($response == 'BAD') {
3d2504a1 227 printf (_("Bad request: %s")."<br>\r\n", $message);
9c737111 228 } else {
3d2504a1 229 printf (_("Unknown error: %s") . "<br>\n", $message);
9c737111 230 }
231 echo '<br>' . _("Read data:") . "<br>\n";
232 if (is_array($read)) {
233 foreach ($read as $line) {
234 echo htmlspecialchars($line) . "<br>\n";
235 }
236 }
237 exit;
165e24a7 238 } else {
3411d4ec 239 /*
240 * If the user does not log in with the correct
1c72b151 241 * username and password it is not possible to get the
242 * correct locale from the user's preferences.
243 * Therefore, apply the same hack as on the login
244 * screen.
3411d4ec 245 *
246 * $squirrelmail_language is set by a cookie when
1c72b151 247 * the user selects language and logs out
bee165ef 248 */
9be8198d 249
9c737111 250 set_up_language($squirrelmail_language, true);
9be8198d 251 include_once( '../functions/display_messages.php' );
252 logout_error( _("Unknown user or password incorrect.") );
9c737111 253 session_destroy();
254 exit;
052e0c26 255 }
9c737111 256 } else {
052e0c26 257 exit;
9c737111 258 }
259 }
9c737111 260 return $imap_stream;
261}
f1e6f580 262
3411d4ec 263/* Simply logs out the IMAP session */
9c737111 264function sqimap_logout ($imap_stream) {
1c72b151 265 /* Logout is not valid until the server returns 'BYE' */
266 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
9c737111 267}
268
269function sqimap_capability($imap_stream, $capability) {
270 global $sqimap_capabilities;
85fc999e 271
9c737111 272 if (!is_array($sqimap_capabilities)) {
1c72b151 273 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
274
9c737111 275 $c = explode(' ', $read[0]);
276 for ($i=2; $i < count($c); $i++) {
277 $cap_list = explode('=', $c[$i]);
3411d4ec 278 if (isset($cap_list[1])) {
9c737111 279 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
3411d4ec 280 } else {
9c737111 281 $sqimap_capabilities[$cap_list[0]] = TRUE;
3411d4ec 282 }
f1e6f580 283 }
9c737111 284 }
3411d4ec 285 if (isset($sqimap_capabilities[$capability])) {
f1e6f580 286 return $sqimap_capabilities[$capability];
3411d4ec 287 } else {
288 return false;
f1e6f580 289 }
9c737111 290}
291
3411d4ec 292/* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
9c737111 293function sqimap_get_delimiter ($imap_stream = false) {
3411d4ec 294 global $sqimap_delimiter, $optional_delimiter;
85fc999e 295
9c737111 296 /* Use configured delimiter if set */
297 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
298 return $optional_delimiter;
299 }
85fc999e 300
9c737111 301 /* Do some caching here */
302 if (!$sqimap_delimiter) {
303 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
3411d4ec 304 /*
305 * According to something that I can't find, this is supposed to work on all systems
306 * OS: This won't work in Courier IMAP.
307 * OS: According to rfc2342 response from NAMESPACE command is:
308 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
309 * OS: We want to lookup all personal NAMESPACES...
310 */
1c72b151 311 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
f1e6f580 312 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
9c737111 313 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
f1e6f580 314 $pn = $data2[1];
9c737111 315 }
f1e6f580 316 $pna = explode(')(', $pn);
9c737111 317 while (list($k, $v) = each($pna)) {
862ff2d3 318 $lst = explode('"', $v);
319 if (isset($lst[3])) {
320 $pn[$lst[1]] = $lst[3];
321 } else {
74424a43 322 $pn[$lst[1]] = '';
862ff2d3 323 }
f1e6f580 324 }
325 }
326 $sqimap_delimiter = $pn[0];
327 } else {
328 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
329 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
330 $quote_position = strpos ($read[0], '"');
331 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
332 }
333 }
334 return $sqimap_delimiter;
9c737111 335}
052e0c26 336
337
3411d4ec 338/* Gets the number of messages in the current mailbox. */
9c737111 339function sqimap_get_num_messages ($imap_stream, $mailbox) {
41600f7d 340 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
9c737111 341 for ($i = 0; $i < count($read_ary); $i++) {
85fc999e 342 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
343 return $regs[1];
344 }
9c737111 345 }
3411d4ec 346 return "BUG! Couldn't get number of messages in $mailbox!";
9c737111 347}
348
349
3411d4ec 350/* Returns a displayable email address.
351 * Luke Ehresman <lehresma@css.tayloru.edu>
352 * "Luke Ehresman" <lehresma@css.tayloru.edu>
353 * <lehresma@css.tayloru.edu>
354 * lehresma@css.tayloru.edu (Luke Ehresman)
355 * lehresma@css.tayloru.edu
356 * becomes: lehresma@css.tayloru.edu
357 */
9c737111 358function sqimap_find_email ($string) {
9c737111 359 if (ereg("<([^>]+)>", $string, $regs)) {
360 $string = $regs[1];
3411d4ec 361 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
362 $string = $regs[1];
9c737111 363 }
364 return trim($string);
365}
366
367
368/*
3411d4ec 369 * Takes the From: field and creates a displayable name.
370 * Luke Ehresman <lkehresman@yahoo.com>
371 * "Luke Ehresman" <lkehresman@yahoo.com>
372 * lkehresman@yahoo.com (Luke Ehresman)
373 * becomes: Luke Ehresman
374 * <lkehresman@yahoo.com>
375 * becomes: lkehresman@yahoo.com
6282af09 376 */
9c737111 377function sqimap_find_displayable_name ($string) {
6282af09 378 $string = trim($string);
2f7bda0a 379
7e3de682 380 if ( ereg('^(.+)<.*>', $string, $regs) ) {
092d4f2c 381 $orig_string = $string;
2f7bda0a 382 $string = str_replace ('"', '', $regs[1] );
092d4f2c 383 if (trim($string) == '') {
384 $string = sqimap_find_email($orig_string);
385 }
33565ec4 386 if( $string == '' || $string == ' ' ){
387 $string = '&nbsp';
388 }
7e3de682 389 }
390 elseif ( ereg('\((.*)\)', $string, $regs) ) {
9e9c63e4 391 if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){
392 if ( ereg('^(.+) \(', $string, $regs) ) {
393 $string = ereg_replace( ' \(\)$', '', $string );
394 } else {
395 $string = '&nbsp';
396 }
397 } else {
398 $string = $regs[1];
399 }
7e3de682 400 }
401 else {
2f7bda0a 402 $string = str_replace ('"', '', sqimap_find_email($string));
9c737111 403 }
9c737111 404
7e3de682 405 return trim($string);
406}
85fc999e 407
9c737111 408/*
3411d4ec 409 * Returns the number of unseen messages in this folder
410 */
9c737111 411function sqimap_unseen_messages ($imap_stream, $mailbox) {
1a16af11 412 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
ea7ff111 413 $i = 0;
414 while (isset($read_ary[$i])) {
415 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
416 break;
417 }
418 $i++;
419 }
9c737111 420 return $regs[1];
421}
422
423
424/*
3411d4ec 425 * Saves a message to a given folder -- used for saving sent messages
426 */
9c737111 427function sqimap_append ($imap_stream, $sent_folder, $length) {
428 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
85fc999e 429 $tmp = fgets ($imap_stream, 1024);
9c737111 430}
431
432function sqimap_append_done ($imap_stream) {
433 fputs ($imap_stream, "\r\n");
434 $tmp = fgets ($imap_stream, 1024);
435}
85fc999e 436
15e6162e 437?>