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