59177427 |
1 | <?php |
895905c0 |
2 | |
35586184 |
3 | /** |
4 | * move_messages.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 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. */ |
15 | define('SM_PATH','../'); |
16 | |
17 | /* SquirrelMail required files. */ |
08185f2a |
18 | require_once(SM_PATH . 'include/validate.php'); |
86725763 |
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 | |
acc28bac |
23 | global $compose_new_win; |
24 | |
07abb0f3 |
25 | if (isset($_SESSION['composesession'])) { |
26 | $composesession = $_SESSION['composesession']; |
27 | } else { |
28 | $composesession = 0; |
29 | } |
ce2c797b |
30 | /* obsolete ?? */ |
65c3ec94 |
31 | function putSelectedMessagesIntoString($msg) { |
32 | $j = 0; |
33 | $i = 0; |
34 | $firstLoop = true; |
65c3ec94 |
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]) { |
5e9e90fd |
40 | if ($firstLoop != true) { |
41 | $selectedMessages .= "&"; |
42 | } else { |
43 | $firstLoop = false; |
44 | } |
dd88d31f |
45 | $selectedMessages .= "selMsg[$j]=$msg[$i]"; |
05207a68 |
46 | $j++; |
65c3ec94 |
47 | } |
48 | $i++; |
49 | } |
50 | } |
51 | |
acc28bac |
52 | function attachSelectedMessages($msg, $imapConnection) { |
a2a30ee7 |
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 | |
a2a30ee7 |
58 | if (!isset($compose_messages)) { |
59 | $compose_messages = array(); |
60 | sqsession_register($compose_messages,'compose_messages'); |
acc28bac |
61 | } |
62 | |
07abb0f3 |
63 | if (!$composesession) { |
21f3c131 |
64 | $composesession = 1; |
a2a30ee7 |
65 | sqsession_register($composesession,'composesession'); |
acc28bac |
66 | } else { |
21f3c131 |
67 | $composesession++; |
07abb0f3 |
68 | sqsession_register($composesession,'composesession'); |
acc28bac |
69 | } |
70 | |
21f3c131 |
71 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession); |
acc28bac |
72 | |
8c33aaf7 |
73 | if ($thread_sort_messages || $allow_server_sort) { |
74 | $start_index=0; |
75 | } else { |
76 | $start_index = ($startMessage-1) * $show_num; |
77 | } |
acc28bac |
78 | |
79 | $i = 0; |
80 | $j = 0; |
21f3c131 |
81 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
a2a30ee7 |
82 | |
83 | $composeMessage = new Message(); |
84 | $rfc822_header = new Rfc822Header(); |
85 | $composeMessage->rfc822_header = $rfc822_header; |
86 | $composeMessage->reply_rfc822_header = ''; |
87 | |
acc28bac |
88 | while ($j < count($msg)) { |
89 | if (isset($msg[$i])) { |
21f3c131 |
90 | $id = $msg[$i]; |
5b8d68ca |
91 | $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support); |
21f3c131 |
92 | if ($response = 'OK') { |
8c33aaf7 |
93 | $k = $i + $start_index; |
94 | $subject = $msgs[$k]['SUBJECT']; |
21f3c131 |
95 | |
8c33aaf7 |
96 | array_shift($body_a); |
97 | $body = implode('', $body_a); |
98 | $body .= "\r\n"; |
21f3c131 |
99 | |
8c33aaf7 |
100 | $localfilename = GenerateRandomString(32, 'FILE', 7); |
101 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
102 | |
81d79073 |
103 | $fp = fopen( $full_localfilename, 'wb'); |
8c33aaf7 |
104 | fwrite ($fp, $body); |
105 | fclose($fp); |
a2a30ee7 |
106 | $composeMessage->initAttachment('message/rfc822',$subject.'.eml', |
107 | $full_localfilename); |
acc28bac |
108 | } |
8c33aaf7 |
109 | $j++; |
110 | } |
111 | $i++; |
acc28bac |
112 | } |
a2a30ee7 |
113 | $compose_messages[$composesession] = $composeMessage; |
87d5f199 |
114 | sqsession_register($compose_messages,'compose_messages'); |
115 | session_write_close(); |
acc28bac |
116 | return $composesession; |
117 | } |
118 | |
9c22eb94 |
119 | |
9837b0a5 |
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']; |
2e716bd9 |
128 | if (isset($_GET['mailbox'])) { |
129 | $mailbox = $_GET['mailbox']; |
9837b0a5 |
130 | } |
2e716bd9 |
131 | if (isset($_GET['startMessage'])) { |
132 | $startMessage = $_GET['startMessage']; |
9837b0a5 |
133 | } |
134 | if (isset($_POST['moveButton'])) { |
135 | $moveButton = $_POST['moveButton']; |
136 | } |
137 | if (isset($_POST['msg'])) { |
138 | $msg = $_POST['msg']; |
139 | } |
2e716bd9 |
140 | elseif (isset($_GET['msg'])) { |
141 | $msg = $_GET['msg']; |
142 | } |
882cd908 |
143 | |
144 | if (isset($_SESSION['msgs'])) { |
145 | $msgs = $_SESSION['msgs']; |
146 | } |
147 | |
9837b0a5 |
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 | |
c083ce9a |
173 | if (isset($_POST['location'])) { |
174 | $location = $_POST['location']; |
175 | } |
176 | |
882cd908 |
177 | if (isset($_SESSION['composesession'])) { |
178 | $composesession = $_SESSION['composesession']; |
179 | } |
9837b0a5 |
180 | /* end of get globals */ |
181 | |
65c3ec94 |
182 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
9c22eb94 |
183 | $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox); |
65c3ec94 |
184 | |
87d5f199 |
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); |
8c33aaf7 |
188 | |
cd885078 |
189 | /* remember changes to mailbox setting */ |
190 | if (!isset($lastTargetMailbox)) { |
191 | $lastTargetMailbox = 'INBOX'; |
192 | } |
193 | if ($targetMailbox != $lastTargetMailbox) { |
194 | $lastTargetMailbox = $targetMailbox; |
9837b0a5 |
195 | sqsession_register($lastTargetMailbox, 'lastTargetMailbox'); |
cd885078 |
196 | } |
87d5f199 |
197 | $exception = false; |
65c3ec94 |
198 | // expunge-on-demand if user isn't using move_to_trash or auto_expunge |
199 | if(isset($expungeButton)) { |
9c22eb94 |
200 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
201 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
202 | if ($startMessage > $show_num) { |
87d5f199 |
203 | $location = set_url_var($location,'startMessage',$startMessage-$show_num,false); |
9c22eb94 |
204 | } else { |
87d5f199 |
205 | $location = set_url_var($location,'startMessage',1,false); |
9c22eb94 |
206 | } |
207 | } |
65c3ec94 |
208 | } elseif(isset($undeleteButton)) { |
209 | // undelete messages if user isn't using move_to_trash or auto_expunge |
65c3ec94 |
210 | if (is_array($msg) == 1) { |
211 | // Removes \Deleted flag from selected messages |
ce2c797b |
212 | $j = $i = 0; |
213 | $id = array(); |
65c3ec94 |
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)) { |
f9b3e5d9 |
217 | if ($msg[$i]) { |
ce2c797b |
218 | $id[] = $msg[$i]; |
8c33aaf7 |
219 | $j++; |
f9b3e5d9 |
220 | } |
221 | $i++; |
65c3ec94 |
222 | } |
ce2c797b |
223 | if (count($id)) { |
224 | sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true); |
225 | } |
65c3ec94 |
226 | } else { |
87d5f199 |
227 | $exception = true; |
65c3ec94 |
228 | } |
229 | } elseif (!isset($moveButton)) { |
230 | // If the delete button was pressed, the moveButton variable will not be set. |
ce2c797b |
231 | if (is_array($msg)) { |
65c3ec94 |
232 | // Marks the selected messages as 'Deleted' |
ce2c797b |
233 | $j = $i = $cnt = 0; |
234 | $id = array(); |
65c3ec94 |
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)) { |
23fd3c8e |
238 | if (isset($msg[$i])) { |
ce2c797b |
239 | $id[] = $msg[$i]; |
65c3ec94 |
240 | $j++; |
2d7d3c2a |
241 | } |
dd88d31f |
242 | $i++; |
65c3ec94 |
243 | } |
ce2c797b |
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 | } |
8c33aaf7 |
256 | if (isset($attache)) { |
acc28bac |
257 | $composesession = attachSelectedMessages($msg, $imapConnection); |
87d5f199 |
258 | $location = set_url_var($location, 'session', $composesession, false); |
8c33aaf7 |
259 | if ($compose_new_win) { |
87d5f199 |
260 | $location = set_url_var($location, 'composenew', 1, false); |
acc28bac |
261 | } else { |
8c33aaf7 |
262 | $location = str_replace('search.php','compose.php',$location); |
263 | $location = str_replace('right_main.php','compose.php',$location); |
acc28bac |
264 | } |
8c33aaf7 |
265 | } else { |
9c22eb94 |
266 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
267 | if ($startMessage > $show_num) { |
87d5f199 |
268 | $location = set_url_var($location,'startMessage',$startMessage-$show_num, false); |
9c22eb94 |
269 | } else { |
87d5f199 |
270 | $location = set_url_var($location,'startMessage',1, false); |
9c22eb94 |
271 | } |
272 | } |
8c33aaf7 |
273 | } |
65c3ec94 |
274 | } else { |
87d5f199 |
275 | $exception = true; |
65c3ec94 |
276 | } |
277 | } else { // Move messages |
278 | // lets check to see if they selected any messages |
ce2c797b |
279 | if (is_array($msg)) { |
280 | $j = $i = 0; |
281 | $id = array(); |
65c3ec94 |
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. |
9c22eb94 |
284 | $cnt = count($msg); |
285 | while ($j < $cnt) { |
23fd3c8e |
286 | if (isset($msg[$i])) { |
ce2c797b |
287 | $id[] = $msg[$i]; |
65c3ec94 |
288 | $j++; |
dd88d31f |
289 | } |
290 | $i++; |
65c3ec94 |
291 | } |
ce2c797b |
292 | sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox); |
8c33aaf7 |
293 | if ($auto_expunge) { |
9c22eb94 |
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) { |
87d5f199 |
301 | $location = set_url_var($location,'startMessage',$startMessage-$show_num, false); |
9c22eb94 |
302 | } else { |
87d5f199 |
303 | $location = set_url_var($location,'startMessage',1, false); |
9c22eb94 |
304 | } |
8c33aaf7 |
305 | } |
65c3ec94 |
306 | } else { |
87d5f199 |
307 | $exception = true; |
65c3ec94 |
308 | } |
309 | } |
65c3ec94 |
310 | // Log out this session |
311 | sqimap_logout($imapConnection); |
87d5f199 |
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 | } |
2aa12d5e |
319 | ?> |
6332704d |
320 | </BODY></HTML> |