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