correct Move & next when moving the first message in a mailbox
[squirrelmail.git] / src / move_messages.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * move_messages.php
5 *
82d304a0 6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Enables message moving between folders on the IMAP server.
10 *
11 * $Id$
8f6f9ba5 12 * @package squirrelmail
35586184 13 */
ef870322 14
8f6f9ba5 15/** Path for SquirrelMail required files. */
86725763 16define('SM_PATH','../');
17
18/* SquirrelMail required files. */
08185f2a 19require_once(SM_PATH . 'include/validate.php');
8650e9e1 20require_once(SM_PATH . 'functions/global.php');
86725763 21require_once(SM_PATH . 'functions/display_messages.php');
22require_once(SM_PATH . 'functions/imap.php');
23require_once(SM_PATH . 'functions/html.php');
24
acc28bac 25global $compose_new_win;
26
1e12d1ff 27if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
2cb178e3 28 $composesession = 0;
1e12d1ff 29}
30
acc28bac 31function attachSelectedMessages($msg, $imapConnection) {
133b9a0e 32 global $username, $attachment_dir, $startMessage,
6201339c 33 $data_dir, $composesession,
2cb178e3 34 $msgs, $show_num, $compose_messages;
a2a30ee7 35
a2a30ee7 36 if (!isset($compose_messages)) {
11fd4a7b 37 $compose_messages = array();
2cb178e3 38 sqsession_register($compose_messages,'compose_messages');
acc28bac 39 }
40
07abb0f3 41 if (!$composesession) {
11fd4a7b 42 $composesession = 1;
2cb178e3 43 sqsession_register($composesession,'composesession');
acc28bac 44 } else {
11fd4a7b 45 $composesession++;
46 sqsession_register($composesession,'composesession');
acc28bac 47 }
48
21f3c131 49 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a2a30ee7 50
51 $composeMessage = new Message();
52 $rfc822_header = new Rfc822Header();
53 $composeMessage->rfc822_header = $rfc822_header;
54 $composeMessage->reply_rfc822_header = '';
55
2cb178e3 56 foreach($msg as $id) {
6201339c 57 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
11fd4a7b 58
2cb178e3 59 if ($response == 'OK') {
2cb178e3 60 // fetch the subject for the message with $id from msgs.
61 // is there a more efficient way to do this?
62 foreach($msgs as $k => $vals) {
63 if($vals['ID'] == $id) {
64 $subject = $msgs[$k]['SUBJECT'];
65 break;
a2388eb7 66 }
2cb178e3 67 }
a2388eb7 68
2cb178e3 69 array_shift($body_a);
c2517a3b 70 array_pop($body_a);
2cb178e3 71 $body = implode('', $body_a);
72 $body .= "\r\n";
a2388eb7 73
2cb178e3 74 $localfilename = GenerateRandomString(32, 'FILE', 7);
75 $full_localfilename = "$hashed_attachment_dir/$localfilename";
a2388eb7 76
2cb178e3 77 $fp = fopen( $full_localfilename, 'wb');
78 fwrite ($fp, $body);
79 fclose($fp);
745fca27 80 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
2cb178e3 81 $full_localfilename);
a2388eb7 82 }
acc28bac 83 }
2cb178e3 84
a2a30ee7 85 $compose_messages[$composesession] = $composeMessage;
87d5f199 86 sqsession_register($compose_messages,'compose_messages');
87 session_write_close();
acc28bac 88 return $composesession;
89}
90
9c22eb94 91
9837b0a5 92
93/* get globals */
1e12d1ff 94sqgetGlobalVar('key', $key, SQ_COOKIE);
95sqgetGlobalVar('username', $username, SQ_SESSION);
96sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
97sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
98sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
99
100sqgetGlobalVar('mailbox', $mailbox);
101sqgetGlobalVar('startMessage', $startMessage);
102sqgetGlobalVar('msg', $msg);
103
104sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
105sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
106sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
107
108sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
109sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
110sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
111sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
112sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
113sqgetGlobalVar('markRead', $markRead, SQ_POST);
114sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
057dbe18 115sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST);
116sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST);
1e12d1ff 117sqgetGlobalVar('attache', $attache, SQ_POST);
118sqgetGlobalVar('location', $location, SQ_POST);
6c540963 119sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST);
11026a30 120sqgetGlobalVar('dmn', $is_dmn, SQ_POST);
9837b0a5 121
9837b0a5 122/* end of get globals */
123
65c3ec94 124$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 125$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 126
87d5f199 127$location = set_url_var($location,'composenew',0,false);
128$location = set_url_var($location,'composesession',0,false);
129$location = set_url_var($location,'session',0,false);
8c33aaf7 130
cd885078 131/* remember changes to mailbox setting */
132if (!isset($lastTargetMailbox)) {
133 $lastTargetMailbox = 'INBOX';
134}
135if ($targetMailbox != $lastTargetMailbox) {
136 $lastTargetMailbox = $targetMailbox;
9837b0a5 137 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 138}
87d5f199 139$exception = false;
00f9181e 140
141do_hook('move_before_move');
142
11fd4a7b 143
144/*
145 Move msg list sorting up here, as it is used several times,
146 makes it more efficient to do it in one place for the code
147*/
148$id = array();
149if (isset($msg) && is_array($msg)) {
150 foreach( $msg as $key=>$uid ) {
151 // using foreach removes the risk of infinite loops that was there //
152 $id[] = $uid;
153 }
154}
155
65c3ec94 156// expunge-on-demand if user isn't using move_to_trash or auto_expunge
157if(isset($expungeButton)) {
9c22eb94 158 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
159 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
160 if ($startMessage > $show_num) {
11fd4a7b 161 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
162 } else {
163 $location = set_url_var($location,'startMessage',1,false);
164 }
9c22eb94 165 }
65c3ec94 166} elseif(isset($undeleteButton)) {
167 // undelete messages if user isn't using move_to_trash or auto_expunge
11fd4a7b 168 // Removes \Deleted flag from selected messages
169 if (count($id)) {
170 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
65c3ec94 171 } else {
11fd4a7b 172 $exception = true;
65c3ec94 173 }
174} elseif (!isset($moveButton)) {
11fd4a7b 175 if (count($id)) {
176 $cnt = count($id);
177 if (!isset($attache)) {
178 if (isset($markRead)) {
179 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
180 } else if (isset($markUnread)) {
181 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
fd181f53 182 } else if (isset($markFlagged)) {
183 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', true, true);
184 } else if (isset($markUnflagged)) {
185 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', false, true);
11fd4a7b 186 } else {
fd181f53 187 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
6c540963 188 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
d3714d3a 189 if ($auto_expunge) {
190 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
191 }
11fd4a7b 192 }
2d7d3c2a 193 }
ce2c797b 194 }
8c33aaf7 195 if (isset($attache)) {
11fd4a7b 196 $composesession = attachSelectedMessages($id, $imapConnection);
197 $location = set_url_var($location, 'session', $composesession, false);
198 if ($compose_new_win) {
199 $location = set_url_var($location, 'composenew', 1, false);
200 } else {
201 $location = str_replace('search.php','compose.php',$location);
202 $location = str_replace('right_main.php','compose.php',$location);
203 }
204 } else {
205 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
206 if ($startMessage > $show_num) {
207 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
208 } else {
209 $location = set_url_var($location,'startMessage',1, false);
210 }
211 }
212 }
65c3ec94 213 } else {
11fd4a7b 214 $exception = true;
65c3ec94 215 }
216} else { // Move messages
11fd4a7b 217
11026a30 218 $num_ids = count($id);
219 if ( $num_ids > 0 ) {
220 if ( $is_dmn && count($id) == 1 ) {
221 sqimap_msgs_list_move($imapConnection,$id[0],$targetMailbox);
222 $cnt = sqimap_mailbox_expunge_dmn($id[0]);
11fd4a7b 223 } else {
11026a30 224 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
225 if ($auto_expunge) {
226 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
227 } else {
228 $cnt = 0;
229 }
11fd4a7b 230 }
231
232 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
233 if ($startMessage > $show_num) {
234 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
235 } else {
236 $location = set_url_var($location,'startMessage',1, false);
237 }
238 }
65c3ec94 239 } else {
11fd4a7b 240 $exception = true;
65c3ec94 241 }
242}
65c3ec94 243// Log out this session
244sqimap_logout($imapConnection);
87d5f199 245if ($exception) {
246 displayPageHeader($color, $mailbox);
247 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
248} else {
249 header("Location: $location");
250 exit;
251}
dcc1cc82 252?>
253</BODY></HTML>