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