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