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