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