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