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