59177427 |
1 | <?php |
895905c0 |
2 | |
35586184 |
3 | /** |
4 | * move_messages.php |
5 | * |
82d304a0 |
6 | * Copyright (c) 1999-2004 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$ |
8f6f9ba5 |
12 | * @package squirrelmail |
35586184 |
13 | */ |
ef870322 |
14 | |
8f6f9ba5 |
15 | /** Path for SquirrelMail required files. */ |
86725763 |
16 | define('SM_PATH','../'); |
17 | |
18 | /* SquirrelMail required files. */ |
08185f2a |
19 | require_once(SM_PATH . 'include/validate.php'); |
8650e9e1 |
20 | require_once(SM_PATH . 'functions/global.php'); |
86725763 |
21 | require_once(SM_PATH . 'functions/display_messages.php'); |
22 | require_once(SM_PATH . 'functions/imap.php'); |
23 | require_once(SM_PATH . 'functions/html.php'); |
24 | |
acc28bac |
25 | global $compose_new_win; |
26 | |
1e12d1ff |
27 | if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) { |
2cb178e3 |
28 | $composesession = 0; |
1e12d1ff |
29 | } |
30 | |
acc28bac |
31 | function attachSelectedMessages($msg, $imapConnection) { |
133b9a0e |
32 | global $username, $attachment_dir, $startMessage, |
a2a30ee7 |
33 | $data_dir, $composesession, $uid_support, |
2cb178e3 |
34 | $msgs, $show_num, $compose_messages; |
a2a30ee7 |
35 | |
a2a30ee7 |
36 | if (!isset($compose_messages)) { |
11fd4a7b |
37 | $compose_messages = array(); |
2cb178e3 |
38 | sqsession_register($compose_messages,'compose_messages'); |
acc28bac |
39 | } |
40 | |
07abb0f3 |
41 | if (!$composesession) { |
11fd4a7b |
42 | $composesession = 1; |
2cb178e3 |
43 | sqsession_register($composesession,'composesession'); |
acc28bac |
44 | } else { |
11fd4a7b |
45 | $composesession++; |
46 | sqsession_register($composesession,'composesession'); |
acc28bac |
47 | } |
48 | |
21f3c131 |
49 | $hashed_attachment_dir = getHashedDir($username, $attachment_dir); |
a2a30ee7 |
50 | |
51 | $composeMessage = new Message(); |
52 | $rfc822_header = new Rfc822Header(); |
53 | $composeMessage->rfc822_header = $rfc822_header; |
54 | $composeMessage->reply_rfc822_header = ''; |
55 | |
2cb178e3 |
56 | foreach($msg as $id) { |
57 | $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822", true, $response, $readmessage, $uid_support); |
11fd4a7b |
58 | |
2cb178e3 |
59 | if ($response == 'OK') { |
11fd4a7b |
60 | |
2cb178e3 |
61 | // fetch the subject for the message with $id from msgs. |
62 | // is there a more efficient way to do this? |
63 | foreach($msgs as $k => $vals) { |
64 | if($vals['ID'] == $id) { |
65 | $subject = $msgs[$k]['SUBJECT']; |
66 | break; |
a2388eb7 |
67 | } |
2cb178e3 |
68 | } |
a2388eb7 |
69 | |
2cb178e3 |
70 | array_shift($body_a); |
c2517a3b |
71 | array_pop($body_a); |
2cb178e3 |
72 | $body = implode('', $body_a); |
73 | $body .= "\r\n"; |
a2388eb7 |
74 | |
2cb178e3 |
75 | $localfilename = GenerateRandomString(32, 'FILE', 7); |
76 | $full_localfilename = "$hashed_attachment_dir/$localfilename"; |
a2388eb7 |
77 | |
2cb178e3 |
78 | $fp = fopen( $full_localfilename, 'wb'); |
79 | fwrite ($fp, $body); |
80 | fclose($fp); |
745fca27 |
81 | $composeMessage->initAttachment('message/rfc822',$subject.'.msg', |
2cb178e3 |
82 | $full_localfilename); |
a2388eb7 |
83 | } |
acc28bac |
84 | } |
2cb178e3 |
85 | |
a2a30ee7 |
86 | $compose_messages[$composesession] = $composeMessage; |
87d5f199 |
87 | sqsession_register($compose_messages,'compose_messages'); |
88 | session_write_close(); |
acc28bac |
89 | return $composesession; |
90 | } |
91 | |
9c22eb94 |
92 | |
9837b0a5 |
93 | |
94 | /* get globals */ |
1e12d1ff |
95 | sqgetGlobalVar('key', $key, SQ_COOKIE); |
96 | sqgetGlobalVar('username', $username, SQ_SESSION); |
97 | sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION); |
98 | sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION); |
99 | sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION); |
100 | |
101 | sqgetGlobalVar('mailbox', $mailbox); |
102 | sqgetGlobalVar('startMessage', $startMessage); |
103 | sqgetGlobalVar('msg', $msg); |
104 | |
105 | sqgetGlobalVar('msgs', $msgs, SQ_SESSION); |
106 | sqgetGlobalVar('composesession', $composesession, SQ_SESSION); |
107 | sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION); |
108 | |
109 | sqgetGlobalVar('moveButton', $moveButton, SQ_POST); |
110 | sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST); |
111 | sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST); |
112 | sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST); |
113 | sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST); |
114 | sqgetGlobalVar('markRead', $markRead, SQ_POST); |
115 | sqgetGlobalVar('markUnread', $markUnread, SQ_POST); |
116 | sqgetGlobalVar('attache', $attache, SQ_POST); |
117 | sqgetGlobalVar('location', $location, SQ_POST); |
6c540963 |
118 | sqgetGlobalVar('bypass_trash', $bypass_trash, SQ_POST); |
9837b0a5 |
119 | |
9837b0a5 |
120 | /* end of get globals */ |
121 | |
65c3ec94 |
122 | $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0); |
9c22eb94 |
123 | $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox); |
65c3ec94 |
124 | |
87d5f199 |
125 | $location = set_url_var($location,'composenew',0,false); |
126 | $location = set_url_var($location,'composesession',0,false); |
127 | $location = set_url_var($location,'session',0,false); |
8c33aaf7 |
128 | |
cd885078 |
129 | /* remember changes to mailbox setting */ |
130 | if (!isset($lastTargetMailbox)) { |
131 | $lastTargetMailbox = 'INBOX'; |
132 | } |
133 | if ($targetMailbox != $lastTargetMailbox) { |
134 | $lastTargetMailbox = $targetMailbox; |
9837b0a5 |
135 | sqsession_register($lastTargetMailbox, 'lastTargetMailbox'); |
cd885078 |
136 | } |
87d5f199 |
137 | $exception = false; |
00f9181e |
138 | |
139 | do_hook('move_before_move'); |
140 | |
11fd4a7b |
141 | |
142 | /* |
143 | Move msg list sorting up here, as it is used several times, |
144 | makes it more efficient to do it in one place for the code |
145 | */ |
146 | $id = array(); |
147 | if (isset($msg) && is_array($msg)) { |
148 | foreach( $msg as $key=>$uid ) { |
149 | // using foreach removes the risk of infinite loops that was there // |
150 | $id[] = $uid; |
151 | } |
152 | } |
153 | |
65c3ec94 |
154 | // expunge-on-demand if user isn't using move_to_trash or auto_expunge |
155 | if(isset($expungeButton)) { |
9c22eb94 |
156 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
157 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
158 | if ($startMessage > $show_num) { |
11fd4a7b |
159 | $location = set_url_var($location,'startMessage',$startMessage-$show_num,false); |
160 | } else { |
161 | $location = set_url_var($location,'startMessage',1,false); |
162 | } |
9c22eb94 |
163 | } |
65c3ec94 |
164 | } elseif(isset($undeleteButton)) { |
165 | // undelete messages if user isn't using move_to_trash or auto_expunge |
11fd4a7b |
166 | // Removes \Deleted flag from selected messages |
167 | if (count($id)) { |
168 | sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true); |
65c3ec94 |
169 | } else { |
11fd4a7b |
170 | $exception = true; |
65c3ec94 |
171 | } |
172 | } elseif (!isset($moveButton)) { |
11fd4a7b |
173 | if (count($id)) { |
174 | $cnt = count($id); |
175 | if (!isset($attache)) { |
176 | if (isset($markRead)) { |
177 | sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true); |
178 | } else if (isset($markUnread)) { |
179 | sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true); |
fd181f53 |
180 | } else if (isset($markFlagged)) { |
181 | sqimap_toggle_flag($imapConnection, $id, '\\Flagged', true, true); |
182 | } else if (isset($markUnflagged)) { |
183 | sqimap_toggle_flag($imapConnection, $id, '\\Flagged', false, true); |
11fd4a7b |
184 | } else { |
fd181f53 |
185 | if (!boolean_hook_function('move_messages_button_action', NULL, 1)) { |
6c540963 |
186 | sqimap_msgs_list_delete($imapConnection, $mailbox, $id,$bypass_trash); |
d3714d3a |
187 | if ($auto_expunge) { |
188 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
189 | } |
11fd4a7b |
190 | } |
2d7d3c2a |
191 | } |
ce2c797b |
192 | } |
8c33aaf7 |
193 | if (isset($attache)) { |
11fd4a7b |
194 | $composesession = attachSelectedMessages($id, $imapConnection); |
195 | $location = set_url_var($location, 'session', $composesession, false); |
196 | if ($compose_new_win) { |
197 | $location = set_url_var($location, 'composenew', 1, false); |
198 | } else { |
199 | $location = str_replace('search.php','compose.php',$location); |
200 | $location = str_replace('right_main.php','compose.php',$location); |
201 | } |
202 | } else { |
203 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
204 | if ($startMessage > $show_num) { |
205 | $location = set_url_var($location,'startMessage',$startMessage-$show_num, false); |
206 | } else { |
207 | $location = set_url_var($location,'startMessage',1, false); |
208 | } |
209 | } |
210 | } |
65c3ec94 |
211 | } else { |
11fd4a7b |
212 | $exception = true; |
65c3ec94 |
213 | } |
214 | } else { // Move messages |
11fd4a7b |
215 | |
216 | if (count($id)) { |
35261ae8 |
217 | sqimap_msgs_list_move($imapConnection,$id,$targetMailbox); |
8c33aaf7 |
218 | if ($auto_expunge) { |
9c22eb94 |
219 | $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true); |
11fd4a7b |
220 | } else { |
221 | $cnt = 0; |
222 | } |
223 | |
224 | if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) { |
225 | if ($startMessage > $show_num) { |
226 | $location = set_url_var($location,'startMessage',$startMessage-$show_num, false); |
227 | } else { |
228 | $location = set_url_var($location,'startMessage',1, false); |
229 | } |
230 | } |
65c3ec94 |
231 | } else { |
11fd4a7b |
232 | $exception = true; |
65c3ec94 |
233 | } |
234 | } |
65c3ec94 |
235 | // Log out this session |
236 | sqimap_logout($imapConnection); |
87d5f199 |
237 | if ($exception) { |
238 | displayPageHeader($color, $mailbox); |
239 | error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color); |
240 | } else { |
241 | header("Location: $location"); |
242 | exit; |
243 | } |
dcc1cc82 |
244 | ?> |
245 | </BODY></HTML> |