9ee018bfcde85dc41f292d75d4c0a600939d0b8c
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, $uid_support,
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, $uid_support);
59 if ($response == 'OK') {
61 // fetch the subject for the message with $id from msgs.
62 // is there a more efficient way to do this?
63 foreach($msgs as $k => $vals) {
64 if($vals['ID'] == $id) {
65 $subject = $msgs[$k]['SUBJECT'];
72 $body = implode('', $body_a);
75 $localfilename = GenerateRandomString(32, 'FILE', 7);
76 $full_localfilename = "$hashed_attachment_dir/$localfilename";
78 $fp = fopen( $full_localfilename, 'wb');
81 $composeMessage->initAttachment('message/rfc822',$subject.'.msg',
86 $compose_messages[$composesession] = $composeMessage;
87 sqsession_register($compose_messages,'compose_messages');
88 session_write_close();
89 return $composesession;
95 sqgetGlobalVar('key', $key, SQ_COOKIE
);
96 sqgetGlobalVar('username', $username, SQ_SESSION
);
97 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
98 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
99 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
101 sqgetGlobalVar('mailbox', $mailbox);
102 sqgetGlobalVar('startMessage', $startMessage);
103 sqgetGlobalVar('msg', $msg);
105 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
106 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
107 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
109 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
110 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
111 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
112 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
113 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
114 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
115 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
116 sqgetGlobalVar('attache', $attache, SQ_POST
);
117 sqgetGlobalVar('location', $location, SQ_POST
);
118 sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST
);
120 /* end of get globals */
122 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
123 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
125 $location = set_url_var($location,'composenew',0,false);
126 $location = set_url_var($location,'composesession',0,false);
127 $location = set_url_var($location,'session',0,false);
129 /* remember changes to mailbox setting */
130 if (!isset($lastTargetMailbox)) {
131 $lastTargetMailbox = 'INBOX';
133 if ($targetMailbox != $lastTargetMailbox) {
134 $lastTargetMailbox = $targetMailbox;
135 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
139 do_hook('move_before_move');
143 Move msg list sorting up here, as it is used several times,
144 makes it more efficient to do it in one place for the code
147 if (isset($msg) && is_array($msg)) {
148 foreach( $msg as $key=>$uid ) {
149 // using foreach removes the risk of infinite loops that was there //
154 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
155 if(isset($expungeButton)) {
156 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
157 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
158 if ($startMessage > $show_num) {
159 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
161 $location = set_url_var($location,'startMessage',1,false);
164 } elseif(isset($undeleteButton)) {
165 // undelete messages if user isn't using move_to_trash or auto_expunge
166 // Removes \Deleted flag from selected messages
168 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
172 } elseif (!isset($moveButton)) {
175 if (!isset($attache)) {
176 $button_action = concat_hook_function('move_messages_button_action');
177 if (isset($markRead)) {
178 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
179 } else if (isset($markUnread)) {
180 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
182 if (!$button_action) {
183 sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash);
185 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
190 if (isset($attache)) {
191 $composesession = attachSelectedMessages($id, $imapConnection);
192 $location = set_url_var($location, 'session', $composesession, false);
193 if ($compose_new_win) {
194 $location = set_url_var($location, 'composenew', 1, false);
196 $location = str_replace('search.php','compose.php',$location);
197 $location = str_replace('right_main.php','compose.php',$location);
200 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
201 if ($startMessage > $show_num) {
202 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
204 $location = set_url_var($location,'startMessage',1, false);
211 } else { // Move messages
214 sqimap_msgs_list_move($imapConnection,$id,$targetMailbox);
216 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
221 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
222 if ($startMessage > $show_num) {
223 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
225 $location = set_url_var($location,'startMessage',1, false);
232 // Log out this session
233 sqimap_logout($imapConnection);
235 displayPageHeader($color, $mailbox);
236 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
238 header("Location: $location");