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
15 /** Path for SquirrelMail required files. */
16 define('SM_PATH','../');
18 /* SquirrelMail required files. */
19 require_once(SM_PATH
. 'include/validate.php');
20 require_once(SM_PATH
. 'functions/global.php');
21 require_once(SM_PATH
. 'functions/display_messages.php');
22 require_once(SM_PATH
. 'functions/imap.php');
23 require_once(SM_PATH
. 'functions/html.php');
25 global $compose_new_win;
27 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION
) ) {
31 function attachSelectedMessages($msg, $imapConnection) {
32 global $username, $attachment_dir, $startMessage,
33 $data_dir, $composesession,
34 $msgs, $show_num, $compose_messages;
36 if (!isset($compose_messages)) {
37 $compose_messages = array();
38 sqsession_register($compose_messages,'compose_messages');
41 if (!$composesession) {
43 sqsession_register($composesession,'composesession');
46 sqsession_register($composesession,'composesession');
49 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
51 $composeMessage = new Message();
52 $rfc822_header = new Rfc822Header();
53 $composeMessage->rfc822_header
= $rfc822_header;
54 $composeMessage->reply_rfc822_header
= '';
56 foreach($msg as $id) {
57 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, TRUE);
59 if ($response == 'OK') {
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'];
71 $body = implode('', $body_a);
74 $localfilename = GenerateRandomString(32, 'FILE', 7);
75 $full_localfilename = "$hashed_attachment_dir/$localfilename";
77 $fp = fopen( $full_localfilename, 'wb');
80 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
85 $compose_messages[$composesession] = $composeMessage;
86 sqsession_register($compose_messages,'compose_messages');
87 session_write_close();
88 return $composesession;
94 sqgetGlobalVar('key', $key, SQ_COOKIE
);
95 sqgetGlobalVar('username', $username, SQ_SESSION
);
96 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
97 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
98 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
100 sqgetGlobalVar('mailbox', $mailbox);
101 sqgetGlobalVar('startMessage', $startMessage);
102 sqgetGlobalVar('msg', $msg);
104 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
105 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
106 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
108 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
109 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
110 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
111 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
112 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
113 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
114 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
115 sqgetGlobalVar('markFlagged', $markFlagged, SQ_POST
);
116 sqgetGlobalVar('markUnflagged', $markUnflagged, SQ_POST
);
117 sqgetGlobalVar('attache', $attache, SQ_POST
);
118 sqgetGlobalVar('location', $location, SQ_POST
);
119 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
120 sqgetGlobalVar('dmn', $is_dmn, SQ_POST
);
122 /* end of get globals */
124 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
125 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
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);
131 /* remember changes to mailbox setting */
132 if (!isset($lastTargetMailbox)) {
133 $lastTargetMailbox = 'INBOX';
135 if ($targetMailbox != $lastTargetMailbox) {
136 $lastTargetMailbox = $targetMailbox;
137 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
141 do_hook('move_before_move');
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
149 if (isset($msg) && is_array($msg)) {
150 foreach( $msg as $key=>$uid ) {
151 // using foreach removes the risk of infinite loops that was there //
156 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
157 if(isset($expungeButton)) {
158 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
159 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
160 if ($startMessage > $show_num) {
161 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
163 $location = set_url_var($location,'startMessage',1,false);
166 } elseif(isset($undeleteButton)) {
167 // undelete messages if user isn't using move_to_trash or auto_expunge
168 // Removes \Deleted flag from selected messages
170 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
174 } elseif (!isset($moveButton)) {
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);
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);
187 if (!boolean_hook_function('move_messages_button_action', NULL, 1)) {
188 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
190 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
195 if (isset($attache)) {
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);
201 $location = str_replace('search.php','compose.php',$location);
202 $location = str_replace('right_main.php','compose.php',$location);
205 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
206 if ($startMessage > $show_num) {
207 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
209 $location = set_url_var($location,'startMessage',1, false);
216 } else { // Move messages
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]);
224 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
226 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
232 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
233 if ($startMessage > $show_num) {
234 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
236 $location = set_url_var($location,'startMessage',1, false);
243 // Log out this session
244 sqimap_logout($imapConnection);
246 displayPageHeader($color, $mailbox);
247 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
249 header("Location: $location");