make use of the set_url_var extra input parameter $link and use it as
[squirrelmail.git] / src / move_messages.php
1 <?php
2
3 /**
4 * move_messages.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 */
13
14 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
16
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');
22
23 global $compose_new_win;
24
25 if (isset($_SESSION['composesession'])) {
26 $composesession = $_SESSION['composesession'];
27 } else {
28 $composesession = 0;
29 }
30
31 function putSelectedMessagesIntoString($msg) {
32 $j = 0;
33 $i = 0;
34 $firstLoop = true;
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]) {
40 if ($firstLoop != true) {
41 $selectedMessages .= "&amp;";
42 } else {
43 $firstLoop = false;
44 }
45 $selectedMessages .= "selMsg[$j]=$msg[$i]";
46 $j++;
47 }
48 $i++;
49 }
50 }
51
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,
56 $compose_messages;
57
58
59 if (!isset($compose_messages)) {
60 $compose_messages = array();
61 sqsession_register($compose_messages,'compose_messages');
62 }
63
64 if (!$composesession) {
65 $composesession = 1;
66 sqsession_register($composesession,'composesession');
67 } else {
68 $composesession++;
69 sqsession_register($composesession,'composesession');
70 }
71
72 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
73
74 if ($thread_sort_messages || $allow_server_sort) {
75 $start_index=0;
76 } else {
77 $start_index = ($startMessage-1) * $show_num;
78 }
79
80 $i = 0;
81 $j = 0;
82 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
83
84 $composeMessage = new Message();
85 $rfc822_header = new Rfc822Header();
86 $composeMessage->rfc822_header = $rfc822_header;
87 $composeMessage->reply_rfc822_header = '';
88
89 while ($j < count($msg)) {
90 if (isset($msg[$i])) {
91 $id = $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'];
96
97 array_shift($body_a);
98 $body = implode('', $body_a);
99 $body .= "\r\n";
100
101 $localfilename = GenerateRandomString(32, 'FILE', 7);
102 $full_localfilename = "$hashed_attachment_dir/$localfilename";
103
104 $fp = fopen( $full_localfilename, 'wb');
105 fwrite ($fp, $body);
106 fclose($fp);
107 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
108 $full_localfilename);
109 }
110 $j++;
111 }
112 $i++;
113 }
114 $compose_messages[$composesession] = $composeMessage;
115 sqsession_register($compose_messages,'compose_messages');
116 session_write_close();
117 return $composesession;
118 }
119
120
121
122 /* get globals */
123
124 $username = $_SESSION['username'];
125 $key = $_COOKIE['key'];
126 $onetimepad = $_SESSION['onetimepad'];
127 $base_uri = $_SESSION['base_uri'];
128 $delimiter = $_SESSION['delimiter'];
129 if (isset($_GET['mailbox'])) {
130 $mailbox = $_GET['mailbox'];
131 }
132 if (isset($_GET['startMessage'])) {
133 $startMessage = $_GET['startMessage'];
134 }
135 if (isset($_POST['moveButton'])) {
136 $moveButton = $_POST['moveButton'];
137 }
138 if (isset($_POST['msg'])) {
139 $msg = $_POST['msg'];
140 }
141 elseif (isset($_GET['msg'])) {
142 $msg = $_GET['msg'];
143 }
144
145 if (isset($_SESSION['msgs'])) {
146 $msgs = $_SESSION['msgs'];
147 }
148
149
150 if (isset($_POST['expungeButton'])) {
151 $expungeButton = $_POST['expungeButton'];
152 }
153 if (isset($_POST['targetMailbox'])) {
154 $targetMailbox = $_POST['targetMailbox'];
155 }
156 if (isset($_SESSION['lastTargetMailbox'])) {
157 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
158 }
159 if (isset($_POST['expungeButton'])) {
160 $expungeButton = $_POST['expungeButton'];
161 }
162 if (isset($_POST['undeleteButton'])) {
163 $undeleteButton = $_POST['undeleteButton'];
164 }
165 if (isset($_POST['markRead'])) {
166 $markRead = $_POST['markRead'];
167 }
168 if (isset($_POST['markUnread'])) {
169 $markUnread = $_POST['markUnread'];
170 }
171 if (isset($_POST['attache'])) {
172 $attache = $_POST['attache'];
173 }
174
175 if (isset($_POST['location'])) {
176 $location = $_POST['location'];
177 }
178
179 if (isset($_SESSION['composesession'])) {
180 $composesession = $_SESSION['composesession'];
181 }
182 /* end of get globals */
183
184 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
185 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
186
187 $location = set_url_var($location,'composenew',0,false);
188 $location = set_url_var($location,'composesession',0,false);
189 $location = set_url_var($location,'session',0,false);
190
191 /* remember changes to mailbox setting */
192 if (!isset($lastTargetMailbox)) {
193 $lastTargetMailbox = 'INBOX';
194 }
195 if ($targetMailbox != $lastTargetMailbox) {
196 $lastTargetMailbox = $targetMailbox;
197 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
198 }
199 $exception = false;
200 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
201 if(isset($expungeButton)) {
202 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
203 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
204 if ($startMessage > $show_num) {
205 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
206 } else {
207 $location = set_url_var($location,'startMessage',1,false);
208 }
209 }
210 } elseif(isset($undeleteButton)) {
211 // undelete messages if user isn't using move_to_trash or auto_expunge
212 if (is_array($msg) == 1) {
213 // Removes \Deleted flag from selected messages
214 $j = 0;
215 $i = 0;
216 // If they have selected nothing msg is size one still, but will be an infinite
217 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
218 while ($j < count($msg)) {
219 if ($msg[$i]) {
220 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
221 $j++;
222 }
223 $i++;
224 }
225 } else {
226 $exception = true;
227 }
228 } elseif (!isset($moveButton)) {
229 // If the delete button was pressed, the moveButton variable will not be set.
230 if (is_array($msg) == 1) {
231 // Marks the selected messages as 'Deleted'
232 $j = 0;
233 $i = 0;
234 // If they have selected nothing msg is size one still, but will be an infinite
235 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
236 while ($j < count($msg)) {
237 if (isset($msg[$i])) {
238 if (isset($markRead)) {
239 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
240 } else if (isset($markUnread)) {
241 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
242 } else if (isset($attache)) {
243 break;
244 } else {
245 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
246 }
247 $j++;
248 }
249 $i++;
250 }
251 if ($auto_expunge) {
252 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
253 } else {
254 $cnt = 0;
255 }
256 if (isset($attache)) {
257 $composesession = attachSelectedMessages($msg, $imapConnection);
258 $location = set_url_var($location, 'session', $composesession, false);
259 if ($compose_new_win) {
260 $location = set_url_var($location, 'composenew', 1, false);
261 } else {
262 $location = str_replace('search.php','compose.php',$location);
263 $location = str_replace('right_main.php','compose.php',$location);
264 }
265 } else {
266 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
267 if ($startMessage > $show_num) {
268 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
269 } else {
270 $location = set_url_var($location,'startMessage',1, false);
271 }
272 }
273 }
274 } else {
275 $exception = true;
276 }
277 } else { // Move messages
278 // lets check to see if they selected any messages
279 if (is_array($msg) == 1) {
280 $j = 0;
281 $i = 0;
282 // If they have selected nothing msg is size one still, but will be an infinite
283 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
284 $cnt = count($msg);
285 while ($j < $cnt) {
286 if (isset($msg[$i])) {
287 /** check if they would like to move it to the trash folder or not */
288 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
289 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
290 $j++;
291 }
292 $i++;
293 }
294 if ($auto_expunge) {
295 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
296 } else {
297 $cnt = 0;
298 }
299
300 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
301 if ($startMessage > $show_num) {
302 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
303 } else {
304 $location = set_url_var($location,'startMessage',1, false);
305 }
306 }
307 } else {
308 $exception = true;
309 }
310 }
311 // Log out this session
312 sqimap_logout($imapConnection);
313 if ($exception) {
314 displayPageHeader($color, $mailbox);
315 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
316 } else {
317 header("Location: $location");
318 exit;
319 }
320 ?>
321 </BODY></HTML>