6 * Copyright (c) 1999-2002 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 function putSelectedMessagesIntoString($msg) {
29 // If they have selected nothing msg is size one still, but will
30 // be an infinite loop because we never increment j. so check to
31 // see if msg[0] is set or not to fix this.
32 while (($j < count($msg)) && ($msg[0])) {
34 if ($firstLoop != true) {
35 $selectedMessages .= "&";
39 $selectedMessages .= "selMsg[$j]=$msg[$i]";
46 function attachSelectedMessages($msg, $imapConnection) {
47 global $username, $attachment_dir,
48 $data_dir, $composesession, $uid_support,
49 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
53 if (!isset($compose_messages)) {
54 $compose_messages = array();
55 sqsession_register($compose_messages,'compose_messages');
58 if (!isset($composesession) ) {
60 sqsession_register($composesession,'composesession');
65 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
67 if ($thread_sort_messages ||
$allow_server_sort) {
70 $start_index = ($startMessage-1) * $show_num;
75 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
77 $composeMessage = new Message();
78 $rfc822_header = new Rfc822Header();
79 $composeMessage->rfc822_header
= $rfc822_header;
80 $composeMessage->reply_rfc822_header
= '';
82 while ($j < count($msg)) {
83 if (isset($msg[$i])) {
85 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
86 if ($response = 'OK') {
87 $k = $i +
$start_index;
88 $subject = $msgs[$k]['SUBJECT'];
91 $body = implode('', $body_a);
94 $localfilename = GenerateRandomString(32, 'FILE', 7);
95 $full_localfilename = "$hashed_attachment_dir/$localfilename";
97 $fp = fopen( $full_localfilename, 'wb');
100 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
101 $full_localfilename);
107 $compose_messages[$composesession] = $composeMessage;
108 sqsession_register($compose_messages,'compose_messages');
109 return $composesession;
116 $username = $_SESSION['username'];
117 $key = $_COOKIE['key'];
118 $onetimepad = $_SESSION['onetimepad'];
119 $base_uri = $_SESSION['base_uri'];
120 $delimiter = $_SESSION['delimiter'];
122 if (isset($_POST['mailbox'])) {
123 $mailbox = $_POST['mailbox'];
125 if (isset($_POST['startMessage'])) {
126 $startMessage = $_POST['startMessage'];
128 if (isset($_POST['moveButton'])) {
129 $moveButton = $_POST['moveButton'];
131 if (isset($_POST['msg'])) {
132 $msg = $_POST['msg'];
134 if (isset($_POST['expungeButton'])) {
135 $expungeButton = $_POST['expungeButton'];
137 if (isset($_POST['targetMailbox'])) {
138 $targetMailbox = $_POST['targetMailbox'];
140 if (isset($_SESSION['lastTargetMailbox'])) {
141 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
143 if (isset($_POST['expungeButton'])) {
144 $expungeButton = $_POST['expungeButton'];
146 if (isset($_POST['undeleteButton'])) {
147 $undeleteButton = $_POST['undeleteButton'];
149 if (isset($_POST['markRead'])) {
150 $markRead = $_POST['markRead'];
152 if (isset($_POST['markUnread'])) {
153 $markUnread = $_POST['markUnread'];
155 if (isset($_POST['attache'])) {
156 $attache = $_POST['attache'];
159 /* end of get globals */
161 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
162 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
164 $location = set_url_var($location,'composenew');
165 $location = set_url_var($location,'composesession');
166 $location = set_url_var($location,'session');
168 /* remember changes to mailbox setting */
169 if (!isset($lastTargetMailbox)) {
170 $lastTargetMailbox = 'INBOX';
172 if ($targetMailbox != $lastTargetMailbox) {
173 $lastTargetMailbox = $targetMailbox;
174 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
177 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
178 if(isset($expungeButton)) {
179 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
180 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
181 if ($startMessage > $show_num) {
182 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
184 $location = set_url_var($location,'startMessage',1);
187 header("Location: $location");
188 } elseif(isset($undeleteButton)) {
189 // undelete messages if user isn't using move_to_trash or auto_expunge
190 if (is_array($msg) == 1) {
191 // Removes \Deleted flag from selected messages
194 // If they have selected nothing msg is size one still, but will be an infinite
195 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
196 while ($j < count($msg)) {
198 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
203 header ("Location: $location");
205 displayPageHeader($color, $mailbox);
206 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
208 } elseif (!isset($moveButton)) {
209 // If the delete button was pressed, the moveButton variable will not be set.
210 if (is_array($msg) == 1) {
211 // Marks the selected messages as 'Deleted'
214 // If they have selected nothing msg is size one still, but will be an infinite
215 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
216 while ($j < count($msg)) {
217 if (isset($msg[$i])) {
218 if (isset($markRead)) {
219 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
220 } else if (isset($markUnread)) {
221 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
222 } else if (isset($attache)) {
225 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
232 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
236 if (isset($attache)) {
237 $composesession = attachSelectedMessages($msg, $imapConnection);
238 if ($compose_new_win) {
239 header ("Location: $location&composenew=1&session=$composesession");
241 $location = str_replace('search.php','compose.php',$location);
242 $location = str_replace('right_main.php','compose.php',$location);
243 header ("Location: $location&session=$composesession");
246 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
247 if ($startMessage > $show_num) {
248 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
250 $location = set_url_var($location,'startMessage',1);
253 header ("Location: $location");
256 displayPageHeader($color, $mailbox);
257 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
259 } else { // Move messages
260 // lets check to see if they selected any messages
261 if (is_array($msg) == 1) {
264 // If they have selected nothing msg is size one still, but will be an infinite
265 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
268 if (isset($msg[$i])) {
269 /** check if they would like to move it to the trash folder or not */
270 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
271 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
277 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
282 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
283 if ($startMessage > $show_num) {
284 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
286 $location = set_url_var($location,'startMessage',1);
289 header ("Location: $location");
291 displayPageHeader($color, $mailbox);
292 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
295 // Log out this session
296 sqimap_logout($imapConnection);