Added var $link to set_url_var so we correctly can set the new uri with
[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 return $composesession;
117 }
118
119
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'];
128 if (isset($_GET['mailbox'])) {
129 $mailbox = $_GET['mailbox'];
130 }
131 if (isset($_GET['startMessage'])) {
132 $startMessage = $_GET['startMessage'];
133 }
134 if (isset($_POST['moveButton'])) {
135 $moveButton = $_POST['moveButton'];
136 }
137 if (isset($_POST['msg'])) {
138 $msg = $_POST['msg'];
139 }
140 elseif (isset($_GET['msg'])) {
141 $msg = $_GET['msg'];
142 }
143
144 if (isset($_SESSION['msgs'])) {
145 $msgs = $_SESSION['msgs'];
146 }
147
148
149 if (isset($_POST['expungeButton'])) {
150 $expungeButton = $_POST['expungeButton'];
151 }
152 if (isset($_POST['targetMailbox'])) {
153 $targetMailbox = $_POST['targetMailbox'];
154 }
155 if (isset($_SESSION['lastTargetMailbox'])) {
156 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
157 }
158 if (isset($_POST['expungeButton'])) {
159 $expungeButton = $_POST['expungeButton'];
160 }
161 if (isset($_POST['undeleteButton'])) {
162 $undeleteButton = $_POST['undeleteButton'];
163 }
164 if (isset($_POST['markRead'])) {
165 $markRead = $_POST['markRead'];
166 }
167 if (isset($_POST['markUnread'])) {
168 $markUnread = $_POST['markUnread'];
169 }
170 if (isset($_POST['attache'])) {
171 $attache = $_POST['attache'];
172 }
173
174 if (isset($_POST['location'])) {
175 $location = $_POST['location'];
176 }
177
178 if (isset($_SESSION['composesession'])) {
179 $composesession = $_SESSION['composesession'];
180 }
181 /* end of get globals */
182
183 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
184 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
185
186 $location = set_url_var($location,'composenew');
187 $location = set_url_var($location,'composesession');
188 $location = set_url_var($location,'session');
189
190 /* remember changes to mailbox setting */
191 if (!isset($lastTargetMailbox)) {
192 $lastTargetMailbox = 'INBOX';
193 }
194 if ($targetMailbox != $lastTargetMailbox) {
195 $lastTargetMailbox = $targetMailbox;
196 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
197 }
198
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);
205 } else {
206 $location = set_url_var($location,'startMessage',1);
207 }
208 }
209 header("Location: $location");
210 exit;
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
215 $j = 0;
216 $i = 0;
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)) {
220 if ($msg[$i]) {
221 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
222 $j++;
223 }
224 $i++;
225 }
226 header ("Location: $location");
227 exit;
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;
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)) {
247 break;
248 } else {
249 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
250 }
251 $j++;
252 }
253 $i++;
254 }
255 if ($auto_expunge) {
256 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
257 } else {
258 $cnt = 0;
259 }
260 if (isset($attache)) {
261 $composesession = attachSelectedMessages($msg, $imapConnection);
262 if ($compose_new_win) {
263 header ("Location: $location&composenew=1&session=$composesession");
264 exit;
265 } else {
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");
269 exit;
270 }
271 } else {
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 }
279 header ("Location: $location");
280 exit;
281 }
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;
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.
293 $cnt = count($msg);
294 while ($j < $cnt) {
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);
299 $j++;
300 }
301 $i++;
302 }
303 if ($auto_expunge) {
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 }
315 }
316 header ("Location: $location");
317 exit;
318 } else {
319 displayPageHeader($color, $mailbox);
320 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
321 }
322 }
323 // Log out this session
324 sqimap_logout($imapConnection);
325 ?>
326 </BODY></HTML>