Fix Bug #522149 by closing the <center> tag
[squirrelmail.git] / src / move_messages.php
1 <?php
2
3 /**
4 * move_messages.php
5 *
6 * Copyright (c) 1999-2002 The SquirrelMail Project Team
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 */
13
14 require_once('../src/validate.php');
15 require_once('../functions/display_messages.php');
16 require_once('../functions/imap.php');
17
18 global $compose_new_win;
19
20 function 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]) {
30 if ($firstLoop != true) {
31 $selectedMessages .= "&amp;";
32 } else {
33 $firstLoop = false;
34 }
35
36 $selectedMessages .= "selMsg[$j]=$msg[$i]";
37
38 $j++;
39 }
40 $i++;
41 }
42 }
43
44 function attachSelectedMessages($msg, $imapConnection) {
45
46 global $mailbox, $username, $attachment_dir, $attachments, $identity, $data_dir, $composesession;
47
48
49 if (!isset($attachments)) {
50 $attachments = array();
51 session_register('attachments');
52 }
53
54 if (!isset($composesession)) {
55 $composesession = 1;
56 session_register('$composesession');
57 } else {
58 $composesession++;
59 }
60
61 $hashed_attachment_dir = getHashedDir($username, $attachment_dir,$composesession);
62
63 $rem_attachments = array();
64 foreach ($attachments as $info) {
65 if ($info['session'] == $composesession) {
66 $attached_file = "$hashed_attachment_dir/$info[localfilename]";
67 if (file_exists($attached_file)) {
68 unlink($attached_file);
69 }
70 } else {
71 $rem_attachments[] = $info;
72 }
73 }
74
75 $attachments = $rem_attachments;
76
77
78 $i = 0;
79 $j = 0;
80
81 while ($j < count($msg)) {
82 if (isset($msg[$i])) {
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
101 $localfilename = GenerateRandomString(32, 'FILE', 7);
102 $full_localfilename = "$hashed_attachment_dir/$localfilename";
103
104 $fp = fopen( $full_localfilename, 'w');
105 fwrite ($fp, $body);
106 fclose($fp);
107
108 $newAttachment = array();
109 $newAttachment['localfilename'] = $localfilename;
110 $newAttachment['type'] = "message/rfc822";
111 $newAttachment['remotefilename'] = "$subject".".eml";
112 $newAttachment['session'] = $composesession;
113 $attachments[] = $newAttachment;
114 flush();
115 }
116 $j++;
117 }
118 $i++;
119
120 }
121 return $composesession;
122 }
123
124 $imapConnection = sqimap_login($username, $key, $imapServerAddress, $imapPort, 0);
125 sqimap_mailbox_select($imapConnection, $mailbox);
126
127 // expunge-on-demand if user isn't using move_to_trash or auto_expunge
128 if(isset($expungeButton)) {
129 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
130 $location = get_location();
131 if (isset($where) && isset($what)) {
132 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
133 } else {
134 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
135 }
136
137 } elseif(isset($undeleteButton)) {
138 // undelete messages if user isn't using move_to_trash or auto_expunge
139
140 if (is_array($msg) == 1) {
141 // Removes \Deleted flag from selected messages
142 $j = 0;
143 $i = 0;
144
145 // If they have selected nothing msg is size one still, but will be an infinite
146 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
147 while ($j < count($msg)) {
148 if ($msg[$i]) {
149 sqimap_messages_remove_flag ($imapConnection, $msg[$i], $msg[$i], "Deleted");
150 $j++;
151 }
152 $i++;
153 }
154 $location = get_location();
155
156 if ($where && $what)
157 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
158 else
159 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
160 } else {
161 displayPageHeader($color, $mailbox);
162 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
163 }
164 } elseif (!isset($moveButton)) {
165 // If the delete button was pressed, the moveButton variable will not be set.
166 if (is_array($msg) == 1) {
167 // Marks the selected messages as 'Deleted'
168 $j = 0;
169 $i = 0;
170
171 // If they have selected nothing msg is size one still, but will be an infinite
172 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
173 while ($j < count($msg)) {
174 if (isset($msg[$i])) {
175 if (isset($markRead)) {
176 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Seen");
177 } else if (isset($markUnread)) {
178 sqimap_messages_remove_flag($imapConnection, $msg[$i], $msg[$i], "Seen");
179 } else if (isset($attache)) {
180 break;
181 } else {
182 sqimap_messages_delete($imapConnection, $msg[$i], $msg[$i], $mailbox);
183 }
184 $j++;
185 }
186 $i++;
187 }
188 if ($auto_expunge) {
189 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
190 }
191 $location = get_location();
192 if (isset($where) && isset($what)) {
193 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
194 } elseif(isset($attache)) {
195 $composesession = attachSelectedMessages($msg, $imapConnection);
196 if ($compose_new_win == '1') {
197 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox)."&composenew=1&session=$composesession&attachedmessages=true");
198 } else {
199 header ("Location: $location/compose.php?startMessage=$startMessage&mailbox=". urlencode($mailbox)."&session=$composesession&attachedmessages=true");
200 }
201 } else {
202 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
203 }
204 } else {
205 displayPageHeader($color, $mailbox);
206 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
207 }
208 } else { // Move messages
209 // lets check to see if they selected any messages
210 if (is_array($msg) == 1) {
211 $j = 0;
212 $i = 0;
213
214 // If they have selected nothing msg is size one still, but will be an infinite
215 // loop because we never increment j. so check to see if msg[0] is set or not to fix this.
216 while ($j < count($msg)) {
217 if (isset($msg[$i])) {
218 /** check if they would like to move it to the trash folder or not */
219 sqimap_messages_copy($imapConnection, $msg[$i], $msg[$i], $targetMailbox);
220 sqimap_messages_flag($imapConnection, $msg[$i], $msg[$i], "Deleted");
221 $j++;
222 }
223 $i++;
224 }
225 if ($auto_expunge == true)
226 sqimap_mailbox_expunge($imapConnection, $mailbox, true);
227
228 $location = get_location();
229 if (isset($where) && isset($what))
230 header ("Location: $location/search.php?mailbox=".urlencode($mailbox)."&what=".urlencode($what)."&where=".urlencode($where));
231 else
232 header ("Location: $location/right_main.php?sort=$sort&startMessage=$startMessage&mailbox=". urlencode($mailbox));
233 } else {
234 displayPageHeader($color, $mailbox);
235 error_message(_("No messages were selected."), $mailbox, $sort, $startMessage, $color);
236 }
237 }
238
239 // Log out this session
240 sqimap_logout($imapConnection);
241
242 ?>
243 </BODY></HTML>