| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_general.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This implements all functions that do general imap functions. |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | require_once(SM_PATH . 'functions/page_header.php'); |
| 15 | require_once(SM_PATH . 'functions/auth.php'); |
| 16 | |
| 17 | |
| 18 | global $sqimap_session_id; |
| 19 | $sqimap_session_id = 1; |
| 20 | |
| 21 | /* Sets an unique session id in order to avoid simultanous sessions crash. */ |
| 22 | function sqimap_session_id($unique_id = false) { |
| 23 | global $data_dir, $username, $sqimap_session_id; |
| 24 | if (!$unique_id) { |
| 25 | return( sprintf("A%03d", $sqimap_session_id++) ); |
| 26 | } else { |
| 27 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * Both send a command and accept the result from the command. |
| 33 | * This is to allow proper session number handling. |
| 34 | */ |
| 35 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
| 36 | if ($imap_stream) { |
| 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; |
| 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); |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
| 55 | if ($imap_stream) { |
| 56 | $sid = sqimap_session_id($unique_id); |
| 57 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
| 58 | $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query); |
| 59 | return $read; |
| 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); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /* |
| 75 | * custom fgets function. gets a line from IMAP |
| 76 | * no matter how big it may be |
| 77 | */ |
| 78 | |
| 79 | function sqimap_fgets($imap_stream) { |
| 80 | $read = ''; |
| 81 | $buffer = 4096; |
| 82 | $results = ''; |
| 83 | $offset = 0; |
| 84 | while (strpos($results, "\r\n", $offset) === false) { |
| 85 | if (!($read = fgets($imap_stream, $buffer))) { |
| 86 | break; |
| 87 | } |
| 88 | if ( $results != '' ) { |
| 89 | $offset = strlen($results) - 1; |
| 90 | } |
| 91 | $results .= $read; |
| 92 | } |
| 93 | return $results; |
| 94 | } |
| 95 | |
| 96 | /* |
| 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 |
| 100 | */ |
| 101 | |
| 102 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
| 103 | global $color, $squirrelmail_language; |
| 104 | $read = ''; |
| 105 | $pre_a = explode(' ',trim($pre)); |
| 106 | $pre = $pre_a[0]; |
| 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; |
| 120 | case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs): |
| 121 | $fetch_data = array(); |
| 122 | $fetch_data[] = $read; |
| 123 | $read = sqimap_fgets($imap_stream); |
| 124 | while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) && |
| 125 | !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) { |
| 126 | $fetch_data[] = $read; |
| 127 | $last = $read; |
| 128 | $read = sqimap_fgets($imap_stream); |
| 129 | } |
| 130 | if (isset($last) && preg_match('/^\)/', $last)) { |
| 131 | array_pop($fetch_data); |
| 132 | } |
| 133 | $resultlist[] = $fetch_data; |
| 134 | break 1; |
| 135 | default: |
| 136 | $data[] = $read; |
| 137 | $read = sqimap_fgets($imap_stream); |
| 138 | break 1; |
| 139 | } |
| 140 | } |
| 141 | if (!empty($data)) { |
| 142 | $resultlist[] = $data; |
| 143 | } |
| 144 | elseif (empty($resultlist)) { |
| 145 | $resultlist[] = array(); |
| 146 | } |
| 147 | if ($handle_errors == false) { |
| 148 | return( $resultlist ); |
| 149 | } |
| 150 | elseif ($response == 'NO') { |
| 151 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
| 152 | if (strstr($message, 'command resulted in') === false) { |
| 153 | set_up_language($squirrelmail_language); |
| 154 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 155 | $string = "<b><font color=$color[2]>\n" . |
| 156 | _("ERROR : Could not complete request.") . |
| 157 | "</b><br>\n" . |
| 158 | _("Query:") . ' ' . |
| 159 | htmlspecialchars($query) . '<br>' . |
| 160 | _("Reason Given: ") . |
| 161 | htmlspecialchars($message) . "</font><br>\n"; |
| 162 | error_box($string,$color); |
| 163 | exit; |
| 164 | } |
| 165 | } |
| 166 | elseif ($response == 'BAD') { |
| 167 | set_up_language($squirrelmail_language); |
| 168 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 169 | $string = "<b><font color=$color[2]>\n" . |
| 170 | _("ERROR : Bad or malformed request.") . |
| 171 | "</b><br>\n" . |
| 172 | _("Query:") . ' '. |
| 173 | htmlspecialchars($query) . '<br>' . |
| 174 | _("Server responded: ") . |
| 175 | htmlspecialchars($message) . "</font><br>\n"; |
| 176 | error_box($string,$color); |
| 177 | exit; |
| 178 | } |
| 179 | else { |
| 180 | return $resultlist; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
| 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"]); |
| 198 | } |
| 199 | } |
| 200 | if (isset($result)) { |
| 201 | return $result; |
| 202 | } |
| 203 | else { |
| 204 | return $res[0]; |
| 205 | } |
| 206 | |
| 207 | } |
| 208 | |
| 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 | */ |
| 213 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 214 | global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, $imap_auth_mech; |
| 215 | |
| 216 | if (!isset($onetimepad) || empty($onetimepad)) { |
| 217 | sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION ); |
| 218 | } |
| 219 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
| 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 | } |
| 226 | |
| 227 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15); |
| 228 | |
| 229 | /* Do some error correction */ |
| 230 | if (!$imap_stream) { |
| 231 | if (!$hide) { |
| 232 | set_up_language($squirrelmail_language, true); |
| 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) . |
| 236 | "$error_number : $error_string<br>\r\n"; |
| 237 | logout_error($string,$color); |
| 238 | } |
| 239 | exit; |
| 240 | } |
| 241 | |
| 242 | $server_info = fgets ($imap_stream, 1024); |
| 243 | |
| 244 | /* Decrypt the password */ |
| 245 | $password = OneTimePadDecrypt($password, $onetimepad); |
| 246 | |
| 247 | if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) { |
| 248 | // We're using some sort of authentication OTHER than plain or login |
| 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]; |
| 279 | } else { |
| 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.'; |
| 284 | } |
| 285 | } elseif ($imap_auth_mech == 'login') { |
| 286 | // Original IMAP login code |
| 287 | $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"'; |
| 288 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
| 289 | } elseif ($imap_auth_mech == 'plain') { |
| 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 | } |
| 297 | |
| 298 | /* If the connection was not successful, lets see why */ |
| 299 | if ($response != 'OK') { |
| 300 | if (!$hide) { |
| 301 | if ($response != 'NO') { |
| 302 | /* "BAD" and anything else gets reported here. */ |
| 303 | $message = htmlspecialchars($message); |
| 304 | set_up_language($squirrelmail_language, true); |
| 305 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 306 | if ($response == 'BAD') { |
| 307 | $string = sprintf (_("Bad request: %s")."<br>\r\n", $message); |
| 308 | } else { |
| 309 | $string = sprintf (_("Unknown error: %s") . "<br>\n", $message); |
| 310 | } |
| 311 | if (isset($read) && is_array($read)) { |
| 312 | $string .= '<br>' . _("Read data:") . "<br>\n"; |
| 313 | foreach ($read as $line) { |
| 314 | $string .= htmlspecialchars($line) . "<br>\n"; |
| 315 | } |
| 316 | } |
| 317 | error_box($string,$color); |
| 318 | exit; |
| 319 | } else { |
| 320 | /* |
| 321 | * If the user does not log in with the correct |
| 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. |
| 326 | * |
| 327 | * $squirrelmail_language is set by a cookie when |
| 328 | * the user selects language and logs out |
| 329 | */ |
| 330 | |
| 331 | set_up_language($squirrelmail_language, true); |
| 332 | include_once(SM_PATH . 'functions/display_messages.php' ); |
| 333 | sqsession_destroy(); |
| 334 | logout_error( _("Unknown user or password incorrect.") ); |
| 335 | exit; |
| 336 | } |
| 337 | } else { |
| 338 | exit; |
| 339 | } |
| 340 | } |
| 341 | return $imap_stream; |
| 342 | } |
| 343 | |
| 344 | /* Simply logs out the IMAP session */ |
| 345 | function sqimap_logout ($imap_stream) { |
| 346 | /* Logout is not valid until the server returns 'BYE' |
| 347 | * If we don't have an imap_ stream we're already logged out */ |
| 348 | if(isset($imap_stream) && $imap_stream) |
| 349 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
| 350 | } |
| 351 | |
| 352 | function sqimap_capability($imap_stream, $capability='') { |
| 353 | global $sqimap_capabilities; |
| 354 | if (!is_array($sqimap_capabilities)) { |
| 355 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
| 356 | |
| 357 | $c = explode(' ', $read[0]); |
| 358 | for ($i=2; $i < count($c); $i++) { |
| 359 | $cap_list = explode('=', $c[$i]); |
| 360 | if (isset($cap_list[1])) { |
| 361 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
| 362 | } else { |
| 363 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | if ($capability) { |
| 368 | if (isset($sqimap_capabilities[$capability])) { |
| 369 | return $sqimap_capabilities[$capability]; |
| 370 | } else { |
| 371 | return false; |
| 372 | } |
| 373 | } |
| 374 | return $sqimap_capabilities; |
| 375 | } |
| 376 | |
| 377 | /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */ |
| 378 | function sqimap_get_delimiter ($imap_stream = false) { |
| 379 | global $sqimap_delimiter, $optional_delimiter; |
| 380 | |
| 381 | /* Use configured delimiter if set */ |
| 382 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
| 383 | return $optional_delimiter; |
| 384 | } |
| 385 | |
| 386 | /* Do some caching here */ |
| 387 | if (!$sqimap_delimiter) { |
| 388 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
| 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 | */ |
| 396 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
| 397 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
| 398 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
| 399 | $pn = $data2[1]; |
| 400 | } |
| 401 | $pna = explode(')(', $pn); |
| 402 | while (list($k, $v) = each($pna)) { |
| 403 | $lst = explode('"', $v); |
| 404 | if (isset($lst[3])) { |
| 405 | $pn[$lst[1]] = $lst[3]; |
| 406 | } else { |
| 407 | $pn[$lst[1]] = ''; |
| 408 | } |
| 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; |
| 420 | } |
| 421 | |
| 422 | |
| 423 | /* Gets the number of messages in the current mailbox. */ |
| 424 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
| 425 | $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", false, $result, $message); |
| 426 | for ($i = 0; $i < count($read_ary); $i++) { |
| 427 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
| 428 | return $regs[1]; |
| 429 | } |
| 430 | } |
| 431 | return false; //"BUG! Couldn't get number of messages in $mailbox!"; |
| 432 | } |
| 433 | |
| 434 | |
| 435 | /* Returns a displayable email address. |
| 436 | * Luke Ehresman <lehresma@css.tayloru.edu> |
| 437 | * "Luke Ehresman" <lehresma@css.tayloru.edu> |
| 438 | * <lehresma@css.tayloru.edu> |
| 439 | * lehresma@css.tayloru.edu (Luke Ehresman) |
| 440 | * lehresma@css.tayloru.edu |
| 441 | * becomes: lehresma@css.tayloru.edu |
| 442 | */ |
| 443 | function sqimap_find_email ($string) { |
| 444 | if (ereg("<([^>]+)>", $string, $regs)) { |
| 445 | $string = $regs[1]; |
| 446 | } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) { |
| 447 | $string = $regs[1]; |
| 448 | } |
| 449 | return trim($string); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | /* |
| 454 | * Takes the From: field and creates a displayable name. |
| 455 | * Luke Ehresman <lkehresman@yahoo.com> |
| 456 | * "Luke Ehresman" <lkehresman@yahoo.com> |
| 457 | * lkehresman@yahoo.com (Luke Ehresman) |
| 458 | * becomes: Luke Ehresman |
| 459 | * <lkehresman@yahoo.com> |
| 460 | * becomes: lkehresman@yahoo.com |
| 461 | */ |
| 462 | function sqimap_find_displayable_name ($string) { |
| 463 | $string = trim($string); |
| 464 | |
| 465 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
| 466 | $orig_string = $string; |
| 467 | $string = str_replace ('"', '', $regs[1] ); |
| 468 | if (trim($string) == '') { |
| 469 | $string = sqimap_find_email($orig_string); |
| 470 | } |
| 471 | if( $string == '' || $string == ' ' ){ |
| 472 | $string = ' '; |
| 473 | } |
| 474 | } |
| 475 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
| 476 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
| 477 | if ( ereg('^(.+) \(', $string, $regs) ) { |
| 478 | $string = ereg_replace( ' \(\)$', '', $string ); |
| 479 | } else { |
| 480 | $string = ' '; |
| 481 | } |
| 482 | } else { |
| 483 | $string = $regs[1]; |
| 484 | } |
| 485 | } |
| 486 | else { |
| 487 | $string = str_replace ('"', '', sqimap_find_email($string)); |
| 488 | } |
| 489 | |
| 490 | return trim($string); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * Returns the number of unseen messages in this folder |
| 495 | */ |
| 496 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
| 497 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", false, $result, $message); |
| 498 | $i = 0; |
| 499 | $regs = array(false, false); |
| 500 | while (isset($read_ary[$i])) { |
| 501 | if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) { |
| 502 | break; |
| 503 | } |
| 504 | $i++; |
| 505 | } |
| 506 | return $regs[1]; |
| 507 | } |
| 508 | |
| 509 | /* |
| 510 | * Returns the number of unseen/total messages in this folder |
| 511 | */ |
| 512 | function sqimap_status_messages ($imap_stream, $mailbox) { |
| 513 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (MESSAGES UNSEEN)", false, $result, $message); |
| 514 | $i = 0; |
| 515 | $messages = $unseen = false; |
| 516 | $regs = array(false,false); |
| 517 | while (isset($read_ary[$i])) { |
| 518 | if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 519 | $unseen = $regs[1]; |
| 520 | } |
| 521 | if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 522 | $messages = $regs[1]; |
| 523 | } |
| 524 | $i++; |
| 525 | } |
| 526 | return array('MESSAGES' => $messages, 'UNSEEN'=>$unseen); |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /* |
| 531 | * Saves a message to a given folder -- used for saving sent messages |
| 532 | */ |
| 533 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 534 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
| 535 | $tmp = fgets ($imap_stream, 1024); |
| 536 | } |
| 537 | |
| 538 | function sqimap_append_done ($imap_stream, $folder='') { |
| 539 | global $squirrelmail_language, $color; |
| 540 | fputs ($imap_stream, "\r\n"); |
| 541 | $tmp = fgets ($imap_stream, 1024); |
| 542 | if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) { |
| 543 | set_up_language($squirrelmail_language); |
| 544 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 545 | $reason = $regs[3]; |
| 546 | if ($regs[2] == 'NO') { |
| 547 | $string = "<b><font color=$color[2]>\n" . |
| 548 | _("ERROR : Could not append message to") ." $folder." . |
| 549 | "</b><br>\n" . |
| 550 | _("Server responded: ") . |
| 551 | $reason . "<br>\n"; |
| 552 | if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) { |
| 553 | $string .= _("Solution: ") . |
| 554 | _("Remove unneccessary messages from your folder and start with your Trash folder.") |
| 555 | ."<br>\n"; |
| 556 | } |
| 557 | $string .= "</font>\n"; |
| 558 | error_box($string,$color); |
| 559 | } else { |
| 560 | $string = "<b><font color=$color[2]>\n" . |
| 561 | _("ERROR : Bad or malformed request.") . |
| 562 | "</b><br>\n" . |
| 563 | _("Server responded: ") . |
| 564 | $tmp . "</font><br>\n"; |
| 565 | error_box($string,$color); |
| 566 | exit; |
| 567 | } |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | function sqimap_get_user_server ($imap_server, $username) { |
| 572 | if (substr($imap_server, 0, 4) != "map:") { |
| 573 | return $imap_server; |
| 574 | } |
| 575 | $function = substr($imap_server, 4); |
| 576 | return $function($username); |
| 577 | } |
| 578 | |
| 579 | /* This is an example that gets imapservers from yellowpages (NIS). |
| 580 | * you can simple put map:map_yp_alias in your $imap_server_address |
| 581 | * in config.php use your own function instead map_yp_alias to map your |
| 582 | * LDAP whatever way to find the users imapserver. */ |
| 583 | |
| 584 | function map_yp_alias($username) { |
| 585 | $yp = `ypmatch $username aliases`; |
| 586 | return chop(substr($yp, strlen($username)+1)); |
| 587 | } |
| 588 | |
| 589 | ?> |