6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * Enables message moving between folders on the IMAP server.
14 /* Path for SquirrelMail required files. */
15 define('SM_PATH','../');
17 /* SquirrelMail required files. */
18 require_once(SM_PATH
. 'include/validate.php');
19 require_once(SM_PATH
. 'functions/display_messages.php');
20 require_once(SM_PATH
. 'functions/imap.php');
21 require_once(SM_PATH
. 'functions/html.php');
23 global $compose_new_win;
25 if (isset($_SESSION['composesession'])) {
26 $composesession = $_SESSION['composesession'];
31 function putSelectedMessagesIntoString($msg) {
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])) {
40 if ($firstLoop != true) {
41 $selectedMessages .= "&";
45 $selectedMessages .= "selMsg[$j]=$msg[$i]";
52 function attachSelectedMessages($msg, $imapConnection) {
53 global $username, $attachment_dir,
54 $data_dir, $composesession, $uid_support,
55 $msgs, $thread_sort_messages, $allow_server_sort, $show_num,
58 if (!isset($compose_messages)) {
59 $compose_messages = array();
60 sqsession_register($compose_messages,'compose_messages');
63 if (!$composesession) {
65 sqsession_register($composesession,'composesession');
68 sqsession_register($composesession,'composesession');
71 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
73 if ($thread_sort_messages ||
$allow_server_sort) {
76 $start_index = ($startMessage-1) * $show_num;
81 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
83 $composeMessage = new Message();
84 $rfc822_header = new Rfc822Header();
85 $composeMessage->rfc822_header
= $rfc822_header;
86 $composeMessage->reply_rfc822_header
= '';
88 while ($j < count($msg)) {
89 if (isset($msg[$i])) {
91 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
92 if ($response = 'OK') {
93 $k = $i +
$start_index;
94 $subject = $msgs[$k]['SUBJECT'];
97 $body = implode('', $body_a);
100 $localfilename = GenerateRandomString(32, 'FILE', 7);
101 $full_localfilename = "$hashed_attachment_dir/$localfilename";
103 $fp = fopen( $full_localfilename, 'wb');
106 $composeMessage->initAttachment('message/rfc822',$subject.'.eml',
107 $full_localfilename);
113 $compose_messages[$composesession] = $composeMessage;
114 sqsession_register($compose_messages,'compose_messages');
115 session_write_close();
116 return $composesession;
123 $username = $_SESSION['username'];
124 $key = $_COOKIE['key'];
125 $onetimepad = $_SESSION['onetimepad'];
126 $base_uri = $_SESSION['base_uri'];
127 $delimiter = $_SESSION['delimiter'];
129 sqGetGlobalVar('mailbox', $mailbox);
130 sqGetGlobalVar('startMessage', $startMessage);
131 sqGetGlobalVar('msg', $msg);
133 if (isset($_POST['moveButton'])) {
134 $moveButton = $_POST['moveButton'];
137 if (isset($_SESSION['msgs'])) {
138 $msgs = $_SESSION['msgs'];
141 if (isset($_POST['expungeButton'])) {
142 $expungeButton = $_POST['expungeButton'];
144 if (isset($_POST['targetMailbox'])) {
145 $targetMailbox = $_POST['targetMailbox'];
147 if (isset($_SESSION['lastTargetMailbox'])) {
148 $lastTargetMailbox = $_SESSION['lastTargetMailbox'];
150 if (isset($_POST['expungeButton'])) {
151 $expungeButton = $_POST['expungeButton'];
153 if (isset($_POST['undeleteButton'])) {
154 $undeleteButton = $_POST['undeleteButton'];
156 if (isset($_POST['markRead'])) {
157 $markRead = $_POST['markRead'];
159 if (isset($_POST['markUnread'])) {
160 $markUnread = $_POST['markUnread'];
162 if (isset($_POST['attache'])) {
163 $attache = $_POST['attache'];
166 if (isset($_POST['location'])) {
167 $location = $_POST['location'];
170 if (isset($_SESSION['composesession'])) {
171 $composesession = $_SESSION['composesession'];
173 /* end of get globals */
175 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
176 $mbx_response=sqimap_mailbox_select($imapConnection, $mailbox);
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);
182 /* remember changes to mailbox setting */
183 if (!isset($lastTargetMailbox)) {
184 $lastTargetMailbox = 'INBOX';
186 if ($targetMailbox != $lastTargetMailbox) {
187 $lastTargetMailbox = $targetMailbox;
188 sqsession_register($lastTargetMailbox, 'lastTargetMailbox');
191 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
192 if(isset($expungeButton)) {
193 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
194 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
195 if ($startMessage > $show_num) {
196 $location = set_url_var($location,'startMessage',$startMessage-$show_num,false);
198 $location = set_url_var($location,'startMessage',1,false);
201 } elseif(isset($undeleteButton)) {
202 // undelete messages if user isn't using move_to_trash or auto_expunge
203 if (is_array($msg) == 1) {
204 // Removes \Deleted flag from selected messages
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)) {
217 sqimap_toggle_flag($imapConnection, $id, '\\Deleted',false,true);
222 } elseif (!isset($moveButton)) {
223 // If the delete button was pressed, the moveButton variable will not be set.
224 if (is_array($msg)) {
225 // Marks the selected messages as 'Deleted'
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)) {
231 if (isset($msg[$i])) {
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);
243 sqimap_msgs_list_delete($imapConnection, $mailbox, $id);
245 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
249 if (isset($attache)) {
250 $composesession = attachSelectedMessages($msg, $imapConnection);
251 $location = set_url_var($location, 'session', $composesession, false);
252 if ($compose_new_win) {
253 $location = set_url_var($location, 'composenew', 1, false);
255 $location = str_replace('search.php','compose.php',$location);
256 $location = str_replace('right_main.php','compose.php',$location);
259 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
260 if ($startMessage > $show_num) {
261 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
263 $location = set_url_var($location,'startMessage',1, false);
270 } else { // Move messages
271 // lets check to see if they selected any messages
272 if (is_array($msg)) {
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.
279 if (isset($msg[$i])) {
285 sqimap_msgs_list_copy($imapConnection,$id,$targetMailbox);
287 $cnt = sqimap_mailbox_expunge($imapConnection, $mailbox, true);
292 if (($startMessage+
$cnt-1) >= $mbx_response['EXISTS']) {
293 if ($startMessage > $show_num) {
294 $location = set_url_var($location,'startMessage',$startMessage-$show_num, false);
296 $location = set_url_var($location,'startMessage',1, false);
303 // Log out this session
304 sqimap_logout($imapConnection);
306 displayPageHeader($color, $mailbox);
307 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
309 header("Location: $location");