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