| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_general.php |
| 5 | * |
| 6 | * This implements all functions that do general IMAP functions. |
| 7 | * |
| 8 | * @copyright © 1999-2006 The SquirrelMail Project Team |
| 9 | * @license http://opensource.org/licenses/gpl-license.php GNU Public License |
| 10 | * @version $Id$ |
| 11 | * @package squirrelmail |
| 12 | * @subpackage imap |
| 13 | */ |
| 14 | |
| 15 | /** Includes.. */ |
| 16 | require_once(SM_PATH . 'functions/page_header.php'); |
| 17 | require_once(SM_PATH . 'functions/auth.php'); |
| 18 | include_once(SM_PATH . 'functions/rfc822address.php'); |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * Generates a new session ID by incrementing the last one used; |
| 23 | * this ensures that each command has a unique ID. |
| 24 | * @param bool $unique_id (since 1.3.0) controls use of unique |
| 25 | * identifiers/message sequence numbers in IMAP commands. See IMAP |
| 26 | * rfc 'UID command' chapter. |
| 27 | * @return string IMAP session id of the form 'A000'. |
| 28 | * @since 1.2.0 |
| 29 | */ |
| 30 | function sqimap_session_id($unique_id = FALSE) { |
| 31 | static $sqimap_session_id = 1; |
| 32 | |
| 33 | if (!$unique_id) { |
| 34 | return( sprintf("A%03d", $sqimap_session_id++) ); |
| 35 | } else { |
| 36 | return( sprintf("A%03d", $sqimap_session_id++) . ' UID' ); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Both send a command and accept the result from the command. |
| 42 | * This is to allow proper session number handling. |
| 43 | * @param stream $imap_stream imap connection resource |
| 44 | * @param string $query imap command |
| 45 | * @param boolean $handle_errors see sqimap_retrieve_imap_response() |
| 46 | * @param array $response |
| 47 | * @param array $message |
| 48 | * @param boolean $unique_id (since 1.3.0) see sqimap_session_id(). |
| 49 | * @return mixed returns false on imap error. displays error message |
| 50 | * if imap stream is not available. |
| 51 | * @since 1.2.3 |
| 52 | */ |
| 53 | function sqimap_run_command_list ($imap_stream, $query, $handle_errors, &$response, &$message, $unique_id = false) { |
| 54 | if ($imap_stream) { |
| 55 | $sid = sqimap_session_id($unique_id); |
| 56 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
| 57 | $tag_uid_a = explode(' ',trim($sid)); |
| 58 | $tag = $tag_uid_a[0]; |
| 59 | $read = sqimap_retrieve_imap_response ($imap_stream, $tag, $handle_errors, $response, $message, $query ); |
| 60 | /* get the response and the message */ |
| 61 | $message = $message[$tag]; |
| 62 | $response = $response[$tag]; |
| 63 | return $read[$tag]; |
| 64 | } else { |
| 65 | global $squirrelmail_language, $color; |
| 66 | set_up_language($squirrelmail_language); |
| 67 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 68 | $string = "<b><font color=\"$color[2]\">\n" . |
| 69 | _("ERROR: No available IMAP stream.") . |
| 70 | "</b></font>\n"; |
| 71 | error_box($string,$color); |
| 72 | return false; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @param stream $imap_stream imap connection resource |
| 78 | * @param string $query imap command |
| 79 | * @param boolean $handle_errors see sqimap_retrieve_imap_response() |
| 80 | * @param array $response empty string, if return = false |
| 81 | * @param array $message empty string, if return = false |
| 82 | * @param boolean $unique_id (since 1.3.0) see sqimap_session_id() |
| 83 | * @param boolean $filter (since 1.4.1 and 1.5.0) see sqimap_fread() |
| 84 | * @param mixed $outputstream (since 1.4.1 and 1.5.0) see sqimap_fread() |
| 85 | * @param boolean $no_return (since 1.4.1 and 1.5.0) see sqimap_fread() |
| 86 | * @return mixed returns false on imap error. displays error message |
| 87 | * if imap stream is not available. |
| 88 | * @since 1.2.3 |
| 89 | */ |
| 90 | function sqimap_run_command ($imap_stream, $query, $handle_errors, &$response, |
| 91 | &$message, $unique_id = false,$filter=false, |
| 92 | $outputstream=false,$no_return=false) { |
| 93 | if ($imap_stream) { |
| 94 | $sid = sqimap_session_id($unique_id); |
| 95 | fputs ($imap_stream, $sid . ' ' . $query . "\r\n"); |
| 96 | $tag_uid_a = explode(' ',trim($sid)); |
| 97 | $tag = $tag_uid_a[0]; |
| 98 | |
| 99 | $read = sqimap_read_data ($imap_stream, $tag, $handle_errors, $response, |
| 100 | $message, $query,$filter,$outputstream,$no_return); |
| 101 | if (empty($read)) { //IMAP server dropped its connection |
| 102 | $response = ''; |
| 103 | $message = ''; |
| 104 | return false; |
| 105 | } |
| 106 | /* retrieve the response and the message */ |
| 107 | $response = $response[$tag]; |
| 108 | $message = $message[$tag]; |
| 109 | |
| 110 | if (!empty($read[$tag])) { |
| 111 | return $read[$tag][0]; |
| 112 | } else { |
| 113 | return $read[$tag]; |
| 114 | } |
| 115 | } else { |
| 116 | global $squirrelmail_language, $color; |
| 117 | set_up_language($squirrelmail_language); |
| 118 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 119 | $string = "<b><font color=\"$color[2]\">\n" . |
| 120 | _("ERROR: No available IMAP stream.") . |
| 121 | "</b></font>\n"; |
| 122 | error_box($string,$color); |
| 123 | return false; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param mixed $new_query |
| 129 | * @param string $tag |
| 130 | * @param array $aQuery |
| 131 | * @param boolean $unique_id see sqimap_session_id() |
| 132 | * @since 1.5.0 |
| 133 | */ |
| 134 | function sqimap_prepare_pipelined_query($new_query,&$tag,&$aQuery,$unique_id) { |
| 135 | $sid = sqimap_session_id($unique_id); |
| 136 | $tag_uid_a = explode(' ',trim($sid)); |
| 137 | $tag = $tag_uid_a[0]; |
| 138 | $query = $sid . ' '.$new_query."\r\n"; |
| 139 | $aQuery[$tag] = $query; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param stream $imap_stream imap stream |
| 144 | * @param array $aQueryList |
| 145 | * @param boolean $handle_errors |
| 146 | * @param array $aServerResponse |
| 147 | * @param array $aServerMessage |
| 148 | * @param boolean $unique_id see sqimap_session_id() |
| 149 | * @param boolean $filter see sqimap_fread() |
| 150 | * @param mixed $outputstream see sqimap_fread() |
| 151 | * @param boolean $no_return see sqimap_fread() |
| 152 | * @since 1.5.0 |
| 153 | */ |
| 154 | function sqimap_run_pipelined_command ($imap_stream, $aQueryList, $handle_errors, |
| 155 | &$aServerResponse, &$aServerMessage, $unique_id = false, |
| 156 | $filter=false,$outputstream=false,$no_return=false) { |
| 157 | $aResponse = false; |
| 158 | |
| 159 | /* |
| 160 | Do not fire all calls at once to the IMAP server but split the calls up |
| 161 | in portions of $iChunkSize. If we do not do that I think we misbehave as |
| 162 | IMAP client or should handle BYE calls if the IMAP server drops the |
| 163 | connection because the number of queries is to large. This isn't tested |
| 164 | but a wild guess how it could work in the field. |
| 165 | |
| 166 | After testing it on Exchange 2000 we discovered that a chunksize of 32 |
| 167 | was quicker then when we raised it to 128. |
| 168 | */ |
| 169 | $iQueryCount = count($aQueryList); |
| 170 | $iChunkSize = 32; |
| 171 | // array_chunk would also do the job but it's supported from php > 4.2 |
| 172 | $aQueryChunks = array(); |
| 173 | $iLoops = floor($iQueryCount / $iChunkSize); |
| 174 | |
| 175 | if ($iLoops * $iChunkSize != $iQueryCount) ++$iLoops; |
| 176 | |
| 177 | if (!function_exists('array_chunk')) { // arraychunk replacement |
| 178 | reset($aQueryList); |
| 179 | for($i=0;$i<$iLoops;++$i) { |
| 180 | for($j=0;$j<$iChunkSize;++$j) { |
| 181 | $key = key($aQueryList); |
| 182 | $aTmp[$key] = $aQueryList[$key]; |
| 183 | if (next($aQueryList) === false) break; |
| 184 | } |
| 185 | $aQueryChunks[] = $aTmp; |
| 186 | } |
| 187 | } else { |
| 188 | $aQueryChunks = array_chunk($aQueryList,$iChunkSize,true); |
| 189 | } |
| 190 | |
| 191 | for ($i=0;$i<$iLoops;++$i) { |
| 192 | $aQuery = $aQueryChunks[$i]; |
| 193 | foreach($aQuery as $tag => $query) { |
| 194 | fputs($imap_stream,$query); |
| 195 | $aResults[$tag] = false; |
| 196 | } |
| 197 | foreach($aQuery as $tag => $query) { |
| 198 | if ($aResults[$tag] == false) { |
| 199 | $aReturnedResponse = sqimap_retrieve_imap_response ($imap_stream, $tag, |
| 200 | $handle_errors, $response, $message, $query, |
| 201 | $filter,$outputstream,$no_return); |
| 202 | foreach ($aReturnedResponse as $returned_tag => $aResponse) { |
| 203 | if (!empty($aResponse)) { |
| 204 | $aResults[$returned_tag] = $aResponse[0]; |
| 205 | } else { |
| 206 | $aResults[$returned_tag] = $aResponse; |
| 207 | } |
| 208 | $aServerResponse[$returned_tag] = $response[$returned_tag]; |
| 209 | $aServerMessage[$returned_tag] = $message[$returned_tag]; |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | return $aResults; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Custom fgets function: gets a line from the IMAP server, |
| 219 | * no matter how big it may be. |
| 220 | * @param stream $imap_stream the stream to read from |
| 221 | * @return string a line |
| 222 | * @since 1.2.8 |
| 223 | */ |
| 224 | function sqimap_fgets($imap_stream) { |
| 225 | $read = ''; |
| 226 | $buffer = 4096; |
| 227 | $results = ''; |
| 228 | $offset = 0; |
| 229 | while (strpos($results, "\r\n", $offset) === false) { |
| 230 | if (!($read = fgets($imap_stream, $buffer))) { |
| 231 | /* this happens in case of an error */ |
| 232 | /* reset $results because it's useless */ |
| 233 | $results = false; |
| 234 | break; |
| 235 | } |
| 236 | if ( $results != '' ) { |
| 237 | $offset = strlen($results) - 1; |
| 238 | } |
| 239 | $results .= $read; |
| 240 | } |
| 241 | return $results; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * @param stream $imap_stream |
| 246 | * @param integer $iSize |
| 247 | * @param boolean $filter |
| 248 | * @param mixed $outputstream stream or 'php://stdout' string |
| 249 | * @param boolean $no_return controls data returned by function |
| 250 | * @return string |
| 251 | * @since 1.4.1 |
| 252 | */ |
| 253 | function sqimap_fread($imap_stream,$iSize,$filter=false, |
| 254 | $outputstream=false, $no_return=false) { |
| 255 | if (!$filter || !$outputstream) { |
| 256 | $iBufferSize = $iSize; |
| 257 | } else { |
| 258 | // see php bug 24033. They changed fread behaviour %$^&$% |
| 259 | $iBufferSize = 7800; // multiple of 78 in case of base64 decoding. |
| 260 | } |
| 261 | if ($iSize < $iBufferSize) { |
| 262 | $iBufferSize = $iSize; |
| 263 | } |
| 264 | |
| 265 | $iRetrieved = 0; |
| 266 | $results = ''; |
| 267 | $sRead = $sReadRem = ''; |
| 268 | // NB: fread can also stop at end of a packet on sockets. |
| 269 | while ($iRetrieved < $iSize) { |
| 270 | $sRead = fread($imap_stream,$iBufferSize); |
| 271 | $iLength = strlen($sRead); |
| 272 | $iRetrieved += $iLength ; |
| 273 | $iRemaining = $iSize - $iRetrieved; |
| 274 | if ($iRemaining < $iBufferSize) { |
| 275 | $iBufferSize = $iRemaining; |
| 276 | } |
| 277 | if ($sRead == '') { |
| 278 | $results = false; |
| 279 | break; |
| 280 | } |
| 281 | if ($sReadRem != '') { |
| 282 | $sRead = $sReadRem . $sRead; |
| 283 | $sReadRem = ''; |
| 284 | } |
| 285 | |
| 286 | if ($filter && $sRead != '') { |
| 287 | // in case the filter is base64 decoding we return a remainder |
| 288 | $sReadRem = $filter($sRead); |
| 289 | } |
| 290 | if ($outputstream && $sRead != '') { |
| 291 | if (is_resource($outputstream)) { |
| 292 | fwrite($outputstream,$sRead); |
| 293 | } else if ($outputstream == 'php://stdout') { |
| 294 | echo $sRead; |
| 295 | } |
| 296 | } |
| 297 | if ($no_return) { |
| 298 | $sRead = ''; |
| 299 | } else { |
| 300 | $results .= $sRead; |
| 301 | } |
| 302 | } |
| 303 | return $results; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /** |
| 308 | * Obsolete function, inform plugins that use it |
| 309 | * @param stream $imap_stream |
| 310 | * @param string $tag |
| 311 | * @param boolean $handle_errors |
| 312 | * @param array $response |
| 313 | * @param array $message |
| 314 | * @param string $query |
| 315 | * @since 1.1.3 |
| 316 | * @deprecated (since 1.5.0) use sqimap_run_command or sqimap_run_command_list instead |
| 317 | */ |
| 318 | function sqimap_read_data_list($imap_stream, $tag, $handle_errors, |
| 319 | &$response, &$message, $query = '') { |
| 320 | global $color, $squirrelmail_language; |
| 321 | set_up_language($squirrelmail_language); |
| 322 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 323 | $string = "<b><font color=\"$color[2]\">\n" . |
| 324 | _("ERROR: Bad function call.") . |
| 325 | "</b><br />\n" . |
| 326 | _("Reason:") . ' '. |
| 327 | 'There is a plugin installed which make use of the <br />' . |
| 328 | 'SquirrelMail internal function sqimap_read_data_list.<br />'. |
| 329 | 'Please adapt the installed plugin and let it use<br />'. |
| 330 | 'sqimap_run_command or sqimap_run_command_list instead<br /><br />'. |
| 331 | 'The following query was issued:<br />'. |
| 332 | htmlspecialchars($query) . '<br />' . "</font><br />\n"; |
| 333 | error_box($string,$color); |
| 334 | echo '</body></html>'; |
| 335 | exit; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Function to display an error related to an IMAP query. |
| 340 | * @param string title the caption of the error box |
| 341 | * @param string query the query that went wrong |
| 342 | * @param string message_title optional message title |
| 343 | * @param string message optional error message |
| 344 | * @param string $link an optional link to try again |
| 345 | * @return void |
| 346 | * @since 1.5.0 |
| 347 | */ |
| 348 | function sqimap_error_box($title, $query = '', $message_title = '', $message = '', $link = '') |
| 349 | { |
| 350 | global $color, $squirrelmail_language; |
| 351 | |
| 352 | set_up_language($squirrelmail_language); |
| 353 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 354 | $string = "<font color=\"$color[2]\"><b>\n" . $title . "</b><br />\n"; |
| 355 | $cmd = explode(' ',$query); |
| 356 | $cmd= strtolower($cmd[0]); |
| 357 | |
| 358 | if ($query != '' && $cmd != 'login') |
| 359 | $string .= _("Query:") . ' ' . htmlspecialchars($query) . '<br />'; |
| 360 | if ($message_title != '') |
| 361 | $string .= $message_title; |
| 362 | if ($message != '') |
| 363 | $string .= htmlspecialchars($message); |
| 364 | $string .= "</font><br />\n"; |
| 365 | if ($link != '') |
| 366 | $string .= $link; |
| 367 | error_box($string,$color); |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * Reads the output from the IMAP stream. If handle_errors is set to true, |
| 372 | * this will also handle all errors that are received. If it is not set, |
| 373 | * the errors will be sent back through $response and $message. |
| 374 | * @param stream $imap_stream imap stream |
| 375 | * @param string $tag |
| 376 | * @param boolean $handle_errors handle errors internally or send them in $response and $message. |
| 377 | * @param array $response |
| 378 | * @param array $message |
| 379 | * @param string $query command that can be printed if something fails |
| 380 | * @param boolean $filter see sqimap_fread() |
| 381 | * @param mixed $outputstream see sqimap_fread() |
| 382 | * @param boolean $no_return see sqimap_fread() |
| 383 | * @since 1.5.0 |
| 384 | */ |
| 385 | function sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors, |
| 386 | &$response, &$message, $query = '', |
| 387 | $filter = false, $outputstream = false, $no_return = false) { |
| 388 | global $color, $squirrelmail_language; |
| 389 | $read = ''; |
| 390 | if (!is_array($message)) $message = array(); |
| 391 | if (!is_array($response)) $response = array(); |
| 392 | $aResponse = ''; |
| 393 | $resultlist = array(); |
| 394 | $data = array(); |
| 395 | $read = sqimap_fgets($imap_stream); |
| 396 | $i = 0; |
| 397 | while ($read) { |
| 398 | $char = $read{0}; |
| 399 | switch ($char) |
| 400 | { |
| 401 | case '+': |
| 402 | default: |
| 403 | $read = sqimap_fgets($imap_stream); |
| 404 | break; |
| 405 | |
| 406 | case $tag{0}: |
| 407 | { |
| 408 | /* get the command */ |
| 409 | $arg = ''; |
| 410 | $i = strlen($tag)+1; |
| 411 | $s = substr($read,$i); |
| 412 | if (($j = strpos($s,' ')) || ($j = strpos($s,"\n"))) { |
| 413 | $arg = substr($s,0,$j); |
| 414 | } |
| 415 | $found_tag = substr($read,0,$i-1); |
| 416 | if ($found_tag) { |
| 417 | switch ($arg) |
| 418 | { |
| 419 | case 'OK': |
| 420 | case 'BAD': |
| 421 | case 'NO': |
| 422 | case 'BYE': |
| 423 | case 'PREAUTH': |
| 424 | $response[$found_tag] = $arg; |
| 425 | $message[$found_tag] = trim(substr($read,$i+strlen($arg))); |
| 426 | if (!empty($data)) { |
| 427 | $resultlist[] = $data; |
| 428 | } |
| 429 | $aResponse[$found_tag] = $resultlist; |
| 430 | $data = $resultlist = array(); |
| 431 | if ($found_tag == $tag) { |
| 432 | break 3; /* switch switch while */ |
| 433 | } |
| 434 | break; |
| 435 | default: |
| 436 | /* this shouldn't happen */ |
| 437 | $response[$found_tag] = $arg; |
| 438 | $message[$found_tag] = trim(substr($read,$i+strlen($arg))); |
| 439 | if (!empty($data)) { |
| 440 | $resultlist[] = $data; |
| 441 | } |
| 442 | $aResponse[$found_tag] = $resultlist; |
| 443 | $data = $resultlist = array(); |
| 444 | if ($found_tag == $tag) { |
| 445 | break 3; /* switch switch while */ |
| 446 | } |
| 447 | } |
| 448 | } |
| 449 | $read = sqimap_fgets($imap_stream); |
| 450 | if ($read === false) { /* error */ |
| 451 | break 2; /* switch while */ |
| 452 | } |
| 453 | break; |
| 454 | } // end case $tag{0} |
| 455 | |
| 456 | case '*': |
| 457 | { |
| 458 | if (preg_match('/^\*\s\d+\sFETCH/',$read)) { |
| 459 | /* check for literal */ |
| 460 | $s = substr($read,-3); |
| 461 | $fetch_data = array(); |
| 462 | do { /* outer loop, continue until next untagged fetch |
| 463 | or tagged reponse */ |
| 464 | do { /* innerloop for fetching literals. with this loop |
| 465 | we prohibid that literal responses appear in the |
| 466 | outer loop so we can trust the untagged and |
| 467 | tagged info provided by $read */ |
| 468 | if ($s === "}\r\n") { |
| 469 | $j = strrpos($read,'{'); |
| 470 | $iLit = substr($read,$j+1,-3); |
| 471 | $fetch_data[] = $read; |
| 472 | $sLiteral = sqimap_fread($imap_stream,$iLit,$filter,$outputstream,$no_return); |
| 473 | if ($sLiteral === false) { /* error */ |
| 474 | break 4; /* while while switch while */ |
| 475 | } |
| 476 | /* backwards compattibility */ |
| 477 | $aLiteral = explode("\n", $sLiteral); |
| 478 | /* release not neaded data */ |
| 479 | unset($sLiteral); |
| 480 | foreach ($aLiteral as $line) { |
| 481 | $fetch_data[] = $line ."\n"; |
| 482 | } |
| 483 | /* release not neaded data */ |
| 484 | unset($aLiteral); |
| 485 | /* next fgets belongs to this fetch because |
| 486 | we just got the exact literalsize and there |
| 487 | must follow data to complete the response */ |
| 488 | $read = sqimap_fgets($imap_stream); |
| 489 | if ($read === false) { /* error */ |
| 490 | break 4; /* while while switch while */ |
| 491 | } |
| 492 | $fetch_data[] = $read; |
| 493 | } else { |
| 494 | $fetch_data[] = $read; |
| 495 | } |
| 496 | /* retrieve next line and check in the while |
| 497 | statements if it belongs to this fetch response */ |
| 498 | $read = sqimap_fgets($imap_stream); |
| 499 | if ($read === false) { /* error */ |
| 500 | break 4; /* while while switch while */ |
| 501 | } |
| 502 | /* check for next untagged reponse and break */ |
| 503 | if ($read{0} == '*') break 2; |
| 504 | $s = substr($read,-3); |
| 505 | } while ($s === "}\r\n"); |
| 506 | $s = substr($read,-3); |
| 507 | } while ($read{0} !== '*' && |
| 508 | substr($read,0,strlen($tag)) !== $tag); |
| 509 | $resultlist[] = $fetch_data; |
| 510 | /* release not neaded data */ |
| 511 | unset ($fetch_data); |
| 512 | } else { |
| 513 | $s = substr($read,-3); |
| 514 | do { |
| 515 | if ($s === "}\r\n") { |
| 516 | $j = strrpos($read,'{'); |
| 517 | $iLit = substr($read,$j+1,-3); |
| 518 | $data[] = $read; |
| 519 | $sLiteral = fread($imap_stream,$iLit); |
| 520 | if ($sLiteral === false) { /* error */ |
| 521 | $read = false; |
| 522 | break 3; /* while switch while */ |
| 523 | } |
| 524 | $data[] = $sLiteral; |
| 525 | $data[] = sqimap_fgets($imap_stream); |
| 526 | } else { |
| 527 | $data[] = $read; |
| 528 | } |
| 529 | $read = sqimap_fgets($imap_stream); |
| 530 | if ($read === false) { |
| 531 | break 3; /* while switch while */ |
| 532 | } else if ($read{0} == '*') { |
| 533 | break; |
| 534 | } |
| 535 | $s = substr($read,-3); |
| 536 | } while ($s === "}\r\n"); |
| 537 | break 1; |
| 538 | } |
| 539 | break; |
| 540 | } // end case '*' |
| 541 | } // end switch |
| 542 | } // end while |
| 543 | |
| 544 | /* error processing in case $read is false */ |
| 545 | if ($read === false) { |
| 546 | // try to retrieve an untagged bye respons from the results |
| 547 | $sResponse = array_pop($data); |
| 548 | if ($sResponse !== NULL && strpos($sResponse,'* BYE') !== false) { |
| 549 | if (!$handle_errors) { |
| 550 | $query = ''; |
| 551 | } |
| 552 | sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:"),$sResponse); |
| 553 | echo '</body></html>'; |
| 554 | exit; |
| 555 | } else if ($handle_errors) { |
| 556 | unset($data); |
| 557 | sqimap_error_box(_("ERROR: Connection dropped by IMAP server."), $query); |
| 558 | exit; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | /* Set $resultlist array */ |
| 563 | if (!empty($data)) { |
| 564 | //$resultlist[] = $data; |
| 565 | } |
| 566 | elseif (empty($resultlist)) { |
| 567 | $resultlist[] = array(); |
| 568 | } |
| 569 | |
| 570 | /* Return result or handle errors */ |
| 571 | if ($handle_errors == false) { |
| 572 | return $aResponse; |
| 573 | } |
| 574 | switch ($response[$tag]) { |
| 575 | case 'OK': |
| 576 | return $aResponse; |
| 577 | break; |
| 578 | case 'NO': |
| 579 | /* ignore this error from M$ exchange, it is not fatal (aka bug) */ |
| 580 | if (strstr($message[$tag], 'command resulted in') === false) { |
| 581 | sqimap_error_box(_("ERROR: Could not complete request."), $query, _("Reason Given:") . ' ', $message[$tag]); |
| 582 | echo '</body></html>'; |
| 583 | exit; |
| 584 | } |
| 585 | break; |
| 586 | case 'BAD': |
| 587 | sqimap_error_box(_("ERROR: Bad or malformed request."), $query, _("Server responded:") . ' ', $message[$tag]); |
| 588 | echo '</body></html>'; |
| 589 | exit; |
| 590 | case 'BYE': |
| 591 | sqimap_error_box(_("ERROR: IMAP server closed the connection."), $query, _("Server responded:") . ' ', $message[$tag]); |
| 592 | echo '</body></html>'; |
| 593 | exit; |
| 594 | default: |
| 595 | sqimap_error_box(_("ERROR: Unknown IMAP response."), $query, _("Server responded:") . ' ', $message[$tag]); |
| 596 | /* the error is displayed but because we don't know the reponse we |
| 597 | return the result anyway */ |
| 598 | return $aResponse; |
| 599 | break; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * @param stream $imap_stream imap string |
| 605 | * @param string $tag_uid |
| 606 | * @param boolean $handle_errors |
| 607 | * @param array $response |
| 608 | * @param array $message |
| 609 | * @param string $query (since 1.2.5) |
| 610 | * @param boolean $filter (since 1.4.1) see sqimap_fread() |
| 611 | * @param mixed $outputstream (since 1.4.1) see sqimap_fread() |
| 612 | * @param boolean $no_return (since 1.4.1) see sqimap_fread() |
| 613 | */ |
| 614 | function sqimap_read_data ($imap_stream, $tag_uid, $handle_errors, |
| 615 | &$response, &$message, $query = '', |
| 616 | $filter=false,$outputstream=false,$no_return=false) { |
| 617 | |
| 618 | $tag_uid_a = explode(' ',trim($tag_uid)); |
| 619 | $tag = $tag_uid_a[0]; |
| 620 | |
| 621 | $res = sqimap_retrieve_imap_response($imap_stream, $tag, $handle_errors, |
| 622 | $response, $message, $query,$filter,$outputstream,$no_return); |
| 623 | return $res; |
| 624 | } |
| 625 | |
| 626 | /** |
| 627 | * Connects to the IMAP server and returns a resource identifier for use with |
| 628 | * the other SquirrelMail IMAP functions. Does NOT login! |
| 629 | * @param string server hostname of IMAP server |
| 630 | * @param int port port number to connect to |
| 631 | * @param integer $tls whether to use plain text(0), TLS(1) or STARTTLS(2) when connecting. |
| 632 | * Argument was boolean before 1.5.1. |
| 633 | * @return imap-stream resource identifier |
| 634 | * @since 1.5.0 (usable only in 1.5.1 or later) |
| 635 | */ |
| 636 | function sqimap_create_stream($server,$port,$tls=0) { |
| 637 | global $squirrelmail_language; |
| 638 | |
| 639 | if (strstr($server,':') && ! preg_match("/^\[.*\]$/",$server)) { |
| 640 | // numerical IPv6 address must be enclosed in square brackets |
| 641 | $server = '['.$server.']'; |
| 642 | } |
| 643 | |
| 644 | if ($tls == 1) { |
| 645 | if ((check_php_version(4,3)) and (extension_loaded('openssl'))) { |
| 646 | /* Use TLS by prefixing "tls://" to the hostname */ |
| 647 | $server = 'tls://' . $server; |
| 648 | } else { |
| 649 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 650 | logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server). |
| 651 | '<br />'. |
| 652 | _("TLS is enabled, but this version of PHP does not support TLS sockets, or is missing the openssl extension."). |
| 653 | '<br /><br />'. |
| 654 | _("Please contact your system administrator and report this error."), |
| 655 | sprintf(_("Error connecting to IMAP server: %s."), $server)); |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | $imap_stream = @fsockopen($server, $port, $error_number, $error_string, 15); |
| 660 | |
| 661 | /* Do some error correction */ |
| 662 | if (!$imap_stream) { |
| 663 | set_up_language($squirrelmail_language, true); |
| 664 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 665 | logout_error( sprintf(_("Error connecting to IMAP server: %s."), $server). |
| 666 | "<br />\r\n$error_number : $error_string<br />\r\n", |
| 667 | sprintf(_("Error connecting to IMAP server: %s."), $server) ); |
| 668 | exit; |
| 669 | } |
| 670 | $server_info = fgets ($imap_stream, 1024); |
| 671 | |
| 672 | /** |
| 673 | * Implementing IMAP STARTTLS (rfc2595) in php 5.1.0+ |
| 674 | * http://www.php.net/stream-socket-enable-crypto |
| 675 | */ |
| 676 | if ($tls == 2) { |
| 677 | if (function_exists('stream_socket_enable_crypto')) { |
| 678 | // check starttls capability, don't use cached capability version |
| 679 | if (! sqimap_capability($imap_stream, 'STARTTLS', false)) { |
| 680 | // imap server does not declare starttls support |
| 681 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
| 682 | '','', |
| 683 | _("IMAP STARTTLS is enabled in SquirrelMail configuration, but used IMAP server does not support STARTTLS.")); |
| 684 | exit; |
| 685 | } |
| 686 | |
| 687 | // issue starttls command and check response |
| 688 | sqimap_run_command($imap_stream, 'STARTTLS', false, $starttls_response, $starttls_message); |
| 689 | // check response |
| 690 | if ($starttls_response!='OK') { |
| 691 | // starttls command failed |
| 692 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
| 693 | 'STARTTLS', |
| 694 | _("Server replied:") . ' ', |
| 695 | $starttls_message); |
| 696 | exit(); |
| 697 | } |
| 698 | |
| 699 | // start crypto on connection. suppress function errors. |
| 700 | if (@stream_socket_enable_crypto($imap_stream,true,STREAM_CRYPTO_METHOD_TLS_CLIENT)) { |
| 701 | // starttls was successful |
| 702 | |
| 703 | /** |
| 704 | * RFC 2595 requires to discard CAPABILITY information after successful |
| 705 | * STARTTLS command. We don't follow RFC, because SquirrelMail stores CAPABILITY |
| 706 | * information only after successful login (src/redirect.php) and cached information |
| 707 | * is used only in other php script connections after successful STARTTLS. If script |
| 708 | * issues sqimap_capability() call before sqimap_login() and wants to get initial |
| 709 | * capability response, script should set third sqimap_capability() argument to false. |
| 710 | */ |
| 711 | //sqsession_unregister('sqimap_capabilities'); |
| 712 | } else { |
| 713 | /** |
| 714 | * stream_socket_enable_crypto() call failed. Possible issues: |
| 715 | * - broken ssl certificate (uw drops connection, error is in syslog mail facility) |
| 716 | * - some ssl error (can reproduce with STREAM_CRYPTO_METHOD_SSLv3_CLIENT, PHP E_WARNING |
| 717 | * suppressed in stream_socket_enable_crypto() call) |
| 718 | */ |
| 719 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
| 720 | '','', |
| 721 | _("Unable to start TLS.")); |
| 722 | /** |
| 723 | * Bug: stream_socket_enable_crypto() does not register SSL errors in |
| 724 | * openssl_error_string() or stream notification wrapper and displays |
| 725 | * them in E_WARNING level message. It is impossible to retrieve error |
| 726 | * message without own error handler. |
| 727 | */ |
| 728 | exit; |
| 729 | } |
| 730 | } else { |
| 731 | // php install does not support stream_socket_enable_crypto() function |
| 732 | sqimap_error_box(sprintf(_("Error connecting to IMAP server: %s."), $server), |
| 733 | '','', |
| 734 | _("IMAP STARTTLS is enabled in SquirrelMail configuration, but used PHP version does not support functions that allow to enable encryption on open socket.")); |
| 735 | exit; |
| 736 | } |
| 737 | } |
| 738 | return $imap_stream; |
| 739 | } |
| 740 | |
| 741 | /** |
| 742 | * Logs the user into the IMAP server. If $hide is set, no error messages |
| 743 | * will be displayed. This function returns the IMAP connection handle. |
| 744 | * @param string $username user name |
| 745 | * @param string $password encrypted password |
| 746 | * @param string $imap_server_address address of imap server |
| 747 | * @param integer $imap_port port of imap server |
| 748 | * @param boolean $hide controls display connection errors |
| 749 | * @return stream |
| 750 | */ |
| 751 | function sqimap_login ($username, $password, $imap_server_address, $imap_port, $hide) { |
| 752 | global $color, $squirrelmail_language, $onetimepad, $use_imap_tls, |
| 753 | $imap_auth_mech, $sqimap_capabilities; |
| 754 | |
| 755 | if (!isset($onetimepad) || empty($onetimepad)) { |
| 756 | sqgetglobalvar('onetimepad' , $onetimepad , SQ_SESSION ); |
| 757 | } |
| 758 | if (!isset($sqimap_capabilities)) { |
| 759 | sqgetglobalvar('sqimap_capabilities' , $capability , SQ_SESSION ); |
| 760 | } |
| 761 | |
| 762 | $host = $imap_server_address; |
| 763 | $imap_server_address = sqimap_get_user_server($imap_server_address, $username); |
| 764 | |
| 765 | $imap_stream = sqimap_create_stream($imap_server_address,$imap_port,$use_imap_tls); |
| 766 | |
| 767 | /* Decrypt the password */ |
| 768 | $password = OneTimePadDecrypt($password, $onetimepad); |
| 769 | |
| 770 | if (($imap_auth_mech == 'cram-md5') OR ($imap_auth_mech == 'digest-md5')) { |
| 771 | // We're using some sort of authentication OTHER than plain or login |
| 772 | $tag=sqimap_session_id(false); |
| 773 | if ($imap_auth_mech == 'digest-md5') { |
| 774 | $query = $tag . " AUTHENTICATE DIGEST-MD5\r\n"; |
| 775 | } elseif ($imap_auth_mech == 'cram-md5') { |
| 776 | $query = $tag . " AUTHENTICATE CRAM-MD5\r\n"; |
| 777 | } |
| 778 | fputs($imap_stream,$query); |
| 779 | $answer=sqimap_fgets($imap_stream); |
| 780 | // Trim the "+ " off the front |
| 781 | $response=explode(" ",$answer,3); |
| 782 | if ($response[0] == '+') { |
| 783 | // Got a challenge back |
| 784 | $challenge=$response[1]; |
| 785 | if ($imap_auth_mech == 'digest-md5') { |
| 786 | $reply = digest_md5_response($username,$password,$challenge,'imap',$host); |
| 787 | } elseif ($imap_auth_mech == 'cram-md5') { |
| 788 | $reply = cram_md5_response($username,$password,$challenge); |
| 789 | } |
| 790 | fputs($imap_stream,$reply); |
| 791 | $read=sqimap_fgets($imap_stream); |
| 792 | if ($imap_auth_mech == 'digest-md5') { |
| 793 | // DIGEST-MD5 has an extra step.. |
| 794 | if (substr($read,0,1) == '+') { // OK so far.. |
| 795 | fputs($imap_stream,"\r\n"); |
| 796 | $read=sqimap_fgets($imap_stream); |
| 797 | } |
| 798 | } |
| 799 | $results=explode(" ",$read,3); |
| 800 | $response=$results[1]; |
| 801 | $message=$results[2]; |
| 802 | } else { |
| 803 | // Fake the response, so the error trap at the bottom will work |
| 804 | $response="BAD"; |
| 805 | $message='IMAP server does not appear to support the authentication method selected.'; |
| 806 | $message .= ' Please contact your system administrator.'; |
| 807 | } |
| 808 | } elseif ($imap_auth_mech == 'login') { |
| 809 | // Original IMAP login code |
| 810 | $query = 'LOGIN "' . quoteimap($username) . '" "' . quoteimap($password) . '"'; |
| 811 | $read = sqimap_run_command ($imap_stream, $query, false, $response, $message); |
| 812 | } elseif ($imap_auth_mech == 'plain') { |
| 813 | /*** |
| 814 | * SASL PLAIN |
| 815 | * |
| 816 | * RFC 2595 Chapter 6 |
| 817 | * |
| 818 | * The mechanism consists of a single message from the client to the |
| 819 | * server. The client sends the authorization identity (identity to |
| 820 | * login as), followed by a US-ASCII NUL character, followed by the |
| 821 | * authentication identity (identity whose password will be used), |
| 822 | * followed by a US-ASCII NUL character, followed by the clear-text |
| 823 | * password. The client may leave the authorization identity empty to |
| 824 | * indicate that it is the same as the authentication identity. |
| 825 | * |
| 826 | **/ |
| 827 | $tag=sqimap_session_id(false); |
| 828 | $sasl = (isset($capability['SASL-IR']) && $capability['SASL-IR']) ? true : false; |
| 829 | $auth = base64_encode("$username\0$username\0$password"); |
| 830 | if ($sasl) { |
| 831 | // IMAP Extension for SASL Initial Client Response |
| 832 | // <draft-siemborski-imap-sasl-initial-response-01b.txt> |
| 833 | $query = $tag . " AUTHENTICATE PLAIN $auth\r\n"; |
| 834 | fputs($imap_stream, $query); |
| 835 | $read = sqimap_fgets($imap_stream); |
| 836 | } else { |
| 837 | $query = $tag . " AUTHENTICATE PLAIN\r\n"; |
| 838 | fputs($imap_stream, $query); |
| 839 | $read=sqimap_fgets($imap_stream); |
| 840 | if (substr($read,0,1) == '+') { // OK so far.. |
| 841 | fputs($imap_stream, "$auth\r\n"); |
| 842 | $read = sqimap_fgets($imap_stream); |
| 843 | } |
| 844 | } |
| 845 | $results=explode(" ",$read,3); |
| 846 | $response=$results[1]; |
| 847 | $message=$results[2]; |
| 848 | } else { |
| 849 | $response="BAD"; |
| 850 | $message="Internal SquirrelMail error - unknown IMAP authentication method chosen. Please contact the developers."; |
| 851 | } |
| 852 | |
| 853 | /* If the connection was not successful, lets see why */ |
| 854 | if ($response != 'OK') { |
| 855 | if (!$hide) { |
| 856 | if ($response != 'NO') { |
| 857 | /* "BAD" and anything else gets reported here. */ |
| 858 | $message = htmlspecialchars($message); |
| 859 | set_up_language($squirrelmail_language, true); |
| 860 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 861 | if ($response == 'BAD') { |
| 862 | $string = sprintf (_("Bad request: %s")."<br />\r\n", $message); |
| 863 | } else { |
| 864 | $string = sprintf (_("Unknown error: %s") . "<br />\n", $message); |
| 865 | } |
| 866 | if (isset($read) && is_array($read)) { |
| 867 | $string .= '<br />' . _("Read data:") . "<br />\n"; |
| 868 | foreach ($read as $line) { |
| 869 | $string .= htmlspecialchars($line) . "<br />\n"; |
| 870 | } |
| 871 | } |
| 872 | error_box($string,$color); |
| 873 | exit; |
| 874 | } else { |
| 875 | /* |
| 876 | * If the user does not log in with the correct |
| 877 | * username and password it is not possible to get the |
| 878 | * correct locale from the user's preferences. |
| 879 | * Therefore, apply the same hack as on the login |
| 880 | * screen. |
| 881 | * |
| 882 | * $squirrelmail_language is set by a cookie when |
| 883 | * the user selects language and logs out |
| 884 | */ |
| 885 | |
| 886 | set_up_language($squirrelmail_language, true); |
| 887 | include_once(SM_PATH . 'functions/display_messages.php' ); |
| 888 | sqsession_destroy(); |
| 889 | /* terminate the session nicely */ |
| 890 | sqimap_logout($imap_stream); |
| 891 | logout_error( _("Unknown user or password incorrect.") ); |
| 892 | exit; |
| 893 | } |
| 894 | } else { |
| 895 | exit; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | /* Special error case: |
| 900 | * Login referrals. The server returns: |
| 901 | * ? OK [REFERRAL <imap url>] |
| 902 | * Check RFC 2221 for details. Since we do not support login referrals yet |
| 903 | * we log the user out. |
| 904 | */ |
| 905 | if ( stristr($message, 'REFERRAL imap') === TRUE ) { |
| 906 | sqimap_logout($imap_stream); |
| 907 | set_up_language($squirrelmail_language, true); |
| 908 | include_once(SM_PATH . 'functions/display_messages.php' ); |
| 909 | sqsession_destroy(); |
| 910 | logout_error( _("Your mailbox is not located at this server. Try a different server or consult your system administrator") ); |
| 911 | exit; |
| 912 | } |
| 913 | |
| 914 | return $imap_stream; |
| 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Simply logs out the IMAP session |
| 919 | * @param stream $imap_stream the IMAP connection to log out. |
| 920 | * @return void |
| 921 | */ |
| 922 | function sqimap_logout ($imap_stream) { |
| 923 | /* Logout is not valid until the server returns 'BYE' |
| 924 | * If we don't have an imap_ stream we're already logged out */ |
| 925 | if(isset($imap_stream) && $imap_stream) |
| 926 | sqimap_run_command($imap_stream, 'LOGOUT', false, $response, $message); |
| 927 | } |
| 928 | |
| 929 | /** |
| 930 | * Retrieve the CAPABILITY string from the IMAP server. |
| 931 | * If capability is set, returns only that specific capability, |
| 932 | * else returns array of all capabilities. |
| 933 | * @param stream $imap_stream |
| 934 | * @param string $capability (since 1.3.0) |
| 935 | * @param boolean $bUseCache (since 1.5.1) Controls use of capability data stored in session |
| 936 | * @return mixed (string if $capability is set and found, |
| 937 | * false, if $capability is set and not found, |
| 938 | * array if $capability not set) |
| 939 | */ |
| 940 | function sqimap_capability($imap_stream, $capability='', $bUseCache=true) { |
| 941 | // sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION); |
| 942 | |
| 943 | if (!$bUseCache || ! sqgetGlobalVar('sqimap_capabilities', $sqimap_capabilities, SQ_SESSION)) { |
| 944 | $read = sqimap_run_command($imap_stream, 'CAPABILITY', true, $a, $b); |
| 945 | |
| 946 | $c = explode(' ', $read[0]); |
| 947 | for ($i=2; $i < count($c); $i++) { |
| 948 | $cap_list = explode('=', $c[$i]); |
| 949 | if (isset($cap_list[1])) { |
| 950 | $sqimap_capabilities[trim($cap_list[0])][] = $cap_list[1]; |
| 951 | } else { |
| 952 | $sqimap_capabilities[trim($cap_list[0])] = TRUE; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | if ($capability) { |
| 957 | if (isset($sqimap_capabilities[$capability])) { |
| 958 | return $sqimap_capabilities[$capability]; |
| 959 | } else { |
| 960 | return false; |
| 961 | } |
| 962 | } |
| 963 | return $sqimap_capabilities; |
| 964 | } |
| 965 | |
| 966 | /** |
| 967 | * Returns the delimiter between mailboxes: INBOX/Test, or INBOX.Test |
| 968 | * @param stream $imap_stream |
| 969 | * @return string |
| 970 | */ |
| 971 | function sqimap_get_delimiter ($imap_stream = false) { |
| 972 | global $sqimap_delimiter, $optional_delimiter; |
| 973 | |
| 974 | /* Use configured delimiter if set */ |
| 975 | if((!empty($optional_delimiter)) && $optional_delimiter != 'detect') { |
| 976 | return $optional_delimiter; |
| 977 | } |
| 978 | |
| 979 | /* Delimiter is stored in the session from redirect. Try fetching from there first */ |
| 980 | if (empty($sqimap_delimiter)) { |
| 981 | sqgetGlobalVar('delimiter',$sqimap_delimiter,SQ_SESSION); |
| 982 | } |
| 983 | |
| 984 | /* Do some caching here */ |
| 985 | if (!$sqimap_delimiter) { |
| 986 | if (sqimap_capability($imap_stream, 'NAMESPACE')) { |
| 987 | /* |
| 988 | * According to something that I can't find, this is supposed to work on all systems |
| 989 | * OS: This won't work in Courier IMAP. |
| 990 | * OS: According to rfc2342 response from NAMESPACE command is: |
| 991 | * OS: * NAMESPACE (PERSONAL NAMESPACES) (OTHER_USERS NAMESPACE) (SHARED NAMESPACES) |
| 992 | * OS: We want to lookup all personal NAMESPACES... |
| 993 | */ |
| 994 | $read = sqimap_run_command($imap_stream, 'NAMESPACE', true, $a, $b); |
| 995 | if (eregi('\\* NAMESPACE +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL) +(\\( *\\(.+\\) *\\)|NIL)', $read[0], $data)) { |
| 996 | if (eregi('^\\( *\\((.*)\\) *\\)', $data[1], $data2)) { |
| 997 | $pn = $data2[1]; |
| 998 | } |
| 999 | $pna = explode(')(', $pn); |
| 1000 | while (list($k, $v) = each($pna)) { |
| 1001 | $lst = explode('"', $v); |
| 1002 | if (isset($lst[3])) { |
| 1003 | $pn[$lst[1]] = $lst[3]; |
| 1004 | } else { |
| 1005 | $pn[$lst[1]] = ''; |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | $sqimap_delimiter = $pn[0]; |
| 1010 | } else { |
| 1011 | fputs ($imap_stream, ". LIST \"INBOX\" \"\"\r\n"); |
| 1012 | $read = sqimap_read_data($imap_stream, '.', true, $a, $b); |
| 1013 | $read = $read['.'][0]; //sqimap_read_data() now returns a tag array of response array |
| 1014 | $quote_position = strpos ($read[0], '"'); |
| 1015 | $sqimap_delimiter = substr ($read[0], $quote_position+1, 1); |
| 1016 | } |
| 1017 | } |
| 1018 | return $sqimap_delimiter; |
| 1019 | } |
| 1020 | |
| 1021 | /** |
| 1022 | * This encodes a mailbox name for use in IMAP commands. |
| 1023 | * @param string $what the mailbox to encode |
| 1024 | * @return string the encoded mailbox string |
| 1025 | * @since 1.5.0 |
| 1026 | */ |
| 1027 | function sqimap_encode_mailbox_name($what) |
| 1028 | { |
| 1029 | if (ereg("[\"\\\r\n]", $what)) |
| 1030 | return '{' . strlen($what) . "}\r\n" . $what; /* 4.3 literal form */ |
| 1031 | return '"' . $what . '"'; /* 4.3 quoted string form */ |
| 1032 | } |
| 1033 | |
| 1034 | /** |
| 1035 | * Gets the number of messages in the current mailbox. |
| 1036 | * |
| 1037 | * OBSOLETE use sqimap_status_messages instead. |
| 1038 | * @param stream $imap_stream imap stream |
| 1039 | * @param string $mailbox |
| 1040 | * @deprecated |
| 1041 | */ |
| 1042 | function sqimap_get_num_messages ($imap_stream, $mailbox) { |
| 1043 | $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('MESSAGES')); |
| 1044 | return $aStatus['MESSAGES']; |
| 1045 | } |
| 1046 | |
| 1047 | /** |
| 1048 | * OBSOLETE FUNCTION should be removed after mailbox_display, |
| 1049 | * printMessage function is adapted |
| 1050 | * $addr_ar = array(), $group = '' and $host='' arguments are used in 1.4.0 |
| 1051 | * @param string $address |
| 1052 | * @param integer $max |
| 1053 | * @since 1.4.0 |
| 1054 | * @deprecated See Rfc822Address.php |
| 1055 | */ |
| 1056 | function parseAddress($address, $max=0) { |
| 1057 | $aAddress = parseRFC822Address($address,array('limit'=> $max)); |
| 1058 | /* |
| 1059 | * Because the expected format of the array element is changed we adapt it now. |
| 1060 | * This also implies that this function is obsolete and should be removed after the |
| 1061 | * rest of the source is adapted. See Rfc822Address.php for the new function. |
| 1062 | */ |
| 1063 | array_walk($aAddress, '_adaptAddress'); |
| 1064 | return $aAddress; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
| 1068 | * OBSOLETE FUNCTION should be removed after mailbox_display, |
| 1069 | * printMessage function is adapted |
| 1070 | * |
| 1071 | * callback function used for formating of addresses array in |
| 1072 | * parseAddress() function |
| 1073 | * @param array $aAddr |
| 1074 | * @param integer $k array key |
| 1075 | * @since 1.5.1 |
| 1076 | * @deprecated |
| 1077 | */ |
| 1078 | function _adaptAddress(&$aAddr,$k) { |
| 1079 | $sPersonal = (isset($aAddr[SQM_ADDR_PERSONAL]) && $aAddr[SQM_ADDR_PERSONAL]) ? |
| 1080 | $aAddr[SQM_ADDR_PERSONAL] : ''; |
| 1081 | $sEmail = ($aAddr[SQM_ADDR_HOST]) ? |
| 1082 | $aAddr[SQM_ADDR_MAILBOX] . '@'.$aAddr[SQM_ADDR_HOST] : |
| 1083 | $aAddr[SQM_ADDR_MAILBOX]; |
| 1084 | $aAddr = array($sEmail,$sPersonal); |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * Returns the number of unseen messages in this folder. |
| 1089 | * obsoleted by sqimap_status_messages ! |
| 1090 | * Arguments differ in 1.0.x |
| 1091 | * @param stream $imap_stream |
| 1092 | * @param string $mailbox |
| 1093 | * @return integer |
| 1094 | * @deprecated |
| 1095 | */ |
| 1096 | function sqimap_unseen_messages ($imap_stream, $mailbox) { |
| 1097 | $aStatus = sqimap_status_messages($imap_stream,$mailbox,array('UNSEEN')); |
| 1098 | return $aStatus['UNSEEN']; |
| 1099 | } |
| 1100 | |
| 1101 | /** |
| 1102 | * Returns the status items of a mailbox. |
| 1103 | * Default it returns MESSAGES,UNSEEN and RECENT |
| 1104 | * Supported status items are MESSAGES, UNSEEN, RECENT (since 1.4.0), |
| 1105 | * UIDNEXT (since 1.5.1) and UIDVALIDITY (since 1.5.1) |
| 1106 | * @param stream $imap_stream imap stream |
| 1107 | * @param string $mailbox mail folder |
| 1108 | * @param array $aStatusItems status items |
| 1109 | * @return array |
| 1110 | * @since 1.3.2 |
| 1111 | */ |
| 1112 | function sqimap_status_messages ($imap_stream, $mailbox, |
| 1113 | $aStatusItems = array('MESSAGES','UNSEEN','RECENT')) { |
| 1114 | |
| 1115 | $aStatusItems = implode(' ',$aStatusItems); |
| 1116 | $read_ary = sqimap_run_command ($imap_stream, 'STATUS ' . sqimap_encode_mailbox_name($mailbox) . |
| 1117 | " ($aStatusItems)", false, $result, $message); |
| 1118 | $i = 0; |
| 1119 | $messages = $unseen = $recent = $uidnext = $uidvalidity = false; |
| 1120 | $regs = array(false,false); |
| 1121 | while (isset($read_ary[$i])) { |
| 1122 | if (preg_match('/UNSEEN\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 1123 | $unseen = $regs[1]; |
| 1124 | } |
| 1125 | if (preg_match('/MESSAGES\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 1126 | $messages = $regs[1]; |
| 1127 | } |
| 1128 | if (preg_match('/RECENT\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 1129 | $recent = $regs[1]; |
| 1130 | } |
| 1131 | if (preg_match('/UIDNEXT\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 1132 | $uidnext = $regs[1]; |
| 1133 | } |
| 1134 | if (preg_match('/UIDVALIDITY\s+([0-9]+)/i', $read_ary[$i], $regs)) { |
| 1135 | $uidvalidity = $regs[1]; |
| 1136 | } |
| 1137 | $i++; |
| 1138 | } |
| 1139 | |
| 1140 | $status=array('MESSAGES' => $messages, |
| 1141 | 'UNSEEN'=>$unseen, |
| 1142 | 'RECENT' => $recent, |
| 1143 | 'UIDNEXT' => $uidnext, |
| 1144 | 'UIDVALIDITY' => $uidvalidity); |
| 1145 | |
| 1146 | if (!empty($messages)) { $hook_status['MESSAGES']=$messages; } |
| 1147 | if (!empty($unseen)) { $hook_status['UNSEEN']=$unseen; } |
| 1148 | if (!empty($recent)) { $hook_status['RECENT']=$recent; } |
| 1149 | if (!empty($hook_status)) { |
| 1150 | $hook_status['MAILBOX']=$mailbox; |
| 1151 | $hook_status['CALLER']='sqimap_status_messages'; |
| 1152 | do_hook_function('folder_status',$hook_status); |
| 1153 | } |
| 1154 | return $status; |
| 1155 | } |
| 1156 | |
| 1157 | |
| 1158 | /** |
| 1159 | * Saves a message to a given folder -- used for saving sent messages |
| 1160 | * @param stream $imap_stream |
| 1161 | * @param string $sent_folder |
| 1162 | * @param $length |
| 1163 | */ |
| 1164 | function sqimap_append ($imap_stream, $sent_folder, $length) { |
| 1165 | fputs ($imap_stream, sqimap_session_id() . ' APPEND ' . sqimap_encode_mailbox_name($sent_folder) . " (\\Seen) {".$length."}\r\n"); |
| 1166 | $tmp = fgets ($imap_stream, 1024); |
| 1167 | sqimap_append_checkresponse($tmp, $sent_folder); |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * @param stream imap_stream |
| 1172 | * @param string $folder (since 1.3.2) |
| 1173 | */ |
| 1174 | function sqimap_append_done ($imap_stream, $folder='') { |
| 1175 | fputs ($imap_stream, "\r\n"); |
| 1176 | $tmp = fgets ($imap_stream, 1024); |
| 1177 | sqimap_append_checkresponse($tmp, $folder); |
| 1178 | } |
| 1179 | |
| 1180 | /** |
| 1181 | * Displays error messages, if there are errors in responses to |
| 1182 | * commands issues by sqimap_append() and sqimap_append_done() functions. |
| 1183 | * @param string $response |
| 1184 | * @param string $folder |
| 1185 | * @since 1.5.1 and 1.4.5 |
| 1186 | */ |
| 1187 | function sqimap_append_checkresponse($response, $folder) { |
| 1188 | // TODO use error handler, see how sort and thread errors are handled and see errors.php |
| 1189 | if (preg_match("/(.*)(BAD|NO)(.*)$/", $response, $regs)) { |
| 1190 | global $squirrelmail_language, $color; |
| 1191 | set_up_language($squirrelmail_language); |
| 1192 | require_once(SM_PATH . 'functions/display_messages.php'); |
| 1193 | |
| 1194 | $reason = $regs[3]; |
| 1195 | if ($regs[2] == 'NO') { |
| 1196 | $string = "<b><font color=\"$color[2]\">\n" . |
| 1197 | _("ERROR: Could not append message to") ." $folder." . |
| 1198 | "</b><br />\n" . |
| 1199 | _("Server responded:") . ' ' . |
| 1200 | $reason . "<br />\n"; |
| 1201 | if (preg_match("/(.*)(quota)(.*)$/i", $reason, $regs)) { |
| 1202 | $string .= _("Solution:") . ' ' . |
| 1203 | _("Remove unneccessary messages from your folder and start with your Trash folder.") |
| 1204 | ."<br />\n"; |
| 1205 | } |
| 1206 | $string .= "</font>\n"; |
| 1207 | error_box($string,$color); |
| 1208 | } else { |
| 1209 | $string = "<b><font color=\"$color[2]\">\n" . |
| 1210 | _("ERROR: Bad or malformed request.") . |
| 1211 | "</b><br />\n" . |
| 1212 | _("Server responded:") . ' ' . |
| 1213 | $reason . "</font><br />\n"; |
| 1214 | error_box($string,$color); |
| 1215 | exit; |
| 1216 | } |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | /** |
| 1221 | * Allows mapping of IMAP server address with custom function |
| 1222 | * see map_yp_alias() |
| 1223 | * @param string $imap_server imap server address or mapping |
| 1224 | * @param string $username |
| 1225 | * @return string |
| 1226 | * @since 1.3.0 |
| 1227 | */ |
| 1228 | function sqimap_get_user_server ($imap_server, $username) { |
| 1229 | if (substr($imap_server, 0, 4) != "map:") { |
| 1230 | return $imap_server; |
| 1231 | } |
| 1232 | $function = substr($imap_server, 4); |
| 1233 | return $function($username); |
| 1234 | } |
| 1235 | |
| 1236 | /** |
| 1237 | * This is an example that gets IMAP servers from yellowpages (NIS). |
| 1238 | * you can simple put map:map_yp_alias in your $imap_server_address |
| 1239 | * in config.php use your own function instead map_yp_alias to map your |
| 1240 | * LDAP whatever way to find the users IMAP server. |
| 1241 | * |
| 1242 | * Requires access to external ypmatch program |
| 1243 | * FIXME: it can be implemented in php yp extension or pecl (since php 5.1.0) |
| 1244 | * @param string $username |
| 1245 | * @return string |
| 1246 | * @since 1.3.0 |
| 1247 | */ |
| 1248 | function map_yp_alias($username) { |
| 1249 | $yp = `ypmatch $username aliases`; |
| 1250 | return chop(substr($yp, strlen($username)+1)); |
| 1251 | } |
| 1252 | |
| 1253 | ?> |