move global include earlier..
[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');
86725763 19require_once(SM_PATH . 'functions/display_messages.php');
20require_once(SM_PATH . 'functions/imap.php');
21require_once(SM_PATH . 'functions/html.php');
22
acc28bac 23global $compose_new_win;
24
07abb0f3 25if (isset($_SESSION['composesession'])) {
26 $composesession = $_SESSION['composesession'];
27} else {
28 $composesession = 0;
29}
ce2c797b 30/* obsolete ?? */
65c3ec94 31function putSelectedMessagesIntoString($msg) {
32 $j = 0;
33 $i = 0;
34 $firstLoop = true;
65c3ec94 35 // If they have selected nothing msg is size one still, but will
36 // be an infinite loop because we never increment j. so check to
37 // see if msg[0] is set or not to fix this.
38 while (($j < count($msg)) && ($msg[0])) {
39 if ($msg[$i]) {
5e9e90fd 40 if ($firstLoop != true) {
41 $selectedMessages .= "&amp;";
42 } else {
43 $firstLoop = false;
44 }
dd88d31f 45 $selectedMessages .= "selMsg[$j]=$msg[$i]";
05207a68 46 $j++;
65c3ec94 47 }
48 $i++;
49 }
50}
51
acc28bac 52function attachSelectedMessages($msg, $imapConnection) {
a2a30ee7 53 global $username, $attachment_dir,
54 $data_dir, $composesession, $uid_support,
55 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
56 $compose_messages;
57
a2a30ee7 58 if (!isset($compose_messages)) {
59 $compose_messages = array();
60 sqsession_register($compose_messages,'compose_messages');
acc28bac 61 }
62
07abb0f3 63 if (!$composesession) {
21f3c131 64 $composesession = 1;
a2a30ee7 65 sqsession_register($composesession,'composesession');
acc28bac 66 } else {
21f3c131 67 $composesession++;
07abb0f3 68 sqsession_register($composesession,'composesession');
acc28bac 69 }
70
21f3c131 71 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
acc28bac 72
8c33aaf7 73 if ($thread_sort_messages || $allow_server_sort) {
74 $start_index=0;
75 } else {
76 $start_index = ($startMessage-1) * $show_num;
77 }
acc28bac 78
79 $i = 0;
80 $j = 0;
21f3c131 81 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
a2a30ee7 82
83 $composeMessage = new Message();
84 $rfc822_header = new Rfc822Header();
85 $composeMessage->rfc822_header = $rfc822_header;
86 $composeMessage->reply_rfc822_header = '';
87
acc28bac 88 while ($j < count($msg)) {
89 if (isset($msg[$i])) {
21f3c131 90 $id = $msg[$i];
5b8d68ca 91 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
21f3c131 92 if ($response = 'OK') {
8c33aaf7 93 $k = $i + $start_index;
94 $subject = $msgs[$k]['SUBJECT'];
21f3c131 95
8c33aaf7 96 array_shift($body_a);
97 $body = implode('', $body_a);
98 $body .= "\r\n";
21f3c131 99
8c33aaf7 100 $localfilename = GenerateRandomString(32, 'FILE', 7);
101 $full_localfilename = "$hashed_attachment_dir/$localfilename";
102
81d79073 103 $fp = fopen( $full_localfilename, 'wb');
8c33aaf7 104 fwrite ($fp, $body);
105 fclose($fp);
a2a30ee7 106 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
107 $full_localfilename);
acc28bac 108 }
8c33aaf7 109 $j++;
110 }
111 $i++;
acc28bac 112 }
a2a30ee7 113 $compose_messages[$composesession] = $composeMessage;
87d5f199 114 sqsession_register($compose_messages,'compose_messages');
115 session_write_close();
acc28bac 116 return $composesession;
117}
118
9c22eb94 119
9837b0a5 120
121/* get globals */
122
123$username = $_SESSION['username'];
124$key = $_COOKIE['key'];
125$onetimepad = $_SESSION['onetimepad'];
126$base_uri = $_SESSION['base_uri'];
127$delimiter = $_SESSION['delimiter'];
8fe969f6 128
129sqGetGlobalVar('mailbox', $mailbox);
130sqGetGlobalVar('startMessage', $startMessage);
131sqGetGlobalVar('msg', $msg);
132
9837b0a5 133if (isset($_POST['moveButton'])) {
134 $moveButton = $_POST['moveButton'];
135}
882cd908 136
137if (isset($_SESSION['msgs'])) {
138 $msgs = $_SESSION['msgs'];
139}
140
9837b0a5 141if (isset($_POST['expungeButton'])) {
142 $expungeButton = $_POST['expungeButton'];
143}
144if (isset($_POST['targetMailbox'])) {
145 $targetMailbox = $_POST['targetMailbox'];
146}
147if (isset($_SESSION['lastTargetMailbox'])) {
148 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
149}
150if (isset($_POST['expungeButton'])) {
151 $expungeButton = $_POST['expungeButton'];
152}
153if (isset($_POST['undeleteButton'])) {
154 $undeleteButton = $_POST['undeleteButton'];
155}
156if (isset($_POST['markRead'])) {
157 $markRead = $_POST['markRead'];
158}
159if (isset($_POST['markUnread'])) {
160 $markUnread = $_POST['markUnread'];
161}
162if (isset($_POST['attache'])) {
163 $attache = $_POST['attache'];
164}
165
c083ce9a 166if (isset($_POST['location'])) {
167 $location = $_POST['location'];
168}
169
882cd908 170if (isset($_SESSION['composesession'])) {
171 $composesession = $_SESSION['composesession'];
172}
9837b0a5 173/* end of get globals */
174
65c3ec94 175$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 176$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 177
87d5f199 178$location = set_url_var($location,'composenew',0,false);
179$location = set_url_var($location,'composesession',0,false);
180$location = set_url_var($location,'session',0,false);
8c33aaf7 181
cd885078 182/* remember changes to mailbox setting */
183if (!isset($lastTargetMailbox)) {
184 $lastTargetMailbox = 'INBOX';
185}
186if ($targetMailbox != $lastTargetMailbox) {
187 $lastTargetMailbox = $targetMailbox;
9837b0a5 188 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 189}
87d5f199 190$exception = false;
65c3ec94 191// expunge-on-demand if user isn't using move_to_trash or auto_expunge
192if(isset($expungeButton)) {
9c22eb94 193 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
194 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
195 if ($startMessage > $show_num) {
87d5f199 196 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
9c22eb94 197 } else {
87d5f199 198 $location = set_url_var($location,'startMessage',1,false);
9c22eb94 199 }
200 }
65c3ec94 201} elseif(isset($undeleteButton)) {
202 // undelete messages if user isn't using move_to_trash or auto_expunge
65c3ec94 203 if (is_array($msg) == 1) {
204 // Removes \Deleted flag from selected messages
ce2c797b 205 $j = $i = 0;
206 $id = array();
65c3ec94 207 // If they have selected nothing msg is size one still, but will be an infinite
208 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
209 while ($j < count($msg)) {
f9b3e5d9 210 if ($msg[$i]) {
ce2c797b 211 $id[] = $msg[$i];
8c33aaf7 212 $j++;
f9b3e5d9 213 }
214 $i++;
65c3ec94 215 }
ce2c797b 216 if (count($id)) {
217 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
218 }
65c3ec94 219 } else {
87d5f199 220 $exception = true;
65c3ec94 221 }
222} elseif (!isset($moveButton)) {
223 // If the delete button was pressed, the moveButton variable will not be set.
ce2c797b 224 if (is_array($msg)) {
65c3ec94 225 // Marks the selected messages as 'Deleted'
ce2c797b 226 $j = $i = $cnt = 0;
227 $id = array();
65c3ec94 228 // If they have selected nothing msg is size one still, but will be an infinite
229 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
230 while ($j < count($msg)) {
23fd3c8e 231 if (isset($msg[$i])) {
ce2c797b 232 $id[] = $msg[$i];
65c3ec94 233 $j++;
2d7d3c2a 234 }
dd88d31f 235 $i++;
65c3ec94 236 }
ce2c797b 237 if (count($id) && !isset($attache)) {
238 if (isset($markRead)) {
239 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
240 } else if (isset($markUnread)) {
241 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
242 } else {
243 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
244 if ($auto_expunge) {
245 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
246 }
247 }
248 }
8c33aaf7 249 if (isset($attache)) {
acc28bac 250 $composesession = attachSelectedMessages($msg, $imapConnection);
87d5f199 251 $location = set_url_var($location, 'session', $composesession, false);
8c33aaf7 252 if ($compose_new_win) {
87d5f199 253 $location = set_url_var($location, 'composenew', 1, false);
acc28bac 254 } else {
8c33aaf7 255 $location = str_replace('search.php','compose.php',$location);
256 $location = str_replace('right_main.php','compose.php',$location);
acc28bac 257 }
8c33aaf7 258 } else {
9c22eb94 259 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
260 if ($startMessage > $show_num) {
87d5f199 261 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 262 } else {
87d5f199 263 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 264 }
265 }
8c33aaf7 266 }
65c3ec94 267 } else {
87d5f199 268 $exception = true;
65c3ec94 269 }
270} else { // Move messages
271 // lets check to see if they selected any messages
ce2c797b 272 if (is_array($msg)) {
273 $j = $i = 0;
274 $id = array();
65c3ec94 275 // If they have selected nothing msg is size one still, but will be an infinite
276 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
9c22eb94 277 $cnt = count($msg);
278 while ($j < $cnt) {
23fd3c8e 279 if (isset($msg[$i])) {
ce2c797b 280 $id[] = $msg[$i];
65c3ec94 281 $j++;
dd88d31f 282 }
283 $i++;
65c3ec94 284 }
ce2c797b 285 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
8c33aaf7 286 if ($auto_expunge) {
9c22eb94 287 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
288 } else {
289 $cnt = 0;
290 }
291
292 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
293 if ($startMessage > $show_num) {
87d5f199 294 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 295 } else {
87d5f199 296 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 297 }
8c33aaf7 298 }
65c3ec94 299 } else {
87d5f199 300 $exception = true;
65c3ec94 301 }
302}
65c3ec94 303// Log out this session
304sqimap_logout($imapConnection);
87d5f199 305if ($exception) {
306 displayPageHeader($color, $mailbox);
307 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
308} else {
309 header("Location: $location");
310 exit;
311}
2aa12d5e 312?>
6332704d 313</BODY></HTML>