| 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('../functions/page_header.php'); |
| 15 | require_once('../functions/display_messages.php'); |
| 16 | |
| 17 | /** |
| 18 | * Unique SessionId |
| 19 | * |
| 20 | * Sets an unique session id in order to avoid simultanous sessions crash. |
| 21 | * |
| 22 | * @return string a 4 chars unique string |
| 23 | */ |
| 24 | |
| 25 | global $sqimap_session_id; |
| 26 | $sqimap_session_id = 1; |
| 27 | function sqimap_session_id() { |
| 28 | |
| 29 | global $data_dir, $username, $sqimap_session_id; |
| 30 | |
| 31 | return( sprintf("A%03d", $sqimap_session_id++) ); |
| 32 | } |
| 33 | |
| 34 | /****************************************************************************** |
| 35 | ** Both send a command and accept the result from the command. This is |
| 36 | ** to allow proper session number handling. |
| 37 | ******************************************************************************/ |
| 38 | |
| 39 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message) { |
| 40 | $sid = sqimap_session_id(); |
| 41 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
| 42 | $read = sqimap_read_data_list ($imap_stream, $sid, $handle_errors, $response, $message, $query ); |
| 43 | return $read; |
| 44 | } |
| 45 | |
| 46 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) { |
| 47 | $sid = sqimap_session_id(); |
| 48 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
| 49 | $read = sqimap_read_data ($imap_stream, $sid, $handle_errors, $response, $message, $query); |
| 50 | return $read; |
| 51 | } |
| 52 | |
| 53 | |
| 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 | */ |
| 59 | |
| 60 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, |
| 61 | &$response, &$message, $query = '') { |
| 62 | global $color, $squirrelmail_language; |
| 63 | |
| 64 | $read = ''; |
| 65 | $bufsize = 9096; |
| 66 | $resultlist = array(); |
| 67 | |
| 68 | $more_msgs = true; |
| 69 | while ($more_msgs) { |
| 70 | $data = array(); |
| 71 | $total_size = 0; |
| 72 | |
| 73 | while (strpos($read, "\n") === false) { |
| 74 | if(!($read .= fgets($imap_stream, $bufsize))) { |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { |
| 80 | if (preg_match('/^\* [0-9]+ FETCH.*\{([0-9]+)\}/', $read, $regs)) { |
| 81 | $size = $regs[1]; |
| 82 | //} else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { |
| 83 | } else if (preg_match('/^\* [0-9]+ FETCH/', $read, $regs)) { |
| 84 | // Sizeless response, probably single-line |
| 85 | $size = -1; |
| 86 | $data[] = $read; |
| 87 | $read = fgets($imap_stream, $bufsize); |
| 88 | } else { |
| 89 | $size = -1; |
| 90 | } |
| 91 | while (1) { |
| 92 | while (strpos($read, "\n") === false) { |
| 93 | if(!($read .= fgets($imap_stream, $bufsize))) { |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | // If we know the size, no need to look at the end parameters |
| 98 | if ($size > 0) { |
| 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; |
| 111 | $read = fgets($imap_stream, $bufsize); |
| 112 | while (strpos($read, "\n") === false) { |
| 113 | $read .= fgets($imap_stream, $bufsize); |
| 114 | } |
| 115 | } |
| 116 | $total_size += strlen($read); |
| 117 | } else { |
| 118 | if (preg_match("/^$pre (OK|BAD|NO)(.*)/", $read, $regs) || |
| 119 | // if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || |
| 120 | (($size == -1) && preg_match('/^\* [0-9]+ FETCH.*/', $read, $regs))) { |
| 121 | break; |
| 122 | } else if ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) { |
| 123 | while ( preg_match('/^\* OK \[PARSE.*/', $read, $regs ) ) { |
| 124 | $read = fgets($imap_stream, $bufsize); |
| 125 | } |
| 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. |
| 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. |
| 134 | */ |
| 135 | $data[] = $read; |
| 136 | $read = fgets ($imap_stream, $bufsize); |
| 137 | } else if (preg_match('/^\* BYE \[ALERT\](.*)/', $read, $regs)) { |
| 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); |
| 146 | } else { |
| 147 | $data[] = $read; |
| 148 | $read = fgets ($imap_stream, $bufsize); |
| 149 | } |
| 150 | } |
| 151 | } |
| 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); |
| 158 | } |
| 159 | $resultlist[] = $data; |
| 160 | } |
| 161 | |
| 162 | $response = $regs[1]; |
| 163 | $message = trim($regs[2]); |
| 164 | |
| 165 | if ($handle_errors == false) { |
| 166 | return( $resultlist ); |
| 167 | } else if ($response == 'NO') { |
| 168 | // ignore this error from m$ exchange, it is not fatal (aka bug) |
| 169 | if (strstr($message, 'command resulted in') === false) { |
| 170 | set_up_language($squirrelmail_language); |
| 171 | echo "<br><b><font color=$color[2]>\n" . |
| 172 | _("ERROR : Could not complete request.") . |
| 173 | "</b><br>\n" . |
| 174 | _("Query:") . |
| 175 | $query . '<br>' . |
| 176 | _("Reason Given: ") . |
| 177 | $message . "</font><br>\n"; |
| 178 | exit; |
| 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" . |
| 185 | _("Query:") . |
| 186 | $query . '<br>' . |
| 187 | _("Server responded: ") . |
| 188 | $message . "</font><br>\n"; |
| 189 | exit; |
| 190 | } else { |
| 191 | return( $resultlist ); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message, $query = '') { |
| 196 | |
| 197 | $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message, $query); |
| 198 | return $res[0]; |
| 199 | |
| 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 | ******************************************************************************/ |
| 206 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 207 | |
| 208 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
| 209 | |
| 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); |
| 216 | |
| 217 | // Decrypt the password |
| 218 | $password = OneTimePadDecrypt($password, $onetimepad); |
| 219 | |
| 220 | /** Do some error correction **/ |
| 221 | if (!$imap_stream) { |
| 222 | if (!$hide) { |
| 223 | set_up_language($squirrelmail_language, true); |
| 224 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
| 225 | echo "$error_number : $error_string<br>\r\n"; |
| 226 | } |
| 227 | exit; |
| 228 | } |
| 229 | |
| 230 | $query = 'LOGIN "' . quoteIMAP($username) . '" "' . quoteIMAP($password) . '"'; |
| 231 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
| 232 | |
| 233 | /** If the connection was not successful, lets see why **/ |
| 234 | if ($response != 'OK') { |
| 235 | if (!$hide) { |
| 236 | if ($response != 'NO') { |
| 237 | // "BAD" and anything else gets reported here. |
| 238 | set_up_language($squirrelmail_language, true); |
| 239 | if ($response == 'BAD') { |
| 240 | printf (_("Bad request: %s")."<br>\r\n", $message); |
| 241 | } else { |
| 242 | printf (_("Unknown error: %s") . "<br>\n", $message); |
| 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; |
| 251 | } else { |
| 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. |
| 257 | */ |
| 258 | |
| 259 | /* $squirrelmail_language is set by a cookie when |
| 260 | * the user selects language and logs out |
| 261 | */ |
| 262 | |
| 263 | set_up_language($squirrelmail_language, true); |
| 264 | |
| 265 | displayHtmlHeader( _("Unknown user or password incorrect.") ); |
| 266 | echo "<body bgcolor=\"#ffffff\">\n"; |
| 267 | error_username_password_incorrect(); |
| 268 | session_destroy(); |
| 269 | exit; |
| 270 | } |
| 271 | } else { |
| 272 | exit; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | return $imap_stream; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Simply logs out the imap session |
| 281 | */ |
| 282 | function sqimap_logout ($imap_stream) { |
| 283 | /* Logout is not valid until the server returns 'BYE' */ |
| 284 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
| 285 | } |
| 286 | |
| 287 | function sqimap_capability($imap_stream, $capability) { |
| 288 | global $sqimap_capabilities; |
| 289 | |
| 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 | if (! isset($sqimap_capabilities[$capability])) { |
| 303 | return false; |
| 304 | } else { |
| 305 | return $sqimap_capabilities[$capability]; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | /****************************************************************************** |
| 310 | ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test... |
| 311 | ******************************************************************************/ |
| 312 | function sqimap_get_delimiter ($imap_stream = false) { |
| 313 | |
| 314 | global $sqimap_delimiter; |
| 315 | global $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 | /* 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 | */ |
| 331 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
| 332 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
| 333 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
| 334 | $pn = $data2[1]; |
| 335 | } |
| 336 | $pna = explode(')(', $pn); |
| 337 | while (list($k, $v) = each($pna)) { |
| 338 | $lst = explode('"', $v); |
| 339 | if (isset($lst[3])) { |
| 340 | $pn[$lst[1]] = $lst[3]; |
| 341 | } else { |
| 342 | $pn[$lst[1]] = ''; |
| 343 | } |
| 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; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | /* |
| 359 | * Gets the number of messages in the current mailbox. |
| 360 | */ |
| 361 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
| 362 | |
| 363 | $read_ary = sqimap_run_command ($imap_stream, "EXAMINE \"$mailbox\"", true, $result, $message); |
| 364 | for ($i = 0; $i < count($read_ary); $i++) { |
| 365 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
| 366 | return $regs[1]; |
| 367 | } |
| 368 | } |
| 369 | return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox ); |
| 370 | |
| 371 | } |
| 372 | |
| 373 | |
| 374 | /* |
| 375 | * Returns a displayable email address |
| 376 | */ |
| 377 | function 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 | **/ |
| 385 | |
| 386 | if (ereg("<([^>]+)>", $string, $regs)) { |
| 387 | $string = $regs[1]; |
| 388 | } |
| 389 | return trim($string); |
| 390 | } |
| 391 | |
| 392 | |
| 393 | /* |
| 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) |
| 398 | * becomes: Luke Ehresman |
| 399 | * <lkehresman@yahoo.com> |
| 400 | * becomes: lkehresman@yahoo.com |
| 401 | */ |
| 402 | function sqimap_find_displayable_name ($string) { |
| 403 | $string = trim($string); |
| 404 | |
| 405 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
| 406 | $orig_string = $string; |
| 407 | $string = str_replace ('"', '', $regs[1] ); |
| 408 | if (trim($string) == '') { |
| 409 | $string = sqimap_find_email($orig_string); |
| 410 | } |
| 411 | if( $string == '' || $string == ' ' ){ |
| 412 | $string = ' '; |
| 413 | } |
| 414 | } |
| 415 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
| 416 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
| 417 | if ( ereg('^(.+) \(', $string, $regs) ) { |
| 418 | $string = ereg_replace( ' \(\)$', '', $string ); |
| 419 | } else { |
| 420 | $string = ' '; |
| 421 | } |
| 422 | } else { |
| 423 | $string = $regs[1]; |
| 424 | } |
| 425 | } |
| 426 | else { |
| 427 | $string = str_replace ('"', '', sqimap_find_email($string)); |
| 428 | } |
| 429 | |
| 430 | return trim($string); |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * Returns the number of unseen messages in this folder |
| 435 | */ |
| 436 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
| 437 | //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n"); |
| 438 | $read_ary = sqimap_run_command ($imap_stream, "STATUS \"$mailbox\" (UNSEEN)", true, $result, $message); |
| 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 | } |
| 446 | return $regs[1]; |
| 447 | } |
| 448 | |
| 449 | |
| 450 | /* |
| 451 | * Saves a message to a given folder -- used for saving sent messages |
| 452 | */ |
| 453 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 454 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
| 455 | $tmp = fgets ($imap_stream, 1024); |
| 456 | } |
| 457 | |
| 458 | function sqimap_append_done ($imap_stream) { |
| 459 | fputs ($imap_stream, "\r\n"); |
| 460 | $tmp = fgets ($imap_stream, 1024); |
| 461 | } |
| 462 | |
| 463 | ?> |