| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap.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 | function sqimap_session_id() { |
| 25 | |
| 26 | global $data_dir, $username; |
| 27 | |
| 28 | $IMAPSessionID = substr(session_id(), -4); |
| 29 | if( $IMAPSessionID == '' ) { |
| 30 | $IMAPSessionID = 'A001'; |
| 31 | } |
| 32 | |
| 33 | return( $IMAPSessionID ); |
| 34 | } |
| 35 | |
| 36 | /****************************************************************************** |
| 37 | ** Both send a command and accept the result from the command. This is |
| 38 | ** to allow proper session number handling. |
| 39 | ******************************************************************************/ |
| 40 | |
| 41 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, &$message) { |
| 42 | fputs ($imap_stream, sqimap_session_id() . $query . "\r\n"); |
| 43 | $read = sqimap_read_data ($imap_stream, sqimap_session_id(), $handle_errors, $response, $message); |
| 44 | return $read; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /****************************************************************************** |
| 49 | ** Reads the output from the IMAP stream. If handle_errors is set to true, |
| 50 | ** this will also handle all errors that are received. If it is not set, |
| 51 | ** the errors will be sent back through $response and $message |
| 52 | ******************************************************************************/ |
| 53 | |
| 54 | function sqimap_read_data_list ($imap_stream, $pre, $handle_errors, |
| 55 | &$response, &$message) { |
| 56 | global $color, $squirrelmail_language; |
| 57 | |
| 58 | $read = ''; |
| 59 | $resultlist = array(); |
| 60 | |
| 61 | $more_msgs = true; |
| 62 | while ($more_msgs) { |
| 63 | $data = array(); |
| 64 | $total_size = 0; |
| 65 | while (strpos($read, "\n") === false) { |
| 66 | $read .= fgets($imap_stream, 9096); |
| 67 | } |
| 68 | |
| 69 | if (ereg("^\\* [0-9]+ FETCH.*\\{([0-9]+)\\}", $read, $regs)) { |
| 70 | $size = $regs[1]; |
| 71 | } else if (ereg("^\\* [0-9]+ FETCH", $read, $regs)) { |
| 72 | // Sizeless response, probably single-line |
| 73 | $size = -1; |
| 74 | $data[] = $read; |
| 75 | $read = fgets($imap_stream, 9096); |
| 76 | } else { |
| 77 | $size = -1; |
| 78 | } |
| 79 | while (1) { |
| 80 | while (strpos($read, "\n") === false) { |
| 81 | $read .= fgets($imap_stream, 9096); |
| 82 | } |
| 83 | // If we know the size, no need to look at the end parameters |
| 84 | if ($size > 0) { |
| 85 | if ($total_size == $size) { |
| 86 | // We've reached the end of this 'message', switch to the next one. |
| 87 | $data[] = $read; |
| 88 | break; |
| 89 | } else if ($total_size > $size) { |
| 90 | $difference = $total_size - $size; |
| 91 | $total_size = $total_size - strlen($read); |
| 92 | $data[] = substr ($read, 0, strlen($read)-$difference); |
| 93 | $read = substr ($read, strlen($read)-$difference, strlen($read)); |
| 94 | break; |
| 95 | } else { |
| 96 | $data[] = $read; |
| 97 | $read = fgets($imap_stream, 9096); |
| 98 | while (strpos($read, "\n") === false) { |
| 99 | $read .= fgets($imap_stream, 9096); |
| 100 | } |
| 101 | } |
| 102 | $total_size += strlen($read); |
| 103 | } else { |
| 104 | if (ereg("^$pre (OK|BAD|NO)(.*)", $read, $regs) || |
| 105 | (($size == -1) && ereg("^\\* [0-9]+ FETCH.*", $read, $regs))) { |
| 106 | break; |
| 107 | } else { |
| 108 | $data[] = $read; |
| 109 | $read = fgets ($imap_stream, 9096); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | while (($more_msgs = !ereg("^$pre (OK|BAD|NO)(.*)$", $read, $regs)) && |
| 115 | !ereg("^\\* [0-9]+ FETCH.*", $read, $regs)) { |
| 116 | $read = fgets($imap_stream, 9096); |
| 117 | } |
| 118 | $resultlist[] = $data; |
| 119 | } |
| 120 | $response = $regs[1]; |
| 121 | $message = trim($regs[2]); |
| 122 | |
| 123 | if ($handle_errors == false) { return $resultlist; } |
| 124 | |
| 125 | if ($response == 'NO') { |
| 126 | // ignore this error from m$ exchange, it is not fatal (aka bug) |
| 127 | if (strstr($message, 'command resulted in') === false) { |
| 128 | set_up_language($squirrelmail_language); |
| 129 | echo "<br><b><font color=$color[2]>\n" . |
| 130 | _("ERROR : Could not complete request.") . |
| 131 | "</b><br>\n" . |
| 132 | _("Reason Given: ") . |
| 133 | $message . "</font><br>\n"; |
| 134 | exit; |
| 135 | } |
| 136 | } else if ($response == 'BAD') { |
| 137 | set_up_language($squirrelmail_language); |
| 138 | echo "<br><b><font color=$color[2]>\n" . |
| 139 | _("ERROR : Bad or malformed request.") . |
| 140 | "</b><br>\n" . |
| 141 | _("Server responded: ") . |
| 142 | $message . "</font><br>\n"; |
| 143 | exit; |
| 144 | } |
| 145 | return $resultlist; |
| 146 | } |
| 147 | |
| 148 | function sqimap_read_data ($imap_stream, $pre, $handle_errors, &$response, &$message) { |
| 149 | |
| 150 | $res = sqimap_read_data_list($imap_stream, $pre, $handle_errors, $response, $message); |
| 151 | return $res[0]; |
| 152 | |
| 153 | } |
| 154 | |
| 155 | /****************************************************************************** |
| 156 | ** Logs the user into the imap server. If $hide is set, no error messages |
| 157 | ** will be displayed. This function returns the imap connection handle. |
| 158 | ******************************************************************************/ |
| 159 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 160 | |
| 161 | global $color, $squirrelmail_language, $HTTP_ACCEPT_LANGUAGE, $onetimepad; |
| 162 | |
| 163 | $imap_stream = fsockopen ( $imap_server_address, $imap_port, |
| 164 | $error_number, $error_string, 15); |
| 165 | if ( !$imap_stream ) { |
| 166 | return FALSE; |
| 167 | } |
| 168 | $server_info = fgets ($imap_stream, 1024); |
| 169 | |
| 170 | // Decrypt the password |
| 171 | $password = OneTimePadDecrypt($password, $onetimepad); |
| 172 | |
| 173 | /** Do some error correction **/ |
| 174 | if (!$imap_stream) { |
| 175 | if (!$hide) { |
| 176 | set_up_language($squirrelmail_language, true); |
| 177 | printf (_("Error connecting to IMAP server: %s.")."<br>\r\n", $imap_server_address); |
| 178 | echo "$error_number : $error_string<br>\r\n"; |
| 179 | } |
| 180 | exit; |
| 181 | } |
| 182 | |
| 183 | fputs ($imap_stream, sqimap_session_id() . ' LOGIN "' . quoteIMAP($username) . |
| 184 | '" "' . quoteIMAP($password) . "\"\r\n"); |
| 185 | $read = sqimap_read_data ($imap_stream, sqimap_session_id(), false, $response, $message); |
| 186 | |
| 187 | /** If the connection was not successful, lets see why **/ |
| 188 | if ($response != 'OK') { |
| 189 | if (!$hide) { |
| 190 | if ($response != 'NO') { |
| 191 | // "BAD" and anything else gets reported here. |
| 192 | set_up_language($squirrelmail_language, true); |
| 193 | if ($response == 'BAD') { |
| 194 | printf (_("Bad request: %s")."<br>\r\n", $message); |
| 195 | } else { |
| 196 | printf (_("Unknown error: %s") . "<br>\n", $message); |
| 197 | } |
| 198 | echo '<br>' . _("Read data:") . "<br>\n"; |
| 199 | if (is_array($read)) { |
| 200 | foreach ($read as $line) { |
| 201 | echo htmlspecialchars($line) . "<br>\n"; |
| 202 | } |
| 203 | } |
| 204 | exit; |
| 205 | } else { |
| 206 | // If the user does not log in with the correct |
| 207 | // username and password it is not possible to get the |
| 208 | // correct locale from the user's preferences. |
| 209 | // Therefore, apply the same hack as on the login |
| 210 | // screen. |
| 211 | |
| 212 | // $squirrelmail_language is set by a cookie when |
| 213 | // the user selects language and logs out |
| 214 | |
| 215 | set_up_language($squirrelmail_language, true); |
| 216 | |
| 217 | displayHtmlHeader( _("Unknown user or password incorrect.") ); |
| 218 | echo "<body bgcolor=\"#ffffff\">\n"; |
| 219 | error_username_password_incorrect(); |
| 220 | session_destroy(); |
| 221 | exit; |
| 222 | } |
| 223 | } else { |
| 224 | exit; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return $imap_stream; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Simply logs out the imap session |
| 233 | */ |
| 234 | function sqimap_logout ($imap_stream) { |
| 235 | fputs ($imap_stream, sqimap_session_id() . " LOGOUT\r\n"); |
| 236 | } |
| 237 | |
| 238 | function sqimap_capability($imap_stream, $capability) { |
| 239 | global $sqimap_capabilities; |
| 240 | |
| 241 | if (!is_array($sqimap_capabilities)) { |
| 242 | fputs ($imap_stream, sqimap_session_id() . " CAPABILITY\r\n"); |
| 243 | $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b); |
| 244 | |
| 245 | $c = explode(' ', $read[0]); |
| 246 | for ($i=2; $i < count($c); $i++) { |
| 247 | $cap_list = explode('=', $c[$i]); |
| 248 | if (isset($cap_list[1])) |
| 249 | $sqimap_capabilities[$cap_list[0]] = $cap_list[1]; |
| 250 | else |
| 251 | $sqimap_capabilities[$cap_list[0]] = TRUE; |
| 252 | } |
| 253 | } |
| 254 | if (! isset($sqimap_capabilities[$capability])) { |
| 255 | return false; |
| 256 | } else { |
| 257 | return $sqimap_capabilities[$capability]; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | /****************************************************************************** |
| 262 | ** Returns the delimeter between mailboxes: INBOX/Test, or INBOX.Test... |
| 263 | ******************************************************************************/ |
| 264 | function sqimap_get_delimiter ($imap_stream = false) { |
| 265 | |
| 266 | global $sqimap_delimiter; |
| 267 | global $optional_delimiter; |
| 268 | |
| 269 | /* Use configured delimiter if set */ |
| 270 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
| 271 | return $optional_delimiter; |
| 272 | } |
| 273 | |
| 274 | /* Do some caching here */ |
| 275 | if (!$sqimap_delimiter) { |
| 276 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
| 277 | /* According to something that I can't find, this is supposed to work on all systems |
| 278 | OS: This won't work in Courier IMAP. |
| 279 | OS: According to rfc2342 response from NAMESPACE command is: |
| 280 | OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
| 281 | OS: We want to lookup all personal NAMESPACES... |
| 282 | */ |
| 283 | fputs ($imap_stream, sqimap_session_id() . " NAMESPACE\r\n"); |
| 284 | $read = sqimap_read_data($imap_stream, sqimap_session_id(), true, $a, $b); |
| 285 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
| 286 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
| 287 | $pn = $data2[1]; |
| 288 | } |
| 289 | $pna = explode(')(', $pn); |
| 290 | while (list($k, $v) = each($pna)) { |
| 291 | $lst = explode('"', $v); |
| 292 | if (isset($lst[3])) { |
| 293 | $pn[$lst[1]] = $lst[3]; |
| 294 | } else { |
| 295 | $pn[$lst[1]] = ''; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | $sqimap_delimiter = $pn[0]; |
| 300 | } else { |
| 301 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
| 302 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
| 303 | $quote_position = strpos ($read[0], '"'); |
| 304 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
| 305 | } |
| 306 | } |
| 307 | return $sqimap_delimiter; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /* |
| 312 | * Gets the number of messages in the current mailbox. |
| 313 | */ |
| 314 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
| 315 | |
| 316 | fputs ($imap_stream, sqimap_session_id() . " EXAMINE \"$mailbox\"\r\n"); |
| 317 | $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message); |
| 318 | for ($i = 0; $i < count($read_ary); $i++) { |
| 319 | if (ereg("[^ ]+ +([^ ]+) +EXISTS", $read_ary[$i], $regs)) { |
| 320 | return $regs[1]; |
| 321 | } |
| 322 | } |
| 323 | return sprintf( "BUG! Couldn't get number of messages in %s!", $mailbox ); |
| 324 | |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* |
| 329 | * Returns a displayable email address |
| 330 | */ |
| 331 | function sqimap_find_email ($string) { |
| 332 | /** Luke Ehresman <lehresma@css.tayloru.edu> |
| 333 | ** <lehresma@css.tayloru.edu> |
| 334 | ** lehresma@css.tayloru.edu |
| 335 | ** |
| 336 | ** What about |
| 337 | ** lehresma@css.tayloru.edu (Luke Ehresman) |
| 338 | **/ |
| 339 | |
| 340 | if (ereg("<([^>]+)>", $string, $regs)) { |
| 341 | $string = $regs[1]; |
| 342 | } |
| 343 | return trim($string); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | /* |
| 348 | * Takes the From: field, and creates a displayable name. |
| 349 | * Luke Ehresman <lkehresman@yahoo.com> |
| 350 | * "Luke Ehresman" <lkehresman@yahoo.com> |
| 351 | * lkehresman@yahoo.com (Luke Ehresman) |
| 352 | * become: Luke Ehresman |
| 353 | * <lkehresman@yahoo.com> |
| 354 | * becomes: lkehresman@yahoo.com |
| 355 | */ |
| 356 | function sqimap_find_displayable_name ($string) { |
| 357 | $string = trim($string); |
| 358 | |
| 359 | if ( ereg('^(.+)<.*>', $string, $regs) ) { |
| 360 | $string = ereg_replace ('"', '', $regs[1] ); |
| 361 | } |
| 362 | elseif ( ereg('\((.*)\)', $string, $regs) ) { |
| 363 | if( ( $regs[1] == '' ) || ( $regs[1] == ' ' ) ){ |
| 364 | if ( ereg('^(.+) \(', $string, $regs) ) { |
| 365 | $string = ereg_replace( ' \(\)$', '', $string ); |
| 366 | } else { |
| 367 | $string = ' '; |
| 368 | } |
| 369 | } else { |
| 370 | $string = $regs[1]; |
| 371 | } |
| 372 | } |
| 373 | else { |
| 374 | $string = sqimap_find_email($string); |
| 375 | } |
| 376 | |
| 377 | return trim($string); |
| 378 | } |
| 379 | |
| 380 | /* |
| 381 | * Returns the number of unseen messages in this folder |
| 382 | */ |
| 383 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
| 384 | //fputs ($imap_stream, sqimap_session_id() . " SEARCH UNSEEN NOT DELETED\r\n"); |
| 385 | fputs ($imap_stream, sqimap_session_id() . " STATUS \"$mailbox\" (UNSEEN)\r\n"); |
| 386 | $read_ary = sqimap_read_data ($imap_stream, sqimap_session_id(), true, $result, $message); |
| 387 | ereg("UNSEEN ([0-9]+)", $read_ary[0], $regs); |
| 388 | return $regs[1]; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /* |
| 393 | * Saves a message to a given folder -- used for saving sent messages |
| 394 | */ |
| 395 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 396 | fputs ($imap_stream, sqimap_session_id() . " APPEND \"$sent_folder\" (\\Seen) \{$length}\r\n"); |
| 397 | $tmp = fgets ($imap_stream, 1024); |
| 398 | } |
| 399 | |
| 400 | function sqimap_append_done ($imap_stream) { |
| 401 | fputs ($imap_stream, "\r\n"); |
| 402 | $tmp = fgets ($imap_stream, 1024); |
| 403 | } |
| 404 | |
| 405 | ?> |