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/display_messages.php');
20 require_once(SM_PATH
. 'functions/imap.php');
21 require_once(SM_PATH
. 'functions/html.php');
23 global $compose_new_win;
25 if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION
) ) {
30 function putSelectedMessagesIntoString($msg) {
34 // If they have selected nothing msg is size one still, but will
35 // be an infinite loop because we never increment j. so check to
36 // see if msg[0] is set or not to fix this.
37 while (($j < count($msg)) && ($msg[0])) {
39 if ($firstLoop != true) {
40 $selectedMessages .= "&";
44 $selectedMessages .= "selMsg[$j]=$msg[$i]";
51 function attachSelectedMessages($msg, $imapConnection) {
52 global $username, $attachment_dir,
53 $data_dir, $composesession, $uid_support,
54 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
57 if (!isset($compose_messages)) {
58 $compose_messages = array();
59 sqsession_register($compose_messages,'compose_messages');
62 if (!$composesession) {
64 sqsession_register($composesession,'composesession');
67 sqsession_register($composesession,'composesession');
70 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
72 if ($thread_sort_messages ||
$allow_server_sort) {
75 $start_index = ($startMessage-1) * $show_num;
80 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
82 $composeMessage = new Message();
83 $rfc822_header = new Rfc822Header();
84 $composeMessage->rfc822_header
= $rfc822_header;
85 $composeMessage->reply_rfc822_header
= '';
87 while ($j < count($msg)) {
88 if (isset($msg[$i])) {
90 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
91 if ($response = 'OK') {
92 $k = $i +
$start_index;
93 $subject = $msgs[$k]['SUBJECT'];
96 $body = implode('', $body_a);
99 $localfilename = GenerateRandomString(32, 'FILE', 7);
100 $full_localfilename = "$hashed_attachment_dir/$localfilename";
102 $fp = fopen( $full_localfilename, 'wb');
105 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
106 $full_localfilename);
112 $compose_messages[$composesession] = $composeMessage;
113 sqsession_register($compose_messages,'compose_messages');
114 session_write_close();
115 return $composesession;
121 sqgetGlobalVar('key', $key, SQ_COOKIE
);
122 sqgetGlobalVar('username', $username, SQ_SESSION
);
123 sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION
);
124 sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION
);
125 sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION
);
127 sqgetGlobalVar('mailbox', $mailbox);
128 sqgetGlobalVar('startMessage', $startMessage);
129 sqgetGlobalVar('msg', $msg);
131 sqgetGlobalVar('msgs', $msgs, SQ_SESSION
);
132 sqgetGlobalVar('composesession', $composesession, SQ_SESSION
);
133 sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION
);
135 sqgetGlobalVar('moveButton', $moveButton, SQ_POST
);
136 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
137 sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST
);
138 sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST
);
139 sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST
);
140 sqgetGlobalVar('markRead', $markRead, SQ_POST
);
141 sqgetGlobalVar('markUnread', $markUnread, SQ_POST
);
142 sqgetGlobalVar('attache', $attache, SQ_POST
);
143 sqgetGlobalVar('location', $location, SQ_POST
);
145 /* end of get globals */
147 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
148 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
150 $location = set_url_var($location,'composenew',0,false);
151 $location = set_url_var($location,'composesession',0,false);
152 $location = set_url_var($location,'session',0,false);
154 /* remember changes to mailbox setting */
155 if (!isset($lastTargetMailbox)) {
156 $lastTargetMailbox = 'INBOX';
158 if ($targetMailbox != $lastTargetMailbox) {
159 $lastTargetMailbox = $targetMailbox;
160 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
163 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
164 if(isset($expungeButton)) {
165 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
166 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
167 if ($startMessage > $show_num) {
168 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
170 $location = set_url_var($location,'startMessage',1,false);
173 } elseif(isset($undeleteButton)) {
174 // undelete messages if user isn't using move_to_trash or auto_expunge
175 if (is_array($msg) == 1) {
176 // Removes \Deleted flag from selected messages
179 // If they have selected nothing msg is size one still, but will be an infinite
180 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
181 while ($j < count($msg)) {
189 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
194 } elseif (!isset($moveButton)) {
195 // If the delete button was pressed, the moveButton variable will not be set.
196 if (is_array($msg)) {
197 // Marks the selected messages as 'Deleted'
200 // If they have selected nothing msg is size one still, but will be an infinite
201 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
202 while ($j < count($msg)) {
203 if (isset($msg[$i])) {
209 if (count($id) && !isset($attache)) {
210 if (isset($markRead)) {
211 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
212 } else if (isset($markUnread)) {
213 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
215 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
217 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
221 if (isset($attache)) {
222 $composesession = attachSelectedMessages($msg, $imapConnection);
223 $location = set_url_var($location, 'session', $composesession, false);
224 if ($compose_new_win) {
225 $location = set_url_var($location, 'composenew', 1, false);
227 $location = str_replace('search.php','compose.php',$location);
228 $location = str_replace('right_main.php','compose.php',$location);
231 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
232 if ($startMessage > $show_num) {
233 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
235 $location = set_url_var($location,'startMessage',1, false);
242 } else { // Move messages
243 // lets check to see if they selected any messages
244 if (is_array($msg)) {
247 // If they have selected nothing msg is size one still, but will be an infinite
248 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
251 if (isset($msg[$i])) {
257 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
259 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
264 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
265 if ($startMessage > $show_num) {
266 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
268 $location = set_url_var($location,'startMessage',1, false);
275 // Log out this session
276 sqimap_logout($imapConnection);
278 displayPageHeader($color, $mailbox);
279 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
281 header("Location: $location");