Added var $link to set_url_var so we correctly can set the new uri with
[squirrelmail.git] / src / move_messages.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * move_messages.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * Enables message moving between folders on the IMAP server.
10 *
11 * $Id$
12 */
ef870322 13
86725763 14/* Path for SquirrelMail required files. */
15define('SM_PATH','../');
16
17/* SquirrelMail required files. */
08185f2a 18require_once(SM_PATH . 'include/validate.php');
86725763 19require_once(SM_PATH . 'functions/display_messages.php');
20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/html.php');
22
acc28bac 23global $compose_new_win;
24
07abb0f3 25if (isset($_SESSION['composesession'])) {
26 $composesession = $_SESSION['composesession'];
27} else {
28 $composesession = 0;
29}
30
65c3ec94 31function putSelectedMessagesIntoString($msg) {
32 $j = 0;
33 $i = 0;
34 $firstLoop = true;
65c3ec94 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])) {
39 if ($msg[$i]) {
5e9e90fd 40 if ($firstLoop != true) {
41 $selectedMessages .= "&amp;";
42 } else {
43 $firstLoop = false;
44 }
dd88d31f 45 $selectedMessages .= "selMsg[$j]=$msg[$i]";
05207a68 46 $j++;
65c3ec94 47 }
48 $i++;
49 }
50}
51
acc28bac 52function attachSelectedMessages($msg, $imapConnection) {
a2a30ee7 53 global $username, $attachment_dir,
54 $data_dir, $composesession, $uid_support,
55 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
56 $compose_messages;
57
58
59 if (!isset($compose_messages)) {
60 $compose_messages = array();
61 sqsession_register($compose_messages,'compose_messages');
acc28bac 62 }
63
07abb0f3 64 if (!$composesession) {
21f3c131 65 $composesession = 1;
a2a30ee7 66 sqsession_register($composesession,'composesession');
acc28bac 67 } else {
21f3c131 68 $composesession++;
07abb0f3 69 sqsession_register($composesession,'composesession');
acc28bac 70 }
71
21f3c131 72 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
acc28bac 73
8c33aaf7 74 if ($thread_sort_messages || $allow_server_sort) {
75 $start_index=0;
76 } else {
77 $start_index = ($startMessage-1) * $show_num;
78 }
acc28bac 79
80 $i = 0;
81 $j = 0;
21f3c131 82 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a2a30ee7 83
84 $composeMessage = new Message();
85 $rfc822_header = new Rfc822Header();
86 $composeMessage->rfc822_header = $rfc822_header;
87 $composeMessage->reply_rfc822_header = '';
88
acc28bac 89 while ($j < count($msg)) {
90 if (isset($msg[$i])) {
21f3c131 91 $id = $msg[$i];
5b8d68ca 92 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
21f3c131 93 if ($response = 'OK') {
8c33aaf7 94 $k = $i + $start_index;
95 $subject = $msgs[$k]['SUBJECT'];
21f3c131 96
8c33aaf7 97 array_shift($body_a);
98 $body = implode('', $body_a);
99 $body .= "\r\n";
21f3c131 100
8c33aaf7 101 $localfilename = GenerateRandomString(32, 'FILE', 7);
102 $full_localfilename = "$hashed_attachment_dir/$localfilename";
103
81d79073 104 $fp = fopen( $full_localfilename, 'wb');
8c33aaf7 105 fwrite ($fp, $body);
106 fclose($fp);
a2a30ee7 107 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
108 $full_localfilename);
acc28bac 109 }
8c33aaf7 110 $j++;
111 }
112 $i++;
acc28bac 113 }
a2a30ee7 114 $compose_messages[$composesession] = $composeMessage;
115 sqsession_register($compose_messages,'compose_messages');
acc28bac 116 return $composesession;
117}
118
9c22eb94 119
9837b0a5 120
121/* get globals */
122
123$username = $_SESSION['username'];
124$key = $_COOKIE['key'];
125$onetimepad = $_SESSION['onetimepad'];
126$base_uri = $_SESSION['base_uri'];
127$delimiter = $_SESSION['delimiter'];
2e716bd9 128if (isset($_GET['mailbox'])) {
129 $mailbox = $_GET['mailbox'];
9837b0a5 130}
2e716bd9 131if (isset($_GET['startMessage'])) {
132 $startMessage = $_GET['startMessage'];
9837b0a5 133}
134if (isset($_POST['moveButton'])) {
135 $moveButton = $_POST['moveButton'];
136}
137if (isset($_POST['msg'])) {
138 $msg = $_POST['msg'];
139}
2e716bd9 140elseif (isset($_GET['msg'])) {
141 $msg = $_GET['msg'];
142}
882cd908 143
144if (isset($_SESSION['msgs'])) {
145 $msgs = $_SESSION['msgs'];
146}
147
148
9837b0a5 149if (isset($_POST['expungeButton'])) {
150 $expungeButton = $_POST['expungeButton'];
151}
152if (isset($_POST['targetMailbox'])) {
153 $targetMailbox = $_POST['targetMailbox'];
154}
155if (isset($_SESSION['lastTargetMailbox'])) {
156 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
157}
158if (isset($_POST['expungeButton'])) {
159 $expungeButton = $_POST['expungeButton'];
160}
161if (isset($_POST['undeleteButton'])) {
162 $undeleteButton = $_POST['undeleteButton'];
163}
164if (isset($_POST['markRead'])) {
165 $markRead = $_POST['markRead'];
166}
167if (isset($_POST['markUnread'])) {
168 $markUnread = $_POST['markUnread'];
169}
170if (isset($_POST['attache'])) {
171 $attache = $_POST['attache'];
172}
173
c083ce9a 174if (isset($_POST['location'])) {
175 $location = $_POST['location'];
176}
177
882cd908 178if (isset($_SESSION['composesession'])) {
179 $composesession = $_SESSION['composesession'];
180}
9837b0a5 181/* end of get globals */
182
65c3ec94 183$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 184$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 185
8c33aaf7 186$location = set_url_var($location,'composenew');
187$location = set_url_var($location,'composesession');
188$location = set_url_var($location,'session');
189
cd885078 190/* remember changes to mailbox setting */
191if (!isset($lastTargetMailbox)) {
192 $lastTargetMailbox = 'INBOX';
193}
194if ($targetMailbox != $lastTargetMailbox) {
195 $lastTargetMailbox = $targetMailbox;
9837b0a5 196 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 197}
198
65c3ec94 199// expunge-on-demand if user isn't using move_to_trash or auto_expunge
200if(isset($expungeButton)) {
9c22eb94 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);
205 } else {
206 $location = set_url_var($location,'startMessage',1);
207 }
208 }
8c33aaf7 209 header("Location: $location");
c083ce9a 210 exit;
65c3ec94 211} elseif(isset($undeleteButton)) {
212 // undelete messages if user isn't using move_to_trash or auto_expunge
65c3ec94 213 if (is_array($msg) == 1) {
214 // Removes \Deleted flag from selected messages
215 $j = 0;
216 $i = 0;
65c3ec94 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)) {
f9b3e5d9 220 if ($msg[$i]) {
d8a8203a 221 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
8c33aaf7 222 $j++;
f9b3e5d9 223 }
224 $i++;
65c3ec94 225 }
8c33aaf7 226 header ("Location: $location");
c083ce9a 227 exit;
65c3ec94 228 } else {
229 displayPageHeader($color, $mailbox);
230 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
231 }
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'
236 $j = 0;
237 $i = 0;
65c3ec94 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)) {
23fd3c8e 241 if (isset($msg[$i])) {
65c3ec94 242 if (isset($markRead)) {
d8a8203a 243 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
65c3ec94 244 } else if (isset($markUnread)) {
d8a8203a 245 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
acc28bac 246 } else if (isset($attache)) {
247 break;
248 } else {
65c3ec94 249 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
250 }
251 $j++;
2d7d3c2a 252 }
dd88d31f 253 $i++;
65c3ec94 254 }
255 if ($auto_expunge) {
9c22eb94 256 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
257 } else {
258 $cnt = 0;
259 }
8c33aaf7 260 if (isset($attache)) {
acc28bac 261 $composesession = attachSelectedMessages($msg, $imapConnection);
8c33aaf7 262 if ($compose_new_win) {
263 header ("Location: $location&composenew=1&session=$composesession");
c083ce9a 264 exit;
acc28bac 265 } else {
8c33aaf7 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");
c083ce9a 269 exit;
acc28bac 270 }
8c33aaf7 271 } else {
9c22eb94 272 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
273 if ($startMessage > $show_num) {
274 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
275 } else {
276 $location = set_url_var($location,'startMessage',1);
277 }
278 }
8c33aaf7 279 header ("Location: $location");
c083ce9a 280 exit;
8c33aaf7 281 }
65c3ec94 282 } else {
283 displayPageHeader($color, $mailbox);
284 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
285 }
286} else { // Move messages
287 // lets check to see if they selected any messages
288 if (is_array($msg) == 1) {
289 $j = 0;
290 $i = 0;
65c3ec94 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.
9c22eb94 293 $cnt = count($msg);
294 while ($j < $cnt) {
23fd3c8e 295 if (isset($msg[$i])) {
65c3ec94 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);
d8a8203a 298 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
65c3ec94 299 $j++;
dd88d31f 300 }
301 $i++;
65c3ec94 302 }
8c33aaf7 303 if ($auto_expunge) {
9c22eb94 304 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
305 } else {
306 $cnt = 0;
307 }
308
309 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
310 if ($startMessage > $show_num) {
311 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
312 } else {
313 $location = set_url_var($location,'startMessage',1);
314 }
8c33aaf7 315 }
c083ce9a 316 header ("Location: $location");
317 exit;
65c3ec94 318 } else {
319 displayPageHeader($color, $mailbox);
320 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
321 }
322}
65c3ec94 323// Log out this session
324sqimap_logout($imapConnection);
2aa12d5e 325?>
6332704d 326</BODY></HTML>