Adapted individual flag setting and make it use one imap-call instead of
[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 /* obsolete ?? */
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 if (!isset($compose_messages)) {
59 $compose_messages = array();
60 sqsession_register($compose_messages,'compose_messages');
61 }
62
63 if (!$composesession) {
64 $composesession = 1;
65 sqsession_register($composesession,'composesession');
66 } else {
67 $composesession++;
68 sqsession_register($composesession,'composesession');
69 }
70
71 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
72
73 if ($thread_sort_messages || $allow_server_sort) {
74 $start_index=0;
75 } else {
76 $start_index = ($startMessage-1) * $show_num;
77 }
78
79 $i = 0;
80 $j = 0;
81 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
82
83 $composeMessage = new Message();
84 $rfc822_header = new Rfc822Header();
85 $composeMessage->rfc822_header = $rfc822_header;
86 $composeMessage->reply_rfc822_header = '';
87
88 while ($j < count($msg)) {
89 if (isset($msg[$i])) {
90 $id = $msg[$i];
91 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
92 if ($response = 'OK') {
93 $k = $i + $start_index;
94 $subject = $msgs[$k]['SUBJECT'];
95
96 array_shift($body_a);
97 $body = implode('', $body_a);
98 $body .= "\r\n";
99
100 $localfilename = GenerateRandomString(32, 'FILE', 7);
101 $full_localfilename = "$hashed_attachment_dir/$localfilename";
102
103 $fp = fopen( $full_localfilename, 'wb');
104 fwrite ($fp, $body);
105 fclose($fp);
106 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
107 $full_localfilename);
108 }
109 $j++;
110 }
111 $i++;
112 }
113 $compose_messages[$composesession] = $composeMessage;
114 sqsession_register($compose_messages,'compose_messages');
115 session_write_close();
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 if (isset($_POST['expungeButton'])) {
149 $expungeButton = $_POST['expungeButton'];
150 }
151 if (isset($_POST['targetMailbox'])) {
152 $targetMailbox = $_POST['targetMailbox'];
153 }
154 if (isset($_SESSION['lastTargetMailbox'])) {
155 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
156 }
157 if (isset($_POST['expungeButton'])) {
158 $expungeButton = $_POST['expungeButton'];
159 }
160 if (isset($_POST['undeleteButton'])) {
161 $undeleteButton = $_POST['undeleteButton'];
162 }
163 if (isset($_POST['markRead'])) {
164 $markRead = $_POST['markRead'];
165 }
166 if (isset($_POST['markUnread'])) {
167 $markUnread = $_POST['markUnread'];
168 }
169 if (isset($_POST['attache'])) {
170 $attache = $_POST['attache'];
171 }
172
173 if (isset($_POST['location'])) {
174 $location = $_POST['location'];
175 }
176
177 if (isset($_SESSION['composesession'])) {
178 $composesession = $_SESSION['composesession'];
179 }
180 /* end of get globals */
181
182 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
183 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
184
185 $location = set_url_var($location,'composenew',0,false);
186 $location = set_url_var($location,'composesession',0,false);
187 $location = set_url_var($location,'session',0,false);
188
189 /* remember changes to mailbox setting */
190 if (!isset($lastTargetMailbox)) {
191 $lastTargetMailbox = 'INBOX';
192 }
193 if ($targetMailbox != $lastTargetMailbox) {
194 $lastTargetMailbox = $targetMailbox;
195 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
196 }
197 $exception = false;
198 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
199 if(isset($expungeButton)) {
200 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
201 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
202 if ($startMessage > $show_num) {
203 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
204 } else {
205 $location = set_url_var($location,'startMessage',1,false);
206 }
207 }
208 } elseif(isset($undeleteButton)) {
209 // undelete messages if user isn't using move_to_trash or auto_expunge
210 if (is_array($msg) == 1) {
211 // Removes \Deleted flag from selected messages
212 $j = $i = 0;
213 $id = array();
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 ($msg[$i]) {
218 $id[] = $msg[$i];
219 $j++;
220 }
221 $i++;
222 }
223 if (count($id)) {
224 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
225 }
226 } else {
227 $exception = true;
228 }
229 } elseif (!isset($moveButton)) {
230 // If the delete button was pressed, the moveButton variable will not be set.
231 if (is_array($msg)) {
232 // Marks the selected messages as 'Deleted'
233 $j = $i = $cnt = 0;
234 $id = array();
235 // If they have selected nothing msg is size one still, but will be an infinite
236 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
237 while ($j < count($msg)) {
238 if (isset($msg[$i])) {
239 $id[] = $msg[$i];
240 $j++;
241 }
242 $i++;
243 }
244 if (count($id) && !isset($attache)) {
245 if (isset($markRead)) {
246 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
247 } else if (isset($markUnread)) {
248 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
249 } else {
250 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
251 if ($auto_expunge) {
252 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
253 }
254 }
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)) {
280 $j = $i = 0;
281 $id = array();
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 $id[] = $msg[$i];
288 $j++;
289 }
290 $i++;
291 }
292 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
293 if ($auto_expunge) {
294 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
295 } else {
296 $cnt = 0;
297 }
298
299 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
300 if ($startMessage > $show_num) {
301 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
302 } else {
303 $location = set_url_var($location,'startMessage',1, false);
304 }
305 }
306 } else {
307 $exception = true;
308 }
309 }
310 // Log out this session
311 sqimap_logout($imapConnection);
312 if ($exception) {
313 displayPageHeader($color, $mailbox);
314 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
315 } else {
316 header("Location: $location");
317 exit;
318 }
319 ?>
320 </BODY></HTML>