6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Enables message moving between folders on the IMAP server.
12 * @package squirrelmail
16 * Path for SquirrelMail required files.
19 define('SM_PATH','../');
21 /* SquirrelMail required files. */
22 require_once(SM_PATH
. 'include/validate.php');
23 require_once(SM_PATH
. 'functions/global.php');
24 require_once(SM_PATH
. 'functions/display_messages.php');
25 require_once(SM_PATH
. 'functions/imap.php');
26 require_once(SM_PATH
. 'functions/html.php');
28 global $compose_new_win;
30 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION
) ) {
34 function attachSelectedMessages($msg, $imapConnection) {
35 global $username, $attachment_dir, $startMessage,
36 $data_dir, $composesession,
37 $msgs, $show_num, $compose_messages;
39 if (!isset($compose_messages)) {
40 $compose_messages = array();
41 sqsession_register($compose_messages,'compose_messages');
44 if (!$composesession) {
46 sqsession_register($composesession,'composesession');
49 sqsession_register($composesession,'composesession');
52 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
54 $composeMessage = new Message();
55 $rfc822_header = new Rfc822Header();
56 $composeMessage->rfc822_header
= $rfc822_header;
57 $composeMessage->reply_rfc822_header
= '';
59 foreach($msg as $id) {
60 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
62 if ($response == 'OK') {
63 // fetch the subject for the message with $id from msgs.
64 // is there a more efficient way to do this?
65 foreach($msgs as $k => $vals) {
66 if($vals['ID'] == $id) {
67 $subject = $msgs[$k]['SUBJECT'];
74 $body = implode('', $body_a);
77 $localfilename = GenerateRandomString(32, 'FILE', 7);
78 $full_localfilename = "$hashed_attachment_dir/$localfilename";
80 $fp = fopen( $full_localfilename, 'wb');
83 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
88 $compose_messages[$composesession] = $composeMessage;
89 sqsession_register($compose_messages,'compose_messages');
90 session_write_close();
91 return $composesession;
97 sqgetGlobalVar('key', $key, SQ_COOKIE
);
98 sqgetGlobalVar('username', $username, SQ_SESSION
);
99 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
100 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
101 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
103 sqgetGlobalVar('mailbox', $mailbox);
104 sqgetGlobalVar('startMessage', $startMessage);
105 sqgetGlobalVar('msg', $msg);
107 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
108 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
109 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
111 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
112 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
113 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
114 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
115 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
116 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
117 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
118 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST
);
119 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST
);
120 sqgetGlobalVar('attache', $attache, SQ_POST
);
121 sqgetGlobalVar('location', $location, SQ_POST
);
122 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
123 sqgetGlobalVar('dmn', $is_dmn, SQ_POST
);
125 /* end of get globals */
127 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
128 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
130 $location = set_url_var($location,'composenew',0,false);
131 $location = set_url_var($location,'composesession',0,false);
132 $location = set_url_var($location,'session',0,false);
134 /* remember changes to mailbox setting */
135 if (!isset($lastTargetMailbox)) {
136 $lastTargetMailbox = 'INBOX';
138 if ($targetMailbox != $lastTargetMailbox) {
139 $lastTargetMailbox = $targetMailbox;
140 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
144 do_hook('move_before_move');
148 Move msg list sorting up here, as it is used several times,
149 makes it more efficient to do it in one place for the code
152 if (isset($msg) && is_array($msg)) {
153 foreach( $msg as $key=>$uid ) {
154 // using foreach removes the risk of infinite loops that was there //
159 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
160 if(isset($expungeButton)) {
161 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
162 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
163 if ($startMessage > $show_num) {
164 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
166 $location = set_url_var($location,'startMessage',1,false);
169 } elseif(isset($undeleteButton)) {
170 // undelete messages if user isn't using move_to_trash or auto_expunge
171 // Removes \Deleted flag from selected messages
173 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
177 } elseif (!isset($moveButton)) {
180 if (!isset($attache)) {
181 if (isset($markRead)) {
182 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
183 } else if (isset($markUnread)) {
184 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
185 } else if (isset($markFlagged)) {
186 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', true, true);
187 } else if (isset($markUnflagged)) {
188 sqimap_toggle_flag($imapConnection, $id, '\\Flagged', false, true);
190 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
191 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
193 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
198 if (isset($attache)) {
199 $composesession = attachSelectedMessages($id, $imapConnection);
200 $location = set_url_var($location, 'session', $composesession, false);
201 if ($compose_new_win) {
202 $location = set_url_var($location, 'composenew', 1, false);
204 $location = str_replace('search.php','compose.php',$location);
205 $location = str_replace('right_main.php','compose.php',$location);
208 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
209 if ($startMessage > $show_num) {
210 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
212 $location = set_url_var($location,'startMessage',1, false);
219 } else { // Move messages
221 $num_ids = count($id);
222 if ( $num_ids > 0 ) {
223 if ( $is_dmn && count($id) == 1 ) {
224 sqimap_msgs_list_move($imapConnection,$id[0],$targetMailbox);
225 $cnt = sqimap_mailbox_expunge_dmn($id[0]);
227 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
229 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
235 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
236 if ($startMessage > $show_num) {
237 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
239 $location = set_url_var($location,'startMessage',1, false);
246 // Log out this session
247 sqimap_logout($imapConnection);
249 displayPageHeader($color, $mailbox);
250 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
252 header("Location: $location");