6 * Copyright (c) 1999-2003 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.
14 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
17 /* SquirrelMail required files. */
18 require_once(SM_PATH
. 'include/validate.php');
19 require_once(SM_PATH
. 'functions/global.php');
20 require_once(SM_PATH
. 'functions/display_messages.php');
21 require_once(SM_PATH
. 'functions/imap.php');
22 require_once(SM_PATH
. 'functions/html.php');
24 global $compose_new_win;
26 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION
) ) {
30 function attachSelectedMessages($msg, $imapConnection) {
31 global $username, $attachment_dir, $startMessage,
32 $data_dir, $composesession, $uid_support,
33 $msgs, $show_num, $compose_messages;
35 if (!isset($compose_messages)) {
36 $compose_messages = array();
37 sqsession_register($compose_messages,'compose_messages');
40 if (!$composesession) {
42 sqsession_register($composesession,'composesession');
45 sqsession_register($composesession,'composesession');
48 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
50 $composeMessage = new Message();
51 $rfc822_header = new Rfc822Header();
52 $composeMessage->rfc822_header
= $rfc822_header;
53 $composeMessage->reply_rfc822_header
= '';
55 foreach($msg as $id) {
56 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, $uid_support);
58 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.'.eml',
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('attache', $attache, SQ_POST
);
116 sqgetGlobalVar('location', $location, SQ_POST
);
118 /* end of get globals */
120 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
121 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
123 $location = set_url_var($location,'composenew',0,false);
124 $location = set_url_var($location,'composesession',0,false);
125 $location = set_url_var($location,'session',0,false);
127 /* remember changes to mailbox setting */
128 if (!isset($lastTargetMailbox)) {
129 $lastTargetMailbox = 'INBOX';
131 if ($targetMailbox != $lastTargetMailbox) {
132 $lastTargetMailbox = $targetMailbox;
133 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
137 do_hook('move_before_move');
141 Move msg list sorting up here, as it is used several times,
142 makes it more efficient to do it in one place for the code
145 if (isset($msg) && is_array($msg)) {
146 foreach( $msg as $key=>$uid ) {
147 // using foreach removes the risk of infinite loops that was there //
152 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
153 if(isset($expungeButton)) {
154 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
155 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
156 if ($startMessage > $show_num) {
157 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
159 $location = set_url_var($location,'startMessage',1,false);
162 } elseif(isset($undeleteButton)) {
163 // undelete messages if user isn't using move_to_trash or auto_expunge
164 // Removes \Deleted flag from selected messages
166 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
170 } elseif (!isset($moveButton)) {
173 if (!isset($attache)) {
174 $button_action = concat_hook_function('move_messages_button_action');
175 if (isset($markRead)) {
176 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
177 } else if (isset($markUnread)) {
178 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
180 if (!$button_action) {
181 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
183 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
188 if (isset($attache)) {
189 $composesession = attachSelectedMessages($id, $imapConnection);
190 $location = set_url_var($location, 'session', $composesession, false);
191 if ($compose_new_win) {
192 $location = set_url_var($location, 'composenew', 1, false);
194 $location = str_replace('search.php','compose.php',$location);
195 $location = str_replace('right_main.php','compose.php',$location);
198 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
199 if ($startMessage > $show_num) {
200 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
202 $location = set_url_var($location,'startMessage',1, false);
209 } else { // Move messages
212 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
214 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
219 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
220 if ($startMessage > $show_num) {
221 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
223 $location = set_url_var($location,'startMessage',1, false);
230 // Log out this session
231 sqimap_logout($imapConnection);
233 displayPageHeader($color, $mailbox);
234 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
236 header("Location: $location");