| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_messages.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This implements functions that manipulate messages |
| 10 | * |
| 11 | * $Id$ |
| 12 | */ |
| 13 | |
| 14 | /* Copies specified messages to specified folder */ |
| 15 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
| 16 | global $uid_support; |
| 17 | $read = sqimap_run_command ($imap_stream, "COPY $start:$end \"$mailbox\"", true, $response, $message, $uid_support); |
| 18 | } |
| 19 | |
| 20 | /* Deletes specified messages and moves them to trash if possible */ |
| 21 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox) { |
| 22 | global $move_to_trash, $trash_folder, $auto_expunge, $uid_support; |
| 23 | |
| 24 | if (($move_to_trash == true) && (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
| 25 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
| 26 | } |
| 27 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted"); |
| 28 | } |
| 29 | |
| 30 | /* Sets the specified messages with specified flag */ |
| 31 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag) { |
| 32 | global $uid_support; |
| 33 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", true, $response, $message, $uid_support); |
| 34 | } |
| 35 | |
| 36 | /* Remove specified flag from specified messages */ |
| 37 | function sqimap_messages_remove_flag ($imap_stream, $start, $end, $flag) { |
| 38 | global $uid_support; |
| 39 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end -FLAGS (\\$flag)", true, $response, $message, $uid_support); |
| 40 | } |
| 41 | |
| 42 | /* Returns some general header information -- FROM, DATE, and SUBJECT */ |
| 43 | class small_header { |
| 44 | var $from = '', $subject = '', $date = '', $to = '', |
| 45 | $priority = 0, $message_id = 0, $cc = '', $uid = ''; |
| 46 | } |
| 47 | |
| 48 | function sqimap_get_small_header ($imap_stream, $id, $sent) { |
| 49 | $res = sqimap_get_small_header_list($imap_stream, array($id), $sent); |
| 50 | return $res[0]; |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Sort the message list and crunch to be as small as possible |
| 55 | * (overflow could happen, so make it small if possible) |
| 56 | */ |
| 57 | function sqimap_message_list_squisher($messages_array) { |
| 58 | if( !is_array( $messages_array ) ) { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | sort($messages_array, SORT_NUMERIC); |
| 63 | $msgs_str = ''; |
| 64 | while ($messages_array) { |
| 65 | $start = array_shift($messages_array); |
| 66 | $end = $start; |
| 67 | while (isset($messages_array[0]) && $messages_array[0] == $end + 1) { |
| 68 | $end = array_shift($messages_array); |
| 69 | } |
| 70 | if ($msgs_str != '') { |
| 71 | $msgs_str .= ','; |
| 72 | } |
| 73 | $msgs_str .= $start; |
| 74 | if ($start != $end) { |
| 75 | $msgs_str .= ':' . $end; |
| 76 | } |
| 77 | } |
| 78 | return $msgs_str; |
| 79 | } |
| 80 | |
| 81 | /* returns the references header lines */ |
| 82 | function get_reference_header ($imap_stream, $message) { |
| 83 | global $uid_support; |
| 84 | $responses = array (); |
| 85 | $sid = sqimap_session_id($uid_support); |
| 86 | $results = array(); |
| 87 | $references = ""; |
| 88 | $query = "$sid FETCH $message BODY[HEADER.FIELDS (References)]\r\n"; |
| 89 | fputs ($imap_stream, $query); |
| 90 | $responses = sqimap_read_data_list($imap_stream, $sid, true, $responses, $message); |
| 91 | if (!eregi("^\\* ([0-9]+) FETCH", $responses[0][0], $regs)) { |
| 92 | $responses = array (); |
| 93 | } |
| 94 | return $responses; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | /* get sort order from server and |
| 99 | * return it as the $id array for |
| 100 | * mailbox_display |
| 101 | */ |
| 102 | |
| 103 | function sqimap_get_sort_order ($imap_stream, $sort, $mbxresponse) { |
| 104 | global $default_charset, $thread_sort_messages, |
| 105 | $internal_date_sort, $server_sort_array, |
| 106 | $sent_folder, $mailbox, $uid_support; |
| 107 | |
| 108 | if (session_is_registered('server_sort_array')) { |
| 109 | session_unregister('server_sort_array'); |
| 110 | } |
| 111 | |
| 112 | $sid = sqimap_session_id($uid_support); |
| 113 | $sort_on = array(); |
| 114 | $reverse = 0; |
| 115 | $server_sort_array = array(); |
| 116 | $sort_test = array(); |
| 117 | $sort_query = ''; |
| 118 | |
| 119 | if ($sort == 6) { |
| 120 | if ($uid_support) { |
| 121 | $uidnext = $mbxresponse['UIDNEXT']-1; |
| 122 | $uid_query = "$sid SEARCH UID 1:$uidnext\r\n"; |
| 123 | fputs($imap_stream, $uid_query); |
| 124 | $uids = sqimap_read_data($imap_stream, $sid, true ,&$response, $message); |
| 125 | if (isset($uids[0])) { |
| 126 | if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) { |
| 127 | $server_sort_array = preg_split("/ /", trim($regs[1])); |
| 128 | } |
| 129 | } |
| 130 | if (!preg_match("/OK/", $response)) { |
| 131 | $server_sort_array = 'no'; |
| 132 | } |
| 133 | } else { |
| 134 | $qty = $mbxresponse['EXISTS']; |
| 135 | $server_sort_array = range(1, $qty); |
| 136 | } |
| 137 | session_register('server_sort_array'); |
| 138 | return $server_sort_array; |
| 139 | } |
| 140 | |
| 141 | $sort_on = array (0=> 'DATE', |
| 142 | 1=> 'DATE', |
| 143 | 2=> 'FROM', |
| 144 | 3=> 'FROM', |
| 145 | 4=> 'SUBJECT', |
| 146 | 5=> 'SUBJECT'); |
| 147 | if ($internal_date_sort == true) { |
| 148 | $sort_on[0] = 'ARRIVAL'; |
| 149 | $sort_on[1] = 'ARRIVAL'; |
| 150 | } |
| 151 | if ($sent_folder == $mailbox) { |
| 152 | $sort_on[2] = 'TO'; |
| 153 | $sort_on[3] = 'TO'; |
| 154 | } |
| 155 | if (!empty($sort_on[$sort])) { |
| 156 | $sort_query = "$sid SORT ($sort_on[$sort]) ".strtoupper($default_charset)." ALL\r\n"; |
| 157 | fputs($imap_stream, $sort_query); |
| 158 | $sort_test = sqimap_read_data($imap_stream, $sid, true ,&$response, $message); |
| 159 | } |
| 160 | if (isset($sort_test[0])) { |
| 161 | if (preg_match("/^\* SORT (.+)$/", $sort_test[0], $regs)) { |
| 162 | $server_sort_array = preg_split("/ /", trim($regs[1])); |
| 163 | } |
| 164 | } |
| 165 | if ($sort == 0 || $sort == 2 || $sort == 4) { |
| 166 | $server_sort_array = array_reverse($server_sort_array); |
| 167 | } |
| 168 | if (!preg_match("/OK/", $response)) { |
| 169 | $server_sort_array = 'no'; |
| 170 | } |
| 171 | session_register('server_sort_array'); |
| 172 | return $server_sort_array; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) { |
| 177 | global $uid_support; |
| 178 | |
| 179 | if (session_is_registered('php_sort_array')) { |
| 180 | session_unregister('php_sort_array'); |
| 181 | } |
| 182 | |
| 183 | $sid = sqimap_session_id($uid_support); |
| 184 | $php_sort_array = array(); |
| 185 | |
| 186 | if ($uid_support) { |
| 187 | $uidnext = $mbxresponse['UIDNEXT']-1; |
| 188 | $uid_query = "$sid SEARCH UID 1:$uidnext\r\n"; |
| 189 | fputs($imap_stream, $uid_query); |
| 190 | $uids = sqimap_read_data($imap_stream, $sid, true ,&$response, $message); |
| 191 | if (isset($uids[0])) { |
| 192 | if (preg_match("/^\* SEARCH (.+)$/", $uids[0], $regs)) { |
| 193 | $php_sort_array = preg_split("/ /", trim($regs[1])); |
| 194 | } |
| 195 | } |
| 196 | if (!preg_match("/OK/", $response)) { |
| 197 | $php_sort_array = 'no'; |
| 198 | } |
| 199 | } else { |
| 200 | $qty = $mbxresponse['EXISTS']; |
| 201 | $php_sort_array = range(1, $qty); |
| 202 | } |
| 203 | session_register('php_sort_array'); |
| 204 | return $php_sort_array; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | /* returns an indent array for printMessageinfo() |
| 209 | this represents the amount of indent needed (value) |
| 210 | for this message number (key) |
| 211 | */ |
| 212 | |
| 213 | function get_parent_level ($imap_stream) { |
| 214 | global $sort_by_ref, $default_charset, $thread_new; |
| 215 | $parent = ""; |
| 216 | $child = ""; |
| 217 | $cutoff = 0; |
| 218 | |
| 219 | /* loop through the threads and take unwanted characters out |
| 220 | of the thread string then chop it up |
| 221 | */ |
| 222 | for ($i=0;$i<count($thread_new);$i++) { |
| 223 | $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]); |
| 224 | $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]); |
| 225 | $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY); |
| 226 | } |
| 227 | $indent_array = array(); |
| 228 | if (!$thread_new) { |
| 229 | $thread_new = array(); |
| 230 | } |
| 231 | /* looping through the parts of one message thread */ |
| 232 | |
| 233 | for ($i=0;$i<count($thread_new);$i++) { |
| 234 | /* first grab the parent, it does not indent */ |
| 235 | |
| 236 | if (isset($thread_new[$i][0])) { |
| 237 | if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) { |
| 238 | $parent = $regs[1]; |
| 239 | } |
| 240 | } |
| 241 | $indent_array[$parent] = 0; |
| 242 | |
| 243 | /* now the children, checking each thread portion for |
| 244 | ),(, and space, adjusting the level and space values |
| 245 | to get the indent level |
| 246 | */ |
| 247 | $level = 0; |
| 248 | $spaces = array(); |
| 249 | $spaces_total = 0; |
| 250 | $indent = 0; |
| 251 | $fake = FALSE; |
| 252 | for ($k=1;$k<(count($thread_new[$i]))-1;$k++) { |
| 253 | $chars = count_chars($thread_new[$i][$k], 1); |
| 254 | if (isset($chars['40'])) { /* testing for ( */ |
| 255 | $level = $level + $chars['40']; |
| 256 | } |
| 257 | if (isset($chars['41'])) { /* testing for ) */ |
| 258 | $level = $level - $chars['41']; |
| 259 | $spaces[$level] = 0; |
| 260 | /* if we were faking lets stop, this portion |
| 261 | of the thread is over |
| 262 | */ |
| 263 | if ($level == $cutoff) { |
| 264 | $fake = FALSE; |
| 265 | } |
| 266 | } |
| 267 | if (isset($chars['32'])) { /* testing for space */ |
| 268 | if (!isset($spaces[$level])) { |
| 269 | $spaces[$level] = 0; |
| 270 | } |
| 271 | $spaces[$level] = $spaces[$level] + $chars['32']; |
| 272 | } |
| 273 | for ($x=0;$x<=$level;$x++) { |
| 274 | if (isset($spaces[$x])) { |
| 275 | $spaces_total = $spaces_total + $spaces[$x]; |
| 276 | } |
| 277 | } |
| 278 | $indent = $level + $spaces_total; |
| 279 | /* must have run into a message that broke the thread |
| 280 | so we are adjusting for that portion |
| 281 | */ |
| 282 | if ($fake == TRUE) { |
| 283 | $indent = $indent +1; |
| 284 | } |
| 285 | if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) { |
| 286 | $child = $regs[1]; |
| 287 | } |
| 288 | /* the thread must be broken if $indent == 0 |
| 289 | so indent the message once and start faking it |
| 290 | */ |
| 291 | if ($indent == 0) { |
| 292 | $indent = 1; |
| 293 | $fake = TRUE; |
| 294 | $cutoff = $level; |
| 295 | } |
| 296 | /* dont need abs but if indent was negative |
| 297 | errors would occur |
| 298 | */ |
| 299 | $indent_array[$child] = abs($indent); |
| 300 | $spaces_total = 0; |
| 301 | } |
| 302 | } |
| 303 | return $indent_array; |
| 304 | } |
| 305 | |
| 306 | |
| 307 | /* returns an array with each element as a string |
| 308 | representing one message thread as returned by |
| 309 | the IMAP server |
| 310 | */ |
| 311 | |
| 312 | function get_thread_sort ($imap_stream) { |
| 313 | global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $uid_support; |
| 314 | if (session_is_registered('thread_new')) { |
| 315 | session_unregister('thread_new'); |
| 316 | } |
| 317 | if (session_is_registered('server_sort_array')) { |
| 318 | session_unregister('server_sort_array'); |
| 319 | } |
| 320 | $sid = sqimap_session_id($uid_support); |
| 321 | $thread_temp = array (); |
| 322 | if ($sort_by_ref == 1) { |
| 323 | $sort_type = 'REFERENCES'; |
| 324 | } |
| 325 | else { |
| 326 | $sort_type = 'ORDEREDSUBJECT'; |
| 327 | } |
| 328 | $thread_query = "$sid THREAD $sort_type ".strtoupper($default_charset)." ALL\r\n"; |
| 329 | fputs($imap_stream, $thread_query); |
| 330 | $thread_test = sqimap_read_data($imap_stream, $sid, false, $response, $message); |
| 331 | if (isset($thread_test[0])) { |
| 332 | if (preg_match("/^\* THREAD (.+)$/", $thread_test[0], $regs)) { |
| 333 | $thread_list = trim($regs[1]); |
| 334 | } |
| 335 | } |
| 336 | else { |
| 337 | $thread_list = ""; |
| 338 | } |
| 339 | if (!preg_match("/OK/", $response)) { |
| 340 | $server_sort_array = 'no'; |
| 341 | return $server_sort_array; |
| 342 | } |
| 343 | if (isset($thread_list)) { |
| 344 | $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
| 345 | } |
| 346 | $char_count = count($thread_temp); |
| 347 | $counter = 0; |
| 348 | $thread_new = array(); |
| 349 | $k = 0; |
| 350 | $thread_new[0] = ""; |
| 351 | for ($i=0;$i<$char_count;$i++) { |
| 352 | if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') { |
| 353 | $thread_new[$k] = $thread_new[$k] . $thread_temp[$i]; |
| 354 | } |
| 355 | elseif ($thread_temp[$i] == '(') { |
| 356 | $thread_new[$k] .= $thread_temp[$i]; |
| 357 | $counter++; |
| 358 | } |
| 359 | elseif ($thread_temp[$i] == ')') { |
| 360 | if ($counter > 1) { |
| 361 | $thread_new[$k] .= $thread_temp[$i]; |
| 362 | $counter = $counter - 1; |
| 363 | } |
| 364 | else { |
| 365 | $thread_new[$k] .= $thread_temp[$i]; |
| 366 | $k++; |
| 367 | $thread_new[$k] = ""; |
| 368 | $counter = $counter - 1; |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | session_register('thread_new'); |
| 373 | $thread_new = array_reverse($thread_new); |
| 374 | $thread_list = implode(" ", $thread_new); |
| 375 | $thread_list = str_replace("(", " ", $thread_list); |
| 376 | $thread_list = str_replace(")", " ", $thread_list); |
| 377 | $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
| 378 | $server_sort_array = $thread_list; |
| 379 | session_register('server_sort_array'); |
| 380 | return $thread_list; |
| 381 | } |
| 382 | |
| 383 | function elapsedTime($start) { |
| 384 | $stop = gettimeofday(); |
| 385 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
| 386 | return $timepassed; |
| 387 | } |
| 388 | |
| 389 | function sqimap_get_small_header_list ($imap_stream, $msg_list) { |
| 390 | global $squirrelmail_language, $color, $data_dir, $username, $imap_server_type; |
| 391 | global $uid_support; |
| 392 | |
| 393 | /* Get the small headers for each message in $msg_list */ |
| 394 | $sid = sqimap_session_id($uid_support); |
| 395 | |
| 396 | $maxmsg = sizeof($msg_list); |
| 397 | $msgs_str = sqimap_message_list_squisher($msg_list); |
| 398 | $results = array(); |
| 399 | $read_list = array(); |
| 400 | /* |
| 401 | * We need to return the data in the same order as the caller supplied |
| 402 | * in $msg_list, but IMAP servers are free to return responses in |
| 403 | * whatever order they wish... So we need to re-sort manually |
| 404 | */ |
| 405 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
| 406 | $id2index[$msg_list[$i]] = $i; |
| 407 | } |
| 408 | |
| 409 | $internaldate = getPref($data_dir, $username, 'internal_date_sort'); |
| 410 | if ($internaldate) { |
| 411 | $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n"; |
| 412 | } else { |
| 413 | $query = "$sid FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To From Cc Subject X-Priority Content-Type)])\r\n"; |
| 414 | } |
| 415 | fputs ($imap_stream, $query); |
| 416 | $readin_list = sqimap_read_data_list($imap_stream, $sid, false, $response, $message); |
| 417 | $i = 0; |
| 418 | foreach ($readin_list as $r) { |
| 419 | if (!$uid_support) { |
| 420 | if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH/iAU",$r[0], $regs)) { |
| 421 | set_up_language($squirrelmail_language); |
| 422 | echo '<br><b><font color=$color[2]>' . |
| 423 | _("ERROR : Could not complete request.") . |
| 424 | '</b><br>' . |
| 425 | _("Unknown response from IMAP server: ") . ' 1.' . |
| 426 | $r[0] . "</font><br>\n"; |
| 427 | } else if (! isset($id2index[$regs[1]]) || !count($id2index[$regs[1]])) { |
| 428 | set_up_language($squirrelmail_language); |
| 429 | echo '<br><b><font color=$color[2]>' . |
| 430 | _("ERROR : Could not complete request.") . |
| 431 | '</b><br>' . |
| 432 | _("Unknown message number in reply from server: ") . |
| 433 | $regs[1] . "</font><br>\n"; |
| 434 | } else { |
| 435 | $read_list[$id2index[$regs[1]]] = $r; |
| 436 | } |
| 437 | } else { |
| 438 | if (!preg_match("/^\\*\s+([0-9]+)\s+FETCH.*UID\s+([0-9]+)\s+/iAU",$r[0], $regs)) { |
| 439 | set_up_language($squirrelmail_language); |
| 440 | echo '<br><b><font color=$color[2]>' . |
| 441 | _("ERROR : Could not complete request.") . |
| 442 | '</b><br>' . |
| 443 | _("Unknown response from IMAP server: ") . ' 1.' . |
| 444 | $r[0] . "</font><br>\n"; |
| 445 | } else if (! isset($id2index[$regs[2]]) || !count($id2index[$regs[2]])) { |
| 446 | set_up_language($squirrelmail_language); |
| 447 | echo '<br><b><font color=$color[2]>' . |
| 448 | _("ERROR : Could not complete request.") . |
| 449 | '</b><br>' . |
| 450 | _("Unknown message number in reply from server: ") . |
| 451 | $regs[2] . "</font><br>\n"; |
| 452 | } else { |
| 453 | $read_list[$id2index[$regs[2]]] = $r; |
| 454 | $unique_id = $regs[2]; |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | arsort($read_list); |
| 459 | |
| 460 | $patterns = array ( |
| 461 | "/^To:(.*)\$/AUi", |
| 462 | "/^From:(.*)\$/AUi", |
| 463 | "/^X-Priority:(.*)\$/AUi", |
| 464 | "/^Cc:(.*)\$/AUi", |
| 465 | "/^Date:(.*)\$/AUi", |
| 466 | "/^Subject:(.*)\$/AUi", |
| 467 | "/^Content-Type:(.*)\$/AUi" |
| 468 | ); |
| 469 | $regpattern = ''; |
| 470 | |
| 471 | for ($msgi = 0; $msgi < $maxmsg; $msgi++) { |
| 472 | $subject = _("(no subject)"); |
| 473 | $from = _("Unknown Sender"); |
| 474 | $priority = 0; |
| 475 | $messageid = "<>"; |
| 476 | $cc = ""; |
| 477 | $to = ""; |
| 478 | $date = ""; |
| 479 | $type[0] = ""; |
| 480 | $type[1] = ""; |
| 481 | $inrepto = ""; |
| 482 | $flag_seen = false; |
| 483 | $flag_answered = false; |
| 484 | $flag_deleted = false; |
| 485 | $flag_flagged = false; |
| 486 | $read = $read_list[$msgi]; |
| 487 | $prevline = false; |
| 488 | |
| 489 | foreach ($read as $read_part) { |
| 490 | //unfold multi-line headers |
| 491 | while ($prevline && strspn($read_part, "\t ") > 0) { |
| 492 | $read_part = substr($prevline, 0, -2) . ' ' . ltrim($read_part); |
| 493 | } |
| 494 | $prevline = $read_part; |
| 495 | if ($read_part{0} == '*') { |
| 496 | if ($internaldate) { |
| 497 | if (preg_match ("/^.+INTERNALDATE\s+\"(.+)\".+/iUA",$read_part, $reg)) { |
| 498 | $tmpdate = trim($reg[1]); |
| 499 | $tmpdate = str_replace(' ',' ',$tmpdate); |
| 500 | $tmpdate = explode(' ',$tmpdate); |
| 501 | $date = str_replace('-',' ',$tmpdate[0]) . " " . |
| 502 | $tmpdate[1] . " " . |
| 503 | $tmpdate[2]; |
| 504 | } |
| 505 | } |
| 506 | if (preg_match ("/^.+RFC822.SIZE\s+(\d+).+/iA",$read_part, $reg)) { |
| 507 | $size = $reg[1]; |
| 508 | } |
| 509 | if (preg_match("/^.+FLAGS\s+\((.*)\).+/iUA", $read_part, $regs)) { |
| 510 | $flags = explode(' ',trim($regs[1])); |
| 511 | foreach ($flags as $flag) { |
| 512 | $flag = strtolower($flag); |
| 513 | if ($flag == '\\seen') { |
| 514 | $flag_seen = true; |
| 515 | } else if ($flag == '\\answered') { |
| 516 | $flag_answered = true; |
| 517 | } else if ($flag == '\\deleted') { |
| 518 | $flag_deleted = true; |
| 519 | } else if ($flag == '\\flagged') { |
| 520 | $flag_flagged = true; |
| 521 | } |
| 522 | } |
| 523 | } |
| 524 | if (preg_match ("/^.+UID\s+(\d+).+/iA",$read_part, $reg)) { |
| 525 | $unique_id = $reg[1]; |
| 526 | } |
| 527 | } else { |
| 528 | $firstchar = $read_part{0}; |
| 529 | if ($firstchar == 'T') { |
| 530 | $regpattern = $patterns[0]; |
| 531 | $id = 1; |
| 532 | } else if ($firstchar == 'F') { |
| 533 | $regpattern = $patterns[1]; |
| 534 | $id = 2; |
| 535 | } else if ($firstchar == 'X') { |
| 536 | $regpattern = $patterns[2]; |
| 537 | $id = 3; |
| 538 | } else if ($firstchar == 'C') { |
| 539 | if (strtolower($read_part{1}) == 'c') { |
| 540 | $regpattern = $patterns[3]; |
| 541 | $id = 4; |
| 542 | } else if (strtolower($read_part{1}) == 'o') { |
| 543 | $regpattern = $patterns[6]; |
| 544 | $id = 7; |
| 545 | } |
| 546 | } else if ($firstchar == 'D' && !$internaldate ) { |
| 547 | $regpattern = $patterns[4]; |
| 548 | $id = 5; |
| 549 | } else if ($firstchar == 'S') { |
| 550 | $regpattern = $patterns[5]; |
| 551 | $id = 6; |
| 552 | } else $regpattern = ''; |
| 553 | |
| 554 | if ($regpattern) { |
| 555 | if (preg_match ($regpattern, $read_part, $regs)) { |
| 556 | switch ($id) { |
| 557 | case 1: |
| 558 | $to = $regs[1]; |
| 559 | break; |
| 560 | case 2: |
| 561 | $from = $regs[1]; |
| 562 | break; |
| 563 | case 3: |
| 564 | $priority = $regs[1]; |
| 565 | break; |
| 566 | case 4: |
| 567 | $cc = $regs[1]; |
| 568 | break; |
| 569 | case 5: |
| 570 | $date = $regs[1]; |
| 571 | break; |
| 572 | case 6: |
| 573 | $subject = htmlspecialchars(trim($regs[1])); |
| 574 | if ($subject == "") { |
| 575 | $subject = _("(no subject)"); |
| 576 | } |
| 577 | break; |
| 578 | case 7: |
| 579 | $type = strtolower(trim($regs[1])); |
| 580 | if ($pos = strpos($type, ";")) { |
| 581 | $type = substr($type, 0, $pos); |
| 582 | } |
| 583 | $type = explode("/", $type); |
| 584 | if (!isset($type[1])) { |
| 585 | $type[1] = ''; |
| 586 | } |
| 587 | break; |
| 588 | default: |
| 589 | break; |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | } |
| 596 | |
| 597 | $header = new small_header; |
| 598 | |
| 599 | if ($uid_support) { |
| 600 | $header->uid = $unique_id; |
| 601 | } else { |
| 602 | $header->uid = $msg_list[$msgi]; |
| 603 | } |
| 604 | $header->date = $date; |
| 605 | $header->subject = $subject; |
| 606 | $header->to = $to; |
| 607 | $header->from = $from; |
| 608 | $header->priority = $priority; |
| 609 | $header->message_id = $messageid; |
| 610 | $header->cc = $cc; |
| 611 | $header->size = $size; |
| 612 | $header->type0 = $type[0]; |
| 613 | $header->type1 = $type[1]; |
| 614 | $header->flag_seen = $flag_seen; |
| 615 | $header->flag_answered = $flag_answered; |
| 616 | $header->flag_deleted = $flag_deleted; |
| 617 | $header->flag_flagged = $flag_flagged; |
| 618 | $header->inrepto = $inrepto; |
| 619 | $result[] = $header; |
| 620 | } |
| 621 | return $result; |
| 622 | } |
| 623 | |
| 624 | /* |
| 625 | * Returns a message array with all the information about a message. |
| 626 | * See the documentation folder for more information about this array. |
| 627 | */ |
| 628 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
| 629 | global $uid_support; |
| 630 | |
| 631 | $flags = array(); |
| 632 | $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support); |
| 633 | if ($read) { |
| 634 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
| 635 | if (trim($regs[1])) { |
| 636 | $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY'); |
| 637 | } |
| 638 | } |
| 639 | } else { |
| 640 | echo "ERROR Yeah I know, not a very usefull errormessage (id = $id, mailbox = $mailbox sqimap_get_message)"; |
| 641 | exit; |
| 642 | } |
| 643 | $bodystructure = implode('',$read); |
| 644 | $msg = mime_structure($bodystructure,$flags); |
| 645 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support); |
| 646 | $msg->addRFC822Header($read); |
| 647 | $msg->id = $id; |
| 648 | $msg->mailbox = $mailbox; |
| 649 | return $msg; |
| 650 | } |
| 651 | |
| 652 | /* Wrapper function that reformats the header information. */ |
| 653 | function sqimap_get_message_header ($imap_stream, $id, $mailbox) { |
| 654 | global $uid_support; |
| 655 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, $uid_support); |
| 656 | $header = sqimap_get_header($imap_stream, $read); |
| 657 | $header->id = $id; |
| 658 | $header->mailbox = $mailbox; |
| 659 | return $header; |
| 660 | } |
| 661 | |
| 662 | /* Wrapper function that reformats the entity header information. */ |
| 663 | function sqimap_get_ent_header ($imap_stream, $id, $mailbox, $ent) { |
| 664 | global $uid_support; |
| 665 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[$ent.HEADER]", true, $response, $message, $uid_support); |
| 666 | $header = sqimap_get_header($imap_stream, $read); |
| 667 | $header->id = $id; |
| 668 | $header->mailbox = $mailbox; |
| 669 | return $header; |
| 670 | } |
| 671 | |
| 672 | |
| 673 | /* Wrapper function that returns entity headers for use by decodeMime */ |
| 674 | /* |
| 675 | function sqimap_get_entity_header ($imap_stream, &$read, &$type0, &$type1, &$bound, &$encoding, &$charset, &$filename) { |
| 676 | $header = sqimap_get_header($imap_stream, $read); |
| 677 | $type0 = $header["TYPE0"]; |
| 678 | $type1 = $header["TYPE1"]; |
| 679 | $bound = $header["BOUNDARY"]; |
| 680 | $encoding = $header["ENCODING"]; |
| 681 | $charset = $header["CHARSET"]; |
| 682 | $filename = $header["FILENAME"]; |
| 683 | } |
| 684 | |
| 685 | /* function to get the mime headers */ |
| 686 | function sqimap_get_mime_ent_header ($imap_stream, $id, $mailbox, $ent) { |
| 687 | global $uid_support; |
| 688 | $read = sqimap_run_command ($imap_stream, "FETCH $id:$id BODY[$ent.MIME]", true, $response, $message, $uid_support); |
| 689 | $header = sqimap_get_header($imap_stream, $read); |
| 690 | $header->id = $id; |
| 691 | $header->mailbox = $mailbox; |
| 692 | return $header; |
| 693 | } |
| 694 | |
| 695 | /* Returns the body of a message. */ |
| 696 | function sqimap_get_message_body ($imap_stream, &$header) { |
| 697 | // return decodeMime($imap_stream, $header->id); |
| 698 | } |
| 699 | |
| 700 | ?> |