Fetching the subject for the messages-to-be-attached went wrong. $i
[squirrelmail.git] / src / move_messages.php
CommitLineData
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. */
15define('SM_PATH','../');
16
17/* SquirrelMail required files. */
08185f2a 18require_once(SM_PATH . 'include/validate.php');
8650e9e1 19require_once(SM_PATH . 'functions/global.php');
86725763 20require_once(SM_PATH . 'functions/display_messages.php');
21require_once(SM_PATH . 'functions/imap.php');
22require_once(SM_PATH . 'functions/html.php');
23
acc28bac 24global $compose_new_win;
25
1e12d1ff 26if ( !sqgetGlobalVar('composesession', $composesession, SQ_SESSION) ) {
27 $composesession = 0;
28}
29
acc28bac 30function 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);
11fd4a7b 70
a2388eb7 71 if ($response == 'OK') {
11fd4a7b 72
a2388eb7 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;
79 }
80 }
81
82 array_shift($body_a);
83 $body = implode('', $body_a);
84 $body .= "\r\n";
85
86 $localfilename = GenerateRandomString(32, 'FILE', 7);
87 $full_localfilename = "$hashed_attachment_dir/$localfilename";
88
89 $fp = fopen( $full_localfilename, 'wb');
90 fwrite ($fp, $body);
91 fclose($fp);
92 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
11fd4a7b 93 $full_localfilename);
94 }
a2388eb7 95 $j++;
96 }
97 $i++;
acc28bac 98 }
a2a30ee7 99 $compose_messages[$composesession] = $composeMessage;
87d5f199 100 sqsession_register($compose_messages,'compose_messages');
101 session_write_close();
acc28bac 102 return $composesession;
103}
104
9c22eb94 105
9837b0a5 106
107/* get globals */
1e12d1ff 108sqgetGlobalVar('key', $key, SQ_COOKIE);
109sqgetGlobalVar('username', $username, SQ_SESSION);
110sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
111sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
112sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
113
114sqgetGlobalVar('mailbox', $mailbox);
115sqgetGlobalVar('startMessage', $startMessage);
116sqgetGlobalVar('msg', $msg);
117
118sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
119sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
120sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
121
122sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
123sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
124sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
125sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
126sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
127sqgetGlobalVar('markRead', $markRead, SQ_POST);
128sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
129sqgetGlobalVar('attache', $attache, SQ_POST);
130sqgetGlobalVar('location', $location, SQ_POST);
9837b0a5 131
9837b0a5 132/* end of get globals */
133
65c3ec94 134$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 135$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 136
87d5f199 137$location = set_url_var($location,'composenew',0,false);
138$location = set_url_var($location,'composesession',0,false);
139$location = set_url_var($location,'session',0,false);
8c33aaf7 140
cd885078 141/* remember changes to mailbox setting */
142if (!isset($lastTargetMailbox)) {
143 $lastTargetMailbox = 'INBOX';
144}
145if ($targetMailbox != $lastTargetMailbox) {
146 $lastTargetMailbox = $targetMailbox;
9837b0a5 147 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 148}
87d5f199 149$exception = false;
00f9181e 150
151do_hook('move_before_move');
152
11fd4a7b 153
154/*
155 Move msg list sorting up here, as it is used several times,
156 makes it more efficient to do it in one place for the code
157*/
158$id = array();
159if (isset($msg) && is_array($msg)) {
160 foreach( $msg as $key=>$uid ) {
161 // using foreach removes the risk of infinite loops that was there //
162 $id[] = $uid;
163 }
164}
165
65c3ec94 166// expunge-on-demand if user isn't using move_to_trash or auto_expunge
167if(isset($expungeButton)) {
9c22eb94 168 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
169 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
170 if ($startMessage > $show_num) {
11fd4a7b 171 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
172 } else {
173 $location = set_url_var($location,'startMessage',1,false);
174 }
9c22eb94 175 }
65c3ec94 176} elseif(isset($undeleteButton)) {
177 // undelete messages if user isn't using move_to_trash or auto_expunge
11fd4a7b 178 // Removes \Deleted flag from selected messages
179 if (count($id)) {
180 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
65c3ec94 181 } else {
11fd4a7b 182 $exception = true;
65c3ec94 183 }
184} elseif (!isset($moveButton)) {
11fd4a7b 185 if (count($id)) {
186 $cnt = count($id);
187 if (!isset($attache)) {
d3714d3a 188 $button_action = concat_hook_function('move_messages_button_action');
11fd4a7b 189 if (isset($markRead)) {
190 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
191 } else if (isset($markUnread)) {
192 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
193 } else {
d3714d3a 194 if (!$button_action) {
195 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
196 if ($auto_expunge) {
197 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
198 }
11fd4a7b 199 }
2d7d3c2a 200 }
ce2c797b 201 }
8c33aaf7 202 if (isset($attache)) {
11fd4a7b 203 $composesession = attachSelectedMessages($id, $imapConnection);
204 $location = set_url_var($location, 'session', $composesession, false);
205 if ($compose_new_win) {
206 $location = set_url_var($location, 'composenew', 1, false);
207 } else {
208 $location = str_replace('search.php','compose.php',$location);
209 $location = str_replace('right_main.php','compose.php',$location);
210 }
211 } else {
212 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
213 if ($startMessage > $show_num) {
214 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
215 } else {
216 $location = set_url_var($location,'startMessage',1, false);
217 }
218 }
219 }
65c3ec94 220 } else {
11fd4a7b 221 $exception = true;
65c3ec94 222 }
223} else { // Move messages
11fd4a7b 224
225 if (count($id)) {
226 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
8c33aaf7 227 if ($auto_expunge) {
9c22eb94 228 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
11fd4a7b 229 } else {
230 $cnt = 0;
231 }
232
233 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
234 if ($startMessage > $show_num) {
235 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
236 } else {
237 $location = set_url_var($location,'startMessage',1, false);
238 }
239 }
65c3ec94 240 } else {
11fd4a7b 241 $exception = true;
65c3ec94 242 }
243}
65c3ec94 244// Log out this session
245sqimap_logout($imapConnection);
87d5f199 246if ($exception) {
247 displayPageHeader($color, $mailbox);
248 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
249} else {
250 header("Location: $location");
251 exit;
252}
2aa12d5e 253?>
6332704d 254</BODY></HTML>