6
* Copyright (c) 1999-2003 The SquirrelMail Project Team
7
* Licensed under the GNU GPL. For full terms see the file COPYING.
9
* This implements functions that manipulate messages
10
* NOTE: Quite a few functions in this file are obsolete
12
* $Id: imap_messages.php,v 1.151 2004/01/30 06:59:45 ebullient Exp $
13
* @package squirrelmail
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
24
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, $uid_support);
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
36
function sqimap_msgs_list_copy ($imap_stream, $id, $mailbox) {
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);
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
49
function sqimap_msgs_list_move ($imap_stream, $id, $mailbox) {
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);
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
66
function sqimap_messages_delete ($imap_stream, $start, $end, $mailbox, $bypass_trash=false) {
67
global $move_to_trash, $trash_folder, $auto_expunge, $uid_support;
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);
73
sqimap_messages_flag ($imap_stream, $start, $end, "Deleted", true);
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);
83
$read = sqimap_run_command ($imap_stream, "STORE $msgs_id +FLAGS (\\Deleted)", true, $response, $message, $uid_support);
88
* Sets the specified messages with specified flag
90
function sqimap_messages_flag ($imap_stream, $start, $end, $flag, $handle_errors) {
92
$read = sqimap_run_command ($imap_stream, "STORE $start:$end +FLAGS (\\$flag)", $handle_errors, $response, $message, $uid_support);
95
function sqimap_toggle_flag($imap_stream, $id, $flag, $set, $handle_errors) {
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);
103
function sqimap_get_small_header ($imap_stream, $id, $sent) {
104
$res = sqimap_get_small_header_list($imap_stream, $id, $sent);
109
* Sort the message list and crunch to be as small as possible
110
* (overflow could happen, so make it small if possible)
112
function sqimap_message_list_squisher($messages_array) {
113
if( !is_array( $messages_array ) ) {
114
return $messages_array;
117
sort($messages_array, SORT_NUMERIC);
119
while ($messages_array) {
120
$start = array_shift($messages_array);
122
while (isset($messages_array[0]) && $messages_array[0] == $end + 1) {
123
$end = array_shift($messages_array);
125
if ($msgs_str != '') {
129
if ($start != $end) {
130
$msgs_str .= ':' . $end;
137
* Get sort order from server and return it as the $id array for mailbox_display.
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;
144
if (sqsession_is_registered('server_sort_array')) {
145
sqsession_unregister('server_sort_array');
150
$server_sort_array = array();
151
$sort_test = array();
156
if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
157
$uidnext = $mbxresponse['UIDNEXT']-1;
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]));
168
if (!preg_match("/OK/", $response)) {
169
$server_sort_array = 'no';
172
$qty = $mbxresponse['EXISTS'];
173
$server_sort_array = range(1, $qty);
175
$server_sort_array = array_reverse($server_sort_array);
176
sqsession_register($server_sort_array, 'server_sort_array');
177
return $server_sort_array;
180
$sort_on = array (0=> 'DATE',
186
if ($internal_date_sort == true) {
187
$sort_on[0] = 'ARRIVAL';
188
$sort_on[1] = 'ARRIVAL';
190
if ($sent_folder == $mailbox) {
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);
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]));
206
if ($sort == 0 || $sort == 2 || $sort == 4) {
207
$server_sort_array = array_reverse($server_sort_array);
209
if (!preg_match("/OK/", $response)) {
210
$server_sort_array = 'no';
212
sqsession_register($server_sort_array, 'server_sort_array');
213
return $server_sort_array;
217
* Get sort order from server if server does not have the SORT extension
218
* and return it as array for mailbox_display.
220
* @param resource $imap_stream
221
* @param array $mbxresponse response from a sqimap_mailbox_select
222
* @return array $php_sort_array
224
function sqimap_get_php_sort_order ($imap_stream, $mbxresponse) {
227
if (sqsession_is_registered('php_sort_array')) {
228
sqsession_unregister('php_sort_array');
231
$php_sort_array = array();
234
if (isset($mbxresponse['UIDNEXT']) && $mbxresponse['UIDNEXT']) {
235
$uidnext = $mbxresponse['UIDNEXT']-1;
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]));
250
if (!preg_match("/OK/", $response)) {
251
$php_sort_array = 'no';
254
$qty = $mbxresponse['EXISTS'];
255
$php_sort_array = range(1, $qty);
257
sqsession_register($php_sort_array, 'php_sort_array');
258
return $php_sort_array;
263
* Returns an indent array for printMessageinfo()
264
* This represents the amount of indent needed (value),
265
* for this message number (key)
267
function get_parent_level ($imap_stream) {
268
global $sort_by_ref, $default_charset, $thread_new;
273
/* loop through the threads and take unwanted characters out
274
of the thread string then chop it up
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);
281
$indent_array = array();
283
$thread_new = array();
285
/* looping through the parts of one message thread */
287
for ($i=0;$i<count($thread_new);$i++) {
288
/* first grab the parent, it does not indent */
290
if (isset($thread_new[$i][0])) {
291
if (preg_match("/(\d+)/", $thread_new[$i][0], $regs)) {
295
$indent_array[$parent] = 0;
297
/* now the children, checking each thread portion for
298
),(, and space, adjusting the level and space values
299
to get the indent level
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'];
311
if (isset($chars['41'])) { /* testing for ) */
312
$level = $level - $chars['41'];
314
/* if we were faking lets stop, this portion
315
of the thread is over
317
if ($level == $cutoff) {
321
if (isset($chars['32'])) { /* testing for space */
322
if (!isset($spaces[$level])) {
325
$spaces[$level] = $spaces[$level] + $chars['32'];
327
for ($x=0;$x<=$level;$x++) {
328
if (isset($spaces[$x])) {
329
$spaces_total = $spaces_total + $spaces[$x];
332
$indent = $level + $spaces_total;
333
/* must have run into a message that broke the thread
334
so we are adjusting for that portion
337
$indent = $indent +1;
339
if (preg_match("/(\d+)/", $thread_new[$i][$k], $regs)) {
342
/* the thread must be broken if $indent == 0
343
so indent the message once and start faking it
350
/* dont need abs but if indent was negative
353
$indent_array[$child] = abs($indent);
357
return $indent_array;
362
* Returns an array with each element as a string representing one
363
* message-thread as returned by the IMAP server.
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');
370
if (sqsession_is_registered('server_sort_array')) {
371
sqsession_unregister('server_sort_array');
373
$thread_temp = array ();
374
if ($sort_by_ref == 1) {
375
$sort_type = 'REFERENCES';
378
$sort_type = 'ORDEREDSUBJECT';
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]);
393
if (!preg_match("/OK/", $response)) {
394
$server_sort_array = 'no';
395
return $server_sort_array;
397
if (isset($thread_list)) {
398
$thread_temp = preg_split("//", $thread_list, -1, PREG_SPLIT_NO_EMPTY);
400
$char_count = count($thread_temp);
402
$thread_new = array();
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];
409
elseif ($thread_temp[$i] == '(') {
410
$thread_new[$k] .= $thread_temp[$i];
413
elseif ($thread_temp[$i] == ')') {
415
$thread_new[$k] .= $thread_temp[$i];
416
$counter = $counter - 1;
419
$thread_new[$k] .= $thread_temp[$i];
421
$thread_new[$k] = "";
422
$counter = $counter - 1;
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');
438
function elapsedTime($start) {
439
$stop = gettimeofday();
440
$timepassed = 1000000 * ($stop['sec'] - $start['sec']) + $stop['usec'] - $start['usec'];
444
// only used in sqimap_get_small_header_list
445
function parseString($read,&$i) {
451
$iPos = strpos($read,'"',$iPos);
453
if ($iPos && $read{$iPos -1} != '\\') {
454
$s = substr($read,$i,($iPos-$i));
459
if ($iPos > strlen($read)) {
463
} else if ($char == '{') {
466
$iPos = strpos($read,'}',$i);
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): '');
473
/* temp bugfix (SM 1.5 will have a working clean version)
474
too much work to implement that version right now */
476
} else { /* should never happen */
477
$i += 3; /* } + \r + \n */
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);
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);
512
$read_list = array();
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
519
for ($i = 0; $i < sizeof($msg_list); $i++) {
520
$messages["$msg_list[$i]"] = array();
523
$internaldate = getPref($data_dir, $username, 'internal_date_sort');
525
$query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE INTERNALDATE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
527
$query = "FETCH $msgs_str (FLAGS UID RFC822.SIZE BODY.PEEK[HEADER.FIELDS (Date To Cc From Subject X-Priority Content-Type)])";
529
$read_list = sqimap_run_command_list ($imap_stream, $query, true, $response, $message, $uid_support);
532
foreach ($read_list as $r) {
533
$subject = _("(no subject)");
534
$from = _("Unknown sender");
537
$cc = $to = $date = $type[0] = $type[1] = $inrepto = '';
538
$flag_seen = $flag_answered = $flag_deleted = $flag_flagged = false;
540
$read = implode('',$r);
543
* #id<space>FETCH<space>(
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.") .
555
_("Unknown response from IMAP server: ") . ' 1.' .
556
htmlspecialchars($read) . "</font><br>\n";
559
$i = strpos($read,'(',$i_space+5);
560
$read = substr($read,$i+1);
561
$i_len = strlen($read);
563
while ($i < $i_len && $i !== false) {
565
$read = trim(substr($read,$i));
566
$i_len = strlen($read);
567
$i = strpos($read,' ');
568
$arg = substr($read,0,$i);
573
$i_pos = strpos($read,' ',$i);
575
$i_pos = strpos($read,')',$i);
578
$unique_id = substr($read,$i,$i_pos-$i);
585
$flags = parseArray($read,$i);
586
if (!$flags) break 3;
587
foreach ($flags as $flag) {
588
$flag = strtolower($flag);
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;
600
$i_pos = strpos($read,' ',$i);
602
$i_pos = strpos($read,')',$i);
605
$size = substr($read,$i,$i_pos-$i);
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];
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, ':');
633
$field = strtolower(substr($line, 0, $pos));
634
if (!strstr($field,' ')) { /* valid field */
635
$value = trim(substr($line, $pos+1));
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;
645
if ($subject == "") {
646
$subject = _("(no subject)");
651
if ($pos = strpos($type, ";")) {
652
$type = substr($type, 0, $pos);
654
$type = explode("/", $type);
655
if(!is_array($type)) {
658
if (!isset($type[1])) {
674
$date = str_replace(' ', ' ', $date);
675
$tmpdate = explode(' ', trim($date));
677
$tmpdate = $date = array('', '', '', '', '', '');
681
$messages[$msgi]['ID'] = $unique_id;
684
$messages[$msgi]['ID'] = $id;
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);
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;
702
/* non server sort stuff */
703
if (!$allow_server_sort) {
704
$from = parseAddress($from);
706
$from = decodeHeader($from[0][1]);
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];
715
$messages[$msgi]['SUBJECT-SORT'] = $subject_sort;
720
array_reverse($messages);
721
$new_messages = array();
722
foreach ($messages as $i =>$message) {
723
$new_messages[] = $message;
725
return $new_messages;
729
* Returns a message array with all the information about a message.
730
* See the documentation folder for more information about this array.
732
function sqimap_get_message ($imap_stream, $id, $mailbox) {
736
$read = sqimap_run_command ($imap_stream, "FETCH $id (FLAGS BODYSTRUCTURE)", true, $response, $message, $uid_support);
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');
744
/* the message was not found, maybe the mailbox was modified? */
745
global $sort, $startMessage, $color;
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);
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;