Looks like somebody commented out the reference headers, which will break
[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,
33 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
34 $compose_messages;
35
a2a30ee7 36 if (!isset($compose_messages)) {
37 $compose_messages = array();
38 sqsession_register($compose_messages,'compose_messages');
acc28bac 39 }
40
07abb0f3 41 if (!$composesession) {
21f3c131 42 $composesession = 1;
a2a30ee7 43 sqsession_register($composesession,'composesession');
acc28bac 44 } else {
21f3c131 45 $composesession++;
07abb0f3 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])) {
21f3c131 68 $id = $msg[$i];
5b8d68ca 69 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
d0519c03 70 if ($response == 'OK') {
8c33aaf7 71 $k = $i + $start_index;
72 $subject = $msgs[$k]['SUBJECT'];
21f3c131 73
8c33aaf7 74 array_shift($body_a);
75 $body = implode('', $body_a);
76 $body .= "\r\n";
21f3c131 77
8c33aaf7 78 $localfilename = GenerateRandomString(32, 'FILE', 7);
79 $full_localfilename = "$hashed_attachment_dir/$localfilename";
80
81d79073 81 $fp = fopen( $full_localfilename, 'wb');
8c33aaf7 82 fwrite ($fp, $body);
83 fclose($fp);
a2a30ee7 84 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
85 $full_localfilename);
acc28bac 86 }
8c33aaf7 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 100sqgetGlobalVar('key', $key, SQ_COOKIE);
101sqgetGlobalVar('username', $username, SQ_SESSION);
102sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
103sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
104sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
105
106sqgetGlobalVar('mailbox', $mailbox);
107sqgetGlobalVar('startMessage', $startMessage);
108sqgetGlobalVar('msg', $msg);
109
110sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
111sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
112sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
113
114sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
115sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
116sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
117sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
118sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
119sqgetGlobalVar('markRead', $markRead, SQ_POST);
120sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
121sqgetGlobalVar('attache', $attache, SQ_POST);
122sqgetGlobalVar('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 */
134if (!isset($lastTargetMailbox)) {
135 $lastTargetMailbox = 'INBOX';
136}
137if ($targetMailbox != $lastTargetMailbox) {
138 $lastTargetMailbox = $targetMailbox;
9837b0a5 139 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 140}
87d5f199 141$exception = false;
00f9181e 142
143do_hook('move_before_move');
144
65c3ec94 145// expunge-on-demand if user isn't using move_to_trash or auto_expunge
146if(isset($expungeButton)) {
9c22eb94 147 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
148 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
149 if ($startMessage > $show_num) {
87d5f199 150 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
9c22eb94 151 } else {
87d5f199 152 $location = set_url_var($location,'startMessage',1,false);
9c22eb94 153 }
154 }
65c3ec94 155} elseif(isset($undeleteButton)) {
156 // undelete messages if user isn't using move_to_trash or auto_expunge
65c3ec94 157 if (is_array($msg) == 1) {
158 // Removes \Deleted flag from selected messages
ce2c797b 159 $j = $i = 0;
160 $id = array();
65c3ec94 161 // If they have selected nothing msg is size one still, but will be an infinite
162 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
163 while ($j < count($msg)) {
f9b3e5d9 164 if ($msg[$i]) {
ce2c797b 165 $id[] = $msg[$i];
8c33aaf7 166 $j++;
f9b3e5d9 167 }
168 $i++;
65c3ec94 169 }
ce2c797b 170 if (count($id)) {
171 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
172 }
65c3ec94 173 } else {
87d5f199 174 $exception = true;
65c3ec94 175 }
176} elseif (!isset($moveButton)) {
177 // If the delete button was pressed, the moveButton variable will not be set.
ce2c797b 178 if (is_array($msg)) {
65c3ec94 179 // Marks the selected messages as 'Deleted'
ce2c797b 180 $j = $i = $cnt = 0;
181 $id = array();
65c3ec94 182 // If they have selected nothing msg is size one still, but will be an infinite
183 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
184 while ($j < count($msg)) {
23fd3c8e 185 if (isset($msg[$i])) {
ce2c797b 186 $id[] = $msg[$i];
65c3ec94 187 $j++;
2d7d3c2a 188 }
dd88d31f 189 $i++;
65c3ec94 190 }
ce2c797b 191 if (count($id) && !isset($attache)) {
192 if (isset($markRead)) {
193 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
194 } else if (isset($markUnread)) {
195 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
196 } else {
197 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
198 if ($auto_expunge) {
199 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
200 }
201 }
202 }
8c33aaf7 203 if (isset($attache)) {
acc28bac 204 $composesession = attachSelectedMessages($msg, $imapConnection);
87d5f199 205 $location = set_url_var($location, 'session', $composesession, false);
8c33aaf7 206 if ($compose_new_win) {
87d5f199 207 $location = set_url_var($location, 'composenew', 1, false);
acc28bac 208 } else {
8c33aaf7 209 $location = str_replace('search.php','compose.php',$location);
210 $location = str_replace('right_main.php','compose.php',$location);
acc28bac 211 }
8c33aaf7 212 } else {
9c22eb94 213 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
214 if ($startMessage > $show_num) {
87d5f199 215 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 216 } else {
87d5f199 217 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 218 }
219 }
8c33aaf7 220 }
65c3ec94 221 } else {
87d5f199 222 $exception = true;
65c3ec94 223 }
224} else { // Move messages
225 // lets check to see if they selected any messages
ce2c797b 226 if (is_array($msg)) {
227 $j = $i = 0;
228 $id = array();
65c3ec94 229 // If they have selected nothing msg is size one still, but will be an infinite
230 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
9c22eb94 231 $cnt = count($msg);
232 while ($j < $cnt) {
23fd3c8e 233 if (isset($msg[$i])) {
ce2c797b 234 $id[] = $msg[$i];
65c3ec94 235 $j++;
dd88d31f 236 }
237 $i++;
65c3ec94 238 }
ce2c797b 239 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
8c33aaf7 240 if ($auto_expunge) {
9c22eb94 241 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
242 } else {
243 $cnt = 0;
244 }
245
246 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
247 if ($startMessage > $show_num) {
87d5f199 248 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 249 } else {
87d5f199 250 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 251 }
8c33aaf7 252 }
65c3ec94 253 } else {
87d5f199 254 $exception = true;
65c3ec94 255 }
256}
65c3ec94 257// Log out this session
258sqimap_logout($imapConnection);
87d5f199 259if ($exception) {
260 displayPageHeader($color, $mailbox);
261 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
262} else {
263 header("Location: $location");
264 exit;
265}
2aa12d5e 266?>
6332704d 267</BODY></HTML>