performance tweak
[squirrelmail.git] / functions / imap_general.php
CommitLineData
59177427 1<?php
bccadd02 2
35586184 3/**
a6fd80f5 4 * imap_general.php
35586184 5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
15e6162e 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');
47a29326 15require_once(SM_PATH . 'functions/auth.php');
16
35586184 17
41600f7d 18global $sqimap_session_id;
19$sqimap_session_id = 1;
71257f8b 20
3411d4ec 21/* Sets an unique session id in order to avoid simultanous sessions crash. */
487daa81 22function sqimap_session_id($unique_id = false) {
41600f7d 23 global $data_dir, $username, $sqimap_session_id;
487daa81 24 if (!$unique_id) {
098ea084 25 return( sprintf("A%03d", $sqimap_session_id++) );
487daa81 26 } else {
098ea084 27 return( sprintf("A%03d", $sqimap_session_id++) . ' UID' );
487daa81 28 }
9c737111 29}
30
3411d4ec 31/*
32 * Both send a command and accept the result from the command.
33 * This is to allow proper session number handling.
34 */
487daa81 35function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
c5809184 36 if ($imap_stream) {
098ea084 37 $sid = sqimap_session_id($unique_id);
38 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
39 $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query );
40 return $read;
c5809184 41 } else {
42 global $squirrelmail_language, $color;
43 set_up_language($squirrelmail_language);
44 require_once(SM_PATH . 'functions/display_messages.php');
45 $string = "<b><font color=$color[2]>\n" .
46 _("ERROR : No available imapstream.") .
47 "</b></font>\n";
48 error_box($string,$color);
098ea084 49 return false;
c5809184 50 }
51
1c72b151 52}
53
487daa81 54function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) {
c5809184 55 if ($imap_stream) {
56 $sid = sqimap_session_id($unique_id);
098ea084 57 fputs ($imap_stream, $sid . ' ' . $query . "\r\n");
58 $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query);
59 return $read;
c5809184 60 } else {
61 global $squirrelmail_language, $color;
62 set_up_language($squirrelmail_language);
63 require_once(SM_PATH . 'functions/display_messages.php');
64 $string = "<b><font color=$color[2]>\n" .
65 _("ERROR : No available imapstream.") .
66 "</b></font>\n";
67 error_box($string,$color);
098ea084 68 return false;
c5809184 69 }
70
42a07ac1 71}
72
9c737111 73
b8c285ab 74/*
75 * custom fgets function. gets a line from IMAP
76 * no matter how big it may be
77 */
78
79function sqimap_fgets($imap_stream) {
80 $read = '';
81 $buffer = 4096;
82 $results = '';
c41daf03 83 $offset = 0;
84 while (strpos($results, "\r\n", $offset) === false) {
b8c285ab 85 if (!($read = fgets($imap_stream, $buffer))) {
86 break;
87 }
c41daf03 88 if ( $results != '' ) {
89 $offset = strlen($results) - 1;
90 }
b8c285ab 91 $results .= $read;
92 }
93 return $results;
94}
95
bee165ef 96/*
3411d4ec 97 * Reads the output from the IMAP stream. If handle_errors is set to true,
98 * this will also handle all errors that are received. If it is not set,
99 * the errors will be sent back through $response and $message
bee165ef 100 */
b8c285ab 101
3411d4ec 102function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
9c737111 103 global $color, $squirrelmail_language;
9c737111 104 $read = '';
487daa81 105 $pre_a = explode(' ',trim($pre));
106 $pre = $pre_a[0];
b8c285ab 107 $resultlist = array();
108 $data = array();
109 $read = sqimap_fgets($imap_stream);
110 while (1) {
111 switch (true) {
112 case preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read, $regs):
113 case preg_match('/^\* (BYE \[ALERT\])(.*)$/', $read, $regs):
114 $response = $regs[1];
115 $message = trim($regs[2]);
116 break 2;
117 case preg_match("/^\* (OK \[PARSE\])(.*)$/", $read):
118 $read = sqimap_fgets($imap_stream);
119 break 1;
56afb33f 120 case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs):
b8c285ab 121 $fetch_data = array();
122 $fetch_data[] = $read;
123 $read = sqimap_fgets($imap_stream);
863936bb 124 while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) &&
b8c285ab 125 !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) {
126 $fetch_data[] = $read;
863936bb 127 $last = $read;
b8c285ab 128 $read = sqimap_fgets($imap_stream);
9c737111 129 }
863936bb 130 if (isset($last) && preg_match('/^\)/', $last)) {
131 array_pop($fetch_data);
132 }
b8c285ab 133 $resultlist[] = $fetch_data;
134 break 1;
135 default:
136 $data[] = $read;
137 $read = sqimap_fgets($imap_stream);
138 break 1;
9c737111 139 }
b8c285ab 140 }
141 if (!empty($data)) {
9c737111 142 $resultlist[] = $data;
143 }
b8c285ab 144 elseif (empty($resultlist)) {
145 $resultlist[] = array();
146 }
bee165ef 147 if ($handle_errors == false) {
148 return( $resultlist );
b8c285ab 149 }
150 elseif ($response == 'NO') {
151 /* ignore this error from M$ exchange, it is not fatal (aka bug) */
9c737111 152 if (strstr($message, 'command resulted in') === false) {
441f2d33 153 set_up_language($squirrelmail_language);
098ea084 154 require_once(SM_PATH . 'functions/display_messages.php');
155 $string = "<b><font color=$color[2]>\n" .
b8c285ab 156 _("ERROR : Could not complete request.") .
157 "</b><br>\n" .
9b761dbd 158 _("Query:") . ' ' .
159 htmlspecialchars($query) . '<br>' .
b8c285ab 160 _("Reason Given: ") .
9b761dbd 161 htmlspecialchars($message) . "</font><br>\n";
098ea084 162 error_box($string,$color);
052e0c26 163 exit;
9c737111 164 }
b8c285ab 165 }
166 elseif ($response == 'BAD') {
9c737111 167 set_up_language($squirrelmail_language);
098ea084 168 require_once(SM_PATH . 'functions/display_messages.php');
bef6629c 169 $string = "<b><font color=$color[2]>\n" .
b8c285ab 170 _("ERROR : Bad or malformed request.") .
171 "</b><br>\n" .
9b761dbd 172 _("Query:") . ' '.
173 htmlspecialchars($query) . '<br>' .
b8c285ab 174 _("Server responded: ") .
9b761dbd 175 htmlspecialchars($message) . "</font><br>\n";
098ea084 176 error_box($string,$color);
9c737111 177 exit;
b8c285ab 178 }
179 else {
3411d4ec 180 return $resultlist;
9c737111 181 }
9c737111 182}
183
b3837b0f 184function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') {
863936bb 185 $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query);
186
187 /* sqimap_read_data should be called for one response
188 but since it just calls sqimap_read_data_list which
189 handles multiple responses we need to check for that
190 and merge the $res array IF they are seperated and
191 IF it was a FETCH response. */
192
193 if (isset($res[1]) && is_array($res[1]) && isset($res[1][0])
194 && preg_match('/^\* \d+ FETCH/', $res[1][0])) {
195 $result = array();
196 foreach($res as $index=>$value) {
197 $result = array_merge($result, $res["$index"]);
56afb33f 198 }
199 }
863936bb 200 if (isset($result)) {
201 return $result;
56afb33f 202 }
56afb33f 203 else {
863936bb 204 return $res[0];
56afb33f 205 }
206
9c737111 207}
208
3411d4ec 209/*
210 * Logs the user into the imap server. If $hide is set, no error messages
211 * will be displayed. This function returns the imap connection handle.
212 */
9c737111 213function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) {
47a29326 214 global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, $imap_auth_mech;
85fc999e 215
eb2f6102 216 if (!isset($onetimepad) || empty($onetimepad)) {
217 sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION );
218 }
bd9829d7 219 $imap_server_address = sqimap_get_user_server($imap_server_address, $username);
098ea084 220 $host=$imap_server_address;
221
222 if (($use_imap_tls == true) and (check_php_version(4,3)) and (extension_loaded('openssl'))) {
223 /* Use TLS by prefixing "tls://" to the hostname */
224 $imap_server_address = 'tls://' . $imap_server_address;
225 }
47a29326 226
3411d4ec 227 $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15);
85fc999e 228
3411d4ec 229 /* Do some error correction */
9c737111 230 if (!$imap_stream) {
231 if (!$hide) {
441f2d33 232 set_up_language($squirrelmail_language, true);
098ea084 233 require_once(SM_PATH . 'functions/display_messages.php');
234 $string = sprintf (_("Error connecting to IMAP server: %s.") .
235 "<br>\r\n", $imap_server_address) .
1f720b34 236 "$error_number : $error_string<br>\r\n";
098ea084 237 logout_error($string,$color);
9c737111 238 }
239 exit;
240 }
052e0c26 241
2f1f7a12 242 $server_info = fgets ($imap_stream, 1024);
243
244 /* Decrypt the password */
245 $password = OneTimePadDecrypt($password, $onetimepad);
246
098ea084 247 if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) {
fe0b18b3 248 // We're using some sort of authentication OTHER than plain or login
098ea084 249 $tag=sqimap_session_id(false);
250 if ($imap_auth_mech == 'digest-md5') {
251 $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n";
252 } elseif ($imap_auth_mech == 'cram-md5') {
253 $query = $tag . " AUTHENTICATE CRAM-MD5\r\n";
254 }
255 fputs($imap_stream,$query);
256 $answer=sqimap_fgets($imap_stream);
257 // Trim the "+ " off the front
258 $response=explode(" ",$answer,3);
259 if ($response[0] == '+') {
260 // Got a challenge back
261 $challenge=$response[1];
262 if ($imap_auth_mech == 'digest-md5') {
263 $reply = digest_md5_response($username,$password,$challenge,'imap',$host);
264 } elseif ($imap_auth_mech == 'cram-md5') {
265 $reply = cram_md5_response($username,$password,$challenge);
266 }
267 fputs($imap_stream,$reply);
268 $read=sqimap_fgets($imap_stream);
269 if ($imap_auth_mech == 'digest-md5') {
270 // DIGEST-MD5 has an extra step..
271 if (substr($read,0,1) == '+') { // OK so far..
272 fputs($imap_stream,"\r\n");
273 $read=sqimap_fgets($imap_stream);
274 }
275 }
276 $results=explode(" ",$read,3);
277 $response=$results[1];
278 $message=$results[2];
47a29326 279 } else {
098ea084 280 // Fake the response, so the error trap at the bottom will work
281 $response="BAD";
282 $message='IMAP server does not appear to support the authentication method selected.';
283 $message .= ' Please contact your system administrator.';
47a29326 284 }
fe0b18b3 285 } elseif ($imap_auth_mech == 'login') {
098ea084 286 // Original IMAP login code
fbb76d0e 287 $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"';
47a29326 288 $read = sqimap_run_command ($imap_stream, $query, false, $response, $message);
1e7fc1cb 289 } elseif ($imap_auth_mech == 'plain') {
098ea084 290 /* Replace this with SASL PLAIN if it ever gets implemented */
291 $response="BAD";
292 $message='SquirrelMail does not support SASL PLAIN yet. Rerun conf.pl and use login instead.';
293 } else {
294 $response="BAD";
295 $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers.";
296 }
47a29326 297
098ea084 298 /* If the connection was not successful, lets see why */
9c737111 299 if ($response != 'OK') {
300 if (!$hide) {
74424a43 301 if ($response != 'NO') {
3411d4ec 302 /* "BAD" and anything else gets reported here. */
098ea084 303 $message = htmlspecialchars($message);
9c737111 304 set_up_language($squirrelmail_language, true);
098ea084 305 require_once(SM_PATH . 'functions/display_messages.php');
9c737111 306 if ($response == 'BAD') {
1f720b34 307 $string = sprintf (_("Bad request: %s")."<br>\r\n", $message);
9c737111 308 } else {
1f720b34 309 $string = sprintf (_("Unknown error: %s") . "<br>\n", $message);
9c737111 310 }
1e7fc1cb 311 if (isset($read) && is_array($read)) {
098ea084 312 $string .= '<br>' . _("Read data:") . "<br>\n";
9c737111 313 foreach ($read as $line) {
1f720b34 314 $string .= htmlspecialchars($line) . "<br>\n";
9c737111 315 }
316 }
098ea084 317 error_box($string,$color);
9c737111 318 exit;
165e24a7 319 } else {
3411d4ec 320 /*
321 * If the user does not log in with the correct
1c72b151 322 * username and password it is not possible to get the
323 * correct locale from the user's preferences.
324 * Therefore, apply the same hack as on the login
325 * screen.
3411d4ec 326 *
327 * $squirrelmail_language is set by a cookie when
1c72b151 328 * the user selects language and logs out
bee165ef 329 */
9be8198d 330
9c737111 331 set_up_language($squirrelmail_language, true);
bd9c880b 332 include_once(SM_PATH . 'functions/display_messages.php' );
098ea084 333 sqsession_destroy();
bd9c880b 334 logout_error( _("Unknown user or password incorrect.") );
9c737111 335 exit;
052e0c26 336 }
9c737111 337 } else {
052e0c26 338 exit;
9c737111 339 }
340 }
9c737111 341 return $imap_stream;
342}
f1e6f580 343
3411d4ec 344/* Simply logs out the IMAP session */
9c737111 345function sqimap_logout ($imap_stream) {
8d936b0c 346 /* Logout is not valid until the server returns 'BYE'
347 * If we don't have an imap_ stream we're already logged out */
26a2cc8b 348 if(isset($imap_stream) && $imap_stream)
8d936b0c 349 sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message);
9c737111 350}
351
487daa81 352function sqimap_capability($imap_stream, $capability='') {
9c737111 353 global $sqimap_capabilities;
9c737111 354 if (!is_array($sqimap_capabilities)) {
1c72b151 355 $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b);
356
9c737111 357 $c = explode(' ', $read[0]);
358 for ($i=2; $i < count($c); $i++) {
359 $cap_list = explode('=', $c[$i]);
3411d4ec 360 if (isset($cap_list[1])) {
9c737111 361 $sqimap_capabilities[$cap_list[0]] = $cap_list[1];
3411d4ec 362 } else {
9c737111 363 $sqimap_capabilities[$cap_list[0]] = TRUE;
3411d4ec 364 }
f1e6f580 365 }
9c737111 366 }
487daa81 367 if ($capability) {
098ea084 368 if (isset($sqimap_capabilities[$capability])) {
369 return $sqimap_capabilities[$capability];
370 } else {
371 return false;
372 }
f1e6f580 373 }
487daa81 374 return $sqimap_capabilities;
9c737111 375}
376
3411d4ec 377/* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */
9c737111 378function sqimap_get_delimiter ($imap_stream = false) {
3411d4ec 379 global $sqimap_delimiter, $optional_delimiter;
85fc999e 380
9c737111 381 /* Use configured delimiter if set */
382 if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') {
383 return $optional_delimiter;
384 }
85fc999e 385
9c737111 386 /* Do some caching here */
387 if (!$sqimap_delimiter) {
388 if (sqimap_capability($imap_stream, 'NAMESPACE')) {
3411d4ec 389 /*
390 * According to something that I can't find, this is supposed to work on all systems
391 * OS: This won't work in Courier IMAP.
392 * OS: According to rfc2342 response from NAMESPACE command is:
393 * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES)
394 * OS: We want to lookup all personal NAMESPACES...
395 */
1c72b151 396 $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b);
f1e6f580 397 if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) {
9c737111 398 if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) {
f1e6f580 399 $pn = $data2[1];
9c737111 400 }
f1e6f580 401 $pna = explode(')(', $pn);
9c737111 402 while (list($k, $v) = each($pna)) {
862ff2d3 403 $lst = explode('"', $v);
404 if (isset($lst[3])) {
405 $pn[$lst[1]] = $lst[3];
406 } else {
74424a43 407 $pn[$lst[1]] = '';
862ff2d3 408 }
f1e6f580 409 }
410 }
411 $sqimap_delimiter = $pn[0];
412 } else {
413 fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n");
414 $read = sqimap_read_data($imap_stream, '.', true, $a, $b);
415 $quote_position = strpos ($read[0], '"');
416 $sqimap_delimiter = substr ($read[0], $quote_position+1, 1);
417 }
418 }
419 return $sqimap_delimiter;
9c737111 420}
052e0c26 421
422
3411d4ec 423/* Gets the number of messages in the current mailbox. */
9c737111 424function sqimap_get_num_messages ($imap_stream, $mailbox) {
1f720b34 425 $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", false, $result, $message);
9c737111 426 for ($i = 0; $i < count($read_ary); $i++) {
85fc999e 427 if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) {
428 return $regs[1];
429 }
9c737111 430 }
1f720b34 431 return false; //"BUG! Couldn't get number of messages in $mailbox!";
9c737111 432}
433
cfe1a81e 434
267b2e40 435function parseAddress($address, $max=0, $addr_ar = array(), $group = '', $host='', $limit=0) {
098ea084 436 $pos = 0;
437 $j = strlen($address);
438 $personal = '';
439 $addr = '';
440 $comment = '';
267b2e40 441 if ($max && $max == count($addr_ar)) {
098ea084 442 return $addr_ar;
9c737111 443 }
098ea084 444 while ($pos < $j) {
267b2e40 445 if ($max && $max == count($addr_ar)) {
098ea084 446 return $addr_ar;
092d4f2c 447 }
098ea084 448 $char = $address{$pos};
449 switch ($char) {
450 case '=':
267b2e40 451 /* check if it is an encoded string */
098ea084 452 if (preg_match('/^(=\?([^?]*)\?(Q|B)\?([^?]*)\?=)(.*)/Ui',substr($address,$pos),$reg)) {
267b2e40 453 /* add stringpart before the encoded string to the personal var */
c97645d0 454 if (!$personal) {
455 $personal = substr($address,0,$pos);
456 }
c96c32f4 457 $personal .= $reg[1];
267b2e40 458 $pos += strlen($reg[1]);
459 } else {
460 ++$pos;
461 }
098ea084 462 break;
463 case '"': /* get the personal name */
464 ++$pos;
465 if ($address{$pos} == '"') {
466 ++$pos;
467 } else {
468 $personal_start = $personal_end = $pos;
469 while ($pos < $j) {
470 $personal_end = strpos($address,'"',$pos);
471 if (($personal_end-2)>0 && (substr($address,$personal_end-2,2) === '\\"' ||
472 substr($address,$personal_end-2,2) === '\\\\')) {
473 $pos = $personal_end+1;
474 } else {
475 $personal = substr($address,$personal_start,$personal_end-$personal_start);
476 break;
477 }
478 }
479 if ($personal_end) { /* prohibit endless loops due to very wrong addresses */
480 $pos = $personal_end+1;
481 } else {
482 $pos = $j;
483 }
484 }
485 break;
486 case '<': /* get email address */
487 $addr_start = $pos;
488 $addr_end = strpos($address,'>',$addr_start);
04bfe52b 489 if($addr_end === FALSE) {
490 // in case the address doesn't end, prevent loop
491 $pos++;
492 } else {
493 $addr = substr($address,$addr_start+1,$addr_end-$addr_start-1);
494 $pos = $addr_end+1;
495 }
098ea084 496 break;
497 case '(': /* rip off comments */
498 $addr_start = $pos;
499 $pos = strpos($address,')');
500 if ($pos !== false) {
501 $comment = substr($address, $addr_start+1,($pos-$addr_start-1));
502 $address_start = substr($address, 0, $addr_start);
503 $address_end = substr($address, $pos + 1);
504 $address = $address_start . $address_end;
505 }
506 $j = strlen($address);
507 $pos = $addr_start + 1;
508 break;
267b2e40 509 case ';': /* we reached a non rfc2822 compliant delimiter */
510 if ($group) {
511 $address = substr($address, 0, $pos - 1);
512 ++$pos;
513 break;
514 }
098ea084 515 case ',': /* we reached a delimiter */
516 if ($addr == '') {
517 $addr = substr($address, 0, $pos);
518 } else if ($personal == '') {
519 $personal = trim(substr($address, 0, $addr_start));
520 }
521 if (!$personal && $comment) $personal = $comment;
522 if ($personal) $personal = decodeHeader($personal);
523 $addr_ar[] = array($addr,$personal);
524 $address = trim(substr($address, $pos+1));
525 $j = strlen($address);
526 $pos = 0;
527 $personal = '';
528 $addr = '';
529 break;
530 case ':': /* process the group addresses */
531 /* group marker */
532 $group = substr($address, 0, $pos);
533 $address = substr($address, $pos+1);
534 $result = parseAddress($address, $max, $addr_ar, $group);
7b8d923b 535 $addr_ar = $result[0];
098ea084 536 $pos = $result[1];
537 $address = substr($address, $pos++);
538 $j = strlen($address);
539 $group = '';
540 break;
098ea084 541 default:
542 ++$pos;
543 break;
33565ec4 544 }
7e3de682 545 }
098ea084 546 if ($addr == '') {
547 $addr = substr($address, 0, $pos);
548 } else if ($personal == '') {
549 $personal = trim(substr($address, 0, $addr_start));
7e3de682 550 }
098ea084 551 if (!$personal && $comment) $personal = $comment;
552 $email = $addr;
553 if ($group && $addr == '') { /* no addresses found in group */
554 $personal = $group;
555 $addr_ar[] = array('',$personal);
556 return (array($addr_ar,$pos+1 ));
557 } elseif ($group) {
558 $addr_ar[] = array($addr,$personal);
559 return (array($addr_ar,$pos+1 ));
560 } else {
561 if ($personal || $addr) {
562 $addr_ar[] = array($addr, $personal);
563 }
9c737111 564 }
098ea084 565 return ($addr_ar);
7e3de682 566}
85fc999e 567
9c737111 568/*
3411d4ec 569 * Returns the number of unseen messages in this folder
570 */
9c737111 571function sqimap_unseen_messages ($imap_stream, $mailbox) {
1f720b34 572 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", false, $result, $message);
ea7ff111 573 $i = 0;
1f720b34 574 $regs = array(false, false);
ea7ff111 575 while (isset($read_ary[$i])) {
576 if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) {
577 break;
578 }
579 $i++;
580 }
9c737111 581 return $regs[1];
582}
583
1f720b34 584/*
585 * Returns the number of unseen/total messages in this folder
586 */
587function sqimap_status_messages ($imap_stream, $mailbox) {
b8ec18ef 588 $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (MESSAGES UNSEEN RECENT)", false, $result, $message);
1f720b34 589 $i = 0;
b8ec18ef 590 $messages = $unseen = $recent = false;
1f720b34 591 $regs = array(false,false);
592 while (isset($read_ary[$i])) {
593 if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) {
098ea084 594 $unseen = $regs[1];
595 }
1f720b34 596 if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) {
098ea084 597 $messages = $regs[1];
598 }
b8ec18ef 599 if (preg_match('/RECENT\s+([0-9]+)/i', $read_ary[$i], $regs)) {
098ea084 600 $recent = $regs[1];
601 }
1f720b34 602 $i++;
603 }
b8ec18ef 604 return array('MESSAGES' => $messages, 'UNSEEN'=>$unseen, 'RECENT' => $recent);
1f720b34 605}
606
9c737111 607
608/*
3411d4ec 609 * Saves a message to a given folder -- used for saving sent messages
610 */
9c737111 611function sqimap_append ($imap_stream, $sent_folder, $length) {
612 fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n");
85fc999e 613 $tmp = fgets ($imap_stream, 1024);
9c737111 614}
615
8813fb15 616function sqimap_append_done ($imap_stream, $folder='') {
1f720b34 617 global $squirrelmail_language, $color;
9c737111 618 fputs ($imap_stream, "\r\n");
619 $tmp = fgets ($imap_stream, 1024);
69146537 620 if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) {
621 set_up_language($squirrelmail_language);
1f720b34 622 require_once(SM_PATH . 'functions/display_messages.php');
098ea084 623 $reason = $regs[3];
624 if ($regs[2] == 'NO') {
625 $string = "<b><font color=$color[2]>\n" .
626 _("ERROR : Could not append message to") ." $folder." .
627 "</b><br>\n" .
628 _("Server responded: ") .
629 $reason . "<br>\n";
630 if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) {
631 $string .= _("Solution: ") .
632 _("Remove unneccessary messages from your folder and start with your Trash folder.")
633 ."<br>\n";
634 }
635 $string .= "</font>\n";
636 error_box($string,$color);
637 } else {
8813fb15 638 $string = "<b><font color=$color[2]>\n" .
098ea084 639 _("ERROR : Bad or malformed request.") .
640 "</b><br>\n" .
641 _("Server responded: ") .
642 $tmp . "</font><br>\n";
643 error_box($string,$color);
8813fb15 644 exit;
098ea084 645 }
69146537 646 }
9c737111 647}
85fc999e 648
bd9829d7 649function sqimap_get_user_server ($imap_server, $username) {
bd9829d7 650 if (substr($imap_server, 0, 4) != "map:") {
651 return $imap_server;
652 }
bd9829d7 653 $function = substr($imap_server, 4);
654 return $function($username);
655}
656
657/* This is an example that gets imapservers from yellowpages (NIS).
658 * you can simple put map:map_yp_alias in your $imap_server_address
659 * in config.php use your own function instead map_yp_alias to map your
660 * LDAP whatever way to find the users imapserver. */
661
662function map_yp_alias($username) {
663 $yp = `ypmatch $username aliases`;
664 return chop(substr($yp, strlen($username)+1));
665}
666
15e6162e 667?>