Do not use mixed get/post variables because this breaks some browers.
[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 function putSelectedMessagesIntoString($msg) {
26 $j = 0;
27 $i = 0;
28 $firstLoop = true;
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])) {
33 if ($msg[$i]) {
34 if ($firstLoop != true) {
35 $selectedMessages .= "&amp;";
36 } else {
37 $firstLoop = false;
38 }
39 $selectedMessages .= "selMsg[$j]=$msg[$i]";
40 $j++;
41 }
42 $i++;
43 }
44 }
45
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,
50 $compose_messages;
51
52
53 if (!isset($compose_messages)) {
54 $compose_messages = array();
55 sqsession_register($compose_messages,'compose_messages');
56 }
57
58 if (!isset($composesession) ) {
59 $composesession = 1;
60 sqsession_register($composesession,'composesession');
61 } else {
62 $composesession++;
63 }
64
65 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
66
67 if ($thread_sort_messages || $allow_server_sort) {
68 $start_index=0;
69 } else {
70 $start_index = ($startMessage-1) * $show_num;
71 }
72
73 $i = 0;
74 $j = 0;
75 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
76
77 $composeMessage = new Message();
78 $rfc822_header = new Rfc822Header();
79 $composeMessage->rfc822_header = $rfc822_header;
80 $composeMessage->reply_rfc822_header = '';
81
82 while ($j < count($msg)) {
83 if (isset($msg[$i])) {
84 $id = $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'];
89
90 array_shift($body_a);
91 $body = implode('', $body_a);
92 $body .= "\r\n";
93
94 $localfilename = GenerateRandomString(32, 'FILE', 7);
95 $full_localfilename = "$hashed_attachment_dir/$localfilename";
96
97 $fp = fopen( $full_localfilename, 'wb');
98 fwrite ($fp, $body);
99 fclose($fp);
100 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
101 $full_localfilename);
102 }
103 $j++;
104 }
105 $i++;
106 }
107 $compose_messages[$composesession] = $composeMessage;
108 sqsession_register($compose_messages,'compose_messages');
109 return $composesession;
110 }
111
112
113
114 /* get globals */
115
116 $username = $_SESSION['username'];
117 $key = $_COOKIE['key'];
118 $onetimepad = $_SESSION['onetimepad'];
119 $base_uri = $_SESSION['base_uri'];
120 $delimiter = $_SESSION['delimiter'];
121
122 if (isset($_POST['mailbox'])) {
123 $mailbox = $_POST['mailbox'];
124 }
125 if (isset($_POST['startMessage'])) {
126 $startMessage = $_POST['startMessage'];
127 }
128 if (isset($_POST['moveButton'])) {
129 $moveButton = $_POST['moveButton'];
130 }
131 if (isset($_POST['msg'])) {
132 $msg = $_POST['msg'];
133 }
134 if (isset($_POST['expungeButton'])) {
135 $expungeButton = $_POST['expungeButton'];
136 }
137 if (isset($_POST['targetMailbox'])) {
138 $targetMailbox = $_POST['targetMailbox'];
139 }
140 if (isset($_SESSION['lastTargetMailbox'])) {
141 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
142 }
143 if (isset($_POST['expungeButton'])) {
144 $expungeButton = $_POST['expungeButton'];
145 }
146 if (isset($_POST['undeleteButton'])) {
147 $undeleteButton = $_POST['undeleteButton'];
148 }
149 if (isset($_POST['markRead'])) {
150 $markRead = $_POST['markRead'];
151 }
152 if (isset($_POST['markUnread'])) {
153 $markUnread = $_POST['markUnread'];
154 }
155 if (isset($_POST['attache'])) {
156 $attache = $_POST['attache'];
157 }
158
159 /* end of get globals */
160
161 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
162 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
163
164 $location = set_url_var($location,'composenew');
165 $location = set_url_var($location,'composesession');
166 $location = set_url_var($location,'session');
167
168 /* remember changes to mailbox setting */
169 if (!isset($lastTargetMailbox)) {
170 $lastTargetMailbox = 'INBOX';
171 }
172 if ($targetMailbox != $lastTargetMailbox) {
173 $lastTargetMailbox = $targetMailbox;
174 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
175 }
176
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);
183 } else {
184 $location = set_url_var($location,'startMessage',1);
185 }
186 }
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
192 $j = 0;
193 $i = 0;
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)) {
197 if ($msg[$i]) {
198 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
199 $j++;
200 }
201 $i++;
202 }
203 header ("Location: $location");
204 } else {
205 displayPageHeader($color, $mailbox);
206 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
207 }
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'
212 $j = 0;
213 $i = 0;
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)) {
223 break;
224 } else {
225 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
226 }
227 $j++;
228 }
229 $i++;
230 }
231 if ($auto_expunge) {
232 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
233 } else {
234 $cnt = 0;
235 }
236 if (isset($attache)) {
237 $composesession = attachSelectedMessages($msg, $imapConnection);
238 if ($compose_new_win) {
239 header ("Location: $location&composenew=1&session=$composesession");
240 } else {
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");
244 }
245 } else {
246 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
247 if ($startMessage > $show_num) {
248 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
249 } else {
250 $location = set_url_var($location,'startMessage',1);
251 }
252 }
253 header ("Location: $location");
254 }
255 } else {
256 displayPageHeader($color, $mailbox);
257 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
258 }
259 } else { // Move messages
260 // lets check to see if they selected any messages
261 if (is_array($msg) == 1) {
262 $j = 0;
263 $i = 0;
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.
266 $cnt = count($msg);
267 while ($j < $cnt) {
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);
272 $j++;
273 }
274 $i++;
275 }
276 if ($auto_expunge) {
277 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
278 } else {
279 $cnt = 0;
280 }
281
282 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
283 if ($startMessage > $show_num) {
284 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
285 } else {
286 $location = set_url_var($location,'startMessage',1);
287 }
288 }
289 header ("Location: $location");
290 } else {
291 displayPageHeader($color, $mailbox);
292 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
293 }
294 }
295 // Log out this session
296 sqimap_logout($imapConnection);
297
298 ?>
299 </BODY></HTML>