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'); |
8650e9e1 |
19 | require_once(SM_PATH . 'functions/global.php'); |
86725763 |
20 | require_once(SM_PATH . 'functions/display_messages.php'); |
21 | require_once(SM_PATH . 'functions/imap.php'); |
22 | require_once(SM_PATH . 'functions/html.php'); |
23 | |
acc28bac |
24 | global $compose_new_win; |
25 | |
1e12d1ff |
26 | if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) { |
27 | $composesession = 0; |
28 | } |
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 */ |
1e12d1ff |
122 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
123 | sqgetGlobalVar('username', $username, SQ_SESSION); |
124 | sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION); |
125 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
126 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
127 | |
128 | sqgetGlobalVar('mailbox', $mailbox); |
129 | sqgetGlobalVar('startMessage', $startMessage); |
130 | sqgetGlobalVar('msg', $msg); |
131 | |
132 | sqgetGlobalVar('msgs', $msgs, SQ_SESSION); |
133 | sqgetGlobalVar('composesession', $composesession, SQ_SESSION); |
134 | sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION); |
135 | |
136 | sqgetGlobalVar('moveButton', $moveButton, SQ_POST); |
137 | sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST); |
138 | sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST); |
139 | sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST); |
140 | sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST); |
141 | sqgetGlobalVar('markRead', $markRead, SQ_POST); |
142 | sqgetGlobalVar('markUnread', $markUnread, SQ_POST); |
143 | sqgetGlobalVar('attache', $attache, SQ_POST); |
144 | sqgetGlobalVar('location', $location, SQ_POST); |
9837b0a5 |
145 | |
9837b0a5 |
146 | /* end of get globals */ |
147 | |
65c3ec94 |
148 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
9c22eb94 |
149 | $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox); |
65c3ec94 |
150 | |
87d5f199 |
151 | $location = set_url_var($location,'composenew',0,false); |
152 | $location = set_url_var($location,'composesession',0,false); |
153 | $location = set_url_var($location,'session',0,false); |
8c33aaf7 |
154 | |
cd885078 |
155 | /* remember changes to mailbox setting */ |
156 | if (!isset($lastTargetMailbox)) { |
157 | $lastTargetMailbox = 'INBOX'; |
158 | } |
159 | if ($targetMailbox != $lastTargetMailbox) { |
160 | $lastTargetMailbox = $targetMailbox; |
9837b0a5 |
161 | sqsession_register($lastTargetMailbox, 'lastTargetMailbox'); |
cd885078 |
162 | } |
87d5f199 |
163 | $exception = false; |
00f9181e |
164 | |
165 | do_hook('move_before_move'); |
166 | |
65c3ec94 |
167 | // expunge-on-demand if user isn't using move_to_trash or auto_expunge |
168 | if(isset($expungeButton)) { |
9c22eb94 |
169 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
170 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
171 | if ($startMessage > $show_num) { |
87d5f199 |
172 | $location = set_url_var($location,'startMessage',$startMessage-$show_num,false); |
9c22eb94 |
173 | } else { |
87d5f199 |
174 | $location = set_url_var($location,'startMessage',1,false); |
9c22eb94 |
175 | } |
176 | } |
65c3ec94 |
177 | } elseif(isset($undeleteButton)) { |
178 | // undelete messages if user isn't using move_to_trash or auto_expunge |
65c3ec94 |
179 | if (is_array($msg) == 1) { |
180 | // Removes \Deleted flag from selected messages |
ce2c797b |
181 | $j = $i = 0; |
182 | $id = array(); |
65c3ec94 |
183 | // If they have selected nothing msg is size one still, but will be an infinite |
184 | // loop because we never increment j. so check to see if msg[0] is set or not to fix this. |
185 | while ($j < count($msg)) { |
f9b3e5d9 |
186 | if ($msg[$i]) { |
ce2c797b |
187 | $id[] = $msg[$i]; |
8c33aaf7 |
188 | $j++; |
f9b3e5d9 |
189 | } |
190 | $i++; |
65c3ec94 |
191 | } |
ce2c797b |
192 | if (count($id)) { |
193 | sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true); |
194 | } |
65c3ec94 |
195 | } else { |
87d5f199 |
196 | $exception = true; |
65c3ec94 |
197 | } |
198 | } elseif (!isset($moveButton)) { |
199 | // If the delete button was pressed, the moveButton variable will not be set. |
ce2c797b |
200 | if (is_array($msg)) { |
65c3ec94 |
201 | // Marks the selected messages as 'Deleted' |
ce2c797b |
202 | $j = $i = $cnt = 0; |
203 | $id = array(); |
65c3ec94 |
204 | // If they have selected nothing msg is size one still, but will be an infinite |
205 | // loop because we never increment j. so check to see if msg[0] is set or not to fix this. |
206 | while ($j < count($msg)) { |
23fd3c8e |
207 | if (isset($msg[$i])) { |
ce2c797b |
208 | $id[] = $msg[$i]; |
65c3ec94 |
209 | $j++; |
2d7d3c2a |
210 | } |
dd88d31f |
211 | $i++; |
65c3ec94 |
212 | } |
ce2c797b |
213 | if (count($id) && !isset($attache)) { |
214 | if (isset($markRead)) { |
215 | sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true); |
216 | } else if (isset($markUnread)) { |
217 | sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true); |
218 | } else { |
219 | sqimap_msgs_list_delete($imapConnection, $mailbox, $id); |
220 | if ($auto_expunge) { |
221 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
222 | } |
223 | } |
224 | } |
8c33aaf7 |
225 | if (isset($attache)) { |
acc28bac |
226 | $composesession = attachSelectedMessages($msg, $imapConnection); |
87d5f199 |
227 | $location = set_url_var($location, 'session', $composesession, false); |
8c33aaf7 |
228 | if ($compose_new_win) { |
87d5f199 |
229 | $location = set_url_var($location, 'composenew', 1, false); |
acc28bac |
230 | } else { |
8c33aaf7 |
231 | $location = str_replace('search.php','compose.php',$location); |
232 | $location = str_replace('right_main.php','compose.php',$location); |
acc28bac |
233 | } |
8c33aaf7 |
234 | } else { |
9c22eb94 |
235 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
236 | if ($startMessage > $show_num) { |
87d5f199 |
237 | $location = set_url_var($location,'startMessage',$startMessage-$show_num, false); |
9c22eb94 |
238 | } else { |
87d5f199 |
239 | $location = set_url_var($location,'startMessage',1, false); |
9c22eb94 |
240 | } |
241 | } |
8c33aaf7 |
242 | } |
65c3ec94 |
243 | } else { |
87d5f199 |
244 | $exception = true; |
65c3ec94 |
245 | } |
246 | } else { // Move messages |
247 | // lets check to see if they selected any messages |
ce2c797b |
248 | if (is_array($msg)) { |
249 | $j = $i = 0; |
250 | $id = array(); |
65c3ec94 |
251 | // If they have selected nothing msg is size one still, but will be an infinite |
252 | // loop because we never increment j. so check to see if msg[0] is set or not to fix this. |
9c22eb94 |
253 | $cnt = count($msg); |
254 | while ($j < $cnt) { |
23fd3c8e |
255 | if (isset($msg[$i])) { |
ce2c797b |
256 | $id[] = $msg[$i]; |
65c3ec94 |
257 | $j++; |
dd88d31f |
258 | } |
259 | $i++; |
65c3ec94 |
260 | } |
ce2c797b |
261 | sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox); |
8c33aaf7 |
262 | if ($auto_expunge) { |
9c22eb94 |
263 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
264 | } else { |
265 | $cnt = 0; |
266 | } |
267 | |
268 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
269 | if ($startMessage > $show_num) { |
87d5f199 |
270 | $location = set_url_var($location,'startMessage',$startMessage-$show_num, false); |
9c22eb94 |
271 | } else { |
87d5f199 |
272 | $location = set_url_var($location,'startMessage',1, false); |
9c22eb94 |
273 | } |
8c33aaf7 |
274 | } |
65c3ec94 |
275 | } else { |
87d5f199 |
276 | $exception = true; |
65c3ec94 |
277 | } |
278 | } |
65c3ec94 |
279 | // Log out this session |
280 | sqimap_logout($imapConnection); |
87d5f199 |
281 | if ($exception) { |
282 | displayPageHeader($color, $mailbox); |
283 | error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color); |
284 | } else { |
285 | header("Location: $location"); |
286 | exit; |
287 | } |
2aa12d5e |
288 | ?> |
6332704d |
289 | </BODY></HTML> |