rg=0 fixes; ReplyCitation fix, Draft save fix
[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 if (isset($_GET['mailbox'])) {
122 $mailbox = $_GET['mailbox'];
123 }
124 if (isset($_GET['startMessage'])) {
125 $startMessage = $_GET['startMessage'];
126 }
127 if (isset($_POST['moveButton'])) {
128 $moveButton = $_POST['moveButton'];
129 }
130 if (isset($_POST['msg'])) {
131 $msg = $_POST['msg'];
132 }
133 elseif (isset($_GET['msg'])) {
134 $msg = $_GET['msg'];
135 }
136 if (isset($_POST['expungeButton'])) {
137 $expungeButton = $_POST['expungeButton'];
138 }
139 if (isset($_POST['targetMailbox'])) {
140 $targetMailbox = $_POST['targetMailbox'];
141 }
142 if (isset($_SESSION['lastTargetMailbox'])) {
143 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
144 }
145 if (isset($_POST['expungeButton'])) {
146 $expungeButton = $_POST['expungeButton'];
147 }
148 if (isset($_POST['undeleteButton'])) {
149 $undeleteButton = $_POST['undeleteButton'];
150 }
151 if (isset($_POST['markRead'])) {
152 $markRead = $_POST['markRead'];
153 }
154 if (isset($_POST['markUnread'])) {
155 $markUnread = $_POST['markUnread'];
156 }
157 if (isset($_POST['attache'])) {
158 $attache = $_POST['attache'];
159 }
160
161 if (isset($_POST['location'])) {
162 $location = $_POST['location'];
163 }
164
165 /* end of get globals */
166
167 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
168 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
169
170 $location = set_url_var($location,'composenew');
171 $location = set_url_var($location,'composesession');
172 $location = set_url_var($location,'session');
173
174 /* remember changes to mailbox setting */
175 if (!isset($lastTargetMailbox)) {
176 $lastTargetMailbox = 'INBOX';
177 }
178 if ($targetMailbox != $lastTargetMailbox) {
179 $lastTargetMailbox = $targetMailbox;
180 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
181 }
182
183 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
184 if(isset($expungeButton)) {
185 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
186 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
187 if ($startMessage > $show_num) {
188 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
189 } else {
190 $location = set_url_var($location,'startMessage',1);
191 }
192 }
193 header("Location: $location");
194 exit;
195 } elseif(isset($undeleteButton)) {
196 // undelete messages if user isn't using move_to_trash or auto_expunge
197 if (is_array($msg) == 1) {
198 // Removes \Deleted flag from selected messages
199 $j = 0;
200 $i = 0;
201 // If they have selected nothing msg is size one still, but will be an infinite
202 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
203 while ($j < count($msg)) {
204 if ($msg[$i]) {
205 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
206 $j++;
207 }
208 $i++;
209 }
210 header ("Location: $location");
211 exit;
212 } else {
213 displayPageHeader($color, $mailbox);
214 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
215 }
216 } elseif (!isset($moveButton)) {
217 // If the delete button was pressed, the moveButton variable will not be set.
218 if (is_array($msg) == 1) {
219 // Marks the selected messages as 'Deleted'
220 $j = 0;
221 $i = 0;
222 // If they have selected nothing msg is size one still, but will be an infinite
223 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
224 while ($j < count($msg)) {
225 if (isset($msg[$i])) {
226 if (isset($markRead)) {
227 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
228 } else if (isset($markUnread)) {
229 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
230 } else if (isset($attache)) {
231 break;
232 } else {
233 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
234 }
235 $j++;
236 }
237 $i++;
238 }
239 if ($auto_expunge) {
240 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
241 } else {
242 $cnt = 0;
243 }
244 if (isset($attache)) {
245 $composesession = attachSelectedMessages($msg, $imapConnection);
246 if ($compose_new_win) {
247 header ("Location: $location&composenew=1&session=$composesession");
248 exit;
249 } else {
250 $location = str_replace('search.php','compose.php',$location);
251 $location = str_replace('right_main.php','compose.php',$location);
252 header ("Location: $location&session=$composesession");
253 exit;
254 }
255 } else {
256 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
257 if ($startMessage > $show_num) {
258 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
259 } else {
260 $location = set_url_var($location,'startMessage',1);
261 }
262 }
263 header ("Location: $location");
264 exit;
265 }
266 } else {
267 displayPageHeader($color, $mailbox);
268 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
269 }
270 } else { // Move messages
271 // lets check to see if they selected any messages
272 if (is_array($msg) == 1) {
273 $j = 0;
274 $i = 0;
275 // If they have selected nothing msg is size one still, but will be an infinite
276 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
277 $cnt = count($msg);
278 while ($j < $cnt) {
279 if (isset($msg[$i])) {
280 /** check if they would like to move it to the trash folder or not */
281 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
282 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
283 $j++;
284 }
285 $i++;
286 }
287 if ($auto_expunge) {
288 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
289 } else {
290 $cnt = 0;
291 }
292
293 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
294 if ($startMessage > $show_num) {
295 $location = set_url_var($location,'startMessage',$startMessage-$show_num);
296 } else {
297 $location = set_url_var($location,'startMessage',1);
298 }
299 }
300 header ("Location: $location");
301 exit;
302 } else {
303 displayPageHeader($color, $mailbox);
304 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
305 }
306 }
307 // Log out this session
308 sqimap_logout($imapConnection);
309 ?>
310 </BODY></HTML>