somehow url vars with &amp are not picked up by PHP
[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
65c3ec94 25function putSelectedMessagesIntoString($msg) {
26 $j = 0;
27 $i = 0;
28 $firstLoop = true;
65c3ec94 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]) {
5e9e90fd 34 if ($firstLoop != true) {
35 $selectedMessages .= "&amp;";
36 } else {
37 $firstLoop = false;
38 }
dd88d31f 39 $selectedMessages .= "selMsg[$j]=$msg[$i]";
05207a68 40 $j++;
65c3ec94 41 }
42 $i++;
43 }
44}
45
acc28bac 46function attachSelectedMessages($msg, $imapConnection) {
5b8d68ca 47 global $mailbox, $username, $attachment_dir, $attachments, $identity,
8c33aaf7 48 $data_dir, $composesession, $lastTargetMailbox, $uid_support,
49 $msgs, $startMessage, $show_num, $thread_sort_messages,
50 $allow_server_sort;
acc28bac 51
acc28bac 52 if (!isset($attachments)) {
21f3c131 53 $attachments = array();
54 session_register('attachments');
acc28bac 55 }
56
fee4465d 57 if (!isset($composesession) ) {
21f3c131 58 $composesession = 1;
fee4465d 59 session_register('composesession');
acc28bac 60 } else {
21f3c131 61 $composesession++;
acc28bac 62 }
63
21f3c131 64 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
acc28bac 65
66 $rem_attachments = array();
67 foreach ($attachments as $info) {
21f3c131 68 if ($info['session'] == $composesession) {
acc28bac 69 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
70 if (file_exists($attached_file)) {
21f3c131 71 unlink($attached_file);
acc28bac 72 }
21f3c131 73 } else {
74 $rem_attachments[] = $info;
75 }
acc28bac 76 }
77
78 $attachments = $rem_attachments;
79
8c33aaf7 80 if ($thread_sort_messages || $allow_server_sort) {
81 $start_index=0;
82 } else {
83 $start_index = ($startMessage-1) * $show_num;
84 }
acc28bac 85
86 $i = 0;
87 $j = 0;
21f3c131 88 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
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);
107 $newAttachment = array();
108 $newAttachment['localfilename'] = $localfilename;
109 $newAttachment['type'] = "message/rfc822";
110 $newAttachment['remotefilename'] = $subject.'.eml';
111 $newAttachment['session'] = $composesession;
112 $attachments[] = $newAttachment;
113 flush();
acc28bac 114 }
8c33aaf7 115 $j++;
116 }
117 $i++;
acc28bac 118 }
6540ee5e 119 setPref($data_dir, $username, 'attachments', serialize($attachments));
acc28bac 120 return $composesession;
121}
122
9c22eb94 123
65c3ec94 124$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 125$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 126
8c33aaf7 127$location = set_url_var($location,'composenew');
128$location = set_url_var($location,'composesession');
129$location = set_url_var($location,'session');
130
cd885078 131/* remember changes to mailbox setting */
132if (!isset($lastTargetMailbox)) {
133 $lastTargetMailbox = 'INBOX';
134}
135if ($targetMailbox != $lastTargetMailbox) {
136 $lastTargetMailbox = $targetMailbox;
137 session_register('lastTargetMailbox');
138}
139
65c3ec94 140// expunge-on-demand if user isn't using move_to_trash or auto_expunge
141if(isset($expungeButton)) {
9c22eb94 142 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
143 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
144 if ($startMessage > $show_num) {
145 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
146 } else {
147 $location = set_url_var($location,'startMessage',1);
148 }
149 }
8c33aaf7 150 header("Location: $location");
65c3ec94 151} elseif(isset($undeleteButton)) {
152 // undelete messages if user isn't using move_to_trash or auto_expunge
65c3ec94 153 if (is_array($msg) == 1) {
154 // Removes \Deleted flag from selected messages
155 $j = 0;
156 $i = 0;
65c3ec94 157 // If they have selected nothing msg is size one still, but will be an infinite
158 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
159 while ($j < count($msg)) {
f9b3e5d9 160 if ($msg[$i]) {
d8a8203a 161 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
8c33aaf7 162 $j++;
f9b3e5d9 163 }
164 $i++;
65c3ec94 165 }
8c33aaf7 166 header ("Location: $location");
65c3ec94 167 } else {
168 displayPageHeader($color, $mailbox);
169 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
170 }
171} elseif (!isset($moveButton)) {
172 // If the delete button was pressed, the moveButton variable will not be set.
173 if (is_array($msg) == 1) {
174 // Marks the selected messages as 'Deleted'
175 $j = 0;
176 $i = 0;
65c3ec94 177 // If they have selected nothing msg is size one still, but will be an infinite
178 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
179 while ($j < count($msg)) {
23fd3c8e 180 if (isset($msg[$i])) {
65c3ec94 181 if (isset($markRead)) {
d8a8203a 182 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
65c3ec94 183 } else if (isset($markUnread)) {
d8a8203a 184 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
acc28bac 185 } else if (isset($attache)) {
186 break;
187 } else {
65c3ec94 188 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
189 }
190 $j++;
2d7d3c2a 191 }
dd88d31f 192 $i++;
65c3ec94 193 }
194 if ($auto_expunge) {
9c22eb94 195 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
196 } else {
197 $cnt = 0;
198 }
8c33aaf7 199 if (isset($attache)) {
acc28bac 200 $composesession = attachSelectedMessages($msg, $imapConnection);
8c33aaf7 201 if ($compose_new_win) {
202 header ("Location: $location&composenew=1&session=$composesession");
acc28bac 203 } else {
8c33aaf7 204 $location = str_replace('search.php','compose.php',$location);
205 $location = str_replace('right_main.php','compose.php',$location);
206 header ("Location: $location&session=$composesession");
acc28bac 207 }
8c33aaf7 208 } else {
9c22eb94 209 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
210 if ($startMessage > $show_num) {
211 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
212 } else {
213 $location = set_url_var($location,'startMessage',1);
214 }
215 }
216 echo $location . ' ' .$show_num. ' '.$cnt;
8c33aaf7 217 header ("Location: $location");
218 }
65c3ec94 219 } else {
220 displayPageHeader($color, $mailbox);
221 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
222 }
223} else { // Move messages
224 // lets check to see if they selected any messages
225 if (is_array($msg) == 1) {
226 $j = 0;
227 $i = 0;
65c3ec94 228 // If they have selected nothing msg is size one still, but will be an infinite
229 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
9c22eb94 230 $cnt = count($msg);
231 while ($j < $cnt) {
23fd3c8e 232 if (isset($msg[$i])) {
65c3ec94 233 /** check if they would like to move it to the trash folder or not */
234 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
d8a8203a 235 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
65c3ec94 236 $j++;
dd88d31f 237 }
238 $i++;
65c3ec94 239 }
8c33aaf7 240 if ($auto_expunge) {
9c22eb94 241 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
242 } else {
243 $cnt = 0;
244 }
245
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 }
8c33aaf7 252 }
253 header ("Location: $location");
65c3ec94 254 } else {
255 displayPageHeader($color, $mailbox);
256 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
257 }
258}
65c3ec94 259// Log out this session
260sqimap_logout($imapConnection);
b40316f9 261
2aa12d5e 262?>
6332704d 263</BODY></HTML>