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