Remove weird duplicate
[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
b68edc75 14require_once(SM_PATH . '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) {
c5809184 34 if ($imap_stream) {
35 $sid = sqimap_session_id($unique_id);
36 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
37 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
38 return $read;
39 } else {
40 global $squirrelmail_language, $color;
41 set_up_language($squirrelmail_language);
42 require_once(SM_PATH . 'functions/display_messages.php');
43 $string = "<b><font color=$color[2]>\n" .
44 _("ERROR : No available imapstream.") .
45 "</b></font>\n";
46 error_box($string,$color);
47 return false;
48 }
49
1c72b151 50}
51
487daa81 52function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
c5809184 53 if ($imap_stream) {
54 $sid = sqimap_session_id($unique_id);
55 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
56 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
57 return $read;
58 } else {
59 global $squirrelmail_language, $color;
60 set_up_language($squirrelmail_language);
61 require_once(SM_PATH . 'functions/display_messages.php');
62 $string = "<b><font color=$color[2]>\n" .
63 _("ERROR : No available imapstream.") .
64 "</b></font>\n";
65 error_box($string,$color);
66 return false;
67 }
68
42a07ac1 69}
70
9c737111 71
b8c285ab 72/*
73 * custom fgets function. gets a line from IMAP
74 * no matter how big it may be
75 */
76
77function sqimap_fgets($imap_stream) {
78 $read = '';
79 $buffer = 4096;
80 $results = '';
81 while (strpos($read, "\n") === false) {
82 if (!($read = fgets($imap_stream, $buffer))) {
83 break;
84 }
85 $results .= $read;
86 }
87 return $results;
88}
89
bee165ef 90/*
3411d4ec 91 * Reads the output from the IMAP stream. If handle_errors is set to true,
92 * this will also handle all errors that are received. If it is not set,
93 * the errors will be sent back through $response and $message
bee165ef 94 */
b8c285ab 95
3411d4ec 96function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
9c737111 97 global $color, $squirrelmail_language;
9c737111 98 $read = '';
487daa81 99 $pre_a = explode(' ',trim($pre));
100 $pre = $pre_a[0];
b8c285ab 101 $resultlist = array();
102 $data = array();
103 $read = sqimap_fgets($imap_stream);
104 while (1) {
105 switch (true) {
106 case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs):
107 case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs):
108 $response = $regs[1];
109 $message = trim($regs[2]);
110 break 2;
111 case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read):
112 $read = sqimap_fgets($imap_stream);
113 break 1;
56afb33f 114 case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs):
b8c285ab 115 $fetch_data = array();
116 $fetch_data[] = $read;
117 $read = sqimap_fgets($imap_stream);
863936bb 118 while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) &&
b8c285ab 119 !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
120 $fetch_data[] = $read;
863936bb 121 $last = $read;
b8c285ab 122 $read = sqimap_fgets($imap_stream);
9c737111 123 }
863936bb 124 if (isset($last) && preg_match('/^\)/', $last)) {
125 array_pop($fetch_data);
126 }
b8c285ab 127 $resultlist[] = $fetch_data;
128 break 1;
129 default:
130 $data[] = $read;
131 $read = sqimap_fgets($imap_stream);
132 break 1;
9c737111 133 }
b8c285ab 134 }
135 if (!empty($data)) {
9c737111 136 $resultlist[] = $data;
137 }
b8c285ab 138 elseif (empty($resultlist)) {
139 $resultlist[] = array();
140 }
bee165ef 141 if ($handle_errors == false) {
142 return( $resultlist );
b8c285ab 143 }
144 elseif ($response == 'NO') {
145 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
9c737111 146 if (strstr($message, 'command resulted in') === false) {
441f2d33 147 set_up_language($squirrelmail_language);
1f720b34 148 require_once(SM_PATH . 'functions/display_messages.php');
149 $string = "<b><font color=$color[2]>\n" .
b8c285ab 150 _("ERROR : Could not complete request.") .
151 "</b><br>\n" .
152 _("Query:") .
153 $query . '<br>' .
154 _("Reason Given: ") .
155 $message . "</font><br>\n";
1f720b34 156 error_box($string,$color);
052e0c26 157 exit;
9c737111 158 }
b8c285ab 159 }
160 elseif ($response == 'BAD') {
9c737111 161 set_up_language($squirrelmail_language);
1f720b34 162 require_once(SM_PATH . 'functions/display_messages.php');
bef6629c 163 $string = "<b><font color=$color[2]>\n" .
b8c285ab 164 _("ERROR : Bad or malformed request.") .
165 "</b><br>\n" .
166 _("Query:") .
167 $query . '<br>' .
168 _("Server responded: ") .
169 $message . "</font><br>\n";
1f720b34 170 error_box($string,$color);
9c737111 171 exit;
b8c285ab 172 }
173 else {
3411d4ec 174 return $resultlist;
9c737111 175 }
9c737111 176}
177
b3837b0f 178function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
863936bb 179 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
180
181 /* sqimap_read_data should be called for one response
182 but since it just calls sqimap_read_data_list which
183 handles multiple responses we need to check for that
184 and merge the $res array IF they are seperated and
185 IF it was a FETCH response. */
186
187 if (isset($res[1]) && is_array($res[1]) && isset($res[1][0])
188 && preg_match('/^\* \d+ FETCH/', $res[1][0])) {
189 $result = array();
190 foreach($res as $index=>$value) {
191 $result = array_merge($result, $res["$index"]);
56afb33f 192 }
193 }
863936bb 194 if (isset($result)) {
195 return $result;
56afb33f 196 }
56afb33f 197 else {
863936bb 198 return $res[0];
56afb33f 199 }
200
9c737111 201}
202
3411d4ec 203/*
204 * Logs the user into the imap server. If $hide is set, no error messages
205 * will be displayed. This function returns the imap connection handle.
206 */
9c737111 207function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
9eb0fbd4 208 global $color, $squirrelmail_language, $onetimepad;
85fc999e 209
bd9829d7 210 $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
211
3411d4ec 212 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
85fc999e 213
3411d4ec 214 /* Do some error correction */
9c737111 215 if (!$imap_stream) {
216 if (!$hide) {
441f2d33 217 set_up_language($squirrelmail_language, true);
1f720b34 218 require_once(SM_PATH . 'functions/display_messages.php');
219 $string = sprintf (_("Error connecting to IMAP server: %s.") .
220 "<br>\r\n", $imap_server_address) .
221 "$error_number : $error_string<br>\r\n";
2f1f7a12 222 logout_error($string,$color);
9c737111 223 }
224 exit;
225 }
052e0c26 226
2f1f7a12 227 $server_info = fgets ($imap_stream, 1024);
228
229 /* Decrypt the password */
230 $password = OneTimePadDecrypt($password, $onetimepad);
231
1c72b151 232 $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"';
233 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
052e0c26 234
3411d4ec 235 /* If the connection was not successful, lets see why */
9c737111 236 if ($response != 'OK') {
237 if (!$hide) {
74424a43 238 if ($response != 'NO') {
3411d4ec 239 /* "BAD" and anything else gets reported here. */
9c737111 240 set_up_language($squirrelmail_language, true);
1f720b34 241 require_once(SM_PATH . 'functions/display_messages.php');
9c737111 242 if ($response == 'BAD') {
1f720b34 243 $string = sprintf (_("Bad request: %s")."<br>\r\n", $message);
9c737111 244 } else {
1f720b34 245 $string = sprintf (_("Unknown error: %s") . "<br>\n", $message);
9c737111 246 }
1f720b34 247 $string .= '<br>' . _("Read data:") . "<br>\n";
9c737111 248 if (is_array($read)) {
249 foreach ($read as $line) {
1f720b34 250 $string .= htmlspecialchars($line) . "<br>\n";
9c737111 251 }
252 }
1f720b34 253 error_box($string,$color);
9c737111 254 exit;
165e24a7 255 } else {
3411d4ec 256 /*
257 * If the user does not log in with the correct
1c72b151 258 * username and password it is not possible to get the
259 * correct locale from the user's preferences.
260 * Therefore, apply the same hack as on the login
261 * screen.
3411d4ec 262 *
263 * $squirrelmail_language is set by a cookie when
1c72b151 264 * the user selects language and logs out
bee165ef 265 */
9be8198d 266
9c737111 267 set_up_language($squirrelmail_language, true);
bd9c880b 268 include_once(SM_PATH . 'functions/display_messages.php' );
69146537 269 sqsession_destroy();
bd9c880b 270 logout_error( _("Unknown user or password incorrect.") );
9c737111 271 exit;
052e0c26 272 }
9c737111 273 } else {
052e0c26 274 exit;
9c737111 275 }
276 }
9c737111 277 return $imap_stream;
278}
f1e6f580 279
3411d4ec 280/* Simply logs out the IMAP session */
9c737111 281function sqimap_logout ($imap_stream) {
8d936b0c 282 /* Logout is not valid until the server returns 'BYE'
283 * If we don't have an imap_ stream we're already logged out */
26a2cc8b 284 if(isset($imap_stream) && $imap_stream)
8d936b0c 285 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
9c737111 286}
287
487daa81 288function sqimap_capability($imap_stream, $capability='') {
9c737111 289 global $sqimap_capabilities;
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]);
3411d4ec 296 if (isset($cap_list[1])) {
9c737111 297 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
3411d4ec 298 } else {
9c737111 299 $sqimap_capabilities[$cap_list[0]] = TRUE;
3411d4ec 300 }
f1e6f580 301 }
9c737111 302 }
487daa81 303 if ($capability) {
304 if (isset($sqimap_capabilities[$capability])) {
305 return $sqimap_capabilities[$capability];
306 } else {
307 return false;
308 }
f1e6f580 309 }
487daa81 310 return $sqimap_capabilities;
9c737111 311}
312
3411d4ec 313/* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
9c737111 314function sqimap_get_delimiter ($imap_stream = false) {
3411d4ec 315 global $sqimap_delimiter, $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')) {
3411d4ec 325 /*
326 * According to something that I can't find, this is supposed to work on all systems
327 * OS: This won't work in Courier IMAP.
328 * OS: According to rfc2342 response from NAMESPACE command is:
329 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
330 * OS: We want to lookup all personal NAMESPACES...
331 */
1c72b151 332 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
f1e6f580 333 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
9c737111 334 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
f1e6f580 335 $pn = $data2[1];
9c737111 336 }
f1e6f580 337 $pna = explode(')(', $pn);
9c737111 338 while (list($k, $v) = each($pna)) {
862ff2d3 339 $lst = explode('"', $v);
340 if (isset($lst[3])) {
341 $pn[$lst[1]] = $lst[3];
342 } else {
74424a43 343 $pn[$lst[1]] = '';
862ff2d3 344 }
f1e6f580 345 }
346 }
347 $sqimap_delimiter = $pn[0];
348 } else {
349 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
350 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
351 $quote_position = strpos ($read[0], '"');
352 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
353 }
354 }
355 return $sqimap_delimiter;
9c737111 356}
052e0c26 357
358
3411d4ec 359/* Gets the number of messages in the current mailbox. */
9c737111 360function sqimap_get_num_messages ($imap_stream, $mailbox) {
1f720b34 361 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", false, $result, $message);
9c737111 362 for ($i = 0; $i < count($read_ary); $i++) {
85fc999e 363 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
364 return $regs[1];
365 }
9c737111 366 }
1f720b34 367 return false; //"BUG! Couldn't get number of messages in $mailbox!";
9c737111 368}
369
370
3411d4ec 371/* Returns a displayable email address.
372 * Luke Ehresman <lehresma@css.tayloru.edu>
373 * "Luke Ehresman" <lehresma@css.tayloru.edu>
374 * <lehresma@css.tayloru.edu>
375 * lehresma@css.tayloru.edu (Luke Ehresman)
376 * lehresma@css.tayloru.edu
377 * becomes: lehresma@css.tayloru.edu
378 */
9c737111 379function sqimap_find_email ($string) {
9c737111 380 if (ereg("<([^>]+)>", $string, $regs)) {
381 $string = $regs[1];
3411d4ec 382 } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) {
383 $string = $regs[1];
9c737111 384 }
385 return trim($string);
386}
387
388
389/*
3411d4ec 390 * Takes the From: field and creates a displayable name.
391 * Luke Ehresman <lkehresman@yahoo.com>
392 * "Luke Ehresman" <lkehresman@yahoo.com>
393 * lkehresman@yahoo.com (Luke Ehresman)
394 * becomes: Luke Ehresman
395 * <lkehresman@yahoo.com>
396 * becomes: lkehresman@yahoo.com
6282af09 397 */
9c737111 398function sqimap_find_displayable_name ($string) {
6282af09 399 $string = trim($string);
2f7bda0a 400
7e3de682 401 if ( ereg('^(.+)<.*>', $string, $regs) ) {
092d4f2c 402 $orig_string = $string;
2f7bda0a 403 $string = str_replace ('"', '', $regs[1] );
092d4f2c 404 if (trim($string) == '') {
405 $string = sqimap_find_email($orig_string);
406 }
33565ec4 407 if( $string == '' || $string == ' ' ){
408 $string = '&nbsp';
409 }
7e3de682 410 }
411 elseif ( ereg('\((.*)\)', $string, $regs) ) {
9e9c63e4 412 if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){
413 if ( ereg('^(.+) \(', $string, $regs) ) {
414 $string = ereg_replace( ' \(\)$', '', $string );
415 } else {
416 $string = '&nbsp';
417 }
418 } else {
419 $string = $regs[1];
420 }
7e3de682 421 }
422 else {
2f7bda0a 423 $string = str_replace ('"', '', sqimap_find_email($string));
9c737111 424 }
9c737111 425
7e3de682 426 return trim($string);
427}
85fc999e 428
9c737111 429/*
3411d4ec 430 * Returns the number of unseen messages in this folder
431 */
9c737111 432function sqimap_unseen_messages ($imap_stream, $mailbox) {
1f720b34 433 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", false, $result, $message);
ea7ff111 434 $i = 0;
1f720b34 435 $regs = array(false, false);
ea7ff111 436 while (isset($read_ary[$i])) {
437 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
438 break;
439 }
440 $i++;
441 }
9c737111 442 return $regs[1];
443}
444
1f720b34 445/*
446 * Returns the number of unseen/total messages in this folder
447 */
448function sqimap_status_messages ($imap_stream, $mailbox) {
449 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (MESSAGES UNSEEN)", false, $result, $message);
450 $i = 0;
451 $messages = $unseen = false;
452 $regs = array(false,false);
453 while (isset($read_ary[$i])) {
454 if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) {
455 $unseen = $regs[1];
456 }
457 if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) {
458 $messages = $regs[1];
459 }
460 $i++;
461 }
462 return array('MESSAGES' => $messages, 'UNSEEN'=>$unseen);
463}
464
9c737111 465
466/*
3411d4ec 467 * Saves a message to a given folder -- used for saving sent messages
468 */
9c737111 469function sqimap_append ($imap_stream, $sent_folder, $length) {
470 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
85fc999e 471 $tmp = fgets ($imap_stream, 1024);
9c737111 472}
473
8813fb15 474function sqimap_append_done ($imap_stream, $folder='') {
1f720b34 475 global $squirrelmail_language, $color;
9c737111 476 fputs ($imap_stream, "\r\n");
477 $tmp = fgets ($imap_stream, 1024);
69146537 478 if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) {
479 set_up_language($squirrelmail_language);
1f720b34 480 require_once(SM_PATH . 'functions/display_messages.php');
8813fb15 481 $reason = $regs[3];
482 if ($regs[2] == 'NO') {
483 $string = "<b><font color=$color[2]>\n" .
484 _("ERROR : Could not append message to") ." $folder." .
485 "</b><br>\n" .
486 _("Server responded: ") .
487 $reason . "<br>\n";
488 if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) {
5296b8ef 489 $string .= _("Solution: ") .
490 _("Remove unneccessary messages from your folder and start with your Trash folder.")
8813fb15 491 ."<br>\n";
492 }
493 $string .= "</font>\n";
494 error_box($string,$color);
495 } else {
496 $string = "<b><font color=$color[2]>\n" .
1f720b34 497 _("ERROR : Bad or malformed request.") .
498 "</b><br>\n" .
499 _("Server responded: ") .
500 $tmp . "</font><br>\n";
8813fb15 501 error_box($string,$color);
502 exit;
503 }
69146537 504 }
9c737111 505}
85fc999e 506
bd9829d7 507function sqimap_get_user_server ($imap_server, $username) {
bd9829d7 508 if (substr($imap_server, 0, 4) != "map:") {
509 return $imap_server;
510 }
bd9829d7 511 $function = substr($imap_server, 4);
512 return $function($username);
513}
514
515/* This is an example that gets imapservers from yellowpages (NIS).
516 * you can simple put map:map_yp_alias in your $imap_server_address
517 * in config.php use your own function instead map_yp_alias to map your
518 * LDAP whatever way to find the users imapserver. */
519
520function map_yp_alias($username) {
521 $yp = `ypmatch $username aliases`;
522 return chop(substr($yp, strlen($username)+1));
523}
524
15e6162e 525?>