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