| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_messages.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This implements functions that manipulate messages |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | /* NOTE: quite some functions in this file are not used anymore. */ |
| 15 | |
| 16 | /* Copies specified messages to specified folder */ |
| 17 | /* obsolete */ |
| 18 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
| 19 | global $uid_support; |
| 20 | $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message, $uid_support); |
| 21 | } |
| 22 | |
| 23 | function sqimap_msgs_list_copy ($imap_stream, $id, $mailbox) { |
| 24 | global $uid_support; |
| 25 | $msgs_id = sqimap_message_list_squisher($id); |
| 26 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id \"$mailbox\"", true, $response, $message, $uid_support); |
| 27 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, $uid_support); |
| 28 | } |
| 29 | |
| 30 | |
| 31 | /* Deletes specified messages and moves them to trash if possible */ |
| 32 | /* obsolete */ |
| 33 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) { |
| 34 | global $move_to_trash, $trash_folder, $auto_expunge, $uid_support; |
| 35 | |
| 36 | if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
| 37 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
| 38 | } |
| 39 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true); |
| 40 | } |
| 41 | |
| 42 | function sqimap_msgs_list_delete ($imap_stream, $mailbox, $id) { |
| 43 | global $move_to_trash, $trash_folder, $uid_support; |
| 44 | $msgs_id = sqimap_message_list_squisher($id); |
| 45 | if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
| 46 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id \"$trash_folder\"", true, $response, $message, $uid_support); |
| 47 | } |
| 48 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, $uid_support); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /* Sets the specified messages with specified flag */ |
| 53 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) { |
| 54 | global $uid_support; |
| 55 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support); |
| 56 | } |
| 57 | |
| 58 | /* Remove specified flag from specified messages */ |
| 59 | function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag, $handle_errors) { |
| 60 | global $uid_support; |
| 61 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support); |
| 62 | } |
| 63 | |
| 64 | function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) { |
| 65 | global $uid_support; |
| 66 | $msgs_id = sqimap_message_list_squisher($id); |
| 67 | $set_string = ($set ? '+' : '-'); |
| 68 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, $uid_support); |
| 69 | } |
| 70 | |
| 71 | // obsolete? |
| 72 | function sqimap_get_small_header ($imap_stream, $id, $sent) { |
| 73 | $res = sqimap_get_small_header_list($imap_stream, $id, $sent); |
| 74 | return $res[0]; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Sort the message list and crunch to be as small as possible |
| 79 | * (overflow could happen, so make it small if possible) |
| 80 | */ |
| 81 | function sqimap_message_list_squisher($messages_array) { |
| 82 | if( !is_array( $messages_array ) ) { |
| 83 | return $messages_array; |
| 84 | } |
| 85 | |
| 86 | sort($messages_array, SORT_NUMERIC); |
| 87 | $msgs_str = ''; |
| 88 | while ($messages_array) { |
| 89 | $start = array_shift($messages_array); |
| 90 | $end = $start; |
| 91 | while (isset($messages_array[0]) && $messages_array[0] == $end + 1) { |
| 92 | $end = array_shift($messages_array); |
| 93 | } |
| 94 | if ($msgs_str != '') { |
| 95 | $msgs_str .= ','; |
| 96 | } |
| 97 | $msgs_str .= $start; |
| 98 | if ($start != $end) { |
| 99 | $msgs_str .= ':' . $end; |
| 100 | } |
| 101 | } |
| 102 | return $msgs_str; |
| 103 | } |
| 104 | |
| 105 | /* returns the references header lines */ |
| 106 | function get_reference_header ($imap_stream, $message) { |
| 107 | global $uid_support; |
| 108 | $responses = array (); |
| 109 | $results = array(); |
| 110 | $references = ""; |
| 111 | $responses = sqimap_run_command_list ($imap_stream, "FETCH $message BODY[HEADER.FIELDS (References)]", true, $response, $message, $uid_support); |
| 112 | if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) { |
| 113 | $responses = array (); |
| 114 | } |
| 115 | return $responses; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | /* get sort order from server and |
| 120 | * return it as the $id array for |
| 121 | * mailbox_display |
| 122 | */ |
| 123 | |
| 124 | function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) { |
| 125 | global $default_charset, $thread_sort_messages, |
| 126 | $internal_date_sort, $server_sort_array, |
| 127 | $sent_folder, $mailbox, $uid_support; |
| 128 | |
| 129 | if (sqsession_is_registered('server_sort_array')) { |
| 130 | sqsession_unregister('server_sort_array'); |
| 131 | } |
| 132 | |
| 133 | $sort_on = array(); |
| 134 | $reverse = 0; |
| 135 | $server_sort_array = array(); |
| 136 | $sort_test = array(); |
| 137 | $sort_query = ''; |
| 138 | |
| 139 | if ($sort == 6) { |
| 140 | if ($uid_support) { |
| 141 | if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) { |
| 142 | $uidnext = $mbxresponse['UIDNEXT']-1; |
| 143 | } else { |
| 144 | $uidnext = '*'; |
| 145 | } |
| 146 | $query = "SEARCH UID 1:$uidnext"; |
| 147 | $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true); |
| 148 | if (isset($uids[0])) { |
| 149 | if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) { |
| 150 | $server_sort_array = preg_split("/ /", trim($regs[1])); |
| 151 | } |
| 152 | } |
| 153 | if (!preg_match("/OK/", $response)) { |
| 154 | $server_sort_array = 'no'; |
| 155 | } |
| 156 | } else { |
| 157 | $qty = $mbxresponse['EXISTS']; |
| 158 | $server_sort_array = range(1, $qty); |
| 159 | } |
| 160 | $server_sort_array = array_reverse($server_sort_array); |
| 161 | sqsession_register($server_sort_array, 'server_sort_array'); |
| 162 | return $server_sort_array; |
| 163 | } |
| 164 | |
| 165 | $sort_on = array (0=> 'DATE', |
| 166 | 1=> 'DATE', |
| 167 | 2=> 'FROM', |
| 168 | 3=> 'FROM', |
| 169 | 4=> 'SUBJECT', |
| 170 | 5=> 'SUBJECT'); |
| 171 | if ($internal_date_sort == true) { |
| 172 | $sort_on[0] = 'ARRIVAL'; |
| 173 | $sort_on[1] = 'ARRIVAL'; |
| 174 | } |
| 175 | if ($sent_folder == $mailbox) { |
| 176 | $sort_on[2] = 'TO'; |
| 177 | $sort_on[3] = 'TO'; |
| 178 | } |
| 179 | if (!empty($sort_on[$sort])) { |
| 180 | $query = "SORT ($sort_on[$sort]) ".strtoupper($default_charset).' ALL'; |
| 181 | $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, $uid_support); |
| 182 | } |
| 183 | if (isset($sort_test[0])) { |
| 184 | if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) { |
| 185 | $server_sort_array = preg_split("/ /", trim($regs[1])); |
| 186 | } |
| 187 | } |
| 188 | if ($sort == 0 || $sort == 2 || $sort == 4) { |
| 189 | $server_sort_array = array_reverse($server_sort_array); |
| 190 | } |
| 191 | if (!preg_match("/OK/", $response)) { |
| 192 | $server_sort_array = 'no'; |
| 193 | } |
| 194 | sqsession_register($server_sort_array, 'server_sort_array'); |
| 195 | return $server_sort_array; |
| 196 | } |
| 197 | |
| 198 | |
| 199 | function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) { |
| 200 | global $uid_support; |
| 201 | |
| 202 | if (sqsession_is_registered('php_sort_array')) { |
| 203 | sqsession_unregister('php_sort_array'); |
| 204 | } |
| 205 | |
| 206 | $php_sort_array = array(); |
| 207 | |
| 208 | if ($uid_support) { |
| 209 | if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) { |
| 210 | $uidnext = $mbxresponse['UIDNEXT']-1; |
| 211 | } else { |
| 212 | $uidnext = '*'; |
| 213 | } |
| 214 | $query = "SEARCH UID 1:$uidnext"; |
| 215 | $uids = sqimap_run_command ($imap_stream, $query, true, $response, $message, true); |
| 216 | if (isset($uids[0])) { |
| 217 | if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) { |
| 218 | $php_sort_array = preg_split("/ /", trim($regs[1])); |
| 219 | } |
| 220 | } |
| 221 | if (!preg_match("/OK/", $response)) { |
| 222 | $php_sort_array = 'no'; |
| 223 | } |
| 224 | } else { |
| 225 | $qty = $mbxresponse['EXISTS']; |
| 226 | $php_sort_array = range(1, $qty); |
| 227 | } |
| 228 | sqsession_register($php_sort_array, 'php_sort_array'); |
| 229 | return $php_sort_array; |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /* returns an indent array for printMessageinfo() |
| 234 | this represents the amount of indent needed (value) |
| 235 | for this message number (key) |
| 236 | */ |
| 237 | |
| 238 | function get_parent_level ($imap_stream) { |
| 239 | global $sort_by_ref, $default_charset, $thread_new; |
| 240 | $parent = ""; |
| 241 | $child = ""; |
| 242 | $cutoff = 0; |
| 243 | |
| 244 | /* loop through the threads and take unwanted characters out |
| 245 | of the thread string then chop it up |
| 246 | */ |
| 247 | for ($i=0;$i<count($thread_new);$i++) { |
| 248 | $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]); |
| 249 | $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]); |
| 250 | $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY); |
| 251 | } |
| 252 | $indent_array = array(); |
| 253 | if (!$thread_new) { |
| 254 | $thread_new = array(); |
| 255 | } |
| 256 | /* looping through the parts of one message thread */ |
| 257 | |
| 258 | for ($i=0;$i<count($thread_new);$i++) { |
| 259 | /* first grab the parent, it does not indent */ |
| 260 | |
| 261 | if (isset($thread_new[$i][0])) { |
| 262 | if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) { |
| 263 | $parent = $regs[1]; |
| 264 | } |
| 265 | } |
| 266 | $indent_array[$parent] = 0; |
| 267 | |
| 268 | /* now the children, checking each thread portion for |
| 269 | ),(, and space, adjusting the level and space values |
| 270 | to get the indent level |
| 271 | */ |
| 272 | $level = 0; |
| 273 | $spaces = array(); |
| 274 | $spaces_total = 0; |
| 275 | $indent = 0; |
| 276 | $fake = FALSE; |
| 277 | for ($k=1;$k<(count($thread_new[$i]))-1;$k++) { |
| 278 | $chars = count_chars($thread_new[$i][$k], 1); |
| 279 | if (isset($chars['40'])) { /* testing for ( */ |
| 280 | $level = $level + $chars['40']; |
| 281 | } |
| 282 | if (isset($chars['41'])) { /* testing for ) */ |
| 283 | $level = $level - $chars['41']; |
| 284 | $spaces[$level] = 0; |
| 285 | /* if we were faking lets stop, this portion |
| 286 | of the thread is over |
| 287 | */ |
| 288 | if ($level == $cutoff) { |
| 289 | $fake = FALSE; |
| 290 | } |
| 291 | } |
| 292 | if (isset($chars['32'])) { /* testing for space */ |
| 293 | if (!isset($spaces[$level])) { |
| 294 | $spaces[$level] = 0; |
| 295 | } |
| 296 | $spaces[$level] = $spaces[$level] + $chars['32']; |
| 297 | } |
| 298 | for ($x=0;$x<=$level;$x++) { |
| 299 | if (isset($spaces[$x])) { |
| 300 | $spaces_total = $spaces_total + $spaces[$x]; |
| 301 | } |
| 302 | } |
| 303 | $indent = $level + $spaces_total; |
| 304 | /* must have run into a message that broke the thread |
| 305 | so we are adjusting for that portion |
| 306 | */ |
| 307 | if ($fake == TRUE) { |
| 308 | $indent = $indent +1; |
| 309 | } |
| 310 | if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) { |
| 311 | $child = $regs[1]; |
| 312 | } |
| 313 | /* the thread must be broken if $indent == 0 |
| 314 | so indent the message once and start faking it |
| 315 | */ |
| 316 | if ($indent == 0) { |
| 317 | $indent = 1; |
| 318 | $fake = TRUE; |
| 319 | $cutoff = $level; |
| 320 | } |
| 321 | /* dont need abs but if indent was negative |
| 322 | errors would occur |
| 323 | */ |
| 324 | $indent_array[$child] = abs($indent); |
| 325 | $spaces_total = 0; |
| 326 | } |
| 327 | } |
| 328 | return $indent_array; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | /* returns an array with each element as a string |
| 333 | representing one message thread as returned by |
| 334 | the IMAP server |
| 335 | */ |
| 336 | |
| 337 | function get_thread_sort ($imap_stream) { |
| 338 | global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $uid_support; |
| 339 | if (sqsession_is_registered('thread_new')) { |
| 340 | sqsession_unregister('thread_new'); |
| 341 | } |
| 342 | if (sqsession_is_registered('server_sort_array')) { |
| 343 | sqsession_unregister('server_sort_array'); |
| 344 | } |
| 345 | $thread_temp = array (); |
| 346 | if ($sort_by_ref == 1) { |
| 347 | $sort_type = 'REFERENCES'; |
| 348 | } |
| 349 | else { |
| 350 | $sort_type = 'ORDEREDSUBJECT'; |
| 351 | } |
| 352 | $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL"; |
| 353 | $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, $uid_support); |
| 354 | if (isset($thread_test[0])) { |
| 355 | for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) { |
| 356 | if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) { |
| 357 | $thread_list = trim($regs[1]); |
| 358 | break; |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | else { |
| 363 | $thread_list = ""; |
| 364 | } |
| 365 | if (!preg_match("/OK/", $response)) { |
| 366 | $server_sort_array = 'no'; |
| 367 | return $server_sort_array; |
| 368 | } |
| 369 | if (isset($thread_list)) { |
| 370 | $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
| 371 | } |
| 372 | $char_count = count($thread_temp); |
| 373 | $counter = 0; |
| 374 | $thread_new = array(); |
| 375 | $k = 0; |
| 376 | $thread_new[0] = ""; |
| 377 | for ($i=0;$i<$char_count;$i++) { |
| 378 | if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') { |
| 379 | $thread_new[$k] = $thread_new[$k] . $thread_temp[$i]; |
| 380 | } |
| 381 | elseif ($thread_temp[$i] == '(') { |
| 382 | $thread_new[$k] .= $thread_temp[$i]; |
| 383 | $counter++; |
| 384 | } |
| 385 | elseif ($thread_temp[$i] == ')') { |
| 386 | if ($counter > 1) { |
| 387 | $thread_new[$k] .= $thread_temp[$i]; |
| 388 | $counter = $counter - 1; |
| 389 | } |
| 390 | else { |
| 391 | $thread_new[$k] .= $thread_temp[$i]; |
| 392 | $k++; |
| 393 | $thread_new[$k] = ""; |
| 394 | $counter = $counter - 1; |
| 395 | } |
| 396 | } |
| 397 | } |
| 398 | sqsession_register($thread_new, 'thread_new'); |
| 399 | $thread_new = array_reverse($thread_new); |
| 400 | $thread_list = implode(" ", $thread_new); |
| 401 | $thread_list = str_replace("(", " ", $thread_list); |
| 402 | $thread_list = str_replace(")", " ", $thread_list); |
| 403 | $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
| 404 | $server_sort_array = $thread_list; |
| 405 | sqsession_register($server_sort_array, 'server_sort_array'); |
| 406 | return $thread_list; |
| 407 | } |
| 408 | |
| 409 | |
| 410 | function elapsedTime($start) { |
| 411 | $stop = gettimeofday(); |
| 412 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
| 413 | return $timepassed; |
| 414 | } |
| 415 | |
| 416 | // only used in sqimap_get_small_header_list |
| 417 | function parseString($read,&$i) { |
| 418 | $char = $read{$i}; |
| 419 | $s = ''; |
| 420 | if ($char == '"') { |
| 421 | $iPos = ++$i; |
| 422 | while (true) { |
| 423 | $iPos = strpos($read,'"',$iPos); |
| 424 | if (!$iPos) break; |
| 425 | if ($iPos && $read{$iPos -1} != '\\') { |
| 426 | $s = substr($read,$i,($iPos-$i)); |
| 427 | $i = $iPos; |
| 428 | break; |
| 429 | } |
| 430 | $iPos++; |
| 431 | if ($iPos > strlen($read)) { |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | } else if ($char == '{') { |
| 436 | $lit_cnt = ''; |
| 437 | ++$i; |
| 438 | $iPos = strpos($read,'}',$i); |
| 439 | if ($iPos) { |
| 440 | $lit_cnt = substr($read, $i, $iPos - $i); |
| 441 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
| 442 | /* Now read the literal */ |
| 443 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
| 444 | $i += $lit_cnt; |
| 445 | /* temp bugfix (SM 1.5 will have a working clean version) |
| 446 | too much work to implement that version right now */ |
| 447 | --$i; |
| 448 | } else { /* should never happen */ |
| 449 | $i += 3; /* } + \r + \n */ |
| 450 | $s = ''; |
| 451 | } |
| 452 | } else { |
| 453 | return false; |
| 454 | } |
| 455 | ++$i; |
| 456 | return $s; |
| 457 | } |
| 458 | |
| 459 | // only used in sqimap_get_small_header_list |
| 460 | function parseArray($read,&$i) { |
| 461 | $i = strpos($read,'(',$i); |
| 462 | $i_pos = strpos($read,')',$i); |
| 463 | $s = substr($read,$i+1,$i_pos - $i -1); |
| 464 | $a = explode(' ',$s); |
| 465 | if ($i_pos) { |
| 466 | $i = $i_pos+1; |
| 467 | return $a; |
| 468 | } else { |
| 469 | return false; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false) { |
| 474 | global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type; |
| 475 | global $uid_support, $allow_server_sort; |
| 476 | /* Get the small headers for each message in $msg_list */ |
| 477 | $maxmsg = sizeof($msg_list); |
| 478 | if ($show_num != '999999') { |
| 479 | $msgs_str = sqimap_message_list_squisher($msg_list); |
| 480 | } else { |
| 481 | $msgs_str = '1:*'; |
| 482 | } |
| 483 | $messages = array(); |
| 484 | $read_list = array(); |
| 485 | |
| 486 | /* |
| 487 | * We need to return the data in the same order as the caller supplied |
| 488 | * in $msg_list, but IMAP servers are free to return responses in |
| 489 | * whatever order they wish... So we need to re-sort manually |
| 490 | */ |
| 491 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
| 492 | $messages["$msg_list[$i]"] = array(); |
| 493 | } |
| 494 | |
| 495 | $internaldate = getPref($data_dir, $username, 'internal_date_sort'); |
| 496 | if ($internaldate) { |
| 497 | $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])"; |
| 498 | } else { |
| 499 | $query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])"; |
| 500 | } |
| 501 | $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $uid_support); |
| 502 | $i = 0; |
| 503 | |
| 504 | foreach ($read_list as $r) { |
| 505 | $subject = _("(no subject)"); |
| 506 | $from = _("Unknown Sender"); |
| 507 | $priority = 0; |
| 508 | $messageid = '<>'; |
| 509 | $cc = $to = $date = $type[0] = $type[1] = $inrepto = ''; |
| 510 | $flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false; |
| 511 | |
| 512 | $read = implode('',$r); |
| 513 | |
| 514 | /* |
| 515 | * #id<space>FETCH<space>( |
| 516 | */ |
| 517 | |
| 518 | /* extract the message id */ |
| 519 | $i_space = strpos($read,' ',2); |
| 520 | $id = substr($read,2,$i_space-2); |
| 521 | $fetch = substr($read,$i_space+1,5); |
| 522 | if (!is_numeric($id) && $fetch !== 'FETCH') { |
| 523 | set_up_language($squirrelmail_language); |
| 524 | echo '<br><b><font color=$color[2]>' . |
| 525 | _("ERROR : Could not complete request.") . |
| 526 | '</b><br>' . |
| 527 | _("Unknown response from IMAP server: ") . ' 1.' . |
| 528 | htmlspecialchars($read) . "</font><br>\n"; |
| 529 | break; |
| 530 | } |
| 531 | $i = strpos($read,'(',$i_space+5); |
| 532 | $read = substr($read,$i+1); |
| 533 | $i_len = strlen($read); |
| 534 | $i = 0; |
| 535 | while ($i < $i_len && $i !== false) { |
| 536 | /* get argument */ |
| 537 | $read = trim(substr($read,$i)); |
| 538 | $i_len = strlen($read); |
| 539 | $i = strpos($read,' '); |
| 540 | $arg = substr($read,0,$i); |
| 541 | ++$i; |
| 542 | switch ($arg) |
| 543 | { |
| 544 | case 'UID': |
| 545 | $i_pos = strpos($read,' ',$i); |
| 546 | if (!$i_pos) { |
| 547 | $i_pos = strpos($read,')',$i); |
| 548 | } |
| 549 | if ($i_pos) { |
| 550 | $unique_id = substr($read,$i,$i_pos-$i); |
| 551 | $i = $i_pos+1; |
| 552 | } else { |
| 553 | break 3; |
| 554 | } |
| 555 | break; |
| 556 | case 'FLAGS': |
| 557 | $flags = parseArray($read,$i); |
| 558 | if (!$flags) break 3; |
| 559 | foreach ($flags as $flag) { |
| 560 | $flag = strtolower($flag); |
| 561 | switch ($flag) |
| 562 | { |
| 563 | case '\\seen': $flag_seen = true; break; |
| 564 | case '\\answered': $flag_answered = true; break; |
| 565 | case '\\deleted': $flag_deleted = true; break; |
| 566 | case '\\flagged': $flag_flagged = true; break; |
| 567 | default: break; |
| 568 | } |
| 569 | } |
| 570 | break; |
| 571 | case 'RFC822.SIZE': |
| 572 | $i_pos = strpos($read,' ',$i); |
| 573 | if (!$i_pos) { |
| 574 | $i_pos = strpos($read,')',$i); |
| 575 | } |
| 576 | if ($i_pos) { |
| 577 | $size = substr($read,$i,$i_pos-$i); |
| 578 | $i = $i_pos+1; |
| 579 | } else { |
| 580 | break 3; |
| 581 | } |
| 582 | |
| 583 | break; |
| 584 | case 'INTERNALDATE': |
| 585 | $date = parseString($read,$i); |
| 586 | //if ($tmpdate === false) break 3; |
| 587 | //$tmpdate = str_replace(' ',' ',$tmpdate); |
| 588 | //$tmpdate = explode(' ',$tmpdate); |
| 589 | //$date = str_replace('-',' ',$tmpdate[0]) . " " . |
| 590 | // $tmpdate[1] . ' ' . $tmpdate[2]; |
| 591 | break; |
| 592 | case 'BODY.PEEK[HEADER.FIELDS': |
| 593 | case 'BODY[HEADER.FIELDS': |
| 594 | $i = strpos($read,'{',$i); |
| 595 | $header = parseString($read,$i); |
| 596 | if ($header === false) break 3; |
| 597 | /* First we unfold the header */ |
| 598 | $hdr = trim(str_replace(array("\r\n\t", "\r\n "),array('', ''), $header)); |
| 599 | /* Now we can make a new header array with */ |
| 600 | /* each element representing a headerline */ |
| 601 | $hdr = explode("\r\n" , $hdr); |
| 602 | foreach ($hdr as $line) { |
| 603 | $pos = strpos($line, ':'); |
| 604 | if ($pos > 0) { |
| 605 | $field = strtolower(substr($line, 0, $pos)); |
| 606 | if (!strstr($field,' ')) { /* valid field */ |
| 607 | $value = trim(substr($line, $pos+1)); |
| 608 | switch($field) |
| 609 | { |
| 610 | case 'to': $to = $value; break; |
| 611 | case 'cc': $cc = $value; break; |
| 612 | case 'from': $from = $value; break; |
| 613 | case 'date': $date = $value; break; |
| 614 | case 'x-priority': $priority = $value; break; |
| 615 | case 'subject': |
| 616 | $subject = $value; |
| 617 | if ($subject == "") { |
| 618 | $subject = _("(no subject)"); |
| 619 | } |
| 620 | break; |
| 621 | case 'content-type': |
| 622 | $type = $value; |
| 623 | if ($pos = strpos($type, ";")) { |
| 624 | $type = substr($type, 0, $pos); |
| 625 | } |
| 626 | $type = explode("/", $type); |
| 627 | if(!is_array($type)) { |
| 628 | $type[0] = 'text'; |
| 629 | } |
| 630 | if (!isset($type[1])) { |
| 631 | $type[1] = ''; |
| 632 | } |
| 633 | break; |
| 634 | default: break; |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | break; |
| 640 | default: |
| 641 | ++$i; |
| 642 | break; |
| 643 | } |
| 644 | } |
| 645 | if (isset($date)) { |
| 646 | $date = str_replace(' ', ' ', $date); |
| 647 | $tmpdate = explode(' ', trim($date)); |
| 648 | } else { |
| 649 | $tmpdate = $date = array('', '', '', '', '', ''); |
| 650 | } |
| 651 | if ($uid_support) { |
| 652 | $msgi ="$unique_id"; |
| 653 | $messages[$msgi]['ID'] = $unique_id; |
| 654 | } else { |
| 655 | $msgi = "$id"; |
| 656 | $messages[$msgi]['ID'] = $id; |
| 657 | } |
| 658 | $messages[$msgi]['TIME_STAMP'] = getTimeStamp($tmpdate); |
| 659 | $messages[$msgi]['DATE_STRING'] = getDateString($messages[$msgi]['TIME_STAMP']); |
| 660 | $messages[$msgi]['FROM'] = $from; //parseAddress($from); |
| 661 | $messages[$msgi]['SUBJECT'] = $subject; |
| 662 | // if (handleAsSent($mailbox)) { |
| 663 | $messages[$msgi]['TO'] = $to; //parseAddress($to); |
| 664 | // } |
| 665 | $messages[$msgi]['PRIORITY'] = $priority; |
| 666 | $messages[$msgi]['CC'] = $cc; //parseAddress($cc); |
| 667 | $messages[$msgi]['SIZE'] = $size; |
| 668 | $messages[$msgi]['TYPE0'] = $type[0]; |
| 669 | $messages[$msgi]['FLAG_DELETED'] = $flag_deleted; |
| 670 | $messages[$msgi]['FLAG_ANSWERED'] = $flag_answered; |
| 671 | $messages[$msgi]['FLAG_SEEN'] = $flag_seen; |
| 672 | $messages[$msgi]['FLAG_FLAGGED'] = $flag_flagged; |
| 673 | |
| 674 | /* non server sort stuff */ |
| 675 | if (!$allow_server_sort) { |
| 676 | $from = parseAddress($from); |
| 677 | if ($from[0][1]) { |
| 678 | $from = decodeHeader($from[0][1]); |
| 679 | } else { |
| 680 | $from = $from[0][0]; |
| 681 | } |
| 682 | $messages[$msgi]['FROM-SORT'] = $from; |
| 683 | $subject_sort = strtolower(decodeHeader($subject)); |
| 684 | if (preg_match("/^(vedr|sv|re|aw):\s*(.*)$/si", $subject_sort, $matches)){ |
| 685 | $messages[$msgi]['SUBJECT-SORT'] = $matches[2]; |
| 686 | } else { |
| 687 | $messages[$msgi]['SUBJECT-SORT'] = $subject_sort; |
| 688 | } |
| 689 | } |
| 690 | ++$msgi; |
| 691 | } |
| 692 | array_reverse($messages); |
| 693 | $new_messages = array(); |
| 694 | foreach ($messages as $i =>$message) { |
| 695 | $new_messages[] = $message; |
| 696 | } |
| 697 | return $new_messages; |
| 698 | } |
| 699 | |
| 700 | // obsolete? |
| 701 | function sqimap_get_headerfield($imap_stream, $field) { |
| 702 | global $uid_support; |
| 703 | $sid = sqimap_session_id(false); |
| 704 | |
| 705 | $results = array(); |
| 706 | $read_list = array(); |
| 707 | |
| 708 | $query = "FETCH 1:* (UID BODY.PEEK[HEADER.FIELDS ($field)])"; |
| 709 | $readin_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $uid_support); |
| 710 | $i = 0; |
| 711 | |
| 712 | foreach ($readin_list as $r) { |
| 713 | $r = implode('',$r); |
| 714 | /* first we unfold the header */ |
| 715 | $r = str_replace(array("\r\n\t","\r\n\s"),array('',''),$r); |
| 716 | /* |
| 717 | * now we can make a new header array with each element representing |
| 718 | * a headerline |
| 719 | */ |
| 720 | $r = explode("\r\n" , $r); |
| 721 | if (!$uid_support) { |
| 722 | if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) { |
| 723 | set_up_language($squirrelmail_language); |
| 724 | echo '<br><b><font color=$color[2]>' . |
| 725 | _("ERROR : Could not complete request.") . |
| 726 | '</b><br>' . |
| 727 | _("Unknown response from IMAP server: ") . ' 1.' . |
| 728 | $r[0] . "</font><br>\n"; |
| 729 | } else { |
| 730 | $id = $regs[1]; |
| 731 | } |
| 732 | } else { |
| 733 | if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) { |
| 734 | set_up_language($squirrelmail_language); |
| 735 | echo '<br><b><font color=$color[2]>' . |
| 736 | _("ERROR : Could not complete request.") . |
| 737 | '</b><br>' . |
| 738 | _("Unknown response from IMAP server: ") . ' 1.' . |
| 739 | $r[0] . "</font><br>\n"; |
| 740 | } else { |
| 741 | $id = $regs[2]; |
| 742 | } |
| 743 | } |
| 744 | $field = $r[1]; |
| 745 | $field = substr($field,strlen($field)+2); |
| 746 | $result[] = array($id,$field); |
| 747 | } |
| 748 | return $result; |
| 749 | } |
| 750 | |
| 751 | |
| 752 | |
| 753 | |
| 754 | |
| 755 | /* |
| 756 | * Returns a message array with all the information about a message. |
| 757 | * See the documentation folder for more information about this array. |
| 758 | */ |
| 759 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
| 760 | global $uid_support; |
| 761 | |
| 762 | $flags = array(); |
| 763 | $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support); |
| 764 | if ($read) { |
| 765 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
| 766 | if (trim($regs[1])) { |
| 767 | $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY'); |
| 768 | } |
| 769 | } |
| 770 | } else { |
| 771 | /* the message was not found, maybe the mailbox was modified? */ |
| 772 | global $sort, $startMessage, $color; |
| 773 | |
| 774 | $errmessage = _("The server couldn't find the message you requested.") . |
| 775 | '<p>'._("Most probably your message list was out of date and the message has been moved away or deleted (perhaps by another program accessing the same mailbox)."); |
| 776 | /* this will include a link back to the message list */ |
| 777 | error_message($errmessage, $mailbox, $sort, $startMessage, $color); |
| 778 | exit; |
| 779 | } |
| 780 | $bodystructure = implode('',$read); |
| 781 | $msg = mime_structure($bodystructure,$flags); |
| 782 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support); |
| 783 | $rfc822_header = new Rfc822Header(); |
| 784 | $rfc822_header->parseHeader($read); |
| 785 | $msg->rfc822_header = $rfc822_header; |
| 786 | return $msg; |
| 787 | } |
| 788 | |
| 789 | /* Wrapper function that reformats the header information. */ |
| 790 | // obsolete? |
| 791 | function sqimap_get_message_header ($imap_stream, $id, $mailbox) { |
| 792 | global $uid_support; |
| 793 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support); |
| 794 | $header = sqimap_get_header($imap_stream, $read); |
| 795 | $header->id = $id; |
| 796 | $header->mailbox = $mailbox; |
| 797 | return $header; |
| 798 | } |
| 799 | |
| 800 | /* Wrapper function that reformats the entity header information. */ |
| 801 | // obsolete? |
| 802 | function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) { |
| 803 | global $uid_support; |
| 804 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support); |
| 805 | $header = sqimap_get_header($imap_stream, $read); |
| 806 | $header->id = $id; |
| 807 | $header->mailbox = $mailbox; |
| 808 | return $header; |
| 809 | } |
| 810 | |
| 811 | /* function to get the mime headers */ |
| 812 | // obsolete? |
| 813 | function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) { |
| 814 | global $uid_support; |
| 815 | $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support); |
| 816 | $header = sqimap_get_header($imap_stream, $read); |
| 817 | $header->id = $id; |
| 818 | $header->mailbox = $mailbox; |
| 819 | return $header; |
| 820 | } |
| 821 | |
| 822 | ?> |