| 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 | * @version $Id$ |
| 13 | * @package squirrelmail |
| 14 | * @subpackage imap |
| 15 | */ |
| 16 | |
| 17 | /** |
| 18 | * Copies specified messages to specified folder |
| 19 | * @param int $imap_stream The resource ID for the IMAP connection |
| 20 | * @param string $start Beginning of range to copy |
| 21 | * @param string $end End of the range to copy |
| 22 | * @param string $mailbox Which box to copy to |
| 23 | * @deprecated This function is obsolete and should not be used |
| 24 | */ |
| 25 | function sqimap_messages_copy ($imap_stream, $start, $end, $mailbox) { |
| 26 | $read = sqimap_run_command ($imap_stream, "COPY $start:$end " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
| 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 | $msgs_id = sqimap_message_list_squisher($id); |
| 38 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * move a range of messages ($id) to another mailbox. Deletes the originals. |
| 43 | * @param int $imap_stream The resource ID for the IMAP socket |
| 44 | * @param string $id The list of messages to move |
| 45 | * @param string $mailbox The destination to move to |
| 46 | * @return void |
| 47 | */ |
| 48 | function sqimap_msgs_list_move ($imap_stream, $id, $mailbox) { |
| 49 | $msgs_id = sqimap_message_list_squisher($id); |
| 50 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($mailbox), true, $response, $message, TRUE); |
| 51 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response,$message, TRUE); |
| 52 | } |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Deletes specified messages and moves them to trash if possible |
| 57 | * @deprecated This function is obsolete and should no longer be used |
| 58 | * @param int $imap_steam The resource ID for the IMAP connection |
| 59 | * @param string $start Start of range |
| 60 | * @param string $end End of range |
| 61 | * @param string $mailbox Mailbox messages are being deleted from |
| 62 | * @return void |
| 63 | */ |
| 64 | function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox, $bypass_trash=false) { |
| 65 | global $move_to_trash, $trash_folder, $auto_expunge; |
| 66 | |
| 67 | if (($move_to_trash == true) && ($bypass_trash != true) && |
| 68 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder))) { |
| 69 | sqimap_messages_copy ($imap_stream, $start, $end, $trash_folder); |
| 70 | } |
| 71 | sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true); |
| 72 | } |
| 73 | |
| 74 | function sqimap_msgs_list_delete ($imap_stream, $mailbox, $id, $bypass_trash=false) { |
| 75 | global $move_to_trash, $trash_folder; |
| 76 | $msgs_id = sqimap_message_list_squisher($id); |
| 77 | if (($move_to_trash == true) && ($bypass_trash != true) && |
| 78 | (sqimap_mailbox_exists($imap_stream, $trash_folder) && ($mailbox != $trash_folder)) ) { |
| 79 | $read = sqimap_run_command ($imap_stream, "COPY $msgs_id " . sqimap_encode_mailbox_name($trash_folder), true, $response, $message, TRUE); |
| 80 | } |
| 81 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, TRUE); |
| 82 | } |
| 83 | |
| 84 | |
| 85 | /** |
| 86 | * Sets the specified messages with specified flag |
| 87 | */ |
| 88 | function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) { |
| 89 | $read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, TRUE); |
| 90 | } |
| 91 | |
| 92 | function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) { |
| 93 | $msgs_id = sqimap_message_list_squisher($id); |
| 94 | $set_string = ($set ? '+' : '-'); |
| 95 | $read = sqimap_run_command ($imap_stream, "STORE $msgs_id ".$set_string."FLAGS ($flag)", $handle_errors, $response, $message, TRUE); |
| 96 | } |
| 97 | |
| 98 | /** @deprecated */ |
| 99 | function sqimap_get_small_header ($imap_stream, $id, $sent) { |
| 100 | $res = sqimap_get_small_header_list($imap_stream, $id, $sent); |
| 101 | return $res[0]; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Sort the message list and crunch to be as small as possible |
| 106 | * (overflow could happen, so make it small if possible) |
| 107 | */ |
| 108 | function sqimap_message_list_squisher($messages_array) { |
| 109 | if( !is_array( $messages_array ) ) { |
| 110 | return $messages_array; |
| 111 | } |
| 112 | |
| 113 | sort($messages_array, SORT_NUMERIC); |
| 114 | $msgs_str = ''; |
| 115 | while ($messages_array) { |
| 116 | $start = array_shift($messages_array); |
| 117 | $end = $start; |
| 118 | while (isset($messages_array[0]) && $messages_array[0] == $end + 1) { |
| 119 | $end = array_shift($messages_array); |
| 120 | } |
| 121 | if ($msgs_str != '') { |
| 122 | $msgs_str .= ','; |
| 123 | } |
| 124 | $msgs_str .= $start; |
| 125 | if ($start != $end) { |
| 126 | $msgs_str .= ':' . $end; |
| 127 | } |
| 128 | } |
| 129 | return $msgs_str; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Retrieves an array with a sorted uid list. Sorting is done on the imap server |
| 134 | * |
| 135 | * @param resource $imap_stream IMAP socket connection |
| 136 | * @param string $sSortField Field to sort on |
| 137 | * @param bool $reverse Reverse order search |
| 138 | * @return array $id sorted uid list |
| 139 | */ |
| 140 | function sqimap_get_sort_order ($imap_stream, $sSortField,$reverse) { |
| 141 | global $default_charset, |
| 142 | $sent_folder; |
| 143 | |
| 144 | $id = array(); |
| 145 | $sort_test = array(); |
| 146 | $sort_query = ''; |
| 147 | |
| 148 | if ($sSortField) { |
| 149 | if ($reverse) { |
| 150 | $sSortField = 'REVERSE '.$sSortField; |
| 151 | } |
| 152 | $query = "SORT ($sSortField) ".strtoupper($default_charset).' ALL'; |
| 153 | $sort_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
| 154 | } |
| 155 | if (isset($sort_test[0])) { |
| 156 | for ($i=0,$iCnt=count($sort_test);$i<$iCnt;++$i) { |
| 157 | if (preg_match("/^\* SORT (.+)$/", $sort_test[$i], $regs)) { |
| 158 | $id = preg_split("/ /", trim($regs[1])); |
| 159 | break; |
| 160 | } |
| 161 | } |
| 162 | } |
| 163 | if (!preg_match("/OK/", $response)) { |
| 164 | return false; |
| 165 | } else { |
| 166 | return $id; |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Retrieves an array with a sorted uid list. Sorting is done by SquirrelMail |
| 172 | * |
| 173 | * @param resource $imap_stream IMAP socket connection |
| 174 | * @param string $sSortField Field to sort on |
| 175 | * @param bool $reverse Reverse order search |
| 176 | * @return array $aUid sorted uid list |
| 177 | */ |
| 178 | function get_squirrel_sort ($imap_stream, $sSortField, $reverse = false) { |
| 179 | if ($sSortField != 'RFC822.SIZE' && $sSortField != 'INTERNALDATE') { |
| 180 | $msgs = sqimap_get_small_header_list($imap_stream, false, '*', |
| 181 | array($sSortField), array()); |
| 182 | } else { |
| 183 | $msgs = sqimap_get_small_header_list($imap_stream, false, '*', |
| 184 | array(), array($sSortField)); |
| 185 | } |
| 186 | $aUid = array(); |
| 187 | $walk = false; |
| 188 | switch ($sSortField) { |
| 189 | // natcasesort section |
| 190 | case 'FROM': |
| 191 | case 'TO': |
| 192 | case 'CC': |
| 193 | if(!$walk) { |
| 194 | array_walk($msgs, create_function('&$v,&$k,$f', |
| 195 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
| 196 | $addr = parseAddress($v[$f]); |
| 197 | $v[$f] = ($addr[0][1]) ? decodeHeader($addr[0][1]):$addr[0][0];'),$sSortField); |
| 198 | $walk = true; |
| 199 | } |
| 200 | // nobreak |
| 201 | case 'SUBJECT': |
| 202 | if(!$walk) { |
| 203 | array_walk($msgs, create_function('&$v,&$k,$f', |
| 204 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
| 205 | $v[$f] = strtolower(decodeHeader(trim($v[$f]))); |
| 206 | $v[$f] = (preg_match("/^(vedr|sv|re|aw|\[\w\]):\s*(.*)$/si", $v[$f], $matches)) ? |
| 207 | $matches[2] : $v[$f];'),$sSortField); |
| 208 | $walk = true; |
| 209 | } |
| 210 | foreach ($msgs as $item) { |
| 211 | $aUid[$item['ID']] = $item[$sSortField]; |
| 212 | } |
| 213 | natcasesort($aUid); |
| 214 | $aUid = array_keys($aUid); |
| 215 | if ($reverse) { |
| 216 | $aUid = array_reverse($aUid); |
| 217 | } |
| 218 | break; |
| 219 | // \natcasesort section |
| 220 | // sort_numeric section |
| 221 | case 'DATE': |
| 222 | case 'INTERNALDATE': |
| 223 | if(!$walk) { |
| 224 | array_walk($msgs, create_function('&$v,$k,$f', |
| 225 | '$v[$f] = (isset($v[$f])) ? $v[$f] : ""; |
| 226 | $v[$f] = getTimeStamp(explode(" ",$v[$f]));'),$sSortField); |
| 227 | $walk = true; |
| 228 | } |
| 229 | // nobreak; |
| 230 | case 'RFC822.SIZE': |
| 231 | if(!$walk) { |
| 232 | // redefine $sSortField to maintain the same namespace between |
| 233 | // server-side sorting and squirrelmail sorting |
| 234 | $sSortField = 'SIZE'; |
| 235 | } |
| 236 | foreach ($msgs as $item) { |
| 237 | $aUid[$item['ID']] = (isset($item[$sSortField])) ? $item[$sSortField] : 0; |
| 238 | } |
| 239 | if ($reverse) { |
| 240 | arsort($aUid,SORT_NUMERIC); |
| 241 | } else { |
| 242 | asort($aUid, SORT_NUMERIC); |
| 243 | } |
| 244 | $aUid = array_keys($aUid); |
| 245 | break; |
| 246 | // \sort_numeric section |
| 247 | case 'UID': |
| 248 | $aUid = array_reverse($msgs); |
| 249 | break; |
| 250 | } |
| 251 | return $aUid; |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Returns an indent array for printMessageinfo() |
| 256 | * This represents the amount of indent needed (value), |
| 257 | * for this message number (key) |
| 258 | */ |
| 259 | |
| 260 | /* |
| 261 | * Notes for future work: |
| 262 | * indent_array should contain: indent_level, parent and flags, |
| 263 | * sibling notes .. |
| 264 | * To achieve that we need to define the following flags: |
| 265 | * 0: hasnochildren |
| 266 | * 1: haschildren |
| 267 | * 2: is first |
| 268 | * 4: is last |
| 269 | * a node has sibling nodes if it's not the last node |
| 270 | * a node has no sibling nodes if it's the last node |
| 271 | * By using binary comparations we can store the flag in one var |
| 272 | * |
| 273 | * example: |
| 274 | * -1 par = 0, level = 0, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
| 275 | * \-2 par = 1, level = 1, flag = 0 + 2 = 2 (hasnochildren, isfirst) |
| 276 | * |-3 par = 1, level = 1, flag = 1 + 4 = 5 (haschildren, islast) |
| 277 | * \-4 par = 3, level = 2, flag = 1 + 2 + 4 = 7 (haschildren, isfirst, islast) |
| 278 | * \-5 par = 4, level = 3, flag = 0 + 2 + 4 = 6 (hasnochildren, isfirst, islast) |
| 279 | */ |
| 280 | function get_parent_level ($thread_new) { |
| 281 | $parent = ''; |
| 282 | $child = ''; |
| 283 | $cutoff = 0; |
| 284 | |
| 285 | /* |
| 286 | * loop through the threads and take unwanted characters out |
| 287 | * of the thread string then chop it up |
| 288 | */ |
| 289 | for ($i=0;$i<count($thread_new);$i++) { |
| 290 | $thread_new[$i] = preg_replace("/\s\(/", "(", $thread_new[$i]); |
| 291 | $thread_new[$i] = preg_replace("/(\d+)/", "$1|", $thread_new[$i]); |
| 292 | $thread_new[$i] = preg_split("/\|/", $thread_new[$i], -1, PREG_SPLIT_NO_EMPTY); |
| 293 | } |
| 294 | $indent_array = array(); |
| 295 | if (!$thread_new) { |
| 296 | $thread_new = array(); |
| 297 | } |
| 298 | /* looping through the parts of one message thread */ |
| 299 | |
| 300 | for ($i=0;$i<count($thread_new);$i++) { |
| 301 | /* first grab the parent, it does not indent */ |
| 302 | |
| 303 | if (isset($thread_new[$i][0])) { |
| 304 | if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) { |
| 305 | $parent = $regs[1]; |
| 306 | } |
| 307 | } |
| 308 | $indent_array[$parent] = 0; |
| 309 | |
| 310 | /* |
| 311 | * now the children, checking each thread portion for |
| 312 | * ),(, and space, adjusting the level and space values |
| 313 | * to get the indent level |
| 314 | */ |
| 315 | $level = 0; |
| 316 | $spaces = array(); |
| 317 | $spaces_total = 0; |
| 318 | $indent = 0; |
| 319 | $fake = FALSE; |
| 320 | for ($k=1,$iCnt=count($thread_new[$i])-1;$k<$iCnt;++$k) { |
| 321 | $chars = count_chars($thread_new[$i][$k], 1); |
| 322 | if (isset($chars['40'])) { /* testing for ( */ |
| 323 | $level += $chars['40']; |
| 324 | } |
| 325 | if (isset($chars['41'])) { /* testing for ) */ |
| 326 | $level -= $chars['41']; |
| 327 | $spaces[$level] = 0; |
| 328 | /* if we were faking lets stop, this portion |
| 329 | * of the thread is over |
| 330 | */ |
| 331 | if ($level == $cutoff) { |
| 332 | $fake = FALSE; |
| 333 | } |
| 334 | } |
| 335 | if (isset($chars['32'])) { /* testing for space */ |
| 336 | if (!isset($spaces[$level])) { |
| 337 | $spaces[$level] = 0; |
| 338 | } |
| 339 | $spaces[$level] += $chars['32']; |
| 340 | } |
| 341 | for ($x=0;$x<=$level;$x++) { |
| 342 | if (isset($spaces[$x])) { |
| 343 | $spaces_total += $spaces[$x]; |
| 344 | } |
| 345 | } |
| 346 | $indent = $level + $spaces_total; |
| 347 | /* must have run into a message that broke the thread |
| 348 | * so we are adjusting for that portion |
| 349 | */ |
| 350 | if ($fake == TRUE) { |
| 351 | $indent = $indent +1; |
| 352 | } |
| 353 | if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) { |
| 354 | $child = $regs[1]; |
| 355 | } |
| 356 | /* the thread must be broken if $indent == 0 |
| 357 | * so indent the message once and start faking it |
| 358 | */ |
| 359 | if ($indent == 0) { |
| 360 | $indent = 1; |
| 361 | $fake = TRUE; |
| 362 | $cutoff = $level; |
| 363 | } |
| 364 | /* dont need abs but if indent was negative |
| 365 | * errors would occur |
| 366 | */ |
| 367 | $indent_array[$child] = ($indent < 0) ? 0 : $indent; |
| 368 | $spaces_total = 0; |
| 369 | } |
| 370 | } |
| 371 | return $indent_array; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /** |
| 376 | * Returns an array with each element as a string representing one |
| 377 | * message-thread as returned by the IMAP server. |
| 378 | */ |
| 379 | function get_thread_sort ($imap_stream) { |
| 380 | global $thread_new, $sort_by_ref, $default_charset, $server_sort_array, $indent_array; |
| 381 | if (sqsession_is_registered('thread_new')) { |
| 382 | sqsession_unregister('thread_new'); |
| 383 | } |
| 384 | if (sqsession_is_registered('indent_array')) { |
| 385 | sqsession_unregister('indent_array'); |
| 386 | } |
| 387 | if (sqsession_is_registered('server_sort_array')) { |
| 388 | sqsession_unregister('server_sort_array'); |
| 389 | } |
| 390 | $thread_temp = array (); |
| 391 | if ($sort_by_ref == 1) { |
| 392 | $sort_type = 'REFERENCES'; |
| 393 | } else { |
| 394 | $sort_type = 'ORDEREDSUBJECT'; |
| 395 | } |
| 396 | $query = "THREAD $sort_type ".strtoupper($default_charset)." ALL"; |
| 397 | $thread_test = sqimap_run_command ($imap_stream, $query, true, $response, $message, TRUE); |
| 398 | if (isset($thread_test[0])) { |
| 399 | for ($i=0,$iCnt=count($thread_test);$i<$iCnt;++$i) { |
| 400 | if (preg_match("/^\* THREAD (.+)$/", $thread_test[$i], $regs)) { |
| 401 | $thread_list = trim($regs[1]); |
| 402 | break; |
| 403 | } |
| 404 | } |
| 405 | } else { |
| 406 | $thread_list = ""; |
| 407 | } |
| 408 | if (!preg_match("/OK/", $response)) { |
| 409 | $server_sort_array = 'no'; |
| 410 | return $server_sort_array; |
| 411 | } |
| 412 | if (isset($thread_list)) { |
| 413 | $thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
| 414 | } |
| 415 | |
| 416 | $char_count = count($thread_temp); |
| 417 | $counter = 0; |
| 418 | $thread_new = array(); |
| 419 | $k = 0; |
| 420 | $thread_new[0] = ""; |
| 421 | /* |
| 422 | * parse the thread response into separate threads |
| 423 | * |
| 424 | * example: |
| 425 | * [0] => (540) |
| 426 | * [1] => (1386) |
| 427 | * [2] => (1599 759 959 37) |
| 428 | * [3] => (492 1787) |
| 429 | * [4] => ((933)(1891)) |
| 430 | * [5] => (1030 (1497)(845)(1637)) |
| 431 | */ |
| 432 | for ($i=0,$iCnt=count($thread_temp);$i<$iCnt;$i++) { |
| 433 | if ($thread_temp[$i] != ')' && $thread_temp[$i] != '(') { |
| 434 | $thread_new[$k] = $thread_new[$k] . $thread_temp[$i]; |
| 435 | } elseif ($thread_temp[$i] == '(') { |
| 436 | $thread_new[$k] .= $thread_temp[$i]; |
| 437 | $counter++; |
| 438 | } elseif ($thread_temp[$i] == ')') { |
| 439 | if ($counter > 1) { |
| 440 | $thread_new[$k] .= $thread_temp[$i]; |
| 441 | $counter = $counter - 1; |
| 442 | } else { |
| 443 | $thread_new[$k] .= $thread_temp[$i]; |
| 444 | $k++; |
| 445 | $thread_new[$k] = ""; |
| 446 | $counter = $counter - 1; |
| 447 | } |
| 448 | } |
| 449 | } |
| 450 | sqsession_register($thread_new, 'thread_new'); |
| 451 | $thread_new = array_reverse($thread_new); |
| 452 | /* place the threads after each other in one string */ |
| 453 | $thread_list = implode(" ", $thread_new); |
| 454 | $thread_list = str_replace("(", " ", $thread_list); |
| 455 | $thread_list = str_replace(")", " ", $thread_list); |
| 456 | $thread_list = preg_split("/\s/", $thread_list, -1, PREG_SPLIT_NO_EMPTY); |
| 457 | $server_sort_array = $thread_list; |
| 458 | |
| 459 | $indent_array = get_parent_level ($thread_new); |
| 460 | sqsession_register($indent_array, 'indent_array'); |
| 461 | |
| 462 | sqsession_register($server_sort_array, 'server_sort_array'); |
| 463 | return $thread_list; |
| 464 | } |
| 465 | |
| 466 | |
| 467 | function elapsedTime($start) { |
| 468 | $stop = gettimeofday(); |
| 469 | $timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec']; |
| 470 | return $timepassed; |
| 471 | } |
| 472 | |
| 473 | // only used in sqimap_get_small_header_list |
| 474 | function parseString($read,&$i) { |
| 475 | $char = $read{$i}; |
| 476 | $s = ''; |
| 477 | if ($char == '"') { |
| 478 | $iPos = ++$i; |
| 479 | while (true) { |
| 480 | $iPos = strpos($read,'"',$iPos); |
| 481 | if (!$iPos) break; |
| 482 | if ($iPos && $read{$iPos -1} != '\\') { |
| 483 | $s = substr($read,$i,($iPos-$i)); |
| 484 | $i = $iPos; |
| 485 | break; |
| 486 | } |
| 487 | $iPos++; |
| 488 | if ($iPos > strlen($read)) { |
| 489 | break; |
| 490 | } |
| 491 | } |
| 492 | } else if ($char == '{') { |
| 493 | $lit_cnt = ''; |
| 494 | ++$i; |
| 495 | $iPos = strpos($read,'}',$i); |
| 496 | if ($iPos) { |
| 497 | $lit_cnt = substr($read, $i, $iPos - $i); |
| 498 | $i += strlen($lit_cnt) + 3; /* skip } + \r + \n */ |
| 499 | /* Now read the literal */ |
| 500 | $s = ($lit_cnt ? substr($read,$i,$lit_cnt): ''); |
| 501 | $i += $lit_cnt; |
| 502 | /* temp bugfix (SM 1.5 will have a working clean version) |
| 503 | too much work to implement that version right now */ |
| 504 | --$i; |
| 505 | } else { /* should never happen */ |
| 506 | $i += 3; /* } + \r + \n */ |
| 507 | $s = ''; |
| 508 | } |
| 509 | } else { |
| 510 | return false; |
| 511 | } |
| 512 | ++$i; |
| 513 | return $s; |
| 514 | } |
| 515 | |
| 516 | // only used in sqimap_get_small_header_list |
| 517 | function parseArray($read,&$i) { |
| 518 | $i = strpos($read,'(',$i); |
| 519 | $i_pos = strpos($read,')',$i); |
| 520 | $s = substr($read,$i+1,$i_pos - $i -1); |
| 521 | $a = explode(' ',$s); |
| 522 | if ($i_pos) { |
| 523 | $i = $i_pos+1; |
| 524 | return $a; |
| 525 | } else { |
| 526 | return false; |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | function sqimap_get_small_header_list ($imap_stream, $msg_list, $show_num=false, |
| 531 | $aHeaderFields = array('Date', 'To', 'Cc', 'From', 'Subject', 'X-Priority', 'Content-Type'), |
| 532 | $aFetchItems = array('FLAGS', 'RFC822.SIZE', 'INTERNALDATE')) { |
| 533 | |
| 534 | $messages = array(); |
| 535 | $read_list = array(); |
| 536 | |
| 537 | if (array_search('UID',$aFetchItems,true) !== false) { |
| 538 | $bUidFetch = false; |
| 539 | } else { |
| 540 | $bUidFetch = true; |
| 541 | } |
| 542 | |
| 543 | /* Get the small headers for each message in $msg_list */ |
| 544 | if ($show_num != '999999' && $show_num != '*' ) { |
| 545 | $msgs_str = sqimap_message_list_squisher($msg_list); |
| 546 | /* |
| 547 | * We need to return the data in the same order as the caller supplied |
| 548 | * in $msg_list, but IMAP servers are free to return responses in |
| 549 | * whatever order they wish... So we need to re-sort manually |
| 550 | */ |
| 551 | if ($bUidFetch) { |
| 552 | for ($i = 0; $i < sizeof($msg_list); $i++) { |
| 553 | $messages["$msg_list[$i]"] = array(); |
| 554 | } |
| 555 | } |
| 556 | } else { |
| 557 | $msgs_str = '1:*'; |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | /* |
| 563 | * Create the query |
| 564 | */ |
| 565 | |
| 566 | $sFetchItems = ''; |
| 567 | $query = "FETCH $msgs_str ("; |
| 568 | if (count($aFetchItems)) { |
| 569 | $sFetchItems = implode(' ',$aFetchItems); |
| 570 | } |
| 571 | if (count($aHeaderFields)) { |
| 572 | $sHeaderFields = implode(' ',$aHeaderFields); |
| 573 | $sFetchItems .= ' BODY.PEEK[HEADER.FIELDS ('.$sHeaderFields.')]'; |
| 574 | } |
| 575 | $query .= trim($sFetchItems) . ')'; |
| 576 | |
| 577 | $read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $bUidFetch); |
| 578 | $i = 0; |
| 579 | |
| 580 | foreach ($read_list as $r) { |
| 581 | // use unset because we do isset below |
| 582 | $read = implode('',$r); |
| 583 | |
| 584 | /* |
| 585 | * #id<space>FETCH<space>( |
| 586 | */ |
| 587 | |
| 588 | /* extract the message id */ |
| 589 | $i_space = strpos($read,' ',2); |
| 590 | $id = substr($read,2,$i_space-2); |
| 591 | $fetch = substr($read,$i_space+1,5); |
| 592 | if (!is_numeric($id) && $fetch !== 'FETCH') { |
| 593 | $msg['ERROR'] = $read; // htmlspecialchars should be done just before display. this is backend code |
| 594 | break; |
| 595 | } |
| 596 | $i = strpos($read,'(',$i_space+5); |
| 597 | $read = substr($read,$i+1); |
| 598 | $i_len = strlen($read); |
| 599 | $i = 0; |
| 600 | while ($i < $i_len && $i !== false) { |
| 601 | /* get argument */ |
| 602 | $read = trim(substr($read,$i)); |
| 603 | $i_len = strlen($read); |
| 604 | $i = strpos($read,' '); |
| 605 | $arg = substr($read,0,$i); |
| 606 | ++$i; |
| 607 | switch ($arg) |
| 608 | { |
| 609 | case 'UID': |
| 610 | $i_pos = strpos($read,' ',$i); |
| 611 | if (!$i_pos) { |
| 612 | $i_pos = strpos($read,')',$i); |
| 613 | } |
| 614 | if ($i_pos) { |
| 615 | $unique_id = substr($read,$i,$i_pos-$i); |
| 616 | $i = $i_pos+1; |
| 617 | } else { |
| 618 | break 3; |
| 619 | } |
| 620 | break; |
| 621 | case 'FLAGS': |
| 622 | $flags = parseArray($read,$i); |
| 623 | if (!$flags) break 3; |
| 624 | $aFlags = array(); |
| 625 | foreach ($flags as $flag) { |
| 626 | $flag = strtolower($flag); |
| 627 | $aFlags[$flag] = true; |
| 628 | } |
| 629 | $msg['FLAGS'] = $aFlags; |
| 630 | break; |
| 631 | case 'RFC822.SIZE': |
| 632 | $i_pos = strpos($read,' ',$i); |
| 633 | if (!$i_pos) { |
| 634 | $i_pos = strpos($read,')',$i); |
| 635 | } |
| 636 | if ($i_pos) { |
| 637 | $msg['SIZE'] = substr($read,$i,$i_pos-$i); |
| 638 | $i = $i_pos+1; |
| 639 | } else { |
| 640 | break 3; |
| 641 | } |
| 642 | |
| 643 | break; |
| 644 | case 'ENVELOPE': |
| 645 | break; // to be implemented, moving imap code out of the nessages class |
| 646 | sqimap_parse_address($read,$i,$msg); |
| 647 | break; // to be implemented, moving imap code out of the nessages class |
| 648 | case 'BODYSTRUCTURE': |
| 649 | break; |
| 650 | case 'INTERNALDATE': |
| 651 | $msg['INTERNALDATE'] = str_replace(' ', ' ',parseString($read,$i)); |
| 652 | break; |
| 653 | case 'BODY.PEEK[HEADER.FIELDS': |
| 654 | case 'BODY[HEADER.FIELDS': |
| 655 | $i = strpos($read,'{',$i); |
| 656 | $header = parseString($read,$i); |
| 657 | if ($header === false) break 2; |
| 658 | /* First we replace all \r\n by \n, and unfold the header */ |
| 659 | $hdr = trim(str_replace(array("\r\n", "\n\t", "\n "),array("\n", ' ', ' '), $header)); |
| 660 | /* Now we can make a new header array with */ |
| 661 | /* each element representing a headerline */ |
| 662 | $hdr = explode("\n" , $hdr); |
| 663 | $aReceived = array(); |
| 664 | foreach ($hdr as $line) { |
| 665 | $pos = strpos($line, ':'); |
| 666 | if ($pos > 0) { |
| 667 | $field = strtolower(substr($line, 0, $pos)); |
| 668 | if (!strstr($field,' ')) { /* valid field */ |
| 669 | $value = trim(substr($line, $pos+1)); |
| 670 | switch($field) |
| 671 | { |
| 672 | case 'to': $msg['TO'] = $value; break; |
| 673 | case 'cc': $msg['CC'] = $value; break; |
| 674 | case 'from': $msg['FROM'] = $value; break; |
| 675 | case 'date': |
| 676 | $msg['DATE'] = str_replace(' ', ' ', $value); |
| 677 | break; |
| 678 | case 'x-priority': $msg['PRIORITY'] = $value; break; |
| 679 | case 'subject': $msg['SUBJECT'] = $value; break; |
| 680 | case 'content-type': |
| 681 | $type = $value; |
| 682 | if ($pos = strpos($type, ";")) { |
| 683 | $type = substr($type, 0, $pos); |
| 684 | } |
| 685 | $type = explode("/", $type); |
| 686 | if(!is_array($type)) { |
| 687 | $msg['TYPE0'] = 'text'; |
| 688 | $msg['TYPE1'] = 'plain'; |
| 689 | } |
| 690 | break; |
| 691 | case 'received': |
| 692 | $aReceived[] = $value; |
| 693 | break; |
| 694 | default: break; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | if (count($aReceived)) { |
| 700 | $msg['RECEIVED'] = $aReceived; |
| 701 | } |
| 702 | break; |
| 703 | default: |
| 704 | ++$i; |
| 705 | break; |
| 706 | } |
| 707 | } |
| 708 | $msgi ="$unique_id"; |
| 709 | $msg['ID'] = $unique_id; |
| 710 | |
| 711 | $messages[$msgi] = $msg; |
| 712 | ++$msgi; |
| 713 | } |
| 714 | array_reverse($messages); |
| 715 | return $messages; |
| 716 | } |
| 717 | |
| 718 | function sqimap_parse_envelope($read, &$i, &$msg) { |
| 719 | $arg_no = 0; |
| 720 | $arg_a = array(); |
| 721 | ++$i; |
| 722 | for ($cnt = strlen($read); ($i < $cnt) && ($read{$i} != ')'); ++$i) { |
| 723 | $char = strtoupper($read{$i}); |
| 724 | switch ($char) { |
| 725 | case '{': |
| 726 | case '"': |
| 727 | $arg_a[] = parseString($read,$i); |
| 728 | ++$arg_no; |
| 729 | break; |
| 730 | case 'N': |
| 731 | /* probably NIL argument */ |
| 732 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
| 733 | $arg_a[] = ''; |
| 734 | ++$arg_no; |
| 735 | $i += 2; |
| 736 | } |
| 737 | break; |
| 738 | case '(': |
| 739 | /* Address structure (with group support) |
| 740 | * Note: Group support is useless on SMTP connections |
| 741 | * because the protocol doesn't support it |
| 742 | */ |
| 743 | $addr_a = array(); |
| 744 | $group = ''; |
| 745 | $a=0; |
| 746 | for (; $i < $cnt && $read{$i} != ')'; ++$i) { |
| 747 | if ($read{$i} == '(') { |
| 748 | $addr = sqimap_parse_address($read, $i); |
| 749 | if (($addr[3] == '') && ($addr[2] != '')) { |
| 750 | /* start of group */ |
| 751 | $group = $addr[2]; |
| 752 | $group_addr = $addr; |
| 753 | $j = $a; |
| 754 | } else if ($group && ($addr[3] == '') && ($addr[2] == '')) { |
| 755 | /* end group */ |
| 756 | if ($a == ($j+1)) { /* no group members */ |
| 757 | $group_addr[4] = $group; |
| 758 | $group_addr[2] = ''; |
| 759 | $group_addr[0] = "$group: Undisclosed recipients;"; |
| 760 | $addr_a[] = $group_addr; |
| 761 | $group =''; |
| 762 | } |
| 763 | } else { |
| 764 | $addr[4] = $group; |
| 765 | $addr_a[] = $addr; |
| 766 | } |
| 767 | ++$a; |
| 768 | } |
| 769 | } |
| 770 | $arg_a[] = $addr_a; |
| 771 | break; |
| 772 | default: break; |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | if (count($arg_a) > 9) { |
| 777 | $d = strtr($arg_a[0], array(' ' => ' ')); |
| 778 | $d = explode(' ', $d); |
| 779 | if (!$arg_a[1]) $arg_1[1] = ''; |
| 780 | $msg['DATE'] = $d; /* argument 1: date */ |
| 781 | $msg['SUBJECT'] = $arg_a[1]; /* argument 2: subject */ |
| 782 | $msg['FROM'] = is_array($arg_a[2]) ? $arg_a[2][0] : ''; /* argument 3: from */ |
| 783 | $msg['SENDER'] = is_array($arg_a[3]) ? $arg_a[3][0] : ''; /* argument 4: sender */ |
| 784 | $msg['REPLY-TO'] = is_array($arg_a[4]) ? $arg_a[4][0] : ''; /* argument 5: reply-to */ |
| 785 | $msg['TO'] = $arg_a[5]; /* argument 6: to */ |
| 786 | $msg['CC'] = $arg_a[6]; /* argument 7: cc */ |
| 787 | $msg['BCC'] = $arg_a[7]; /* argument 8: bcc */ |
| 788 | $msg['IN-REPLY-TO'] = $arg_a[8]; /* argument 9: in-reply-to */ |
| 789 | $msg['MESSAGE-ID'] = $arg_a[9]; /* argument 10: message-id */ |
| 790 | } |
| 791 | } |
| 792 | |
| 793 | function sqimap_parse_address($read, &$i) { |
| 794 | $arg_a = array(); |
| 795 | for (; $read{$i} != ')'; ++$i) { |
| 796 | $char = strtoupper($read{$i}); |
| 797 | switch ($char) { |
| 798 | case '{': |
| 799 | case '"': $arg_a[] = parseString($read,$i); break; |
| 800 | case 'n': |
| 801 | case 'N': |
| 802 | if (strtoupper(substr($read, $i, 3)) == 'NIL') { |
| 803 | $arg_a[] = ''; |
| 804 | $i += 2; |
| 805 | } |
| 806 | break; |
| 807 | default: break; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (count($arg_a) == 4) { |
| 812 | return $arg_a; |
| 813 | |
| 814 | // $adr = new AddressStructure(); |
| 815 | // $adr->personal = $arg_a[0]; |
| 816 | // $adr->adl = $arg_a[1]; |
| 817 | // $adr->mailbox = $arg_a[2]; |
| 818 | // $adr->host = $arg_a[3]; |
| 819 | } else { |
| 820 | $adr = ''; |
| 821 | } |
| 822 | return $adr; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Returns a message array with all the information about a message. |
| 827 | * See the documentation folder for more information about this array. |
| 828 | */ |
| 829 | function sqimap_get_message ($imap_stream, $id, $mailbox) { |
| 830 | // typecast to int to prohibit 1:* msgs sets |
| 831 | $id = (int) $id; |
| 832 | $flags = array(); |
| 833 | $read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, TRUE); |
| 834 | if ($read) { |
| 835 | if (preg_match('/.+FLAGS\s\((.*)\)\s/AUi',$read[0],$regs)) { |
| 836 | if (trim($regs[1])) { |
| 837 | $flags = preg_split('/ /', $regs[1],-1,'PREG_SPLIT_NI_EMPTY'); |
| 838 | } |
| 839 | } |
| 840 | } else { |
| 841 | /* the message was not found, maybe the mailbox was modified? */ |
| 842 | global $sort, $startMessage, $color; |
| 843 | |
| 844 | $errmessage = _("The server couldn't find the message you requested.") . |
| 845 | '<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)."); |
| 846 | /* this will include a link back to the message list */ |
| 847 | error_message($errmessage, $mailbox, $sort, (int) $startMessage, $color); |
| 848 | exit; |
| 849 | } |
| 850 | $bodystructure = implode('',$read); |
| 851 | $msg = mime_structure($bodystructure,$flags); |
| 852 | $read = sqimap_run_command ($imap_stream, "FETCH $id BODY[HEADER]", true, $response, $message, TRUE); |
| 853 | $rfc822_header = new Rfc822Header(); |
| 854 | $rfc822_header->parseHeader($read); |
| 855 | $msg->rfc822_header = $rfc822_header; |
| 856 | return $msg; |
| 857 | } |
| 858 | |
| 859 | ?> |