added explicit include of global.php
[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
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 */
1e12d1ff 122sqgetGlobalVar('key', $key, SQ_COOKIE);
123sqgetGlobalVar('username', $username, SQ_SESSION);
124sqgetGlobalVar('onetimepad',$onetimepad, SQ_SESSION);
125sqgetGlobalVar('delimiter', $delimiter, SQ_SESSION);
126sqgetGlobalVar('base_uri', $base_uri, SQ_SESSION);
127
128sqgetGlobalVar('mailbox', $mailbox);
129sqgetGlobalVar('startMessage', $startMessage);
130sqgetGlobalVar('msg', $msg);
131
132sqgetGlobalVar('msgs', $msgs, SQ_SESSION);
133sqgetGlobalVar('composesession', $composesession, SQ_SESSION);
134sqgetGlobalVar('lastTargetMailbox', $lastTargetMailbox, SQ_SESSION);
135
136sqgetGlobalVar('moveButton', $moveButton, SQ_POST);
137sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
138sqgetGlobalVar('targetMailbox', $targetMailbox, SQ_POST);
139sqgetGlobalVar('expungeButton', $expungeButton, SQ_POST);
140sqgetGlobalVar('undeleteButton', $undeleteButton, SQ_POST);
141sqgetGlobalVar('markRead', $markRead, SQ_POST);
142sqgetGlobalVar('markUnread', $markUnread, SQ_POST);
143sqgetGlobalVar('attache', $attache, SQ_POST);
144sqgetGlobalVar('location', $location, SQ_POST);
9837b0a5 145
9837b0a5 146/* end of get globals */
147
65c3ec94 148$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
9c22eb94 149$mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
65c3ec94 150
87d5f199 151$location = set_url_var($location,'composenew',0,false);
152$location = set_url_var($location,'composesession',0,false);
153$location = set_url_var($location,'session',0,false);
8c33aaf7 154
cd885078 155/* remember changes to mailbox setting */
156if (!isset($lastTargetMailbox)) {
157 $lastTargetMailbox = 'INBOX';
158}
159if ($targetMailbox != $lastTargetMailbox) {
160 $lastTargetMailbox = $targetMailbox;
9837b0a5 161 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
cd885078 162}
87d5f199 163$exception = false;
65c3ec94 164// expunge-on-demand if user isn't using move_to_trash or auto_expunge
165if(isset($expungeButton)) {
9c22eb94 166 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
167 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
168 if ($startMessage > $show_num) {
87d5f199 169 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
9c22eb94 170 } else {
87d5f199 171 $location = set_url_var($location,'startMessage',1,false);
9c22eb94 172 }
173 }
65c3ec94 174} elseif(isset($undeleteButton)) {
175 // undelete messages if user isn't using move_to_trash or auto_expunge
65c3ec94 176 if (is_array($msg) == 1) {
177 // Removes \Deleted flag from selected messages
ce2c797b 178 $j = $i = 0;
179 $id = array();
65c3ec94 180 // If they have selected nothing msg is size one still, but will be an infinite
181 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
182 while ($j < count($msg)) {
f9b3e5d9 183 if ($msg[$i]) {
ce2c797b 184 $id[] = $msg[$i];
8c33aaf7 185 $j++;
f9b3e5d9 186 }
187 $i++;
65c3ec94 188 }
ce2c797b 189 if (count($id)) {
190 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
191 }
65c3ec94 192 } else {
87d5f199 193 $exception = true;
65c3ec94 194 }
195} elseif (!isset($moveButton)) {
196 // If the delete button was pressed, the moveButton variable will not be set.
ce2c797b 197 if (is_array($msg)) {
65c3ec94 198 // Marks the selected messages as 'Deleted'
ce2c797b 199 $j = $i = $cnt = 0;
200 $id = array();
65c3ec94 201 // If they have selected nothing msg is size one still, but will be an infinite
202 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
203 while ($j < count($msg)) {
23fd3c8e 204 if (isset($msg[$i])) {
ce2c797b 205 $id[] = $msg[$i];
65c3ec94 206 $j++;
2d7d3c2a 207 }
dd88d31f 208 $i++;
65c3ec94 209 }
ce2c797b 210 if (count($id) && !isset($attache)) {
211 if (isset($markRead)) {
212 sqimap_toggle_flag($imapConnection, $id, '\\Seen',true,true);
213 } else if (isset($markUnread)) {
214 sqimap_toggle_flag($imapConnection, $id, '\\Seen',false,true);
215 } else {
216 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
217 if ($auto_expunge) {
218 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
219 }
220 }
221 }
8c33aaf7 222 if (isset($attache)) {
acc28bac 223 $composesession = attachSelectedMessages($msg, $imapConnection);
87d5f199 224 $location = set_url_var($location, 'session', $composesession, false);
8c33aaf7 225 if ($compose_new_win) {
87d5f199 226 $location = set_url_var($location, 'composenew', 1, false);
acc28bac 227 } else {
8c33aaf7 228 $location = str_replace('search.php','compose.php',$location);
229 $location = str_replace('right_main.php','compose.php',$location);
acc28bac 230 }
8c33aaf7 231 } else {
9c22eb94 232 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
233 if ($startMessage > $show_num) {
87d5f199 234 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 235 } else {
87d5f199 236 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 237 }
238 }
8c33aaf7 239 }
65c3ec94 240 } else {
87d5f199 241 $exception = true;
65c3ec94 242 }
243} else { // Move messages
244 // lets check to see if they selected any messages
ce2c797b 245 if (is_array($msg)) {
246 $j = $i = 0;
247 $id = array();
65c3ec94 248 // If they have selected nothing msg is size one still, but will be an infinite
249 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
9c22eb94 250 $cnt = count($msg);
251 while ($j < $cnt) {
23fd3c8e 252 if (isset($msg[$i])) {
ce2c797b 253 $id[] = $msg[$i];
65c3ec94 254 $j++;
dd88d31f 255 }
256 $i++;
65c3ec94 257 }
ce2c797b 258 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
8c33aaf7 259 if ($auto_expunge) {
9c22eb94 260 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
261 } else {
262 $cnt = 0;
263 }
264
265 if (($startMessage+$cnt-1) >= $mbx_response['EXISTS']) {
266 if ($startMessage > $show_num) {
87d5f199 267 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
9c22eb94 268 } else {
87d5f199 269 $location = set_url_var($location,'startMessage',1, false);
9c22eb94 270 }
8c33aaf7 271 }
65c3ec94 272 } else {
87d5f199 273 $exception = true;
65c3ec94 274 }
275}
65c3ec94 276// Log out this session
277sqimap_logout($imapConnection);
87d5f199 278if ($exception) {
279 displayPageHeader($color, $mailbox);
280 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
281} else {
282 header("Location: $location");
283 exit;
284}
2aa12d5e 285?>
6332704d 286</BODY></HTML>