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