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