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
) ) {
31 function putSelectedMessagesIntoString($msg) {
35 // If they have selected nothing msg is size one still, but will
36 // be an infinite loop because we never increment j. so check to
37 // see if msg[0] is set or not to fix this.
38 while (($j < count($msg)) && ($msg[0])) {
40 if ($firstLoop != true) {
41 $selectedMessages .= "&";
45 $selectedMessages .= "selMsg[$j]=$msg[$i]";
52 function attachSelectedMessages($msg, $imapConnection) {
53 global $username, $attachment_dir, $startMessage,
54 $data_dir, $composesession, $uid_support,
55 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
58 if (!isset($compose_messages)) {
59 $compose_messages = array();
60 sqsession_register($compose_messages,'compose_messages');
63 if (!$composesession) {
65 sqsession_register($composesession,'composesession');
68 sqsession_register($composesession,'composesession');
71 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
73 if ($thread_sort_messages ||
$allow_server_sort) {
76 $start_index = ($startMessage-1) * $show_num;
81 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
83 $composeMessage = new Message();
84 $rfc822_header = new Rfc822Header();
85 $composeMessage->rfc822_header
= $rfc822_header;
86 $composeMessage->reply_rfc822_header
= '';
88 while ($j < count($msg)) {
89 if (isset($msg[$i])) {
91 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
92 if ($response = 'OK') {
93 $k = $i +
$start_index;
94 $subject = $msgs[$k]['SUBJECT'];
97 $body = implode('', $body_a);
100 $localfilename = GenerateRandomString(32, 'FILE', 7);
101 $full_localfilename = "$hashed_attachment_dir/$localfilename";
103 $fp = fopen( $full_localfilename, 'wb');
106 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
107 $full_localfilename);
113 $compose_messages[$composesession] = $composeMessage;
114 sqsession_register($compose_messages,'compose_messages');
115 session_write_close();
116 return $composesession;
122 sqgetGlobalVar('key', $key, SQ_COOKIE
);
123 sqgetGlobalVar('username', $username, SQ_SESSION
);
124 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
125 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
126 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
128 sqgetGlobalVar('mailbox', $mailbox);
129 sqgetGlobalVar('startMessage', $startMessage);
130 sqgetGlobalVar('msg', $msg);
132 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
133 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
134 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
136 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
137 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
138 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
139 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
140 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
141 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
142 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
143 sqgetGlobalVar('attache', $attache, SQ_POST
);
144 sqgetGlobalVar('location', $location, SQ_POST
);
146 /* end of get globals */
148 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
149 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
151 $location = set_url_var($location,'composenew',0,false);
152 $location = set_url_var($location,'composesession',0,false);
153 $location = set_url_var($location,'session',0,false);
155 /* remember changes to mailbox setting */
156 if (!isset($lastTargetMailbox)) {
157 $lastTargetMailbox = 'INBOX';
159 if ($targetMailbox != $lastTargetMailbox) {
160 $lastTargetMailbox = $targetMailbox;
161 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
165 do_hook('move_before_move');
167 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
168 if(isset($expungeButton)) {
169 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
170 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
171 if ($startMessage > $show_num) {
172 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
174 $location = set_url_var($location,'startMessage',1,false);
177 } elseif(isset($undeleteButton)) {
178 // undelete messages if user isn't using move_to_trash or auto_expunge
179 if (is_array($msg) == 1) {
180 // Removes \Deleted flag from selected messages
183 // If they have selected nothing msg is size one still, but will be an infinite
184 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
185 while ($j < count($msg)) {
193 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
198 } elseif (!isset($moveButton)) {
199 // If the delete button was pressed, the moveButton variable will not be set.
200 if (is_array($msg)) {
201 // Marks the selected messages as 'Deleted'
204 // If they have selected nothing msg is size one still, but will be an infinite
205 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
206 while ($j < count($msg)) {
207 if (isset($msg[$i])) {
213 if (count($id) && !isset($attache)) {
214 if (isset($markRead)) {
215 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
216 } else if (isset($markUnread)) {
217 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
219 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
221 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
225 if (isset($attache)) {
226 $composesession = attachSelectedMessages($msg, $imapConnection);
227 $location = set_url_var($location, 'session', $composesession, false);
228 if ($compose_new_win) {
229 $location = set_url_var($location, 'composenew', 1, false);
231 $location = str_replace('search.php','compose.php',$location);
232 $location = str_replace('right_main.php','compose.php',$location);
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 } else { // Move messages
247 // lets check to see if they selected any messages
248 if (is_array($msg)) {
251 // If they have selected nothing msg is size one still, but will be an infinite
252 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
255 if (isset($msg[$i])) {
261 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
263 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
268 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
269 if ($startMessage > $show_num) {
270 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
272 $location = set_url_var($location,'startMessage',1, false);
279 // Log out this session
280 sqimap_logout($imapConnection);
282 displayPageHeader($color, $mailbox);
283 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
285 header("Location: $location");