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 if (isset($_SESSION['composesession'])) {
26 $composesession = $_SESSION['composesession'];
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,
54 $data_dir, $composesession, $uid_support,
55 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
59 if (!isset($compose_messages)) {
60 $compose_messages = array();
61 sqsession_register($compose_messages,'compose_messages');
64 if (!$composesession) {
66 sqsession_register($composesession,'composesession');
69 sqsession_register($composesession,'composesession');
72 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
74 if ($thread_sort_messages ||
$allow_server_sort) {
77 $start_index = ($startMessage-1) * $show_num;
82 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
84 $composeMessage = new Message();
85 $rfc822_header = new Rfc822Header();
86 $composeMessage->rfc822_header
= $rfc822_header;
87 $composeMessage->reply_rfc822_header
= '';
89 while ($j < count($msg)) {
90 if (isset($msg[$i])) {
92 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
93 if ($response = 'OK') {
94 $k = $i +
$start_index;
95 $subject = $msgs[$k]['SUBJECT'];
98 $body = implode('', $body_a);
101 $localfilename = GenerateRandomString(32, 'FILE', 7);
102 $full_localfilename = "$hashed_attachment_dir/$localfilename";
104 $fp = fopen( $full_localfilename, 'wb');
107 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
108 $full_localfilename);
114 $compose_messages[$composesession] = $composeMessage;
115 sqsession_register($compose_messages,'compose_messages');
116 return $composesession;
123 $username = $_SESSION['username'];
124 $key = $_COOKIE['key'];
125 $onetimepad = $_SESSION['onetimepad'];
126 $base_uri = $_SESSION['base_uri'];
127 $delimiter = $_SESSION['delimiter'];
128 if (isset($_GET['mailbox'])) {
129 $mailbox = $_GET['mailbox'];
131 if (isset($_GET['startMessage'])) {
132 $startMessage = $_GET['startMessage'];
134 if (isset($_POST['moveButton'])) {
135 $moveButton = $_POST['moveButton'];
137 if (isset($_POST['msg'])) {
138 $msg = $_POST['msg'];
140 elseif (isset($_GET['msg'])) {
144 if (isset($_SESSION['msgs'])) {
145 $msgs = $_SESSION['msgs'];
149 if (isset($_POST['expungeButton'])) {
150 $expungeButton = $_POST['expungeButton'];
152 if (isset($_POST['targetMailbox'])) {
153 $targetMailbox = $_POST['targetMailbox'];
155 if (isset($_SESSION['lastTargetMailbox'])) {
156 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
158 if (isset($_POST['expungeButton'])) {
159 $expungeButton = $_POST['expungeButton'];
161 if (isset($_POST['undeleteButton'])) {
162 $undeleteButton = $_POST['undeleteButton'];
164 if (isset($_POST['markRead'])) {
165 $markRead = $_POST['markRead'];
167 if (isset($_POST['markUnread'])) {
168 $markUnread = $_POST['markUnread'];
170 if (isset($_POST['attache'])) {
171 $attache = $_POST['attache'];
174 if (isset($_POST['location'])) {
175 $location = $_POST['location'];
178 if (isset($_SESSION['composesession'])) {
179 $composesession = $_SESSION['composesession'];
181 /* end of get globals */
183 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
184 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
186 $location = set_url_var($location,'composenew');
187 $location = set_url_var($location,'composesession');
188 $location = set_url_var($location,'session');
190 /* remember changes to mailbox setting */
191 if (!isset($lastTargetMailbox)) {
192 $lastTargetMailbox = 'INBOX';
194 if ($targetMailbox != $lastTargetMailbox) {
195 $lastTargetMailbox = $targetMailbox;
196 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
199 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
200 if(isset($expungeButton)) {
201 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
202 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
203 if ($startMessage > $show_num) {
204 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
206 $location = set_url_var($location,'startMessage',1);
209 header("Location: $location");
211 } elseif(isset($undeleteButton)) {
212 // undelete messages if user isn't using move_to_trash or auto_expunge
213 if (is_array($msg) == 1) {
214 // Removes \Deleted flag from selected messages
217 // If they have selected nothing msg is size one still, but will be an infinite
218 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
219 while ($j < count($msg)) {
221 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
226 header ("Location: $location");
229 displayPageHeader($color, $mailbox);
230 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
232 } elseif (!isset($moveButton)) {
233 // If the delete button was pressed, the moveButton variable will not be set.
234 if (is_array($msg) == 1) {
235 // Marks the selected messages as 'Deleted'
238 // If they have selected nothing msg is size one still, but will be an infinite
239 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
240 while ($j < count($msg)) {
241 if (isset($msg[$i])) {
242 if (isset($markRead)) {
243 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
244 } else if (isset($markUnread)) {
245 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
246 } else if (isset($attache)) {
249 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
256 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
260 if (isset($attache)) {
261 $composesession = attachSelectedMessages($msg, $imapConnection);
262 if ($compose_new_win) {
263 header ("Location: $location&composenew=1&session=$composesession");
266 $location = str_replace('search.php','compose.php',$location);
267 $location = str_replace('right_main.php','compose.php',$location);
268 header ("Location: $location&session=$composesession");
272 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
273 if ($startMessage > $show_num) {
274 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
276 $location = set_url_var($location,'startMessage',1);
279 header ("Location: $location");
283 displayPageHeader($color, $mailbox);
284 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
286 } else { // Move messages
287 // lets check to see if they selected any messages
288 if (is_array($msg) == 1) {
291 // If they have selected nothing msg is size one still, but will be an infinite
292 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
295 if (isset($msg[$i])) {
296 /** check if they would like to move it to the trash folder or not */
297 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
298 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
304 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
309 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
310 if ($startMessage > $show_num) {
311 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
313 $location = set_url_var($location,'startMessage',1);
316 header ("Location: $location");
319 displayPageHeader($color, $mailbox);
320 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
323 // Log out this session
324 sqimap_logout($imapConnection);