| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_general.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 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 | |
| 16 | global $sqimap_session_id; |
| 17 | $sqimap_session_id = 1; |
| 18 | |
| 19 | /* Sets an unique session id in order to avoid simultanous sessions crash. */ |
| 20 | function sqimap_session_id($unique_id = false) { |
| 21 | global $data_dir, $username, $sqimap_session_id; |
| 22 | if (!$unique_id) { |
| 23 | return( sprintf("A%03d", $sqimap_session_id++) ); |
| 24 | } else { |
| 25 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | /* |
| 30 | * Both send a command and accept the result from the command. |
| 31 | * This is to allow proper session number handling. |
| 32 | */ |
| 33 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
| 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 | |
| 50 | } |
| 51 | |
| 52 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
| 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 | |
| 69 | } |
| 70 | |
| 71 | |
| 72 | /* |
| 73 | * custom fgets function. gets a line from IMAP |
| 74 | * no matter how big it may be |
| 75 | */ |
| 76 | |
| 77 | function 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 | |
| 90 | /* |
| 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 |
| 94 | */ |
| 95 | |
| 96 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
| 97 | global $color, $squirrelmail_language; |
| 98 | $read = ''; |
| 99 | $pre_a = explode(' ',trim($pre)); |
| 100 | $pre = $pre_a[0]; |
| 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; |
| 114 | case preg_match('/^\* ([0-9]+) FETCH.*/', $read, $regs): |
| 115 | $fetch_data = array(); |
| 116 | $fetch_data[] = $read; |
| 117 | $read = sqimap_fgets($imap_stream); |
| 118 | while (!preg_match('/^\* [0-9]+ FETCH.*/', $read) && |
| 119 | !preg_match("/^$pre (OK|BAD|NO)(.*)$/", $read)) { |
| 120 | $fetch_data[] = $read; |
| 121 | $last = $read; |
| 122 | $read = sqimap_fgets($imap_stream); |
| 123 | } |
| 124 | if (isset($last) && preg_match('/^\)/', $last)) { |
| 125 | array_pop($fetch_data); |
| 126 | } |
| 127 | $resultlist[] = $fetch_data; |
| 128 | break 1; |
| 129 | default: |
| 130 | $data[] = $read; |
| 131 | $read = sqimap_fgets($imap_stream); |
| 132 | break 1; |
| 133 | } |
| 134 | } |
| 135 | if (!empty($data)) { |
| 136 | $resultlist[] = $data; |
| 137 | } |
| 138 | elseif (empty($resultlist)) { |
| 139 | $resultlist[] = array(); |
| 140 | } |
| 141 | if ($handle_errors == false) { |
| 142 | return( $resultlist ); |
| 143 | } |
| 144 | elseif ($response == 'NO') { |
| 145 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
| 146 | if (strstr($message, 'command resulted in') === false) { |
| 147 | set_up_language($squirrelmail_language); |
| 148 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 149 | $string = "<b><font color=$color[2]>\n" . |
| 150 | _("ERROR : Could not complete request.") . |
| 151 | "</b><br>\n" . |
| 152 | _("Query:") . |
| 153 | $query . '<br>' . |
| 154 | _("Reason Given: ") . |
| 155 | $message . "</font><br>\n"; |
| 156 | error_box($string,$color); |
| 157 | exit; |
| 158 | } |
| 159 | } |
| 160 | elseif ($response == 'BAD') { |
| 161 | set_up_language($squirrelmail_language); |
| 162 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 163 | $string = "<b><font color=$color[2]>\n" . |
| 164 | _("ERROR : Bad or malformed request.") . |
| 165 | "</b><br>\n" . |
| 166 | _("Query:") . |
| 167 | $query . '<br>' . |
| 168 | _("Server responded: ") . |
| 169 | $message . "</font><br>\n"; |
| 170 | error_box($string,$color); |
| 171 | exit; |
| 172 | } |
| 173 | else { |
| 174 | return $resultlist; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
| 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"]); |
| 192 | } |
| 193 | } |
| 194 | if (isset($result)) { |
| 195 | return $result; |
| 196 | } |
| 197 | else { |
| 198 | return $res[0]; |
| 199 | } |
| 200 | |
| 201 | } |
| 202 | |
| 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 | */ |
| 207 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 208 | global $color, $squirrelmail_language, $onetimepad; |
| 209 | |
| 210 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
| 211 | |
| 212 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, $error_number, $error_string, 15); |
| 213 | |
| 214 | /* Do some error correction */ |
| 215 | if (!$imap_stream) { |
| 216 | if (!$hide) { |
| 217 | set_up_language($squirrelmail_language, true); |
| 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"; |
| 222 | logout_error($string,$color); |
| 223 | } |
| 224 | exit; |
| 225 | } |
| 226 | |
| 227 | $server_info = fgets ($imap_stream, 1024); |
| 228 | |
| 229 | /* Decrypt the password */ |
| 230 | $password = OneTimePadDecrypt($password, $onetimepad); |
| 231 | |
| 232 | $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"'; |
| 233 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
| 234 | |
| 235 | /* If the connection was not successful, lets see why */ |
| 236 | if ($response != 'OK') { |
| 237 | if (!$hide) { |
| 238 | if ($response != 'NO') { |
| 239 | /* "BAD" and anything else gets reported here. */ |
| 240 | set_up_language($squirrelmail_language, true); |
| 241 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 242 | if ($response == 'BAD') { |
| 243 | $string = sprintf (_("Bad request: %s")."<br>\r\n", $message); |
| 244 | } else { |
| 245 | $string = sprintf (_("Unknown error: %s") . "<br>\n", $message); |
| 246 | } |
| 247 | $string .= '<br>' . _("Read data:") . "<br>\n"; |
| 248 | if (is_array($read)) { |
| 249 | foreach ($read as $line) { |
| 250 | $string .= htmlspecialchars($line) . "<br>\n"; |
| 251 | } |
| 252 | } |
| 253 | error_box($string,$color); |
| 254 | exit; |
| 255 | } else { |
| 256 | /* |
| 257 | * If the user does not log in with the correct |
| 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. |
| 262 | * |
| 263 | * $squirrelmail_language is set by a cookie when |
| 264 | * the user selects language and logs out |
| 265 | */ |
| 266 | |
| 267 | set_up_language($squirrelmail_language, true); |
| 268 | include_once(SM_PATH . 'functions/display_messages.php' ); |
| 269 | sqsession_destroy(); |
| 270 | logout_error( _("Unknown user or password incorrect.") ); |
| 271 | exit; |
| 272 | } |
| 273 | } else { |
| 274 | exit; |
| 275 | } |
| 276 | } |
| 277 | return $imap_stream; |
| 278 | } |
| 279 | |
| 280 | /* Simply logs out the IMAP session */ |
| 281 | function sqimap_logout ($imap_stream) { |
| 282 | /* Logout is not valid until the server returns 'BYE' |
| 283 | * If we don't have an imap_ stream we're already logged out */ |
| 284 | if(isset($imap_stream) && $imap_stream) |
| 285 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
| 286 | } |
| 287 | |
| 288 | function sqimap_capability($imap_stream, $capability='') { |
| 289 | global $sqimap_capabilities; |
| 290 | if (!is_array($sqimap_capabilities)) { |
| 291 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
| 292 | |
| 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; |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | if ($capability) { |
| 304 | if (isset($sqimap_capabilities[$capability])) { |
| 305 | return $sqimap_capabilities[$capability]; |
| 306 | } else { |
| 307 | return false; |
| 308 | } |
| 309 | } |
| 310 | return $sqimap_capabilities; |
| 311 | } |
| 312 | |
| 313 | /* Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test */ |
| 314 | function sqimap_get_delimiter ($imap_stream = false) { |
| 315 | global $sqimap_delimiter, $optional_delimiter; |
| 316 | |
| 317 | /* Use configured delimiter if set */ |
| 318 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
| 319 | return $optional_delimiter; |
| 320 | } |
| 321 | |
| 322 | /* Do some caching here */ |
| 323 | if (!$sqimap_delimiter) { |
| 324 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
| 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 | */ |
| 332 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
| 333 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
| 334 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
| 335 | $pn = $data2[1]; |
| 336 | } |
| 337 | $pna = explode(')(', $pn); |
| 338 | while (list($k, $v) = each($pna)) { |
| 339 | $lst = explode('"', $v); |
| 340 | if (isset($lst[3])) { |
| 341 | $pn[$lst[1]] = $lst[3]; |
| 342 | } else { |
| 343 | $pn[$lst[1]] = ''; |
| 344 | } |
| 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; |
| 356 | } |
| 357 | |
| 358 | |
| 359 | /* Gets the number of messages in the current mailbox. */ |
| 360 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
| 361 | $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", false, $result, $message); |
| 362 | for ($i = 0; $i < count($read_ary); $i++) { |
| 363 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
| 364 | return $regs[1]; |
| 365 | } |
| 366 | } |
| 367 | return false; //"BUG! Couldn't get number of messages in $mailbox!"; |
| 368 | } |
| 369 | |
| 370 | |
| 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 | */ |
| 379 | function sqimap_find_email ($string) { |
| 380 | if (ereg("<([^>]+)>", $string, $regs)) { |
| 381 | $string = $regs[1]; |
| 382 | } else if (ereg("([^ ]+@[^ ]+)", $string, $regs)) { |
| 383 | $string = $regs[1]; |
| 384 | } |
| 385 | return trim($string); |
| 386 | } |
| 387 | |
| 388 | |
| 389 | /* |
| 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 |
| 397 | */ |
| 398 | function sqimap_find_displayable_name ($string) { |
| 399 | $string = trim($string); |
| 400 | |
| 401 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
| 402 | $orig_string = $string; |
| 403 | $string = str_replace ('"', '', $regs[1] ); |
| 404 | if (trim($string) == '') { |
| 405 | $string = sqimap_find_email($orig_string); |
| 406 | } |
| 407 | if( $string == '' || $string == ' ' ){ |
| 408 | $string = ' '; |
| 409 | } |
| 410 | } |
| 411 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
| 412 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
| 413 | if ( ereg('^(.+) \(', $string, $regs) ) { |
| 414 | $string = ereg_replace( ' \(\)$', '', $string ); |
| 415 | } else { |
| 416 | $string = ' '; |
| 417 | } |
| 418 | } else { |
| 419 | $string = $regs[1]; |
| 420 | } |
| 421 | } |
| 422 | else { |
| 423 | $string = str_replace ('"', '', sqimap_find_email($string)); |
| 424 | } |
| 425 | |
| 426 | return trim($string); |
| 427 | } |
| 428 | |
| 429 | /* |
| 430 | * Returns the number of unseen messages in this folder |
| 431 | */ |
| 432 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
| 433 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", false, $result, $message); |
| 434 | $i = 0; |
| 435 | $regs = array(false, false); |
| 436 | while (isset($read_ary[$i])) { |
| 437 | if (ereg("UNSEEN ([0-9]+)", $read_ary[$i], $regs)) { |
| 438 | break; |
| 439 | } |
| 440 | $i++; |
| 441 | } |
| 442 | return $regs[1]; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Returns the number of unseen/total messages in this folder |
| 447 | */ |
| 448 | function 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 | |
| 465 | |
| 466 | /* |
| 467 | * Saves a message to a given folder -- used for saving sent messages |
| 468 | */ |
| 469 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 470 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
| 471 | $tmp = fgets ($imap_stream, 1024); |
| 472 | } |
| 473 | |
| 474 | function sqimap_append_done ($imap_stream, $folder='') { |
| 475 | global $squirrelmail_language, $color; |
| 476 | fputs ($imap_stream, "\r\n"); |
| 477 | $tmp = fgets ($imap_stream, 1024); |
| 478 | if (preg_match("/(.*)(BAD|NO)(.*)$/", $tmp, $regs)) { |
| 479 | set_up_language($squirrelmail_language); |
| 480 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 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)) { |
| 489 | $string .= _("Solution: ") . |
| 490 | _("Remove unneccessary messages from your folder and start with your Trash folder.") |
| 491 | ."<br>\n"; |
| 492 | } |
| 493 | $string .= "</font>\n"; |
| 494 | error_box($string,$color); |
| 495 | } else { |
| 496 | $string = "<b><font color=$color[2]>\n" . |
| 497 | _("ERROR : Bad or malformed request.") . |
| 498 | "</b><br>\n" . |
| 499 | _("Server responded: ") . |
| 500 | $tmp . "</font><br>\n"; |
| 501 | error_box($string,$color); |
| 502 | exit; |
| 503 | } |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | function sqimap_get_user_server ($imap_server, $username) { |
| 508 | if (substr($imap_server, 0, 4) != "map:") { |
| 509 | return $imap_server; |
| 510 | } |
| 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 | |
| 520 | function map_yp_alias($username) { |
| 521 | $yp = `ypmatch $username aliases`; |
| 522 | return chop(substr($yp, strlen($username)+1)); |
| 523 | } |
| 524 | |
| 525 | ?> |