s/Deliver_SentMail/Deliver_SendMail/
[squirrelmail.git] / src / move_messages.php
CommitLineData
59177427 1<?php
895905c0 2
35586184 3/**
4 * move_messages.php
5 *
15e6162e 6 * Copyright (c) 1999-2002 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
35586184 14require_once('../src/validate.php');
15require_once('../functions/display_messages.php');
16require_once('../functions/imap.php');
8c33aaf7 17require_once('../functions/html.php');
acc28bac 18global $compose_new_win;
19
65c3ec94 20function putSelectedMessagesIntoString($msg) {
21 $j = 0;
22 $i = 0;
23 $firstLoop = true;
65c3ec94 24 // If they have selected nothing msg is size one still, but will
25 // be an infinite loop because we never increment j. so check to
26 // see if msg[0] is set or not to fix this.
27 while (($j < count($msg)) && ($msg[0])) {
28 if ($msg[$i]) {
5e9e90fd 29 if ($firstLoop != true) {
30 $selectedMessages .= "&amp;";
31 } else {
32 $firstLoop = false;
33 }
dd88d31f 34 $selectedMessages .= "selMsg[$j]=$msg[$i]";
05207a68 35 $j++;
65c3ec94 36 }
37 $i++;
38 }
39}
40
acc28bac 41function attachSelectedMessages($msg, $imapConnection) {
5b8d68ca 42 global $mailbox, $username, $attachment_dir, $attachments, $identity,
8c33aaf7 43 $data_dir, $composesession, $lastTargetMailbox, $uid_support,
44 $msgs, $startMessage, $show_num, $thread_sort_messages,
45 $allow_server_sort;
acc28bac 46
acc28bac 47 if (!isset($attachments)) {
21f3c131 48 $attachments = array();
49 session_register('attachments');
acc28bac 50 }
51
fee4465d 52 if (!isset($composesession) ) {
21f3c131 53 $composesession = 1;
fee4465d 54 session_register('composesession');
acc28bac 55 } else {
21f3c131 56 $composesession++;
acc28bac 57 }
58
21f3c131 59 $hashed_attachment_dir = getHashedDir($username, $attachment_dir, $composesession);
acc28bac 60
61 $rem_attachments = array();
62 foreach ($attachments as $info) {
21f3c131 63 if ($info['session'] == $composesession) {
acc28bac 64 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
65 if (file_exists($attached_file)) {
21f3c131 66 unlink($attached_file);
acc28bac 67 }
21f3c131 68 } else {
69 $rem_attachments[] = $info;
70 }
acc28bac 71 }
72
73 $attachments = $rem_attachments;
74
8c33aaf7 75 if ($thread_sort_messages || $allow_server_sort) {
76 $start_index=0;
77 } else {
78 $start_index = ($startMessage-1) * $show_num;
79 }
acc28bac 80
81 $i = 0;
82 $j = 0;
21f3c131 83 $hashed_attachment_dir = getHashedDir($username, $attachment_dir);
acc28bac 84 while ($j < count($msg)) {
85 if (isset($msg[$i])) {
21f3c131 86 $id = $msg[$i];
5b8d68ca 87 $body_a = sqimap_run_command($imapConnection, "FETCH $id RFC822",true, $response, $readmessage, $uid_support);
21f3c131 88 if ($response = 'OK') {
8c33aaf7 89 $k = $i + $start_index;
90 $subject = $msgs[$k]['SUBJECT'];
21f3c131 91
8c33aaf7 92 array_shift($body_a);
93 $body = implode('', $body_a);
94 $body .= "\r\n";
21f3c131 95
8c33aaf7 96 $localfilename = GenerateRandomString(32, 'FILE', 7);
97 $full_localfilename = "$hashed_attachment_dir/$localfilename";
98
81d79073 99 $fp = fopen( $full_localfilename, 'wb');
8c33aaf7 100 fwrite ($fp, $body);
101 fclose($fp);
102 $newAttachment = array();
103 $newAttachment['localfilename'] = $localfilename;
104 $newAttachment['type'] = "message/rfc822";
105 $newAttachment['remotefilename'] = $subject.'.eml';
106 $newAttachment['session'] = $composesession;
107 $attachments[] = $newAttachment;
108 flush();
acc28bac 109 }
8c33aaf7 110 $j++;
111 }
112 $i++;
acc28bac 113 }
6540ee5e 114 setPref($data_dir, $username, 'attachments', serialize($attachments));
acc28bac 115 return $composesession;
116}
117
65c3ec94 118$imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
119sqimap_mailbox_select($imapConnection, $mailbox);
120
8c33aaf7 121$location = set_url_var($location,'composenew');
122$location = set_url_var($location,'composesession');
123$location = set_url_var($location,'session');
124
125
cd885078 126/* remember changes to mailbox setting */
127if (!isset($lastTargetMailbox)) {
128 $lastTargetMailbox = 'INBOX';
129}
130if ($targetMailbox != $lastTargetMailbox) {
131 $lastTargetMailbox = $targetMailbox;
132 session_register('lastTargetMailbox');
133}
134
65c3ec94 135// expunge-on-demand if user isn't using move_to_trash or auto_expunge
136if(isset($expungeButton)) {
137 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
8c33aaf7 138 header("Location: $location");
65c3ec94 139} elseif(isset($undeleteButton)) {
140 // undelete messages if user isn't using move_to_trash or auto_expunge
141
142 if (is_array($msg) == 1) {
143 // Removes \Deleted flag from selected messages
144 $j = 0;
145 $i = 0;
65c3ec94 146 // If they have selected nothing msg is size one still, but will be an infinite
147 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
148 while ($j < count($msg)) {
f9b3e5d9 149 if ($msg[$i]) {
d8a8203a 150 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
8c33aaf7 151 $j++;
f9b3e5d9 152 }
153 $i++;
65c3ec94 154 }
8c33aaf7 155 header ("Location: $location");
65c3ec94 156 } else {
157 displayPageHeader($color, $mailbox);
158 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
159 }
160} elseif (!isset($moveButton)) {
161 // If the delete button was pressed, the moveButton variable will not be set.
162 if (is_array($msg) == 1) {
163 // Marks the selected messages as 'Deleted'
164 $j = 0;
165 $i = 0;
65c3ec94 166 // If they have selected nothing msg is size one still, but will be an infinite
167 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
168 while ($j < count($msg)) {
23fd3c8e 169 if (isset($msg[$i])) {
65c3ec94 170 if (isset($markRead)) {
d8a8203a 171 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
65c3ec94 172 } else if (isset($markUnread)) {
d8a8203a 173 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen", true);
acc28bac 174 } else if (isset($attache)) {
175 break;
176 } else {
65c3ec94 177 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
178 }
179 $j++;
2d7d3c2a 180 }
dd88d31f 181 $i++;
65c3ec94 182 }
183 if ($auto_expunge) {
8cf653ad 184 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
65c3ec94 185 }
8c33aaf7 186 if (isset($attache)) {
acc28bac 187 $composesession = attachSelectedMessages($msg, $imapConnection);
8c33aaf7 188 if ($compose_new_win) {
189 header ("Location: $location&composenew=1&session=$composesession");
acc28bac 190 } else {
8c33aaf7 191 $location = str_replace('search.php','compose.php',$location);
192 $location = str_replace('right_main.php','compose.php',$location);
193 header ("Location: $location&session=$composesession");
acc28bac 194 }
8c33aaf7 195 } else {
196 header ("Location: $location");
197 }
65c3ec94 198 } else {
199 displayPageHeader($color, $mailbox);
200 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
201 }
202} else { // Move messages
203 // lets check to see if they selected any messages
204 if (is_array($msg) == 1) {
205 $j = 0;
206 $i = 0;
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)) {
23fd3c8e 210 if (isset($msg[$i])) {
65c3ec94 211 /** check if they would like to move it to the trash folder or not */
212 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
d8a8203a 213 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted", true);
65c3ec94 214 $j++;
dd88d31f 215 }
216 $i++;
65c3ec94 217 }
8c33aaf7 218 if ($auto_expunge) {
8cf653ad 219 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
8c33aaf7 220 }
221 header ("Location: $location");
65c3ec94 222 } else {
223 displayPageHeader($color, $mailbox);
224 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
225 }
226}
65c3ec94 227// Log out this session
228sqimap_logout($imapConnection);
b40316f9 229
2aa12d5e 230?>
6332704d 231</BODY></HTML>