Clear out tabs for proper spaces
[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');
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 *
7e235a1a 22 * @return string a 4 chars unique string
35586184 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);
0dbccd34 130 } else if (preg_match("/^\* BYE \[ALERT\](.*)/", $read, $regs)) {
131 /*
132 It seems that the IMAP server has coughed a lung up
133 and hung up the connection. Print any info we have
134 and abort.
135 */
136 echo _("Please contact your system administrator and report the following error:") . "<br>\n";
137 echo "$regs[1]";
138 exit(0);
9c737111 139 } else {
140 $data[] = $read;
bee165ef 141 $read = fgets ($imap_stream, $bufsize);
9c737111 142 }
4f5c1bcb 143 }
9c737111 144 }
bee165ef 145
146 // while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) &&
147 // !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) {
148 while (($more_msgs = !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs)) &&
149 !preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs)) {
150 $read = fgets($imap_stream, $bufsize);
9c737111 151 }
152 $resultlist[] = $data;
153 }
bee165ef 154
9c737111 155 $response = $regs[1];
156 $message = trim($regs[2]);
bee165ef 157
158 if ($handle_errors == false) {
159 return( $resultlist );
160 } else if ($response == 'NO') {
9c737111 161 // ignore this error from m$ exchange, it is not fatal (aka bug)
162 if (strstr($message, 'command resulted in') === false) {
441f2d33 163 set_up_language($squirrelmail_language);
9c737111 164 echo "<br><b><font color=$color[2]>\n" .
165 _("ERROR : Could not complete request.") .
166 "</b><br>\n" .
b3837b0f 167 _("Query:") .
168 $query . '<br>' .
9c737111 169 _("Reason Given: ") .
170 $message . "</font><br>\n";
052e0c26 171 exit;
9c737111 172 }
173 } else if ($response == 'BAD') {
174 set_up_language($squirrelmail_language);
175 echo "<br><b><font color=$color[2]>\n" .
176 _("ERROR : Bad or malformed request.") .
177 "</b><br>\n" .
b3837b0f 178 _("Query:") .
179 $query . '<br>' .
9c737111 180 _("Server responded: ") .
181 $message . "</font><br>\n";
182 exit;
bee165ef 183 } else {
184 return( $resultlist );
9c737111 185 }
9c737111 186}
187
b3837b0f 188function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
9c737111 189
b3837b0f 190 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
9c737111 191 return $res[0];
bee165ef 192
9c737111 193}
194
195/******************************************************************************
196** Logs the user into the imap server. If $hide is set, no error messages
197** will be displayed. This function returns the imap connection handle.
198******************************************************************************/
199function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
200
201 global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad;
202
203 $imap_stream = fsockopen ( $imap_server_address, $imap_port,
204 $error_number, $error_string, 15);
205 if ( !$imap_stream ) {
206 return FALSE;
207 }
208 $server_info = fgets ($imap_stream, 1024);
209
210 // Decrypt the password
211 $password = OneTimePadDecrypt($password, $onetimepad);
212
213 /** Do some error correction **/
214 if (!$imap_stream) {
215 if (!$hide) {
441f2d33 216 set_up_language($squirrelmail_language, true);
1b187352 217 printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address);
052e0c26 218 echo "$error_number : $error_string<br>\r\n";
9c737111 219 }
220 exit;
221 }
052e0c26 222
1c72b151 223 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
224 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
052e0c26 225
9c737111 226 /** If the connection was not successful, lets see why **/
227 if ($response != 'OK') {
228 if (!$hide) {
74424a43 229 if ($response != 'NO') {
9c737111 230 // "BAD" and anything else gets reported here.
231 set_up_language($squirrelmail_language, true);
232 if ($response == 'BAD') {
3d2504a1 233 printf (_("Bad request: %s")."<br>\r\n", $message);
9c737111 234 } else {
3d2504a1 235 printf (_("Unknown error: %s") . "<br>\n", $message);
9c737111 236 }
237 echo '<br>' . _("Read data:") . "<br>\n";
238 if (is_array($read)) {
239 foreach ($read as $line) {
240 echo htmlspecialchars($line) . "<br>\n";
241 }
242 }
243 exit;
165e24a7 244 } else {
1c72b151 245 /* If the user does not log in with the correct
246 * username and password it is not possible to get the
247 * correct locale from the user's preferences.
248 * Therefore, apply the same hack as on the login
249 * screen.
bee165ef 250 */
9c737111 251
1c72b151 252 /* $squirrelmail_language is set by a cookie when
253 * the user selects language and logs out
bee165ef 254 */
9c737111 255
256 set_up_language($squirrelmail_language, true);
257
258 displayHtmlHeader( _("Unknown user or password incorrect.") );
259 echo "<body bgcolor=\"#ffffff\">\n";
260 error_username_password_incorrect();
261 session_destroy();
262 exit;
052e0c26 263 }
9c737111 264 } else {
052e0c26 265 exit;
9c737111 266 }
267 }
268
269 return $imap_stream;
270}
f1e6f580 271
9c737111 272/*
273 * Simply logs out the imap session
274 */
275function sqimap_logout ($imap_stream) {
1c72b151 276 /* Logout is not valid until the server returns 'BYE' */
277 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
9c737111 278}
279
280function sqimap_capability($imap_stream, $capability) {
281 global $sqimap_capabilities;
282
283 if (!is_array($sqimap_capabilities)) {
1c72b151 284 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
285
9c737111 286 $c = explode(' ', $read[0]);
287 for ($i=2; $i < count($c); $i++) {
288 $cap_list = explode('=', $c[$i]);
289 if (isset($cap_list[1]))
290 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
291 else
292 $sqimap_capabilities[$cap_list[0]] = TRUE;
f1e6f580 293 }
9c737111 294 }
295 if (! isset($sqimap_capabilities[$capability])) {
f1e6f580 296 return false;
9c737111 297 } else {
f1e6f580 298 return $sqimap_capabilities[$capability];
299 }
9c737111 300}
301
302/******************************************************************************
303** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test...
304******************************************************************************/
305function sqimap_get_delimiter ($imap_stream = false) {
306
307 global $sqimap_delimiter;
308 global $optional_delimiter;
309
310 /* Use configured delimiter if set */
311 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
312 return $optional_delimiter;
313 }
314
315 /* Do some caching here */
316 if (!$sqimap_delimiter) {
317 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
f1e6f580 318 /* According to something that I can't find, this is supposed to work on all systems
319 OS: This won't work in Courier IMAP.
320 OS: According to rfc2342 response from NAMESPACE command is:
321 OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
322 OS: We want to lookup all personal NAMESPACES...
323 */
1c72b151 324 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
f1e6f580 325 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
9c737111 326 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
f1e6f580 327 $pn = $data2[1];
9c737111 328 }
f1e6f580 329 $pna = explode(')(', $pn);
9c737111 330 while (list($k, $v) = each($pna)) {
862ff2d3 331 $lst = explode('"', $v);
332 if (isset($lst[3])) {
333 $pn[$lst[1]] = $lst[3];
334 } else {
74424a43 335 $pn[$lst[1]] = '';
862ff2d3 336 }
f1e6f580 337 }
338 }
339 $sqimap_delimiter = $pn[0];
340 } else {
341 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
342 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
343 $quote_position = strpos ($read[0], '"');
344 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
345 }
346 }
347 return $sqimap_delimiter;
9c737111 348}
052e0c26 349
350
9c737111 351/*
352 * Gets the number of messages in the current mailbox.
353 */
354function sqimap_get_num_messages ($imap_stream, $mailbox) {
355
41600f7d 356 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message);
9c737111 357 for ($i = 0; $i < count($read_ary); $i++) {
358 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
359 return $regs[1];
360 }
361 }
362 return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox );
363
364}
365
366
367/*
368* Returns a displayable email address
369*/
370function sqimap_find_email ($string) {
371 /** Luke Ehresman <lehresma@css.tayloru.edu>
372 ** <lehresma@css.tayloru.edu>
373 ** lehresma@css.tayloru.edu
374 **
375 ** What about
376 ** lehresma@css.tayloru.edu (Luke Ehresman)
377 **/
378
379 if (ereg("<([^>]+)>", $string, $regs)) {
380 $string = $regs[1];
381 }
382 return trim($string);
383}
384
385
386/*
6282af09 387 * Takes the From: field, and creates a displayable name.
388 * Luke Ehresman <lkehresman@yahoo.com>
389 * "Luke Ehresman" <lkehresman@yahoo.com>
390 * lkehresman@yahoo.com (Luke Ehresman)
027bbd2b 391 * becomes: Luke Ehresman
6282af09 392 * <lkehresman@yahoo.com>
393 * becomes: lkehresman@yahoo.com
394 */
9c737111 395function sqimap_find_displayable_name ($string) {
6282af09 396 $string = trim($string);
2f7bda0a 397
7e3de682 398 if ( ereg('^(.+)<.*>', $string, $regs) ) {
092d4f2c 399 $orig_string = $string;
2f7bda0a 400 $string = str_replace ('"', '', $regs[1] );
092d4f2c 401 if (trim($string) == '') {
402 $string = sqimap_find_email($orig_string);
403 }
33565ec4 404 if( $string == '' || $string == ' ' ){
405 $string = '&nbsp';
406 }
7e3de682 407 }
408 elseif ( ereg('\((.*)\)', $string, $regs) ) {
9e9c63e4 409 if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){
410 if ( ereg('^(.+) \(', $string, $regs) ) {
411 $string = ereg_replace( ' \(\)$', '', $string );
412 } else {
413 $string = '&nbsp';
414 }
415 } else {
416 $string = $regs[1];
417 }
7e3de682 418 }
419 else {
2f7bda0a 420 $string = str_replace ('"', '', sqimap_find_email($string));
9c737111 421 }
9c737111 422
7e3de682 423 return trim($string);
424}
425
9c737111 426/*
427* Returns the number of unseen messages in this folder
428*/
429function sqimap_unseen_messages ($imap_stream, $mailbox) {
430 //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n");
1a16af11 431 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message);
9c737111 432 ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs);
433 return $regs[1];
434}
435
436
437/*
438* Saves a message to a given folder -- used for saving sent messages
439*/
440function sqimap_append ($imap_stream, $sent_folder, $length) {
441 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
1c72b151 442 $tmp = fgets ($imap_stream, 1024);
9c737111 443}
444
445function sqimap_append_done ($imap_stream) {
446 fputs ($imap_stream, "\r\n");
447 $tmp = fgets ($imap_stream, 1024);
448}
449
15e6162e 450?>